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)
236 if (!self->priv->show_dialog_item)
239 icon = g_themed_icon_new ("application-x-executable");
241 gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);
242 real_insert_separator (self, FALSE, &iter);
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,
250 g_object_unref (icon);
254 gtk_app_chooser_button_populate (GtkAppChooserButton *self)
256 GList *recommended_apps = NULL, *l;
258 GtkTreeIter iter, iter2;
262 recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
265 for (l = recommended_apps; l != NULL; l = l->next)
269 icon = g_app_info_get_icon (app);
272 icon = g_themed_icon_new ("application-x-executable");
278 get_first_iter (self->priv->store, &iter);
283 gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
287 gtk_list_store_set (self->priv->store, &iter,
288 COLUMN_APP_INFO, app,
289 COLUMN_LABEL, g_app_info_get_display_name (app),
291 COLUMN_CUSTOM, FALSE,
294 g_object_unref (icon);
297 gtk_app_chooser_button_ensure_dialog_item (self, &iter);
298 gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
302 gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
304 GtkCellRenderer *cell;
306 self->priv->store = gtk_list_store_new (NUM_COLUMNS,
308 G_TYPE_STRING, /* name */
309 G_TYPE_STRING, /* label */
311 G_TYPE_BOOLEAN, /* separator */
312 G_TYPE_BOOLEAN); /* custom */
314 gtk_combo_box_set_model (GTK_COMBO_BOX (self),
315 GTK_TREE_MODEL (self->priv->store));
317 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
318 row_separator_func, NULL, NULL);
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,
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,
335 gtk_app_chooser_button_populate (self);
339 gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
343 gboolean custom, res;
345 model = GTK_TREE_MODEL (self->priv->store);
347 if (!gtk_tree_model_get_iter_first (model, &iter))
351 gtk_tree_model_get (model, &iter,
352 COLUMN_CUSTOM, &custom,
355 res = gtk_tree_model_iter_next (model, &iter);
357 res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
362 gtk_app_chooser_button_changed (GtkComboBox *object)
364 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
370 if (!gtk_combo_box_get_active_iter (object, &iter))
373 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
375 COLUMN_CUSTOM, &custom,
382 name_quark = g_quark_from_string (name);
383 g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
387 /* trigger the dialog internally */
388 other_application_item_activated_cb (self);
396 gtk_app_chooser_button_refresh (GtkAppChooser *object)
398 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
400 gtk_app_chooser_button_remove_non_custom (self);
401 gtk_app_chooser_button_populate (self);
405 gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
407 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
411 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
414 gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
415 COLUMN_APP_INFO, &info,
422 gtk_app_chooser_button_constructed (GObject *obj)
424 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
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);
429 g_assert (self->priv->content_type != NULL);
431 gtk_app_chooser_button_build_ui (self);
435 gtk_app_chooser_button_set_property (GObject *obj,
440 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
444 case PROP_CONTENT_TYPE:
445 self->priv->content_type = g_value_dup_string (value);
447 case PROP_SHOW_DIALOG_ITEM:
448 gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
457 gtk_app_chooser_button_get_property (GObject *obj,
462 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
466 case PROP_CONTENT_TYPE:
467 g_value_set_string (value, self->priv->content_type);
469 case PROP_SHOW_DIALOG_ITEM:
470 g_value_set_boolean (value, self->priv->show_dialog_item);
473 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
479 gtk_app_chooser_button_finalize (GObject *obj)
481 GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
483 g_hash_table_destroy (self->priv->custom_item_names);
484 g_free (self->priv->content_type);
486 G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
490 app_chooser_iface_init (GtkAppChooserIface *iface)
492 iface->get_app_info = gtk_app_chooser_button_get_app_info;
493 iface->refresh = gtk_app_chooser_button_refresh;
497 gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
499 GObjectClass *oclass = G_OBJECT_CLASS (klass);
500 GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
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;
508 combo_class->changed = gtk_app_chooser_button_changed;
510 g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");
513 * GtkAppChooserButton:show-dialog-item:
515 * The ::show-dialog-item property determines whether the dropdown menu
516 * should show an item that triggers a #GtkAppChooserDialog when clicked.
518 pspec = g_param_spec_boolean ("show-dialog-item",
519 P_("Include an 'Other...' item"),
520 P_("Whether the combobox should include an item that triggers a GtkAppChooserDialog"),
522 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
523 g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
525 signals[SIGNAL_CUSTOM_ITEM_ACTIVATED] =
526 g_signal_new ("custom-item-activated",
527 GTK_TYPE_APP_CHOOSER_BUTTON,
528 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
529 G_STRUCT_OFFSET (GtkAppChooserButtonClass, custom_item_activated),
531 _gtk_marshal_VOID__STRING,
535 g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
539 gtk_app_chooser_button_init (GtkAppChooserButton *self)
541 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_BUTTON,
542 GtkAppChooserButtonPrivate);
543 self->priv->custom_item_names =
544 g_hash_table_new_full (g_str_hash, g_str_equal,
549 real_insert_custom_item (GtkAppChooserButton *self,
558 if (g_hash_table_lookup (self->priv->custom_item_names,
561 g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
562 "when there's already an item with the same name", name);
566 g_hash_table_insert (self->priv->custom_item_names,
567 g_strdup (name), GINT_TO_POINTER (1));
570 gtk_list_store_set (self->priv->store, iter,
574 COLUMN_CUSTOM, custom,
575 COLUMN_SEPARATOR, FALSE,
580 real_insert_separator (GtkAppChooserButton *self,
584 gtk_list_store_set (self->priv->store, iter,
585 COLUMN_CUSTOM, custom,
586 COLUMN_SEPARATOR, TRUE,
591 * gtk_app_chooser_button_new:
592 * @content_type: the content type to show applications for
594 * Creates a new #GtkAppChooserButton for applications
595 * that can handle content of the given type.
597 * Returns: a newly created #GtkAppChooserButton
602 gtk_app_chooser_button_new (const gchar *content_type)
604 g_return_val_if_fail (content_type != NULL, NULL);
606 return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
607 "content-type", content_type,
612 * gtk_app_chooser_button_append_separator:
613 * @self: a #GtkAppChooserButton
615 * Appends a separator to the list of applications that is shown
621 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
625 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
627 gtk_list_store_append (self->priv->store, &iter);
628 real_insert_separator (self, TRUE, &iter);
632 * gtk_app_chooser_button_append_custom_item:
633 * @self: a #GtkAppChooserButton
634 * @name: the name of the custom item
635 * @label: the label for the custom item
636 * @icon: the icon for the custom item
638 * Appends a custom item to the list of applications that is shown
639 * in the popup; the item name must be unique per-widget.
640 * Clients can use the provided name as a detail for the ::custom-item-activated
641 * signal, to add a callback for the activation of a particular
642 * custom item in the list.
643 * See also gtk_app_chooser_button_append_separator().
648 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
655 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
656 g_return_if_fail (name != NULL);
658 gtk_list_store_append (self->priv->store, &iter);
659 real_insert_custom_item (self, name, label, icon, TRUE, &iter);
663 * gtk_app_chooser_button_get_show_dialog_item:
664 * @self: a #GtkAppChooserButton
666 * Returns the current value of the #GtkAppChooserButton:show-dialog-item
669 * Returns: the value of #GtkAppChooserButton:show-dialog-item
674 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
676 g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
678 return self->priv->show_dialog_item;
682 * gtk_app_chooser_button_get_show_dialog_item:
683 * @self: a #GtkAppChooserButton
684 * @setting: the new value for #GtkAppChooserButton:show-dialog-item
686 * Sets whether the dropdown menu of this button should show an
687 * entry to trigger a #GtkAppChooserDialog.
692 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
695 if (self->priv->show_dialog_item != setting)
697 self->priv->show_dialog_item = setting;
699 g_object_notify (G_OBJECT (self), "show-dialog-item");
701 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));