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