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