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.
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"),
523 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
524 g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);
527 * GtkAppChooserButton::custom-item-activated:
528 * @self: the object which received the signal
529 * @item_name: the name of the activated item
531 * Emitted when a custom item, previously added with
532 * gtk_app_chooser_button_append_custom_item(), is activated from the
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),
541 _gtk_marshal_VOID__STRING,
545 g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
549 gtk_app_chooser_button_init (GtkAppChooserButton *self)
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,
559 real_insert_custom_item (GtkAppChooserButton *self,
568 if (g_hash_table_lookup (self->priv->custom_item_names,
571 g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
572 "when there's already an item with the same name", name);
576 g_hash_table_insert (self->priv->custom_item_names,
577 g_strdup (name), GINT_TO_POINTER (1));
580 gtk_list_store_set (self->priv->store, iter,
584 COLUMN_CUSTOM, custom,
585 COLUMN_SEPARATOR, FALSE,
590 real_insert_separator (GtkAppChooserButton *self,
594 gtk_list_store_set (self->priv->store, iter,
595 COLUMN_CUSTOM, custom,
596 COLUMN_SEPARATOR, TRUE,
601 * gtk_app_chooser_button_new:
602 * @content_type: the content type to show applications for
604 * Creates a new #GtkAppChooserButton for applications
605 * that can handle content of the given type.
607 * Returns: a newly created #GtkAppChooserButton
612 gtk_app_chooser_button_new (const gchar *content_type)
614 g_return_val_if_fail (content_type != NULL, NULL);
616 return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
617 "content-type", content_type,
622 * gtk_app_chooser_button_append_separator:
623 * @self: a #GtkAppChooserButton
625 * Appends a separator to the list of applications that is shown
631 gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
635 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
637 gtk_list_store_append (self->priv->store, &iter);
638 real_insert_separator (self, TRUE, &iter);
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
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().
658 gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
665 g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
666 g_return_if_fail (name != NULL);
668 gtk_list_store_append (self->priv->store, &iter);
669 real_insert_custom_item (self, name, label, icon, TRUE, &iter);
673 * gtk_app_chooser_button_get_show_dialog_item:
674 * @self: a #GtkAppChooserButton
676 * Returns the current value of the #GtkAppChooserButton:show-dialog-item
679 * Returns: the value of #GtkAppChooserButton:show-dialog-item
684 gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
686 g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
688 return self->priv->show_dialog_item;
692 * gtk_app_chooser_button_get_show_dialog_item:
693 * @self: a #GtkAppChooserButton
694 * @setting: the new value for #GtkAppChooserButton:show-dialog-item
696 * Sets whether the dropdown menu of this button should show an
697 * entry to trigger a #GtkAppChooserDialog.
702 gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
705 if (self->priv->show_dialog_item != setting)
707 self->priv->show_dialog_item = setting;
709 g_object_notify (G_OBJECT (self), "show-dialog-item");
711 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));