]> Pileus Git - ~andy/gtk/blob - gtk/gtkappchooserbutton.c
c01ce9b1f0ece8aed3aaff3ff8b74f0c04216ad7
[~andy/gtk] / gtk / gtkappchooserbutton.c
1 /*
2  * gtkappchooserbutton.h: an app-chooser combobox
3  *
4  * Copyright (C) 2010 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with the Gnome Library; see the file COPYING.LIB.  If not,
18  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Cosimo Cecchi <ccecchi@redhat.com>
22  */
23
24 /**
25  * SECTION:gtkappchooserbutton
26  * @Title: GtkAppChooserButton
27  * @Short_description: A button to launch an application chooser dialog
28  *
29  * The #GtkAppChooserButton is a widget that lets the user select
30  * an application. It implements the #GtkAppChooser interface.
31  */
32 #include "config.h"
33
34 #include "gtkappchooserbutton.h"
35
36 #include "gtkappchooser.h"
37 #include "gtkappchooserdialog.h"
38 #include "gtkappchooserprivate.h"
39 #include "gtkcelllayout.h"
40 #include "gtkcellrendererpixbuf.h"
41 #include "gtkcellrenderertext.h"
42 #include "gtkcombobox.h"
43 #include "gtkdialog.h"
44 #include "gtkintl.h"
45 #include "gtkmarshalers.h"
46
47 enum {
48   PROP_CONTENT_TYPE = 1,
49   PROP_SHOW_DIALOG_ITEM,
50 };
51
52 enum {
53   SIGNAL_CUSTOM_ITEM_ACTIVATED,
54   NUM_SIGNALS
55 };
56
57 enum {
58   COLUMN_APP_INFO,
59   COLUMN_NAME,
60   COLUMN_LABEL,
61   COLUMN_ICON,
62   COLUMN_CUSTOM,
63   COLUMN_SEPARATOR,
64   NUM_COLUMNS,
65 };
66
67 #define CUSTOM_ITEM_OTHER_APP "gtk-internal-item-other-app"
68
69 static void app_chooser_iface_init (GtkAppChooserIface *iface);
70
71 static void real_insert_custom_item (GtkAppChooserButton *self,
72                                      const gchar *name,
73                                      const gchar *label,
74                                      GIcon *icon,
75                                      gboolean custom,
76                                      GtkTreeIter *iter);
77
78 static void real_insert_separator (GtkAppChooserButton *self,
79                                    gboolean custom,
80                                    GtkTreeIter *iter);
81
82 static guint signals[NUM_SIGNALS] = { 0, };
83
84 G_DEFINE_TYPE_WITH_CODE (GtkAppChooserButton, gtk_app_chooser_button, GTK_TYPE_COMBO_BOX,
85                          G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
86                                                 app_chooser_iface_init));
87
88 struct _GtkAppChooserButtonPrivate {
89   GtkListStore *store;
90
91   gchar *content_type;
92   gboolean show_dialog_item;
93
94   GHashTable *custom_item_names;
95 };
96
97 static gboolean
98 row_separator_func (GtkTreeModel *model,
99                     GtkTreeIter  *iter,
100                     gpointer      user_data)
101 {
102   gboolean separator;
103
104   gtk_tree_model_get (model, iter,
105                       COLUMN_SEPARATOR, &separator,
106                       -1);
107
108   return separator;
109 }
110
111 static void
112 get_first_iter (GtkListStore *store,
113                 GtkTreeIter  *iter)
114 {
115   GtkTreeIter iter2;
116
117   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), iter))
118     {
119       /* the model is empty, append */
120       gtk_list_store_append (store, iter);
121     }
122   else
123     {
124       gtk_list_store_insert_before (store, &iter2, iter);
125       *iter = iter2;
126     }
127 }
128
129 typedef struct {
130   GtkAppChooserButton *self;
131   GAppInfo *info;
132   gint active_index;
133 } SelectAppData;
134
135 static void
136 select_app_data_free (SelectAppData *data)
137 {
138   g_clear_object (&data->self);
139   g_clear_object (&data->info);
140
141   g_slice_free (SelectAppData, data);
142 }
143
144 static gboolean
145 select_application_func_cb (GtkTreeModel *model,
146                             GtkTreePath *path,
147                             GtkTreeIter *iter,
148                             gpointer user_data)
149 {
150   SelectAppData *data = user_data;
151   GAppInfo *app_to_match = data->info, *app = NULL;
152   gboolean custom;
153
154   gtk_tree_model_get (model, iter,
155                       COLUMN_APP_INFO, &app,
156                       COLUMN_CUSTOM, &custom,
157                       -1);
158
159   /* cutsom items are always after GAppInfos, so iterating further here
160    * is just useless.
161    */
162   if (custom)
163     return TRUE;
164
165   if (g_app_info_equal (app, app_to_match))
166     {
167       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (data->self), iter);
168       return TRUE;
169     }
170
171   return FALSE;
172 }
173
174 static void
175 gtk_app_chooser_button_select_application (GtkAppChooserButton *self,
176                                            GAppInfo *info)
177 {
178   SelectAppData *data;
179
180   data = g_slice_new0 (SelectAppData);
181   data->self = g_object_ref (self);
182   data->info = g_object_ref (info);
183
184   gtk_tree_model_foreach (GTK_TREE_MODEL (self->priv->store),
185                           select_application_func_cb, data);
186
187   select_app_data_free (data);
188 }
189
190 static void
191 other_application_dialog_response_cb (GtkDialog *dialog,
192                                       gint response_id,
193                                       gpointer user_data)
194 {
195   GtkAppChooserButton *self = user_data;
196   GAppInfo *info;
197
198   if (response_id != GTK_RESPONSE_OK)
199     {
200       /* reset the active item, otherwise we are stuck on
201        * 'Other application...'
202        */
203       gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
204       gtk_widget_destroy (GTK_WIDGET (dialog));
205       return;
206     }
207
208   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (dialog));
209
210   /* refresh the combobox to get the new application */
211   gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
212   gtk_app_chooser_button_select_application (self, info);
213
214   g_object_unref (info);
215 }
216
217 static void
218 other_application_item_activated_cb (GtkAppChooserButton *self)
219 {
220   GtkWidget *dialog, *widget;
221   GtkWindow *toplevel;
222
223   toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self)));
224   dialog = gtk_app_chooser_dialog_new_for_content_type (toplevel, GTK_DIALOG_DESTROY_WITH_PARENT,
225                                                         self->priv->content_type);
226
227   gtk_window_set_modal (GTK_WINDOW (dialog), gtk_window_get_modal (toplevel));
228
229   widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
230   g_object_set (widget,
231                 "show-fallback", TRUE,
232                 "show-other", TRUE,
233                 NULL);
234   gtk_widget_show (dialog);
235
236   g_signal_connect (dialog, "response",
237                     G_CALLBACK (other_application_dialog_response_cb), self);
238 }
239
240 static void
241 gtk_app_chooser_button_ensure_dialog_item (GtkAppChooserButton *self,
242                                            GtkTreeIter *prev_iter)
243 {
244   GtkTreeIter iter, iter2;
245
246   if (!self->priv->show_dialog_item)
247     return;
248
249   if (prev_iter == NULL)
250     gtk_list_store_append (self->priv->store, &iter);
251   else
252     gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);
253
254   real_insert_separator (self, FALSE, &iter);
255   iter2 = iter;
256
257   gtk_list_store_insert_after (self->priv->store, &iter, &iter2);
258   real_insert_custom_item (self, CUSTOM_ITEM_OTHER_APP,
259                            _("Other application..."), NULL,
260                            FALSE, &iter);
261 }
262
263 static void
264 gtk_app_chooser_button_populate (GtkAppChooserButton *self)
265 {
266   GList *recommended_apps = NULL, *l;
267   GAppInfo *app;
268   GtkTreeIter iter, iter2;
269   GIcon *icon;
270   gboolean cycled_recommended;
271
272 #ifndef G_OS_WIN32
273   recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
274 #endif
275   cycled_recommended = FALSE;
276
277   for (l = recommended_apps; l != NULL; l = l->next)
278     {
279       app = l->data;
280
281       icon = g_app_info_get_icon (app);
282
283       if (icon == NULL)
284         icon = g_themed_icon_new ("application-x-executable");
285       else
286         g_object_ref (icon);
287
288       if (cycled_recommended)
289         {
290           gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
291           iter = iter2;
292         }
293       else
294         {
295           get_first_iter (self->priv->store, &iter);
296           cycled_recommended = TRUE;
297         }
298
299       gtk_list_store_set (self->priv->store, &iter,
300                           COLUMN_APP_INFO, app,
301                           COLUMN_LABEL, g_app_info_get_display_name (app),
302                           COLUMN_ICON, icon,
303                           COLUMN_CUSTOM, FALSE,
304                           -1);
305
306       g_object_unref (icon);
307     }
308
309   if (!cycled_recommended)
310     gtk_app_chooser_button_ensure_dialog_item (self, NULL);
311   else
312     gtk_app_chooser_button_ensure_dialog_item (self, &iter);
313
314   gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
315 }
316
317 static void
318 gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
319 {
320   GtkCellRenderer *cell;
321   GtkCellArea *area;
322
323   self->priv->store = gtk_list_store_new (NUM_COLUMNS,
324                                           G_TYPE_APP_INFO,
325                                           G_TYPE_STRING, /* name */
326                                           G_TYPE_STRING, /* label */
327                                           G_TYPE_ICON,
328                                           G_TYPE_BOOLEAN, /* separator */
329                                           G_TYPE_BOOLEAN); /* custom */
330
331   gtk_combo_box_set_model (GTK_COMBO_BOX (self),
332                            GTK_TREE_MODEL (self->priv->store));
333
334   area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (self));
335
336   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
337                                         row_separator_func, NULL, NULL);
338
339   cell = gtk_cell_renderer_pixbuf_new ();
340   gtk_cell_area_add_with_properties (area, cell,
341                                      "align", FALSE,
342                                      "expand", FALSE,
343                                      "fixed-size", FALSE,
344                                      NULL);
345   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
346                                   "gicon", COLUMN_ICON,
347                                   NULL);
348
349   cell = gtk_cell_renderer_text_new ();
350   gtk_cell_area_add_with_properties (area, cell,
351                                      "align", FALSE,
352                                      "expand", TRUE,
353                                      NULL);
354   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
355                                   "text", COLUMN_LABEL,
356                                   NULL);
357
358   gtk_app_chooser_button_populate (self);
359 }
360
361 static void
362 gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
363 {
364   GtkTreeModel *model;
365   GtkTreeIter iter;
366   gboolean custom, res;
367
368   model = GTK_TREE_MODEL (self->priv->store);
369
370   if (!gtk_tree_model_get_iter_first (model, &iter))
371     return;
372
373   do {
374     gtk_tree_model_get (model, &iter,
375                         COLUMN_CUSTOM, &custom,
376                         -1);
377     if (custom)
378       res = gtk_tree_model_iter_next (model, &iter);
379     else
380       res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
381   } while (res);
382 }
383
384 static void
385 gtk_app_chooser_button_changed (GtkComboBox *object)
386 {
387   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
388   GtkTreeIter iter;
389   gchar *name = NULL;
390   gboolean custom;
391   GQuark name_quark;
392
393   if (!gtk_combo_box_get_active_iter (object, &iter))
394     return;
395
396   gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
397                       COLUMN_NAME, &name,
398                       COLUMN_CUSTOM, &custom,
399                       -1);
400
401   if (name != NULL)
402     {
403       if (custom)
404         {
405           name_quark = g_quark_from_string (name);
406           g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
407         }
408       else
409         {
410           /* trigger the dialog internally */
411           other_application_item_activated_cb (self);
412         }
413
414       g_free (name);
415     }
416 }
417
418 static void
419 gtk_app_chooser_button_refresh (GtkAppChooser *object)
420 {
421   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
422
423   gtk_app_chooser_button_remove_non_custom (self);
424   gtk_app_chooser_button_populate (self);
425 }
426
427 static GAppInfo *
428 gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
429 {
430   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
431   GtkTreeIter iter;
432   GAppInfo *info;
433
434   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
435     return NULL;
436
437   gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
438                       COLUMN_APP_INFO, &info,
439                       -1);
440
441   return info;
442 }
443
444 static void
445 gtk_app_chooser_button_constructed (GObject *obj)
446 {
447   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
448
449   if (G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed != NULL)
450     G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed (obj);
451
452   g_assert (self->priv->content_type != NULL);
453
454   gtk_app_chooser_button_build_ui (self);
455 }
456
457 static void
458 gtk_app_chooser_button_set_property (GObject      *obj,
459                                      guint         property_id,
460                                      const GValue *value,
461                                      GParamSpec   *pspec)
462 {
463   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
464
465   switch (property_id)
466     {
467     case PROP_CONTENT_TYPE:
468       self->priv->content_type = g_value_dup_string (value);
469       break;
470     case PROP_SHOW_DIALOG_ITEM:
471       gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
472       break;
473     default:
474       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
475       break;
476     }
477 }
478
479 static void
480 gtk_app_chooser_button_get_property (GObject    *obj,
481                                      guint       property_id,
482                                      GValue     *value,
483                                      GParamSpec *pspec)
484 {
485   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
486
487   switch (property_id)
488     {
489     case PROP_CONTENT_TYPE:
490       g_value_set_string (value, self->priv->content_type);
491       break;
492     case PROP_SHOW_DIALOG_ITEM:
493       g_value_set_boolean (value, self->priv->show_dialog_item);
494       break;
495     default:
496       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
497       break;
498     }
499 }
500
501 static void
502 gtk_app_chooser_button_finalize (GObject *obj)
503 {
504   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
505
506   g_hash_table_destroy (self->priv->custom_item_names);
507   g_free (self->priv->content_type);
508
509   G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
510 }
511
512 static void
513 app_chooser_iface_init (GtkAppChooserIface *iface)
514 {
515   iface->get_app_info = gtk_app_chooser_button_get_app_info;
516   iface->refresh = gtk_app_chooser_button_refresh;
517 }
518
519 static void
520 gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
521 {
522   GObjectClass *oclass = G_OBJECT_CLASS (klass);
523   GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
524   GParamSpec *pspec;
525
526   oclass->set_property = gtk_app_chooser_button_set_property;
527   oclass->get_property = gtk_app_chooser_button_get_property;
528   oclass->finalize = gtk_app_chooser_button_finalize;
529   oclass->constructed = gtk_app_chooser_button_constructed;
530
531   combo_class->changed = gtk_app_chooser_button_changed;
532
533   g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");
534
535   /**
536    * GtkAppChooserButton:show-dialog-item:
537    *
538    * The #GtkAppChooserButton:show-dialog-item property determines whether the dropdown menu
539    * should show an item that triggers a #GtkAppChooserDialog when clicked.
540    */
541   pspec =
542     g_param_spec_boolean ("show-dialog-item",
543                           P_("Include an 'Other...' item"),
544                           P_("Whether the combobox should include an item that triggers a GtkAppChooserDialog"),
545                           FALSE,
546                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
547   g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
548
549   /**
550    * GtkAppChooserButton::custom-item-activated:
551    * @self: the object which received the signal
552    * @item_name: the name of the activated item
553    *
554    * Emitted when a custom item, previously added with
555    * gtk_app_chooser_button_append_custom_item(), is activated from the
556    * dropdown menu.
557    */
558   signals[SIGNAL_CUSTOM_ITEM_ACTIVATED] =
559     g_signal_new ("custom-item-activated",
560                   GTK_TYPE_APP_CHOOSER_BUTTON,
561                   G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
562                   G_STRUCT_OFFSET (GtkAppChooserButtonClass, custom_item_activated),
563                   NULL, NULL,
564                   _gtk_marshal_VOID__STRING,
565                   G_TYPE_NONE,
566                   1, G_TYPE_STRING);
567
568   g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
569 }
570
571 static void
572 gtk_app_chooser_button_init (GtkAppChooserButton *self)
573 {
574   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_BUTTON,
575                                             GtkAppChooserButtonPrivate);
576   self->priv->custom_item_names =
577     g_hash_table_new_full (g_str_hash, g_str_equal,
578                            g_free, NULL);
579 }
580
581 static gboolean
582 app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
583                                           const gchar *name,
584                                           GtkTreeIter *set_me)
585 {
586   GtkTreeIter iter;
587   gchar *custom_name = NULL;
588
589   if (!gtk_tree_model_get_iter_first
590       (GTK_TREE_MODEL (self->priv->store), &iter))
591     return FALSE;
592
593   do {
594     gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
595                         COLUMN_NAME, &custom_name,
596                         -1);
597
598     if (g_strcmp0 (custom_name, name) == 0)
599       {
600         g_free (custom_name);
601         *set_me = iter;
602
603         return TRUE;
604       }
605
606     g_free (custom_name);
607   } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));
608
609   return FALSE;
610 }
611
612 static void
613 real_insert_custom_item (GtkAppChooserButton *self,
614                          const gchar *name,
615                          const gchar *label,
616                          GIcon *icon,
617                          gboolean custom,
618                          GtkTreeIter *iter)
619 {
620   if (custom)
621     {
622       if (g_hash_table_lookup (self->priv->custom_item_names,
623                                name) != NULL)
624         {
625           g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
626                      "when there's already an item with the same name", name);
627           return;
628         }
629
630       g_hash_table_insert (self->priv->custom_item_names,
631                            g_strdup (name), GINT_TO_POINTER (1));
632     }
633
634   gtk_list_store_set (self->priv->store, iter,
635                       COLUMN_NAME, name,
636                       COLUMN_LABEL, label,
637                       COLUMN_ICON, icon,
638                       COLUMN_CUSTOM, custom,
639                       COLUMN_SEPARATOR, FALSE,
640                       -1);
641 }
642
643 static void
644 real_insert_separator (GtkAppChooserButton *self,
645                        gboolean custom,
646                        GtkTreeIter *iter)
647 {
648   gtk_list_store_set (self->priv->store, iter,
649                       COLUMN_CUSTOM, custom,
650                       COLUMN_SEPARATOR, TRUE,
651                       -1);
652 }
653
654 /**
655  * gtk_app_chooser_button_new:
656  * @content_type: the content type to show applications for
657  *
658  * Creates a new #GtkAppChooserButton for applications
659  * that can handle content of the given type.
660  *
661  * Returns: a newly created #GtkAppChooserButton
662  *
663  * Since: 3.0
664  */
665 GtkWidget *
666 gtk_app_chooser_button_new (const gchar *content_type)
667 {
668   g_return_val_if_fail (content_type != NULL, NULL);
669
670   return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
671                        "content-type", content_type,
672                        NULL);
673 }
674
675 /**
676  * gtk_app_chooser_button_append_separator:
677  * @self: a #GtkAppChooserButton
678  *
679  * Appends a separator to the list of applications that is shown
680  * in the popup.
681  *
682  * Since: 3.0
683  */
684 void
685 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
686 {
687   GtkTreeIter iter;
688
689   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
690
691   gtk_list_store_append (self->priv->store, &iter);
692   real_insert_separator (self, TRUE, &iter);
693 }
694
695 /**
696  * gtk_app_chooser_button_append_custom_item:
697  * @self: a #GtkAppChooserButton
698  * @name: the name of the custom item
699  * @label: the label for the custom item
700  * @icon: the icon for the custom item
701  *
702  * Appends a custom item to the list of applications that is shown
703  * in the popup; the item name must be unique per-widget.
704  * Clients can use the provided name as a detail for the ::custom-item-activated
705  * signal, to add a callback for the activation of a particular
706  * custom item in the list.
707  * See also gtk_app_chooser_button_append_separator().
708  *
709  * Since: 3.0
710  */
711 void
712 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
713                                            const gchar         *name,
714                                            const gchar         *label,
715                                            GIcon               *icon)
716 {
717   GtkTreeIter iter;
718
719   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
720   g_return_if_fail (name != NULL);
721
722   gtk_list_store_append (self->priv->store, &iter);
723   real_insert_custom_item (self, name, label, icon, TRUE, &iter);
724 }
725
726 /**
727  * gtk_app_chooser_button_select_custom_item:
728  * @self: a #GtkAppChooserButton
729  * @name: the name of the custom item
730  *
731  * Selects a custom item previously added with
732  * gtk_app_chooser_button_append_custom_item().
733  * Use gtk_app_chooser_refresh() to bring the selection to its initial
734  * state.
735  *
736  * Since: 3.0
737  */
738 void
739 gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
740                                                const gchar         *name)
741 {
742   GtkTreeIter iter;
743
744   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
745   g_return_if_fail (name != NULL);
746
747   if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
748       !app_chooser_button_iter_from_custom_name (self, name, &iter))
749     {
750       g_warning ("Can't find the item named %s in the app chooser.",
751                  name);
752       return;
753     }
754
755   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
756 }
757
758 /**
759  * gtk_app_chooser_button_get_show_dialog_item:
760  * @self: a #GtkAppChooserButton
761  *
762  * Returns the current value of the #GtkAppChooserButton:show-dialog-item
763  * property.
764  *
765  * Returns: the value of #GtkAppChooserButton:show-dialog-item
766  *
767  * Since: 3.0
768  */
769 gboolean
770 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
771 {
772   g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
773
774   return self->priv->show_dialog_item;
775 }
776
777 /**
778  * gtk_app_chooser_button_set_show_dialog_item:
779  * @self: a #GtkAppChooserButton
780  * @setting: the new value for #GtkAppChooserButton:show-dialog-item
781  *
782  * Sets whether the dropdown menu of this button should show an
783  * entry to trigger a #GtkAppChooserDialog.
784  *
785  * Since: 3.0
786  */
787 void
788 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
789                                              gboolean setting)
790 {
791   if (self->priv->show_dialog_item != setting)
792     {
793       self->priv->show_dialog_item = setting;
794
795       g_object_notify (G_OBJECT (self), "show-dialog-item");
796
797       gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
798     }
799 }