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