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;
87 static void gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface);
88 G_DEFINE_TYPE_WITH_CODE (GtkAppChooserDialog, gtk_app_chooser_dialog, GTK_TYPE_DIALOG,
89 G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
90 gtk_app_chooser_dialog_iface_init));
93 show_error_dialog (const gchar *primary,
94 const gchar *secondary,
97 GtkWidget *message_dialog;
99 message_dialog = gtk_message_dialog_new (parent, 0,
103 g_object_set (message_dialog,
105 "secondary-text", secondary,
107 gtk_dialog_set_default_response (GTK_DIALOG (message_dialog), GTK_RESPONSE_OK);
109 gtk_widget_show (message_dialog);
111 g_signal_connect (message_dialog, "response",
112 G_CALLBACK (gtk_widget_destroy), NULL);
116 search_for_mimetype_ready_cb (GObject *source,
120 GtkAppChooserOnline *online = GTK_APP_CHOOSER_ONLINE (source);
121 GtkAppChooserDialog *self = user_data;
122 GError *error = NULL;
124 gdk_threads_enter ();
126 _gtk_app_chooser_online_search_for_mimetype_finish (online, res, &error);
130 show_error_dialog (_("Failed to look for applications online"),
131 error->message, GTK_WINDOW (self));
132 g_error_free (error);
136 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
139 gdk_threads_leave ();
143 online_button_clicked_cb (GtkButton *b,
146 GtkAppChooserDialog *self = user_data;
148 _gtk_app_chooser_online_search_for_mimetype_async (self->priv->online,
149 self->priv->content_type,
151 search_for_mimetype_ready_cb,
156 app_chooser_online_get_default_ready_cb (GObject *source,
160 GtkAppChooserDialog *self = user_data;
162 gdk_threads_enter ();
164 self->priv->online = _gtk_app_chooser_online_get_default_finish (source, res);
166 if (self->priv->online != NULL)
168 GtkWidget *action_area;
170 action_area = gtk_dialog_get_action_area (GTK_DIALOG (self));
171 self->priv->online_button = gtk_button_new_with_label (_("Find applications online"));
172 gtk_box_pack_start (GTK_BOX (action_area), self->priv->online_button,
174 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (action_area), self->priv->online_button,
176 g_signal_connect (self->priv->online_button, "clicked",
177 G_CALLBACK (online_button_clicked_cb), self);
180 if (!self->priv->content_type)
181 gtk_widget_set_sensitive (self->priv->online_button, FALSE);
183 gtk_widget_show (self->priv->online_button);
186 gdk_threads_leave ();
190 ensure_online_button (GtkAppChooserDialog *self)
192 _gtk_app_chooser_online_get_default_async (app_chooser_online_get_default_ready_cb, self);
195 /* An application is valid if:
198 * 2) The user has permissions to run the file
201 check_application (GtkAppChooserDialog *self,
208 GError *error = NULL;
214 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
221 command = g_app_info_get_executable (info);
223 g_shell_parse_argv (command, &argc, &argv, &error);
227 show_error_dialog (_("Could not run application"),
230 g_error_free (error);
235 path = g_find_program_in_path (argv[0]);
240 error_message = g_strdup_printf (_("Could not find '%s'"),
243 show_error_dialog (_("Could not find application"),
246 g_free (error_message);
261 add_or_find_application (GtkAppChooserDialog *self)
265 app = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
269 /* we don't care about reporting errors here */
270 if (self->priv->content_type)
271 g_app_info_set_as_last_used_for_type (app,
272 self->priv->content_type,
274 g_object_unref (app);
279 gtk_app_chooser_dialog_response (GtkDialog *dialog,
283 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (dialog);
287 case GTK_RESPONSE_OK:
288 add_or_find_application (self);
296 widget_application_selected_cb (GtkAppChooserWidget *widget,
300 GtkAppChooserDialog *self = user_data;
302 gtk_widget_set_sensitive (self->priv->button, TRUE);
306 widget_application_activated_cb (GtkAppChooserWidget *widget,
310 GtkAppChooserDialog *self = user_data;
312 gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
316 get_extension (const char *basename)
320 p = strrchr (basename, '.');
322 if (p && *(p + 1) != '\0')
323 return g_strdup (p + 1);
329 set_dialog_properties (GtkAppChooserDialog *self)
338 PangoFontDescription *font_desc;
346 if (self->priv->gfile != NULL)
348 name = g_file_get_basename (self->priv->gfile);
349 extension = get_extension (name);
352 if (self->priv->content_type)
354 description = g_content_type_get_description (self->priv->content_type);
355 unknown = g_content_type_is_unknown (self->priv->content_type);
358 gtk_window_set_title (GTK_WINDOW (self), "");
362 /* Translators: %s is a filename */
363 label = g_strdup_printf (_("Select an application to open \"%s\""), name);
364 string = g_strdup_printf (_("No applications available to open \"%s\""),
369 /* Translators: %s is a file type description */
370 label = g_strdup_printf (_("Select an application for \"%s\" files"),
371 unknown ? self->priv->content_type : description);
372 string = g_strdup_printf (_("No applications available to open \"%s\" files"),
373 unknown ? self->priv->content_type : description);
376 font_desc = pango_font_description_new ();
377 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
378 gtk_widget_override_font (self->priv->label, font_desc);
379 pango_font_description_free (font_desc);
381 if (self->priv->heading != NULL)
382 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
384 gtk_label_set_markup (GTK_LABEL (self->priv->label), label);
386 default_text = g_strdup_printf ("<big><b>%s</b></big>\n%s",
388 _("Click \"Show other applications\", for more options, or "
389 "\"Find applications online\" to install a new application"));
391 gtk_app_chooser_widget_set_default_text (GTK_APP_CHOOSER_WIDGET (self->priv->app_chooser_widget),
397 g_free (description);
399 g_free (default_text);
403 show_more_button_clicked_cb (GtkButton *button,
406 GtkAppChooserDialog *self = user_data;
408 g_object_set (self->priv->app_chooser_widget,
409 "show-recommended", TRUE,
410 "show-fallback", TRUE,
414 gtk_widget_hide (self->priv->show_more_button);
415 self->priv->show_more_clicked = TRUE;
419 widget_notify_for_button_cb (GObject *source,
423 GtkAppChooserDialog *self = user_data;
424 GtkAppChooserWidget *widget = GTK_APP_CHOOSER_WIDGET (source);
425 gboolean should_hide;
427 should_hide = gtk_app_chooser_widget_get_show_other (widget) ||
428 self->priv->show_more_clicked;
431 gtk_widget_hide (self->priv->show_more_button);
435 forget_menu_item_activate_cb (GtkMenuItem *item,
438 GtkAppChooserDialog *self = user_data;
441 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
445 g_app_info_remove_supports_type (info, self->priv->content_type, NULL);
447 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
449 g_object_unref (info);
454 build_forget_menu_item (GtkAppChooserDialog *self)
458 retval = gtk_menu_item_new_with_label (_("Forget association"));
459 gtk_widget_show (retval);
461 g_signal_connect (retval, "activate",
462 G_CALLBACK (forget_menu_item_activate_cb), self);
468 widget_populate_popup_cb (GtkAppChooserWidget *widget,
473 GtkAppChooserDialog *self = user_data;
474 GtkWidget *menu_item;
476 if (g_app_info_can_remove_supports_type (info))
478 menu_item = build_forget_menu_item (self);
479 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
484 build_dialog_ui (GtkAppChooserDialog *self)
488 GtkWidget *button, *w;
490 gtk_container_set_border_width (GTK_CONTAINER (self), 5);
492 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
493 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
494 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
495 gtk_widget_show (vbox);
497 vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
498 gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
499 gtk_widget_show (vbox2);
501 self->priv->label = gtk_label_new ("");
502 gtk_widget_set_halign (self->priv->label, GTK_ALIGN_START);
503 gtk_widget_set_valign (self->priv->label, GTK_ALIGN_CENTER);
504 gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
505 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
507 gtk_widget_show (self->priv->label);
509 self->priv->app_chooser_widget =
510 gtk_app_chooser_widget_new (self->priv->content_type);
511 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->app_chooser_widget, TRUE, TRUE, 0);
512 gtk_widget_show (self->priv->app_chooser_widget);
514 g_signal_connect (self->priv->app_chooser_widget, "application-selected",
515 G_CALLBACK (widget_application_selected_cb), self);
516 g_signal_connect (self->priv->app_chooser_widget, "application-activated",
517 G_CALLBACK (widget_application_activated_cb), self);
518 g_signal_connect (self->priv->app_chooser_widget, "notify::show-other",
519 G_CALLBACK (widget_notify_for_button_cb), self);
520 g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
521 G_CALLBACK (widget_populate_popup_cb), self);
523 button = gtk_button_new_with_label (_("Show other applications"));
524 self->priv->show_more_button = button;
525 w = gtk_image_new_from_stock (GTK_STOCK_ADD,
526 GTK_ICON_SIZE_BUTTON);
527 gtk_button_set_image (GTK_BUTTON (button), w);
528 gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget), button, FALSE, FALSE, 6);
529 gtk_widget_show_all (button);
531 g_signal_connect (button, "clicked",
532 G_CALLBACK (show_more_button_clicked_cb), self);
534 gtk_dialog_add_button (GTK_DIALOG (self),
536 GTK_RESPONSE_CANCEL);
538 self->priv->button = gtk_dialog_add_button (GTK_DIALOG (self),
542 gtk_dialog_set_default_response (GTK_DIALOG (self),
547 set_gfile_and_content_type (GtkAppChooserDialog *self,
555 self->priv->gfile = g_object_ref (file);
557 info = g_file_query_info (self->priv->gfile,
558 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
560 self->priv->content_type = g_strdup (g_file_info_get_content_type (info));
562 g_object_unref (info);
566 gtk_app_chooser_dialog_get_app_info (GtkAppChooser *object)
568 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
569 GAppInfo *app = NULL;
571 if (!check_application (self, &app))
578 gtk_app_chooser_dialog_refresh (GtkAppChooser *object)
580 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
582 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
586 gtk_app_chooser_dialog_constructed (GObject *object)
588 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
590 if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
591 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
593 build_dialog_ui (self);
594 set_dialog_properties (self);
595 ensure_online_button (self);
599 gtk_app_chooser_dialog_dispose (GObject *object)
601 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
603 g_clear_object (&self->priv->gfile);
604 g_clear_object (&self->priv->online);
606 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);
610 gtk_app_chooser_dialog_finalize (GObject *object)
612 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
614 g_free (self->priv->content_type);
616 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->finalize (object);
620 gtk_app_chooser_dialog_set_property (GObject *object,
625 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
630 set_gfile_and_content_type (self, g_value_get_object (value));
632 case PROP_CONTENT_TYPE:
633 /* don't try to override a value previously set with the GFile */
634 if (self->priv->content_type == NULL)
635 self->priv->content_type = g_value_dup_string (value);
638 gtk_app_chooser_dialog_set_heading (self, g_value_get_string (value));
641 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
647 gtk_app_chooser_dialog_get_property (GObject *object,
652 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
657 if (self->priv->gfile != NULL)
658 g_value_set_object (value, self->priv->gfile);
660 case PROP_CONTENT_TYPE:
661 g_value_set_string (value, self->priv->content_type);
664 g_value_set_string (value, self->priv->heading);
667 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
673 gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
675 iface->get_app_info = gtk_app_chooser_dialog_get_app_info;
676 iface->refresh = gtk_app_chooser_dialog_refresh;
680 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
682 GObjectClass *gobject_class;
685 gobject_class = G_OBJECT_CLASS (klass);
686 gobject_class->dispose = gtk_app_chooser_dialog_dispose;
687 gobject_class->finalize = gtk_app_chooser_dialog_finalize;
688 gobject_class->set_property = gtk_app_chooser_dialog_set_property;
689 gobject_class->get_property = gtk_app_chooser_dialog_get_property;
690 gobject_class->constructed = gtk_app_chooser_dialog_constructed;
692 g_object_class_override_property (gobject_class, PROP_CONTENT_TYPE, "content-type");
695 * GtkAppChooserDialog:gfile:
697 * The GFile used by the #GtkAppChooserDialog.
698 * The dialog's #GtkAppChooserWidget content type will be guessed from the
701 pspec = g_param_spec_object ("gfile",
703 P_("The GFile used by the app chooser dialog"),
705 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
706 G_PARAM_STATIC_STRINGS);
707 g_object_class_install_property (gobject_class, PROP_GFILE, pspec);
710 * GtkAppChooserDialog:heading:
712 * The text to show at the top of the dialog.
713 * The string may contain Pango markup.
715 pspec = g_param_spec_string ("heading",
717 P_("The text to show at the top of the dialog"),
719 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
720 g_object_class_install_property (gobject_class, PROP_HEADING, pspec);
723 g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
727 gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
729 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
730 GtkAppChooserDialogPrivate);
732 /* we can't override the class signal handler here, as it's a RUN_LAST;
733 * we want our signal handler instead to be executed before any user code.
735 g_signal_connect (self, "response",
736 G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
740 set_parent_and_flags (GtkWidget *dialog,
742 GtkDialogFlags flags)
745 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
747 if (flags & GTK_DIALOG_MODAL)
748 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
750 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
751 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
755 * gtk_app_chooser_dialog_new:
756 * @parent: (allow-none): a #GtkWindow, or %NULL
757 * @flags: flags for this dialog
760 * Creates a new #GtkAppChooserDialog for the provided #GFile,
761 * to allow the user to select an application for it.
763 * Returns: a newly created #GtkAppChooserDialog
768 gtk_app_chooser_dialog_new (GtkWindow *parent,
769 GtkDialogFlags flags,
774 g_return_val_if_fail (G_IS_FILE (file), NULL);
776 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
780 set_parent_and_flags (retval, parent, flags);
786 * gtk_app_chooser_dialog_new_for_content_type:
787 * @parent: (allow-none): a #GtkWindow, or %NULL
788 * @flags: flags for this dialog
789 * @content_type: a content type string
791 * Creates a new #GtkAppChooserDialog for the provided content type,
792 * to allow the user to select an application for it.
794 * Returns: a newly created #GtkAppChooserDialog
799 gtk_app_chooser_dialog_new_for_content_type (GtkWindow *parent,
800 GtkDialogFlags flags,
801 const gchar *content_type)
805 g_return_val_if_fail (content_type != NULL, NULL);
807 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
808 "content-type", content_type,
811 set_parent_and_flags (retval, parent, flags);
817 * gtk_app_chooser_dialog_get_widget:
818 * @self: a #GtkAppChooserDialog
820 * Returns the #GtkAppChooserWidget of this dialog.
822 * Returns: (transfer none): the #GtkAppChooserWidget of @self
827 gtk_app_chooser_dialog_get_widget (GtkAppChooserDialog *self)
829 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
831 return self->priv->app_chooser_widget;
835 * gtk_app_chooser_dialog_set_heading:
836 * @self: a #GtkAppChooserDialog
837 * @heading: a string containing Pango markup
839 * Sets the text to display at the top of the dialog.
840 * If the heading is not set, the dialog displays a default text.
843 gtk_app_chooser_dialog_set_heading (GtkAppChooserDialog *self,
844 const gchar *heading)
846 g_return_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self));
848 g_free (self->priv->heading);
849 self->priv->heading = g_strdup (heading);
851 if (self->priv->label && self->priv->heading)
852 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
854 g_object_notify (G_OBJECT (self), "heading");
858 * gtk_app_chooser_dialog_get_heading:
859 * @self: a #GtkAppChooserDialog
861 * Returns the text to display at the top of the dialog.
863 * Returns: the text to display at the top of the dialog, or %NULL, in which
864 * case a default text is displayed
867 gtk_app_chooser_dialog_get_heading (GtkAppChooserDialog *self)
869 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
871 return self->priv->heading;