]> Pileus Git - ~andy/gtk/blob - gtk/gtkappchooserdialog.c
appchooser: add a GCancellable to the search operation
[~andy/gtk] / gtk / gtkappchooserdialog.c
1 /*
2  * gtkappchooserdialog.c: an app-chooser dialog
3  *
4  * Copyright (C) 2004 Novell, Inc.
5  * Copyright (C) 2007, 2010 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
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.
21  *
22  * Authors: Dave Camp <dave@novell.com>
23  *          Alexander Larsson <alexl@redhat.com>
24  *          Cosimo Cecchi <ccecchi@redhat.com>
25  */
26
27 /**
28  * SECTION:gtkappchooserdialog
29  * @Title: GtkAppChooserDialog
30  * @Short_description: An application chooser dialog
31  *
32  * #GtkAppChooserDialog shows a #GtkAppChooserWidget inside a #GtkDialog.
33  *
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.
38  *
39  * To set the heading that is shown above the #GtkAppChooserWidget,
40  * use gtk_app_chooser_dialog_set_heading().
41  */
42 #include "config.h"
43
44 #include "gtkappchooserdialog.h"
45
46 #include "gtkintl.h"
47 #include "gtkappchooser.h"
48 #include "gtkappchooseronline.h"
49 #include "gtkappchooserprivate.h"
50 #include "gtkappchooserprivate.h"
51
52 #include "gtkmessagedialog.h"
53 #include "gtklabel.h"
54 #include "gtkbbox.h"
55 #include "gtkbutton.h"
56 #include "gtkmenuitem.h"
57 #include "gtkstock.h"
58
59 #include <string.h>
60 #include <glib/gi18n-lib.h>
61 #include <gio/gio.h>
62
63 #define sure_string(s) ((const char *) ((s) != NULL ? (s) : ""))
64
65 struct _GtkAppChooserDialogPrivate {
66   char *content_type;
67   GFile *gfile;
68   char *heading;
69
70   GtkWidget *label;
71   GtkWidget *button;
72   GtkWidget *online_button;
73
74   GtkWidget *open_label;
75
76   GtkWidget *app_chooser_widget;
77   GtkWidget *show_more_button;
78
79   GtkAppChooserOnline *online;
80   GCancellable *online_cancellable;
81
82   gboolean show_more_clicked;
83 };
84
85 enum {
86   PROP_GFILE = 1,
87   PROP_CONTENT_TYPE,
88   PROP_HEADING
89 };
90
91 static void gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface);
92 G_DEFINE_TYPE_WITH_CODE (GtkAppChooserDialog, gtk_app_chooser_dialog, GTK_TYPE_DIALOG,
93                          G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
94                                                 gtk_app_chooser_dialog_iface_init));
95
96 static void
97 show_error_dialog (const gchar *primary,
98                    const gchar *secondary,
99                    GtkWindow   *parent)
100 {
101   GtkWidget *message_dialog;
102
103   message_dialog = gtk_message_dialog_new (parent, 0,
104                                            GTK_MESSAGE_ERROR,
105                                            GTK_BUTTONS_OK,
106                                            NULL);
107   g_object_set (message_dialog,
108                 "text", primary,
109                 "secondary-text", secondary,
110                 NULL);
111   gtk_dialog_set_default_response (GTK_DIALOG (message_dialog), GTK_RESPONSE_OK);
112
113   gtk_widget_show (message_dialog);
114
115   g_signal_connect (message_dialog, "response",
116                     G_CALLBACK (gtk_widget_destroy), NULL);
117 }
118
119 static void
120 search_for_mimetype_ready_cb (GObject      *source,
121                               GAsyncResult *res,
122                               gpointer      user_data)
123 {
124   GtkAppChooserOnline *online = GTK_APP_CHOOSER_ONLINE (source);
125   GtkAppChooserDialog *self = user_data;
126   GError *error = NULL;
127
128   gdk_threads_enter ();
129
130   _gtk_app_chooser_online_search_for_mimetype_finish (online, res, &error);
131
132   if (error != NULL &&
133       !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
134     {
135       show_error_dialog (_("Failed to look for applications online"),
136                          error->message, GTK_WINDOW (self));
137     }
138   else
139     {
140       gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
141     }
142
143   g_clear_error (&error);
144
145   gdk_threads_leave ();
146 }
147
148 static void
149 online_button_clicked_cb (GtkButton *b,
150                           gpointer   user_data)
151 {
152   GtkAppChooserDialog *self = user_data;
153
154   self->priv->online_cancellable = g_cancellable_new ();
155   _gtk_app_chooser_online_search_for_mimetype_async (self->priv->online,
156                                                      self->priv->content_type,
157                                                      GTK_WINDOW (self),
158                                                      self->priv->online_cancellable,
159                                                      search_for_mimetype_ready_cb,
160                                                      self);
161 }
162
163 static void
164 app_chooser_online_get_default_ready_cb (GObject *source,
165                                          GAsyncResult *res,
166                                          gpointer user_data)
167 {
168   GtkAppChooserDialog *self = user_data;
169
170   gdk_threads_enter ();
171
172   self->priv->online = _gtk_app_chooser_online_get_default_finish (source, res);
173
174   if (self->priv->online != NULL)
175     {
176       GtkWidget *action_area;
177
178       action_area = gtk_dialog_get_action_area (GTK_DIALOG (self));
179       self->priv->online_button = gtk_button_new_with_label (_("Find applications online"));
180       gtk_box_pack_start (GTK_BOX (action_area), self->priv->online_button,
181                           FALSE, FALSE, 0);
182       gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (action_area), self->priv->online_button,
183                                           TRUE);
184       g_signal_connect (self->priv->online_button, "clicked",
185                         G_CALLBACK (online_button_clicked_cb), self);
186
187
188       if (!self->priv->content_type)
189         gtk_widget_set_sensitive (self->priv->online_button, FALSE);
190
191       gtk_widget_show (self->priv->online_button);
192     }
193
194   gdk_threads_leave ();
195 }
196
197 static void
198 ensure_online_button (GtkAppChooserDialog *self)
199 {
200   _gtk_app_chooser_online_get_default_async (app_chooser_online_get_default_ready_cb, self);
201 }
202
203 /* An application is valid if:
204  *
205  * 1) The file exists
206  * 2) The user has permissions to run the file
207  */
208 static gboolean
209 check_application (GtkAppChooserDialog  *self,
210                    GAppInfo            **app_out)
211 {
212   const char *command;
213   char *path = NULL;
214   char **argv = NULL;
215   int argc;
216   GError *error = NULL;
217   gint retval = TRUE;
218   GAppInfo *info;
219
220   command = NULL;
221
222   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
223   if (info == NULL)
224     {
225       *app_out = NULL;
226       return FALSE;
227     }
228
229   command = g_app_info_get_executable (info);
230
231   g_shell_parse_argv (command, &argc, &argv, &error);
232
233   if (error)
234     {
235       show_error_dialog (_("Could not run application"),
236                          error->message,
237                          GTK_WINDOW (self));
238       g_error_free (error);
239       retval = FALSE;
240       goto cleanup;
241     }
242
243   path = g_find_program_in_path (argv[0]);
244   if (!path)
245     {
246       char *error_message;
247
248       error_message = g_strdup_printf (_("Could not find '%s'"),
249                                        argv[0]);
250
251       show_error_dialog (_("Could not find application"),
252                          error_message,
253                          GTK_WINDOW (self));
254       g_free (error_message);
255       retval = FALSE;
256       goto cleanup;
257     }
258
259   *app_out = info;
260
261  cleanup:
262   g_strfreev (argv);
263   g_free (path);
264
265   return retval;
266 }
267
268 static void
269 add_or_find_application (GtkAppChooserDialog *self)
270 {
271   GAppInfo *app;
272
273   app = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
274
275   if (app)
276     {
277       /* we don't care about reporting errors here */
278       if (self->priv->content_type)
279         g_app_info_set_as_last_used_for_type (app,
280                                               self->priv->content_type,
281                                               NULL);
282       g_object_unref (app);
283     }
284 }
285
286 static void
287 gtk_app_chooser_dialog_response (GtkDialog *dialog,
288                                  gint       response_id,
289                                  gpointer   user_data)
290 {
291   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (dialog);
292
293   switch (response_id)
294     {
295     case GTK_RESPONSE_OK:
296       add_or_find_application (self);
297       break;
298     default :
299       break;
300     }
301 }
302
303 static void
304 widget_application_selected_cb (GtkAppChooserWidget *widget,
305                                 GAppInfo            *app_info,
306                                 gpointer             user_data)
307 {
308   GtkAppChooserDialog *self = user_data;
309
310   gtk_widget_set_sensitive (self->priv->button, TRUE);
311 }
312
313 static void
314 widget_application_activated_cb (GtkAppChooserWidget *widget,
315                                  GAppInfo            *app_info,
316                                  gpointer             user_data)
317 {
318   GtkAppChooserDialog *self = user_data;
319
320   gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
321 }
322
323 static char *
324 get_extension (const char *basename)
325 {
326   char *p;
327
328   p = strrchr (basename, '.');
329
330   if (p && *(p + 1) != '\0')
331     return g_strdup (p + 1);
332
333   return NULL;
334 }
335
336 static void
337 set_dialog_properties (GtkAppChooserDialog *self)
338 {
339   gchar *label;
340   gchar *name;
341   gchar *extension;
342   gchar *description;
343   gchar *default_text;
344   gchar *string;
345   gboolean unknown;
346   PangoFontDescription *font_desc;
347
348   name = NULL;
349   extension = NULL;
350   label = NULL;
351   description = NULL;
352   unknown = TRUE;
353
354   if (self->priv->gfile != NULL)
355     {
356       name = g_file_get_basename (self->priv->gfile);
357       extension = get_extension (name);
358     }
359
360   if (self->priv->content_type)
361     {
362       description = g_content_type_get_description (self->priv->content_type);
363       unknown = g_content_type_is_unknown (self->priv->content_type);
364     }
365
366   gtk_window_set_title (GTK_WINDOW (self), "");
367
368   if (name != NULL)
369     {
370       /* Translators: %s is a filename */
371       label = g_strdup_printf (_("Select an application to open \"%s\""), name);
372       string = g_strdup_printf (_("No applications available to open \"%s\""),
373                                 name);
374     }
375   else
376     {
377       /* Translators: %s is a file type description */
378       label = g_strdup_printf (_("Select an application for \"%s\" files"),
379                                unknown ? self->priv->content_type : description);
380       string = g_strdup_printf (_("No applications available to open \"%s\" files"),
381                                unknown ? self->priv->content_type : description);
382     }
383
384   font_desc = pango_font_description_new ();
385   pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
386   gtk_widget_override_font (self->priv->label, font_desc);
387   pango_font_description_free (font_desc);
388
389   if (self->priv->heading != NULL)
390     gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
391   else
392     gtk_label_set_markup (GTK_LABEL (self->priv->label), label);
393
394   default_text = g_strdup_printf ("<big><b>%s</b></big>\n%s",
395                                   string,
396                                   _("Click \"Show other applications\", for more options, or "
397                                     "\"Find applications online\" to install a new application"));
398
399   gtk_app_chooser_widget_set_default_text (GTK_APP_CHOOSER_WIDGET (self->priv->app_chooser_widget),
400                                            default_text);
401
402   g_free (label);
403   g_free (name);
404   g_free (extension);
405   g_free (description);
406   g_free (string);
407   g_free (default_text);
408 }
409
410 static void
411 show_more_button_clicked_cb (GtkButton *button,
412                              gpointer   user_data)
413 {
414   GtkAppChooserDialog *self = user_data;
415
416   g_object_set (self->priv->app_chooser_widget,
417                 "show-recommended", TRUE,
418                 "show-fallback", TRUE,
419                 "show-other", TRUE,
420                 NULL);
421
422   gtk_widget_hide (self->priv->show_more_button);
423   self->priv->show_more_clicked = TRUE;
424 }
425
426 static void
427 widget_notify_for_button_cb (GObject    *source,
428                              GParamSpec *pspec,
429                              gpointer    user_data)
430 {
431   GtkAppChooserDialog *self = user_data;
432   GtkAppChooserWidget *widget = GTK_APP_CHOOSER_WIDGET (source);
433   gboolean should_hide;
434
435   should_hide = gtk_app_chooser_widget_get_show_other (widget) ||
436     self->priv->show_more_clicked;
437
438   if (should_hide)
439     gtk_widget_hide (self->priv->show_more_button);
440 }
441
442 static void
443 forget_menu_item_activate_cb (GtkMenuItem *item,
444                               gpointer     user_data)
445 {
446   GtkAppChooserDialog *self = user_data;
447   GAppInfo *info;
448
449   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
450
451   if (info != NULL)
452     {
453       g_app_info_remove_supports_type (info, self->priv->content_type, NULL);
454
455       gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
456
457       g_object_unref (info);
458     }
459 }
460
461 static GtkWidget *
462 build_forget_menu_item (GtkAppChooserDialog *self)
463 {
464   GtkWidget *retval;
465
466   retval = gtk_menu_item_new_with_label (_("Forget association"));
467   gtk_widget_show (retval);
468
469   g_signal_connect (retval, "activate",
470                     G_CALLBACK (forget_menu_item_activate_cb), self);
471
472   return retval;
473 }
474
475 static void
476 widget_populate_popup_cb (GtkAppChooserWidget *widget,
477                           GtkMenu             *menu,
478                           GAppInfo            *info,
479                           gpointer             user_data)
480 {
481   GtkAppChooserDialog *self = user_data;
482   GtkWidget *menu_item;
483
484   if (g_app_info_can_remove_supports_type (info))
485     {
486       menu_item = build_forget_menu_item (self);
487       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
488     }
489 }
490
491 static void
492 build_dialog_ui (GtkAppChooserDialog *self)
493 {
494   GtkWidget *vbox;
495   GtkWidget *vbox2;
496   GtkWidget *button, *w;
497   GAppInfo *info;
498
499   gtk_container_set_border_width (GTK_CONTAINER (self), 5);
500
501   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
502   gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
503   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
504   gtk_widget_show (vbox);
505
506   vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
507   gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
508   gtk_widget_show (vbox2);
509
510   self->priv->label = gtk_label_new ("");
511   gtk_widget_set_halign (self->priv->label, GTK_ALIGN_START);
512   gtk_widget_set_valign (self->priv->label, GTK_ALIGN_CENTER);
513   gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
514   gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
515                       FALSE, FALSE, 0);
516   gtk_widget_show (self->priv->label);
517
518   self->priv->app_chooser_widget =
519     gtk_app_chooser_widget_new (self->priv->content_type);
520   gtk_box_pack_start (GTK_BOX (vbox2), self->priv->app_chooser_widget, TRUE, TRUE, 0);
521   gtk_widget_show (self->priv->app_chooser_widget);
522
523   g_signal_connect (self->priv->app_chooser_widget, "application-selected",
524                     G_CALLBACK (widget_application_selected_cb), self);
525   g_signal_connect (self->priv->app_chooser_widget, "application-activated",
526                     G_CALLBACK (widget_application_activated_cb), self);
527   g_signal_connect (self->priv->app_chooser_widget, "notify::show-other",
528                     G_CALLBACK (widget_notify_for_button_cb), self);
529   g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
530                     G_CALLBACK (widget_populate_popup_cb), self);
531
532   button = gtk_button_new_with_label (_("Show other applications"));
533   self->priv->show_more_button = button;
534   w = gtk_image_new_from_stock (GTK_STOCK_ADD,
535                                 GTK_ICON_SIZE_BUTTON);
536   gtk_button_set_image (GTK_BUTTON (button), w);
537   gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget), button, FALSE, FALSE, 6);
538   gtk_widget_show_all (button);
539
540   g_signal_connect (button, "clicked",
541                     G_CALLBACK (show_more_button_clicked_cb), self);
542
543   gtk_dialog_add_button (GTK_DIALOG (self),
544                          GTK_STOCK_CANCEL,
545                          GTK_RESPONSE_CANCEL);
546
547   self->priv->button = gtk_dialog_add_button (GTK_DIALOG (self),
548                                               _("_Select"),
549                                               GTK_RESPONSE_OK);
550
551   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
552   gtk_widget_set_sensitive (self->priv->button, info != NULL);
553   if (info)
554     g_object_unref (info);
555
556   gtk_dialog_set_default_response (GTK_DIALOG (self),
557                                    GTK_RESPONSE_OK);
558 }
559
560 static void
561 set_gfile_and_content_type (GtkAppChooserDialog *self,
562                             GFile *file)
563 {
564   GFileInfo *info;
565
566   if (file == NULL)
567     return;
568
569   self->priv->gfile = g_object_ref (file);
570
571   info = g_file_query_info (self->priv->gfile,
572                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
573                             0, NULL, NULL);
574   self->priv->content_type = g_strdup (g_file_info_get_content_type (info));
575
576   g_object_unref (info);
577 }
578
579 static GAppInfo *
580 gtk_app_chooser_dialog_get_app_info (GtkAppChooser *object)
581 {
582   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
583   GAppInfo *app = NULL;
584
585   if (!check_application (self, &app))
586     return NULL;
587
588   return app;
589 }
590
591 static void
592 gtk_app_chooser_dialog_refresh (GtkAppChooser *object)
593 {
594   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
595
596   gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
597 }
598
599 static void
600 gtk_app_chooser_dialog_constructed (GObject *object)
601 {
602   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
603
604   if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
605     G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
606
607   build_dialog_ui (self);
608   set_dialog_properties (self);
609   ensure_online_button (self);
610 }
611
612 static void
613 gtk_app_chooser_dialog_dispose (GObject *object)
614 {
615   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
616   
617   g_clear_object (&self->priv->gfile);
618   g_clear_object (&self->priv->online);
619
620   if (self->priv->online_cancellable != NULL)
621     {
622       g_cancellable_cancel (self->priv->online_cancellable);
623       g_clear_object (&self->priv->online_cancellable);
624     }
625
626   G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);
627 }
628
629 static void
630 gtk_app_chooser_dialog_finalize (GObject *object)
631 {
632   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
633
634   g_free (self->priv->content_type);
635
636   G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->finalize (object);
637 }
638
639 static void
640 gtk_app_chooser_dialog_set_property (GObject      *object,
641                                      guint         property_id,
642                                      const GValue *value,
643                                      GParamSpec   *pspec)
644 {
645   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
646
647   switch (property_id)
648     {
649     case PROP_GFILE:
650       set_gfile_and_content_type (self, g_value_get_object (value));
651       break;
652     case PROP_CONTENT_TYPE:
653       /* don't try to override a value previously set with the GFile */
654       if (self->priv->content_type == NULL)
655         self->priv->content_type = g_value_dup_string (value);
656       break;
657     case PROP_HEADING:
658       gtk_app_chooser_dialog_set_heading (self, g_value_get_string (value));
659       break;
660     default:
661       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
662       break;
663     }
664 }
665
666 static void
667 gtk_app_chooser_dialog_get_property (GObject    *object,
668                                      guint       property_id,
669                                      GValue     *value,
670                                      GParamSpec *pspec)
671 {
672   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
673
674   switch (property_id)
675     {
676     case PROP_GFILE:
677       if (self->priv->gfile != NULL)
678         g_value_set_object (value, self->priv->gfile);
679       break;
680     case PROP_CONTENT_TYPE:
681       g_value_set_string (value, self->priv->content_type);
682       break;
683     case PROP_HEADING:
684       g_value_set_string (value, self->priv->heading);
685       break;
686     default:
687       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
688       break;
689     }
690 }
691
692 static void
693 gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
694 {
695   iface->get_app_info = gtk_app_chooser_dialog_get_app_info;
696   iface->refresh = gtk_app_chooser_dialog_refresh;
697 }
698
699 static void
700 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
701 {
702   GObjectClass *gobject_class;
703   GParamSpec *pspec;
704
705   gobject_class = G_OBJECT_CLASS (klass);
706   gobject_class->dispose = gtk_app_chooser_dialog_dispose;
707   gobject_class->finalize = gtk_app_chooser_dialog_finalize;
708   gobject_class->set_property = gtk_app_chooser_dialog_set_property;
709   gobject_class->get_property = gtk_app_chooser_dialog_get_property;
710   gobject_class->constructed = gtk_app_chooser_dialog_constructed;
711
712   g_object_class_override_property (gobject_class, PROP_CONTENT_TYPE, "content-type");
713
714   /**
715    * GtkAppChooserDialog:gfile:
716    *
717    * The GFile used by the #GtkAppChooserDialog.
718    * The dialog's #GtkAppChooserWidget content type will be guessed from the
719    * file, if present.
720    */
721   pspec = g_param_spec_object ("gfile",
722                                P_("GFile"),
723                                P_("The GFile used by the app chooser dialog"),
724                                G_TYPE_FILE,
725                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
726                                G_PARAM_STATIC_STRINGS);
727   g_object_class_install_property (gobject_class, PROP_GFILE, pspec);
728
729   /**
730    * GtkAppChooserDialog:heading:
731    *
732    * The text to show at the top of the dialog.
733    * The string may contain Pango markup.
734    */
735   pspec = g_param_spec_string ("heading",
736                                P_("Heading"),
737                                P_("The text to show at the top of the dialog"),
738                                NULL,
739                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
740   g_object_class_install_property (gobject_class, PROP_HEADING, pspec);
741
742
743   g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
744 }
745
746 static void
747 gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
748 {
749   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
750                                             GtkAppChooserDialogPrivate);
751
752   /* we can't override the class signal handler here, as it's a RUN_LAST;
753    * we want our signal handler instead to be executed before any user code.
754    */
755   g_signal_connect (self, "response",
756                     G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
757 }
758
759 static void
760 set_parent_and_flags (GtkWidget      *dialog,
761                       GtkWindow      *parent,
762                       GtkDialogFlags  flags)
763 {
764   if (parent != NULL)
765     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
766
767   if (flags & GTK_DIALOG_MODAL)
768     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
769
770   if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
771     gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
772 }
773
774 /**
775  * gtk_app_chooser_dialog_new:
776  * @parent: (allow-none): a #GtkWindow, or %NULL
777  * @flags: flags for this dialog
778  * @file: a #GFile
779  *
780  * Creates a new #GtkAppChooserDialog for the provided #GFile,
781  * to allow the user to select an application for it.
782  *
783  * Returns: a newly created #GtkAppChooserDialog
784  *
785  * Since: 3.0
786  **/
787 GtkWidget *
788 gtk_app_chooser_dialog_new (GtkWindow      *parent,
789                             GtkDialogFlags  flags,
790                             GFile          *file)
791 {
792   GtkWidget *retval;
793
794   g_return_val_if_fail (G_IS_FILE (file), NULL);
795
796   retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
797                          "gfile", file,
798                          NULL);
799
800   set_parent_and_flags (retval, parent, flags);
801
802   return retval;
803 }
804
805 /**
806  * gtk_app_chooser_dialog_new_for_content_type:
807  * @parent: (allow-none): a #GtkWindow, or %NULL
808  * @flags: flags for this dialog
809  * @content_type: a content type string
810  *
811  * Creates a new #GtkAppChooserDialog for the provided content type,
812  * to allow the user to select an application for it.
813  *
814  * Returns: a newly created #GtkAppChooserDialog
815  *
816  * Since: 3.0
817  **/
818 GtkWidget *
819 gtk_app_chooser_dialog_new_for_content_type (GtkWindow      *parent,
820                                              GtkDialogFlags  flags,
821                                              const gchar    *content_type)
822 {
823   GtkWidget *retval;
824
825   g_return_val_if_fail (content_type != NULL, NULL);
826
827   retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
828                          "content-type", content_type,
829                          NULL);
830
831   set_parent_and_flags (retval, parent, flags);
832
833   return retval;
834 }
835
836 /**
837  * gtk_app_chooser_dialog_get_widget:
838  * @self: a #GtkAppChooserDialog
839  *
840  * Returns the #GtkAppChooserWidget of this dialog.
841  *
842  * Returns: (transfer none): the #GtkAppChooserWidget of @self
843  *
844  * Since: 3.0
845  */
846 GtkWidget *
847 gtk_app_chooser_dialog_get_widget (GtkAppChooserDialog *self)
848 {
849   g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
850
851   return self->priv->app_chooser_widget;
852 }
853
854 /**
855  * gtk_app_chooser_dialog_set_heading:
856  * @self: a #GtkAppChooserDialog
857  * @heading: a string containing Pango markup
858  *
859  * Sets the text to display at the top of the dialog.
860  * If the heading is not set, the dialog displays a default text.
861  */
862 void
863 gtk_app_chooser_dialog_set_heading (GtkAppChooserDialog *self,
864                                     const gchar         *heading)
865 {
866   g_return_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self));
867
868   g_free (self->priv->heading);
869   self->priv->heading = g_strdup (heading);
870
871   if (self->priv->label && self->priv->heading)
872     gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
873
874   g_object_notify (G_OBJECT (self), "heading");
875 }
876
877 /**
878  * gtk_app_chooser_dialog_get_heading:
879  * @self: a #GtkAppChooserDialog
880  *
881  * Returns the text to display at the top of the dialog.
882  *
883  * Returns: the text to display at the top of the dialog, or %NULL, in which
884  *     case a default text is displayed
885  */
886 const gchar *
887 gtk_app_chooser_dialog_get_heading (GtkAppChooserDialog *self)
888 {
889   g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
890
891   return self->priv->heading;
892 }