Switch to using variants for timestamps, split out signals
[platform/upstream/glib.git] / gio / gapplication.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2010 Red Hat, Inc
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Colin Walters <walters@verbum.org>
21  *          Emmanuele Bassi <ebassi@linux.intel.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <gobject/gvaluecollector.h>
30
31 #include "gapplication.h"
32 #include "gio-marshal.h"
33 #include "glibintl.h"
34
35 #include "gioerror.h"
36
37 #include "gdbusconnection.h"
38 #include "gdbusintrospection.h"
39 #include "gdbusmethodinvocation.h"
40
41 #include "gioalias.h"
42
43 /**
44  * SECTION: gapplication
45  * @title: GApplication
46  * @short_description: Core application class
47  *
48  * A #GApplication is the foundation of an application, unique for a
49  * given application identifier.  The #GApplication wraps some
50  * low-level platform-specific services and is intended to act as the
51  * foundation for higher-level application classes such as
52  * #GtkApplication or #MxApplication.  In general, you should not use
53  * this class outside of a higher level framework.  By default,
54  * g_application_register_with_data() will invoke g_error() if it is
55  * run in a context where it cannot support its core features.  Note
56  * that g_error() is by default fatal.
57  *
58  * One of the core features that #GApplication provides is process
59  * uniqueness, in the context of a "session".  The session concept is
60  * platform-dependent, but corresponds roughly to a graphical desktop
61  * login.  When your application is launched again, its arguments
62  * are passed through platform communication to the already running
63  * program.
64  *
65  * In addition, #GApplication provides support for 'actions', which
66  * can be presented to the user in a platform-specific way
67  * (e.g. Windows 7 jump lists). Note that these are just simple
68  * actions without parameters. For more flexible scriptability,
69  * implementing a a separate D-Bus interface is recommended, see e.g.
70  * <xref linkend="gdbus-convenience"/>.
71  * 
72  * Finally, #GApplication acts as a basic lifecycle root; see the
73  * g_application_run() and g_application_quit_with_data() methods.
74  *
75  * Before using #GApplication, you must choose an "application identifier".
76  * The expected form of an application identifier is very close to that of
77  * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
78  * Examples include: "com.example.MyApp" "org.example.internal-apps.Calculator"
79  * For convenience, the restrictions on application identifiers are reproduced
80  * here:
81  * <itemizedlist>
82  *   <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
83  *   <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
84  *   <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
85  *   <listitem>Application identifiers must not exceed 255 characters.</listitem>
86  * </itemizedlist>
87  *
88  * <refsect2><title>D-Bus implementation</title>
89  * <para>
90  * On UNIX systems using D-Bus, #GApplication is implemented by claiming the
91  * application identifier as a bus name on the session bus. The implementation
92  * exports an object at the object path that is created by replacing '.' with
93  * '/' in the application identifier (e.g. the object path for the
94  * application id 'org.gtk.TestApp' is '/org/gtk/TestApp'). The object
95  * implements the org.gtk.Application interface.
96  * </para>
97  * <classsynopsis class="interface">
98  *   <ooclass><interfacename>org.gtk.Application</interfacename></ooclass>
99  *   <methodsynopsis>
100  *     <void/>
101  *     <methodname>Activate</methodname>
102  *     <methodparam><modifier>in</modifier><type>aay</type><parameter>arguments</parameter></methodparam>
103  *     <methodparam><modifier>in</modifier><type>a{sv}</type><parameter>data</parameter></methodparam>
104  *   </methodsynopsis>
105  *   <methodsynopsis>
106  *     <void/>
107  *     <methodname>InvokeAction</methodname>
108  *     <methodparam><modifier>in</modifier><type>s</type><parameter>action</parameter></methodparam>
109  *     <methodparam><modifier>in</modifier><type>a{sv}</type><parameter>data</parameter></methodparam>
110  *   </methodsynopsis>
111  *   <methodsynopsis>
112  *     <type>a{s(sb)}</type>
113  *     <methodname>ListActions</methodname>
114  *     <void/>
115  *   </methodsynopsis>
116  *   <methodsynopsis>
117  *     <void/>
118  *     <methodname>Quit</methodname>
119  *     <methodparam><modifier>in</modifier><type>a{sv}</type><parameter>data</parameter></methodparam>
120  *   </methodsynopsis>
121  *   <methodsynopsis>
122  *     <modifier>Signal</modifier>
123  *     <void/>
124  *     <methodname>ActionsChanged</methodname>
125  *     <void/>
126  *   </methodsynopsis>
127  * </classsynopsis>
128  * <para>
129  * The <methodname>Activate</methodname> function is called on the existing
130  * application instance when a second instance fails to take the bus name.
131  * @arguments contains the commandline arguments given to the second instance
132  * and @data contains platform-specific additional data.
133  * </para>
134  * <para>
135  * The <methodname>InvokeAction</methodname> function can be called to
136  * invoke one of the actions exported by the application.  On X11
137  * platforms, the platform_data argument should have a "timestamp"
138  * parameter of type "u" with the server time of the initiating event.
139  * </para>
140  * <para>
141  * The <methodname>ListActions</methodname> function returns a dictionary
142  * with the exported actions of the application. The keys of the dictionary
143  * are the action names, and the values are structs containing the description
144  * for the action and a boolean that represents if the action is enabled or not.
145  * </para>
146  * <para>
147  * The <methodname>Quit</methodname> function can be called to
148  * terminate the application. The @data parameter contains
149  * platform-specific data.  On X11 platforms, the platform_data
150  * argument should have a "timestamp" parameter of type "u" with the
151  * server time of the initiating event.
152  * </para>
153  * <para>
154  * The <methodname>ActionsChanged</methodname> signal is emitted when the
155  * exported actions change (i.e. an action is added, removed, enabled,
156  * disabled, or otherwise changed).
157  * </para>
158  * <para>
159  * #GApplication is supported since Gio 2.26.
160  * </para>
161  * </refsect2>
162  */
163
164 G_DEFINE_TYPE (GApplication, g_application, G_TYPE_OBJECT);
165
166 enum
167 {
168   PROP_0,
169
170   PROP_APPLICATION_ID,
171   PROP_DEFAULT_QUIT,
172   PROP_IS_REMOTE
173 };
174
175 enum
176 {
177   QUIT_WITH_DATA,
178   ACTION_WITH_DATA,
179   PREPARE_ACTIVATION,
180
181   LAST_SIGNAL
182 };
183
184 static guint application_signals[LAST_SIGNAL] = { 0 };
185
186 typedef struct {
187   gchar *name;
188   gchar *description;
189   guint enabled : 1;
190 } GApplicationAction;
191
192 struct _GApplicationPrivate
193 {
194   gchar *appid;
195   GHashTable *actions; /* name -> GApplicationAction */
196   GMainLoop *mainloop;
197
198   guint default_quit : 1;
199   guint is_remote    : 1;
200
201   guint actions_changed_id;
202
203 #ifdef G_OS_UNIX
204   gchar *dbus_path;
205   GDBusConnection *session_bus;
206 #endif
207 };
208
209 static GApplication *primary_application = NULL;
210 static GHashTable *instances_for_appid = NULL;
211
212 static void     _g_application_platform_init                    (GApplication  *app); 
213 static gboolean _g_application_platform_acquire_single_instance (GApplication  *app,
214                                                                  GError       **error);
215 static void     _g_application_platform_remote_invoke_action    (GApplication  *app,
216                                                                  const gchar   *action,
217                                                                  GVariant      *platform_data);
218 static void     _g_application_platform_remote_quit             (GApplication  *app,
219                                                                  GVariant      *platform_data);
220 static void     _g_application_platform_activate                (GApplication  *app,
221                                                                  GVariant      *data) G_GNUC_NORETURN;
222 static void     _g_application_platform_on_actions_changed      (GApplication  *app);
223
224 #ifdef G_OS_UNIX
225 #include "gdbusapplication.c"
226 #else
227 #include "gnullapplication.c"
228 #endif
229
230 static gboolean
231 _g_application_validate_id (const char *id)
232 {
233   gboolean allow_dot;
234
235   if (strlen (id) > 255)
236     return FALSE;
237
238   if (!g_ascii_isalpha (*id))
239     return FALSE;
240
241   id++;
242   allow_dot = FALSE;
243   for (; *id; id++)
244     {
245       if (g_ascii_isalnum (*id) || (*id == '-') || (*id == '_'))
246         allow_dot = TRUE;
247       else if (allow_dot && *id == '.')
248         allow_dot = FALSE;
249       else
250         return FALSE;
251     }
252   return TRUE;
253 }
254
255 static gpointer
256 init_appid_statics (gpointer data)
257 {
258   instances_for_appid = g_hash_table_new (g_str_hash, g_str_equal);
259   return NULL;
260 }
261
262 static GApplication *
263 application_for_appid (const char *appid)
264 {
265   static GOnce appid_once = G_ONCE_INIT;
266
267   g_once (&appid_once, init_appid_statics, NULL);
268
269   return g_hash_table_lookup (instances_for_appid, appid);
270 }
271
272 static gboolean
273 g_application_default_quit_with_data (GApplication *application,
274                                       GVariant     *platform_data)
275 {
276   g_return_val_if_fail (application->priv->mainloop != NULL, FALSE);
277   g_main_loop_quit (application->priv->mainloop);
278
279   return TRUE;
280 }
281
282 static void
283 g_application_default_run (GApplication *application)
284 {
285   if (application->priv->mainloop == NULL)
286     application->priv->mainloop = g_main_loop_new (NULL, TRUE);
287
288   g_main_loop_run (application->priv->mainloop);
289 }
290
291 static void
292 _g_application_handle_activation (GApplication  *app,
293                                   int            argc,
294                                   char         **argv,
295                                   GVariant      *platform_data)
296 {
297   GVariantBuilder builder;
298   GVariant *message;
299   int i;
300
301   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(aaya{sv})"));
302   g_variant_builder_open (&builder, G_VARIANT_TYPE ("aay"));
303
304   for (i = 1; i < argc; i++)
305     {
306       int j;
307       guint8 *argv_bytes;
308
309       g_variant_builder_open (&builder, G_VARIANT_TYPE ("ay"));
310
311       argv_bytes = (guint8*) argv[i];
312       for (j = 0; argv_bytes[j]; j++)
313         g_variant_builder_add_value (&builder,
314                                      g_variant_new_byte (argv_bytes[j]));
315       g_variant_builder_close (&builder);
316     }
317   g_variant_builder_close (&builder);
318
319   if (platform_data)
320     g_variant_builder_add (&builder, "@a{sv}", platform_data);
321   else
322     {
323       g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
324       g_variant_builder_close (&builder);
325     }
326
327   message = g_variant_builder_end (&builder);
328   _g_application_platform_activate (app, message);
329   g_variant_unref (message);
330 }
331
332 static gboolean
333 timeout_handle_actions_changed (gpointer user_data)
334 {
335   GApplication *application = user_data;
336
337   application->priv->actions_changed_id = 0;
338
339   _g_application_platform_on_actions_changed (application);
340
341   return FALSE;
342 }
343
344 static inline void
345 queue_actions_change_notification (GApplication *application)
346 {
347   GApplicationPrivate *priv = application->priv;
348
349   if (priv->actions_changed_id == 0)
350     priv->actions_changed_id = g_timeout_add (0, timeout_handle_actions_changed, application);
351 }
352
353 static void
354 g_application_action_free (gpointer data)
355 {
356   if (G_LIKELY (data != NULL))
357     {
358       GApplicationAction *action = data;
359
360       g_free (action->name);
361       g_free (action->description);
362
363       g_slice_free (GApplicationAction, action);
364     }
365 }
366
367
368 /**
369  * g_application_new:
370  * @appid: System-dependent application identifier
371  *
372  * Create a new #GApplication.  The application is initially in
373  * "remote" mode.  Almost certainly, you want to call
374  * g_application_register() immediately after this function, which
375  * will finish initialization.
376  *
377  * As a convenience, this function is defined to call g_type_init() as
378  * its very first action.
379  *
380  * Returns: (transfer full): An application instance
381  *
382  * Since: 2.26
383  */
384 GApplication *
385 g_application_new (const gchar *appid)
386 {
387   g_type_init ();
388
389   return G_APPLICATION (g_object_new (G_TYPE_APPLICATION, "application-id", appid, NULL));
390 }
391
392 /**
393  * g_application_register_with_data:
394  * @application: A #GApplication
395  * @argc: System argument count
396  * @argv: (array length=argc): System argument vector
397  * @platform_data: (allow-none): Arbitrary platform-specific data, must have signature "a{sv}"
398  *
399  * Ensure the current process is the unique owner of the application.
400  * If successful, the #GApplication:is-remote property will be changed
401  * to %FALSE, and it is safe to continue creating other resources
402  * such as graphics windows.
403  *
404  * If the given @appid is already running in another process, the
405  * #GApplication:default-exit property will be evaluated.  If it's
406  * %TRUE, then a platform-specific action such as bringing any
407  * graphics windows associated with the application to the foreground
408  * may be initiated.  After that, the current process will terminate.
409  * If %FALSE, then the application remains in the #GApplication:is-remote
410  * state, and you can e.g. call g_application_invoke_action().
411  *
412  * This function may do synchronous I/O to obtain unique ownership
413  * of the application id, and will block the calling thread in this
414  * case.
415  */
416 void
417 g_application_register_with_data (GApplication  *application,
418                                   gint           argc,
419                                   gchar        **argv,
420                                   GVariant      *platform_data)
421 {
422   g_return_if_fail (application->priv->appid != NULL);
423   g_return_if_fail (application->priv->is_remote);
424   g_return_if_fail (platform_data == NULL
425                     || g_variant_is_of_type (platform_data, G_VARIANT_TYPE ("a{sv}")));
426
427   if (!_g_application_platform_acquire_single_instance (application, NULL))
428     {
429       if (application->priv->default_quit)
430         _g_application_handle_activation (application, argc, argv, platform_data);
431       else
432         return;
433     }
434
435   application->priv->is_remote = FALSE;
436
437   _g_application_platform_init (application);
438 }
439
440 /**
441  * g_application_new_and_register:
442  * @appid: An application identifier
443  * @argc: System argument count
444  * @argv: (array length=argc): System argument vector
445  *
446  * This is a convenience function which combines g_application_new()
447  * with g_application_register_with_data(). Therefore, it may block
448  * the calling thread just like g_application_register_with_data().
449  */
450 GApplication *
451 g_application_new_and_register (const gchar  *appid,
452                                 gint          argc,
453                                 gchar       **argv)
454 {
455   GApplication *app = g_application_new (appid);
456   g_application_register_with_data (app, argc, argv, NULL);
457   return app;
458 }
459
460 /**
461  * g_application_add_action:
462  * @application: a #GApplication
463  * @name: the action name
464  * @description: the action description; can be a translatable
465  *   string
466  *
467  * Adds an action @name to the list of exported actions of @application.
468  *
469  * It is an error to call this function if @application is a proxy for
470  * a remote application.
471  *
472  * You can invoke an action using g_application_invoke_action().
473  *
474  * The newly added action is enabled by default; you can call
475  * g_application_set_action_enabled() to disable it.
476  *
477  * Since: 2.26
478  */
479 void
480 g_application_add_action (GApplication *application,
481                           const gchar  *name,
482                           const gchar  *description)
483 {
484   GApplicationPrivate *priv;
485   GApplicationAction *action;
486
487   g_return_if_fail (G_IS_APPLICATION (application));
488   g_return_if_fail (name != NULL && *name != '\0');
489   g_return_if_fail (!application->priv->is_remote);
490
491   priv = application->priv;
492
493   g_return_if_fail (g_hash_table_lookup (priv->actions, name) == NULL);
494
495   action = g_slice_new (GApplicationAction);
496   action->name = g_strdup (name);
497   action->description = g_strdup (description);
498   action->enabled = TRUE;
499
500   g_hash_table_insert (priv->actions, action->name, action);
501   queue_actions_change_notification (application);
502 }
503
504 /**
505  * g_application_remove_action:
506  * @application: a #GApplication
507  * @name: the name of the action to remove
508  *
509  * Removes the action @name from the list of exported actions of @application.
510  *
511  * It is an error to call this function if @application is a proxy for
512  * a remote application.
513  *
514  * Since: 2.26
515  */
516 void
517 g_application_remove_action (GApplication *application,
518                              const gchar  *name)
519 {
520   GApplicationPrivate *priv;
521
522   g_return_if_fail (G_IS_APPLICATION (application));
523   g_return_if_fail (name != NULL && *name != '\0');
524   g_return_if_fail (!application->priv->is_remote);
525
526   priv = application->priv;
527
528   g_return_if_fail (g_hash_table_lookup (priv->actions, name) != NULL);
529
530   g_hash_table_remove (priv->actions, name);
531   queue_actions_change_notification (application);
532 }
533
534 /**
535  * g_application_invoke_action:
536  * @application: a #GApplication
537  * @name: the name of the action to invoke
538  * @platform_data: (allow-none): platform-specific event data
539  *
540  * Invokes the action @name of the passed #GApplication.
541  *
542  * This function has different behavior depending on whether @application
543  * is acting as a proxy for another process.  In the normal case where
544  * the current process is hosting the application, and the specified
545  * action exists and is enabled, the #GApplication::action signal will
546  * be emitted.
547  *
548  * If @application is a proxy, then the specified action will be invoked
549  * in the remote process. It is not necessary to call
550  * g_application_add_action() in the current process in order to invoke
551  * one remotely.
552  *
553  * Since: 2.26
554  */
555 void
556 g_application_invoke_action (GApplication *application,
557                              const gchar  *name,
558                              GVariant     *platform_data)
559 {
560   GApplicationPrivate *priv;
561   GApplicationAction *action;
562
563   g_return_if_fail (G_IS_APPLICATION (application));
564   g_return_if_fail (name != NULL);
565   g_return_if_fail (platform_data == NULL
566                     || g_variant_is_of_type (platform_data, "a{sv}"));
567
568   priv = application->priv;
569
570   if (priv->is_remote)
571     {
572       _g_application_platform_remote_invoke_action (application, name, platform_data);
573       return;
574     }
575
576   action = g_hash_table_lookup (priv->actions, name);
577   g_return_if_fail (action != NULL);
578   if (!action->enabled)
579     return;
580
581   g_signal_emit (application, application_signals[ACTION_WITH_DATA],
582                  g_quark_from_string (name),
583                  name,
584                  platform_data);
585 }
586
587 /**
588  * g_application_list_actions:
589  * @application: a #GApplication
590  *
591  * Retrieves the list of action names currently exported by @application.
592  *
593  * It is an error to call this function if @application is a proxy for
594  * a remote application.
595  *
596  * Return value: (transfer full): a newly allocation, %NULL-terminated array
597  *   of strings containing action names; use g_strfreev() to free the
598  *   resources used by the returned array
599  *
600  * Since: 2.26
601  */
602 gchar **
603 g_application_list_actions (GApplication *application)
604 {
605   GApplicationPrivate *priv;
606   GHashTableIter iter;
607   gpointer key;
608   gchar **retval;
609   gint i;
610
611   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
612   g_return_val_if_fail (!application->priv->is_remote, NULL);
613
614   priv = application->priv;
615
616   retval = g_new (gchar*, g_hash_table_size (priv->actions));
617
618   i = 0;
619   g_hash_table_iter_init (&iter, priv->actions);
620   while (g_hash_table_iter_next (&iter, &key, NULL))
621     retval[i++] = g_strdup (key);
622
623   retval[i] = NULL;
624
625   return retval;
626 }
627
628 /**
629  * g_application_set_action_enabled:
630  * @application: a #GApplication
631  * @name: the name of the application
632  * @enabled: whether to enable or disable the action @name
633  *
634  * Sets whether the action @name inside @application should be enabled
635  * or disabled.
636  *
637  * It is an error to call this function if @application is a proxy for
638  * a remote application.
639  *
640  * Invoking a disabled action will not result in the #GApplication::action
641  * signal being emitted.
642  *
643  * Since: 2.26
644  */
645 void
646 g_application_set_action_enabled (GApplication *application,
647                                   const gchar  *name,
648                                   gboolean      enabled)
649 {
650   GApplicationAction *action;
651
652   g_return_if_fail (G_IS_APPLICATION (application));
653   g_return_if_fail (name != NULL);
654   g_return_if_fail (!application->priv->is_remote);
655
656   enabled = !!enabled;
657
658   action = g_hash_table_lookup (application->priv->actions, name);
659   g_return_if_fail (action != NULL);
660   if (action->enabled == enabled)
661     return;
662
663   action->enabled = enabled;
664
665   queue_actions_change_notification (application);
666 }
667
668
669 /**
670  * g_application_get_action_description:
671  * @application: a #GApplication
672  * @name: Action name
673  *
674  * Gets the description of the action @name.
675  *
676  * It is an error to call this function if @application is a proxy for
677  * a remote application.
678  *
679  * Returns: Description for the given action named @name
680  *
681  * Since: 2.26
682  */
683 G_CONST_RETURN gchar *
684 g_application_get_action_description (GApplication *application,
685                                       const gchar  *name)
686 {
687   GApplicationAction *action;
688   
689   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
690   g_return_val_if_fail (name != NULL, NULL);
691   g_return_val_if_fail (!application->priv->is_remote, NULL);
692
693   action = g_hash_table_lookup (application->priv->actions, name);
694   g_return_val_if_fail (action != NULL, NULL);
695
696   return action->description;
697 }
698
699
700 /**
701  * g_application_get_action_enabled:
702  * @application: a #GApplication
703  * @name: the name of the action
704  *
705  * Retrieves whether the action @name is enabled or not.
706  *
707  * See g_application_set_action_enabled().
708  *
709  * It is an error to call this function if @application is a proxy for
710  * a remote application.
711  *
712  * Return value: %TRUE if the action was enabled, and %FALSE otherwise
713  *
714  * Since: 2.26
715  */
716 gboolean
717 g_application_get_action_enabled (GApplication *application,
718                                   const gchar  *name)
719 {
720   GApplicationAction *action;
721
722   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
723   g_return_val_if_fail (name != NULL, FALSE);
724   g_return_val_if_fail (!application->priv->is_remote, FALSE);
725
726   action = g_hash_table_lookup (application->priv->actions, name);
727   g_return_val_if_fail (action != NULL, FALSE);
728
729   return action->enabled;
730 }
731
732 /**
733  * g_application_run:
734  * @application: a #GApplication
735  *
736  * Starts the application.
737  *
738  * The default implementation of this virtual function will simply run
739  * a main loop.
740  *
741  * It is an error to call this function if @application is a proxy for
742  * a remote application.
743  *
744  * Since: 2.26
745  */
746 void
747 g_application_run (GApplication *application)
748 {
749   g_return_if_fail (G_IS_APPLICATION (application));
750   g_return_if_fail (!application->priv->is_remote);
751
752   G_APPLICATION_GET_CLASS (application)->run (application);
753 }
754
755 /**
756  * g_application_quit_with_data:
757  * @application: a #GApplication
758  * @platform_data: (allow-none): platform-specific data
759  *
760  * Request that the application quits.
761  *
762  * This function has different behavior depending on whether @application
763  * is acting as a proxy for another process.  In the normal case where
764  * the current process is hosting the application, the default
765  * implementation will quit the main loop created by g_application_run().
766  *
767  * If @application is a proxy, then the remote process will be asked
768  * to quit.
769  *
770  * Returns: %TRUE if the application accepted the request, %FALSE otherwise
771  *
772  * Since: 2.26
773  */
774 gboolean
775 g_application_quit_with_data (GApplication *application,
776                               GVariant     *platform_data)
777 {
778   gboolean retval = FALSE;
779
780   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
781   g_return_val_if_fail (platform_data == NULL
782                         || g_variant_is_of_type (platform_data, "a{sv}"), FALSE);
783
784   if (application->priv->is_remote)
785     {
786        _g_application_platform_remote_quit (application, platform_data);
787        retval = TRUE;
788     }
789   else
790     g_signal_emit (application, application_signals[QUIT_WITH_DATA], 0, platform_data, &retval);
791
792   return retval;
793 }
794
795 /**
796  * g_application_get_instance:
797  *
798  * In the normal case where there is exactly one #GApplication instance
799  * in this process, return that instance.  If there are multiple, the
800  * first one created will be returned.  Otherwise, return %NULL.
801  *
802  * Returns: (transfer none): The primary instance of #GApplication,
803  *   or %NULL if none is set
804  *
805  * Since: 2.26
806  */
807 GApplication *
808 g_application_get_instance (void)
809 {
810   return primary_application;
811 }
812
813 /**
814  * g_application_get_id:
815  * @application: a #GApplication
816  *
817  * Retrieves the platform-specific identifier for the #GApplication.
818  *
819  * Return value: The platform-specific identifier. The returned string
820  *   is owned by the #GApplication instance and it should never be
821  *   modified or freed
822  *
823  * Since: 2.26
824  */
825 G_CONST_RETURN gchar *
826 g_application_get_id (GApplication *application)
827 {
828   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
829
830   return application->priv->appid;
831 }
832
833 /**
834  * g_application_is_remote:
835  * @application: a #GApplication
836  *
837  * Returns: %TRUE if this object represents a proxy for a remote application.
838  */
839 gboolean
840 g_application_is_remote (GApplication *application)
841 {
842   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
843
844   return application->priv->is_remote;
845 }
846
847 static void
848 g_application_init (GApplication *app)
849 {
850   app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app,
851                                            G_TYPE_APPLICATION,
852                                            GApplicationPrivate);
853
854   app->priv->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
855                                               NULL,
856                                               g_application_action_free);
857   app->priv->default_quit = TRUE;
858   app->priv->is_remote = TRUE;
859 }
860
861 static void
862 g_application_get_property (GObject    *object,
863                             guint       prop_id,
864                             GValue     *value,
865                             GParamSpec *pspec)
866 {
867   GApplication *app = G_APPLICATION (object);
868
869   switch (prop_id)
870     {
871     case PROP_APPLICATION_ID:
872       g_value_set_string (value, g_application_get_id (app));
873       break;
874
875     case PROP_DEFAULT_QUIT:
876       g_value_set_boolean (value, app->priv->default_quit);
877       break;
878
879     case PROP_IS_REMOTE:
880       g_value_set_boolean (value, g_application_is_remote (app));
881       break;
882
883     default:
884       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
885     }
886 }
887
888 static void
889 g_application_set_property (GObject      *object,
890                             guint         prop_id,
891                             const GValue *value,
892                             GParamSpec   *pspec)
893 {
894   GApplication *app = G_APPLICATION (object);
895
896   switch (prop_id)
897     {
898     case PROP_APPLICATION_ID:
899       g_return_if_fail (_g_application_validate_id (g_value_get_string (value)));
900       app->priv->appid = g_value_dup_string (value);
901       break;
902
903     case PROP_DEFAULT_QUIT:
904       app->priv->default_quit = g_value_get_boolean (value);
905       break;
906
907     default:
908       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
909     }
910 }
911
912 static GObject*
913 g_application_constructor (GType                  type,
914                            guint                  n_construct_properties,
915                            GObjectConstructParam *construct_params)
916 {
917   GApplication *app;
918   GObject *object;
919   const char *appid = NULL;
920   guint i;
921
922   for (i = 0; i < n_construct_properties; i++)
923     {
924       GObjectConstructParam *param = &construct_params[i];
925       if (strcmp (param->pspec->name, "application-id") == 0)
926         appid = g_value_get_string (param->value);
927     }
928
929   g_return_val_if_fail (appid != NULL, NULL);
930
931   app = application_for_appid (appid);
932   if (app != NULL)
933     return g_object_ref (app);
934
935   object = (* G_OBJECT_CLASS (g_application_parent_class)->constructor) (type,
936                                                                          n_construct_properties,
937                                                                          construct_params);
938   app = G_APPLICATION (object);
939
940   if (primary_application == NULL)
941     primary_application = app;
942   g_hash_table_insert (instances_for_appid, g_strdup (appid), app);
943
944   return object;
945 }
946
947 static void
948 g_application_finalize (GObject *object)
949 {
950   GApplication *app = G_APPLICATION (object);
951
952   g_free (app->priv->appid);
953   if (app->priv->actions)
954     g_hash_table_unref (app->priv->actions);
955   if (app->priv->actions_changed_id)
956     g_source_remove (app->priv->actions_changed_id);
957   if (app->priv->mainloop)
958     g_main_loop_unref (app->priv->mainloop);
959
960 #ifdef G_OS_UNIX
961   g_free (app->priv->dbus_path);
962   if (app->priv->session_bus)
963     g_object_unref (app->priv->session_bus);
964 #endif
965
966   G_OBJECT_CLASS (g_application_parent_class)->finalize (object);
967 }
968
969 static void
970 g_application_class_init (GApplicationClass *klass)
971 {
972   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
973
974   g_type_class_add_private (klass, sizeof (GApplicationPrivate));
975
976   gobject_class->constructor = g_application_constructor;
977   gobject_class->set_property = g_application_set_property;
978   gobject_class->get_property = g_application_get_property;
979
980   gobject_class->finalize = g_application_finalize;
981
982   klass->run = g_application_default_run;
983   klass->quit_with_data = g_application_default_quit_with_data;
984
985   /**
986    * GApplication::quit-with-data:
987    * @application: the object on which the signal is emitted
988    * @platform_data: Platform-specific data, or %NULL
989    *
990    * This signal is emitted when the Quit action is invoked on the
991    * application.
992    *
993    * The default handler for this signal exits the mainloop of the
994    * application.
995    *
996    * Returns: %TRUE if the signal has been handled, %FALSE to continue
997    *   signal emission
998    */
999   application_signals[QUIT_WITH_DATA] =
1000     g_signal_new (g_intern_static_string ("quit-with-data"),
1001                   G_OBJECT_CLASS_TYPE (klass),
1002                   G_SIGNAL_RUN_LAST,
1003                   G_STRUCT_OFFSET (GApplicationClass, quit_with_data),
1004                   g_signal_accumulator_true_handled, NULL,
1005                   _gio_marshal_BOOLEAN__BOXED,
1006                   G_TYPE_BOOLEAN, 1,
1007                   G_TYPE_VARIANT);
1008
1009   /**
1010    * GApplication::action-with-data:
1011    * @application: the object on which the signal is emitted
1012    * @name: The name of the activated action
1013    * @platform_data: Platform-specific data, or %NULL
1014    *
1015    * This signal is emitted when an action is activated. The action name
1016    * is passed as the first argument, but also as signal detail, so it
1017    * is possible to connect to this signal for individual actions.
1018    *
1019    * The signal is never emitted for disabled actions.
1020    */
1021   application_signals[ACTION_WITH_DATA] =
1022     g_signal_new (g_intern_static_string ("action-with-data"),
1023                   G_OBJECT_CLASS_TYPE (klass),
1024                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
1025                   G_STRUCT_OFFSET (GApplicationClass, action_with_data),
1026                   NULL, NULL,
1027                   _gio_marshal_VOID__STRING_BOXED,
1028                   G_TYPE_NONE, 2,
1029                   G_TYPE_STRING,
1030                   G_TYPE_VARIANT);
1031
1032    /**
1033    * GApplication::prepare-activation:
1034    * @application: the object on which the signal is emitted
1035    * @arguments: A #GVariant with the signature "aay"
1036    * @platform_data: A #GVariant with the signature "a{sv}", or %NULL
1037    *
1038    * This signal is emitted when a non-primary process for a given
1039    * application is invoked while your application is running; for
1040    * example, when a file browser launches your program to open a
1041    * file.  The raw operating system arguments are passed in the
1042    * @arguments variant.  Additional platform-dependent data is
1043    * stored in @platform_data.
1044    */
1045   application_signals[PREPARE_ACTIVATION] =
1046     g_signal_new (g_intern_static_string ("prepare-activation"),
1047                   G_OBJECT_CLASS_TYPE (klass),
1048                   G_SIGNAL_RUN_LAST,
1049                   G_STRUCT_OFFSET (GApplicationClass, prepare_activation),
1050                   NULL, NULL,
1051                   _gio_marshal_VOID__BOXED_BOXED,
1052                   G_TYPE_NONE, 2,
1053                   G_TYPE_VARIANT,
1054                   G_TYPE_VARIANT);
1055
1056    /**
1057    * GApplication:application-id:
1058    *
1059    * The unique identifier for this application.  See the documentation for
1060    * #GApplication for more information about this property.
1061    *
1062    */
1063   g_object_class_install_property (gobject_class,
1064                                    PROP_APPLICATION_ID,
1065                                    g_param_spec_string ("application-id",
1066                                                         P_("Application ID"),
1067                                                         P_("Identifier for this application"),
1068                                                         NULL,
1069                                                         G_PARAM_READWRITE |
1070                                                         G_PARAM_CONSTRUCT_ONLY |
1071                                                         G_PARAM_STATIC_STRINGS));
1072
1073   /**
1074    * GApplication:default-quit:
1075    *
1076    * By default, if a different process is running this application, the
1077    * process will be exited.  Set this property to %FALSE to allow custom
1078    * interaction with the remote process.
1079    *
1080    */
1081   g_object_class_install_property (gobject_class,
1082                                    PROP_DEFAULT_QUIT,
1083                                    g_param_spec_boolean ("default-quit",
1084                                                          P_("Default Quit"),
1085                                                          P_("Exit the process by default"),
1086                                                          TRUE,
1087                                                          G_PARAM_READWRITE |
1088                                                          G_PARAM_CONSTRUCT_ONLY |
1089                                                          G_PARAM_STATIC_STRINGS));
1090
1091
1092   /**
1093    * GApplication:is-remote:
1094    *
1095    * This property is %TRUE if this application instance represents a proxy
1096    * to the instance of this application in another process.
1097    *
1098    */
1099   g_object_class_install_property (gobject_class,
1100                                    PROP_IS_REMOTE,
1101                                    g_param_spec_boolean ("is-remote",
1102                                                          P_("Is Remote"),
1103                                                          P_("Whether this application is a proxy for another process"),
1104                                                          TRUE,
1105                                                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1106 }
1107
1108 #define __G_APPLICATION_C__
1109 #include "gioaliasdef.c"