Coverage Report

Created: 2025-07-01 07:09

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