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