2 * gtkappchooserdialog.c: an app-chooser dialog
4 * Copyright (C) 2004 Novell, Inc.
5 * Copyright (C) 2007, 2010 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with the Gnome Library; see the file COPYING.LIB. If not,
19 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Authors: Dave Camp <dave@novell.com>
23 * Alexander Larsson <alexl@redhat.com>
24 * Cosimo Cecchi <ccecchi@redhat.com>
28 * SECTION:gtkappchooserdialog
29 * @Title: GtkAppChooserDialog
30 * @Short_description: An application chooser dialog
32 * #GtkAppChooserDialog shows a #GtkAppChooserWidget inside a #GtkDialog.
34 * Note that #GtkAppChooserDialog does not have any interesting methods
35 * of its own. Instead, you should get the embedded #GtkAppChooserWidget
36 * using gtk_app_chooser_dialog_get_widget() and call its methods if
37 * the generic #GtkAppChooser interface is not sufficient for your needs.
41 #include "gtkappchooserdialog.h"
44 #include "gtkappchooser.h"
45 #include "gtkappchooseronline.h"
46 #include "gtkappchooserprivate.h"
47 #include "gtkappchooserprivate.h"
49 #include "gtkmessagedialog.h"
52 #include "gtkbutton.h"
53 #include "gtkmenuitem.h"
57 #include <glib/gi18n-lib.h>
60 #define sure_string(s) ((const char *) ((s) != NULL ? (s) : ""))
62 struct _GtkAppChooserDialogPrivate {
69 GtkWidget *online_button;
71 GtkWidget *open_label;
73 GtkWidget *app_chooser_widget;
74 GtkWidget *show_more_button;
76 GtkAppChooserOnline *online;
78 gboolean show_more_clicked;
88 static void gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface);
89 G_DEFINE_TYPE_WITH_CODE (GtkAppChooserDialog, gtk_app_chooser_dialog, GTK_TYPE_DIALOG,
90 G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
91 gtk_app_chooser_dialog_iface_init));
94 show_error_dialog (const gchar *primary,
95 const gchar *secondary,
98 GtkWidget *message_dialog;
100 message_dialog = gtk_message_dialog_new (parent, 0,
104 g_object_set (message_dialog,
106 "secondary-text", secondary,
108 gtk_dialog_set_default_response (GTK_DIALOG (message_dialog), GTK_RESPONSE_OK);
110 gtk_widget_show (message_dialog);
112 g_signal_connect (message_dialog, "response",
113 G_CALLBACK (gtk_widget_destroy), NULL);
117 search_for_mimetype_ready_cb (GObject *source,
121 GtkAppChooserOnline *online = GTK_APP_CHOOSER_ONLINE (source);
122 GtkAppChooserDialog *self = user_data;
123 GError *error = NULL;
125 _gtk_app_chooser_online_search_for_mimetype_finish (online, res, &error);
129 show_error_dialog (_("Failed to look for applications online"),
130 error->message, GTK_WINDOW (self));
131 g_error_free (error);
135 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
140 online_button_clicked_cb (GtkButton *b,
143 GtkAppChooserDialog *self = user_data;
145 _gtk_app_chooser_online_search_for_mimetype_async (self->priv->online,
146 self->priv->content_type,
148 search_for_mimetype_ready_cb,
153 app_chooser_online_get_default_ready_cb (GObject *source,
157 GtkAppChooserDialog *self = user_data;
159 self->priv->online = _gtk_app_chooser_online_get_default_finish (source, res);
161 if (self->priv->online != NULL)
163 GtkWidget *action_area;
165 action_area = gtk_dialog_get_action_area (GTK_DIALOG (self));
166 self->priv->online_button = gtk_button_new_with_label (_("Find applications online"));
167 gtk_box_pack_start (GTK_BOX (action_area), self->priv->online_button,
169 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (action_area), self->priv->online_button,
171 g_signal_connect (self->priv->online_button, "clicked",
172 G_CALLBACK (online_button_clicked_cb), self);
174 gtk_widget_show (self->priv->online_button);
179 ensure_online_button (GtkAppChooserDialog *self)
181 _gtk_app_chooser_online_get_default_async (app_chooser_online_get_default_ready_cb, self);
184 /* An application is valid if:
187 * 2) The user has permissions to run the file
190 check_application (GtkAppChooserDialog *self,
197 GError *error = NULL;
203 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
204 command = g_app_info_get_executable (info);
206 g_shell_parse_argv (command, &argc, &argv, &error);
210 show_error_dialog (_("Could not run application"),
213 g_error_free (error);
218 path = g_find_program_in_path (argv[0]);
223 error_message = g_strdup_printf (_("Could not find '%s'"),
226 show_error_dialog (_("Could not find application"),
229 g_free (error_message);
244 add_or_find_application (GtkAppChooserDialog *self)
248 app = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
250 /* we don't care about reporting errors here */
251 g_app_info_set_as_last_used_for_type (app,
252 self->priv->content_type,
255 g_object_unref (app);
259 gtk_app_chooser_dialog_response (GtkDialog *dialog,
263 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (dialog);
267 case GTK_RESPONSE_OK:
268 add_or_find_application (self);
276 widget_application_selected_cb (GtkAppChooserWidget *widget,
280 GtkAppChooserDialog *self = user_data;
282 gtk_widget_set_sensitive (self->priv->button, TRUE);
286 widget_application_activated_cb (GtkAppChooserWidget *widget,
290 GtkAppChooserDialog *self = user_data;
292 gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
296 get_extension (const char *basename)
300 p = strrchr (basename, '.');
302 if (p && *(p + 1) != '\0')
303 return g_strdup (p + 1);
309 set_dialog_properties (GtkAppChooserDialog *self)
317 PangoFontDescription *font_desc;
324 if (self->priv->gfile != NULL)
326 name = g_file_get_basename (self->priv->gfile);
327 extension = get_extension (name);
330 description = g_content_type_get_description (self->priv->content_type);
331 gtk_window_set_title (GTK_WINDOW (self), "");
335 /* Translators: %s is a filename */
336 label = g_strdup_printf (_("Select an application to open \"%s\""), name);
337 string = g_strdup_printf (_("No applications available to open \"%s\""),
342 /* Translators: %s is a file type description */
343 label = g_strdup_printf (_("Select an application for \"%s\" files"),
344 g_content_type_is_unknown (self->priv->content_type) ?
345 self->priv->content_type : description);
346 string = g_strdup_printf (_("No applications available to open \"%s\" files"),
347 g_content_type_is_unknown (self->priv->content_type) ?
348 self->priv->content_type : description);
351 font_desc = pango_font_description_new ();
352 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
353 gtk_widget_override_font (self->priv->label, font_desc);
354 pango_font_description_free (font_desc);
356 if (self->priv->heading != NULL)
357 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
359 gtk_label_set_markup (GTK_LABEL (self->priv->label), label);
361 default_text = g_strdup_printf ("<big><b>%s</b></big>\n%s",
363 _("Click \"Show other applications\", for more options, or "
364 "\"Find applications online\" to install a new application"));
366 gtk_app_chooser_widget_set_default_text (GTK_APP_CHOOSER_WIDGET (self->priv->app_chooser_widget),
372 g_free (description);
374 g_free (default_text);
378 show_more_button_clicked_cb (GtkButton *button,
381 GtkAppChooserDialog *self = user_data;
383 g_object_set (self->priv->app_chooser_widget,
384 "show-recommended", TRUE,
385 "show-fallback", TRUE,
389 gtk_widget_hide (self->priv->show_more_button);
390 self->priv->show_more_clicked = TRUE;
394 widget_notify_for_button_cb (GObject *source,
398 GtkAppChooserDialog *self = user_data;
399 GtkAppChooserWidget *widget = GTK_APP_CHOOSER_WIDGET (source);
400 gboolean should_hide;
402 should_hide = gtk_app_chooser_widget_get_show_other (widget) ||
403 self->priv->show_more_clicked;
406 gtk_widget_hide (self->priv->show_more_button);
410 forget_menu_item_activate_cb (GtkMenuItem *item,
413 GtkAppChooserDialog *self = user_data;
416 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
420 g_app_info_remove_supports_type (info, self->priv->content_type, NULL);
422 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
424 g_object_unref (info);
429 build_forget_menu_item (GtkAppChooserDialog *self)
433 retval = gtk_menu_item_new_with_label (_("Forget association"));
434 gtk_widget_show (retval);
436 g_signal_connect (retval, "activate",
437 G_CALLBACK (forget_menu_item_activate_cb), self);
443 widget_populate_popup_cb (GtkAppChooserWidget *widget,
448 GtkAppChooserDialog *self = user_data;
449 GtkWidget *menu_item;
451 if (g_app_info_can_remove_supports_type (info))
453 menu_item = build_forget_menu_item (self);
454 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
459 build_dialog_ui (GtkAppChooserDialog *self)
464 GtkWidget *button, *w;
466 gtk_container_set_border_width (GTK_CONTAINER (self), 5);
468 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
469 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
470 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
471 gtk_widget_show (vbox);
473 vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
474 gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
475 gtk_widget_show (vbox2);
477 self->priv->label = gtk_label_new ("");
478 gtk_misc_set_alignment (GTK_MISC (self->priv->label), 0, 0.5);
479 gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
480 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
482 gtk_widget_show (self->priv->label);
484 self->priv->app_chooser_widget =
485 gtk_app_chooser_widget_new (self->priv->content_type);
486 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->app_chooser_widget, TRUE, TRUE, 0);
487 gtk_widget_show (self->priv->app_chooser_widget);
489 g_signal_connect (self->priv->app_chooser_widget, "application-selected",
490 G_CALLBACK (widget_application_selected_cb), self);
491 g_signal_connect (self->priv->app_chooser_widget, "application-activated",
492 G_CALLBACK (widget_application_activated_cb), self);
493 g_signal_connect (self->priv->app_chooser_widget, "notify::show-other",
494 G_CALLBACK (widget_notify_for_button_cb), self);
495 g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
496 G_CALLBACK (widget_populate_popup_cb), self);
498 button = gtk_button_new_with_label (_("Show other applications"));
499 self->priv->show_more_button = button;
500 w = gtk_image_new_from_stock (GTK_STOCK_ADD,
501 GTK_ICON_SIZE_BUTTON);
502 gtk_button_set_image (GTK_BUTTON (button), w);
503 gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget), button, FALSE, FALSE, 6);
504 gtk_widget_show_all (button);
506 g_signal_connect (button, "clicked",
507 G_CALLBACK (show_more_button_clicked_cb), self);
509 gtk_dialog_add_button (GTK_DIALOG (self),
511 GTK_RESPONSE_CANCEL);
513 /* Create a custom stock icon */
514 self->priv->button = gtk_button_new ();
516 label = gtk_label_new_with_mnemonic (_("_Select"));
517 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (self->priv->button));
518 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
519 gtk_widget_show (label);
520 self->priv->open_label = label;
522 gtk_container_add (GTK_CONTAINER (self->priv->button),
523 self->priv->open_label);
525 gtk_widget_show (self->priv->button);
526 gtk_widget_set_can_default (self->priv->button, TRUE);
528 gtk_dialog_add_action_widget (GTK_DIALOG (self),
529 self->priv->button, GTK_RESPONSE_OK);
531 gtk_dialog_set_default_response (GTK_DIALOG (self),
536 set_gfile_and_content_type (GtkAppChooserDialog *self,
544 self->priv->gfile = g_object_ref (file);
546 info = g_file_query_info (self->priv->gfile,
547 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
549 self->priv->content_type = g_strdup (g_file_info_get_content_type (info));
551 g_object_unref (info);
555 gtk_app_chooser_dialog_get_app_info (GtkAppChooser *object)
557 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
558 GAppInfo *app = NULL;
560 if (!check_application (self, &app))
567 gtk_app_chooser_dialog_refresh (GtkAppChooser *object)
569 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
571 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
575 gtk_app_chooser_dialog_constructed (GObject *object)
577 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
579 g_assert (self->priv->content_type != NULL ||
580 self->priv->gfile != NULL);
582 if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
583 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
585 build_dialog_ui (self);
586 set_dialog_properties (self);
587 ensure_online_button (self);
591 gtk_app_chooser_dialog_dispose (GObject *object)
593 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
595 g_clear_object (&self->priv->gfile);
596 g_clear_object (&self->priv->online);
598 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);
602 gtk_app_chooser_dialog_finalize (GObject *object)
604 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
606 g_free (self->priv->content_type);
608 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->finalize (object);
612 gtk_app_chooser_dialog_set_property (GObject *object,
617 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
622 set_gfile_and_content_type (self, g_value_get_object (value));
624 case PROP_CONTENT_TYPE:
625 /* don't try to override a value previously set with the GFile */
626 if (self->priv->content_type == NULL)
627 self->priv->content_type = g_value_dup_string (value);
630 gtk_app_chooser_dialog_set_heading (self, g_value_get_string (value));
633 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
639 gtk_app_chooser_dialog_get_property (GObject *object,
644 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
649 if (self->priv->gfile != NULL)
650 g_value_set_object (value, self->priv->gfile);
652 case PROP_CONTENT_TYPE:
653 g_value_set_string (value, self->priv->content_type);
656 g_value_set_string (value, self->priv->heading);
659 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
665 gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
667 iface->get_app_info = gtk_app_chooser_dialog_get_app_info;
668 iface->refresh = gtk_app_chooser_dialog_refresh;
672 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
674 GObjectClass *gobject_class;
677 gobject_class = G_OBJECT_CLASS (klass);
678 gobject_class->dispose = gtk_app_chooser_dialog_dispose;
679 gobject_class->finalize = gtk_app_chooser_dialog_finalize;
680 gobject_class->set_property = gtk_app_chooser_dialog_set_property;
681 gobject_class->get_property = gtk_app_chooser_dialog_get_property;
682 gobject_class->constructed = gtk_app_chooser_dialog_constructed;
684 g_object_class_override_property (gobject_class, PROP_CONTENT_TYPE, "content-type");
687 * GtkAppChooserDialog:gfile:
689 * The GFile used by the #GtkAppChooserDialog.
690 * The dialog's #GtkAppChooserWidget content type will be guessed from the
693 pspec = g_param_spec_object ("gfile",
695 P_("The GFile used by the app chooser dialog"),
697 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
698 G_PARAM_STATIC_STRINGS);
699 g_object_class_install_property (gobject_class, PROP_GFILE, pspec);
702 * GtkAppChooserDialog:heading:
704 * The text to show at the top of the dialog.
705 * The string may contain Pango markup.
707 pspec = g_param_spec_string ("heading",
709 P_("The text to show at the top of the dialog"),
711 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
712 g_object_class_install_property (gobject_class, PROP_HEADING, pspec);
715 g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
719 gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
721 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
722 GtkAppChooserDialogPrivate);
724 /* we can't override the class signal handler here, as it's a RUN_LAST;
725 * we want our signal handler instead to be executed before any user code.
727 g_signal_connect (self, "response",
728 G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
732 set_parent_and_flags (GtkWidget *dialog,
734 GtkDialogFlags flags)
737 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
739 if (flags & GTK_DIALOG_MODAL)
740 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
742 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
743 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
747 * gtk_app_chooser_dialog_new:
748 * @parent: (allow-none): a #GtkWindow, or %NULL
749 * @flags: flags for this dialog
752 * Creates a new #GtkAppChooserDialog for the provided #GFile,
753 * to allow the user to select an application for it.
755 * Returns: a newly created #GtkAppChooserDialog
760 gtk_app_chooser_dialog_new (GtkWindow *parent,
761 GtkDialogFlags flags,
766 g_return_val_if_fail (G_IS_FILE (file), NULL);
768 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
772 set_parent_and_flags (retval, parent, flags);
778 * gtk_app_chooser_dialog_new_for_content_type:
779 * @parent: (allow-none): a #GtkWindow, or %NULL
780 * @flags: flags for this dialog
781 * @content_type: a content type string
783 * Creates a new #GtkAppChooserDialog for the provided content type,
784 * to allow the user to select an application for it.
786 * Returns: a newly created #GtkAppChooserDialog
791 gtk_app_chooser_dialog_new_for_content_type (GtkWindow *parent,
792 GtkDialogFlags flags,
793 const gchar *content_type)
797 g_return_val_if_fail (content_type != NULL, NULL);
799 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
800 "content-type", content_type,
803 set_parent_and_flags (retval, parent, flags);
809 * gtk_app_chooser_dialog_get_widget:
810 * @self: a #GtkAppChooserDialog
812 * Returns the #GtkAppChooserWidget of this dialog.
814 * Returns: (transfer none): the #GtkAppChooserWidget of @self
819 gtk_app_chooser_dialog_get_widget (GtkAppChooserDialog *self)
821 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
823 return self->priv->app_chooser_widget;
827 * gtk_app_chooser_dialog_set_heading:
828 * @self: a #GtkAppChooserDialog
829 * @heading: a string containing Pango markup
831 * Sets the text to display at the top of the dialog.
832 * If the heading is not set, the dialog displays a default text.
835 gtk_app_chooser_dialog_set_heading (GtkAppChooserDialog *self,
836 const gchar *heading)
838 g_return_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self));
840 g_free (self->priv->heading);
841 self->priv->heading = g_strdup (heading);
843 if (self->priv->label && self->priv->heading)
844 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
846 g_object_notify (G_OBJECT (self), "heading");
850 * gtk_app_chooser_dialog_get_heading:
851 * @self: a #GtkAppChooserDialog
853 * Returns the text to display at the top of the dialog.
855 * Returns: the text to display at the top of the dialog, or %NULL, in which
856 * case a default text is displayed
859 gtk_app_chooser_dialog_get_heading (GtkAppChooserDialog *self)
861 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
863 return self->priv->heading;