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