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