]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
application: fix double free of app_id
[~andy/gtk] / gtk / gtkapplication.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include "gtkapplication.h"
23
24 #include <stdlib.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <string.h>
29
30 #include "gtkapplicationprivate.h"
31 #include "gtkclipboard.h"
32 #include "gtkmarshalers.h"
33 #include "gtkmain.h"
34 #include "gtkrecentmanager.h"
35 #include "gtkaccelmapprivate.h"
36 #include "gactionmuxer.h"
37 #include "gtkintl.h"
38
39 #ifdef GDK_WINDOWING_QUARTZ
40 #include "gtkmodelmenu-quartz.h"
41 #import <Cocoa/Cocoa.h>
42 #include <Carbon/Carbon.h>
43 #include "gtkmessagedialog.h"
44 #endif
45
46 #include <gdk/gdk.h>
47 #ifdef GDK_WINDOWING_X11
48 #include <gdk/x11/gdkx.h>
49 #endif
50
51 /**
52  * SECTION:gtkapplication
53  * @title: GtkApplication
54  * @short_description: Application class
55  *
56  * #GtkApplication is a class that handles many important aspects
57  * of a GTK+ application in a convenient fashion, without enforcing
58  * a one-size-fits-all application model.
59  *
60  * Currently, GtkApplication handles GTK+ initialization, application
61  * uniqueness, session management, provides some basic scriptability and
62  * desktop shell integration by exporting actions and menus and manages a
63  * list of toplevel windows whose life-cycle is automatically tied to the
64  * life-cycle of your application.
65  *
66  * While GtkApplication works fine with plain #GtkWindows, it is recommended
67  * to use it together with #GtkApplicationWindow.
68  *
69  * When GDK threads are enabled, GtkApplication will acquire the GDK
70  * lock when invoking actions that arrive from other processes.  The GDK
71  * lock is not touched for local action invocations.  In order to have
72  * actions invoked in a predictable context it is therefore recommended
73  * that the GDK lock be held while invoking actions locally with
74  * g_action_group_activate_action().  The same applies to actions
75  * associated with #GtkApplicationWindow and to the 'activate' and
76  * 'open' #GApplication methods.
77  *
78  * To set an application menu for a GtkApplication, use
79  * gtk_application_set_app_menu(). The #GMenuModel that this function
80  * expects is usually constructed using #GtkBuilder, as seen in the
81  * following example. To specify a menubar that will be shown by
82  * #GtkApplicationWindow<!-- -->s, use gtk_application_set_menubar(). Use the base
83  * #GActionMap interface to add actions, to respond to the user
84  * selecting these menu items.
85  *
86  * GTK+ displays these menus as expected, depending on the platform
87  * the application is running on.
88  *
89  * <figure label="Menu integration in OS X">
90  * <graphic fileref="bloatpad-osx.png" format="PNG"/>
91  * </figure>
92  *
93  * <figure label="Menu integration in GNOME">
94  * <graphic fileref="bloatpad-gnome.png" format="PNG"/>
95  * </figure>
96  *
97  * <figure label="Menu integration in Xfce">
98  * <graphic fileref="bloatpad-xfce.png" format="PNG"/>
99  * </figure>
100  *
101  * <example id="gtkapplication"><title>A simple application</title>
102  * <programlisting>
103  * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
104  *  <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
105  * </xi:include>
106  * </programlisting>
107  * </example>
108  *
109  * GtkApplication optionally registers with a session manager
110  * of the users session (if you set the #GtkApplication:register-session
111  * property) and offers various functionality related to the session
112  * life-cycle.
113  *
114  * An application can block various ways to end the session with
115  * the gtk_application_inhibit() function. Typical use cases for
116  * this kind of inhibiting are long-running, uninterruptible operations,
117  * such as burning a CD or performing a disk backup. The session
118  * manager may not honor the inhibitor, but it can be expected to
119  * inform the user about the negative consequences of ending the
120  * session while inhibitors are present.
121  */
122
123 enum {
124   WINDOW_ADDED,
125   WINDOW_REMOVED,
126   LAST_SIGNAL
127 };
128
129 static guint gtk_application_signals[LAST_SIGNAL];
130
131 enum {
132   PROP_ZERO,
133   PROP_REGISTER_SESSION,
134   PROP_APP_MENU,
135   PROP_MENUBAR
136 };
137
138 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
139
140 struct _GtkApplicationPrivate
141 {
142   GList *windows;
143
144   gboolean register_session;
145
146   GMenuModel      *app_menu;
147   GMenuModel      *menubar;
148
149 #ifdef GDK_WINDOWING_X11
150   GDBusConnection *session_bus;
151   const gchar     *application_id;
152   const gchar     *object_path;
153
154   gchar           *app_menu_path;
155   guint            app_menu_id;
156
157   gchar           *menubar_path;
158   guint            menubar_id;
159
160   guint next_id;
161
162   GDBusProxy *sm_proxy;
163   GDBusProxy *client_proxy;
164   gchar *app_id;
165   gchar *client_path;
166 #endif
167
168 #ifdef GDK_WINDOWING_QUARTZ
169   GActionMuxer *muxer;
170   GMenu *combined;
171
172   GSList *inhibitors;
173   gint quit_inhibit;
174   guint next_cookie;
175 #endif
176 };
177
178 #ifdef GDK_WINDOWING_X11
179 static void
180 gtk_application_x11_publish_menu (GtkApplication  *application,
181                                   const gchar     *type,
182                                   GMenuModel      *model,
183                                   guint           *id,
184                                   gchar          **path)
185 {
186   gint i;
187
188   if (application->priv->session_bus == NULL)
189     return;
190
191   /* unexport any existing menu */
192   if (*id)
193     {
194       g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
195       g_free (*path);
196       *path = NULL;
197       *id = 0;
198     }
199
200   /* export the new menu, if there is one */
201   if (model != NULL)
202     {
203       /* try getting the preferred name */
204       *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
205       *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
206
207       /* keep trying until we get a working name... */
208       for (i = 0; *id == 0; i++)
209         {
210           g_free (*path);
211           *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
212           *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
213         }
214     }
215 }
216
217 static void
218 gtk_application_set_app_menu_x11 (GtkApplication *application,
219                                   GMenuModel     *app_menu)
220 {
221   g_free (application->priv->app_menu_path);
222   gtk_application_x11_publish_menu (application, "appmenu", app_menu,
223                                     &application->priv->app_menu_id,
224                                     &application->priv->app_menu_path);
225 }
226
227 static void
228 gtk_application_set_menubar_x11 (GtkApplication *application,
229                                  GMenuModel     *menubar)
230 {
231   g_free (application->priv->menubar_path);
232   gtk_application_x11_publish_menu (application, "menubar", menubar,
233                                     &application->priv->menubar_id,
234                                     &application->priv->menubar_path);
235 }
236
237 static void
238 gtk_application_window_added_x11 (GtkApplication *application,
239                                   GtkWindow      *window)
240 {
241   if (application->priv->session_bus == NULL)
242     return;
243
244   if (GTK_IS_APPLICATION_WINDOW (window))
245     {
246       GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
247       gboolean success;
248
249       /* GtkApplicationWindow associates with us when it is first created,
250        * so surely it's not realized yet...
251        */
252       g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
253
254       do
255         {
256           gchar *window_path;
257           guint window_id;
258
259           window_id = application->priv->next_id++;
260           window_path = g_strdup_printf ("%s/window/%u", application->priv->object_path, window_id);
261           success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path, window_id);
262           g_free (window_path);
263         }
264       while (!success);
265     }
266 }
267
268 static void
269 gtk_application_window_removed_x11 (GtkApplication *application,
270                                     GtkWindow      *window)
271 {
272   if (application->priv->session_bus == NULL)
273     return;
274
275   if (GTK_IS_APPLICATION_WINDOW (window))
276     gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
277 }
278
279 static void gtk_application_startup_session_dbus (GtkApplication *app);
280
281 static void
282 gtk_application_startup_x11 (GtkApplication *application)
283 {
284   application->priv->session_bus = g_application_get_dbus_connection (G_APPLICATION (application));
285   application->priv->object_path = g_application_get_dbus_object_path (G_APPLICATION (application));
286
287   gtk_application_startup_session_dbus (GTK_APPLICATION (application));
288 }
289
290 static void
291 gtk_application_shutdown_x11 (GtkApplication *application)
292 {
293   application->priv->session_bus = NULL;
294   application->priv->object_path = NULL;
295
296   g_clear_object (&application->priv->sm_proxy);
297   g_clear_object (&application->priv->client_proxy);
298   g_free (application->priv->app_id);
299   g_free (application->priv->client_path);
300
301   g_free (application->priv->app_menu_path);
302   g_free (application->priv->menubar_path);
303 }
304
305 const gchar *
306 gtk_application_get_app_menu_object_path (GtkApplication *application)
307 {
308   return application->priv->app_menu_path;
309 }
310
311 const gchar *
312 gtk_application_get_menubar_object_path (GtkApplication *application)
313 {
314   return application->priv->menubar_path;
315 }
316
317 #endif
318
319 #ifdef GDK_WINDOWING_QUARTZ
320
321 typedef struct {
322   guint cookie;
323   GtkApplicationInhibitFlags flags;
324   char *reason;
325   GtkWindow *window;
326 } GtkApplicationQuartzInhibitor;
327
328 static void
329 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
330 {
331   g_free (inhibitor->reason);
332   g_clear_object (&inhibitor->window);
333   g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
334 }
335
336 static void
337 gtk_application_menu_changed_quartz (GObject    *object,
338                                      GParamSpec *pspec,
339                                      gpointer    user_data)
340 {
341   GtkApplication *application = GTK_APPLICATION (object);
342   GMenu *combined;
343
344   combined = g_menu_new ();
345   g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
346   g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
347
348   gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
349 }
350
351 static void gtk_application_startup_session_quartz (GtkApplication *app);
352
353 static void
354 gtk_application_startup_quartz (GtkApplication *application)
355 {
356   [NSApp finishLaunching];
357
358   application->priv->muxer = g_action_muxer_new ();
359   g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
360
361   g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
362   g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
363   gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
364
365   gtk_application_startup_session_quartz (application);
366 }
367
368 static void
369 gtk_application_shutdown_quartz (GtkApplication *application)
370 {
371   g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
372
373   g_object_unref (application->priv->muxer);
374   application->priv->muxer = NULL;
375
376   g_slist_free_full (application->priv->inhibitors,
377                      (GDestroyNotify) gtk_application_quartz_inhibitor_free);
378   application->priv->inhibitors = NULL;
379 }
380
381 static void
382 gtk_application_focus_changed (GtkApplication *application,
383                                GtkWindow      *window)
384 {
385   if (G_IS_ACTION_GROUP (window))
386     g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
387   else
388     g_action_muxer_remove (application->priv->muxer, "win");
389 }
390 #endif
391
392 static gboolean
393 gtk_application_focus_in_event_cb (GtkWindow      *window,
394                                    GdkEventFocus  *event,
395                                    GtkApplication *application)
396 {
397   GtkApplicationPrivate *priv = application->priv;
398   GList *link;
399
400   /* Keep the window list sorted by most-recently-focused. */
401   link = g_list_find (priv->windows, window);
402   if (link != NULL && link != priv->windows)
403     {
404       priv->windows = g_list_remove_link (priv->windows, link);
405       priv->windows = g_list_concat (link, priv->windows);
406     }
407
408 #ifdef GDK_WINDOWING_QUARTZ
409   gtk_application_focus_changed (application, window);
410 #endif
411
412   return FALSE;
413 }
414
415 static void
416 gtk_application_startup (GApplication *application)
417 {
418   G_APPLICATION_CLASS (gtk_application_parent_class)
419     ->startup (application);
420
421   gtk_init (0, 0);
422
423 #ifdef GDK_WINDOWING_X11
424   gtk_application_startup_x11 (GTK_APPLICATION (application));
425 #endif
426
427 #ifdef GDK_WINDOWING_QUARTZ
428   gtk_application_startup_quartz (GTK_APPLICATION (application));
429 #endif
430 }
431
432 static void
433 gtk_application_shutdown (GApplication *application)
434 {
435 #ifdef GDK_WINDOWING_X11
436   gtk_application_shutdown_x11 (GTK_APPLICATION (application));
437 #endif
438
439 #ifdef GDK_WINDOWING_QUARTZ
440   gtk_application_shutdown_quartz (GTK_APPLICATION (application));
441 #endif
442
443   /* Try storing all clipboard data we have */
444   _gtk_clipboard_store_all ();
445
446   /* Synchronize the recent manager singleton */
447   _gtk_recent_manager_sync ();
448
449   G_APPLICATION_CLASS (gtk_application_parent_class)
450     ->shutdown (application);
451 }
452
453 static void
454 gtk_application_add_platform_data (GApplication    *application,
455                                    GVariantBuilder *builder)
456 {
457   const gchar *startup_id;
458
459   startup_id = getenv ("DESKTOP_STARTUP_ID");
460   
461   if (startup_id && g_utf8_validate (startup_id, -1, NULL))
462     g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
463                            g_variant_new_string (startup_id));
464 }
465
466 static void
467 gtk_application_before_emit (GApplication *application,
468                              GVariant     *platform_data)
469 {
470   GVariantIter iter;
471   const gchar *key;
472   GVariant *value;
473
474   gdk_threads_enter ();
475
476   g_variant_iter_init (&iter, platform_data);
477   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
478     {
479 #ifdef GDK_WINDOWING_X11
480       if (strcmp (key, "desktop-startup-id") == 0)
481         {
482           GdkDisplay *display;
483           const gchar *id;
484
485           display = gdk_display_get_default ();
486           id = g_variant_get_string (value, NULL);
487           if (GDK_IS_X11_DISPLAY (display))
488             gdk_x11_display_set_startup_notification_id (display, id);
489        }
490 #endif
491     }
492 }
493
494 static void
495 gtk_application_after_emit (GApplication *application,
496                             GVariant     *platform_data)
497 {
498   gdk_notify_startup_complete ();
499
500   gdk_threads_leave ();
501 }
502
503 static void
504 gtk_application_init (GtkApplication *application)
505 {
506   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
507                                                    GTK_TYPE_APPLICATION,
508                                                    GtkApplicationPrivate);
509
510 #ifdef GDK_WINDOWING_X11
511   application->priv->next_id = 1;
512 #endif
513 }
514
515 static void
516 gtk_application_window_added (GtkApplication *application,
517                               GtkWindow      *window)
518 {
519   GtkApplicationPrivate *priv = application->priv;
520
521   priv->windows = g_list_prepend (priv->windows, window);
522   gtk_window_set_application (window, application);
523   g_application_hold (G_APPLICATION (application));
524
525   g_signal_connect (window, "focus-in-event",
526                     G_CALLBACK (gtk_application_focus_in_event_cb),
527                     application);
528
529 #ifdef GDK_WINDOWING_X11
530   gtk_application_window_added_x11 (application, window);
531 #endif
532 }
533
534 static void
535 gtk_application_window_removed (GtkApplication *application,
536                                 GtkWindow      *window)
537 {
538   GtkApplicationPrivate *priv = application->priv;
539
540 #ifdef GDK_WINDOWING_X11
541   gtk_application_window_removed_x11 (application, window);
542 #endif
543
544   g_signal_handlers_disconnect_by_func (window,
545                                         gtk_application_focus_in_event_cb,
546                                         application);
547
548   g_application_release (G_APPLICATION (application));
549   priv->windows = g_list_remove (priv->windows, window);
550   gtk_window_set_application (window, NULL);
551 }
552
553 static void
554 extract_accel_from_menu_item (GMenuModel     *model,
555                               gint            item,
556                               GtkApplication *app)
557 {
558   GMenuAttributeIter *iter;
559   const gchar *key;
560   GVariant *value;
561   const gchar *accel = NULL;
562   const gchar *action = NULL;
563   GVariant *target = NULL;
564
565   iter = g_menu_model_iterate_item_attributes (model, item);
566   while (g_menu_attribute_iter_get_next (iter, &key, &value))
567     {
568       if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
569         action = g_variant_get_string (value, NULL);
570       else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
571         accel = g_variant_get_string (value, NULL);
572       else if (g_str_equal (key, "target"))
573         target = g_variant_ref (value);
574       g_variant_unref (value);
575     }
576   g_object_unref (iter);
577
578   if (accel && action)
579     gtk_application_add_accelerator (app, accel, action, target);
580
581   if (target)
582     g_variant_unref (target);
583 }
584
585 static void
586 extract_accels_from_menu (GMenuModel     *model,
587                           GtkApplication *app)
588 {
589   gint i;
590   GMenuLinkIter *iter;
591   const gchar *key;
592   GMenuModel *m;
593
594   for (i = 0; i < g_menu_model_get_n_items (model); i++)
595     {
596       extract_accel_from_menu_item (model, i, app);
597
598       iter = g_menu_model_iterate_item_links (model, i);
599       while (g_menu_link_iter_get_next (iter, &key, &m))
600         {
601           extract_accels_from_menu (m, app);
602           g_object_unref (m);
603         }
604       g_object_unref (iter);
605     }
606 }
607
608 static void
609 gtk_application_get_property (GObject    *object,
610                               guint       prop_id,
611                               GValue     *value,
612                               GParamSpec *pspec)
613 {
614   GtkApplication *application = GTK_APPLICATION (object);
615
616   switch (prop_id)
617     {
618     case PROP_REGISTER_SESSION:
619       g_value_set_boolean (value, application->priv->register_session);
620       break;
621
622     case PROP_APP_MENU:
623       g_value_set_object (value, gtk_application_get_app_menu (application));
624       break;
625
626     case PROP_MENUBAR:
627       g_value_set_object (value, gtk_application_get_menubar (application));
628       break;
629
630     default:
631       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
632       break;
633     }
634 }
635
636 static void
637 gtk_application_set_property (GObject      *object,
638                               guint         prop_id,
639                               const GValue *value,
640                               GParamSpec   *pspec)
641 {
642   GtkApplication *application = GTK_APPLICATION (object);
643
644   switch (prop_id)
645     {
646     case PROP_REGISTER_SESSION:
647       application->priv->register_session = g_value_get_boolean (value);
648       break;
649
650     case PROP_APP_MENU:
651       gtk_application_set_app_menu (application, g_value_get_object (value));
652       break;
653
654     case PROP_MENUBAR:
655       gtk_application_set_menubar (application, g_value_get_object (value));
656       break;
657
658     default:
659       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
660       break;
661     }
662 }
663
664 static void
665 gtk_application_finalize (GObject *object)
666 {
667   GtkApplication *application = GTK_APPLICATION (object);
668
669   g_clear_object (&application->priv->app_menu);
670   g_clear_object (&application->priv->menubar);
671
672   G_OBJECT_CLASS (gtk_application_parent_class)
673     ->finalize (object);
674 }
675
676 static void
677 gtk_application_class_init (GtkApplicationClass *class)
678 {
679   GObjectClass *object_class = G_OBJECT_CLASS (class);
680   GApplicationClass *application_class = G_APPLICATION_CLASS (class);
681
682   object_class->get_property = gtk_application_get_property;
683   object_class->set_property = gtk_application_set_property;
684   object_class->finalize = gtk_application_finalize;
685
686   application_class->add_platform_data = gtk_application_add_platform_data;
687   application_class->before_emit = gtk_application_before_emit;
688   application_class->after_emit = gtk_application_after_emit;
689   application_class->startup = gtk_application_startup;
690   application_class->shutdown = gtk_application_shutdown;
691
692   class->window_added = gtk_application_window_added;
693   class->window_removed = gtk_application_window_removed;
694
695   g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
696
697   /**
698    * GtkApplication::window-added:
699    * @application: the #GtkApplication which emitted the signal
700    * @window: the newly-added #GtkWindow
701    *
702    * Emitted when a #GtkWindow is added to @application through
703    * gtk_application_add_window().
704    *
705    * Since: 3.2
706    */
707   gtk_application_signals[WINDOW_ADDED] =
708     g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
709                   G_STRUCT_OFFSET (GtkApplicationClass, window_added),
710                   NULL, NULL,
711                   g_cclosure_marshal_VOID__OBJECT,
712                   G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
713
714   /**
715    * GtkApplication::window-removed:
716    * @application: the #GtkApplication which emitted the signal
717    * @window: the #GtkWindow that is being removed
718    *
719    * Emitted when a #GtkWindow is removed from @application,
720    * either as a side-effect of being destroyed or explicitly
721    * through gtk_application_remove_window().
722    *
723    * Since: 3.2
724    */
725   gtk_application_signals[WINDOW_REMOVED] =
726     g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
727                   G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
728                   NULL, NULL,
729                   g_cclosure_marshal_VOID__OBJECT,
730                   G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
731
732   /**
733    * GtkApplication:register-session:
734    *
735    * Set this property to %TRUE to register with the session manager.
736    *
737    * Since: 3.4
738    */
739   g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
740     g_param_spec_boolean ("register-session",
741                           P_("Register session"),
742                           P_("Register with the session manager"),
743                           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
744
745   g_object_class_install_property (object_class, PROP_APP_MENU,
746     g_param_spec_object ("app-menu",
747                          P_("Application menu"),
748                          P_("The GMenuModel for the application menu"),
749                          G_TYPE_MENU_MODEL,
750                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
751
752   g_object_class_install_property (object_class, PROP_MENUBAR,
753     g_param_spec_object ("menubar",
754                          P_("Menubar"),
755                          P_("The GMenuModel for the menubar"),
756                          G_TYPE_MENU_MODEL,
757                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
758 }
759
760 /**
761  * gtk_application_new:
762  * @application_id: (allow-none): The application ID.
763  * @flags: the application flags
764  *
765  * Creates a new #GtkApplication instance.
766  *
767  * This function calls g_type_init() for you. gtk_init() is called
768  * as soon as the application gets registered as the primary instance.
769  *
770  * Concretely, gtk_init() is called in the default handler for the
771  * #GApplication::startup signal. Therefore, #GtkApplication subclasses should
772  * chain up in their #GApplication:startup handler before using any GTK+ API.
773  *
774  * Note that commandline arguments are not passed to gtk_init().
775  * All GTK+ functionality that is available via commandline arguments
776  * can also be achieved by setting suitable environment variables
777  * such as <envar>G_DEBUG</envar>, so this should not be a big
778  * problem. If you absolutely must support GTK+ commandline arguments,
779  * you can explicitly call gtk_init() before creating the application
780  * instance.
781  *
782  * If non-%NULL, the application ID must be valid.  See
783  * g_application_id_is_valid().
784  *
785  * If no application ID is given then some features (most notably application 
786  * uniqueness) will be disabled. A null application ID is only allowed with 
787  * GTK+ 3.6 or later.
788  *
789  * Returns: a new #GtkApplication instance
790  *
791  * Since: 3.0
792  */
793 GtkApplication *
794 gtk_application_new (const gchar       *application_id,
795                      GApplicationFlags  flags)
796 {
797   g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
798
799   g_type_init ();
800
801   return g_object_new (GTK_TYPE_APPLICATION,
802                        "application-id", application_id,
803                        "flags", flags,
804                        NULL);
805 }
806
807 /**
808  * gtk_application_add_window:
809  * @application: a #GtkApplication
810  * @window: a #GtkWindow
811  *
812  * Adds a window to @application.
813  *
814  * This call is equivalent to setting the #GtkWindow:application
815  * property of @window to @application.
816  *
817  * Normally, the connection between the application and the window
818  * will remain until the window is destroyed, but you can explicitly
819  * remove it with gtk_application_remove_window().
820  *
821  * GTK+ will keep the application running as long as it has
822  * any windows.
823  *
824  * Since: 3.0
825  **/
826 void
827 gtk_application_add_window (GtkApplication *application,
828                             GtkWindow      *window)
829 {
830   g_return_if_fail (GTK_IS_APPLICATION (application));
831
832   if (!g_list_find (application->priv->windows, window))
833     g_signal_emit (application,
834                    gtk_application_signals[WINDOW_ADDED], 0, window);
835 }
836
837 /**
838  * gtk_application_remove_window:
839  * @application: a #GtkApplication
840  * @window: a #GtkWindow
841  *
842  * Remove a window from @application.
843  *
844  * If @window belongs to @application then this call is equivalent to
845  * setting the #GtkWindow:application property of @window to
846  * %NULL.
847  *
848  * The application may stop running as a result of a call to this
849  * function.
850  *
851  * Since: 3.0
852  **/
853 void
854 gtk_application_remove_window (GtkApplication *application,
855                                GtkWindow      *window)
856 {
857   g_return_if_fail (GTK_IS_APPLICATION (application));
858
859   if (g_list_find (application->priv->windows, window))
860     g_signal_emit (application,
861                    gtk_application_signals[WINDOW_REMOVED], 0, window);
862 }
863
864 /**
865  * gtk_application_get_windows:
866  * @application: a #GtkApplication
867  *
868  * Gets a list of the #GtkWindows associated with @application.
869  *
870  * The list is sorted by most recently focused window, such that the first
871  * element is the currently focused window. (Useful for choosing a parent
872  * for a transient window.)
873  *
874  * The list that is returned should not be modified in any way. It will
875  * only remain valid until the next focus change or window creation or
876  * deletion.
877  *
878  * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
879  *
880  * Since: 3.0
881  **/
882 GList *
883 gtk_application_get_windows (GtkApplication *application)
884 {
885   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
886
887   return application->priv->windows;
888 }
889
890 /**
891  * gtk_application_get_window_by_id:
892  * @application: a #GtkApplication
893  * @id: an identifier number
894  *
895  * Returns: (transfer none): the #GtkApplicationWindow with ID @id, or
896  *   %NULL if there is no window with this ID
897  *
898  * Since: 3.6
899  */
900 GtkWindow *
901 gtk_application_get_window_by_id (GtkApplication *application,
902                                   guint           id)
903 {
904   GList *l;
905
906   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
907
908   for (l = application->priv->windows; l != NULL; l = l->next) 
909     {
910       if (GTK_IS_APPLICATION_WINDOW (l->data) &&
911           gtk_application_window_get_id (GTK_APPLICATION_WINDOW (l->data)) == id)
912         return l->data;
913     }
914
915   return NULL;
916 }
917
918 /**
919  * gtk_application_add_accelerator:
920  * @application: a #GtkApplication
921  * @accelerator: accelerator string
922  * @action_name: the name of the action to activate
923  * @parameter: (allow-none): parameter to pass when activating the action,
924  *   or %NULL if the action does not accept an activation parameter
925  *
926  * Installs an accelerator that will cause the named action
927  * to be activated when the key combination specificed by @accelerator
928  * is pressed.
929  *
930  * @accelerator must be a string that can be parsed by
931  * gtk_accelerator_parse(), e.g. "&lt;Primary&gt;q" or
932  * "&lt;Control&gt;&lt;Alt&gt;p".
933  *
934  * @action_name must be the name of an action as it would be used
935  * in the app menu, i.e. actions that have been added to the application
936  * are referred to with an "app." prefix, and window-specific actions
937  * with a "win." prefix.
938  *
939  * GtkApplication also extracts accelerators out of 'accel' attributes
940  * in the #GMenuModels passed to gtk_application_set_app_menu() and
941  * gtk_application_set_menubar(), which is usually more convenient
942  * than calling this function for each accelerator.
943  *
944  * Since: 3.4
945  */
946 void
947 gtk_application_add_accelerator (GtkApplication *application,
948                                  const gchar    *accelerator,
949                                  const gchar    *action_name,
950                                  GVariant       *parameter)
951 {
952   gchar *accel_path;
953   guint accel_key;
954   GdkModifierType accel_mods;
955
956   g_return_if_fail (GTK_IS_APPLICATION (application));
957
958   /* Call this here, since gtk_init() is only getting called in startup() */
959   _gtk_accel_map_init ();
960
961   gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
962
963   if (accel_key == 0)
964     {
965       g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
966       return;
967     }
968
969   accel_path = _gtk_accel_path_for_action (action_name, parameter);
970
971   if (gtk_accel_map_lookup_entry (accel_path, NULL))
972     gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
973   else
974     gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
975
976   g_free (accel_path);
977 }
978
979 /**
980  * gtk_application_remove_accelerator:
981  * @application: a #GtkApplication
982  * @action_name: the name of the action to activate
983  * @parameter: (allow-none): parameter to pass when activating the action,
984  *   or %NULL if the action does not accept an activation parameter
985  *
986  * Removes an accelerator that has been previously added
987  * with gtk_application_add_accelerator().
988  *
989  * Since: 3.4
990  */
991 void
992 gtk_application_remove_accelerator (GtkApplication *application,
993                                     const gchar    *action_name,
994                                     GVariant       *parameter)
995 {
996   gchar *accel_path;
997
998   g_return_if_fail (GTK_IS_APPLICATION (application));
999
1000   accel_path = _gtk_accel_path_for_action (action_name, parameter);
1001
1002   if (!gtk_accel_map_lookup_entry (accel_path, NULL))
1003     {
1004       g_warning ("No accelerator found for '%s'\n", accel_path);
1005       g_free (accel_path);
1006       return;
1007     }
1008
1009   gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1010   g_free (accel_path);
1011 }
1012
1013 /**
1014  * gtk_application_set_app_menu:
1015  * @application: a #GtkApplication
1016  * @app_menu: (allow-none): a #GMenuModel, or %NULL
1017  *
1018  * Sets or unsets the application menu for @application.
1019  *
1020  * This can only be done in the primary instance of the application,
1021  * after it has been registered.  #GApplication:startup is a good place
1022  * to call this.
1023  *
1024  * The application menu is a single menu containing items that typically
1025  * impact the application as a whole, rather than acting on a specific
1026  * window or document.  For example, you would expect to see
1027  * "Preferences" or "Quit" in an application menu, but not "Save" or
1028  * "Print".
1029  *
1030  * If supported, the application menu will be rendered by the desktop
1031  * environment.
1032  *
1033  * Use the base #GActionMap interface to add actions, to respond to the user
1034  * selecting these menu items.
1035  *
1036  * Since: 3.4
1037  */
1038 void
1039 gtk_application_set_app_menu (GtkApplication *application,
1040                               GMenuModel     *app_menu)
1041 {
1042   g_return_if_fail (GTK_IS_APPLICATION (application));
1043   g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1044   g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1045
1046   if (app_menu != application->priv->app_menu)
1047     {
1048       if (application->priv->app_menu != NULL)
1049         g_object_unref (application->priv->app_menu);
1050
1051       application->priv->app_menu = app_menu;
1052
1053       if (application->priv->app_menu != NULL)
1054         g_object_ref (application->priv->app_menu);
1055
1056       extract_accels_from_menu (app_menu, application);
1057
1058 #ifdef GDK_WINDOWING_X11
1059       gtk_application_set_app_menu_x11 (application, app_menu);
1060 #endif
1061
1062       g_object_notify (G_OBJECT (application), "app-menu");
1063     }
1064 }
1065
1066 /**
1067  * gtk_application_get_app_menu:
1068  * @application: a #GtkApplication
1069  *
1070  * Returns the menu model that has been set with
1071  * gtk_application_set_app_menu().
1072  *
1073  * Returns: (transfer none): the application menu of @application
1074  *
1075  * Since: 3.4
1076  */
1077 GMenuModel *
1078 gtk_application_get_app_menu (GtkApplication *application)
1079 {
1080   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1081
1082   return application->priv->app_menu;
1083 }
1084
1085 /**
1086  * gtk_application_set_menubar:
1087  * @application: a #GtkApplication
1088  * @menubar: (allow-none): a #GMenuModel, or %NULL
1089  *
1090  * Sets or unsets the menubar for windows of @application.
1091  *
1092  * This is a menubar in the traditional sense.
1093  *
1094  * This can only be done in the primary instance of the application,
1095  * after it has been registered.  #GApplication:startup is a good place
1096  * to call this.
1097  *
1098  * Depending on the desktop environment, this may appear at the top of
1099  * each window, or at the top of the screen.  In some environments, if
1100  * both the application menu and the menubar are set, the application
1101  * menu will be presented as if it were the first item of the menubar.
1102  * Other environments treat the two as completely separate -- for
1103  * example, the application menu may be rendered by the desktop shell
1104  * while the menubar (if set) remains in each individual window.
1105  *
1106  * Use the base #GActionMap interface to add actions, to respond to the user
1107  * selecting these menu items.
1108  *
1109  * Since: 3.4
1110  */
1111 void
1112 gtk_application_set_menubar (GtkApplication *application,
1113                              GMenuModel     *menubar)
1114 {
1115   g_return_if_fail (GTK_IS_APPLICATION (application));
1116   g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1117   g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1118
1119   if (menubar != application->priv->menubar)
1120     {
1121       if (application->priv->menubar != NULL)
1122         g_object_unref (application->priv->menubar);
1123
1124       application->priv->menubar = menubar;
1125
1126       if (application->priv->menubar != NULL)
1127         g_object_ref (application->priv->menubar);
1128
1129       extract_accels_from_menu (menubar, application);
1130
1131 #ifdef GDK_WINDOWING_X11
1132       gtk_application_set_menubar_x11 (application, menubar);
1133 #endif
1134
1135       g_object_notify (G_OBJECT (application), "menubar");
1136     }
1137 }
1138
1139 /**
1140  * gtk_application_get_menubar:
1141  * @application: a #GtkApplication
1142  *
1143  * Returns the menu model that has been set with
1144  * gtk_application_set_menubar().
1145  *
1146  * Returns: (transfer none): the menubar for windows of @application
1147  *
1148  * Since: 3.4
1149  */
1150 GMenuModel *
1151 gtk_application_get_menubar (GtkApplication *application)
1152 {
1153   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1154
1155   return application->priv->menubar;
1156 }
1157
1158 #if defined(GDK_WINDOWING_X11)
1159
1160 /* D-Bus Session Management
1161  *
1162  * The protocol and the D-Bus API are described here:
1163  * http://live.gnome.org/SessionManagement/GnomeSession
1164  * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1165  */
1166
1167 static void
1168 unregister_client (GtkApplication *app)
1169 {
1170   GError *error = NULL;
1171
1172   g_debug ("Unregistering client");
1173
1174   g_dbus_proxy_call_sync (app->priv->sm_proxy,
1175                           "UnregisterClient",
1176                           g_variant_new ("(o)", app->priv->client_path),
1177                           G_DBUS_CALL_FLAGS_NONE,
1178                           G_MAXINT,
1179                           NULL,
1180                           &error);
1181
1182   if (error)
1183     {
1184       g_warning ("Failed to unregister client: %s", error->message);
1185       g_error_free (error);
1186     }
1187
1188   g_clear_object (&app->priv->client_proxy);
1189
1190   g_free (app->priv->client_path);
1191   app->priv->client_path = NULL;
1192 }
1193
1194 static void
1195 gtk_application_quit_response (GtkApplication *application,
1196                                gboolean        will_quit,
1197                                const gchar    *reason)
1198 {
1199   g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1200
1201   g_dbus_proxy_call (application->priv->client_proxy,
1202                      "EndSessionResponse",
1203                      g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1204                      G_DBUS_CALL_FLAGS_NONE,
1205                      G_MAXINT,
1206                      NULL, NULL, NULL);
1207 }
1208 static void
1209 client_proxy_signal (GDBusProxy     *proxy,
1210                      const gchar    *sender_name,
1211                      const gchar    *signal_name,
1212                      GVariant       *parameters,
1213                      GtkApplication *app)
1214 {
1215   if (strcmp (signal_name, "QueryEndSession") == 0)
1216     {
1217       g_debug ("Received QueryEndSession");
1218       gtk_application_quit_response (app, TRUE, NULL);
1219     }
1220   else if (strcmp (signal_name, "CancelEndSession") == 0)
1221     {
1222       g_debug ("Received CancelEndSession");
1223     }
1224   else if (strcmp (signal_name, "EndSession") == 0)
1225     {
1226       g_debug ("Received EndSession");
1227       gtk_application_quit_response (app, TRUE, NULL);
1228       unregister_client (app);
1229       g_application_quit (G_APPLICATION (app));
1230     }
1231   else if (strcmp (signal_name, "Stop") == 0)
1232     {
1233       g_debug ("Received Stop");
1234       unregister_client (app);
1235       g_application_quit (G_APPLICATION (app));
1236     }
1237 }
1238
1239 static void
1240 gtk_application_startup_session_dbus (GtkApplication *app)
1241 {
1242   static gchar *client_id;
1243   GError *error = NULL;
1244   GVariant *res;
1245
1246   if (app->priv->session_bus == NULL)
1247     return;
1248
1249   if (client_id == NULL)
1250     {
1251       const gchar *desktop_autostart_id;
1252
1253       desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1254       /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1255        * use the same client id.
1256        */
1257       g_unsetenv ("DESKTOP_AUTOSTART_ID");
1258       client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1259     }
1260
1261   g_debug ("Connecting to session manager");
1262
1263   app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1264                                                G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1265                                                G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1266                                                NULL,
1267                                                "org.gnome.SessionManager",
1268                                                "/org/gnome/SessionManager",
1269                                                "org.gnome.SessionManager",
1270                                                NULL,
1271                                                &error);
1272   if (error)
1273     {
1274       g_warning ("Failed to get a session proxy: %s", error->message);
1275       g_error_free (error);
1276       return;
1277     }
1278
1279   /* FIXME: should we reuse the D-Bus application id here ? */
1280   app->priv->app_id = g_strdup (g_get_prgname ());
1281
1282   if (!app->priv->register_session)
1283     return;
1284
1285   g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1286
1287   res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1288                                 "RegisterClient",
1289                                 g_variant_new ("(ss)", app->priv->app_id, client_id),
1290                                 G_DBUS_CALL_FLAGS_NONE,
1291                                 G_MAXINT,
1292                                 NULL,
1293                                 &error);
1294
1295   if (error)
1296     {
1297       g_warning ("Failed to register client: %s", error->message);
1298       g_error_free (error);
1299       g_clear_object (&app->priv->sm_proxy);
1300       return;
1301     }
1302
1303   g_variant_get (res, "(o)", &app->priv->client_path);
1304   g_variant_unref (res);
1305
1306   g_debug ("Registered client at '%s'", app->priv->client_path);
1307
1308   app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1309                                                    NULL,
1310                                                    "org.gnome.SessionManager",
1311                                                    app->priv->client_path,
1312                                                    "org.gnome.SessionManager.ClientPrivate",
1313                                                    NULL,
1314                                                    &error);
1315   if (error)
1316     {
1317       g_warning ("Failed to get client proxy: %s", error->message);
1318       g_error_free (error);
1319       g_clear_object (&app->priv->sm_proxy);
1320       g_free (app->priv->client_path);
1321       app->priv->client_path = NULL;
1322       return;
1323     }
1324
1325   g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1326 }
1327
1328
1329
1330 /**
1331  * GtkApplicationInhibitFlags:
1332  * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1333  *     by logging out or by shutting down the computer
1334  * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1335  * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1336  *     session or computer
1337  * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1338  *     marked as idle (and possibly locked)
1339  *
1340  * Types of user actions that may be blocked by gtk_application_inhibit().
1341  *
1342  * Since: 3.4
1343  */
1344
1345 /**
1346  * gtk_application_inhibit:
1347  * @application: the #GApplication
1348  * @window: (allow-none): a #GtkWindow, or %NULL
1349  * @flags: what types of actions should be inhibited
1350  * @reason: (allow-none): a short, human-readable string that explains
1351  *     why these operations are inhibited
1352  *
1353  * Inform the session manager that certain types of actions should be
1354  * inhibited. This is not guaranteed to work on all platforms and for
1355  * all types of actions.
1356  *
1357  * Applications should invoke this method when they begin an operation
1358  * that should not be interrupted, such as creating a CD or DVD. The
1359  * types of actions that may be blocked are specified by the @flags
1360  * parameter. When the application completes the operation it should
1361  * call gtk_application_uninhibit() to remove the inhibitor. Note that
1362  * an application can have multiple inhibitors, and all of the must
1363  * be individually removed. Inhibitors are also cleared when the
1364  * application exits.
1365  *
1366  * Applications should not expect that they will always be able to block
1367  * the action. In most cases, users will be given the option to force
1368  * the action to take place.
1369  *
1370  * Reasons should be short and to the point.
1371  *
1372  * If @window is given, the session manager may point the user to
1373  * this window to find out more about why the action is inhibited.
1374  *
1375  * Returns: A non-zero cookie that is used to uniquely identify this
1376  *     request. It should be used as an argument to gtk_application_uninhibit()
1377  *     in order to remove the request. If the platform does not support
1378  *     inhibiting or the request failed for some reason, 0 is returned.
1379  *
1380  * Since: 3.4
1381  */
1382 guint
1383 gtk_application_inhibit (GtkApplication             *application,
1384                          GtkWindow                  *window,
1385                          GtkApplicationInhibitFlags  flags,
1386                          const gchar                *reason)
1387 {
1388   GVariant *res;
1389   GError *error = NULL;
1390   guint cookie;
1391   guint xid;
1392
1393   g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1394   g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1395   g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1396
1397   if (window != NULL)
1398     xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1399   else
1400     xid = 0;
1401
1402   res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1403                                 "Inhibit",
1404                                 g_variant_new ("(susu)",
1405                                                application->priv->app_id,
1406                                                xid,
1407                                                reason,
1408                                                flags),
1409                                 G_DBUS_CALL_FLAGS_NONE,
1410                                 G_MAXINT,
1411                                 NULL,
1412                                 &error);
1413  if (error)
1414     {
1415       g_warning ("Calling Inhibit failed: %s", error->message);
1416       g_error_free (error);
1417       return 0;
1418     }
1419
1420   g_variant_get (res, "(u)", &cookie);
1421   g_variant_unref (res);
1422
1423   return cookie;
1424 }
1425
1426 /**
1427  * gtk_application_uninhibit:
1428  * @application: the #GApplication
1429  * @cookie: a cookie that was returned by gtk_application_inhibit()
1430  *
1431  * Removes an inhibitor that has been established with gtk_application_inhibit().
1432  * Inhibitors are also cleared when the application exits.
1433  *
1434  * Since: 3.4
1435  */
1436 void
1437 gtk_application_uninhibit (GtkApplication *application,
1438                            guint           cookie)
1439 {
1440   g_return_if_fail (GTK_IS_APPLICATION (application));
1441   g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1442   g_return_if_fail (application->priv->sm_proxy != NULL);
1443
1444   g_dbus_proxy_call (application->priv->sm_proxy,
1445                      "Uninhibit",
1446                      g_variant_new ("(u)", cookie),
1447                      G_DBUS_CALL_FLAGS_NONE,
1448                      G_MAXINT,
1449                      NULL, NULL, NULL);
1450 }
1451
1452 /**
1453  * gtk_application_is_inhibited:
1454  * @application: the #GApplication
1455  * @flags: what types of actions should be queried
1456  *
1457  * Determines if any of the actions specified in @flags are
1458  * currently inhibited (possibly by another application).
1459  *
1460  * Returns: %TRUE if any of the actions specified in @flags are inhibited
1461  *
1462  * Since: 3.4
1463  */
1464 gboolean
1465 gtk_application_is_inhibited (GtkApplication             *application,
1466                               GtkApplicationInhibitFlags  flags)
1467 {
1468   GVariant *res;
1469   GError *error = NULL;
1470   gboolean inhibited;
1471
1472   g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1473   g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1474   g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1475
1476   res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1477                                 "IsInhibited",
1478                                 g_variant_new ("(u)", flags),
1479                                 G_DBUS_CALL_FLAGS_NONE,
1480                                 G_MAXINT,
1481                                 NULL,
1482                                 &error);
1483   if (error)
1484     {
1485       g_warning ("Calling IsInhibited failed: %s", error->message);
1486       g_error_free (error);
1487       return FALSE;
1488     }
1489
1490   g_variant_get (res, "(b)", &inhibited);
1491   g_variant_unref (res);
1492
1493   return inhibited;
1494 }
1495
1496 #elif defined(GDK_WINDOWING_QUARTZ)
1497
1498 /* OS X implementation copied from EggSMClient, but simplified since
1499  * it doesn't need to interact with the user.
1500  */
1501
1502 static gboolean
1503 idle_will_quit (gpointer data)
1504 {
1505   GtkApplication *app = data;
1506
1507   if (app->priv->quit_inhibit == 0)
1508     g_application_quit (G_APPLICATION (app));
1509   else
1510     {
1511       GtkApplicationQuartzInhibitor *inhibitor;
1512       GSList *iter;
1513       GtkWidget *dialog;
1514
1515       for (iter = app->priv->inhibitors; iter; iter = iter->next)
1516         {
1517           inhibitor = iter->data;
1518           if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1519             break;
1520         }
1521       g_assert (inhibitor != NULL);
1522
1523       dialog = gtk_message_dialog_new (inhibitor->window,
1524                                        GTK_DIALOG_MODAL,
1525                                        GTK_MESSAGE_ERROR,
1526                                        GTK_BUTTONS_OK,
1527                                        _("%s cannot quit at this time:\n\n%s"),
1528                                        g_get_application_name (),
1529                                        inhibitor->reason);
1530       g_signal_connect_swapped (dialog,
1531                                 "response",
1532                                 G_CALLBACK (gtk_widget_destroy),
1533                                 dialog);
1534       gtk_widget_show_all (dialog);
1535     }
1536
1537   return G_SOURCE_REMOVE;
1538 }
1539
1540 static pascal OSErr
1541 quit_requested (const AppleEvent *aevt,
1542                 AppleEvent       *reply,
1543                 long              refcon)
1544 {
1545   GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1546
1547   /* Don't emit the "quit" signal immediately, since we're
1548    * called from a weird point in the guts of gdkeventloop-quartz.c
1549    */
1550   g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1551
1552   return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1553 }
1554
1555 static void
1556 gtk_application_startup_session_quartz (GtkApplication *app)
1557 {
1558   if (app->priv->register_session)
1559     AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1560                            NewAEEventHandlerUPP (quit_requested),
1561                            (long)GPOINTER_TO_SIZE (app), false);
1562 }
1563
1564 guint
1565 gtk_application_inhibit (GtkApplication             *application,
1566                          GtkWindow                  *window,
1567                          GtkApplicationInhibitFlags  flags,
1568                          const gchar                *reason)
1569 {
1570   GtkApplicationQuartzInhibitor *inhibitor;
1571
1572   g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1573   g_return_val_if_fail (flags != 0, 0);
1574
1575   inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1576   inhibitor->cookie = ++application->priv->next_cookie;
1577   inhibitor->flags = flags;
1578   inhibitor->reason = g_strdup (reason);
1579   inhibitor->window = window ? g_object_ref (window) : NULL;
1580
1581   application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1582
1583   if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1584     application->priv->quit_inhibit++;
1585
1586   return inhibitor->cookie;
1587 }
1588
1589 void
1590 gtk_application_uninhibit (GtkApplication *application,
1591                            guint           cookie)
1592 {
1593   GSList *iter;
1594
1595   for (iter = application->priv->inhibitors; iter; iter = iter->next)
1596     {
1597       GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1598
1599       if (inhibitor->cookie == cookie)
1600         {
1601           if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1602             application->priv->quit_inhibit--;
1603           gtk_application_quartz_inhibitor_free (inhibitor);
1604           application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1605           return;
1606         }
1607     }
1608
1609   g_warning ("Invalid inhibitor cookie");
1610 }
1611
1612 gboolean
1613 gtk_application_is_inhibited (GtkApplication             *application,
1614                               GtkApplicationInhibitFlags  flags)
1615 {
1616   if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1617     return application->priv->quit_inhibit > 0;
1618
1619   return FALSE;
1620 }
1621
1622 #else
1623
1624 /* Trivial implementation.
1625  *
1626  * For the inhibit API on Windows, see
1627  * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1628  */
1629
1630 guint
1631 gtk_application_inhibit (GtkApplication             *application,
1632                          GtkWindow                  *window,
1633                          GtkApplicationInhibitFlags  flags,
1634                          const gchar                *reason)
1635 {
1636   return 0;
1637 }
1638
1639 void
1640 gtk_application_uninhibit (GtkApplication *application,
1641                            guint           cookie)
1642 {
1643 }
1644
1645 gboolean
1646 gtk_application_is_inhibited (GtkApplication             *application,
1647                               GtkApplicationInhibitFlags  flags)
1648 {
1649   return FALSE;
1650 }
1651
1652 #endif