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