Coverage Report

Created: 2026-10-28 06:45

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