2 * gtkappchooserbutton.h: an app-chooser combobox
4 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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.
21 * Authors: Cosimo Cecchi <ccecchi@redhat.com>
26 #include "gtkappchooserbutton.h"
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"
37 #include "gtkmarshalers.h"
40 PROP_CONTENT_TYPE = 1,
41 PROP_SHOW_DIALOG_ITEM,
45 SIGNAL_CUSTOM_ITEM_ACTIVATED,
59 #define CUSTOM_ITEM_OTHER_APP "gtk-internal-item-other-app"
61 static void app_chooser_iface_init (GtkAppChooserIface *iface);
63 static void real_insert_custom_item (GtkAppChooserButton *self,
70 static void real_insert_separator (GtkAppChooserButton *self,
74 static guint signals[NUM_SIGNALS] = { 0, };
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));
80 struct _GtkAppChooserButtonPrivate {
84 gboolean show_dialog_item;
86 GHashTable *custom_item_names;
90 row_separator_func (GtkTreeModel *model,
96 gtk_tree_model_get (model, iter,
97 COLUMN_SEPARATOR, &separator,
104 get_first_iter (GtkListStore *store,
109 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), iter))
111 /* the model is empty, append */
112 gtk_list_store_append (store, iter);
116 gtk_list_store_insert_before (store, &iter2, iter);
122 GtkAppChooserButton *self;
128 select_app_data_free (SelectAppData *data)
130 g_clear_object (&data->self);
131 g_clear_object (&data->info);
133 g_slice_free (SelectAppData, data);
137 select_application_func_cb (GtkTreeModel *model,
142 SelectAppData *data = user_data;
143 GAppInfo *app_to_match = data->info, *app = NULL;
146 gtk_tree_model_get (model, iter,
147 COLUMN_APP_INFO, &app,
148 COLUMN_CUSTOM, &custom,
151 /* cutsom items are always after GAppInfos, so iterating further here
157 if (g_app_info_equal (app, app_to_match))
159 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (data->self), iter);
167 gtk_app_chooser_button_select_application (GtkAppChooserButton *self,
172 data = g_slice_new0 (SelectAppData);
173 data->self = g_object_ref (self);
174 data->info = g_object_ref (info);
176 gtk_tree_model_foreach (GTK_TREE_MODEL (self->priv->store),
177 select_application_func_cb, data);
179 select_app_data_free (data);
183 other_application_dialog_response_cb (GtkDialog *dialog,
187 GtkAppChooserButton *self = user_data;
190 if (response_id != GTK_RESPONSE_OK)
192 /* reset the active item, otherwise we are stuck on
193 * 'Other application...'
195 gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
196 gtk_widget_destroy (GTK_WIDGET (dialog));
200 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (dialog));
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);
206 g_object_unref (info);
210 other_application_item_activated_cb (GtkAppChooserButton *self)
212 GtkWidget *dialog, *widget;
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,
223 gtk_widget_show (dialog);
225 g_signal_connect (dialog, "response",
226 G_CALLBACK (other_application_dialog_response_cb), self);
230 gtk_app_chooser_button_ensure_dialog_item (GtkAppChooserButton *self,
231 GtkTreeIter *prev_iter)
234 GtkTreeIter iter, iter2;
236 if (!self->priv->show_dialog_item)
239 icon = g_themed_icon_new ("application-x-executable");
241 if (prev_iter == NULL)
242 gtk_list_store_append (self->priv->store, &iter);
244 gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);
246 real_insert_separator (self, FALSE, &iter);
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,
254 g_object_unref (icon);
258 gtk_app_chooser_button_populate (GtkAppChooserButton *self)
260 GList *recommended_apps = NULL, *l;
262 GtkTreeIter iter, iter2;
264 gboolean cycled_recommended;
266 recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
267 cycled_recommended = FALSE;
269 for (l = recommended_apps; l != NULL; l = l->next)
273 icon = g_app_info_get_icon (app);
276 icon = g_themed_icon_new ("application-x-executable");
280 if (cycled_recommended)
282 gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
287 get_first_iter (self->priv->store, &iter);
288 cycled_recommended = TRUE;
291 gtk_list_store_set (self->priv->store, &iter,
292 COLUMN_APP_INFO, app,
293 COLUMN_LABEL, g_app_info_get_display_name (app),
295 COLUMN_CUSTOM, FALSE,
298 g_object_unref (icon);
301 if (!cycled_recommended)
302 gtk_app_chooser_button_ensure_dialog_item (self, NULL);
304 gtk_app_chooser_button_ensure_dialog_item (self, &iter);
306 gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
310 gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
312 GtkCellRenderer *cell;
314 self->priv->store = gtk_list_store_new (NUM_COLUMNS,
316 G_TYPE_STRING, /* name */
317 G_TYPE_STRING, /* label */
319 G_TYPE_BOOLEAN, /* separator */
320 G_TYPE_BOOLEAN); /* custom */
322 gtk_combo_box_set_model (GTK_COMBO_BOX (self),
323 GTK_TREE_MODEL (self->priv->store));
325 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
326 row_separator_func, NULL, NULL);
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,
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,
343 gtk_app_chooser_button_populate (self);
347 gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
351 gboolean custom, res;
353 model = GTK_TREE_MODEL (self->priv->store);
355 if (!gtk_tree_model_get_iter_first (model, &iter))
359 gtk_tree_model_get (model, &iter,
360 COLUMN_CUSTOM, &custom,
363 res = gtk_tree_model_iter_next (model, &iter);
365 res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
370 gtk_app_chooser_button_changed (GtkComboBox *object)
372 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
378 if (!gtk_combo_box_get_active_iter (object, &iter))
381 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
383 COLUMN_CUSTOM, &custom,
390 name_quark = g_quark_from_string (name);
391 g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
395 /* trigger the dialog internally */
396 other_application_item_activated_cb (self);
404 gtk_app_chooser_button_refresh (GtkAppChooser *object)
406 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
408 gtk_app_chooser_button_remove_non_custom (self);
409 gtk_app_chooser_button_populate (self);
413 gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
415 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
419 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
422 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
423 COLUMN_APP_INFO, &info,
430 gtk_app_chooser_button_constructed (GObject *obj)
432 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
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);
437 g_assert (self->priv->content_type != NULL);
439 gtk_app_chooser_button_build_ui (self);
443 gtk_app_chooser_button_set_property (GObject *obj,
448 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
452 case PROP_CONTENT_TYPE:
453 self->priv->content_type = g_value_dup_string (value);
455 case PROP_SHOW_DIALOG_ITEM:
456 gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
459 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
465 gtk_app_chooser_button_get_property (GObject *obj,
470 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
474 case PROP_CONTENT_TYPE:
475 g_value_set_string (value, self->priv->content_type);
477 case PROP_SHOW_DIALOG_ITEM:
478 g_value_set_boolean (value, self->priv->show_dialog_item);
481 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
487 gtk_app_chooser_button_finalize (GObject *obj)
489 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
491 g_hash_table_destroy (self->priv->custom_item_names);
492 g_free (self->priv->content_type);
494 G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
498 app_chooser_iface_init (GtkAppChooserIface *iface)
500 iface->get_app_info = gtk_app_chooser_button_get_app_info;
501 iface->refresh = gtk_app_chooser_button_refresh;
505 gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
507 GObjectClass *oclass = G_OBJECT_CLASS (klass);
508 GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
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;
516 combo_class->changed = gtk_app_chooser_button_changed;
518 g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");
521 * GtkAppChooserButton:show-dialog-item:
523 * The #GtkAppChooserButton:show-dialog-item property determines whether the dropdown menu
524 * should show an item that triggers a #GtkAppChooserDialog when clicked.
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"),
531 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
532 g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
535 * GtkAppChooserButton::custom-item-activated:
536 * @self: the object which received the signal
537 * @item_name: the name of the activated item
539 * Emitted when a custom item, previously added with
540 * gtk_app_chooser_button_append_custom_item(), is activated from the
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),
549 _gtk_marshal_VOID__STRING,
553 g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
557 gtk_app_chooser_button_init (GtkAppChooserButton *self)
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,
567 app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
572 gchar *custom_name = NULL;
574 if (!gtk_tree_model_get_iter_first
575 (GTK_TREE_MODEL (self->priv->store), &iter))
579 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
580 COLUMN_NAME, &custom_name,
583 if (g_strcmp0 (custom_name, name) == 0)
585 g_free (custom_name);
591 g_free (custom_name);
592 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));
598 real_insert_custom_item (GtkAppChooserButton *self,
607 if (g_hash_table_lookup (self->priv->custom_item_names,
610 g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
611 "when there's already an item with the same name", name);
615 g_hash_table_insert (self->priv->custom_item_names,
616 g_strdup (name), GINT_TO_POINTER (1));
619 gtk_list_store_set (self->priv->store, iter,
623 COLUMN_CUSTOM, custom,
624 COLUMN_SEPARATOR, FALSE,
629 real_insert_separator (GtkAppChooserButton *self,
633 gtk_list_store_set (self->priv->store, iter,
634 COLUMN_CUSTOM, custom,
635 COLUMN_SEPARATOR, TRUE,
640 * gtk_app_chooser_button_new:
641 * @content_type: the content type to show applications for
643 * Creates a new #GtkAppChooserButton for applications
644 * that can handle content of the given type.
646 * Returns: a newly created #GtkAppChooserButton
651 gtk_app_chooser_button_new (const gchar *content_type)
653 g_return_val_if_fail (content_type != NULL, NULL);
655 return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
656 "content-type", content_type,
661 * gtk_app_chooser_button_append_separator:
662 * @self: a #GtkAppChooserButton
664 * Appends a separator to the list of applications that is shown
670 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
674 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
676 gtk_list_store_append (self->priv->store, &iter);
677 real_insert_separator (self, TRUE, &iter);
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
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().
697 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
704 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
705 g_return_if_fail (name != NULL);
707 gtk_list_store_append (self->priv->store, &iter);
708 real_insert_custom_item (self, name, label, icon, TRUE, &iter);
712 * gtk_app_chooser_button_select_custom_item:
713 * @self: a #GtkAppChooserButton
714 * @name: the name of the custom item
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
724 gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
729 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
730 g_return_if_fail (name != NULL);
732 if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
733 !app_chooser_button_iter_from_custom_name (self, name, &iter))
735 g_warning ("Can't find the item named %s in the app chooser.",
740 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
744 * gtk_app_chooser_button_get_show_dialog_item:
745 * @self: a #GtkAppChooserButton
747 * Returns the current value of the #GtkAppChooserButton:show-dialog-item
750 * Returns: the value of #GtkAppChooserButton:show-dialog-item
755 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
757 g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
759 return self->priv->show_dialog_item;
763 * gtk_app_chooser_button_set_show_dialog_item:
764 * @self: a #GtkAppChooserButton
765 * @setting: the new value for #GtkAppChooserButton:show-dialog-item
767 * Sets whether the dropdown menu of this button should show an
768 * entry to trigger a #GtkAppChooserDialog.
773 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
776 if (self->priv->show_dialog_item != setting)
778 self->priv->show_dialog_item = setting;
780 g_object_notify (G_OBJECT (self), "show-dialog-item");
782 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));