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