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