1 /*
2 * Copyright © 2010 Codethink Limited
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Ryan Lortie <desrt@desrt.ca>
18 */
19
20 /* Prologue {{{1 */
21 #include "config.h"
22
23 #include "gapplication.h"
24
25 #include "gapplicationcommandline.h"
26 #include "gsimpleactiongroup.h"
27 #include "gremoteactiongroup.h"
28 #include "gapplicationimpl.h"
29 #include "gactiongroup.h"
30 #include "gactionmap.h"
31 #include "gsettings.h"
32 #include "gnotification-private.h"
33 #include "gnotificationbackend.h"
34 #include "gdbusutils.h"
35
36 #include "gioenumtypes.h"
37 #include "gioenums.h"
38 #include "gfile.h"
39
40 #include "glibintl.h"
41 #include "gmarshal-internal.h"
42
43 #include <string.h>
44
45 /**
46 * SECTION:gapplication
47 * @title: GApplication
48 * @short_description: Core application class
49 * @include: gio/gio.h
50 *
51 * A #GApplication is the foundation of an application. It wraps some
52 * low-level platform-specific services and is intended to act as the
53 * foundation for higher-level application classes such as
54 * #GtkApplication or #MxApplication. In general, you should not use
55 * this class outside of a higher level framework.
56 *
57 * GApplication provides convenient life cycle management by maintaining
58 * a "use count" for the primary application instance. The use count can
59 * be changed using g_application_hold() and g_application_release(). If
60 * it drops to zero, the application exits. Higher-level classes such as
61 * #GtkApplication employ the use count to ensure that the application
62 * stays alive as long as it has any opened windows.
63 *
64 * Another feature that GApplication (optionally) provides is process
65 * uniqueness. Applications can make use of this functionality by
66 * providing a unique application ID. If given, only one application
67 * with this ID can be running at a time per session. The session
68 * concept is platform-dependent, but corresponds roughly to a graphical
69 * desktop login. When your application is launched again, its
70 * arguments are passed through platform communication to the already
71 * running program. The already running instance of the program is
72 * called the "primary instance"; for non-unique applications this is
73 * the always the current instance. On Linux, the D-Bus session bus
74 * is used for communication.
75 *
76 * The use of #GApplication differs from some other commonly-used
77 * uniqueness libraries (such as libunique) in important ways. The
78 * application is not expected to manually register itself and check
79 * if it is the primary instance. Instead, the main() function of a
80 * #GApplication should do very little more than instantiating the
81 * application instance, possibly connecting signal handlers, then
82 * calling g_application_run(). All checks for uniqueness are done
83 * internally. If the application is the primary instance then the
84 * startup signal is emitted and the mainloop runs. If the application
85 * is not the primary instance then a signal is sent to the primary
86 * instance and g_application_run() promptly returns. See the code
87 * examples below.
88 *
89 * If used, the expected form of an application identifier is the same as
90 * that of of a
91 * [D-Bus well-known bus name](https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus).
92 * Examples include: `com.example.MyApp`, `org.example.internal_apps.Calculator`,
93 * `org._7_zip.Archiver`.
94 * For details on valid application identifiers, see g_application_id_is_valid().
95 *
96 * On Linux, the application identifier is claimed as a well-known bus name
97 * on the user's session bus. This means that the uniqueness of your
98 * application is scoped to the current session. It also means that your
99 * application may provide additional services (through registration of other
100 * object paths) at that bus name. The registration of these object paths
101 * should be done with the shared GDBus session bus. Note that due to the
102 * internal architecture of GDBus, method calls can be dispatched at any time
103 * (even if a main loop is not running). For this reason, you must ensure that
104 * any object paths that you wish to register are registered before #GApplication
105 * attempts to acquire the bus name of your application (which happens in
106 * g_application_register()). Unfortunately, this means that you cannot use
107 * g_application_get_is_remote() to decide if you want to register object paths.
108 *
109 * GApplication also implements the #GActionGroup and #GActionMap
110 * interfaces and lets you easily export actions by adding them with
111 * g_action_map_add_action(). When invoking an action by calling
112 * g_action_group_activate_action() on the application, it is always
113 * invoked in the primary instance. The actions are also exported on
114 * the session bus, and GIO provides the #GDBusActionGroup wrapper to
115 * conveniently access them remotely. GIO provides a #GDBusMenuModel wrapper
116 * for remote access to exported #GMenuModels.
117 *
118 * There is a number of different entry points into a GApplication:
119 *
120 * - via 'Activate' (i.e. just starting the application)
121 *
122 * - via 'Open' (i.e. opening some files)
123 *
124 * - by handling a command-line
125 *
126 * - via activating an action
127 *
128 * The #GApplication::startup signal lets you handle the application
129 * initialization for all of these in a single place.
130 *
131 * Regardless of which of these entry points is used to start the
132 * application, GApplication passes some "platform data from the
133 * launching instance to the primary instance, in the form of a
134 * #GVariant dictionary mapping strings to variants. To use platform
135 * data, override the @before_emit or @after_emit virtual functions
136 * in your #GApplication subclass. When dealing with
137 * #GApplicationCommandLine objects, the platform data is
138 * directly available via g_application_command_line_get_cwd(),
139 * g_application_command_line_get_environ() and
140 * g_application_command_line_get_platform_data().
141 *
142 * As the name indicates, the platform data may vary depending on the
143 * operating system, but it always includes the current directory (key
144 * "cwd"), and optionally the environment (ie the set of environment
145 * variables and their values) of the calling process (key "environ").
146 * The environment is only added to the platform data if the
147 * %G_APPLICATION_SEND_ENVIRONMENT flag is set. #GApplication subclasses
148 * can add their own platform data by overriding the @add_platform_data
149 * virtual function. For instance, #GtkApplication adds startup notification
150 * data in this way.
151 *
152 * To parse commandline arguments you may handle the
153 * #GApplication::command-line signal or override the local_command_line()
154 * vfunc, to parse them in either the primary instance or the local instance,
155 * respectively.
156 *
157 * For an example of opening files with a GApplication, see
158 * [gapplication-example-open.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-open.c).
159 *
160 * For an example of using actions with GApplication, see
161 * [gapplication-example-actions.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-actions.c).
162 *
163 * For an example of using extra D-Bus hooks with GApplication, see
164 * [gapplication-example-dbushooks.c](https://git.gnome.org/browse/glib/tree/gio/tests/gapplication-example-dbushooks.c).
165 */
166
167 /**
168 * GApplication:
169 *
170 * #GApplication is an opaque data structure and can only be accessed
171 * using the following functions.
172 * Since: 2.28
173 */
174
175 /**
176 * GApplicationClass:
177 * @startup: invoked on the primary instance immediately after registration
178 * @shutdown: invoked only on the registered primary instance immediately
179 * after the main loop terminates
180 * @activate: invoked on the primary instance when an activation occurs
181 * @open: invoked on the primary instance when there are files to open
182 * @command_line: invoked on the primary instance when a command-line is
183 * not handled locally
184 * @local_command_line: invoked (locally). The virtual function has the chance
185 * to inspect (and possibly replace) command line arguments. See
186 * g_application_run() for more information. Also see the
187 * #GApplication::handle-local-options signal, which is a simpler
188 * alternative to handling some commandline options locally
189 * @before_emit: invoked on the primary instance before 'activate', 'open',
190 * 'command-line' or any action invocation, gets the 'platform data' from
191 * the calling instance
192 * @after_emit: invoked on the primary instance after 'activate', 'open',
193 * 'command-line' or any action invocation, gets the 'platform data' from
194 * the calling instance
195 * @add_platform_data: invoked (locally) to add 'platform data' to be sent to
196 * the primary instance when activating, opening or invoking actions
197 * @quit_mainloop: Used to be invoked on the primary instance when the use
198 * count of the application drops to zero (and after any inactivity
199 * timeout, if requested). Not used anymore since 2.32
200 * @run_mainloop: Used to be invoked on the primary instance from
201 * g_application_run() if the use-count is non-zero. Since 2.32,
202 * GApplication is iterating the main context directly and is not
203 * using @run_mainloop anymore
204 * @dbus_register: invoked locally during registration, if the application is
205 * using its D-Bus backend. You can use this to export extra objects on the
206 * bus, that need to exist before the application tries to own the bus name.
207 * The function is passed the #GDBusConnection to to session bus, and the
208 * object path that #GApplication will use to export is D-Bus API.
209 * If this function returns %TRUE, registration will proceed; otherwise
210 * registration will abort. Since: 2.34
211 * @dbus_unregister: invoked locally during unregistration, if the application
212 * is using its D-Bus backend. Use this to undo anything done by
213 * the @dbus_register vfunc. Since: 2.34
214 * @handle_local_options: invoked locally after the parsing of the commandline
215 * options has occurred. Since: 2.40
216 * @name_lost: invoked when another instance is taking over the name. Since: 2.60
217 *
218 * Virtual function table for #GApplication.
219 *
220 * Since: 2.28
221 */
222
223 struct _GApplicationPrivate
224 {
225 GApplicationFlags flags;
226 gchar *id;
227 gchar *resource_path;
228
229 GActionGroup *actions;
230
231 guint inactivity_timeout_id;
232 guint inactivity_timeout;
233 guint use_count;
234 guint busy_count;
235
236 guint is_registered : 1;
237 guint is_remote : 1;
238 guint did_startup : 1;
239 guint did_shutdown : 1;
240 guint must_quit_now : 1;
241
242 GRemoteActionGroup *remote_actions;
243 GApplicationImpl *impl;
244
245 GNotificationBackend *notifications;
246
247 /* GOptionContext support */
248 GOptionGroup *main_options;
249 GSList *option_groups;
250 GHashTable *packed_options;
251 gboolean options_parsed;
252 gchar *parameter_string;
253 gchar *summary;
254 gchar *description;
255
256 /* Allocated option strings, from g_application_add_main_option() */
257 GSList *option_strings;
258 };
259
260 enum
261 {
262 PROP_NONE,
263 PROP_APPLICATION_ID,
264 PROP_FLAGS,
265 PROP_RESOURCE_BASE_PATH,
266 PROP_IS_REGISTERED,
267 PROP_IS_REMOTE,
268 PROP_INACTIVITY_TIMEOUT,
269 PROP_ACTION_GROUP,
270 PROP_IS_BUSY
271 };
272
273 enum
274 {
275 SIGNAL_STARTUP,
276 SIGNAL_SHUTDOWN,
277 SIGNAL_ACTIVATE,
278 SIGNAL_OPEN,
279 SIGNAL_ACTION,
280 SIGNAL_COMMAND_LINE,
281 SIGNAL_HANDLE_LOCAL_OPTIONS,
282 SIGNAL_NAME_LOST,
283 NR_SIGNALS
284 };
285
286 static guint g_application_signals[NR_SIGNALS];
287
288 static void g_application_action_group_iface_init (GActionGroupInterface *);
289 static void g_application_action_map_iface_init (GActionMapInterface *);
290 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
291 G_ADD_PRIVATE (GApplication)
292 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
293 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
294
295 /* GApplicationExportedActions {{{1 */
296
297 /* We create a subclass of GSimpleActionGroup that implements
298 * GRemoteActionGroup and deals with the platform data using
299 * GApplication's before/after_emit vfuncs. This is the action group we
300 * will be exporting.
301 *
302 * We could implement GRemoteActionGroup on GApplication directly, but
303 * this would be potentially extremely confusing to have exposed as part
304 * of the public API of GApplication. We certainly don't want anyone in
305 * the same process to be calling these APIs...
306 */
307 typedef GSimpleActionGroupClass GApplicationExportedActionsClass;
308 typedef struct
309 {
310 GSimpleActionGroup parent_instance;
311 GApplication *application;
312 } GApplicationExportedActions;
313
314 static GType g_application_exported_actions_get_type (void);
315 static void g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface);
G_DEFINE_TYPE_WITH_CODE(GApplicationExportedActions,g_application_exported_actions,G_TYPE_SIMPLE_ACTION_GROUP,G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP,g_application_exported_actions_iface_init))316 G_DEFINE_TYPE_WITH_CODE (GApplicationExportedActions, g_application_exported_actions, G_TYPE_SIMPLE_ACTION_GROUP,
317 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_application_exported_actions_iface_init))
318
319 static void
320 g_application_exported_actions_activate_action_full (GRemoteActionGroup *remote,
321 const gchar *action_name,
322 GVariant *parameter,
323 GVariant *platform_data)
324 {
325 GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
326
327 G_APPLICATION_GET_CLASS (exported->application)
328 ->before_emit (exported->application, platform_data);
329
330 g_action_group_activate_action (G_ACTION_GROUP (exported), action_name, parameter);
331
332 G_APPLICATION_GET_CLASS (exported->application)
333 ->after_emit (exported->application, platform_data);
334 }
335
336 static void
g_application_exported_actions_change_action_state_full(GRemoteActionGroup * remote,const gchar * action_name,GVariant * value,GVariant * platform_data)337 g_application_exported_actions_change_action_state_full (GRemoteActionGroup *remote,
338 const gchar *action_name,
339 GVariant *value,
340 GVariant *platform_data)
341 {
342 GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
343
344 G_APPLICATION_GET_CLASS (exported->application)
345 ->before_emit (exported->application, platform_data);
346
347 g_action_group_change_action_state (G_ACTION_GROUP (exported), action_name, value);
348
349 G_APPLICATION_GET_CLASS (exported->application)
350 ->after_emit (exported->application, platform_data);
351 }
352
353 static void
g_application_exported_actions_init(GApplicationExportedActions * actions)354 g_application_exported_actions_init (GApplicationExportedActions *actions)
355 {
356 }
357
358 static void
g_application_exported_actions_iface_init(GRemoteActionGroupInterface * iface)359 g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface)
360 {
361 iface->activate_action_full = g_application_exported_actions_activate_action_full;
362 iface->change_action_state_full = g_application_exported_actions_change_action_state_full;
363 }
364
365 static void
g_application_exported_actions_class_init(GApplicationExportedActionsClass * class)366 g_application_exported_actions_class_init (GApplicationExportedActionsClass *class)
367 {
368 }
369
370 static GActionGroup *
g_application_exported_actions_new(GApplication * application)371 g_application_exported_actions_new (GApplication *application)
372 {
373 GApplicationExportedActions *actions;
374
375 actions = g_object_new (g_application_exported_actions_get_type (), NULL);
376 actions->application = application;
377
378 return G_ACTION_GROUP (actions);
379 }
380
381 /* Command line option handling {{{1 */
382
383 static void
free_option_entry(gpointer data)384 free_option_entry (gpointer data)
385 {
386 GOptionEntry *entry = data;
387
388 switch (entry->arg)
389 {
390 case G_OPTION_ARG_STRING:
391 case G_OPTION_ARG_FILENAME:
392 g_free (*(gchar **) entry->arg_data);
393 break;
394
395 case G_OPTION_ARG_STRING_ARRAY:
396 case G_OPTION_ARG_FILENAME_ARRAY:
397 g_strfreev (*(gchar ***) entry->arg_data);
398 break;
399
400 default:
401 /* most things require no free... */
402 break;
403 }
404
405 /* ...except for the space that we allocated for it ourselves */
406 g_free (entry->arg_data);
407
408 g_slice_free (GOptionEntry, entry);
409 }
410
411 static void
g_application_pack_option_entries(GApplication * application,GVariantDict * dict)412 g_application_pack_option_entries (GApplication *application,
413 GVariantDict *dict)
414 {
415 GHashTableIter iter;
416 gpointer item;
417
418 g_hash_table_iter_init (&iter, application->priv->packed_options);
419 while (g_hash_table_iter_next (&iter, NULL, &item))
420 {
421 GOptionEntry *entry = item;
422 GVariant *value = NULL;
423
424 switch (entry->arg)
425 {
426 case G_OPTION_ARG_NONE:
427 if (*(gboolean *) entry->arg_data != 2)
428 value = g_variant_new_boolean (*(gboolean *) entry->arg_data);
429 break;
430
431 case G_OPTION_ARG_STRING:
432 if (*(gchar **) entry->arg_data)
433 value = g_variant_new_string (*(gchar **) entry->arg_data);
434 break;
435
436 case G_OPTION_ARG_INT:
437 if (*(gint32 *) entry->arg_data)
438 value = g_variant_new_int32 (*(gint32 *) entry->arg_data);
439 break;
440
441 case G_OPTION_ARG_FILENAME:
442 if (*(gchar **) entry->arg_data)
443 value = g_variant_new_bytestring (*(gchar **) entry->arg_data);
444 break;
445
446 case G_OPTION_ARG_STRING_ARRAY:
447 if (*(gchar ***) entry->arg_data)
448 value = g_variant_new_strv (*(const gchar ***) entry->arg_data, -1);
449 break;
450
451 case G_OPTION_ARG_FILENAME_ARRAY:
452 if (*(gchar ***) entry->arg_data)
453 value = g_variant_new_bytestring_array (*(const gchar ***) entry->arg_data, -1);
454 break;
455
456 case G_OPTION_ARG_DOUBLE:
457 if (*(gdouble *) entry->arg_data)
458 value = g_variant_new_double (*(gdouble *) entry->arg_data);
459 break;
460
461 case G_OPTION_ARG_INT64:
462 if (*(gint64 *) entry->arg_data)
463 value = g_variant_new_int64 (*(gint64 *) entry->arg_data);
464 break;
465
466 default:
467 g_assert_not_reached ();
468 }
469
470 if (value)
471 g_variant_dict_insert_value (dict, entry->long_name, value);
472 }
473 }
474
475 static GVariantDict *
g_application_parse_command_line(GApplication * application,gchar *** arguments,GError ** error)476 g_application_parse_command_line (GApplication *application,
477 gchar ***arguments,
478 GError **error)
479 {
480 gboolean become_service = FALSE;
481 gchar *app_id = NULL;
482 gboolean replace = FALSE;
483 GVariantDict *dict = NULL;
484 GOptionContext *context;
485 GOptionGroup *gapplication_group;
486
487 /* Due to the memory management of GOptionGroup we can only parse
488 * options once. That's because once you add a group to the
489 * GOptionContext there is no way to get it back again. This is fine:
490 * local_command_line() should never get invoked more than once
491 * anyway. Add a sanity check just to be sure.
492 */
493 g_return_val_if_fail (!application->priv->options_parsed, NULL);
494
495 context = g_option_context_new (application->priv->parameter_string);
496 g_option_context_set_summary (context, application->priv->summary);
497 g_option_context_set_description (context, application->priv->description);
498
499 gapplication_group = g_option_group_new ("gapplication",
500 _("GApplication options"), _("Show GApplication options"),
501 NULL, NULL);
502 g_option_group_set_translation_domain (gapplication_group, GETTEXT_PACKAGE);
503 g_option_context_add_group (context, gapplication_group);
504
505 /* If the application has not registered local options and it has
506 * G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
507 * their primary instance commandline handler may want to deal with
508 * the arguments. We must therefore ignore them.
509 *
510 * We must also ignore --help in this case since some applications
511 * will try to handle this from the remote side. See #737869.
512 */
513 if (application->priv->main_options == NULL && (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE))
514 {
515 g_option_context_set_ignore_unknown_options (context, TRUE);
516 g_option_context_set_help_enabled (context, FALSE);
517 }
518
519 /* Add the main option group, if it exists */
520 if (application->priv->main_options)
521 {
522 /* This consumes the main_options */
523 g_option_context_set_main_group (context, application->priv->main_options);
524 application->priv->main_options = NULL;
525 }
526
527 /* Add any other option groups if they exist. Adding them to the
528 * context will consume them, so we free the list as we go...
529 */
530 while (application->priv->option_groups)
531 {
532 g_option_context_add_group (context, application->priv->option_groups->data);
533 application->priv->option_groups = g_slist_delete_link (application->priv->option_groups,
534 application->priv->option_groups);
535 }
536
537 /* In the case that we are not explicitly marked as a service or a
538 * launcher then we want to add the "--gapplication-service" option to
539 * allow the process to be made into a service.
540 */
541 if ((application->priv->flags & (G_APPLICATION_IS_SERVICE | G_APPLICATION_IS_LAUNCHER)) == 0)
542 {
543 GOptionEntry entries[] = {
544 { "gapplication-service", '\0', 0, G_OPTION_ARG_NONE, &become_service,
545 N_("Enter GApplication service mode (use from D-Bus service files)") },
546 { NULL }
547 };
548
549 g_option_group_add_entries (gapplication_group, entries);
550 }
551
552 /* Allow overriding the ID if the application allows it */
553 if (application->priv->flags & G_APPLICATION_CAN_OVERRIDE_APP_ID)
554 {
555 GOptionEntry entries[] = {
556 { "gapplication-app-id", '\0', 0, G_OPTION_ARG_STRING, &app_id,
557 N_("Override the application’s ID") },
558 { NULL }
559 };
560
561 g_option_group_add_entries (gapplication_group, entries);
562 }
563
564 /* Allow replacing if the application allows it */
565 if (application->priv->flags & G_APPLICATION_ALLOW_REPLACEMENT)
566 {
567 GOptionEntry entries[] = {
568 { "gapplication-replace", '\0', 0, G_OPTION_ARG_NONE, &replace,
569 N_("Replace the running instance") },
570 { NULL }
571 };
572
573 g_option_group_add_entries (gapplication_group, entries);
574 }
575
576 /* Now we parse... */
577 if (!g_option_context_parse_strv (context, arguments, error))
578 goto out;
579
580 /* Check for --gapplication-service */
581 if (become_service)
582 application->priv->flags |= G_APPLICATION_IS_SERVICE;
583
584 /* Check for --gapplication-app-id */
585 if (app_id)
586 g_application_set_application_id (application, app_id);
587
588 /* Check for --gapplication-replace */
589 if (replace)
590 application->priv->flags |= G_APPLICATION_REPLACE;
591
592 dict = g_variant_dict_new (NULL);
593 if (application->priv->packed_options)
594 {
595 g_application_pack_option_entries (application, dict);
596 g_hash_table_unref (application->priv->packed_options);
597 application->priv->packed_options = NULL;
598 }
599
600 out:
601 /* Make sure we don't run again */
602 application->priv->options_parsed = TRUE;
603
604 g_option_context_free (context);
605 g_free (app_id);
606
607 return dict;
608 }
609
610 static void
add_packed_option(GApplication * application,GOptionEntry * entry)611 add_packed_option (GApplication *application,
612 GOptionEntry *entry)
613 {
614 switch (entry->arg)
615 {
616 case G_OPTION_ARG_NONE:
617 entry->arg_data = g_new (gboolean, 1);
618 *(gboolean *) entry->arg_data = 2;
619 break;
620
621 case G_OPTION_ARG_INT:
622 entry->arg_data = g_new0 (gint, 1);
623 break;
624
625 case G_OPTION_ARG_STRING:
626 case G_OPTION_ARG_FILENAME:
627 case G_OPTION_ARG_STRING_ARRAY:
628 case G_OPTION_ARG_FILENAME_ARRAY:
629 entry->arg_data = g_new0 (gpointer, 1);
630 break;
631
632 case G_OPTION_ARG_INT64:
633 entry->arg_data = g_new0 (gint64, 1);
634 break;
635
636 case G_OPTION_ARG_DOUBLE:
637 entry->arg_data = g_new0 (gdouble, 1);
638 break;
639
640 default:
641 g_return_if_reached ();
642 }
643
644 if (!application->priv->packed_options)
645 application->priv->packed_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_option_entry);
646
647 g_hash_table_insert (application->priv->packed_options,
648 g_strdup (entry->long_name),
649 g_slice_dup (GOptionEntry, entry));
650 }
651
652 /**
653 * g_application_add_main_option_entries:
654 * @application: a #GApplication
655 * @entries: (array zero-terminated=1) (element-type GOptionEntry) a
656 * %NULL-terminated list of #GOptionEntrys
657 *
658 * Adds main option entries to be handled by @application.
659 *
660 * This function is comparable to g_option_context_add_main_entries().
661 *
662 * After the commandline arguments are parsed, the
663 * #GApplication::handle-local-options signal will be emitted. At this
664 * point, the application can inspect the values pointed to by @arg_data
665 * in the given #GOptionEntrys.
666 *
667 * Unlike #GOptionContext, #GApplication supports giving a %NULL
668 * @arg_data for a non-callback #GOptionEntry. This results in the
669 * argument in question being packed into a #GVariantDict which is also
670 * passed to #GApplication::handle-local-options, where it can be
671 * inspected and modified. If %G_APPLICATION_HANDLES_COMMAND_LINE is
672 * set, then the resulting dictionary is sent to the primary instance,
673 * where g_application_command_line_get_options_dict() will return it.
674 * This "packing" is done according to the type of the argument --
675 * booleans for normal flags, strings for strings, bytestrings for
676 * filenames, etc. The packing only occurs if the flag is given (ie: we
677 * do not pack a "false" #GVariant in the case that a flag is missing).
678 *
679 * In general, it is recommended that all commandline arguments are
680 * parsed locally. The options dictionary should then be used to
681 * transmit the result of the parsing to the primary instance, where
682 * g_variant_dict_lookup() can be used. For local options, it is
683 * possible to either use @arg_data in the usual way, or to consult (and
684 * potentially remove) the option from the options dictionary.
685 *
686 * This function is new in GLib 2.40. Before then, the only real choice
687 * was to send all of the commandline arguments (options and all) to the
688 * primary instance for handling. #GApplication ignored them completely
689 * on the local side. Calling this function "opts in" to the new
690 * behaviour, and in particular, means that unrecognised options will be
691 * treated as errors. Unrecognised options have never been ignored when
692 * %G_APPLICATION_HANDLES_COMMAND_LINE is unset.
693 *
694 * If #GApplication::handle-local-options needs to see the list of
695 * filenames, then the use of %G_OPTION_REMAINING is recommended. If
696 * @arg_data is %NULL then %G_OPTION_REMAINING can be used as a key into
697 * the options dictionary. If you do use %G_OPTION_REMAINING then you
698 * need to handle these arguments for yourself because once they are
699 * consumed, they will no longer be visible to the default handling
700 * (which treats them as filenames to be opened).
701 *
702 * It is important to use the proper GVariant format when retrieving
703 * the options with g_variant_dict_lookup():
704 * - for %G_OPTION_ARG_NONE, use b
705 * - for %G_OPTION_ARG_STRING, use &s
706 * - for %G_OPTION_ARG_INT, use i
707 * - for %G_OPTION_ARG_INT64, use x
708 * - for %G_OPTION_ARG_DOUBLE, use d
709 * - for %G_OPTION_ARG_FILENAME, use ^ay
710 * - for %G_OPTION_ARG_STRING_ARRAY, use &as
711 * - for %G_OPTION_ARG_FILENAME_ARRAY, use ^aay
712 *
713 * Since: 2.40
714 */
715 void
g_application_add_main_option_entries(GApplication * application,const GOptionEntry * entries)716 g_application_add_main_option_entries (GApplication *application,
717 const GOptionEntry *entries)
718 {
719 gint i;
720
721 g_return_if_fail (G_IS_APPLICATION (application));
722 g_return_if_fail (entries != NULL);
723
724 if (!application->priv->main_options)
725 {
726 application->priv->main_options = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
727 g_option_group_set_translation_domain (application->priv->main_options, NULL);
728 }
729
730 for (i = 0; entries[i].long_name; i++)
731 {
732 GOptionEntry my_entries[2] = { { NULL }, { NULL } };
733 my_entries[0] = entries[i];
734
735 if (!my_entries[0].arg_data)
736 add_packed_option (application, &my_entries[0]);
737
738 g_option_group_add_entries (application->priv->main_options, my_entries);
739 }
740 }
741
742 /**
743 * g_application_add_main_option:
744 * @application: the #GApplication
745 * @long_name: the long name of an option used to specify it in a commandline
746 * @short_name: the short name of an option
747 * @flags: flags from #GOptionFlags
748 * @arg: the type of the option, as a #GOptionArg
749 * @description: the description for the option in `--help` output
750 * @arg_description: (nullable): the placeholder to use for the extra argument
751 * parsed by the option in `--help` output
752 *
753 * Add an option to be handled by @application.
754 *
755 * Calling this function is the equivalent of calling
756 * g_application_add_main_option_entries() with a single #GOptionEntry
757 * that has its arg_data member set to %NULL.
758 *
759 * The parsed arguments will be packed into a #GVariantDict which
760 * is passed to #GApplication::handle-local-options. If
761 * %G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also
762 * be sent to the primary instance. See
763 * g_application_add_main_option_entries() for more details.
764 *
765 * See #GOptionEntry for more documentation of the arguments.
766 *
767 * Since: 2.42
768 **/
769 void
g_application_add_main_option(GApplication * application,const char * long_name,char short_name,GOptionFlags flags,GOptionArg arg,const char * description,const char * arg_description)770 g_application_add_main_option (GApplication *application,
771 const char *long_name,
772 char short_name,
773 GOptionFlags flags,
774 GOptionArg arg,
775 const char *description,
776 const char *arg_description)
777 {
778 gchar *dup_string;
779 GOptionEntry my_entry[2] = {
780 { NULL, short_name, flags, arg, NULL, NULL, NULL },
781 { NULL }
782 };
783
784 g_return_if_fail (G_IS_APPLICATION (application));
785 g_return_if_fail (long_name != NULL);
786 g_return_if_fail (description != NULL);
787
788 my_entry[0].long_name = dup_string = g_strdup (long_name);
789 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
790
791 my_entry[0].description = dup_string = g_strdup (description);
792 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
793
794 my_entry[0].arg_description = dup_string = g_strdup (arg_description);
795 application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
796
797 g_application_add_main_option_entries (application, my_entry);
798 }
799
800 /**
801 * g_application_add_option_group:
802 * @application: the #GApplication
803 * @group: (transfer full): a #GOptionGroup
804 *
805 * Adds a #GOptionGroup to the commandline handling of @application.
806 *
807 * This function is comparable to g_option_context_add_group().
808 *
809 * Unlike g_application_add_main_option_entries(), this function does
810 * not deal with %NULL @arg_data and never transmits options to the
811 * primary instance.
812 *
813 * The reason for that is because, by the time the options arrive at the
814 * primary instance, it is typically too late to do anything with them.
815 * Taking the GTK option group as an example: GTK will already have been
816 * initialised by the time the #GApplication::command-line handler runs.
817 * In the case that this is not the first-running instance of the
818 * application, the existing instance may already have been running for
819 * a very long time.
820 *
821 * This means that the options from #GOptionGroup are only really usable
822 * in the case that the instance of the application being run is the
823 * first instance. Passing options like `--display=` or `--gdk-debug=`
824 * on future runs will have no effect on the existing primary instance.
825 *
826 * Calling this function will cause the options in the supplied option
827 * group to be parsed, but it does not cause you to be "opted in" to the
828 * new functionality whereby unrecognised options are rejected even if
829 * %G_APPLICATION_HANDLES_COMMAND_LINE was given.
830 *
831 * Since: 2.40
832 **/
833 void
g_application_add_option_group(GApplication * application,GOptionGroup * group)834 g_application_add_option_group (GApplication *application,
835 GOptionGroup *group)
836 {
837 g_return_if_fail (G_IS_APPLICATION (application));
838 g_return_if_fail (group != NULL);
839
840 application->priv->option_groups = g_slist_prepend (application->priv->option_groups, group);
841 }
842
843 /**
844 * g_application_set_option_context_parameter_string:
845 * @application: the #GApplication
846 * @parameter_string: (nullable): a string which is displayed
847 * in the first line of `--help` output, after the usage summary `programname [OPTION...]`.
848 *
849 * Sets the parameter string to be used by the commandline handling of @application.
850 *
851 * This function registers the argument to be passed to g_option_context_new()
852 * when the internal #GOptionContext of @application is created.
853 *
854 * See g_option_context_new() for more information about @parameter_string.
855 *
856 * Since: 2.56
857 */
858 void
g_application_set_option_context_parameter_string(GApplication * application,const gchar * parameter_string)859 g_application_set_option_context_parameter_string (GApplication *application,
860 const gchar *parameter_string)
861 {
862 g_return_if_fail (G_IS_APPLICATION (application));
863
864 g_free (application->priv->parameter_string);
865 application->priv->parameter_string = g_strdup (parameter_string);
866 }
867
868 /**
869 * g_application_set_option_context_summary:
870 * @application: the #GApplication
871 * @summary: (nullable): a string to be shown in `--help` output
872 * before the list of options, or %NULL
873 *
874 * Adds a summary to the @application option context.
875 *
876 * See g_option_context_set_summary() for more information.
877 *
878 * Since: 2.56
879 */
880 void
g_application_set_option_context_summary(GApplication * application,const gchar * summary)881 g_application_set_option_context_summary (GApplication *application,
882 const gchar *summary)
883 {
884 g_return_if_fail (G_IS_APPLICATION (application));
885
886 g_free (application->priv->summary);
887 application->priv->summary = g_strdup (summary);
888 }
889
890 /**
891 * g_application_set_option_context_description:
892 * @application: the #GApplication
893 * @description: (nullable): a string to be shown in `--help` output
894 * after the list of options, or %NULL
895 *
896 * Adds a description to the @application option context.
897 *
898 * See g_option_context_set_description() for more information.
899 *
900 * Since: 2.56
901 */
902 void
g_application_set_option_context_description(GApplication * application,const gchar * description)903 g_application_set_option_context_description (GApplication *application,
904 const gchar *description)
905 {
906 g_return_if_fail (G_IS_APPLICATION (application));
907
908 g_free (application->priv->description);
909 application->priv->description = g_strdup (description);
910
911 }
912
913
914 /* vfunc defaults {{{1 */
915 static void
g_application_real_before_emit(GApplication * application,GVariant * platform_data)916 g_application_real_before_emit (GApplication *application,
917 GVariant *platform_data)
918 {
919 }
920
921 static void
g_application_real_after_emit(GApplication * application,GVariant * platform_data)922 g_application_real_after_emit (GApplication *application,
923 GVariant *platform_data)
924 {
925 }
926
927 static void
g_application_real_startup(GApplication * application)928 g_application_real_startup (GApplication *application)
929 {
930 application->priv->did_startup = TRUE;
931 }
932
933 static void
g_application_real_shutdown(GApplication * application)934 g_application_real_shutdown (GApplication *application)
935 {
936 application->priv->did_shutdown = TRUE;
937 }
938
939 static void
g_application_real_activate(GApplication * application)940 g_application_real_activate (GApplication *application)
941 {
942 if (!g_signal_has_handler_pending (application,
943 g_application_signals[SIGNAL_ACTIVATE],
944 0, TRUE) &&
945 G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
946 {
947 static gboolean warned;
948
949 if (warned)
950 return;
951
952 g_warning ("Your application does not implement "
953 "g_application_activate() and has no handlers connected "
954 "to the 'activate' signal. It should do one of these.");
955 warned = TRUE;
956 }
957 }
958
959 static void
g_application_real_open(GApplication * application,GFile ** files,gint n_files,const gchar * hint)960 g_application_real_open (GApplication *application,
961 GFile **files,
962 gint n_files,
963 const gchar *hint)
964 {
965 if (!g_signal_has_handler_pending (application,
966 g_application_signals[SIGNAL_OPEN],
967 0, TRUE) &&
968 G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
969 {
970 static gboolean warned;
971
972 if (warned)
973 return;
974
975 g_warning ("Your application claims to support opening files "
976 "but does not implement g_application_open() and has no "
977 "handlers connected to the 'open' signal.");
978 warned = TRUE;
979 }
980 }
981
982 static int
g_application_real_command_line(GApplication * application,GApplicationCommandLine * cmdline)983 g_application_real_command_line (GApplication *application,
984 GApplicationCommandLine *cmdline)
985 {
986 if (!g_signal_has_handler_pending (application,
987 g_application_signals[SIGNAL_COMMAND_LINE],
988 0, TRUE) &&
989 G_APPLICATION_GET_CLASS (application)->command_line == g_application_real_command_line)
990 {
991 static gboolean warned;
992
993 if (warned)
994 return 1;
995
996 g_warning ("Your application claims to support custom command line "
997 "handling but does not implement g_application_command_line() "
998 "and has no handlers connected to the 'command-line' signal.");
999
1000 warned = TRUE;
1001 }
1002
1003 return 1;
1004 }
1005
1006 static gint
g_application_real_handle_local_options(GApplication * application,GVariantDict * options)1007 g_application_real_handle_local_options (GApplication *application,
1008 GVariantDict *options)
1009 {
1010 return -1;
1011 }
1012
1013 static GVariant *
get_platform_data(GApplication * application,GVariant * options)1014 get_platform_data (GApplication *application,
1015 GVariant *options)
1016 {
1017 GVariantBuilder *builder;
1018 GVariant *result;
1019
1020 builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
1021
1022 {
1023 gchar *cwd = g_get_current_dir ();
1024 g_variant_builder_add (builder, "{sv}", "cwd",
1025 g_variant_new_bytestring (cwd));
1026 g_free (cwd);
1027 }
1028
1029 if (application->priv->flags & G_APPLICATION_SEND_ENVIRONMENT)
1030 {
1031 GVariant *array;
1032 gchar **envp;
1033
1034 envp = g_get_environ ();
1035 array = g_variant_new_bytestring_array ((const gchar **) envp, -1);
1036 g_strfreev (envp);
1037
1038 g_variant_builder_add (builder, "{sv}", "environ", array);
1039 }
1040
1041 if (options)
1042 g_variant_builder_add (builder, "{sv}", "options", options);
1043
1044 G_APPLICATION_GET_CLASS (application)->
1045 add_platform_data (application, builder);
1046
1047 result = g_variant_builder_end (builder);
1048 g_variant_builder_unref (builder);
1049
1050 return result;
1051 }
1052
1053 static void
g_application_call_command_line(GApplication * application,const gchar * const * arguments,GVariant * options,gint * exit_status)1054 g_application_call_command_line (GApplication *application,
1055 const gchar * const *arguments,
1056 GVariant *options,
1057 gint *exit_status)
1058 {
1059 if (application->priv->is_remote)
1060 {
1061 GVariant *platform_data;
1062
1063 platform_data = get_platform_data (application, options);
1064 *exit_status = g_application_impl_command_line (application->priv->impl, arguments, platform_data);
1065 }
1066 else
1067 {
1068 GApplicationCommandLine *cmdline;
1069 GVariant *v;
1070
1071 v = g_variant_new_bytestring_array ((const gchar **) arguments, -1);
1072 cmdline = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE,
1073 "arguments", v,
1074 "options", options,
1075 NULL);
1076 g_signal_emit (application, g_application_signals[SIGNAL_COMMAND_LINE], 0, cmdline, exit_status);
1077 g_object_unref (cmdline);
1078 }
1079 }
1080
1081 static gboolean
g_application_real_local_command_line(GApplication * application,gchar *** arguments,int * exit_status)1082 g_application_real_local_command_line (GApplication *application,
1083 gchar ***arguments,
1084 int *exit_status)
1085 {
1086 GError *error = NULL;
1087 GVariantDict *options;
1088 gint n_args;
1089
1090 options = g_application_parse_command_line (application, arguments, &error);
1091 if (!options)
1092 {
1093 g_printerr ("%s\n", error->message);
1094 *exit_status = 1;
1095 return TRUE;
1096 }
1097
1098 g_signal_emit (application, g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS], 0, options, exit_status);
1099
1100 if (*exit_status >= 0)
1101 {
1102 g_variant_dict_unref (options);
1103 return TRUE;
1104 }
1105
1106 if (!g_application_register (application, NULL, &error))
1107 {
1108 g_printerr ("Failed to register: %s\n", error->message);
1109 g_variant_dict_unref (options);
1110 g_error_free (error);
1111 *exit_status = 1;
1112 return TRUE;
1113 }
1114
1115 n_args = g_strv_length (*arguments);
1116
1117 if (application->priv->flags & G_APPLICATION_IS_SERVICE)
1118 {
1119 if ((*exit_status = n_args > 1))
1120 {
1121 g_printerr ("GApplication service mode takes no arguments.\n");
1122 application->priv->flags &= ~G_APPLICATION_IS_SERVICE;
1123 *exit_status = 1;
1124 }
1125 else
1126 *exit_status = 0;
1127 }
1128 else if (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE)
1129 {
1130 g_application_call_command_line (application,
1131 (const gchar **) *arguments,
1132 g_variant_dict_end (options),
1133 exit_status);
1134 }
1135 else
1136 {
1137 if (n_args <= 1)
1138 {
1139 g_application_activate (application);
1140 *exit_status = 0;
1141 }
1142
1143 else
1144 {
1145 if (~application->priv->flags & G_APPLICATION_HANDLES_OPEN)
1146 {
1147 g_critical ("This application can not open files.");
1148 *exit_status = 1;
1149 }
1150 else
1151 {
1152 GFile **files;
1153 gint n_files;
1154 gint i;
1155
1156 n_files = n_args - 1;
1157 files = g_new (GFile *, n_files);
1158
1159 for (i = 0; i < n_files; i++)
1160 files[i] = g_file_new_for_commandline_arg ((*arguments)[i + 1]);
1161
1162 g_application_open (application, files, n_files, "");
1163
1164 for (i = 0; i < n_files; i++)
1165 g_object_unref (files[i]);
1166 g_free (files);
1167
1168 *exit_status = 0;
1169 }
1170 }
1171 }
1172
1173 g_variant_dict_unref (options);
1174
1175 return TRUE;
1176 }
1177
1178 static void
g_application_real_add_platform_data(GApplication * application,GVariantBuilder * builder)1179 g_application_real_add_platform_data (GApplication *application,
1180 GVariantBuilder *builder)
1181 {
1182 }
1183
1184 static gboolean
g_application_real_dbus_register(GApplication * application,GDBusConnection * connection,const gchar * object_path,GError ** error)1185 g_application_real_dbus_register (GApplication *application,
1186 GDBusConnection *connection,
1187 const gchar *object_path,
1188 GError **error)
1189 {
1190 return TRUE;
1191 }
1192
1193 static void
g_application_real_dbus_unregister(GApplication * application,GDBusConnection * connection,const gchar * object_path)1194 g_application_real_dbus_unregister (GApplication *application,
1195 GDBusConnection *connection,
1196 const gchar *object_path)
1197 {
1198 }
1199
1200 static gboolean
g_application_real_name_lost(GApplication * application)1201 g_application_real_name_lost (GApplication *application)
1202 {
1203 g_application_quit (application);
1204 return TRUE;
1205 }
1206
1207 /* GObject implementation stuff {{{1 */
1208 static void
g_application_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1209 g_application_set_property (GObject *object,
1210 guint prop_id,
1211 const GValue *value,
1212 GParamSpec *pspec)
1213 {
1214 GApplication *application = G_APPLICATION (object);
1215
1216 switch (prop_id)
1217 {
1218 case PROP_APPLICATION_ID:
1219 g_application_set_application_id (application,
1220 g_value_get_string (value));
1221 break;
1222
1223 case PROP_FLAGS:
1224 g_application_set_flags (application, g_value_get_flags (value));
1225 break;
1226
1227 case PROP_RESOURCE_BASE_PATH:
1228 g_application_set_resource_base_path (application, g_value_get_string (value));
1229 break;
1230
1231 case PROP_INACTIVITY_TIMEOUT:
1232 g_application_set_inactivity_timeout (application,
1233 g_value_get_uint (value));
1234 break;
1235
1236 case PROP_ACTION_GROUP:
1237 g_clear_object (&application->priv->actions);
1238 application->priv->actions = g_value_dup_object (value);
1239 break;
1240
1241 default:
1242 g_assert_not_reached ();
1243 }
1244 }
1245
1246 /**
1247 * g_application_set_action_group:
1248 * @application: a #GApplication
1249 * @action_group: (nullable): a #GActionGroup, or %NULL
1250 *
1251 * This used to be how actions were associated with a #GApplication.
1252 * Now there is #GActionMap for that.
1253 *
1254 * Since: 2.28
1255 *
1256 * Deprecated:2.32:Use the #GActionMap interface instead. Never ever
1257 * mix use of this API with use of #GActionMap on the same @application
1258 * or things will go very badly wrong. This function is known to
1259 * introduce buggy behaviour (ie: signals not emitted on changes to the
1260 * action group), so you should really use #GActionMap instead.
1261 **/
1262 void
g_application_set_action_group(GApplication * application,GActionGroup * action_group)1263 g_application_set_action_group (GApplication *application,
1264 GActionGroup *action_group)
1265 {
1266 g_return_if_fail (G_IS_APPLICATION (application));
1267 g_return_if_fail (!application->priv->is_registered);
1268
1269 if (application->priv->actions != NULL)
1270 g_object_unref (application->priv->actions);
1271
1272 application->priv->actions = action_group;
1273
1274 if (application->priv->actions != NULL)
1275 g_object_ref (application->priv->actions);
1276 }
1277
1278 static void
g_application_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1279 g_application_get_property (GObject *object,
1280 guint prop_id,
1281 GValue *value,
1282 GParamSpec *pspec)
1283 {
1284 GApplication *application = G_APPLICATION (object);
1285
1286 switch (prop_id)
1287 {
1288 case PROP_APPLICATION_ID:
1289 g_value_set_string (value,
1290 g_application_get_application_id (application));
1291 break;
1292
1293 case PROP_FLAGS:
1294 g_value_set_flags (value,
1295 g_application_get_flags (application));
1296 break;
1297
1298 case PROP_RESOURCE_BASE_PATH:
1299 g_value_set_string (value, g_application_get_resource_base_path (application));
1300 break;
1301
1302 case PROP_IS_REGISTERED:
1303 g_value_set_boolean (value,
1304 g_application_get_is_registered (application));
1305 break;
1306
1307 case PROP_IS_REMOTE:
1308 g_value_set_boolean (value,
1309 g_application_get_is_remote (application));
1310 break;
1311
1312 case PROP_INACTIVITY_TIMEOUT:
1313 g_value_set_uint (value,
1314 g_application_get_inactivity_timeout (application));
1315 break;
1316
1317 case PROP_IS_BUSY:
1318 g_value_set_boolean (value, g_application_get_is_busy (application));
1319 break;
1320
1321 default:
1322 g_assert_not_reached ();
1323 }
1324 }
1325
1326 static void
g_application_constructed(GObject * object)1327 g_application_constructed (GObject *object)
1328 {
1329 GApplication *application = G_APPLICATION (object);
1330
1331 if (g_application_get_default () == NULL)
1332 g_application_set_default (application);
1333
1334 /* People should not set properties from _init... */
1335 g_assert (application->priv->resource_path == NULL);
1336
1337 if (application->priv->id != NULL)
1338 {
1339 gint i;
1340
1341 application->priv->resource_path = g_strconcat ("/", application->priv->id, NULL);
1342
1343 for (i = 1; application->priv->resource_path[i]; i++)
1344 if (application->priv->resource_path[i] == '.')
1345 application->priv->resource_path[i] = '/';
1346 }
1347 }
1348
1349 static void
g_application_dispose(GObject * object)1350 g_application_dispose (GObject *object)
1351 {
1352 GApplication *application = G_APPLICATION (object);
1353
1354 if (application->priv->impl != NULL &&
1355 G_APPLICATION_GET_CLASS (application)->dbus_unregister != g_application_real_dbus_unregister)
1356 {
1357 static gboolean warned;
1358
1359 if (!warned)
1360 {
1361 g_warning ("Your application did not unregister from D-Bus before destruction. "
1362 "Consider using g_application_run().");
1363 }
1364
1365 warned = TRUE;
1366 }
1367
1368 G_OBJECT_CLASS (g_application_parent_class)->dispose (object);
1369 }
1370
1371 static void
g_application_finalize(GObject * object)1372 g_application_finalize (GObject *object)
1373 {
1374 GApplication *application = G_APPLICATION (object);
1375
1376 if (application->priv->inactivity_timeout_id)
1377 g_source_remove (application->priv->inactivity_timeout_id);
1378
1379 g_slist_free_full (application->priv->option_groups, (GDestroyNotify) g_option_group_unref);
1380 if (application->priv->main_options)
1381 g_option_group_unref (application->priv->main_options);
1382 if (application->priv->packed_options)
1383 g_hash_table_unref (application->priv->packed_options);
1384
1385 g_free (application->priv->parameter_string);
1386 g_free (application->priv->summary);
1387 g_free (application->priv->description);
1388
1389 g_slist_free_full (application->priv->option_strings, g_free);
1390
1391 if (application->priv->impl)
1392 g_application_impl_destroy (application->priv->impl);
1393 g_free (application->priv->id);
1394
1395 if (g_application_get_default () == application)
1396 g_application_set_default (NULL);
1397
1398 if (application->priv->actions)
1399 g_object_unref (application->priv->actions);
1400
1401 g_clear_object (&application->priv->remote_actions);
1402
1403 if (application->priv->notifications)
1404 g_object_unref (application->priv->notifications);
1405
1406 g_free (application->priv->resource_path);
1407
1408 G_OBJECT_CLASS (g_application_parent_class)
1409 ->finalize (object);
1410 }
1411
1412 static void
g_application_init(GApplication * application)1413 g_application_init (GApplication *application)
1414 {
1415 application->priv = g_application_get_instance_private (application);
1416
1417 application->priv->actions = g_application_exported_actions_new (application);
1418
1419 /* application->priv->actions is the one and only ref on the group, so when
1420 * we dispose, the action group will die, disconnecting all signals.
1421 */
1422 g_signal_connect_swapped (application->priv->actions, "action-added",
1423 G_CALLBACK (g_action_group_action_added), application);
1424 g_signal_connect_swapped (application->priv->actions, "action-enabled-changed",
1425 G_CALLBACK (g_action_group_action_enabled_changed), application);
1426 g_signal_connect_swapped (application->priv->actions, "action-state-changed",
1427 G_CALLBACK (g_action_group_action_state_changed), application);
1428 g_signal_connect_swapped (application->priv->actions, "action-removed",
1429 G_CALLBACK (g_action_group_action_removed), application);
1430 }
1431
1432 static gboolean
g_application_handle_local_options_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)1433 g_application_handle_local_options_accumulator (GSignalInvocationHint *ihint,
1434 GValue *return_accu,
1435 const GValue *handler_return,
1436 gpointer dummy)
1437 {
1438 gint value;
1439
1440 value = g_value_get_int (handler_return);
1441 g_value_set_int (return_accu, value);
1442
1443 return value < 0;
1444 }
1445
1446 static void
g_application_class_init(GApplicationClass * class)1447 g_application_class_init (GApplicationClass *class)
1448 {
1449 GObjectClass *object_class = G_OBJECT_CLASS (class);
1450
1451 object_class->constructed = g_application_constructed;
1452 object_class->dispose = g_application_dispose;
1453 object_class->finalize = g_application_finalize;
1454 object_class->get_property = g_application_get_property;
1455 object_class->set_property = g_application_set_property;
1456
1457 class->before_emit = g_application_real_before_emit;
1458 class->after_emit = g_application_real_after_emit;
1459 class->startup = g_application_real_startup;
1460 class->shutdown = g_application_real_shutdown;
1461 class->activate = g_application_real_activate;
1462 class->open = g_application_real_open;
1463 class->command_line = g_application_real_command_line;
1464 class->local_command_line = g_application_real_local_command_line;
1465 class->handle_local_options = g_application_real_handle_local_options;
1466 class->add_platform_data = g_application_real_add_platform_data;
1467 class->dbus_register = g_application_real_dbus_register;
1468 class->dbus_unregister = g_application_real_dbus_unregister;
1469 class->name_lost = g_application_real_name_lost;
1470
1471 g_object_class_install_property (object_class, PROP_APPLICATION_ID,
1472 g_param_spec_string ("application-id",
1473 P_("Application identifier"),
1474 P_("The unique identifier for the application"),
1475 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
1476 G_PARAM_STATIC_STRINGS));
1477
1478 g_object_class_install_property (object_class, PROP_FLAGS,
1479 g_param_spec_flags ("flags",
1480 P_("Application flags"),
1481 P_("Flags specifying the behaviour of the application"),
1482 G_TYPE_APPLICATION_FLAGS, G_APPLICATION_FLAGS_NONE,
1483 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1484
1485 g_object_class_install_property (object_class, PROP_RESOURCE_BASE_PATH,
1486 g_param_spec_string ("resource-base-path",
1487 P_("Resource base path"),
1488 P_("The base resource path for the application"),
1489 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1490
1491 g_object_class_install_property (object_class, PROP_IS_REGISTERED,
1492 g_param_spec_boolean ("is-registered",
1493 P_("Is registered"),
1494 P_("If g_application_register() has been called"),
1495 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1496
1497 g_object_class_install_property (object_class, PROP_IS_REMOTE,
1498 g_param_spec_boolean ("is-remote",
1499 P_("Is remote"),
1500 P_("If this application instance is remote"),
1501 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1502
1503 g_object_class_install_property (object_class, PROP_INACTIVITY_TIMEOUT,
1504 g_param_spec_uint ("inactivity-timeout",
1505 P_("Inactivity timeout"),
1506 P_("Time (ms) to stay alive after becoming idle"),
1507 0, G_MAXUINT, 0,
1508 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1509
1510 g_object_class_install_property (object_class, PROP_ACTION_GROUP,
1511 g_param_spec_object ("action-group",
1512 P_("Action group"),
1513 P_("The group of actions that the application exports"),
1514 G_TYPE_ACTION_GROUP,
1515 G_PARAM_DEPRECATED | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
1516
1517 /**
1518 * GApplication:is-busy:
1519 *
1520 * Whether the application is currently marked as busy through
1521 * g_application_mark_busy() or g_application_bind_busy_property().
1522 *
1523 * Since: 2.44
1524 */
1525 g_object_class_install_property (object_class, PROP_IS_BUSY,
1526 g_param_spec_boolean ("is-busy",
1527 P_("Is busy"),
1528 P_("If this application is currently marked busy"),
1529 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1530
1531 /**
1532 * GApplication::startup:
1533 * @application: the application
1534 *
1535 * The ::startup signal is emitted on the primary instance immediately
1536 * after registration. See g_application_register().
1537 */
1538 g_application_signals[SIGNAL_STARTUP] =
1539 g_signal_new (I_("startup"), G_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
1540 G_STRUCT_OFFSET (GApplicationClass, startup),
1541 NULL, NULL, NULL, G_TYPE_NONE, 0);
1542
1543 /**
1544 * GApplication::shutdown:
1545 * @application: the application
1546 *
1547 * The ::shutdown signal is emitted only on the registered primary instance
1548 * immediately after the main loop terminates.
1549 */
1550 g_application_signals[SIGNAL_SHUTDOWN] =
1551 g_signal_new (I_("shutdown"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1552 G_STRUCT_OFFSET (GApplicationClass, shutdown),
1553 NULL, NULL, NULL, G_TYPE_NONE, 0);
1554
1555 /**
1556 * GApplication::activate:
1557 * @application: the application
1558 *
1559 * The ::activate signal is emitted on the primary instance when an
1560 * activation occurs. See g_application_activate().
1561 */
1562 g_application_signals[SIGNAL_ACTIVATE] =
1563 g_signal_new (I_("activate"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1564 G_STRUCT_OFFSET (GApplicationClass, activate),
1565 NULL, NULL, NULL, G_TYPE_NONE, 0);
1566
1567
1568 /**
1569 * GApplication::open:
1570 * @application: the application
1571 * @files: (array length=n_files) (element-type GFile): an array of #GFiles
1572 * @n_files: the length of @files
1573 * @hint: a hint provided by the calling instance
1574 *
1575 * The ::open signal is emitted on the primary instance when there are
1576 * files to open. See g_application_open() for more information.
1577 */
1578 g_application_signals[SIGNAL_OPEN] =
1579 g_signal_new (I_("open"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1580 G_STRUCT_OFFSET (GApplicationClass, open),
1581 NULL, NULL,
1582 _g_cclosure_marshal_VOID__POINTER_INT_STRING,
1583 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);
1584 g_signal_set_va_marshaller (g_application_signals[SIGNAL_OPEN],
1585 G_TYPE_FROM_CLASS (class),
1586 _g_cclosure_marshal_VOID__POINTER_INT_STRINGv);
1587
1588 /**
1589 * GApplication::command-line:
1590 * @application: the application
1591 * @command_line: a #GApplicationCommandLine representing the
1592 * passed commandline
1593 *
1594 * The ::command-line signal is emitted on the primary instance when
1595 * a commandline is not handled locally. See g_application_run() and
1596 * the #GApplicationCommandLine documentation for more information.
1597 *
1598 * Returns: An integer that is set as the exit status for the calling
1599 * process. See g_application_command_line_set_exit_status().
1600 */
1601 g_application_signals[SIGNAL_COMMAND_LINE] =
1602 g_signal_new (I_("command-line"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1603 G_STRUCT_OFFSET (GApplicationClass, command_line),
1604 g_signal_accumulator_first_wins, NULL,
1605 _g_cclosure_marshal_INT__OBJECT,
1606 G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
1607 g_signal_set_va_marshaller (g_application_signals[SIGNAL_COMMAND_LINE],
1608 G_TYPE_FROM_CLASS (class),
1609 _g_cclosure_marshal_INT__OBJECTv);
1610
1611 /**
1612 * GApplication::handle-local-options:
1613 * @application: the application
1614 * @options: the options dictionary
1615 *
1616 * The ::handle-local-options signal is emitted on the local instance
1617 * after the parsing of the commandline options has occurred.
1618 *
1619 * You can add options to be recognised during commandline option
1620 * parsing using g_application_add_main_option_entries() and
1621 * g_application_add_option_group().
1622 *
1623 * Signal handlers can inspect @options (along with values pointed to
1624 * from the @arg_data of an installed #GOptionEntrys) in order to
1625 * decide to perform certain actions, including direct local handling
1626 * (which may be useful for options like --version).
1627 *
1628 * In the event that the application is marked
1629 * %G_APPLICATION_HANDLES_COMMAND_LINE the "normal processing" will
1630 * send the @options dictionary to the primary instance where it can be
1631 * read with g_application_command_line_get_options_dict(). The signal
1632 * handler can modify the dictionary before returning, and the
1633 * modified dictionary will be sent.
1634 *
1635 * In the event that %G_APPLICATION_HANDLES_COMMAND_LINE is not set,
1636 * "normal processing" will treat the remaining uncollected command
1637 * line arguments as filenames or URIs. If there are no arguments,
1638 * the application is activated by g_application_activate(). One or
1639 * more arguments results in a call to g_application_open().
1640 *
1641 * If you want to handle the local commandline arguments for yourself
1642 * by converting them to calls to g_application_open() or
1643 * g_action_group_activate_action() then you must be sure to register
1644 * the application first. You should probably not call
1645 * g_application_activate() for yourself, however: just return -1 and
1646 * allow the default handler to do it for you. This will ensure that
1647 * the `--gapplication-service` switch works properly (i.e. no activation
1648 * in that case).
1649 *
1650 * Note that this signal is emitted from the default implementation of
1651 * local_command_line(). If you override that function and don't
1652 * chain up then this signal will never be emitted.
1653 *
1654 * You can override local_command_line() if you need more powerful
1655 * capabilities than what is provided here, but this should not
1656 * normally be required.
1657 *
1658 * Returns: an exit code. If you have handled your options and want
1659 * to exit the process, return a non-negative option, 0 for success,
1660 * and a positive value for failure. To continue, return -1 to let
1661 * the default option processing continue.
1662 *
1663 * Since: 2.40
1664 **/
1665 g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS] =
1666 g_signal_new (I_("handle-local-options"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1667 G_STRUCT_OFFSET (GApplicationClass, handle_local_options),
1668 g_application_handle_local_options_accumulator, NULL,
1669 _g_cclosure_marshal_INT__BOXED,
1670 G_TYPE_INT, 1, G_TYPE_VARIANT_DICT);
1671 g_signal_set_va_marshaller (g_application_signals[SIGNAL_HANDLE_LOCAL_OPTIONS],
1672 G_TYPE_FROM_CLASS (class),
1673 _g_cclosure_marshal_INT__BOXEDv);
1674
1675 /**
1676 * GApplication::name-lost:
1677 * @application: the application
1678 *
1679 * The ::name-lost signal is emitted only on the registered primary instance
1680 * when a new instance has taken over. This can only happen if the application
1681 * is using the %G_APPLICATION_ALLOW_REPLACEMENT flag.
1682 *
1683 * The default handler for this signal calls g_application_quit().
1684 *
1685 * Returns: %TRUE if the signal has been handled
1686 *
1687 * Since: 2.60
1688 */
1689 g_application_signals[SIGNAL_NAME_LOST] =
1690 g_signal_new (I_("name-lost"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
1691 G_STRUCT_OFFSET (GApplicationClass, name_lost),
1692 g_signal_accumulator_true_handled, NULL,
1693 _g_cclosure_marshal_BOOLEAN__VOID,
1694 G_TYPE_BOOLEAN, 0);
1695 g_signal_set_va_marshaller (g_application_signals[SIGNAL_NAME_LOST],
1696 G_TYPE_FROM_CLASS (class),
1697 _g_cclosure_marshal_BOOLEAN__VOIDv);
1698 }
1699
1700 /* Application ID validity {{{1 */
1701
1702 /**
1703 * g_application_id_is_valid:
1704 * @application_id: a potential application identifier
1705 *
1706 * Checks if @application_id is a valid application identifier.
1707 *
1708 * A valid ID is required for calls to g_application_new() and
1709 * g_application_set_application_id().
1710 *
1711 * Application identifiers follow the same format as
1712 * [D-Bus well-known bus names](https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus).
1713 * For convenience, the restrictions on application identifiers are
1714 * reproduced here:
1715 *
1716 * - Application identifiers are composed of 1 or more elements separated by a
1717 * period (`.`) character. All elements must contain at least one character.
1718 *
1719 * - Each element must only contain the ASCII characters `[A-Z][a-z][0-9]_-`,
1720 * with `-` discouraged in new application identifiers. Each element must not
1721 * begin with a digit.
1722 *
1723 * - Application identifiers must contain at least one `.` (period) character
1724 * (and thus at least two elements).
1725 *
1726 * - Application identifiers must not begin with a `.` (period) character.
1727 *
1728 * - Application identifiers must not exceed 255 characters.
1729 *
1730 * Note that the hyphen (`-`) character is allowed in application identifiers,
1731 * but is problematic or not allowed in various specifications and APIs that
1732 * refer to D-Bus, such as
1733 * [Flatpak application IDs](http://docs.flatpak.org/en/latest/introduction.html#identifiers),
1734 * the
1735 * [`DBusActivatable` interface in the Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#dbus),
1736 * and the convention that an application's "main" interface and object path
1737 * resemble its application identifier and bus name. To avoid situations that
1738 * require special-case handling, it is recommended that new application
1739 * identifiers consistently replace hyphens with underscores.
1740 *
1741 * Like D-Bus interface names, application identifiers should start with the
1742 * reversed DNS domain name of the author of the interface (in lower-case), and
1743 * it is conventional for the rest of the application identifier to consist of
1744 * words run together, with initial capital letters.
1745 *
1746 * As with D-Bus interface names, if the author's DNS domain name contains
1747 * hyphen/minus characters they should be replaced by underscores, and if it
1748 * contains leading digits they should be escaped by prepending an underscore.
1749 * For example, if the owner of 7-zip.org used an application identifier for an
1750 * archiving application, it might be named `org._7_zip.Archiver`.
1751 *
1752 * Returns: %TRUE if @application_id is valid
1753 */
1754 gboolean
g_application_id_is_valid(const gchar * application_id)1755 g_application_id_is_valid (const gchar *application_id)
1756 {
1757 return g_dbus_is_name (application_id) &&
1758 !g_dbus_is_unique_name (application_id);
1759 }
1760
1761 /* Public Constructor {{{1 */
1762 /**
1763 * g_application_new:
1764 * @application_id: (nullable): the application id
1765 * @flags: the application flags
1766 *
1767 * Creates a new #GApplication instance.
1768 *
1769 * If non-%NULL, the application id must be valid. See
1770 * g_application_id_is_valid().
1771 *
1772 * If no application ID is given then some features of #GApplication
1773 * (most notably application uniqueness) will be disabled.
1774 *
1775 * Returns: a new #GApplication instance
1776 **/
1777 GApplication *
g_application_new(const gchar * application_id,GApplicationFlags flags)1778 g_application_new (const gchar *application_id,
1779 GApplicationFlags flags)
1780 {
1781 g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
1782
1783 return g_object_new (G_TYPE_APPLICATION,
1784 "application-id", application_id,
1785 "flags", flags,
1786 NULL);
1787 }
1788
1789 /* Simple get/set: application id, flags, inactivity timeout {{{1 */
1790 /**
1791 * g_application_get_application_id:
1792 * @application: a #GApplication
1793 *
1794 * Gets the unique identifier for @application.
1795 *
1796 * Returns: the identifier for @application, owned by @application
1797 *
1798 * Since: 2.28
1799 **/
1800 const gchar *
g_application_get_application_id(GApplication * application)1801 g_application_get_application_id (GApplication *application)
1802 {
1803 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1804
1805 return application->priv->id;
1806 }
1807
1808 /**
1809 * g_application_set_application_id:
1810 * @application: a #GApplication
1811 * @application_id: (nullable): the identifier for @application
1812 *
1813 * Sets the unique identifier for @application.
1814 *
1815 * The application id can only be modified if @application has not yet
1816 * been registered.
1817 *
1818 * If non-%NULL, the application id must be valid. See
1819 * g_application_id_is_valid().
1820 *
1821 * Since: 2.28
1822 **/
1823 void
g_application_set_application_id(GApplication * application,const gchar * application_id)1824 g_application_set_application_id (GApplication *application,
1825 const gchar *application_id)
1826 {
1827 g_return_if_fail (G_IS_APPLICATION (application));
1828
1829 if (g_strcmp0 (application->priv->id, application_id) != 0)
1830 {
1831 g_return_if_fail (application_id == NULL || g_application_id_is_valid (application_id));
1832 g_return_if_fail (!application->priv->is_registered);
1833
1834 g_free (application->priv->id);
1835 application->priv->id = g_strdup (application_id);
1836
1837 g_object_notify (G_OBJECT (application), "application-id");
1838 }
1839 }
1840
1841 /**
1842 * g_application_get_flags:
1843 * @application: a #GApplication
1844 *
1845 * Gets the flags for @application.
1846 *
1847 * See #GApplicationFlags.
1848 *
1849 * Returns: the flags for @application
1850 *
1851 * Since: 2.28
1852 **/
1853 GApplicationFlags
g_application_get_flags(GApplication * application)1854 g_application_get_flags (GApplication *application)
1855 {
1856 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1857
1858 return application->priv->flags;
1859 }
1860
1861 /**
1862 * g_application_set_flags:
1863 * @application: a #GApplication
1864 * @flags: the flags for @application
1865 *
1866 * Sets the flags for @application.
1867 *
1868 * The flags can only be modified if @application has not yet been
1869 * registered.
1870 *
1871 * See #GApplicationFlags.
1872 *
1873 * Since: 2.28
1874 **/
1875 void
g_application_set_flags(GApplication * application,GApplicationFlags flags)1876 g_application_set_flags (GApplication *application,
1877 GApplicationFlags flags)
1878 {
1879 g_return_if_fail (G_IS_APPLICATION (application));
1880
1881 if (application->priv->flags != flags)
1882 {
1883 g_return_if_fail (!application->priv->is_registered);
1884
1885 application->priv->flags = flags;
1886
1887 g_object_notify (G_OBJECT (application), "flags");
1888 }
1889 }
1890
1891 /**
1892 * g_application_get_resource_base_path:
1893 * @application: a #GApplication
1894 *
1895 * Gets the resource base path of @application.
1896 *
1897 * See g_application_set_resource_base_path() for more information.
1898 *
1899 * Returns: (nullable): the base resource path, if one is set
1900 *
1901 * Since: 2.42
1902 */
1903 const gchar *
g_application_get_resource_base_path(GApplication * application)1904 g_application_get_resource_base_path (GApplication *application)
1905 {
1906 g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
1907
1908 return application->priv->resource_path;
1909 }
1910
1911 /**
1912 * g_application_set_resource_base_path:
1913 * @application: a #GApplication
1914 * @resource_path: (nullable): the resource path to use
1915 *
1916 * Sets (or unsets) the base resource path of @application.
1917 *
1918 * The path is used to automatically load various [application
1919 * resources][gresource] such as menu layouts and action descriptions.
1920 * The various types of resources will be found at fixed names relative
1921 * to the given base path.
1922 *
1923 * By default, the resource base path is determined from the application
1924 * ID by prefixing '/' and replacing each '.' with '/'. This is done at
1925 * the time that the #GApplication object is constructed. Changes to
1926 * the application ID after that point will not have an impact on the
1927 * resource base path.
1928 *
1929 * As an example, if the application has an ID of "org.example.app" then
1930 * the default resource base path will be "/org/example/app". If this
1931 * is a #GtkApplication (and you have not manually changed the path)
1932 * then Gtk will then search for the menus of the application at
1933 * "/org/example/app/gtk/menus.ui".
1934 *
1935 * See #GResource for more information about adding resources to your
1936 * application.
1937 *
1938 * You can disable automatic resource loading functionality by setting
1939 * the path to %NULL.
1940 *
1941 * Changing the resource base path once the application is running is
1942 * not recommended. The point at which the resource path is consulted
1943 * for forming paths for various purposes is unspecified. When writing
1944 * a sub-class of #GApplication you should either set the
1945 * #GApplication:resource-base-path property at construction time, or call
1946 * this function during the instance initialization. Alternatively, you
1947 * can call this function in the #GApplicationClass.startup virtual function,
1948 * before chaining up to the parent implementation.
1949 *
1950 * Since: 2.42
1951 */
1952 void
g_application_set_resource_base_path(GApplication * application,const gchar * resource_path)1953 g_application_set_resource_base_path (GApplication *application,
1954 const gchar *resource_path)
1955 {
1956 g_return_if_fail (G_IS_APPLICATION (application));
1957 g_return_if_fail (resource_path == NULL || g_str_has_prefix (resource_path, "/"));
1958
1959 if (g_strcmp0 (application->priv->resource_path, resource_path) != 0)
1960 {
1961 g_free (application->priv->resource_path);
1962
1963 application->priv->resource_path = g_strdup (resource_path);
1964
1965 g_object_notify (G_OBJECT (application), "resource-base-path");
1966 }
1967 }
1968
1969 /**
1970 * g_application_get_inactivity_timeout:
1971 * @application: a #GApplication
1972 *
1973 * Gets the current inactivity timeout for the application.
1974 *
1975 * This is the amount of time (in milliseconds) after the last call to
1976 * g_application_release() before the application stops running.
1977 *
1978 * Returns: the timeout, in milliseconds
1979 *
1980 * Since: 2.28
1981 **/
1982 guint
g_application_get_inactivity_timeout(GApplication * application)1983 g_application_get_inactivity_timeout (GApplication *application)
1984 {
1985 g_return_val_if_fail (G_IS_APPLICATION (application), 0);
1986
1987 return application->priv->inactivity_timeout;
1988 }
1989
1990 /**
1991 * g_application_set_inactivity_timeout:
1992 * @application: a #GApplication
1993 * @inactivity_timeout: the timeout, in milliseconds
1994 *
1995 * Sets the current inactivity timeout for the application.
1996 *
1997 * This is the amount of time (in milliseconds) after the last call to
1998 * g_application_release() before the application stops running.
1999 *
2000 * This call has no side effects of its own. The value set here is only
2001 * used for next time g_application_release() drops the use count to
2002 * zero. Any timeouts currently in progress are not impacted.
2003 *
2004 * Since: 2.28
2005 **/
2006 void
g_application_set_inactivity_timeout(GApplication * application,guint inactivity_timeout)2007 g_application_set_inactivity_timeout (GApplication *application,
2008 guint inactivity_timeout)
2009 {
2010 g_return_if_fail (G_IS_APPLICATION (application));
2011
2012 if (application->priv->inactivity_timeout != inactivity_timeout)
2013 {
2014 application->priv->inactivity_timeout = inactivity_timeout;
2015
2016 g_object_notify (G_OBJECT (application), "inactivity-timeout");
2017 }
2018 }
2019 /* Read-only property getters (is registered, is remote, dbus stuff) {{{1 */
2020 /**
2021 * g_application_get_is_registered:
2022 * @application: a #GApplication
2023 *
2024 * Checks if @application is registered.
2025 *
2026 * An application is registered if g_application_register() has been
2027 * successfully called.
2028 *
2029 * Returns: %TRUE if @application is registered
2030 *
2031 * Since: 2.28
2032 **/
2033 gboolean
g_application_get_is_registered(GApplication * application)2034 g_application_get_is_registered (GApplication *application)
2035 {
2036 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2037
2038 return application->priv->is_registered;
2039 }
2040
2041 /**
2042 * g_application_get_is_remote:
2043 * @application: a #GApplication
2044 *
2045 * Checks if @application is remote.
2046 *
2047 * If @application is remote then it means that another instance of
2048 * application already exists (the 'primary' instance). Calls to
2049 * perform actions on @application will result in the actions being
2050 * performed by the primary instance.
2051 *
2052 * The value of this property cannot be accessed before
2053 * g_application_register() has been called. See
2054 * g_application_get_is_registered().
2055 *
2056 * Returns: %TRUE if @application is remote
2057 *
2058 * Since: 2.28
2059 **/
2060 gboolean
g_application_get_is_remote(GApplication * application)2061 g_application_get_is_remote (GApplication *application)
2062 {
2063 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2064 g_return_val_if_fail (application->priv->is_registered, FALSE);
2065
2066 return application->priv->is_remote;
2067 }
2068
2069 /**
2070 * g_application_get_dbus_connection:
2071 * @application: a #GApplication
2072 *
2073 * Gets the #GDBusConnection being used by the application, or %NULL.
2074 *
2075 * If #GApplication is using its D-Bus backend then this function will
2076 * return the #GDBusConnection being used for uniqueness and
2077 * communication with the desktop environment and other instances of the
2078 * application.
2079 *
2080 * If #GApplication is not using D-Bus then this function will return
2081 * %NULL. This includes the situation where the D-Bus backend would
2082 * normally be in use but we were unable to connect to the bus.
2083 *
2084 * This function must not be called before the application has been
2085 * registered. See g_application_get_is_registered().
2086 *
2087 * Returns: (transfer none): a #GDBusConnection, or %NULL
2088 *
2089 * Since: 2.34
2090 **/
2091 GDBusConnection *
g_application_get_dbus_connection(GApplication * application)2092 g_application_get_dbus_connection (GApplication *application)
2093 {
2094 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2095 g_return_val_if_fail (application->priv->is_registered, FALSE);
2096
2097 return g_application_impl_get_dbus_connection (application->priv->impl);
2098 }
2099
2100 /**
2101 * g_application_get_dbus_object_path:
2102 * @application: a #GApplication
2103 *
2104 * Gets the D-Bus object path being used by the application, or %NULL.
2105 *
2106 * If #GApplication is using its D-Bus backend then this function will
2107 * return the D-Bus object path that #GApplication is using. If the
2108 * application is the primary instance then there is an object published
2109 * at this path. If the application is not the primary instance then
2110 * the result of this function is undefined.
2111 *
2112 * If #GApplication is not using D-Bus then this function will return
2113 * %NULL. This includes the situation where the D-Bus backend would
2114 * normally be in use but we were unable to connect to the bus.
2115 *
2116 * This function must not be called before the application has been
2117 * registered. See g_application_get_is_registered().
2118 *
2119 * Returns: the object path, or %NULL
2120 *
2121 * Since: 2.34
2122 **/
2123 const gchar *
g_application_get_dbus_object_path(GApplication * application)2124 g_application_get_dbus_object_path (GApplication *application)
2125 {
2126 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2127 g_return_val_if_fail (application->priv->is_registered, FALSE);
2128
2129 return g_application_impl_get_dbus_object_path (application->priv->impl);
2130 }
2131
2132
2133 /* Register {{{1 */
2134 /**
2135 * g_application_register:
2136 * @application: a #GApplication
2137 * @cancellable: (nullable): a #GCancellable, or %NULL
2138 * @error: a pointer to a NULL #GError, or %NULL
2139 *
2140 * Attempts registration of the application.
2141 *
2142 * This is the point at which the application discovers if it is the
2143 * primary instance or merely acting as a remote for an already-existing
2144 * primary instance. This is implemented by attempting to acquire the
2145 * application identifier as a unique bus name on the session bus using
2146 * GDBus.
2147 *
2148 * If there is no application ID or if %G_APPLICATION_NON_UNIQUE was
2149 * given, then this process will always become the primary instance.
2150 *
2151 * Due to the internal architecture of GDBus, method calls can be
2152 * dispatched at any time (even if a main loop is not running). For
2153 * this reason, you must ensure that any object paths that you wish to
2154 * register are registered before calling this function.
2155 *
2156 * If the application has already been registered then %TRUE is
2157 * returned with no work performed.
2158 *
2159 * The #GApplication::startup signal is emitted if registration succeeds
2160 * and @application is the primary instance (including the non-unique
2161 * case).
2162 *
2163 * In the event of an error (such as @cancellable being cancelled, or a
2164 * failure to connect to the session bus), %FALSE is returned and @error
2165 * is set appropriately.
2166 *
2167 * Note: the return value of this function is not an indicator that this
2168 * instance is or is not the primary instance of the application. See
2169 * g_application_get_is_remote() for that.
2170 *
2171 * Returns: %TRUE if registration succeeded
2172 *
2173 * Since: 2.28
2174 **/
2175 gboolean
g_application_register(GApplication * application,GCancellable * cancellable,GError ** error)2176 g_application_register (GApplication *application,
2177 GCancellable *cancellable,
2178 GError **error)
2179 {
2180 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2181
2182 if (!application->priv->is_registered)
2183 {
2184 if (application->priv->id == NULL)
2185 application->priv->flags |= G_APPLICATION_NON_UNIQUE;
2186
2187 application->priv->impl =
2188 g_application_impl_register (application, application->priv->id,
2189 application->priv->flags,
2190 application->priv->actions,
2191 &application->priv->remote_actions,
2192 cancellable, error);
2193
2194 if (application->priv->impl == NULL)
2195 return FALSE;
2196
2197 application->priv->is_remote = application->priv->remote_actions != NULL;
2198 application->priv->is_registered = TRUE;
2199
2200 g_object_notify (G_OBJECT (application), "is-registered");
2201
2202 if (!application->priv->is_remote)
2203 {
2204 g_signal_emit (application, g_application_signals[SIGNAL_STARTUP], 0);
2205
2206 if (!application->priv->did_startup)
2207 g_critical ("GApplication subclass '%s' failed to chain up on"
2208 " ::startup (from start of override function)",
2209 G_OBJECT_TYPE_NAME (application));
2210 }
2211 }
2212
2213 return TRUE;
2214 }
2215
2216 /* Hold/release {{{1 */
2217 /**
2218 * g_application_hold:
2219 * @application: a #GApplication
2220 *
2221 * Increases the use count of @application.
2222 *
2223 * Use this function to indicate that the application has a reason to
2224 * continue to run. For example, g_application_hold() is called by GTK+
2225 * when a toplevel window is on the screen.
2226 *
2227 * To cancel the hold, call g_application_release().
2228 **/
2229 void
g_application_hold(GApplication * application)2230 g_application_hold (GApplication *application)
2231 {
2232 g_return_if_fail (G_IS_APPLICATION (application));
2233
2234 if (application->priv->inactivity_timeout_id)
2235 {
2236 g_source_remove (application->priv->inactivity_timeout_id);
2237 application->priv->inactivity_timeout_id = 0;
2238 }
2239
2240 application->priv->use_count++;
2241 }
2242
2243 static gboolean
inactivity_timeout_expired(gpointer data)2244 inactivity_timeout_expired (gpointer data)
2245 {
2246 GApplication *application = G_APPLICATION (data);
2247
2248 application->priv->inactivity_timeout_id = 0;
2249
2250 return G_SOURCE_REMOVE;
2251 }
2252
2253
2254 /**
2255 * g_application_release:
2256 * @application: a #GApplication
2257 *
2258 * Decrease the use count of @application.
2259 *
2260 * When the use count reaches zero, the application will stop running.
2261 *
2262 * Never call this function except to cancel the effect of a previous
2263 * call to g_application_hold().
2264 **/
2265 void
g_application_release(GApplication * application)2266 g_application_release (GApplication *application)
2267 {
2268 g_return_if_fail (G_IS_APPLICATION (application));
2269 g_return_if_fail (application->priv->use_count > 0);
2270
2271 application->priv->use_count--;
2272
2273 if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
2274 application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
2275 inactivity_timeout_expired, application);
2276 }
2277
2278 /* Activate, Open {{{1 */
2279 /**
2280 * g_application_activate:
2281 * @application: a #GApplication
2282 *
2283 * Activates the application.
2284 *
2285 * In essence, this results in the #GApplication::activate signal being
2286 * emitted in the primary instance.
2287 *
2288 * The application must be registered before calling this function.
2289 *
2290 * Since: 2.28
2291 **/
2292 void
g_application_activate(GApplication * application)2293 g_application_activate (GApplication *application)
2294 {
2295 g_return_if_fail (G_IS_APPLICATION (application));
2296 g_return_if_fail (application->priv->is_registered);
2297
2298 if (application->priv->is_remote)
2299 g_application_impl_activate (application->priv->impl,
2300 get_platform_data (application, NULL));
2301
2302 else
2303 g_signal_emit (application, g_application_signals[SIGNAL_ACTIVATE], 0);
2304 }
2305
2306 /**
2307 * g_application_open:
2308 * @application: a #GApplication
2309 * @files: (array length=n_files): an array of #GFiles to open
2310 * @n_files: the length of the @files array
2311 * @hint: a hint (or ""), but never %NULL
2312 *
2313 * Opens the given files.
2314 *
2315 * In essence, this results in the #GApplication::open signal being emitted
2316 * in the primary instance.
2317 *
2318 * @n_files must be greater than zero.
2319 *
2320 * @hint is simply passed through to the ::open signal. It is
2321 * intended to be used by applications that have multiple modes for
2322 * opening files (eg: "view" vs "edit", etc). Unless you have a need
2323 * for this functionality, you should use "".
2324 *
2325 * The application must be registered before calling this function
2326 * and it must have the %G_APPLICATION_HANDLES_OPEN flag set.
2327 *
2328 * Since: 2.28
2329 **/
2330 void
g_application_open(GApplication * application,GFile ** files,gint n_files,const gchar * hint)2331 g_application_open (GApplication *application,
2332 GFile **files,
2333 gint n_files,
2334 const gchar *hint)
2335 {
2336 g_return_if_fail (G_IS_APPLICATION (application));
2337 g_return_if_fail (application->priv->flags &
2338 G_APPLICATION_HANDLES_OPEN);
2339 g_return_if_fail (application->priv->is_registered);
2340
2341 if (application->priv->is_remote)
2342 g_application_impl_open (application->priv->impl,
2343 files, n_files, hint,
2344 get_platform_data (application, NULL));
2345
2346 else
2347 g_signal_emit (application, g_application_signals[SIGNAL_OPEN],
2348 0, files, n_files, hint);
2349 }
2350
2351 /* Run {{{1 */
2352 /**
2353 * g_application_run:
2354 * @application: a #GApplication
2355 * @argc: the argc from main() (or 0 if @argv is %NULL)
2356 * @argv: (array length=argc) (element-type filename) (nullable):
2357 * the argv from main(), or %NULL
2358 *
2359 * Runs the application.
2360 *
2361 * This function is intended to be run from main() and its return value
2362 * is intended to be returned by main(). Although you are expected to pass
2363 * the @argc, @argv parameters from main() to this function, it is possible
2364 * to pass %NULL if @argv is not available or commandline handling is not
2365 * required. Note that on Windows, @argc and @argv are ignored, and
2366 * g_win32_get_command_line() is called internally (for proper support
2367 * of Unicode commandline arguments).
2368 *
2369 * #GApplication will attempt to parse the commandline arguments. You
2370 * can add commandline flags to the list of recognised options by way of
2371 * g_application_add_main_option_entries(). After this, the
2372 * #GApplication::handle-local-options signal is emitted, from which the
2373 * application can inspect the values of its #GOptionEntrys.
2374 *
2375 * #GApplication::handle-local-options is a good place to handle options
2376 * such as `--version`, where an immediate reply from the local process is
2377 * desired (instead of communicating with an already-running instance).
2378 * A #GApplication::handle-local-options handler can stop further processing
2379 * by returning a non-negative value, which then becomes the exit status of
2380 * the process.
2381 *
2382 * What happens next depends on the flags: if
2383 * %G_APPLICATION_HANDLES_COMMAND_LINE was specified then the remaining
2384 * commandline arguments are sent to the primary instance, where a
2385 * #GApplication::command-line signal is emitted. Otherwise, the
2386 * remaining commandline arguments are assumed to be a list of files.
2387 * If there are no files listed, the application is activated via the
2388 * #GApplication::activate signal. If there are one or more files, and
2389 * %G_APPLICATION_HANDLES_OPEN was specified then the files are opened
2390 * via the #GApplication::open signal.
2391 *
2392 * If you are interested in doing more complicated local handling of the
2393 * commandline then you should implement your own #GApplication subclass
2394 * and override local_command_line(). In this case, you most likely want
2395 * to return %TRUE from your local_command_line() implementation to
2396 * suppress the default handling. See
2397 * [gapplication-example-cmdline2.c][gapplication-example-cmdline2]
2398 * for an example.
2399 *
2400 * If, after the above is done, the use count of the application is zero
2401 * then the exit status is returned immediately. If the use count is
2402 * non-zero then the default main context is iterated until the use count
2403 * falls to zero, at which point 0 is returned.
2404 *
2405 * If the %G_APPLICATION_IS_SERVICE flag is set, then the service will
2406 * run for as much as 10 seconds with a use count of zero while waiting
2407 * for the message that caused the activation to arrive. After that,
2408 * if the use count falls to zero the application will exit immediately,
2409 * except in the case that g_application_set_inactivity_timeout() is in
2410 * use.
2411 *
2412 * This function sets the prgname (g_set_prgname()), if not already set,
2413 * to the basename of argv[0].
2414 *
2415 * Much like g_main_loop_run(), this function will acquire the main context
2416 * for the duration that the application is running.
2417 *
2418 * Since 2.40, applications that are not explicitly flagged as services
2419 * or launchers (ie: neither %G_APPLICATION_IS_SERVICE or
2420 * %G_APPLICATION_IS_LAUNCHER are given as flags) will check (from the
2421 * default handler for local_command_line) if "--gapplication-service"
2422 * was given in the command line. If this flag is present then normal
2423 * commandline processing is interrupted and the
2424 * %G_APPLICATION_IS_SERVICE flag is set. This provides a "compromise"
2425 * solution whereby running an application directly from the commandline
2426 * will invoke it in the normal way (which can be useful for debugging)
2427 * while still allowing applications to be D-Bus activated in service
2428 * mode. The D-Bus service file should invoke the executable with
2429 * "--gapplication-service" as the sole commandline argument. This
2430 * approach is suitable for use by most graphical applications but
2431 * should not be used from applications like editors that need precise
2432 * control over when processes invoked via the commandline will exit and
2433 * what their exit status will be.
2434 *
2435 * Returns: the exit status
2436 *
2437 * Since: 2.28
2438 **/
2439 int
g_application_run(GApplication * application,int argc,char ** argv)2440 g_application_run (GApplication *application,
2441 int argc,
2442 char **argv)
2443 {
2444 gchar **arguments;
2445 int status;
2446 GMainContext *context;
2447 gboolean acquired_context;
2448
2449 g_return_val_if_fail (G_IS_APPLICATION (application), 1);
2450 g_return_val_if_fail (argc == 0 || argv != NULL, 1);
2451 g_return_val_if_fail (!application->priv->must_quit_now, 1);
2452
2453 #ifdef G_OS_WIN32
2454 {
2455 gint new_argc = 0;
2456
2457 arguments = g_win32_get_command_line ();
2458
2459 /*
2460 * CommandLineToArgvW(), which is called by g_win32_get_command_line(),
2461 * pulls in the whole command line that is used to call the program. This is
2462 * fine in cases where the program is a .exe program, but in the cases where the
2463 * program is a called via a script, such as PyGObject's gtk-demo.py, which is normally
2464 * called using 'python gtk-demo.py' on Windows, the program name (argv[0])
2465 * returned by g_win32_get_command_line() will not be the argv[0] that ->local_command_line()
2466 * would expect, causing the program to fail with "This application can not open files."
2467 */
2468 new_argc = g_strv_length (arguments);
2469
2470 if (new_argc > argc)
2471 {
2472 gint i;
2473
2474 for (i = 0; i < new_argc - argc; i++)
2475 g_free (arguments[i]);
2476
2477 memmove (&arguments[0],
2478 &arguments[new_argc - argc],
2479 sizeof (arguments[0]) * (argc + 1));
2480 }
2481 }
2482 #elif defined(__APPLE__)
2483 {
2484 gint i, j;
2485
2486 /*
2487 * OSX adds an unexpected parameter on the format -psn_X_XXXXXX
2488 * when opening the application using Launch Services. In order
2489 * to avoid that GOption fails to parse this parameter we just
2490 * skip it if it was provided.
2491 * See: https://gitlab.gnome.org/GNOME/glib/issues/1784
2492 */
2493 arguments = g_new (gchar *, argc + 1);
2494 for (i = 0, j = 0; i < argc; i++)
2495 {
2496 if (!g_str_has_prefix (argv[i], "-psn_"))
2497 {
2498 arguments[j] = g_strdup (argv[i]);
2499 j++;
2500 }
2501 }
2502 arguments[j] = NULL;
2503 }
2504 #else
2505 {
2506 gint i;
2507
2508 arguments = g_new (gchar *, argc + 1);
2509 for (i = 0; i < argc; i++)
2510 arguments[i] = g_strdup (argv[i]);
2511 arguments[i] = NULL;
2512 }
2513 #endif
2514
2515 if (g_get_prgname () == NULL && argc > 0)
2516 {
2517 gchar *prgname;
2518
2519 prgname = g_path_get_basename (argv[0]);
2520 g_set_prgname (prgname);
2521 g_free (prgname);
2522 }
2523
2524 context = g_main_context_default ();
2525 acquired_context = g_main_context_acquire (context);
2526 g_return_val_if_fail (acquired_context, 0);
2527
2528 if (!G_APPLICATION_GET_CLASS (application)
2529 ->local_command_line (application, &arguments, &status))
2530 {
2531 GError *error = NULL;
2532
2533 if (!g_application_register (application, NULL, &error))
2534 {
2535 g_printerr ("Failed to register: %s\n", error->message);
2536 g_error_free (error);
2537 return 1;
2538 }
2539
2540 g_application_call_command_line (application, (const gchar **) arguments, NULL, &status);
2541 }
2542
2543 g_strfreev (arguments);
2544
2545 if (application->priv->flags & G_APPLICATION_IS_SERVICE &&
2546 application->priv->is_registered &&
2547 !application->priv->use_count &&
2548 !application->priv->inactivity_timeout_id)
2549 {
2550 application->priv->inactivity_timeout_id =
2551 g_timeout_add (10000, inactivity_timeout_expired, application);
2552 }
2553
2554 while (application->priv->use_count || application->priv->inactivity_timeout_id)
2555 {
2556 if (application->priv->must_quit_now)
2557 break;
2558
2559 g_main_context_iteration (context, TRUE);
2560 status = 0;
2561 }
2562
2563 if (application->priv->is_registered && !application->priv->is_remote)
2564 {
2565 g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
2566
2567 if (!application->priv->did_shutdown)
2568 g_critical ("GApplication subclass '%s' failed to chain up on"
2569 " ::shutdown (from end of override function)",
2570 G_OBJECT_TYPE_NAME (application));
2571 }
2572
2573 if (application->priv->impl)
2574 {
2575 g_application_impl_flush (application->priv->impl);
2576 g_application_impl_destroy (application->priv->impl);
2577 application->priv->impl = NULL;
2578 }
2579
2580 g_settings_sync ();
2581
2582 if (!application->priv->must_quit_now)
2583 while (g_main_context_iteration (context, FALSE))
2584 ;
2585
2586 g_main_context_release (context);
2587
2588 return status;
2589 }
2590
2591 static gchar **
g_application_list_actions(GActionGroup * action_group)2592 g_application_list_actions (GActionGroup *action_group)
2593 {
2594 GApplication *application = G_APPLICATION (action_group);
2595
2596 g_return_val_if_fail (application->priv->is_registered, NULL);
2597
2598 if (application->priv->remote_actions != NULL)
2599 return g_action_group_list_actions (G_ACTION_GROUP (application->priv->remote_actions));
2600
2601 else if (application->priv->actions != NULL)
2602 return g_action_group_list_actions (application->priv->actions);
2603
2604 else
2605 /* empty string array */
2606 return g_new0 (gchar *, 1);
2607 }
2608
2609 static gboolean
g_application_query_action(GActionGroup * group,const gchar * action_name,gboolean * enabled,const GVariantType ** parameter_type,const GVariantType ** state_type,GVariant ** state_hint,GVariant ** state)2610 g_application_query_action (GActionGroup *group,
2611 const gchar *action_name,
2612 gboolean *enabled,
2613 const GVariantType **parameter_type,
2614 const GVariantType **state_type,
2615 GVariant **state_hint,
2616 GVariant **state)
2617 {
2618 GApplication *application = G_APPLICATION (group);
2619
2620 g_return_val_if_fail (application->priv->is_registered, FALSE);
2621
2622 if (application->priv->remote_actions != NULL)
2623 return g_action_group_query_action (G_ACTION_GROUP (application->priv->remote_actions),
2624 action_name,
2625 enabled,
2626 parameter_type,
2627 state_type,
2628 state_hint,
2629 state);
2630
2631 if (application->priv->actions != NULL)
2632 return g_action_group_query_action (application->priv->actions,
2633 action_name,
2634 enabled,
2635 parameter_type,
2636 state_type,
2637 state_hint,
2638 state);
2639
2640 return FALSE;
2641 }
2642
2643 static void
g_application_change_action_state(GActionGroup * action_group,const gchar * action_name,GVariant * value)2644 g_application_change_action_state (GActionGroup *action_group,
2645 const gchar *action_name,
2646 GVariant *value)
2647 {
2648 GApplication *application = G_APPLICATION (action_group);
2649
2650 g_return_if_fail (application->priv->is_remote ||
2651 application->priv->actions != NULL);
2652 g_return_if_fail (application->priv->is_registered);
2653
2654 if (application->priv->remote_actions)
2655 g_remote_action_group_change_action_state_full (application->priv->remote_actions,
2656 action_name, value, get_platform_data (application, NULL));
2657
2658 else
2659 g_action_group_change_action_state (application->priv->actions, action_name, value);
2660 }
2661
2662 static void
g_application_activate_action(GActionGroup * action_group,const gchar * action_name,GVariant * parameter)2663 g_application_activate_action (GActionGroup *action_group,
2664 const gchar *action_name,
2665 GVariant *parameter)
2666 {
2667 GApplication *application = G_APPLICATION (action_group);
2668
2669 g_return_if_fail (application->priv->is_remote ||
2670 application->priv->actions != NULL);
2671 g_return_if_fail (application->priv->is_registered);
2672
2673 if (application->priv->remote_actions)
2674 g_remote_action_group_activate_action_full (application->priv->remote_actions,
2675 action_name, parameter, get_platform_data (application, NULL));
2676
2677 else
2678 g_action_group_activate_action (application->priv->actions, action_name, parameter);
2679 }
2680
2681 static GAction *
g_application_lookup_action(GActionMap * action_map,const gchar * action_name)2682 g_application_lookup_action (GActionMap *action_map,
2683 const gchar *action_name)
2684 {
2685 GApplication *application = G_APPLICATION (action_map);
2686
2687 g_return_val_if_fail (G_IS_ACTION_MAP (application->priv->actions), NULL);
2688
2689 return g_action_map_lookup_action (G_ACTION_MAP (application->priv->actions), action_name);
2690 }
2691
2692 static void
g_application_add_action(GActionMap * action_map,GAction * action)2693 g_application_add_action (GActionMap *action_map,
2694 GAction *action)
2695 {
2696 GApplication *application = G_APPLICATION (action_map);
2697
2698 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2699
2700 g_action_map_add_action (G_ACTION_MAP (application->priv->actions), action);
2701 }
2702
2703 static void
g_application_remove_action(GActionMap * action_map,const gchar * action_name)2704 g_application_remove_action (GActionMap *action_map,
2705 const gchar *action_name)
2706 {
2707 GApplication *application = G_APPLICATION (action_map);
2708
2709 g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
2710
2711 g_action_map_remove_action (G_ACTION_MAP (application->priv->actions), action_name);
2712 }
2713
2714 static void
g_application_action_group_iface_init(GActionGroupInterface * iface)2715 g_application_action_group_iface_init (GActionGroupInterface *iface)
2716 {
2717 iface->list_actions = g_application_list_actions;
2718 iface->query_action = g_application_query_action;
2719 iface->change_action_state = g_application_change_action_state;
2720 iface->activate_action = g_application_activate_action;
2721 }
2722
2723 static void
g_application_action_map_iface_init(GActionMapInterface * iface)2724 g_application_action_map_iface_init (GActionMapInterface *iface)
2725 {
2726 iface->lookup_action = g_application_lookup_action;
2727 iface->add_action = g_application_add_action;
2728 iface->remove_action = g_application_remove_action;
2729 }
2730
2731 /* Default Application {{{1 */
2732
2733 static GApplication *default_app;
2734
2735 /**
2736 * g_application_get_default:
2737 *
2738 * Returns the default #GApplication instance for this process.
2739 *
2740 * Normally there is only one #GApplication per process and it becomes
2741 * the default when it is created. You can exercise more control over
2742 * this by using g_application_set_default().
2743 *
2744 * If there is no default application then %NULL is returned.
2745 *
2746 * Returns: (transfer none): the default application for this process, or %NULL
2747 *
2748 * Since: 2.32
2749 **/
2750 GApplication *
g_application_get_default(void)2751 g_application_get_default (void)
2752 {
2753 return default_app;
2754 }
2755
2756 /**
2757 * g_application_set_default:
2758 * @application: (nullable): the application to set as default, or %NULL
2759 *
2760 * Sets or unsets the default application for the process, as returned
2761 * by g_application_get_default().
2762 *
2763 * This function does not take its own reference on @application. If
2764 * @application is destroyed then the default application will revert
2765 * back to %NULL.
2766 *
2767 * Since: 2.32
2768 **/
2769 void
g_application_set_default(GApplication * application)2770 g_application_set_default (GApplication *application)
2771 {
2772 default_app = application;
2773 }
2774
2775 /**
2776 * g_application_quit:
2777 * @application: a #GApplication
2778 *
2779 * Immediately quits the application.
2780 *
2781 * Upon return to the mainloop, g_application_run() will return,
2782 * calling only the 'shutdown' function before doing so.
2783 *
2784 * The hold count is ignored.
2785 * Take care if your code has called g_application_hold() on the application and
2786 * is therefore still expecting it to exist.
2787 * (Note that you may have called g_application_hold() indirectly, for example
2788 * through gtk_application_add_window().)
2789 *
2790 * The result of calling g_application_run() again after it returns is
2791 * unspecified.
2792 *
2793 * Since: 2.32
2794 **/
2795 void
g_application_quit(GApplication * application)2796 g_application_quit (GApplication *application)
2797 {
2798 g_return_if_fail (G_IS_APPLICATION (application));
2799
2800 application->priv->must_quit_now = TRUE;
2801 }
2802
2803 /**
2804 * g_application_mark_busy:
2805 * @application: a #GApplication
2806 *
2807 * Increases the busy count of @application.
2808 *
2809 * Use this function to indicate that the application is busy, for instance
2810 * while a long running operation is pending.
2811 *
2812 * The busy state will be exposed to other processes, so a session shell will
2813 * use that information to indicate the state to the user (e.g. with a
2814 * spinner).
2815 *
2816 * To cancel the busy indication, use g_application_unmark_busy().
2817 *
2818 * Since: 2.38
2819 **/
2820 void
g_application_mark_busy(GApplication * application)2821 g_application_mark_busy (GApplication *application)
2822 {
2823 gboolean was_busy;
2824
2825 g_return_if_fail (G_IS_APPLICATION (application));
2826
2827 was_busy = (application->priv->busy_count > 0);
2828 application->priv->busy_count++;
2829
2830 if (!was_busy)
2831 {
2832 g_application_impl_set_busy_state (application->priv->impl, TRUE);
2833 g_object_notify (G_OBJECT (application), "is-busy");
2834 }
2835 }
2836
2837 /**
2838 * g_application_unmark_busy:
2839 * @application: a #GApplication
2840 *
2841 * Decreases the busy count of @application.
2842 *
2843 * When the busy count reaches zero, the new state will be propagated
2844 * to other processes.
2845 *
2846 * This function must only be called to cancel the effect of a previous
2847 * call to g_application_mark_busy().
2848 *
2849 * Since: 2.38
2850 **/
2851 void
g_application_unmark_busy(GApplication * application)2852 g_application_unmark_busy (GApplication *application)
2853 {
2854 g_return_if_fail (G_IS_APPLICATION (application));
2855 g_return_if_fail (application->priv->busy_count > 0);
2856
2857 application->priv->busy_count--;
2858
2859 if (application->priv->busy_count == 0)
2860 {
2861 g_application_impl_set_busy_state (application->priv->impl, FALSE);
2862 g_object_notify (G_OBJECT (application), "is-busy");
2863 }
2864 }
2865
2866 /**
2867 * g_application_get_is_busy:
2868 * @application: a #GApplication
2869 *
2870 * Gets the application's current busy state, as set through
2871 * g_application_mark_busy() or g_application_bind_busy_property().
2872 *
2873 * Returns: %TRUE if @application is currenty marked as busy
2874 *
2875 * Since: 2.44
2876 */
2877 gboolean
g_application_get_is_busy(GApplication * application)2878 g_application_get_is_busy (GApplication *application)
2879 {
2880 g_return_val_if_fail (G_IS_APPLICATION (application), FALSE);
2881
2882 return application->priv->busy_count > 0;
2883 }
2884
2885 /* Notifications {{{1 */
2886
2887 /**
2888 * g_application_send_notification:
2889 * @application: a #GApplication
2890 * @id: (nullable): id of the notification, or %NULL
2891 * @notification: the #GNotification to send
2892 *
2893 * Sends a notification on behalf of @application to the desktop shell.
2894 * There is no guarantee that the notification is displayed immediately,
2895 * or even at all.
2896 *
2897 * Notifications may persist after the application exits. It will be
2898 * D-Bus-activated when the notification or one of its actions is
2899 * activated.
2900 *
2901 * Modifying @notification after this call has no effect. However, the
2902 * object can be reused for a later call to this function.
2903 *
2904 * @id may be any string that uniquely identifies the event for the
2905 * application. It does not need to be in any special format. For
2906 * example, "new-message" might be appropriate for a notification about
2907 * new messages.
2908 *
2909 * If a previous notification was sent with the same @id, it will be
2910 * replaced with @notification and shown again as if it was a new
2911 * notification. This works even for notifications sent from a previous
2912 * execution of the application, as long as @id is the same string.
2913 *
2914 * @id may be %NULL, but it is impossible to replace or withdraw
2915 * notifications without an id.
2916 *
2917 * If @notification is no longer relevant, it can be withdrawn with
2918 * g_application_withdraw_notification().
2919 *
2920 * Since: 2.40
2921 */
2922 void
g_application_send_notification(GApplication * application,const gchar * id,GNotification * notification)2923 g_application_send_notification (GApplication *application,
2924 const gchar *id,
2925 GNotification *notification)
2926 {
2927 gchar *generated_id = NULL;
2928
2929 g_return_if_fail (G_IS_APPLICATION (application));
2930 g_return_if_fail (G_IS_NOTIFICATION (notification));
2931 g_return_if_fail (g_application_get_is_registered (application));
2932 g_return_if_fail (!g_application_get_is_remote (application));
2933
2934 if (application->priv->notifications == NULL)
2935 application->priv->notifications = g_notification_backend_new_default (application);
2936
2937 if (id == NULL)
2938 {
2939 generated_id = g_dbus_generate_guid ();
2940 id = generated_id;
2941 }
2942
2943 g_notification_backend_send_notification (application->priv->notifications, id, notification);
2944
2945 g_free (generated_id);
2946 }
2947
2948 /**
2949 * g_application_withdraw_notification:
2950 * @application: a #GApplication
2951 * @id: id of a previously sent notification
2952 *
2953 * Withdraws a notification that was sent with
2954 * g_application_send_notification().
2955 *
2956 * This call does nothing if a notification with @id doesn't exist or
2957 * the notification was never sent.
2958 *
2959 * This function works even for notifications sent in previous
2960 * executions of this application, as long @id is the same as it was for
2961 * the sent notification.
2962 *
2963 * Note that notifications are dismissed when the user clicks on one
2964 * of the buttons in a notification or triggers its default action, so
2965 * there is no need to explicitly withdraw the notification in that case.
2966 *
2967 * Since: 2.40
2968 */
2969 void
g_application_withdraw_notification(GApplication * application,const gchar * id)2970 g_application_withdraw_notification (GApplication *application,
2971 const gchar *id)
2972 {
2973 g_return_if_fail (G_IS_APPLICATION (application));
2974 g_return_if_fail (id != NULL);
2975
2976 if (application->priv->notifications == NULL)
2977 application->priv->notifications = g_notification_backend_new_default (application);
2978
2979 g_notification_backend_withdraw_notification (application->priv->notifications, id);
2980 }
2981
2982 /* Busy binding {{{1 */
2983
2984 typedef struct
2985 {
2986 GApplication *app;
2987 gboolean is_busy;
2988 } GApplicationBusyBinding;
2989
2990 static void
g_application_busy_binding_destroy(gpointer data,GClosure * closure)2991 g_application_busy_binding_destroy (gpointer data,
2992 GClosure *closure)
2993 {
2994 GApplicationBusyBinding *binding = data;
2995
2996 if (binding->is_busy)
2997 g_application_unmark_busy (binding->app);
2998
2999 g_object_unref (binding->app);
3000 g_slice_free (GApplicationBusyBinding, binding);
3001 }
3002
3003 static void
g_application_notify_busy_binding(GObject * object,GParamSpec * pspec,gpointer user_data)3004 g_application_notify_busy_binding (GObject *object,
3005 GParamSpec *pspec,
3006 gpointer user_data)
3007 {
3008 GApplicationBusyBinding *binding = user_data;
3009 gboolean is_busy;
3010
3011 g_object_get (object, pspec->name, &is_busy, NULL);
3012
3013 if (is_busy && !binding->is_busy)
3014 g_application_mark_busy (binding->app);
3015 else if (!is_busy && binding->is_busy)
3016 g_application_unmark_busy (binding->app);
3017
3018 binding->is_busy = is_busy;
3019 }
3020
3021 /**
3022 * g_application_bind_busy_property:
3023 * @application: a #GApplication
3024 * @object: (type GObject.Object): a #GObject
3025 * @property: the name of a boolean property of @object
3026 *
3027 * Marks @application as busy (see g_application_mark_busy()) while
3028 * @property on @object is %TRUE.
3029 *
3030 * The binding holds a reference to @application while it is active, but
3031 * not to @object. Instead, the binding is destroyed when @object is
3032 * finalized.
3033 *
3034 * Since: 2.44
3035 */
3036 void
g_application_bind_busy_property(GApplication * application,gpointer object,const gchar * property)3037 g_application_bind_busy_property (GApplication *application,
3038 gpointer object,
3039 const gchar *property)
3040 {
3041 guint notify_id;
3042 GQuark property_quark;
3043 GParamSpec *pspec;
3044 GApplicationBusyBinding *binding;
3045 GClosure *closure;
3046
3047 g_return_if_fail (G_IS_APPLICATION (application));
3048 g_return_if_fail (G_IS_OBJECT (object));
3049 g_return_if_fail (property != NULL);
3050
3051 notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
3052 property_quark = g_quark_from_string (property);
3053 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
3054
3055 g_return_if_fail (pspec != NULL && pspec->value_type == G_TYPE_BOOLEAN);
3056
3057 if (g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
3058 notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL) > 0)
3059 {
3060 g_critical ("%s: '%s' is already bound to the busy state of the application", G_STRFUNC, property);
3061 return;
3062 }
3063
3064 binding = g_slice_new (GApplicationBusyBinding);
3065 binding->app = g_object_ref (application);
3066 binding->is_busy = FALSE;
3067
3068 closure = g_cclosure_new (G_CALLBACK (g_application_notify_busy_binding), binding,
3069 g_application_busy_binding_destroy);
3070 g_signal_connect_closure_by_id (object, notify_id, property_quark, closure, FALSE);
3071
3072 /* fetch the initial value */
3073 g_application_notify_busy_binding (object, pspec, binding);
3074 }
3075
3076 /**
3077 * g_application_unbind_busy_property:
3078 * @application: a #GApplication
3079 * @object: (type GObject.Object): a #GObject
3080 * @property: the name of a boolean property of @object
3081 *
3082 * Destroys a binding between @property and the busy state of
3083 * @application that was previously created with
3084 * g_application_bind_busy_property().
3085 *
3086 * Since: 2.44
3087 */
3088 void
g_application_unbind_busy_property(GApplication * application,gpointer object,const gchar * property)3089 g_application_unbind_busy_property (GApplication *application,
3090 gpointer object,
3091 const gchar *property)
3092 {
3093 guint notify_id;
3094 GQuark property_quark;
3095 gulong handler_id;
3096
3097 g_return_if_fail (G_IS_APPLICATION (application));
3098 g_return_if_fail (G_IS_OBJECT (object));
3099 g_return_if_fail (property != NULL);
3100
3101 notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
3102 property_quark = g_quark_from_string (property);
3103
3104 handler_id = g_signal_handler_find (object, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC,
3105 notify_id, property_quark, NULL, g_application_notify_busy_binding, NULL);
3106 if (handler_id == 0)
3107 {
3108 g_critical ("%s: '%s' is not bound to the busy state of the application", G_STRFUNC, property);
3109 return;
3110 }
3111
3112 g_signal_handler_disconnect (object, handler_id);
3113 }
3114
3115 /* Epilogue {{{1 */
3116 /* vim:set foldmethod=marker: */
3117