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