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.
39 * To set the heading that is shown above the #GtkAppChooserWidget,
40 * use gtk_app_chooser_dialog_set_heading().
44 #include "gtkappchooserdialog.h"
47 #include "gtkappchooser.h"
48 #include "gtkappchooseronline.h"
49 #include "gtkappchooserprivate.h"
50 #include "gtkappchooserprivate.h"
52 #include "gtkmessagedialog.h"
55 #include "gtkbutton.h"
56 #include "gtkmenuitem.h"
60 #include <glib/gi18n-lib.h>
63 #define sure_string(s) ((const char *) ((s) != NULL ? (s) : ""))
65 struct _GtkAppChooserDialogPrivate {
72 GtkWidget *online_button;
74 GtkWidget *open_label;
76 GtkWidget *app_chooser_widget;
77 GtkWidget *show_more_button;
79 GtkAppChooserOnline *online;
80 GCancellable *online_cancellable;
82 gboolean show_more_clicked;
92 static void gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface);
93 G_DEFINE_TYPE_WITH_CODE (GtkAppChooserDialog, gtk_app_chooser_dialog, GTK_TYPE_DIALOG,
94 G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
95 gtk_app_chooser_dialog_iface_init));
98 show_error_dialog (const gchar *primary,
99 const gchar *secondary,
102 GtkWidget *message_dialog;
104 message_dialog = gtk_message_dialog_new (parent, 0,
108 g_object_set (message_dialog,
110 "secondary-text", secondary,
112 gtk_dialog_set_default_response (GTK_DIALOG (message_dialog), GTK_RESPONSE_OK);
114 gtk_widget_show (message_dialog);
116 g_signal_connect (message_dialog, "response",
117 G_CALLBACK (gtk_widget_destroy), NULL);
121 search_for_mimetype_ready_cb (GObject *source,
125 GtkAppChooserOnline *online = GTK_APP_CHOOSER_ONLINE (source);
126 GtkAppChooserDialog *self = user_data;
127 GError *error = NULL;
129 gdk_threads_enter ();
131 _gtk_app_chooser_online_search_for_mimetype_finish (online, res, &error);
133 if (self->priv->dismissed)
137 !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
139 show_error_dialog (_("Failed to look for applications online"),
140 error->message, GTK_WINDOW (self));
144 gtk_widget_set_sensitive (self->priv->online_button, TRUE);
145 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
149 g_clear_object (&self->priv->online_cancellable);
150 g_clear_error (&error);
151 g_object_unref (self);
153 gdk_threads_leave ();
157 online_button_clicked_cb (GtkButton *b,
160 GtkAppChooserDialog *self = user_data;
162 self->priv->online_cancellable = g_cancellable_new ();
163 gtk_widget_set_sensitive (self->priv->online_button, FALSE);
165 _gtk_app_chooser_online_search_for_mimetype_async (self->priv->online,
166 self->priv->content_type,
168 self->priv->online_cancellable,
169 search_for_mimetype_ready_cb,
170 g_object_ref (self));
174 app_chooser_online_get_default_ready_cb (GObject *source,
178 GtkAppChooserDialog *self = user_data;
180 gdk_threads_enter ();
182 self->priv->online = _gtk_app_chooser_online_get_default_finish (source, res);
184 if (self->priv->online != NULL &&
185 !self->priv->dismissed)
187 GtkWidget *action_area;
189 action_area = gtk_dialog_get_action_area (GTK_DIALOG (self));
190 self->priv->online_button = gtk_button_new_with_label (_("Find applications online"));
191 gtk_box_pack_start (GTK_BOX (action_area), self->priv->online_button,
193 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (action_area), self->priv->online_button,
195 g_signal_connect (self->priv->online_button, "clicked",
196 G_CALLBACK (online_button_clicked_cb), self);
199 if (!self->priv->content_type)
200 gtk_widget_set_sensitive (self->priv->online_button, FALSE);
202 gtk_widget_show (self->priv->online_button);
205 g_object_unref (self);
207 gdk_threads_leave ();
211 ensure_online_button (GtkAppChooserDialog *self)
213 _gtk_app_chooser_online_get_default_async (app_chooser_online_get_default_ready_cb,
214 g_object_ref (self));
217 /* An application is valid if:
220 * 2) The user has permissions to run the file
223 check_application (GtkAppChooserDialog *self,
230 GError *error = NULL;
236 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
243 command = g_app_info_get_executable (info);
245 g_shell_parse_argv (command, &argc, &argv, &error);
249 show_error_dialog (_("Could not run application"),
252 g_error_free (error);
257 path = g_find_program_in_path (argv[0]);
262 error_message = g_strdup_printf (_("Could not find '%s'"),
265 show_error_dialog (_("Could not find application"),
268 g_free (error_message);
283 add_or_find_application (GtkAppChooserDialog *self)
287 app = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
291 /* we don't care about reporting errors here */
292 if (self->priv->content_type)
293 g_app_info_set_as_last_used_for_type (app,
294 self->priv->content_type,
296 g_object_unref (app);
301 cancel_and_clear_cancellable (GtkAppChooserDialog *self)
303 if (self->priv->online_cancellable != NULL)
305 g_cancellable_cancel (self->priv->online_cancellable);
306 g_clear_object (&self->priv->online_cancellable);
311 gtk_app_chooser_dialog_response (GtkDialog *dialog,
315 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (dialog);
319 case GTK_RESPONSE_OK:
320 add_or_find_application (self);
322 case GTK_RESPONSE_CANCEL:
323 case GTK_RESPONSE_DELETE_EVENT:
324 cancel_and_clear_cancellable (self);
325 self->priv->dismissed = TRUE;
332 widget_application_selected_cb (GtkAppChooserWidget *widget,
336 GtkAppChooserDialog *self = user_data;
338 gtk_widget_set_sensitive (self->priv->button, TRUE);
342 widget_application_activated_cb (GtkAppChooserWidget *widget,
346 GtkAppChooserDialog *self = user_data;
348 gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
352 get_extension (const char *basename)
356 p = strrchr (basename, '.');
358 if (p && *(p + 1) != '\0')
359 return g_strdup (p + 1);
365 set_dialog_properties (GtkAppChooserDialog *self)
374 PangoFontDescription *font_desc;
382 if (self->priv->gfile != NULL)
384 name = g_file_get_basename (self->priv->gfile);
385 extension = get_extension (name);
388 if (self->priv->content_type)
390 description = g_content_type_get_description (self->priv->content_type);
391 unknown = g_content_type_is_unknown (self->priv->content_type);
394 gtk_window_set_title (GTK_WINDOW (self), "");
398 /* Translators: %s is a filename */
399 label = g_strdup_printf (_("Select an application to open \"%s\""), name);
400 string = g_strdup_printf (_("No applications available to open \"%s\""),
405 /* Translators: %s is a file type description */
406 label = g_strdup_printf (_("Select an application for \"%s\" files"),
407 unknown ? self->priv->content_type : description);
408 string = g_strdup_printf (_("No applications available to open \"%s\" files"),
409 unknown ? self->priv->content_type : description);
412 font_desc = pango_font_description_new ();
413 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
414 gtk_widget_override_font (self->priv->label, font_desc);
415 pango_font_description_free (font_desc);
417 if (self->priv->heading != NULL)
418 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
420 gtk_label_set_markup (GTK_LABEL (self->priv->label), label);
422 default_text = g_strdup_printf ("<big><b>%s</b></big>\n%s",
424 _("Click \"Show other applications\", for more options, or "
425 "\"Find applications online\" to install a new application"));
427 gtk_app_chooser_widget_set_default_text (GTK_APP_CHOOSER_WIDGET (self->priv->app_chooser_widget),
433 g_free (description);
435 g_free (default_text);
439 show_more_button_clicked_cb (GtkButton *button,
442 GtkAppChooserDialog *self = user_data;
444 g_object_set (self->priv->app_chooser_widget,
445 "show-recommended", TRUE,
446 "show-fallback", TRUE,
450 gtk_widget_hide (self->priv->show_more_button);
451 self->priv->show_more_clicked = TRUE;
455 widget_notify_for_button_cb (GObject *source,
459 GtkAppChooserDialog *self = user_data;
460 GtkAppChooserWidget *widget = GTK_APP_CHOOSER_WIDGET (source);
461 gboolean should_hide;
463 should_hide = gtk_app_chooser_widget_get_show_other (widget) ||
464 self->priv->show_more_clicked;
467 gtk_widget_hide (self->priv->show_more_button);
471 forget_menu_item_activate_cb (GtkMenuItem *item,
474 GtkAppChooserDialog *self = user_data;
477 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
481 g_app_info_remove_supports_type (info, self->priv->content_type, NULL);
483 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
485 g_object_unref (info);
490 build_forget_menu_item (GtkAppChooserDialog *self)
494 retval = gtk_menu_item_new_with_label (_("Forget association"));
495 gtk_widget_show (retval);
497 g_signal_connect (retval, "activate",
498 G_CALLBACK (forget_menu_item_activate_cb), self);
504 widget_populate_popup_cb (GtkAppChooserWidget *widget,
509 GtkAppChooserDialog *self = user_data;
510 GtkWidget *menu_item;
512 if (g_app_info_can_remove_supports_type (info))
514 menu_item = build_forget_menu_item (self);
515 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
520 build_dialog_ui (GtkAppChooserDialog *self)
524 GtkWidget *button, *w;
527 gtk_container_set_border_width (GTK_CONTAINER (self), 5);
529 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
530 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
531 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
532 gtk_widget_show (vbox);
534 vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
535 gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
536 gtk_widget_show (vbox2);
538 self->priv->label = gtk_label_new ("");
539 gtk_widget_set_halign (self->priv->label, GTK_ALIGN_START);
540 gtk_widget_set_valign (self->priv->label, GTK_ALIGN_CENTER);
541 gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
542 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
544 gtk_widget_show (self->priv->label);
546 self->priv->app_chooser_widget =
547 gtk_app_chooser_widget_new (self->priv->content_type);
548 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->app_chooser_widget, TRUE, TRUE, 0);
549 gtk_widget_show (self->priv->app_chooser_widget);
551 g_signal_connect (self->priv->app_chooser_widget, "application-selected",
552 G_CALLBACK (widget_application_selected_cb), self);
553 g_signal_connect (self->priv->app_chooser_widget, "application-activated",
554 G_CALLBACK (widget_application_activated_cb), self);
555 g_signal_connect (self->priv->app_chooser_widget, "notify::show-other",
556 G_CALLBACK (widget_notify_for_button_cb), self);
557 g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
558 G_CALLBACK (widget_populate_popup_cb), self);
560 button = gtk_button_new_with_label (_("Show other applications"));
561 self->priv->show_more_button = button;
562 w = gtk_image_new_from_stock (GTK_STOCK_ADD,
563 GTK_ICON_SIZE_BUTTON);
564 gtk_button_set_image (GTK_BUTTON (button), w);
565 gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget), button, FALSE, FALSE, 6);
566 gtk_widget_show_all (button);
568 g_signal_connect (button, "clicked",
569 G_CALLBACK (show_more_button_clicked_cb), self);
571 gtk_dialog_add_button (GTK_DIALOG (self),
573 GTK_RESPONSE_CANCEL);
575 self->priv->button = gtk_dialog_add_button (GTK_DIALOG (self),
579 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
580 gtk_widget_set_sensitive (self->priv->button, info != NULL);
582 g_object_unref (info);
584 gtk_dialog_set_default_response (GTK_DIALOG (self),
589 set_gfile_and_content_type (GtkAppChooserDialog *self,
597 self->priv->gfile = g_object_ref (file);
599 info = g_file_query_info (self->priv->gfile,
600 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
602 self->priv->content_type = g_strdup (g_file_info_get_content_type (info));
604 g_object_unref (info);
608 gtk_app_chooser_dialog_get_app_info (GtkAppChooser *object)
610 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
611 GAppInfo *app = NULL;
613 if (!check_application (self, &app))
620 gtk_app_chooser_dialog_refresh (GtkAppChooser *object)
622 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
624 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
628 gtk_app_chooser_dialog_constructed (GObject *object)
630 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
632 if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
633 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
635 build_dialog_ui (self);
636 set_dialog_properties (self);
637 ensure_online_button (self);
641 gtk_app_chooser_dialog_dispose (GObject *object)
643 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
645 g_clear_object (&self->priv->gfile);
646 cancel_and_clear_cancellable (self);
647 g_clear_object (&self->priv->online);
649 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);
653 gtk_app_chooser_dialog_finalize (GObject *object)
655 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
657 g_free (self->priv->content_type);
659 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->finalize (object);
663 gtk_app_chooser_dialog_set_property (GObject *object,
668 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
673 set_gfile_and_content_type (self, g_value_get_object (value));
675 case PROP_CONTENT_TYPE:
676 /* don't try to override a value previously set with the GFile */
677 if (self->priv->content_type == NULL)
678 self->priv->content_type = g_value_dup_string (value);
681 gtk_app_chooser_dialog_set_heading (self, g_value_get_string (value));
684 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
690 gtk_app_chooser_dialog_get_property (GObject *object,
695 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
700 if (self->priv->gfile != NULL)
701 g_value_set_object (value, self->priv->gfile);
703 case PROP_CONTENT_TYPE:
704 g_value_set_string (value, self->priv->content_type);
707 g_value_set_string (value, self->priv->heading);
710 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
716 gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
718 iface->get_app_info = gtk_app_chooser_dialog_get_app_info;
719 iface->refresh = gtk_app_chooser_dialog_refresh;
723 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
725 GObjectClass *gobject_class;
728 gobject_class = G_OBJECT_CLASS (klass);
729 gobject_class->dispose = gtk_app_chooser_dialog_dispose;
730 gobject_class->finalize = gtk_app_chooser_dialog_finalize;
731 gobject_class->set_property = gtk_app_chooser_dialog_set_property;
732 gobject_class->get_property = gtk_app_chooser_dialog_get_property;
733 gobject_class->constructed = gtk_app_chooser_dialog_constructed;
735 g_object_class_override_property (gobject_class, PROP_CONTENT_TYPE, "content-type");
738 * GtkAppChooserDialog:gfile:
740 * The GFile used by the #GtkAppChooserDialog.
741 * The dialog's #GtkAppChooserWidget content type will be guessed from the
744 pspec = g_param_spec_object ("gfile",
746 P_("The GFile used by the app chooser dialog"),
748 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
749 G_PARAM_STATIC_STRINGS);
750 g_object_class_install_property (gobject_class, PROP_GFILE, pspec);
753 * GtkAppChooserDialog:heading:
755 * The text to show at the top of the dialog.
756 * The string may contain Pango markup.
758 pspec = g_param_spec_string ("heading",
760 P_("The text to show at the top of the dialog"),
762 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
763 g_object_class_install_property (gobject_class, PROP_HEADING, pspec);
766 g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
770 gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
772 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
773 GtkAppChooserDialogPrivate);
775 /* we can't override the class signal handler here, as it's a RUN_LAST;
776 * we want our signal handler instead to be executed before any user code.
778 g_signal_connect (self, "response",
779 G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
783 set_parent_and_flags (GtkWidget *dialog,
785 GtkDialogFlags flags)
788 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
790 if (flags & GTK_DIALOG_MODAL)
791 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
793 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
794 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
798 * gtk_app_chooser_dialog_new:
799 * @parent: (allow-none): a #GtkWindow, or %NULL
800 * @flags: flags for this dialog
803 * Creates a new #GtkAppChooserDialog for the provided #GFile,
804 * to allow the user to select an application for it.
806 * Returns: a newly created #GtkAppChooserDialog
811 gtk_app_chooser_dialog_new (GtkWindow *parent,
812 GtkDialogFlags flags,
817 g_return_val_if_fail (G_IS_FILE (file), NULL);
819 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
823 set_parent_and_flags (retval, parent, flags);
829 * gtk_app_chooser_dialog_new_for_content_type:
830 * @parent: (allow-none): a #GtkWindow, or %NULL
831 * @flags: flags for this dialog
832 * @content_type: a content type string
834 * Creates a new #GtkAppChooserDialog for the provided content type,
835 * to allow the user to select an application for it.
837 * Returns: a newly created #GtkAppChooserDialog
842 gtk_app_chooser_dialog_new_for_content_type (GtkWindow *parent,
843 GtkDialogFlags flags,
844 const gchar *content_type)
848 g_return_val_if_fail (content_type != NULL, NULL);
850 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
851 "content-type", content_type,
854 set_parent_and_flags (retval, parent, flags);
860 * gtk_app_chooser_dialog_get_widget:
861 * @self: a #GtkAppChooserDialog
863 * Returns the #GtkAppChooserWidget of this dialog.
865 * Returns: (transfer none): the #GtkAppChooserWidget of @self
870 gtk_app_chooser_dialog_get_widget (GtkAppChooserDialog *self)
872 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
874 return self->priv->app_chooser_widget;
878 * gtk_app_chooser_dialog_set_heading:
879 * @self: a #GtkAppChooserDialog
880 * @heading: a string containing Pango markup
882 * Sets the text to display at the top of the dialog.
883 * If the heading is not set, the dialog displays a default text.
886 gtk_app_chooser_dialog_set_heading (GtkAppChooserDialog *self,
887 const gchar *heading)
889 g_return_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self));
891 g_free (self->priv->heading);
892 self->priv->heading = g_strdup (heading);
894 if (self->priv->label && self->priv->heading)
895 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
897 g_object_notify (G_OBJECT (self), "heading");
901 * gtk_app_chooser_dialog_get_heading:
902 * @self: a #GtkAppChooserDialog
904 * Returns the text to display at the top of the dialog.
906 * Returns: the text to display at the top of the dialog, or %NULL, in which
907 * case a default text is displayed
910 gtk_app_chooser_dialog_get_heading (GtkAppChooserDialog *self)
912 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
914 return self->priv->heading;