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