Merge the wip/gapplication branch
[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
37 #include "gdbusconnection.h"
38 #include "gdbusintrospection.h"
39 #include "gdbusmethodinvocation.h"
40
41 #include "gioalias.h"
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
49  * a given application identifier.  The #GApplication wraps some
50  * low-level platform-specific services; it's expected that most
51  * software will use a higher-level application class such as
52  * #GtkApplication or #MxApplication.
53  *
54  * In addition to single-instance-ness, #GApplication provides support
55  * for 'actions', which can be presented to the user in a platform-specific
56  * way (e.g. Windows 7 jump lists). Note that these are just simple actions
57  * without parameters. For more flexible scriptability, implementing a
58  * a separate D-Bus interface is recommended, see e.g.
59  * <xref linkend="gdbus-convenience"/>.
60  *
61  * Before using #GApplication, you must choose an "application identifier".
62  * The expected form of an application identifier is very close to that of
63  * of a <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
64  * Examples include: "com.example.MyApp" "org.example.internal-apps.Calculator"
65  * For convenience, the restrictions on application identifiers are reproduced
66  * here:
67  * <itemizedlist>
68  *   <listitem>Application identifiers must contain only the ASCII characters "[A-Z][a-z][0-9]_-" and must not begin with a digit.</listitem>
69  *   <listitem>Application identifiers must contain at least one '.' (period) character (and thus at least two elements).</listitem>
70  *   <listitem>Application identifiers must not begin with a '.' (period) character.</listitem>
71  *   <listitem>Application identifiers must not exceed 255 characters.</listitem>
72  * </itemizedlist>
73  *
74  * <refsect2><title>D-Bus implementation</title>
75  * <para>
76  * On UNIX systems using D-Bus, #GApplication is implemented by claiming the
77  * application identifier as a bus name on the session bus. The implementation
78  * exports an object at the object path that is created by replacing '.' with
79  * '/' in the application identifier (e.g. the object path for the
80  * application id 'org.gtk.TestApp' is '/org/gtk/TestApp'). The object
81  * implements the org.gtk.Application interface.
82  * </para>
83  * <classsynopsis class="interface">
84  *   <ooclass><interfacename>org.gtk.Application</interfacename></ooclass>
85  *   <methodsynopsis>
86  *     <void/>
87  *     <methodname>Activate</methodname>
88  *     <methodparam><modifier>in</modifier><type>aay</type><parameter>arguments</parameter></methodparam>
89  *     <methodparam><modifier>in</modifier><type>a{sv}</type><parameter>data</parameter></methodparam>
90  *   </methodsynopsis>
91  *   <methodsynopsis>
92  *     <void/>
93  *     <methodname>InvokeAction</methodname>
94  *     <methodparam><modifier>in</modifier><type>s</type><parameter>action</parameter></methodparam>
95  *     <methodparam><modifier>in</modifier><type>u</type><parameter>timestamp</parameter></methodparam>
96  *   </methodsynopsis>
97  *   <methodsynopsis>
98  *     <type>a{s(sb)}</type>
99  *     <methodname>ListActions</methodname>
100  *     <void/>
101  *   </methodsynopsis>
102  *   <methodsynopsis>
103  *     <void/>
104  *     <methodname>Quit</methodname>
105  *     <methodparam><modifier>in</modifier><type>u</type><parameter>timestamp</parameter></methodparam>
106  *   </methodsynopsis>
107  *   <methodsynopsis>
108  *     <modifier>Signal</modifier>
109  *     <void/>
110  *     <methodname>ActionsChanged</methodname>
111  *     <void/>
112  *   </methodsynopsis>
113  * </classsynopsis>
114  * <para>
115  * The <methodname>Activate</methodname> function is called on the existing
116  * application instance when a second instance fails to take the bus name.
117  * @arguments contains the commandline arguments given to the second instance
118  * and @data contains platform-specific additional data, see
119  * g_application_format_activation_data().
120  * </para>
121  * <para>
122  * The <methodname>InvokeAction</methodname> function can be called to invoke
123  * one of the actions exported by the application. The @timestamp parameter
124  * should be taken from the user event that triggered the method call (e.g.
125  * a button press event).
126  * </para>
127  * <para>
128  * The <methodname>ListActions</methodname> function returns a dictionary
129  * with the exported actions of the application. The keys of the dictionary
130  * are the action names, and the values are structs containing the description
131  * for the action and a boolean that represents if the action is enabled or not.
132  * </para>
133  * <para>
134  * The <methodname>Quit</methodname> function can be called to terminate
135  * the application. The @timestamp parameter should be taken from the user
136  * event that triggered the method call (e.g. a button press event).
137  * </para>
138  * <para>
139  * The <methodname>ActionsChanged</methodname> signal is emitted when the
140  * exported actions change (i.e. an action is added, removed, enabled,
141  * disabled, or otherwise changed).
142  * </para>
143  * </refsect2>
144  *
145  * Since: 2.24
146  */
147
148 G_DEFINE_TYPE (GApplication, g_application, G_TYPE_OBJECT);
149
150 enum
151 {
152   PROP_0,
153
154   PROP_APPID,
155   PROP_DEFAULT_QUIT,
156   PROP_IS_REMOTE
157 };
158
159 enum
160 {
161   QUIT,
162   ACTION,
163   PREPARE_ACTIVATION,
164
165   LAST_SIGNAL
166 };
167
168 static guint application_signals[LAST_SIGNAL] = { 0 };
169
170 typedef struct {
171   char *name;
172   char *description;
173   guint enabled : 1;
174 } GApplicationAction;
175
176 struct _GApplicationPrivate
177 {
178   char *appid;
179   GHashTable *actions; /* name -> GApplicationAction */
180   GMainLoop *mainloop;
181
182   guint default_quit : 1;
183   guint is_remote    : 1;
184
185   guint actions_changed_id;
186
187 #ifdef G_OS_UNIX
188   char *dbus_path;
189   GDBusConnection *session_bus;
190 #endif
191 };
192
193 static GApplication *primary_application = NULL;
194 static GHashTable *instances_for_appid = NULL;
195
196 static void     _g_application_platform_init                    (GApplication  *app); 
197 static gboolean _g_application_platform_acquire_single_instance (GApplication  *app,
198                                                                  GError       **error);
199 static void     _g_application_platform_remote_invoke_action    (GApplication  *app,
200                                                                  const char    *action,
201                                                                  guint          timestamp);
202 static void     _g_application_platform_remote_quit             (GApplication  *app,
203                                                                  guint          timestamp);
204 static void     _g_application_platform_activate                (GApplication  *app,
205                                                                  GVariant      *data) G_GNUC_NORETURN;
206 static void     _g_application_platform_on_actions_changed      (GApplication  *app);
207
208 #ifdef G_OS_UNIX
209 #include "gdbusapplication.c"
210 #else
211 #include "gnullapplication.c"
212 #endif
213
214 static gboolean
215 _g_application_validate_id (const char *id)
216 {
217   gboolean allow_dot;
218
219   if (strlen (id) > 255)
220     return FALSE;
221
222   if (!g_ascii_isalpha (*id))
223     return FALSE;
224
225   id++;
226   allow_dot = FALSE;
227   for (; *id; id++)
228     {
229       if (g_ascii_isalnum (*id) || (*id == '-') || (*id == '_'))
230         allow_dot = TRUE;
231       else if (allow_dot && *id == '.')
232         allow_dot = FALSE;
233       else
234         return FALSE;
235     }
236   return TRUE;
237 }
238
239 static gpointer
240 init_appid_statics (gpointer data)
241 {
242   instances_for_appid = g_hash_table_new (g_str_hash, g_str_equal);
243   return NULL;
244 }
245
246 static GApplication *
247 application_for_appid (const char *appid)
248 {
249   static GOnce appid_once = G_ONCE_INIT;
250
251   g_once (&appid_once, init_appid_statics, NULL);
252
253   return g_hash_table_lookup (instances_for_appid, appid);
254 }
255
256 static gboolean
257 g_application_default_quit (GApplication *application,
258                             guint         timestamp)
259 {
260   g_return_val_if_fail (application->priv->mainloop != NULL, FALSE);
261   g_main_loop_quit (application->priv->mainloop);
262
263   return TRUE;
264 }
265
266 static void
267 g_application_default_run (GApplication *application)
268 {
269   if (application->priv->mainloop == NULL)
270     application->priv->mainloop = g_main_loop_new (NULL, TRUE);
271
272   g_main_loop_run (application->priv->mainloop);
273 }
274
275 static void
276 _g_application_handle_activation (GApplication  *app,
277                                   int            argc,
278                                   char         **argv,
279                                   GVariant      *platform_data)
280 {
281   GVariantBuilder builder;
282   GVariant *message;
283   int i;
284
285   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(aaya{sv})"));
286   g_variant_builder_open (&builder, G_VARIANT_TYPE ("aay"));
287
288   for (i = 1; i < argc; i++)
289     {
290       int j;
291       guint8 *argv_bytes;
292
293       g_variant_builder_open (&builder, G_VARIANT_TYPE ("ay"));
294
295       argv_bytes = (guint8*) argv[i];
296       for (j = 0; argv_bytes[j]; j++)
297         g_variant_builder_add_value (&builder,
298                                      g_variant_new_byte (argv_bytes[j]));
299       g_variant_builder_close (&builder);
300     }
301   g_variant_builder_close (&builder);
302
303   if (platform_data)
304     g_variant_builder_add (&builder, "@a{sv}", platform_data);
305   else
306     {
307       g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
308       g_variant_builder_close (&builder);
309     }
310
311   message = g_variant_builder_end (&builder);
312   _g_application_platform_activate (app, message);
313   g_variant_unref (message);
314 }
315
316 static gboolean
317 timeout_handle_actions_changed (gpointer user_data)
318 {
319   GApplication *application = user_data;
320
321   application->priv->actions_changed_id = 0;
322
323   _g_application_platform_on_actions_changed (application);
324
325   return FALSE;
326 }
327
328 static inline void
329 queue_actions_change_notification (GApplication *application)
330 {
331   GApplicationPrivate *priv = application->priv;
332
333   if (priv->actions_changed_id == 0)
334     priv->actions_changed_id = g_timeout_add (0, timeout_handle_actions_changed, application);
335 }
336
337 static void
338 g_application_action_free (gpointer data)
339 {
340   if (G_LIKELY (data != NULL))
341     {
342       GApplicationAction *action = data;
343
344       g_free (action->name);
345       g_free (action->description);
346
347       g_slice_free (GApplicationAction, action);
348     }
349 }
350
351
352 /**
353  * g_application_new:
354  * @appid: System-dependent application identifier
355  *
356  * Create a new #GApplication.  The application is initially in
357  * "remote" mode.  Almost certainly, you want to call
358  * g_application_register() immediately after this function, which
359  * will finish initialization.
360  *
361  * As a convenience, this function is defined to call g_type_init() as
362  * its very first action.
363  *
364  * Returns: (transfer full): An application instance
365  *
366  * Since: 2.26
367  */
368 GApplication *
369 g_application_new (const char  *appid)
370 {
371   g_type_init ();
372
373   return G_APPLICATION (g_object_new (G_TYPE_APPLICATION, "appid", appid, NULL));
374 }
375
376 /**
377  * g_application_register:
378  * @application: A #GApplication
379  * @argc: System argument count
380  * @argv: (array length=argc): System argument vector
381  * @platform_data: (allow-none): Arbitrary platform-specific data, must have signature "a{sv}"
382  *
383  * Ensure the current process is the unique owner of the application.
384  * If successful, the #GApplication:is-remote property will be changed
385  * to %FALSE, and it is safe to continue creating other resources
386  * such as graphics windows.
387  *
388  * If the given @appid is already running in another process, the
389  * #GApplication:default-exit property will be evaluated.  If it's
390  * %TRUE, then a platform-specific action such as bringing any
391  * graphics windows associated with the application to the foreground
392  * may be initiated.  After that, the current process will terminate.
393  * If %FALSE, then the application remains in the #GApplication:is-remote
394  * state, and you can e.g. call g_application_invoke_action().
395  */
396 void
397 g_application_register_with_data (GApplication  *application,
398                                   int            argc,
399                                   char         **argv,
400                                   GVariant      *platform_data)
401 {
402   g_return_if_fail (application->priv->appid != NULL);
403   g_return_if_fail (application->priv->is_remote);
404   g_return_if_fail (platform_data == NULL
405                     || strcmp (g_variant_get_type_string (platform_data), "a{sv}") == 0);
406
407   if (!_g_application_platform_acquire_single_instance (application, NULL))
408     {
409       if (application->priv->default_quit)
410         _g_application_handle_activation (application, argc, argv, platform_data);
411       else
412         return;
413     }
414
415   application->priv->is_remote = FALSE;
416
417   _g_application_platform_init (application);
418 }
419
420 /**
421  * g_application_new_and_register:
422  * @appid: An application identifier
423  * @argc: System argument count
424  * @argv: (array length=argc): System argument vector
425  *
426  * This is a convenience function which combines g_application_new()
427  * with g_application_register_with_data().
428  */
429 GApplication *
430 g_application_new_and_register (const char  *appid,
431                                 int          argc,
432                                 char       **argv)
433 {
434   GApplication *app = g_application_new (appid);
435   g_application_register_with_data (app, argc, argv, NULL);
436   return app;
437 }
438
439 /**
440  * g_application_add_action:
441  * @application: a #GApplication
442  * @name: the action name
443  * @description: the action description; can be a translatable
444  *   string
445  *
446  * Adds an action @name to the list of exported actions of @application.
447  *
448  * It is an error to call this function if @application is a proxy for
449  * a remote application.
450  *
451  * You can invoke an action using g_application_invoke_action().
452  *
453  * The newly added action is enabled by default; you can call
454  * g_application_set_action_enabled() to disable it.
455  *
456  * Since: 2.26
457  */
458 void
459 g_application_add_action (GApplication *application,
460                           const gchar  *name,
461                           const gchar  *description)
462 {
463   GApplicationPrivate *priv;
464   GApplicationAction *action;
465
466   g_return_if_fail (G_IS_APPLICATION (application));
467   g_return_if_fail (name != NULL && *name != '\0');
468   g_return_if_fail (!application->priv->is_remote);
469
470   priv = application->priv;
471
472   g_return_if_fail (g_hash_table_lookup (priv->actions, name) == NULL);
473
474   action = g_slice_new (GApplicationAction);
475   action->name = g_strdup (name);
476   action->description = g_strdup (description);
477   action->enabled = TRUE;
478
479   g_hash_table_insert (priv->actions, action->name, action);
480   queue_actions_change_notification (application);
481 }
482
483 /**
484  * g_application_remove_action:
485  * @application: a #GApplication
486  * @name: the name of the action to remove
487  *
488  * Removes the action @name from the list of exported actions of @application.
489  *
490  * It is an error to call this function if @application is a proxy for
491  * a remote application.
492  *
493  * Since: 2.26
494  */
495 void
496 g_application_remove_action (GApplication *application,
497                              const gchar  *name)
498 {
499   GApplicationPrivate *priv;
500
501   g_return_if_fail (G_IS_APPLICATION (application));
502   g_return_if_fail (name != NULL && *name != '\0');
503   g_return_if_fail (!application->priv->is_remote);
504
505   priv = application->priv;
506
507   g_return_if_fail (g_hash_table_lookup (priv->actions, name) != NULL);
508
509   g_hash_table_remove (priv->actions, name);
510   queue_actions_change_notification (application);
511 }
512
513 /**
514  * g_application_invoke_action:
515  * @application: a #GApplication
516  * @name: the name of the action to invoke
517  * @timestamp: the timestamp that is going to be passed to
518  *   the #GApplication::action signal
519  *
520  * Invokes the action @name of the passed #GApplication.
521  *
522  * This function has different behavior depending on whether @application
523  * is acting as a proxy for another process.  In the normal case where
524  * the current process is hosting the application, and the specified
525  * action exists and is enabled, the #GApplication::action signal will
526  * be emitted.
527  *
528  * If @application is a proxy, then the specified action will be invoked
529  * in the remote process. It is not necessary to call
530  * g_application_add_action() in the current process in order to invoke
531  * one remotely.
532  *
533  * Since: 2.26
534  */
535 void
536 g_application_invoke_action (GApplication *application,
537                              const char   *name,
538                              guint         timestamp)
539 {
540   GApplicationPrivate *priv;
541   GApplicationAction *action;
542
543   g_return_if_fail (G_IS_APPLICATION (application));
544   g_return_if_fail (name != NULL);
545
546   priv = application->priv;
547
548   if (priv->is_remote)
549     {
550       _g_application_platform_remote_invoke_action (application, name, timestamp);
551       return;
552     }
553
554   action = g_hash_table_lookup (priv->actions, name);
555   g_return_if_fail (action != NULL);
556   if (!action->enabled)
557     return;
558
559   g_signal_emit (application, application_signals[ACTION],
560                  g_quark_from_string (name),
561                  name,
562                  timestamp);
563 }
564
565 /**
566  * g_application_list_actions:
567  * @application: a #GApplication
568  *
569  * Retrieves the list of action names currently exported by @application.
570  *
571  * It is an error to call this function if @application is a proxy for
572  * a remote application.
573  *
574  * Return value: (transfer full): a newly allocation, %NULL-terminated array
575  *   of strings containing action names; use g_strfreev() to free the
576  *   resources used by the returned array
577  *
578  * Since: 2.26
579  */
580 gchar **
581 g_application_list_actions (GApplication *application)
582 {
583   GApplicationPrivate *priv;
584   GHashTableIter iter;
585   gpointer key;
586   gchar **retval;
587   gint i;
588
589   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
590   g_return_val_if_fail (!application->priv->is_remote, NULL);
591
592   priv = application->priv;
593
594   retval = g_new (gchar*, g_hash_table_size (priv->actions));
595
596   i = 0;
597   g_hash_table_iter_init (&iter, priv->actions);
598   while (g_hash_table_iter_next (&iter, &key, NULL))
599     retval[i++] = g_strdup (key);
600
601   retval[i] = NULL;
602
603   return retval;
604 }
605
606 /**
607  * g_application_set_action_enabled:
608  * @application: a #GApplication
609  * @name: the name of the application
610  * @enabled: whether to enable or disable the action @name
611  *
612  * Sets whether the action @name inside @application should be enabled
613  * or disabled.
614  *
615  * It is an error to call this function if @application is a proxy for
616  * a remote application.
617  *
618  * Invoking a disabled action will not result in the #GApplication::action
619  * signal being emitted.
620  *
621  * Since: 2.26
622  */
623 void
624 g_application_set_action_enabled (GApplication *application,
625                                   const gchar  *name,
626                                   gboolean      enabled)
627 {
628   GApplicationAction *action;
629
630   g_return_if_fail (G_IS_APPLICATION (application));
631   g_return_if_fail (name != NULL);
632   g_return_if_fail (!application->priv->is_remote);
633
634   enabled = !!enabled;
635
636   action = g_hash_table_lookup (application->priv->actions, name);
637   g_return_if_fail (action != NULL);
638   if (action->enabled == enabled)
639     return;
640
641   action->enabled = enabled;
642
643   queue_actions_change_notification (application);
644 }
645
646
647 /**
648  * g_application_get_action_description:
649  * @application: a #GApplication
650  * @name: Action name
651  *
652  * Gets the description of the action @name.
653  *
654  * It is an error to call this function if @application is a proxy for
655  * a remote application.
656  *
657  * Returns: Description for the given action named @name
658  *
659  * Since: 2.26
660  */
661 G_CONST_RETURN gchar *
662 g_application_get_action_description (GApplication *application,
663                                       const gchar  *name)
664 {
665   GApplicationAction *action;
666   
667   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
668   g_return_val_if_fail (name != NULL, NULL);
669   g_return_val_if_fail (!application->priv->is_remote, NULL);
670
671   action = g_hash_table_lookup (application->priv->actions, name);
672   g_return_val_if_fail (action != NULL, NULL);
673
674   return action->description;
675 }
676
677
678 /**
679  * g_application_get_action_enabled:
680  * @application: a #GApplication
681  * @name: the name of the action
682  *
683  * Retrieves whether the action @name is enabled or not.
684  *
685  * See g_application_set_action_enabled().
686  *
687  * It is an error to call this function if @application is a proxy for
688  * a remote application.
689  *
690  * Return value: %TRUE if the action was enabled, and %FALSE otherwise
691  *
692  * Since: 2.26
693  */
694 gboolean
695 g_application_get_action_enabled (GApplication *application,
696                                   const gchar  *name)
697 {
698   GApplicationAction *action;
699
700   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
701   g_return_val_if_fail (name != NULL, FALSE);
702   g_return_val_if_fail (!application->priv->is_remote, FALSE);
703
704   action = g_hash_table_lookup (application->priv->actions, name);
705   g_return_val_if_fail (action != NULL, FALSE);
706
707   return action->enabled;
708 }
709
710 /**
711  * g_application_run:
712  * @application: a #GApplication
713  *
714  * Starts the application.
715  *
716  * The default implementation of this virtual function will simply run
717  * a main loop.
718  *
719  * It is an error to call this function if @application is a proxy for
720  * a remote application.
721  *
722  * Since: 2.26
723  */
724 void
725 g_application_run (GApplication *application)
726 {
727   g_return_if_fail (G_IS_APPLICATION (application));
728   g_return_if_fail (!application->priv->is_remote);
729
730   G_APPLICATION_GET_CLASS (application)->run (application);
731 }
732
733 /**
734  * g_application_quit:
735  * @application: a #GApplication
736  * @timestamp: Platform-specific event timestamp, may be 0 for default
737  *
738  * Request that the application quits.
739  *
740  * This function has different behavior depending on whether @application
741  * is acting as a proxy for another process.  In the normal case where
742  * the current process is hosting the application, the default
743  * implementation will quit the main loop created by g_application_run().
744  *
745  * If @application is a proxy, then the remote process will be asked
746  * to quit.
747  *
748  * Returns: %TRUE if the application accepted the request, %FALSE otherwise
749  *
750  * Since: 2.26
751  */
752 gboolean
753 g_application_quit (GApplication *application,
754                     guint         timestamp)
755 {
756   gboolean retval = FALSE;
757
758   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
759
760   if (application->priv->is_remote)
761     {
762        _g_application_platform_remote_quit (application, timestamp);
763        retval = TRUE;
764     }
765   else
766     g_signal_emit (application, application_signals[QUIT], 0, timestamp, &retval);
767
768   return retval;
769 }
770
771 /**
772  * g_application_get_instance:
773  *
774  * In the normal case where there is exactly one #GApplication instance
775  * in this process, return that instance.  If there are multiple, the
776  * first one created will be returned.  Otherwise, return %NULL.
777  *
778  * Returns: (transfer none): The primary instance of #GApplication,
779  *   or %NULL if none is set
780  *
781  * Since: 2.26
782  */
783 GApplication *
784 g_application_get_instance (void)
785 {
786   return primary_application;
787 }
788
789 /**
790  * g_application_get_id:
791  *
792  * Retrieves the platform-specific identifier for the #GApplication.
793  *
794  * Return value: The platform-specific identifier. The returned string
795  *   is owned by the #GApplication instance and it should never be
796  *   modified or freed
797  *
798  * Since: 2.26
799  */
800 G_CONST_RETURN char *
801 g_application_get_id (GApplication *application)
802 {
803   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
804
805   return application->priv->appid;
806 }
807
808 /**
809  * g_application_is_remote:
810  * @application: a #GApplication
811  *
812  * Returns: %TRUE if this object represents a proxy for a remote application.
813  */
814 gboolean
815 g_application_is_remote (GApplication *application)
816 {
817   g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
818
819   return application->priv->is_remote;
820 }
821
822 static void
823 g_application_init (GApplication *app)
824 {
825   app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app,
826                                            G_TYPE_APPLICATION,
827                                            GApplicationPrivate);
828
829   app->priv->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
830                                               NULL,
831                                               g_application_action_free);
832   app->priv->default_quit = TRUE;
833   app->priv->is_remote = TRUE;
834 }
835
836 static void
837 g_application_get_property (GObject    *object,
838                             guint       prop_id,
839                             GValue     *value,
840                             GParamSpec *pspec)
841 {
842   GApplication *app = G_APPLICATION (object);
843
844   switch (prop_id)
845     {
846     case PROP_APPID:
847       g_value_set_string (value, g_application_get_id (app));
848       break;
849
850     case PROP_DEFAULT_QUIT:
851       g_value_set_boolean (value, app->priv->default_quit);
852       break;
853
854     case PROP_IS_REMOTE:
855       g_value_set_boolean (value, g_application_is_remote (app));
856       break;
857
858     default:
859       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860     }
861 }
862
863 static void
864 g_application_set_property (GObject      *object,
865                             guint         prop_id,
866                             const GValue *value,
867                             GParamSpec   *pspec)
868 {
869   GApplication *app = G_APPLICATION (object);
870
871   switch (prop_id)
872     {
873     case PROP_APPID:
874       g_return_if_fail (_g_application_validate_id (g_value_get_string (value)));
875       app->priv->appid = g_value_dup_string (value);
876       break;
877
878     case PROP_DEFAULT_QUIT:
879       app->priv->default_quit = g_value_get_boolean (value);
880       break;
881
882     default:
883       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
884     }
885 }
886
887 static GObject*
888 g_application_constructor (GType                  type,
889                            guint                  n_construct_properties,
890                            GObjectConstructParam *construct_params)
891 {
892   GApplication *app;
893   GObject *object;
894   const char *appid = NULL;
895   guint i;
896
897   for (i = 0; i < n_construct_properties; i++)
898     {
899       GObjectConstructParam *param = &construct_params[i];
900       if (strcmp (param->pspec->name, "appid") == 0)
901         appid = g_value_get_string (param->value);
902     }
903
904   g_return_val_if_fail (appid != NULL, NULL);
905
906   app = application_for_appid (appid);
907   if (app != NULL)
908     return g_object_ref (app);
909
910   object = (* G_OBJECT_CLASS (g_application_parent_class)->constructor) (type,
911                                                                          n_construct_properties,
912                                                                          construct_params);
913   app = G_APPLICATION (object);
914
915   if (primary_application == NULL)
916     primary_application = app;
917   g_hash_table_insert (instances_for_appid, g_strdup (appid), app);
918
919   return object;
920 }
921
922 static void
923 g_application_finalize (GObject *object)
924 {
925   GApplication *app = G_APPLICATION (object);
926
927   g_free (app->priv->appid);
928   if (app->priv->actions)
929     g_hash_table_unref (app->priv->actions);
930   if (app->priv->actions_changed_id)
931     g_source_remove (app->priv->actions_changed_id);
932   if (app->priv->mainloop)
933     g_main_loop_unref (app->priv->mainloop);
934
935 #ifdef G_OS_UNIX
936   g_free (app->priv->dbus_path);
937   if (app->priv->session_bus)
938     g_object_unref (app->priv->session_bus);
939 #endif
940
941   G_OBJECT_CLASS (g_application_parent_class)->finalize (object);
942 }
943
944 static void
945 g_application_class_init (GApplicationClass *klass)
946 {
947   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
948
949   g_type_class_add_private (klass, sizeof (GApplicationPrivate));
950
951   gobject_class->constructor = g_application_constructor;
952   gobject_class->set_property = g_application_set_property;
953   gobject_class->get_property = g_application_get_property;
954
955   gobject_class->finalize = g_application_finalize;
956
957   klass->run = g_application_default_run;
958   klass->quit = g_application_default_quit;
959
960   application_signals[QUIT] =
961     g_signal_new (g_intern_static_string ("quit"),
962                   G_OBJECT_CLASS_TYPE (klass),
963                   G_SIGNAL_RUN_LAST,
964                   G_STRUCT_OFFSET (GApplicationClass, quit),
965                   g_signal_accumulator_true_handled, NULL,
966                   _gio_marshal_BOOLEAN__UINT,
967                   G_TYPE_BOOLEAN, 1,
968                   G_TYPE_UINT);
969
970   application_signals[ACTION] =
971     g_signal_new (g_intern_static_string ("action"),
972                   G_OBJECT_CLASS_TYPE (klass),
973                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
974                   G_STRUCT_OFFSET (GApplicationClass, action),
975                   NULL, NULL,
976                   _gio_marshal_VOID__STRING_INT,
977                   G_TYPE_NONE, 2,
978                   G_TYPE_STRING,
979                   G_TYPE_INT);
980
981    /**
982    * GApplication::prepare-activation:
983    * @arguments: A #GVariant with the signature "aay"
984    * @platform_data: A #GVariant with the signature "a{sv}"
985    *
986    * This signal is emitted when a non-primary process for a given
987    * application is invoked while your application is running; for
988    * example, when a file browser launches your program to open a
989    * file.  The raw operating system arguments are passed in the
990    * @arguments variant.  Additional platform-dependent data is
991    * stored in @platform_data.
992    */
993
994   application_signals[PREPARE_ACTIVATION] =
995     g_signal_new (g_intern_static_string ("prepare-activation"),
996                   G_OBJECT_CLASS_TYPE (klass),
997                   G_SIGNAL_RUN_LAST,
998                   G_STRUCT_OFFSET (GApplicationClass, prepare_activation),
999                   NULL, NULL,
1000                   _gio_marshal_VOID__BOXED_BOXED,
1001                   G_TYPE_NONE, 2,
1002                   G_TYPE_VARIANT,
1003                   G_TYPE_VARIANT);
1004
1005    /**
1006    * GApplication:appid:
1007    *
1008    * The unique identifier for this application.  See the documentation for
1009    * #GApplication for more information about this property.
1010    *
1011    */
1012   g_object_class_install_property (gobject_class,
1013                                    PROP_APPID,
1014                                    g_param_spec_string ("appid",
1015                                                         P_("Application ID"),
1016                                                         P_("Identifier for this application"),
1017                                                         NULL,
1018                                                         G_PARAM_READWRITE |
1019                                                         G_PARAM_CONSTRUCT_ONLY |
1020                                                         G_PARAM_STATIC_STRINGS));
1021
1022   /**
1023    * GApplication:default-quit:
1024    *
1025    * By default, if a different process is running this application, the
1026    * process will be exited.  Set this property to %FALSE to allow custom
1027    * interaction with the remote process.
1028    *
1029    */
1030   g_object_class_install_property (gobject_class,
1031                                    PROP_DEFAULT_QUIT,
1032                                    g_param_spec_boolean ("default-quit",
1033                                                          P_("Default Quit"),
1034                                                          P_("Exit the process by default"),
1035                                                          TRUE,
1036                                                          G_PARAM_READWRITE |
1037                                                          G_PARAM_CONSTRUCT_ONLY |
1038                                                          G_PARAM_STATIC_STRINGS));
1039
1040
1041   /**
1042    * GApplication:is-remote:
1043    *
1044    * This property is %TRUE if this application instance represents a proxy
1045    * to the instance of this application in another process.
1046    *
1047    */
1048   g_object_class_install_property (gobject_class,
1049                                    PROP_IS_REMOTE,
1050                                    g_param_spec_boolean ("is-remote",
1051                                                          P_("Is Remote"),
1052                                                          P_("Whether this application is a proxy for another process"),
1053                                                          TRUE,
1054                                                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1055 }
1056
1057 #define __G_APPLICATION_C__
1058 #include "gioaliasdef.c"