]> Pileus Git - ~andy/gtk/blob - gtk/gtkappchooserbutton.c
Merge branch 'master' into broadway
[~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, iter2;
235
236   if (!self->priv->show_dialog_item)
237     return;
238
239   icon = g_themed_icon_new ("application-x-executable");
240
241   if (prev_iter == NULL)
242     gtk_list_store_append (self->priv->store, &iter);
243   else
244     gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);
245
246   real_insert_separator (self, FALSE, &iter);
247   iter2 = iter;
248
249   gtk_list_store_insert_after (self->priv->store, &iter, &iter2);
250   real_insert_custom_item (self, CUSTOM_ITEM_OTHER_APP,
251                            _("Other application..."), icon,
252                            FALSE, &iter);
253
254   g_object_unref (icon);
255 }
256
257 static void
258 gtk_app_chooser_button_populate (GtkAppChooserButton *self)
259 {
260   GList *recommended_apps = NULL, *l;
261   GAppInfo *app;
262   GtkTreeIter iter, iter2;
263   GIcon *icon;
264   gboolean cycled_recommended;
265
266   recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
267   cycled_recommended = FALSE;
268
269   for (l = recommended_apps; l != NULL; l = l->next)
270     {
271       app = l->data;
272
273       icon = g_app_info_get_icon (app);
274
275       if (icon == NULL)
276         icon = g_themed_icon_new ("application-x-executable");
277       else
278         g_object_ref (icon);
279
280       if (cycled_recommended)
281         {
282           gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
283           iter = iter2;
284         }
285       else
286         {
287           get_first_iter (self->priv->store, &iter);
288           cycled_recommended = TRUE;
289         }
290
291       gtk_list_store_set (self->priv->store, &iter,
292                           COLUMN_APP_INFO, app,
293                           COLUMN_LABEL, g_app_info_get_display_name (app),
294                           COLUMN_ICON, icon,
295                           COLUMN_CUSTOM, FALSE,
296                           -1);
297
298       g_object_unref (icon);
299     }
300
301   if (!cycled_recommended)
302     gtk_app_chooser_button_ensure_dialog_item (self, NULL);
303   else
304     gtk_app_chooser_button_ensure_dialog_item (self, &iter);
305
306   gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
307 }
308
309 static void
310 gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
311 {
312   GtkCellRenderer *cell;
313
314   self->priv->store = gtk_list_store_new (NUM_COLUMNS,
315                                           G_TYPE_APP_INFO,
316                                           G_TYPE_STRING, /* name */
317                                           G_TYPE_STRING, /* label */
318                                           G_TYPE_ICON,
319                                           G_TYPE_BOOLEAN, /* separator */
320                                           G_TYPE_BOOLEAN); /* custom */
321
322   gtk_combo_box_set_model (GTK_COMBO_BOX (self),
323                            GTK_TREE_MODEL (self->priv->store));
324
325   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
326                                         row_separator_func, NULL, NULL);
327
328   cell = gtk_cell_renderer_pixbuf_new ();
329   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
330   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
331                                   "gicon", COLUMN_ICON,
332                                   NULL);
333
334   cell = gtk_cell_renderer_text_new ();
335   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, TRUE);
336   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
337                                   "text", COLUMN_LABEL,
338                                   NULL);
339   g_object_set (cell,
340                 "xpad", 6,
341                 NULL);
342
343   gtk_app_chooser_button_populate (self);
344 }
345
346 static void
347 gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
348 {
349   GtkTreeModel *model;
350   GtkTreeIter iter;
351   gboolean custom, res;
352
353   model = GTK_TREE_MODEL (self->priv->store);
354
355   if (!gtk_tree_model_get_iter_first (model, &iter))
356     return;
357
358   do {
359     gtk_tree_model_get (model, &iter,
360                         COLUMN_CUSTOM, &custom,
361                         -1);
362     if (custom)
363       res = gtk_tree_model_iter_next (model, &iter);
364     else
365       res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
366   } while (res);
367 }
368
369 static void
370 gtk_app_chooser_button_changed (GtkComboBox *object)
371 {
372   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
373   GtkTreeIter iter;
374   gchar *name = NULL;
375   gboolean custom;
376   GQuark name_quark;
377
378   if (!gtk_combo_box_get_active_iter (object, &iter))
379     return;
380
381   gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
382                       COLUMN_NAME, &name,
383                       COLUMN_CUSTOM, &custom,
384                       -1);
385
386   if (name != NULL)
387     {
388       if (custom)
389         {
390           name_quark = g_quark_from_string (name);
391           g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
392         }
393       else
394         {
395           /* trigger the dialog internally */
396           other_application_item_activated_cb (self);
397         }
398
399       g_free (name);
400     }
401 }
402
403 static void
404 gtk_app_chooser_button_refresh (GtkAppChooser *object)
405 {
406   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
407
408   gtk_app_chooser_button_remove_non_custom (self);
409   gtk_app_chooser_button_populate (self);
410 }
411
412 static GAppInfo *
413 gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
414 {
415   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
416   GtkTreeIter iter;
417   GAppInfo *info;
418
419   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
420     return NULL;
421
422   gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
423                       COLUMN_APP_INFO, &info,
424                       -1);
425
426   return info;
427 }
428
429 static void
430 gtk_app_chooser_button_constructed (GObject *obj)
431 {
432   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
433
434   if (G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed != NULL)
435     G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed (obj);
436
437   g_assert (self->priv->content_type != NULL);
438
439   gtk_app_chooser_button_build_ui (self);
440 }
441
442 static void
443 gtk_app_chooser_button_set_property (GObject      *obj,
444                                      guint         property_id,
445                                      const GValue *value,
446                                      GParamSpec   *pspec)
447 {
448   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
449
450   switch (property_id)
451     {
452     case PROP_CONTENT_TYPE:
453       self->priv->content_type = g_value_dup_string (value);
454       break;
455     case PROP_SHOW_DIALOG_ITEM:
456       gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
457       break;
458     default:
459       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
460       break;
461     }
462 }
463
464 static void
465 gtk_app_chooser_button_get_property (GObject    *obj,
466                                      guint       property_id,
467                                      GValue     *value,
468                                      GParamSpec *pspec)
469 {
470   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
471
472   switch (property_id)
473     {
474     case PROP_CONTENT_TYPE:
475       g_value_set_string (value, self->priv->content_type);
476       break;
477     case PROP_SHOW_DIALOG_ITEM:
478       g_value_set_boolean (value, self->priv->show_dialog_item);
479       break;
480     default:
481       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
482       break;
483     }
484 }
485
486 static void
487 gtk_app_chooser_button_finalize (GObject *obj)
488 {
489   GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
490
491   g_hash_table_destroy (self->priv->custom_item_names);
492   g_free (self->priv->content_type);
493
494   G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
495 }
496
497 static void
498 app_chooser_iface_init (GtkAppChooserIface *iface)
499 {
500   iface->get_app_info = gtk_app_chooser_button_get_app_info;
501   iface->refresh = gtk_app_chooser_button_refresh;
502 }
503
504 static void
505 gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
506 {
507   GObjectClass *oclass = G_OBJECT_CLASS (klass);
508   GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
509   GParamSpec *pspec;
510
511   oclass->set_property = gtk_app_chooser_button_set_property;
512   oclass->get_property = gtk_app_chooser_button_get_property;
513   oclass->finalize = gtk_app_chooser_button_finalize;
514   oclass->constructed = gtk_app_chooser_button_constructed;
515
516   combo_class->changed = gtk_app_chooser_button_changed;
517
518   g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");
519
520   /**
521    * GtkAppChooserButton:show-dialog-item:
522    *
523    * The #GtkAppChooserButton:show-dialog-item property determines whether the dropdown menu
524    * should show an item that triggers a #GtkAppChooserDialog when clicked.
525    */
526   pspec =
527     g_param_spec_boolean ("show-dialog-item",
528                           P_("Include an 'Other...' item"),
529                           P_("Whether the combobox should include an item that triggers a GtkAppChooserDialog"),
530                           FALSE,
531                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
532   g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
533
534   /**
535    * GtkAppChooserButton::custom-item-activated:
536    * @self: the object which received the signal
537    * @item_name: the name of the activated item
538    *
539    * Emitted when a custom item, previously added with
540    * gtk_app_chooser_button_append_custom_item(), is activated from the
541    * dropdown menu.
542    */
543   signals[SIGNAL_CUSTOM_ITEM_ACTIVATED] =
544     g_signal_new ("custom-item-activated",
545                   GTK_TYPE_APP_CHOOSER_BUTTON,
546                   G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
547                   G_STRUCT_OFFSET (GtkAppChooserButtonClass, custom_item_activated),
548                   NULL, NULL,
549                   _gtk_marshal_VOID__STRING,
550                   G_TYPE_NONE,
551                   1, G_TYPE_STRING);
552
553   g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
554 }
555
556 static void
557 gtk_app_chooser_button_init (GtkAppChooserButton *self)
558 {
559   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_BUTTON,
560                                             GtkAppChooserButtonPrivate);
561   self->priv->custom_item_names =
562     g_hash_table_new_full (g_str_hash, g_str_equal,
563                            g_free, NULL);
564 }
565
566 static gboolean
567 app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
568                                           const gchar *name,
569                                           GtkTreeIter *set_me)
570 {
571   GtkTreeIter iter;
572   gchar *custom_name = NULL;
573
574   if (!gtk_tree_model_get_iter_first
575       (GTK_TREE_MODEL (self->priv->store), &iter))
576     return FALSE;
577
578   do {
579     gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
580                         COLUMN_NAME, &custom_name,
581                         -1);
582
583     if (g_strcmp0 (custom_name, name) == 0)
584       {
585         g_free (custom_name);
586         *set_me = iter;
587
588         return TRUE;
589       }
590
591     g_free (custom_name);
592   } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));
593
594   return FALSE;
595 }
596
597 static void
598 real_insert_custom_item (GtkAppChooserButton *self,
599                          const gchar *name,
600                          const gchar *label,
601                          GIcon *icon,
602                          gboolean custom,
603                          GtkTreeIter *iter)
604 {
605   if (custom)
606     {
607       if (g_hash_table_lookup (self->priv->custom_item_names,
608                                name) != NULL)
609         {
610           g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
611                      "when there's already an item with the same name", name);
612           return;
613         }
614
615       g_hash_table_insert (self->priv->custom_item_names,
616                            g_strdup (name), GINT_TO_POINTER (1));
617     }
618
619   gtk_list_store_set (self->priv->store, iter,
620                       COLUMN_NAME, name,
621                       COLUMN_LABEL, label,
622                       COLUMN_ICON, icon,
623                       COLUMN_CUSTOM, custom,
624                       COLUMN_SEPARATOR, FALSE,
625                       -1);
626 }
627
628 static void
629 real_insert_separator (GtkAppChooserButton *self,
630                        gboolean custom,
631                        GtkTreeIter *iter)
632 {
633   gtk_list_store_set (self->priv->store, iter,
634                       COLUMN_CUSTOM, custom,
635                       COLUMN_SEPARATOR, TRUE,
636                       -1);
637 }
638
639 /**
640  * gtk_app_chooser_button_new:
641  * @content_type: the content type to show applications for
642  *
643  * Creates a new #GtkAppChooserButton for applications
644  * that can handle content of the given type.
645  *
646  * Returns: a newly created #GtkAppChooserButton
647  *
648  * Since: 3.0
649  */
650 GtkWidget *
651 gtk_app_chooser_button_new (const gchar *content_type)
652 {
653   g_return_val_if_fail (content_type != NULL, NULL);
654
655   return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
656                        "content-type", content_type,
657                        NULL);
658 }
659
660 /**
661  * gtk_app_chooser_button_append_separator:
662  * @self: a #GtkAppChooserButton
663  *
664  * Appends a separator to the list of applications that is shown
665  * in the popup.
666  *
667  * Since: 3.0
668  */
669 void
670 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
671 {
672   GtkTreeIter iter;
673
674   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
675
676   gtk_list_store_append (self->priv->store, &iter);
677   real_insert_separator (self, TRUE, &iter);
678 }
679
680 /**
681  * gtk_app_chooser_button_append_custom_item:
682  * @self: a #GtkAppChooserButton
683  * @name: the name of the custom item
684  * @label: the label for the custom item
685  * @icon: the icon for the custom item
686  *
687  * Appends a custom item to the list of applications that is shown
688  * in the popup; the item name must be unique per-widget.
689  * Clients can use the provided name as a detail for the ::custom-item-activated
690  * signal, to add a callback for the activation of a particular
691  * custom item in the list.
692  * See also gtk_app_chooser_button_append_separator().
693  *
694  * Since: 3.0
695  */
696 void
697 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
698                                            const gchar         *name,
699                                            const gchar         *label,
700                                            GIcon               *icon)
701 {
702   GtkTreeIter iter;
703
704   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
705   g_return_if_fail (name != NULL);
706
707   gtk_list_store_append (self->priv->store, &iter);
708   real_insert_custom_item (self, name, label, icon, TRUE, &iter);
709 }
710
711 /**
712  * gtk_app_chooser_button_select_custom_item:
713  * @self: a #GtkAppChooserButton
714  * @name: the name of the custom item
715  *
716  * Selects a custom item previously added with
717  * gtk_app_chooser_button_append_custom_item().
718  * Use gtk_app_chooser_refresh() to bring the selection to its initial
719  * state.
720  *
721  * Since: 3.0
722  */
723 void
724 gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
725                                                const gchar         *name)
726 {
727   GtkTreeIter iter;
728
729   g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
730   g_return_if_fail (name != NULL);
731
732   if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
733       !app_chooser_button_iter_from_custom_name (self, name, &iter))
734     {
735       g_warning ("Can't find the item named %s in the app chooser.",
736                  name);
737       return;
738     }
739
740   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
741 }
742
743 /**
744  * gtk_app_chooser_button_get_show_dialog_item:
745  * @self: a #GtkAppChooserButton
746  *
747  * Returns the current value of the #GtkAppChooserButton:show-dialog-item
748  * property.
749  *
750  * Returns: the value of #GtkAppChooserButton:show-dialog-item
751  *
752  * Since: 3.0
753  */
754 gboolean
755 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
756 {
757   g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
758
759   return self->priv->show_dialog_item;
760 }
761
762 /**
763  * gtk_app_chooser_button_set_show_dialog_item:
764  * @self: a #GtkAppChooserButton
765  * @setting: the new value for #GtkAppChooserButton:show-dialog-item
766  *
767  * Sets whether the dropdown menu of this button should show an
768  * entry to trigger a #GtkAppChooserDialog.
769  *
770  * Since: 3.0
771  */
772 void
773 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
774                                              gboolean setting)
775 {
776   if (self->priv->show_dialog_item != setting)
777     {
778       self->priv->show_dialog_item = setting;
779
780       g_object_notify (G_OBJECT (self), "show-dialog-item");
781
782       gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
783     }
784 }