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));
215 command = g_app_info_get_executable (info);
217 g_shell_parse_argv (command, &argc, &argv, &error);
221 show_error_dialog (_("Could not run application"),
224 g_error_free (error);
229 path = g_find_program_in_path (argv[0]);
234 error_message = g_strdup_printf (_("Could not find '%s'"),
237 show_error_dialog (_("Could not find application"),
240 g_free (error_message);
255 add_or_find_application (GtkAppChooserDialog *self)
259 app = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
261 /* we don't care about reporting errors here */
262 if (self->priv->content_type)
263 g_app_info_set_as_last_used_for_type (app,
264 self->priv->content_type,
267 g_object_unref (app);
271 gtk_app_chooser_dialog_response (GtkDialog *dialog,
275 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (dialog);
279 case GTK_RESPONSE_OK:
280 add_or_find_application (self);
288 widget_application_selected_cb (GtkAppChooserWidget *widget,
292 GtkAppChooserDialog *self = user_data;
294 gtk_widget_set_sensitive (self->priv->button, TRUE);
298 widget_application_activated_cb (GtkAppChooserWidget *widget,
302 GtkAppChooserDialog *self = user_data;
304 gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
308 get_extension (const char *basename)
312 p = strrchr (basename, '.');
314 if (p && *(p + 1) != '\0')
315 return g_strdup (p + 1);
321 set_dialog_properties (GtkAppChooserDialog *self)
330 PangoFontDescription *font_desc;
338 if (self->priv->gfile != NULL)
340 name = g_file_get_basename (self->priv->gfile);
341 extension = get_extension (name);
344 if (self->priv->content_type)
346 description = g_content_type_get_description (self->priv->content_type);
347 unknown = g_content_type_is_unknown (self->priv->content_type);
350 gtk_window_set_title (GTK_WINDOW (self), "");
354 /* Translators: %s is a filename */
355 label = g_strdup_printf (_("Select an application to open \"%s\""), name);
356 string = g_strdup_printf (_("No applications available to open \"%s\""),
361 /* Translators: %s is a file type description */
362 label = g_strdup_printf (_("Select an application for \"%s\" files"),
363 unknown ? self->priv->content_type : description);
364 string = g_strdup_printf (_("No applications available to open \"%s\" files"),
365 unknown ? self->priv->content_type : description);
368 font_desc = pango_font_description_new ();
369 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
370 gtk_widget_override_font (self->priv->label, font_desc);
371 pango_font_description_free (font_desc);
373 if (self->priv->heading != NULL)
374 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
376 gtk_label_set_markup (GTK_LABEL (self->priv->label), label);
378 default_text = g_strdup_printf ("<big><b>%s</b></big>\n%s",
380 _("Click \"Show other applications\", for more options, or "
381 "\"Find applications online\" to install a new application"));
383 gtk_app_chooser_widget_set_default_text (GTK_APP_CHOOSER_WIDGET (self->priv->app_chooser_widget),
389 g_free (description);
391 g_free (default_text);
395 show_more_button_clicked_cb (GtkButton *button,
398 GtkAppChooserDialog *self = user_data;
400 g_object_set (self->priv->app_chooser_widget,
401 "show-recommended", TRUE,
402 "show-fallback", TRUE,
406 gtk_widget_hide (self->priv->show_more_button);
407 self->priv->show_more_clicked = TRUE;
411 widget_notify_for_button_cb (GObject *source,
415 GtkAppChooserDialog *self = user_data;
416 GtkAppChooserWidget *widget = GTK_APP_CHOOSER_WIDGET (source);
417 gboolean should_hide;
419 should_hide = gtk_app_chooser_widget_get_show_other (widget) ||
420 self->priv->show_more_clicked;
423 gtk_widget_hide (self->priv->show_more_button);
427 forget_menu_item_activate_cb (GtkMenuItem *item,
430 GtkAppChooserDialog *self = user_data;
433 info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
437 g_app_info_remove_supports_type (info, self->priv->content_type, NULL);
439 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
441 g_object_unref (info);
446 build_forget_menu_item (GtkAppChooserDialog *self)
450 retval = gtk_menu_item_new_with_label (_("Forget association"));
451 gtk_widget_show (retval);
453 g_signal_connect (retval, "activate",
454 G_CALLBACK (forget_menu_item_activate_cb), self);
460 widget_populate_popup_cb (GtkAppChooserWidget *widget,
465 GtkAppChooserDialog *self = user_data;
466 GtkWidget *menu_item;
468 if (g_app_info_can_remove_supports_type (info))
470 menu_item = build_forget_menu_item (self);
471 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
476 build_dialog_ui (GtkAppChooserDialog *self)
481 GtkWidget *button, *w;
483 gtk_container_set_border_width (GTK_CONTAINER (self), 5);
485 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
486 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
487 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
488 gtk_widget_show (vbox);
490 vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
491 gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
492 gtk_widget_show (vbox2);
494 self->priv->label = gtk_label_new ("");
495 gtk_misc_set_alignment (GTK_MISC (self->priv->label), 0, 0.5);
496 gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
497 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
499 gtk_widget_show (self->priv->label);
501 self->priv->app_chooser_widget =
502 gtk_app_chooser_widget_new (self->priv->content_type);
503 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->app_chooser_widget, TRUE, TRUE, 0);
504 gtk_widget_show (self->priv->app_chooser_widget);
506 g_signal_connect (self->priv->app_chooser_widget, "application-selected",
507 G_CALLBACK (widget_application_selected_cb), self);
508 g_signal_connect (self->priv->app_chooser_widget, "application-activated",
509 G_CALLBACK (widget_application_activated_cb), self);
510 g_signal_connect (self->priv->app_chooser_widget, "notify::show-other",
511 G_CALLBACK (widget_notify_for_button_cb), self);
512 g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
513 G_CALLBACK (widget_populate_popup_cb), self);
515 button = gtk_button_new_with_label (_("Show other applications"));
516 self->priv->show_more_button = button;
517 w = gtk_image_new_from_stock (GTK_STOCK_ADD,
518 GTK_ICON_SIZE_BUTTON);
519 gtk_button_set_image (GTK_BUTTON (button), w);
520 gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget), button, FALSE, FALSE, 6);
521 gtk_widget_show_all (button);
523 g_signal_connect (button, "clicked",
524 G_CALLBACK (show_more_button_clicked_cb), self);
526 gtk_dialog_add_button (GTK_DIALOG (self),
528 GTK_RESPONSE_CANCEL);
530 /* Create a custom stock icon */
531 self->priv->button = gtk_button_new ();
533 label = gtk_label_new_with_mnemonic (_("_Select"));
534 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (self->priv->button));
535 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
536 gtk_widget_show (label);
537 self->priv->open_label = label;
539 gtk_container_add (GTK_CONTAINER (self->priv->button),
540 self->priv->open_label);
542 gtk_widget_show (self->priv->button);
543 gtk_widget_set_can_default (self->priv->button, TRUE);
545 gtk_dialog_add_action_widget (GTK_DIALOG (self),
546 self->priv->button, GTK_RESPONSE_OK);
548 gtk_dialog_set_default_response (GTK_DIALOG (self),
553 set_gfile_and_content_type (GtkAppChooserDialog *self,
561 self->priv->gfile = g_object_ref (file);
563 info = g_file_query_info (self->priv->gfile,
564 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
566 self->priv->content_type = g_strdup (g_file_info_get_content_type (info));
568 g_object_unref (info);
572 gtk_app_chooser_dialog_get_app_info (GtkAppChooser *object)
574 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
575 GAppInfo *app = NULL;
577 if (!check_application (self, &app))
584 gtk_app_chooser_dialog_refresh (GtkAppChooser *object)
586 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
588 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
592 gtk_app_chooser_dialog_constructed (GObject *object)
594 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
596 if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
597 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
599 build_dialog_ui (self);
600 set_dialog_properties (self);
601 ensure_online_button (self);
605 gtk_app_chooser_dialog_dispose (GObject *object)
607 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
609 g_clear_object (&self->priv->gfile);
610 g_clear_object (&self->priv->online);
612 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);
616 gtk_app_chooser_dialog_finalize (GObject *object)
618 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
620 g_free (self->priv->content_type);
622 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->finalize (object);
626 gtk_app_chooser_dialog_set_property (GObject *object,
631 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
636 set_gfile_and_content_type (self, g_value_get_object (value));
638 case PROP_CONTENT_TYPE:
639 /* don't try to override a value previously set with the GFile */
640 if (self->priv->content_type == NULL)
641 self->priv->content_type = g_value_dup_string (value);
644 gtk_app_chooser_dialog_set_heading (self, g_value_get_string (value));
647 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
653 gtk_app_chooser_dialog_get_property (GObject *object,
658 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
663 if (self->priv->gfile != NULL)
664 g_value_set_object (value, self->priv->gfile);
666 case PROP_CONTENT_TYPE:
667 g_value_set_string (value, self->priv->content_type);
670 g_value_set_string (value, self->priv->heading);
673 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
679 gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
681 iface->get_app_info = gtk_app_chooser_dialog_get_app_info;
682 iface->refresh = gtk_app_chooser_dialog_refresh;
686 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
688 GObjectClass *gobject_class;
691 gobject_class = G_OBJECT_CLASS (klass);
692 gobject_class->dispose = gtk_app_chooser_dialog_dispose;
693 gobject_class->finalize = gtk_app_chooser_dialog_finalize;
694 gobject_class->set_property = gtk_app_chooser_dialog_set_property;
695 gobject_class->get_property = gtk_app_chooser_dialog_get_property;
696 gobject_class->constructed = gtk_app_chooser_dialog_constructed;
698 g_object_class_override_property (gobject_class, PROP_CONTENT_TYPE, "content-type");
701 * GtkAppChooserDialog:gfile:
703 * The GFile used by the #GtkAppChooserDialog.
704 * The dialog's #GtkAppChooserWidget content type will be guessed from the
707 pspec = g_param_spec_object ("gfile",
709 P_("The GFile used by the app chooser dialog"),
711 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
712 G_PARAM_STATIC_STRINGS);
713 g_object_class_install_property (gobject_class, PROP_GFILE, pspec);
716 * GtkAppChooserDialog:heading:
718 * The text to show at the top of the dialog.
719 * The string may contain Pango markup.
721 pspec = g_param_spec_string ("heading",
723 P_("The text to show at the top of the dialog"),
725 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
726 g_object_class_install_property (gobject_class, PROP_HEADING, pspec);
729 g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
733 gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
735 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
736 GtkAppChooserDialogPrivate);
738 /* we can't override the class signal handler here, as it's a RUN_LAST;
739 * we want our signal handler instead to be executed before any user code.
741 g_signal_connect (self, "response",
742 G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
746 set_parent_and_flags (GtkWidget *dialog,
748 GtkDialogFlags flags)
751 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
753 if (flags & GTK_DIALOG_MODAL)
754 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
756 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
757 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
761 * gtk_app_chooser_dialog_new:
762 * @parent: (allow-none): a #GtkWindow, or %NULL
763 * @flags: flags for this dialog
766 * Creates a new #GtkAppChooserDialog for the provided #GFile,
767 * to allow the user to select an application for it.
769 * Returns: a newly created #GtkAppChooserDialog
774 gtk_app_chooser_dialog_new (GtkWindow *parent,
775 GtkDialogFlags flags,
780 g_return_val_if_fail (G_IS_FILE (file), NULL);
782 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
786 set_parent_and_flags (retval, parent, flags);
792 * gtk_app_chooser_dialog_new_for_content_type:
793 * @parent: (allow-none): a #GtkWindow, or %NULL
794 * @flags: flags for this dialog
795 * @content_type: a content type string
797 * Creates a new #GtkAppChooserDialog for the provided content type,
798 * to allow the user to select an application for it.
800 * Returns: a newly created #GtkAppChooserDialog
805 gtk_app_chooser_dialog_new_for_content_type (GtkWindow *parent,
806 GtkDialogFlags flags,
807 const gchar *content_type)
811 g_return_val_if_fail (content_type != NULL, NULL);
813 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
814 "content-type", content_type,
817 set_parent_and_flags (retval, parent, flags);
823 * gtk_app_chooser_dialog_get_widget:
824 * @self: a #GtkAppChooserDialog
826 * Returns the #GtkAppChooserWidget of this dialog.
828 * Returns: (transfer none): the #GtkAppChooserWidget of @self
833 gtk_app_chooser_dialog_get_widget (GtkAppChooserDialog *self)
835 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
837 return self->priv->app_chooser_widget;
841 * gtk_app_chooser_dialog_set_heading:
842 * @self: a #GtkAppChooserDialog
843 * @heading: a string containing Pango markup
845 * Sets the text to display at the top of the dialog.
846 * If the heading is not set, the dialog displays a default text.
849 gtk_app_chooser_dialog_set_heading (GtkAppChooserDialog *self,
850 const gchar *heading)
852 g_return_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self));
854 g_free (self->priv->heading);
855 self->priv->heading = g_strdup (heading);
857 if (self->priv->label && self->priv->heading)
858 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
860 g_object_notify (G_OBJECT (self), "heading");
864 * gtk_app_chooser_dialog_get_heading:
865 * @self: a #GtkAppChooserDialog
867 * Returns the text to display at the top of the dialog.
869 * Returns: the text to display at the top of the dialog, or %NULL, in which
870 * case a default text is displayed
873 gtk_app_chooser_dialog_get_heading (GtkAppChooserDialog *self)
875 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
877 return self->priv->heading;