]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplication.c
fd11c96445b41b60fd7e3751cb77767fc46a9bee
[~andy/gtk] / gtk / gtkapplication.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include "gtkapplication.h"
23
24 #include <stdlib.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <string.h>
29
30 #include "gtkapplicationprivate.h"
31 #include "gtkclipboard.h"
32 #include "gtkmarshalers.h"
33 #include "gtkmain.h"
34 #include "gtkrecentmanager.h"
35 #include "gtkaccelmapprivate.h"
36 #include "gactionmuxer.h"
37 #include "gtkintl.h"
38
39 #ifdef GDK_WINDOWING_QUARTZ
40 #include "gtkmodelmenu-quartz.h"
41 #import <Cocoa/Cocoa.h>
42 #include <Carbon/Carbon.h>
43 #include "gtkmessagedialog.h"
44 #endif
45
46 #include <gdk/gdk.h>
47 #ifdef GDK_WINDOWING_X11
48 #include <gdk/x11/gdkx.h>
49 #endif
50
51 /**
52  * SECTION:gtkapplication
53  * @title: GtkApplication
54  * @short_description: Application class
55  *
56  * #GtkApplication is a class that handles many important aspects
57  * of a GTK+ application in a convenient fashion, without enforcing
58  * a one-size-fits-all application model.
59  *
60  * Currently, GtkApplication handles GTK+ initialization, application
61  * uniqueness, session management, provides some basic scriptability and
62  * desktop shell integration by exporting actions and menus and manages a
63  * list of toplevel windows whose life-cycle is automatically tied to the
64  * life-cycle of your application.
65  *
66  * While GtkApplication works fine with plain #GtkWindows, it is recommended
67  * to use it together with #GtkApplicationWindow.
68  *
69  * When GDK threads are enabled, GtkApplication will acquire the GDK
70  * lock when invoking actions that arrive from other processes.  The GDK
71  * lock is not touched for local action invocations.  In order to have
72  * actions invoked in a predictable context it is therefore recommended
73  * that the GDK lock be held while invoking actions locally with
74  * g_action_group_activate_action().  The same applies to actions
75  * associated with #GtkApplicationWindow and to the 'activate' and
76  * 'open' #GApplication methods.
77  *
78  * To set an application menu for a GtkApplication, use
79  * gtk_application_set_app_menu(). The #GMenuModel that this function
80  * expects is usually constructed using #GtkBuilder, as seen in the
81  * following example. To specify a menubar that will be shown by
82  * #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: (allow-none): 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  * If non-%NULL, the application ID must be valid.  See
778  * g_application_id_is_valid().
779  *
780  * If no application ID is given then some features (most notably application 
781  * uniqueness) will be disabled. A null application ID is only allowed with 
782  * GTK+ 3.6 or later.
783  *
784  * Returns: a new #GtkApplication instance
785  *
786  * Since: 3.0
787  */
788 GtkApplication *
789 gtk_application_new (const gchar       *application_id,
790                      GApplicationFlags  flags)
791 {
792   g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
793
794   g_type_init ();
795
796   return g_object_new (GTK_TYPE_APPLICATION,
797                        "application-id", application_id,
798                        "flags", flags,
799                        NULL);
800 }
801
802 /**
803  * gtk_application_add_window:
804  * @application: a #GtkApplication
805  * @window: a #GtkWindow
806  *
807  * Adds a window to @application.
808  *
809  * This call is equivalent to setting the #GtkWindow:application
810  * property of @window to @application.
811  *
812  * Normally, the connection between the application and the window
813  * will remain until the window is destroyed, but you can explicitly
814  * remove it with gtk_application_remove_window().
815  *
816  * GTK+ will keep the application running as long as it has
817  * any windows.
818  *
819  * Since: 3.0
820  **/
821 void
822 gtk_application_add_window (GtkApplication *application,
823                             GtkWindow      *window)
824 {
825   g_return_if_fail (GTK_IS_APPLICATION (application));
826
827   if (!g_list_find (application->priv->windows, window))
828     g_signal_emit (application,
829                    gtk_application_signals[WINDOW_ADDED], 0, window);
830 }
831
832 /**
833  * gtk_application_remove_window:
834  * @application: a #GtkApplication
835  * @window: a #GtkWindow
836  *
837  * Remove a window from @application.
838  *
839  * If @window belongs to @application then this call is equivalent to
840  * setting the #GtkWindow:application property of @window to
841  * %NULL.
842  *
843  * The application may stop running as a result of a call to this
844  * function.
845  *
846  * Since: 3.0
847  **/
848 void
849 gtk_application_remove_window (GtkApplication *application,
850                                GtkWindow      *window)
851 {
852   g_return_if_fail (GTK_IS_APPLICATION (application));
853
854   if (g_list_find (application->priv->windows, window))
855     g_signal_emit (application,
856                    gtk_application_signals[WINDOW_REMOVED], 0, window);
857 }
858
859 /**
860  * gtk_application_get_windows:
861  * @application: a #GtkApplication
862  *
863  * Gets a list of the #GtkWindows associated with @application.
864  *
865  * The list is sorted by most recently focused window, such that the first
866  * element is the currently focused window. (Useful for choosing a parent
867  * for a transient window.)
868  *
869  * The list that is returned should not be modified in any way. It will
870  * only remain valid until the next focus change or window creation or
871  * deletion.
872  *
873  * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
874  *
875  * Since: 3.0
876  **/
877 GList *
878 gtk_application_get_windows (GtkApplication *application)
879 {
880   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
881
882   return application->priv->windows;
883 }
884
885 /**
886  * gtk_application_get_window_by_id:
887  * @application: a #GtkApplication
888  * @id: an identifier number
889  *
890  * Returns: (transfer none): the #GtkApplicationWindow with ID @id, or
891  *   %NULL if there is no window with this ID
892  *
893  * Since: 3.6
894  */
895 GtkWindow *
896 gtk_application_get_window_by_id (GtkApplication *application,
897                                   guint           id)
898 {
899   GList *l;
900
901   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
902
903   for (l = application->priv->windows; l != NULL; l = l->next) 
904     {
905       if (GTK_IS_APPLICATION_WINDOW (l->data) &&
906           gtk_application_window_get_id (GTK_APPLICATION_WINDOW (l->data)) == id)
907         return l->data;
908     }
909
910   return NULL;
911 }
912
913 /**
914  * gtk_application_add_accelerator:
915  * @application: a #GtkApplication
916  * @accelerator: accelerator string
917  * @action_name: the name of the action to activate
918  * @parameter: (allow-none): parameter to pass when activating the action,
919  *   or %NULL if the action does not accept an activation parameter
920  *
921  * Installs an accelerator that will cause the named action
922  * to be activated when the key combination specificed by @accelerator
923  * is pressed.
924  *
925  * @accelerator must be a string that can be parsed by
926  * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
927  *
928  * @action_name must be the name of an action as it would be used
929  * in the app menu, i.e. actions that have been added to the application
930  * are referred to with an "app." prefix, and window-specific actions
931  * with a "win." prefix.
932  *
933  * GtkApplication also extracts accelerators out of 'accel' attributes
934  * in the #GMenuModels passed to gtk_application_set_app_menu() and
935  * gtk_application_set_menubar(), which is usually more convenient
936  * than calling this function for each accelerator.
937  *
938  * Since: 3.4
939  */
940 void
941 gtk_application_add_accelerator (GtkApplication *application,
942                                  const gchar    *accelerator,
943                                  const gchar    *action_name,
944                                  GVariant       *parameter)
945 {
946   gchar *accel_path;
947   guint accel_key;
948   GdkModifierType accel_mods;
949
950   g_return_if_fail (GTK_IS_APPLICATION (application));
951
952   /* Call this here, since gtk_init() is only getting called in startup() */
953   _gtk_accel_map_init ();
954
955   gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
956
957   if (accel_key == 0)
958     {
959       g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
960       return;
961     }
962
963   accel_path = _gtk_accel_path_for_action (action_name, parameter);
964
965   if (gtk_accel_map_lookup_entry (accel_path, NULL))
966     gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
967   else
968     gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
969
970   g_free (accel_path);
971 }
972
973 /**
974  * gtk_application_remove_accelerator:
975  * @application: a #GtkApplication
976  * @action_name: the name of the action to activate
977  * @parameter: (allow-none): parameter to pass when activating the action,
978  *   or %NULL if the action does not accept an activation parameter
979  *
980  * Removes an accelerator that has been previously added
981  * with gtk_application_add_accelerator().
982  *
983  * Since: 3.4
984  */
985 void
986 gtk_application_remove_accelerator (GtkApplication *application,
987                                     const gchar    *action_name,
988                                     GVariant       *parameter)
989 {
990   gchar *accel_path;
991
992   g_return_if_fail (GTK_IS_APPLICATION (application));
993
994   accel_path = _gtk_accel_path_for_action (action_name, parameter);
995
996   if (!gtk_accel_map_lookup_entry (accel_path, NULL))
997     {
998       g_warning ("No accelerator found for '%s'\n", accel_path);
999       g_free (accel_path);
1000       return;
1001     }
1002
1003   gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1004   g_free (accel_path);
1005 }
1006
1007 /**
1008  * gtk_application_set_app_menu:
1009  * @application: a #GtkApplication
1010  * @app_menu: (allow-none): a #GMenuModel, or %NULL
1011  *
1012  * Sets or unsets the application menu for @application.
1013  *
1014  * This can only be done in the primary instance of the application,
1015  * after it has been registered.  #GApplication:startup is a good place
1016  * to call this.
1017  *
1018  * The application menu is a single menu containing items that typically
1019  * impact the application as a whole, rather than acting on a specific
1020  * window or document.  For example, you would expect to see
1021  * "Preferences" or "Quit" in an application menu, but not "Save" or
1022  * "Print".
1023  *
1024  * If supported, the application menu will be rendered by the desktop
1025  * environment.
1026  *
1027  * Use the base #GActionMap interface to add actions, to respond to the user
1028  * selecting these menu items.
1029  *
1030  * Since: 3.4
1031  */
1032 void
1033 gtk_application_set_app_menu (GtkApplication *application,
1034                               GMenuModel     *app_menu)
1035 {
1036   g_return_if_fail (GTK_IS_APPLICATION (application));
1037   g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1038   g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1039
1040   if (app_menu != application->priv->app_menu)
1041     {
1042       if (application->priv->app_menu != NULL)
1043         g_object_unref (application->priv->app_menu);
1044
1045       application->priv->app_menu = app_menu;
1046
1047       if (application->priv->app_menu != NULL)
1048         g_object_ref (application->priv->app_menu);
1049
1050       extract_accels_from_menu (app_menu, application);
1051
1052 #ifdef GDK_WINDOWING_X11
1053       gtk_application_set_app_menu_x11 (application, app_menu);
1054 #endif
1055
1056       g_object_notify (G_OBJECT (application), "app-menu");
1057     }
1058 }
1059
1060 /**
1061  * gtk_application_get_app_menu:
1062  * @application: a #GtkApplication
1063  *
1064  * Returns the menu model that has been set with
1065  * gtk_application_set_app_menu().
1066  *
1067  * Returns: (transfer none): the application menu of @application
1068  *
1069  * Since: 3.4
1070  */
1071 GMenuModel *
1072 gtk_application_get_app_menu (GtkApplication *application)
1073 {
1074   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1075
1076   return application->priv->app_menu;
1077 }
1078
1079 /**
1080  * gtk_application_set_menubar:
1081  * @application: a #GtkApplication
1082  * @menubar: (allow-none): a #GMenuModel, or %NULL
1083  *
1084  * Sets or unsets the menubar for windows of @application.
1085  *
1086  * This is a menubar in the traditional sense.
1087  *
1088  * This can only be done in the primary instance of the application,
1089  * after it has been registered.  #GApplication:startup is a good place
1090  * to call this.
1091  *
1092  * Depending on the desktop environment, this may appear at the top of
1093  * each window, or at the top of the screen.  In some environments, if
1094  * both the application menu and the menubar are set, the application
1095  * menu will be presented as if it were the first item of the menubar.
1096  * Other environments treat the two as completely separate -- for
1097  * example, the application menu may be rendered by the desktop shell
1098  * while the menubar (if set) remains in each individual window.
1099  *
1100  * Use the base #GActionMap interface to add actions, to respond to the user
1101  * selecting these menu items.
1102  *
1103  * Since: 3.4
1104  */
1105 void
1106 gtk_application_set_menubar (GtkApplication *application,
1107                              GMenuModel     *menubar)
1108 {
1109   g_return_if_fail (GTK_IS_APPLICATION (application));
1110   g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1111   g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1112
1113   if (menubar != application->priv->menubar)
1114     {
1115       if (application->priv->menubar != NULL)
1116         g_object_unref (application->priv->menubar);
1117
1118       application->priv->menubar = menubar;
1119
1120       if (application->priv->menubar != NULL)
1121         g_object_ref (application->priv->menubar);
1122
1123       extract_accels_from_menu (menubar, application);
1124
1125 #ifdef GDK_WINDOWING_X11
1126       gtk_application_set_menubar_x11 (application, menubar);
1127 #endif
1128
1129       g_object_notify (G_OBJECT (application), "menubar");
1130     }
1131 }
1132
1133 /**
1134  * gtk_application_get_menubar:
1135  * @application: a #GtkApplication
1136  *
1137  * Returns the menu model that has been set with
1138  * gtk_application_set_menubar().
1139  *
1140  * Returns: (transfer none): the menubar for windows of @application
1141  *
1142  * Since: 3.4
1143  */
1144 GMenuModel *
1145 gtk_application_get_menubar (GtkApplication *application)
1146 {
1147   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1148
1149   return application->priv->menubar;
1150 }
1151
1152 #if defined(GDK_WINDOWING_X11)
1153
1154 /* D-Bus Session Management
1155  *
1156  * The protocol and the D-Bus API are described here:
1157  * http://live.gnome.org/SessionManagement/GnomeSession
1158  * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1159  */
1160
1161 static void
1162 unregister_client (GtkApplication *app)
1163 {
1164   GError *error = NULL;
1165
1166   g_debug ("Unregistering client");
1167
1168   g_dbus_proxy_call_sync (app->priv->sm_proxy,
1169                           "UnregisterClient",
1170                           g_variant_new ("(o)", app->priv->client_path),
1171                           G_DBUS_CALL_FLAGS_NONE,
1172                           G_MAXINT,
1173                           NULL,
1174                           &error);
1175
1176   if (error)
1177     {
1178       g_warning ("Failed to unregister client: %s", error->message);
1179       g_error_free (error);
1180     }
1181
1182   g_clear_object (&app->priv->client_proxy);
1183
1184   g_free (app->priv->client_path);
1185   app->priv->client_path = NULL;
1186 }
1187
1188 static void
1189 gtk_application_quit_response (GtkApplication *application,
1190                                gboolean        will_quit,
1191                                const gchar    *reason)
1192 {
1193   g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1194
1195   g_dbus_proxy_call (application->priv->client_proxy,
1196                      "EndSessionResponse",
1197                      g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1198                      G_DBUS_CALL_FLAGS_NONE,
1199                      G_MAXINT,
1200                      NULL, NULL, NULL);
1201 }
1202 static void
1203 client_proxy_signal (GDBusProxy     *proxy,
1204                      const gchar    *sender_name,
1205                      const gchar    *signal_name,
1206                      GVariant       *parameters,
1207                      GtkApplication *app)
1208 {
1209   if (strcmp (signal_name, "QueryEndSession") == 0)
1210     {
1211       g_debug ("Received QueryEndSession");
1212       gtk_application_quit_response (app, TRUE, NULL);
1213     }
1214   else if (strcmp (signal_name, "CancelEndSession") == 0)
1215     {
1216       g_debug ("Received CancelEndSession");
1217     }
1218   else if (strcmp (signal_name, "EndSession") == 0)
1219     {
1220       g_debug ("Received EndSession");
1221       gtk_application_quit_response (app, TRUE, NULL);
1222       unregister_client (app);
1223       g_application_quit (G_APPLICATION (app));
1224     }
1225   else if (strcmp (signal_name, "Stop") == 0)
1226     {
1227       g_debug ("Received Stop");
1228       unregister_client (app);
1229       g_application_quit (G_APPLICATION (app));
1230     }
1231 }
1232
1233 static void
1234 gtk_application_startup_session_dbus (GtkApplication *app)
1235 {
1236   static gchar *client_id;
1237   GError *error = NULL;
1238   GVariant *res;
1239
1240   if (app->priv->session_bus == NULL)
1241     return;
1242
1243   if (client_id == NULL)
1244     {
1245       const gchar *desktop_autostart_id;
1246
1247       desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1248       /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1249        * use the same client id.
1250        */
1251       g_unsetenv ("DESKTOP_AUTOSTART_ID");
1252       client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1253     }
1254
1255   g_debug ("Connecting to session manager");
1256
1257   app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1258                                                G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1259                                                G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1260                                                NULL,
1261                                                "org.gnome.SessionManager",
1262                                                "/org/gnome/SessionManager",
1263                                                "org.gnome.SessionManager",
1264                                                NULL,
1265                                                &error);
1266   if (error)
1267     {
1268       g_warning ("Failed to get a session proxy: %s", error->message);
1269       g_error_free (error);
1270       return;
1271     }
1272
1273   /* FIXME: should we reuse the D-Bus application id here ? */
1274   app->priv->app_id = g_strdup (g_get_prgname ());
1275
1276   if (!app->priv->register_session)
1277     return;
1278
1279   g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1280
1281   res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1282                                 "RegisterClient",
1283                                 g_variant_new ("(ss)", app->priv->app_id, client_id),
1284                                 G_DBUS_CALL_FLAGS_NONE,
1285                                 G_MAXINT,
1286                                 NULL,
1287                                 &error);
1288
1289   if (error)
1290     {
1291       g_warning ("Failed to register client: %s", error->message);
1292       g_error_free (error);
1293       g_clear_object (&app->priv->sm_proxy);
1294       return;
1295     }
1296
1297   g_variant_get (res, "(o)", &app->priv->client_path);
1298   g_variant_unref (res);
1299
1300   g_debug ("Registered client at '%s'", app->priv->client_path);
1301
1302   app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1303                                                    NULL,
1304                                                    "org.gnome.SessionManager",
1305                                                    app->priv->client_path,
1306                                                    "org.gnome.SessionManager.ClientPrivate",
1307                                                    NULL,
1308                                                    &error);
1309   if (error)
1310     {
1311       g_warning ("Failed to get client proxy: %s", error->message);
1312       g_error_free (error);
1313       g_clear_object (&app->priv->sm_proxy);
1314       g_free (app->priv->client_path);
1315       app->priv->client_path = NULL;
1316       return;
1317     }
1318
1319   g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1320 }
1321
1322
1323
1324 /**
1325  * GtkApplicationInhibitFlags:
1326  * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1327  *     by logging out or by shutting down the computer
1328  * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1329  * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1330  *     session or computer
1331  * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1332  *     marked as idle (and possibly locked)
1333  *
1334  * Types of user actions that may be blocked by gtk_application_inhibit().
1335  *
1336  * Since: 3.4
1337  */
1338
1339 /**
1340  * gtk_application_inhibit:
1341  * @application: the #GApplication
1342  * @window: (allow-none): a #GtkWindow, or %NULL
1343  * @flags: what types of actions should be inhibited
1344  * @reason: (allow-none): a short, human-readable string that explains
1345  *     why these operations are inhibited
1346  *
1347  * Inform the session manager that certain types of actions should be
1348  * inhibited. This is not guaranteed to work on all platforms and for
1349  * all types of actions.
1350  *
1351  * Applications should invoke this method when they begin an operation
1352  * that should not be interrupted, such as creating a CD or DVD. The
1353  * types of actions that may be blocked are specified by the @flags
1354  * parameter. When the application completes the operation it should
1355  * call g_application_uninhibit() to remove the inhibitor. Note that
1356  * an application can have multiple inhibitors, and all of the must
1357  * be individually removed. Inhibitors are also cleared when the
1358  * application exits.
1359  *
1360  * Applications should not expect that they will always be able to block
1361  * the action. In most cases, users will be given the option to force
1362  * the action to take place.
1363  *
1364  * Reasons should be short and to the point.
1365  *
1366  * If @window is given, the session manager may point the user to
1367  * this window to find out more about why the action is inhibited.
1368  *
1369  * Returns: A non-zero cookie that is used to uniquely identify this
1370  *     request. It should be used as an argument to g_application_uninhibit()
1371  *     in order to remove the request. If the platform does not support
1372  *     inhibiting or the request failed for some reason, 0 is returned.
1373  *
1374  * Since: 3.4
1375  */
1376 guint
1377 gtk_application_inhibit (GtkApplication             *application,
1378                          GtkWindow                  *window,
1379                          GtkApplicationInhibitFlags  flags,
1380                          const gchar                *reason)
1381 {
1382   GVariant *res;
1383   GError *error = NULL;
1384   guint cookie;
1385   guint xid;
1386
1387   g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1388   g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1389   g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1390
1391   if (window != NULL)
1392     xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1393   else
1394     xid = 0;
1395
1396   res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1397                                 "Inhibit",
1398                                 g_variant_new ("(susu)",
1399                                                application->priv->app_id,
1400                                                xid,
1401                                                reason,
1402                                                flags),
1403                                 G_DBUS_CALL_FLAGS_NONE,
1404                                 G_MAXINT,
1405                                 NULL,
1406                                 &error);
1407  if (error)
1408     {
1409       g_warning ("Calling Inhibit failed: %s", error->message);
1410       g_error_free (error);
1411       return 0;
1412     }
1413
1414   g_variant_get (res, "(u)", &cookie);
1415   g_variant_unref (res);
1416
1417   return cookie;
1418 }
1419
1420 /**
1421  * gtk_application_uninhibit:
1422  * @application: the #GApplication
1423  * @cookie: a cookie that was returned by g_application_inhibit()
1424  *
1425  * Removes an inhibitor that has been established with g_application_inhibit().
1426  * Inhibitors are also cleared when the application exits.
1427  *
1428  * Since: 3.4
1429  */
1430 void
1431 gtk_application_uninhibit (GtkApplication *application,
1432                            guint           cookie)
1433 {
1434   g_return_if_fail (GTK_IS_APPLICATION (application));
1435   g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1436   g_return_if_fail (application->priv->sm_proxy != NULL);
1437
1438   g_dbus_proxy_call (application->priv->sm_proxy,
1439                      "Uninhibit",
1440                      g_variant_new ("(u)", cookie),
1441                      G_DBUS_CALL_FLAGS_NONE,
1442                      G_MAXINT,
1443                      NULL, NULL, NULL);
1444 }
1445
1446 /**
1447  * gtk_application_is_inhibited:
1448  * @application: the #GApplication
1449  * @flags: what types of actions should be queried
1450  *
1451  * Determines if any of the actions specified in @flags are
1452  * currently inhibited (possibly by another application).
1453  *
1454  * Returns: %TRUE if any of the actions specified in @flags are inhibited
1455  *
1456  * Since: 3.4
1457  */
1458 gboolean
1459 gtk_application_is_inhibited (GtkApplication             *application,
1460                               GtkApplicationInhibitFlags  flags)
1461 {
1462   GVariant *res;
1463   GError *error = NULL;
1464   gboolean inhibited;
1465
1466   g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1467   g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1468   g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1469
1470   res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1471                                 "IsInhibited",
1472                                 g_variant_new ("(u)", flags),
1473                                 G_DBUS_CALL_FLAGS_NONE,
1474                                 G_MAXINT,
1475                                 NULL,
1476                                 &error);
1477   if (error)
1478     {
1479       g_warning ("Calling IsInhibited failed: %s", error->message);
1480       g_error_free (error);
1481       return FALSE;
1482     }
1483
1484   g_variant_get (res, "(b)", &inhibited);
1485   g_variant_unref (res);
1486
1487   return inhibited;
1488 }
1489
1490 #elif defined(GDK_WINDOWING_QUARTZ)
1491
1492 /* OS X implementation copied from EggSMClient, but simplified since
1493  * it doesn't need to interact with the user.
1494  */
1495
1496 static gboolean
1497 idle_will_quit (gpointer data)
1498 {
1499   GtkApplication *app = data;
1500
1501   if (app->priv->quit_inhibit == 0)
1502     g_application_quit (G_APPLICATION (app));
1503   else
1504     {
1505       GtkApplicationQuartzInhibitor *inhibitor;
1506       GSList *iter;
1507       GtkWidget *dialog;
1508
1509       for (iter = app->priv->inhibitors; iter; iter = iter->next)
1510         {
1511           inhibitor = iter->data;
1512           if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1513             break;
1514         }
1515       g_assert (inhibitor != NULL);
1516
1517       dialog = gtk_message_dialog_new (inhibitor->window,
1518                                        GTK_DIALOG_MODAL,
1519                                        GTK_MESSAGE_ERROR,
1520                                        GTK_BUTTONS_OK,
1521                                        _("%s cannot quit at this time:\n\n%s"),
1522                                        g_get_application_name (),
1523                                        inhibitor->reason);
1524       g_signal_connect_swapped (dialog,
1525                                 "response",
1526                                 G_CALLBACK (gtk_widget_destroy),
1527                                 dialog);
1528       gtk_widget_show_all (dialog);
1529     }
1530
1531   return G_SOURCE_REMOVE;
1532 }
1533
1534 static pascal OSErr
1535 quit_requested (const AppleEvent *aevt,
1536                 AppleEvent       *reply,
1537                 long              refcon)
1538 {
1539   GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1540
1541   /* Don't emit the "quit" signal immediately, since we're
1542    * called from a weird point in the guts of gdkeventloop-quartz.c
1543    */
1544   g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1545
1546   return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1547 }
1548
1549 static void
1550 gtk_application_startup_session_quartz (GtkApplication *app)
1551 {
1552   if (app->priv->register_session)
1553     AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1554                            NewAEEventHandlerUPP (quit_requested),
1555                            (long)GPOINTER_TO_SIZE (app), false);
1556 }
1557
1558 guint
1559 gtk_application_inhibit (GtkApplication             *application,
1560                          GtkWindow                  *window,
1561                          GtkApplicationInhibitFlags  flags,
1562                          const gchar                *reason)
1563 {
1564   GtkApplicationQuartzInhibitor *inhibitor;
1565
1566   g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1567   g_return_val_if_fail (flags != 0, 0);
1568
1569   inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1570   inhibitor->cookie = ++application->priv->next_cookie;
1571   inhibitor->flags = flags;
1572   inhibitor->reason = g_strdup (reason);
1573   inhibitor->window = window ? g_object_ref (window) : NULL;
1574
1575   application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1576
1577   if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1578     application->priv->quit_inhibit++;
1579
1580   return inhibitor->cookie;
1581 }
1582
1583 void
1584 gtk_application_uninhibit (GtkApplication *application,
1585                            guint           cookie)
1586 {
1587   GSList *iter;
1588
1589   for (iter = application->priv->inhibitors; iter; iter = iter->next)
1590     {
1591       GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1592
1593       if (inhibitor->cookie == cookie)
1594         {
1595           if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1596             application->priv->quit_inhibit--;
1597           gtk_application_quartz_inhibitor_free (inhibitor);
1598           application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1599           return;
1600         }
1601     }
1602
1603   g_warning ("Invalid inhibitor cookie");
1604 }
1605
1606 gboolean
1607 gtk_application_is_inhibited (GtkApplication             *application,
1608                               GtkApplicationInhibitFlags  flags)
1609 {
1610   if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1611     return application->priv->quit_inhibit > 0;
1612
1613   return FALSE;
1614 }
1615
1616 #else
1617
1618 /* Trivial implementation.
1619  *
1620  * For the inhibit API on Windows, see
1621  * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1622  */
1623
1624 guint
1625 gtk_application_inhibit (GtkApplication             *application,
1626                          GtkWindow                  *window,
1627                          GtkApplicationInhibitFlags  flags,
1628                          const gchar                *reason)
1629 {
1630   return 0;
1631 }
1632
1633 void
1634 gtk_application_uninhibit (GtkApplication *application,
1635                            guint           cookie)
1636 {
1637 }
1638
1639 gboolean
1640 gtk_application_is_inhibited (GtkApplication             *application,
1641                               GtkApplicationInhibitFlags  flags)
1642 {
1643   return FALSE;
1644 }
1645
1646 #endif