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>
25 * SECTION:gtkappchooserbutton
26 * @Title: GtkAppChooserButton
27 * @Short_description: A button to launch an application chooser dialog
29 * The #GtkAppChooserButton is a widget that lets the user select
30 * an application. It implements the #GtkAppChooser interface.
34 #include "gtkappchooserbutton.h"
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"
45 #include "gtkmarshalers.h"
48 PROP_CONTENT_TYPE = 1,
49 PROP_SHOW_DIALOG_ITEM,
53 SIGNAL_CUSTOM_ITEM_ACTIVATED,
67 #define CUSTOM_ITEM_OTHER_APP "gtk-internal-item-other-app"
69 static void app_chooser_iface_init (GtkAppChooserIface *iface);
71 static void real_insert_custom_item (GtkAppChooserButton *self,
78 static void real_insert_separator (GtkAppChooserButton *self,
82 static guint signals[NUM_SIGNALS] = { 0, };
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));
88 struct _GtkAppChooserButtonPrivate {
92 gboolean show_dialog_item;
94 GHashTable *custom_item_names;
98 row_separator_func (GtkTreeModel *model,
104 gtk_tree_model_get (model, iter,
105 COLUMN_SEPARATOR, &separator,
112 get_first_iter (GtkListStore *store,
117 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), iter))
119 /* the model is empty, append */
120 gtk_list_store_append (store, iter);
124 gtk_list_store_insert_before (store, &iter2, iter);
130 GtkAppChooserButton *self;
136 select_app_data_free (SelectAppData *data)
138 g_clear_object (&data->self);
139 g_clear_object (&data->info);
141 g_slice_free (SelectAppData, data);
145 select_application_func_cb (GtkTreeModel *model,
150 SelectAppData *data = user_data;
151 GAppInfo *app_to_match = data->info, *app = NULL;
154 gtk_tree_model_get (model, iter,
155 COLUMN_APP_INFO, &app,
156 COLUMN_CUSTOM, &custom,
159 /* cutsom items are always after GAppInfos, so iterating further here
165 if (g_app_info_equal (app, app_to_match))
167 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (data->self), iter);
175 gtk_app_chooser_button_select_application (GtkAppChooserButton *self,
180 data = g_slice_new0 (SelectAppData);
181 data->self = g_object_ref (self);
182 data->info = g_object_ref (info);
184 gtk_tree_model_foreach (GTK_TREE_MODEL (self->priv->store),
185 select_application_func_cb, data);
187 select_app_data_free (data);
191 other_application_dialog_response_cb (GtkDialog *dialog,
195 GtkAppChooserButton *self = user_data;
198 if (response_id != GTK_RESPONSE_OK)
200 /* reset the active item, otherwise we are stuck on
201 * 'Other application...'
203 gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
204 gtk_widget_destroy (GTK_WIDGET (dialog));
208 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (dialog));
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);
214 g_object_unref (info);
218 other_application_item_activated_cb (GtkAppChooserButton *self)
220 GtkWidget *dialog, *widget;
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 widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
227 g_object_set (widget,
228 "show-fallback", TRUE,
231 gtk_widget_show (dialog);
233 g_signal_connect (dialog, "response",
234 G_CALLBACK (other_application_dialog_response_cb), self);
238 gtk_app_chooser_button_ensure_dialog_item (GtkAppChooserButton *self,
239 GtkTreeIter *prev_iter)
241 GtkTreeIter iter, iter2;
243 if (!self->priv->show_dialog_item)
246 if (prev_iter == NULL)
247 gtk_list_store_append (self->priv->store, &iter);
249 gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);
251 real_insert_separator (self, FALSE, &iter);
254 gtk_list_store_insert_after (self->priv->store, &iter, &iter2);
255 real_insert_custom_item (self, CUSTOM_ITEM_OTHER_APP,
256 _("Other application..."), NULL,
261 gtk_app_chooser_button_populate (GtkAppChooserButton *self)
263 GList *recommended_apps = NULL, *l;
265 GtkTreeIter iter, iter2;
267 gboolean cycled_recommended;
270 recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
272 cycled_recommended = FALSE;
274 for (l = recommended_apps; l != NULL; l = l->next)
278 icon = g_app_info_get_icon (app);
281 icon = g_themed_icon_new ("application-x-executable");
285 if (cycled_recommended)
287 gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
292 get_first_iter (self->priv->store, &iter);
293 cycled_recommended = TRUE;
296 gtk_list_store_set (self->priv->store, &iter,
297 COLUMN_APP_INFO, app,
298 COLUMN_LABEL, g_app_info_get_display_name (app),
300 COLUMN_CUSTOM, FALSE,
303 g_object_unref (icon);
306 if (!cycled_recommended)
307 gtk_app_chooser_button_ensure_dialog_item (self, NULL);
309 gtk_app_chooser_button_ensure_dialog_item (self, &iter);
311 gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
315 gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
317 GtkCellRenderer *cell;
319 self->priv->store = gtk_list_store_new (NUM_COLUMNS,
321 G_TYPE_STRING, /* name */
322 G_TYPE_STRING, /* label */
324 G_TYPE_BOOLEAN, /* separator */
325 G_TYPE_BOOLEAN); /* custom */
327 gtk_combo_box_set_model (GTK_COMBO_BOX (self),
328 GTK_TREE_MODEL (self->priv->store));
330 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
331 row_separator_func, NULL, NULL);
333 cell = gtk_cell_renderer_pixbuf_new ();
334 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
335 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
336 "gicon", COLUMN_ICON,
339 cell = gtk_cell_renderer_text_new ();
340 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, TRUE);
341 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
342 "text", COLUMN_LABEL,
348 gtk_app_chooser_button_populate (self);
352 gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
356 gboolean custom, res;
358 model = GTK_TREE_MODEL (self->priv->store);
360 if (!gtk_tree_model_get_iter_first (model, &iter))
364 gtk_tree_model_get (model, &iter,
365 COLUMN_CUSTOM, &custom,
368 res = gtk_tree_model_iter_next (model, &iter);
370 res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
375 gtk_app_chooser_button_changed (GtkComboBox *object)
377 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
383 if (!gtk_combo_box_get_active_iter (object, &iter))
386 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
388 COLUMN_CUSTOM, &custom,
395 name_quark = g_quark_from_string (name);
396 g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
400 /* trigger the dialog internally */
401 other_application_item_activated_cb (self);
409 gtk_app_chooser_button_refresh (GtkAppChooser *object)
411 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
413 gtk_app_chooser_button_remove_non_custom (self);
414 gtk_app_chooser_button_populate (self);
418 gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
420 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
424 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
427 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
428 COLUMN_APP_INFO, &info,
435 gtk_app_chooser_button_constructed (GObject *obj)
437 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
439 if (G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed != NULL)
440 G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed (obj);
442 g_assert (self->priv->content_type != NULL);
444 gtk_app_chooser_button_build_ui (self);
448 gtk_app_chooser_button_set_property (GObject *obj,
453 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
457 case PROP_CONTENT_TYPE:
458 self->priv->content_type = g_value_dup_string (value);
460 case PROP_SHOW_DIALOG_ITEM:
461 gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
464 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
470 gtk_app_chooser_button_get_property (GObject *obj,
475 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
479 case PROP_CONTENT_TYPE:
480 g_value_set_string (value, self->priv->content_type);
482 case PROP_SHOW_DIALOG_ITEM:
483 g_value_set_boolean (value, self->priv->show_dialog_item);
486 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
492 gtk_app_chooser_button_finalize (GObject *obj)
494 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
496 g_hash_table_destroy (self->priv->custom_item_names);
497 g_free (self->priv->content_type);
499 G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
503 app_chooser_iface_init (GtkAppChooserIface *iface)
505 iface->get_app_info = gtk_app_chooser_button_get_app_info;
506 iface->refresh = gtk_app_chooser_button_refresh;
510 gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
512 GObjectClass *oclass = G_OBJECT_CLASS (klass);
513 GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
516 oclass->set_property = gtk_app_chooser_button_set_property;
517 oclass->get_property = gtk_app_chooser_button_get_property;
518 oclass->finalize = gtk_app_chooser_button_finalize;
519 oclass->constructed = gtk_app_chooser_button_constructed;
521 combo_class->changed = gtk_app_chooser_button_changed;
523 g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");
526 * GtkAppChooserButton:show-dialog-item:
528 * The #GtkAppChooserButton:show-dialog-item property determines whether the dropdown menu
529 * should show an item that triggers a #GtkAppChooserDialog when clicked.
532 g_param_spec_boolean ("show-dialog-item",
533 P_("Include an 'Other...' item"),
534 P_("Whether the combobox should include an item that triggers a GtkAppChooserDialog"),
536 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
537 g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
540 * GtkAppChooserButton::custom-item-activated:
541 * @self: the object which received the signal
542 * @item_name: the name of the activated item
544 * Emitted when a custom item, previously added with
545 * gtk_app_chooser_button_append_custom_item(), is activated from the
548 signals[SIGNAL_CUSTOM_ITEM_ACTIVATED] =
549 g_signal_new ("custom-item-activated",
550 GTK_TYPE_APP_CHOOSER_BUTTON,
551 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
552 G_STRUCT_OFFSET (GtkAppChooserButtonClass, custom_item_activated),
554 _gtk_marshal_VOID__STRING,
558 g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
562 gtk_app_chooser_button_init (GtkAppChooserButton *self)
564 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_BUTTON,
565 GtkAppChooserButtonPrivate);
566 self->priv->custom_item_names =
567 g_hash_table_new_full (g_str_hash, g_str_equal,
572 app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
577 gchar *custom_name = NULL;
579 if (!gtk_tree_model_get_iter_first
580 (GTK_TREE_MODEL (self->priv->store), &iter))
584 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
585 COLUMN_NAME, &custom_name,
588 if (g_strcmp0 (custom_name, name) == 0)
590 g_free (custom_name);
596 g_free (custom_name);
597 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));
603 real_insert_custom_item (GtkAppChooserButton *self,
612 if (g_hash_table_lookup (self->priv->custom_item_names,
615 g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
616 "when there's already an item with the same name", name);
620 g_hash_table_insert (self->priv->custom_item_names,
621 g_strdup (name), GINT_TO_POINTER (1));
624 gtk_list_store_set (self->priv->store, iter,
628 COLUMN_CUSTOM, custom,
629 COLUMN_SEPARATOR, FALSE,
634 real_insert_separator (GtkAppChooserButton *self,
638 gtk_list_store_set (self->priv->store, iter,
639 COLUMN_CUSTOM, custom,
640 COLUMN_SEPARATOR, TRUE,
645 * gtk_app_chooser_button_new:
646 * @content_type: the content type to show applications for
648 * Creates a new #GtkAppChooserButton for applications
649 * that can handle content of the given type.
651 * Returns: a newly created #GtkAppChooserButton
656 gtk_app_chooser_button_new (const gchar *content_type)
658 g_return_val_if_fail (content_type != NULL, NULL);
660 return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
661 "content-type", content_type,
666 * gtk_app_chooser_button_append_separator:
667 * @self: a #GtkAppChooserButton
669 * Appends a separator to the list of applications that is shown
675 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
679 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
681 gtk_list_store_append (self->priv->store, &iter);
682 real_insert_separator (self, TRUE, &iter);
686 * gtk_app_chooser_button_append_custom_item:
687 * @self: a #GtkAppChooserButton
688 * @name: the name of the custom item
689 * @label: the label for the custom item
690 * @icon: the icon for the custom item
692 * Appends a custom item to the list of applications that is shown
693 * in the popup; the item name must be unique per-widget.
694 * Clients can use the provided name as a detail for the ::custom-item-activated
695 * signal, to add a callback for the activation of a particular
696 * custom item in the list.
697 * See also gtk_app_chooser_button_append_separator().
702 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
709 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
710 g_return_if_fail (name != NULL);
712 gtk_list_store_append (self->priv->store, &iter);
713 real_insert_custom_item (self, name, label, icon, TRUE, &iter);
717 * gtk_app_chooser_button_select_custom_item:
718 * @self: a #GtkAppChooserButton
719 * @name: the name of the custom item
721 * Selects a custom item previously added with
722 * gtk_app_chooser_button_append_custom_item().
723 * Use gtk_app_chooser_refresh() to bring the selection to its initial
729 gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
734 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
735 g_return_if_fail (name != NULL);
737 if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
738 !app_chooser_button_iter_from_custom_name (self, name, &iter))
740 g_warning ("Can't find the item named %s in the app chooser.",
745 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
749 * gtk_app_chooser_button_get_show_dialog_item:
750 * @self: a #GtkAppChooserButton
752 * Returns the current value of the #GtkAppChooserButton:show-dialog-item
755 * Returns: the value of #GtkAppChooserButton:show-dialog-item
760 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
762 g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
764 return self->priv->show_dialog_item;
768 * gtk_app_chooser_button_set_show_dialog_item:
769 * @self: a #GtkAppChooserButton
770 * @setting: the new value for #GtkAppChooserButton:show-dialog-item
772 * Sets whether the dropdown menu of this button should show an
773 * entry to trigger a #GtkAppChooserDialog.
778 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
781 if (self->priv->show_dialog_item != setting)
783 self->priv->show_dialog_item = setting;
785 g_object_notify (G_OBJECT (self), "show-dialog-item");
787 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));