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