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