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