Improve g_application_new documentation
[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-quit 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(). Note that proxy
448  * instances should not call g_application_add_action().
449  *
450  * This function may do synchronous I/O to obtain unique ownership
451  * of the application id, and will block the calling thread in this
452  * case.
453  *
454  * If the environment does not support the basic functionality of
455  * #GApplication, this function will invoke g_error(), which by
456  * default is a fatal operation.  This may arise for example on
457  * UNIX systems using D-Bus when the session bus is not available.
458  *
459  * As a convenience, this function is defined to call g_type_init() as
460  * its very first action.
461  *
462  * Returns: (transfer full): An application instance
463  *
464  * Since: 2.26
465  */
466 GApplication *
467 g_application_new (const gchar *appid,
468                    int          argc,
469                    char       **argv)
470 {
471   GObject *app;
472   GError *error = NULL;
473   GVariant *argv_variant;
474
475   g_type_init ();
476
477   g_return_val_if_fail (appid != NULL, NULL);
478   
479   argv_variant = variant_from_argv (argc, argv);
480   
481   app = g_initable_new (G_TYPE_APPLICATION, 
482                         NULL,
483                         &error,
484                         "application-id", appid, 
485                         "argv", argv_variant, 
486                         NULL);
487   if (!app)
488     {
489       g_error ("%s", error->message);
490       g_clear_error (&error);
491       return NULL;
492     }
493   return G_APPLICATION (app);
494 }
495
496 /**
497  * g_application_try_new:
498  * @appid: System-dependent application identifier
499  * @argc: Number of arguments in @argv
500  * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main() 
501  * @error: a #GError
502  *
503  * This function is similar to g_application_new(), but allows for
504  * more graceful fallback if the environment doesn't support the
505  * basic #GApplication functionality.
506  *
507  * Returns: (transfer full): An application instance
508  *
509  * Since: 2.26
510  */
511 GApplication *
512 g_application_try_new (const gchar *appid,
513                        int          argc,
514                        char       **argv,
515                        GError     **error)
516 {
517   GVariant *argv_variant;
518
519   g_type_init ();
520
521   g_return_val_if_fail (appid != NULL, NULL);
522   
523   argv_variant = variant_from_argv (argc, argv);
524   
525   return G_APPLICATION (g_initable_new (G_TYPE_APPLICATION, 
526                                         NULL,
527                                         error,
528                                         "application-id", appid, 
529                                         "argv", argv_variant, 
530                                         NULL));
531 }
532
533 /**
534  * g_application_unregistered_try_new:
535  * @appid: System-dependent application identifier
536  * @argc: Number of arguments in @argv
537  * @argv: (allow-none) (array length=argc): Argument vector, usually from the <parameter>argv</parameter> parameter of main() 
538  * @error: a #GError
539  *
540  * This function is similar to g_application_try_new(), but also
541  * sets the GApplication:register property to %FALSE.  You can later
542  * call g_application_register() to complete initialization.
543  *
544  * Returns: (transfer full): An application instance
545  *
546  * Since: 2.26
547  */
548 GApplication *
549 g_application_unregistered_try_new (const gchar *appid,
550                                     int          argc,
551                                     char       **argv,
552                                     GError     **error)
553 {
554   GVariant *argv_variant;
555
556   g_type_init ();
557
558   g_return_val_if_fail (appid != NULL, NULL);
559   
560   argv_variant = variant_from_argv (argc, argv);
561   
562   return G_APPLICATION (g_initable_new (G_TYPE_APPLICATION, 
563                                         NULL,
564                                         error,
565                                         "application-id", appid, 
566                                         "argv", argv_variant, 
567                                         "register", FALSE,
568                                         NULL));
569 }
570
571 /**
572  * g_application_register:
573  * @application: a #GApplication
574  *
575  * By default, #GApplication ensures process uniqueness when
576  * initialized, but this behavior is controlled by the
577  * GApplication:register property.  If it was given as %FALSE at
578  * construction time, this function allows you to later attempt
579  * to ensure uniqueness.
580  *
581  * Returns: %TRUE if registration was successful
582  */
583 gboolean
584 g_application_register (GApplication *application)
585 {
586   gboolean unique;
587
588   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
589   g_return_val_if_fail (application->priv->is_remote, FALSE);
590
591   if (!_g_application_platform_register (application, &unique, NULL, NULL))
592     return FALSE;
593   return unique;
594 }
595
596 /**
597  * g_application_add_action:
598  * @application: a #GApplication
599  * @name: the action name
600  * @description: the action description; can be a translatable
601  *   string
602  *
603  * Adds an action @name to the list of exported actions of @application.
604  *
605  * It is an error to call this function if @application is a proxy for
606  * a remote application.
607  *
608  * You can invoke an action using g_application_invoke_action().
609  *
610  * The newly added action is enabled by default; you can call
611  * g_application_set_action_enabled() to disable it.
612  *
613  * Since: 2.26
614  */
615 void
616 g_application_add_action (GApplication *application,
617                           const gchar  *name,
618                           const gchar  *description)
619 {
620   GApplicationPrivate *priv;
621   GApplicationAction *action;
622
623   g_return_if_fail (G_IS_APPLICATION (application));
624   g_return_if_fail (name != NULL && *name != '\0');
625   g_return_if_fail (!application->priv->is_remote);
626
627   priv = application->priv;
628
629   g_return_if_fail (g_hash_table_lookup (priv->actions, name) == NULL);
630
631   action = g_slice_new (GApplicationAction);
632   action->name = g_strdup (name);
633   action->description = g_strdup (description);
634   action->enabled = TRUE;
635
636   g_hash_table_insert (priv->actions, action->name, action);
637   queue_actions_change_notification (application);
638 }
639
640 /**
641  * g_application_remove_action:
642  * @application: a #GApplication
643  * @name: the name of the action to remove
644  *
645  * Removes the action @name from the list of exported actions of @application.
646  *
647  * It is an error to call this function if @application is a proxy for
648  * a remote application.
649  *
650  * Since: 2.26
651  */
652 void
653 g_application_remove_action (GApplication *application,
654                              const gchar  *name)
655 {
656   GApplicationPrivate *priv;
657
658   g_return_if_fail (G_IS_APPLICATION (application));
659   g_return_if_fail (name != NULL && *name != '\0');
660   g_return_if_fail (!application->priv->is_remote);
661
662   priv = application->priv;
663
664   g_return_if_fail (g_hash_table_lookup (priv->actions, name) != NULL);
665
666   g_hash_table_remove (priv->actions, name);
667   queue_actions_change_notification (application);
668 }
669
670 /**
671  * g_application_invoke_action:
672  * @application: a #GApplication
673  * @name: the name of the action to invoke
674  * @platform_data: (allow-none): platform-specific event data
675  *
676  * Invokes the action @name of the passed #GApplication.
677  *
678  * This function has different behavior depending on whether @application
679  * is acting as a proxy for another process.  In the normal case where
680  * the current process is hosting the application, and the specified
681  * action exists and is enabled, the #GApplication::action signal will
682  * be emitted.
683  *
684  * If @application is a proxy, then the specified action will be invoked
685  * in the remote process. It is not necessary to call
686  * g_application_add_action() in the current process in order to invoke
687  * one remotely.
688  *
689  * Since: 2.26
690  */
691 void
692 g_application_invoke_action (GApplication *application,
693                              const gchar  *name,
694                              GVariant     *platform_data)
695 {
696   GApplicationPrivate *priv;
697   GApplicationAction *action;
698
699   g_return_if_fail (G_IS_APPLICATION (application));
700   g_return_if_fail (name != NULL);
701   g_return_if_fail (platform_data == NULL
702                     || g_variant_is_of_type (platform_data, G_VARIANT_TYPE ("a{sv}")));
703   
704   if (platform_data == NULL)
705     platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
706   else
707     g_variant_ref (platform_data);
708
709   priv = application->priv;
710   
711   if (priv->is_remote)
712     {
713       _g_application_platform_remote_invoke_action (application, name, platform_data);
714       goto out;
715     }
716
717   action = g_hash_table_lookup (priv->actions, name);
718   g_return_if_fail (action != NULL);
719   if (!action->enabled)
720     goto out;
721
722   g_signal_emit (application, application_signals[ACTION_WITH_DATA],
723                  g_quark_from_string (name),
724                  name,
725                  platform_data);
726
727  out:
728   g_variant_unref (platform_data);
729 }
730
731 /**
732  * g_application_list_actions:
733  * @application: a #GApplication
734  *
735  * Retrieves the list of action names currently exported by @application.
736  *
737  * It is an error to call this function if @application is a proxy for
738  * a remote application.
739  *
740  * Return value: (transfer full): a newly allocation, %NULL-terminated array
741  *   of strings containing action names; use g_strfreev() to free the
742  *   resources used by the returned array
743  *
744  * Since: 2.26
745  */
746 gchar **
747 g_application_list_actions (GApplication *application)
748 {
749   GApplicationPrivate *priv;
750   GHashTableIter iter;
751   gpointer key;
752   gchar **retval;
753   gint i;
754
755   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
756   g_return_val_if_fail (!application->priv->is_remote, NULL);
757
758   priv = application->priv;
759
760   retval = g_new (gchar*, g_hash_table_size (priv->actions));
761
762   i = 0;
763   g_hash_table_iter_init (&iter, priv->actions);
764   while (g_hash_table_iter_next (&iter, &key, NULL))
765     retval[i++] = g_strdup (key);
766
767   retval[i] = NULL;
768
769   return retval;
770 }
771
772 /**
773  * g_application_set_action_enabled:
774  * @application: a #GApplication
775  * @name: the name of the application
776  * @enabled: whether to enable or disable the action @name
777  *
778  * Sets whether the action @name inside @application should be enabled
779  * or disabled.
780  *
781  * It is an error to call this function if @application is a proxy for
782  * a remote application.
783  *
784  * Invoking a disabled action will not result in the #GApplication::action
785  * signal being emitted.
786  *
787  * Since: 2.26
788  */
789 void
790 g_application_set_action_enabled (GApplication *application,
791                                   const gchar  *name,
792                                   gboolean      enabled)
793 {
794   GApplicationAction *action;
795
796   g_return_if_fail (G_IS_APPLICATION (application));
797   g_return_if_fail (name != NULL);
798   g_return_if_fail (!application->priv->is_remote);
799
800   enabled = !!enabled;
801
802   action = g_hash_table_lookup (application->priv->actions, name);
803   g_return_if_fail (action != NULL);
804   if (action->enabled == enabled)
805     return;
806
807   action->enabled = enabled;
808
809   queue_actions_change_notification (application);
810 }
811
812
813 /**
814  * g_application_get_action_description:
815  * @application: a #GApplication
816  * @name: Action name
817  *
818  * Gets the description of the action @name.
819  *
820  * It is an error to call this function if @application is a proxy for
821  * a remote application.
822  *
823  * Returns: Description for the given action named @name
824  *
825  * Since: 2.26
826  */
827 G_CONST_RETURN gchar *
828 g_application_get_action_description (GApplication *application,
829                                       const gchar  *name)
830 {
831   GApplicationAction *action;
832   
833   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
834   g_return_val_if_fail (name != NULL, NULL);
835   g_return_val_if_fail (!application->priv->is_remote, NULL);
836
837   action = g_hash_table_lookup (application->priv->actions, name);
838   g_return_val_if_fail (action != NULL, NULL);
839
840   return action->description;
841 }
842
843
844 /**
845  * g_application_get_action_enabled:
846  * @application: a #GApplication
847  * @name: the name of the action
848  *
849  * Retrieves whether the action @name is enabled or not.
850  *
851  * See g_application_set_action_enabled().
852  *
853  * It is an error to call this function if @application is a proxy for
854  * a remote application.
855  *
856  * Return value: %TRUE if the action was enabled, and %FALSE otherwise
857  *
858  * Since: 2.26
859  */
860 gboolean
861 g_application_get_action_enabled (GApplication *application,
862                                   const gchar  *name)
863 {
864   GApplicationAction *action;
865
866   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
867   g_return_val_if_fail (name != NULL, FALSE);
868   g_return_val_if_fail (!application->priv->is_remote, FALSE);
869
870   action = g_hash_table_lookup (application->priv->actions, name);
871   g_return_val_if_fail (action != NULL, FALSE);
872
873   return action->enabled;
874 }
875
876 /**
877  * g_application_run:
878  * @application: a #GApplication
879  *
880  * Starts the application.
881  *
882  * The default implementation of this virtual function will simply run
883  * a main loop.
884  *
885  * It is an error to call this function if @application is a proxy for
886  * a remote application.
887  *
888  * Since: 2.26
889  */
890 void
891 g_application_run (GApplication *application)
892 {
893   g_return_if_fail (G_IS_APPLICATION (application));
894   g_return_if_fail (!application->priv->is_remote);
895
896   G_APPLICATION_GET_CLASS (application)->run (application);
897 }
898
899 /**
900  * g_application_quit_with_data:
901  * @application: a #GApplication
902  * @platform_data: (allow-none): platform-specific data
903  *
904  * Request that the application quits.
905  *
906  * This function has different behavior depending on whether @application
907  * is acting as a proxy for another process.  In the normal case where
908  * the current process is hosting the application, the default
909  * implementation will quit the main loop created by g_application_run().
910  *
911  * If @application is a proxy, then the remote process will be asked
912  * to quit.
913  *
914  * Returns: %TRUE if the application accepted the request, %FALSE otherwise
915  *
916  * Since: 2.26
917  */
918 gboolean
919 g_application_quit_with_data (GApplication *application,
920                               GVariant     *platform_data)
921 {
922   gboolean retval = FALSE;
923
924   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
925   g_return_val_if_fail (platform_data == NULL
926                         || g_variant_is_of_type (platform_data, G_VARIANT_TYPE ("a{sv}")), FALSE);
927
928   if (platform_data == NULL)
929     platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
930   else
931     g_variant_ref (platform_data);
932
933   if (application->priv->is_remote)
934     {
935        _g_application_platform_remote_quit (application, platform_data);
936        retval = TRUE;
937     }
938   else
939     g_signal_emit (application, application_signals[QUIT_WITH_DATA], 0, platform_data, &retval);
940
941   g_variant_unref (platform_data);
942
943   return retval;
944 }
945
946 /**
947  * g_application_get_instance:
948  *
949  * In the normal case where there is exactly one #GApplication instance
950  * in this process, return that instance.  If there are multiple, the
951  * first one created will be returned.  Otherwise, return %NULL.
952  *
953  * Returns: (transfer none): The primary instance of #GApplication,
954  *   or %NULL if none is set
955  *
956  * Since: 2.26
957  */
958 GApplication *
959 g_application_get_instance (void)
960 {
961   return primary_application;
962 }
963
964 /**
965  * g_application_get_id:
966  * @application: a #GApplication
967  *
968  * Retrieves the platform-specific identifier for the #GApplication.
969  *
970  * Return value: The platform-specific identifier. The returned string
971  *   is owned by the #GApplication instance and it should never be
972  *   modified or freed
973  *
974  * Since: 2.26
975  */
976 G_CONST_RETURN gchar *
977 g_application_get_id (GApplication *application)
978 {
979   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
980
981   return application->priv->appid;
982 }
983
984 /**
985  * g_application_is_remote:
986  * @application: a #GApplication
987  *
988  * Returns whether the object represents a proxy for a remote application.
989  *
990  * Returns: %TRUE if this object represents a proxy for a remote application.
991  */
992 gboolean
993 g_application_is_remote (GApplication *application)
994 {
995   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
996
997   return application->priv->is_remote;
998 }
999
1000 static void
1001 g_application_init (GApplication *app)
1002 {
1003   app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app,
1004                                            G_TYPE_APPLICATION,
1005                                            GApplicationPrivate);
1006
1007   app->priv->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
1008                                               NULL,
1009                                               g_application_action_free);
1010   app->priv->default_quit = TRUE;
1011   app->priv->do_register = TRUE;
1012   app->priv->is_remote = TRUE;
1013   app->priv->platform_data = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
1014 }
1015
1016 static void
1017 g_application_get_property (GObject    *object,
1018                             guint       prop_id,
1019                             GValue     *value,
1020                             GParamSpec *pspec)
1021 {
1022   GApplication *app = G_APPLICATION (object);
1023
1024   switch (prop_id)
1025     {
1026     case PROP_APPLICATION_ID:
1027       g_value_set_string (value, g_application_get_id (app));
1028       break;
1029
1030     case PROP_DEFAULT_QUIT:
1031       g_value_set_boolean (value, app->priv->default_quit);
1032       break;
1033
1034     case PROP_IS_REMOTE:
1035       g_value_set_boolean (value, g_application_is_remote (app));
1036       break;
1037
1038     case PROP_REGISTER:
1039       g_value_set_boolean (value, app->priv->do_register);
1040       break;
1041
1042     case PROP_ARGV:
1043       g_value_set_variant (value, app->priv->argv);
1044       break;
1045
1046     case PROP_PLATFORM_DATA:
1047       g_value_set_variant (value, app->priv->platform_data);
1048       break;
1049
1050     default:
1051       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1052     }
1053 }
1054
1055 static void
1056 g_application_set_property (GObject      *object,
1057                             guint         prop_id,
1058                             const GValue *value,
1059                             GParamSpec   *pspec)
1060 {
1061   GApplication *app = G_APPLICATION (object);
1062
1063   switch (prop_id)
1064     {
1065     case PROP_APPLICATION_ID:
1066       g_return_if_fail (_g_application_validate_id (g_value_get_string (value)));
1067       app->priv->appid = g_value_dup_string (value);
1068       break;
1069
1070     case PROP_DEFAULT_QUIT:
1071       app->priv->default_quit = g_value_get_boolean (value);
1072       break;
1073
1074     case PROP_REGISTER:
1075       app->priv->do_register = g_value_get_boolean (value);
1076       break;
1077
1078     case PROP_ARGV:
1079       app->priv->argv = g_value_dup_variant (value);
1080       break;
1081
1082     case PROP_PLATFORM_DATA:
1083       {
1084         GVariant *platform_data = g_value_get_variant (value);
1085         if (app->priv->platform_data)
1086           g_variant_unref (app->priv->platform_data);
1087         app->priv->platform_data = g_variant_ref_sink (append_cwd_to_platform_data (platform_data));
1088       }
1089       break;
1090
1091     default:
1092       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1093     }
1094 }
1095
1096 static GObject*
1097 g_application_constructor (GType                  type,
1098                            guint                  n_construct_properties,
1099                            GObjectConstructParam *construct_params)
1100 {
1101   GApplication *app;
1102   GObject *object;
1103   const char *appid = NULL;
1104   guint i;
1105
1106   for (i = 0; i < n_construct_properties; i++)
1107     {
1108       GObjectConstructParam *param = &construct_params[i];
1109       if (strcmp (param->pspec->name, "application-id") == 0)
1110         appid = g_value_get_string (param->value);
1111     }
1112
1113   g_return_val_if_fail (appid != NULL, NULL);
1114
1115   app = application_for_appid (appid);
1116   if (app != NULL)
1117     return g_object_ref (app);
1118
1119   object = (* G_OBJECT_CLASS (g_application_parent_class)->constructor) (type,
1120                                                                          n_construct_properties,
1121                                                                          construct_params);
1122   app = G_APPLICATION (object);
1123
1124   if (primary_application == NULL)
1125     primary_application = app;
1126   g_hash_table_insert (instances_for_appid, g_strdup (appid), app);
1127
1128   return object;
1129 }
1130
1131 static void
1132 g_application_finalize (GObject *object)
1133 {
1134   GApplication *app = G_APPLICATION (object);
1135
1136   g_free (app->priv->appid);
1137   if (app->priv->actions)
1138     g_hash_table_unref (app->priv->actions);
1139   if (app->priv->actions_changed_id)
1140     g_source_remove (app->priv->actions_changed_id);
1141   if (app->priv->mainloop)
1142     g_main_loop_unref (app->priv->mainloop);
1143
1144 #ifdef G_OS_UNIX
1145   g_free (app->priv->dbus_path);
1146   if (app->priv->session_bus)
1147     g_object_unref (app->priv->session_bus);
1148 #endif
1149
1150   G_OBJECT_CLASS (g_application_parent_class)->finalize (object);
1151 }
1152
1153 static void
1154 g_application_class_init (GApplicationClass *klass)
1155 {
1156   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
1157
1158   g_type_class_add_private (klass, sizeof (GApplicationPrivate));
1159
1160   gobject_class->constructor = g_application_constructor;
1161   gobject_class->set_property = g_application_set_property;
1162   gobject_class->get_property = g_application_get_property;
1163
1164   gobject_class->finalize = g_application_finalize;
1165
1166   klass->run = g_application_default_run;
1167   klass->quit_with_data = g_application_default_quit_with_data;
1168
1169   /**
1170    * GApplication::quit-with-data:
1171    * @application: the object on which the signal is emitted
1172    * @platform_data: Platform-specific data, or %NULL
1173    *
1174    * This signal is emitted when the Quit action is invoked on the
1175    * application.
1176    *
1177    * The default handler for this signal exits the mainloop of the
1178    * application.
1179    *
1180    * Returns: %TRUE if the signal has been handled, %FALSE to continue
1181    *   signal emission
1182    */
1183   application_signals[QUIT_WITH_DATA] =
1184     g_signal_new (g_intern_static_string ("quit-with-data"),
1185                   G_OBJECT_CLASS_TYPE (klass),
1186                   G_SIGNAL_RUN_LAST,
1187                   G_STRUCT_OFFSET (GApplicationClass, quit_with_data),
1188                   g_signal_accumulator_true_handled, NULL,
1189                   _gio_marshal_BOOLEAN__VARIANT,
1190                   G_TYPE_BOOLEAN, 1,
1191                   G_TYPE_VARIANT);
1192
1193   /**
1194    * GApplication::action-with-data:
1195    * @application: the object on which the signal is emitted
1196    * @name: The name of the activated action
1197    * @platform_data: Platform-specific data, or %NULL
1198    *
1199    * This signal is emitted when an action is activated. The action name
1200    * is passed as the first argument, but also as signal detail, so it
1201    * is possible to connect to this signal for individual actions.
1202    *
1203    * The signal is never emitted for disabled actions.
1204    */
1205   application_signals[ACTION_WITH_DATA] =
1206     g_signal_new (g_intern_static_string ("action-with-data"),
1207                   G_OBJECT_CLASS_TYPE (klass),
1208                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
1209                   G_STRUCT_OFFSET (GApplicationClass, action_with_data),
1210                   NULL, NULL,
1211                   _gio_marshal_VOID__STRING_VARIANT,
1212                   G_TYPE_NONE, 2,
1213                   G_TYPE_STRING,
1214                   G_TYPE_VARIANT);
1215
1216    /**
1217    * GApplication::prepare-activation:
1218    * @application: the object on which the signal is emitted
1219    * @arguments: A #GVariant with the signature "aay"
1220    * @platform_data: A #GVariant with the signature "a{sv}", or %NULL
1221    *
1222    * This signal is emitted when a non-primary process for a given
1223    * application is invoked while your application is running; for
1224    * example, when a file browser launches your program to open a
1225    * file.  The raw operating system arguments are passed in the
1226    * @arguments variant.  Additional platform-dependent data is
1227    * stored in @platform_data.
1228    */
1229   application_signals[PREPARE_ACTIVATION] =
1230     g_signal_new (g_intern_static_string ("prepare-activation"),
1231                   G_OBJECT_CLASS_TYPE (klass),
1232                   G_SIGNAL_RUN_LAST,
1233                   G_STRUCT_OFFSET (GApplicationClass, prepare_activation),
1234                   NULL, NULL,
1235                   _gio_marshal_VOID__VARIANT_VARIANT,
1236                   G_TYPE_NONE, 2,
1237                   G_TYPE_VARIANT,
1238                   G_TYPE_VARIANT);
1239
1240   /**
1241    * GApplication:application-id:
1242    *
1243    * The unique identifier for this application.  See the documentation for
1244    * #GApplication for more information about this property.
1245    *
1246    */
1247   g_object_class_install_property (gobject_class,
1248                                    PROP_APPLICATION_ID,
1249                                    g_param_spec_string ("application-id",
1250                                                         P_("Application ID"),
1251                                                         P_("Identifier for this application"),
1252                                                         NULL,
1253                                                         G_PARAM_READWRITE |
1254                                                         G_PARAM_CONSTRUCT_ONLY |
1255                                                         G_PARAM_STATIC_STRINGS));
1256
1257   /**
1258    * GApplication:argv:
1259    *
1260    * The argument vector given to this application.  It must be a #GVariant
1261    * with a type signature "aay".
1262    *
1263    */
1264   g_object_class_install_property (gobject_class,
1265                                    PROP_ARGV,
1266                                    g_param_spec_variant ("argv",
1267                                                         P_("Argument vector"),
1268                                                         P_("System argument vector with type signature aay"),
1269                                                         G_VARIANT_TYPE ("aay"),
1270                                                         NULL,
1271                                                         G_PARAM_READWRITE |
1272                                                         G_PARAM_CONSTRUCT_ONLY |
1273                                                         G_PARAM_STATIC_STRINGS));
1274
1275   /**
1276    * GApplication:platform-data:
1277    *
1278    * Platform-specific data retrieved from the operating system
1279    * environment.  It must be a #GVariant with type signature "a{sv}".
1280    *
1281    */
1282   g_object_class_install_property (gobject_class,
1283                                    PROP_PLATFORM_DATA,
1284                                    g_param_spec_variant ("platform-data",
1285                                                          P_("Platform data"),
1286                                                          P_("Environmental data, must have type signature a{sv}"),
1287                                                          G_VARIANT_TYPE ("a{sv}"),
1288                                                          NULL,
1289                                                          G_PARAM_READWRITE |
1290                                                          G_PARAM_CONSTRUCT_ONLY |
1291                                                          G_PARAM_STATIC_STRINGS));
1292
1293   /**
1294    * GApplication:default-quit:
1295    *
1296    * By default, if a different process is running this application, the
1297    * process will be exited.  Set this property to %FALSE to allow custom
1298    * interaction with the remote process.
1299    *
1300    */
1301   g_object_class_install_property (gobject_class,
1302                                    PROP_DEFAULT_QUIT,
1303                                    g_param_spec_boolean ("default-quit",
1304                                                          P_("Default Quit"),
1305                                                          P_("Exit the process by default"),
1306                                                          TRUE,
1307                                                          G_PARAM_READWRITE |
1308                                                          G_PARAM_CONSTRUCT_ONLY |
1309                                                          G_PARAM_STATIC_STRINGS));
1310
1311
1312   /**
1313    * GApplication:is-remote:
1314    *
1315    * This property is %TRUE if this application instance represents a proxy
1316    * to the instance of this application in another process.
1317    *
1318    */
1319   g_object_class_install_property (gobject_class,
1320                                    PROP_IS_REMOTE,
1321                                    g_param_spec_boolean ("is-remote",
1322                                                          P_("Is Remote"),
1323                                                          P_("Whether this application is a proxy for another process"),
1324                                                          TRUE,
1325                                                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1326
1327   /**
1328    * GApplication:register:
1329    *
1330    * If this property is %FALSE, the application construction will not attempt
1331    * to ensure process uniqueness, and the application is guaranteed to be in the
1332    * remote state.  See GApplication:is-remote.
1333    */
1334   g_object_class_install_property (gobject_class,
1335                                    PROP_REGISTER,
1336                                    g_param_spec_boolean ("register",
1337                                                          P_("Register"),
1338                                                          P_("If false, do not "),
1339                                                          TRUE,
1340                                                          G_PARAM_READWRITE | 
1341                                                          G_PARAM_CONSTRUCT_ONLY |
1342                                                          G_PARAM_STATIC_STRINGS));
1343 }
1344
1345 #define __G_APPLICATION_C__
1346 #include "gioaliasdef.c"