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)
480 GtkWidget *button, *w;
482 gtk_container_set_border_width (GTK_CONTAINER (self), 5);
484 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
485 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
486 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
487 gtk_widget_show (vbox);
489 vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
490 gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
491 gtk_widget_show (vbox2);
493 self->priv->label = gtk_label_new ("");
494 gtk_misc_set_alignment (GTK_MISC (self->priv->label), 0, 0.5);
495 gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
496 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
498 gtk_widget_show (self->priv->label);
500 self->priv->app_chooser_widget =
501 gtk_app_chooser_widget_new (self->priv->content_type);
502 gtk_box_pack_start (GTK_BOX (vbox2), self->priv->app_chooser_widget, TRUE, TRUE, 0);
503 gtk_widget_show (self->priv->app_chooser_widget);
505 g_signal_connect (self->priv->app_chooser_widget, "application-selected",
506 G_CALLBACK (widget_application_selected_cb), self);
507 g_signal_connect (self->priv->app_chooser_widget, "application-activated",
508 G_CALLBACK (widget_application_activated_cb), self);
509 g_signal_connect (self->priv->app_chooser_widget, "notify::show-other",
510 G_CALLBACK (widget_notify_for_button_cb), self);
511 g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
512 G_CALLBACK (widget_populate_popup_cb), self);
514 button = gtk_button_new_with_label (_("Show other applications"));
515 self->priv->show_more_button = button;
516 w = gtk_image_new_from_stock (GTK_STOCK_ADD,
517 GTK_ICON_SIZE_BUTTON);
518 gtk_button_set_image (GTK_BUTTON (button), w);
519 gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget), button, FALSE, FALSE, 6);
520 gtk_widget_show_all (button);
522 g_signal_connect (button, "clicked",
523 G_CALLBACK (show_more_button_clicked_cb), self);
525 gtk_dialog_add_button (GTK_DIALOG (self),
527 GTK_RESPONSE_CANCEL);
529 self->priv->button = gtk_dialog_add_button (GTK_DIALOG (self),
533 gtk_dialog_set_default_response (GTK_DIALOG (self),
538 set_gfile_and_content_type (GtkAppChooserDialog *self,
546 self->priv->gfile = g_object_ref (file);
548 info = g_file_query_info (self->priv->gfile,
549 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
551 self->priv->content_type = g_strdup (g_file_info_get_content_type (info));
553 g_object_unref (info);
557 gtk_app_chooser_dialog_get_app_info (GtkAppChooser *object)
559 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
560 GAppInfo *app = NULL;
562 if (!check_application (self, &app))
569 gtk_app_chooser_dialog_refresh (GtkAppChooser *object)
571 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
573 gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
577 gtk_app_chooser_dialog_constructed (GObject *object)
579 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
581 if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
582 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
584 build_dialog_ui (self);
585 set_dialog_properties (self);
586 ensure_online_button (self);
590 gtk_app_chooser_dialog_dispose (GObject *object)
592 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
594 g_clear_object (&self->priv->gfile);
595 g_clear_object (&self->priv->online);
597 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);
601 gtk_app_chooser_dialog_finalize (GObject *object)
603 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
605 g_free (self->priv->content_type);
607 G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->finalize (object);
611 gtk_app_chooser_dialog_set_property (GObject *object,
616 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
621 set_gfile_and_content_type (self, g_value_get_object (value));
623 case PROP_CONTENT_TYPE:
624 /* don't try to override a value previously set with the GFile */
625 if (self->priv->content_type == NULL)
626 self->priv->content_type = g_value_dup_string (value);
629 gtk_app_chooser_dialog_set_heading (self, g_value_get_string (value));
632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
638 gtk_app_chooser_dialog_get_property (GObject *object,
643 GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
648 if (self->priv->gfile != NULL)
649 g_value_set_object (value, self->priv->gfile);
651 case PROP_CONTENT_TYPE:
652 g_value_set_string (value, self->priv->content_type);
655 g_value_set_string (value, self->priv->heading);
658 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
664 gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
666 iface->get_app_info = gtk_app_chooser_dialog_get_app_info;
667 iface->refresh = gtk_app_chooser_dialog_refresh;
671 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
673 GObjectClass *gobject_class;
676 gobject_class = G_OBJECT_CLASS (klass);
677 gobject_class->dispose = gtk_app_chooser_dialog_dispose;
678 gobject_class->finalize = gtk_app_chooser_dialog_finalize;
679 gobject_class->set_property = gtk_app_chooser_dialog_set_property;
680 gobject_class->get_property = gtk_app_chooser_dialog_get_property;
681 gobject_class->constructed = gtk_app_chooser_dialog_constructed;
683 g_object_class_override_property (gobject_class, PROP_CONTENT_TYPE, "content-type");
686 * GtkAppChooserDialog:gfile:
688 * The GFile used by the #GtkAppChooserDialog.
689 * The dialog's #GtkAppChooserWidget content type will be guessed from the
692 pspec = g_param_spec_object ("gfile",
694 P_("The GFile used by the app chooser dialog"),
696 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
697 G_PARAM_STATIC_STRINGS);
698 g_object_class_install_property (gobject_class, PROP_GFILE, pspec);
701 * GtkAppChooserDialog:heading:
703 * The text to show at the top of the dialog.
704 * The string may contain Pango markup.
706 pspec = g_param_spec_string ("heading",
708 P_("The text to show at the top of the dialog"),
710 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
711 g_object_class_install_property (gobject_class, PROP_HEADING, pspec);
714 g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
718 gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
720 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
721 GtkAppChooserDialogPrivate);
723 /* we can't override the class signal handler here, as it's a RUN_LAST;
724 * we want our signal handler instead to be executed before any user code.
726 g_signal_connect (self, "response",
727 G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
731 set_parent_and_flags (GtkWidget *dialog,
733 GtkDialogFlags flags)
736 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
738 if (flags & GTK_DIALOG_MODAL)
739 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
741 if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
742 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
746 * gtk_app_chooser_dialog_new:
747 * @parent: (allow-none): a #GtkWindow, or %NULL
748 * @flags: flags for this dialog
751 * Creates a new #GtkAppChooserDialog for the provided #GFile,
752 * to allow the user to select an application for it.
754 * Returns: a newly created #GtkAppChooserDialog
759 gtk_app_chooser_dialog_new (GtkWindow *parent,
760 GtkDialogFlags flags,
765 g_return_val_if_fail (G_IS_FILE (file), NULL);
767 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
771 set_parent_and_flags (retval, parent, flags);
777 * gtk_app_chooser_dialog_new_for_content_type:
778 * @parent: (allow-none): a #GtkWindow, or %NULL
779 * @flags: flags for this dialog
780 * @content_type: a content type string
782 * Creates a new #GtkAppChooserDialog for the provided content type,
783 * to allow the user to select an application for it.
785 * Returns: a newly created #GtkAppChooserDialog
790 gtk_app_chooser_dialog_new_for_content_type (GtkWindow *parent,
791 GtkDialogFlags flags,
792 const gchar *content_type)
796 g_return_val_if_fail (content_type != NULL, NULL);
798 retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
799 "content-type", content_type,
802 set_parent_and_flags (retval, parent, flags);
808 * gtk_app_chooser_dialog_get_widget:
809 * @self: a #GtkAppChooserDialog
811 * Returns the #GtkAppChooserWidget of this dialog.
813 * Returns: (transfer none): the #GtkAppChooserWidget of @self
818 gtk_app_chooser_dialog_get_widget (GtkAppChooserDialog *self)
820 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
822 return self->priv->app_chooser_widget;
826 * gtk_app_chooser_dialog_set_heading:
827 * @self: a #GtkAppChooserDialog
828 * @heading: a string containing Pango markup
830 * Sets the text to display at the top of the dialog.
831 * If the heading is not set, the dialog displays a default text.
834 gtk_app_chooser_dialog_set_heading (GtkAppChooserDialog *self,
835 const gchar *heading)
837 g_return_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self));
839 g_free (self->priv->heading);
840 self->priv->heading = g_strdup (heading);
842 if (self->priv->label && self->priv->heading)
843 gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
845 g_object_notify (G_OBJECT (self), "heading");
849 * gtk_app_chooser_dialog_get_heading:
850 * @self: a #GtkAppChooserDialog
852 * Returns the text to display at the top of the dialog.
854 * Returns: the text to display at the top of the dialog, or %NULL, in which
855 * case a default text is displayed
858 gtk_app_chooser_dialog_get_heading (GtkAppChooserDialog *self)
860 g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
862 return self->priv->heading;