-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathgapplication.c
More file actions
3320 lines (2909 loc) · 112 KB
/
gapplication.c
File metadata and controls
3320 lines (2909 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2010 Codethink Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
/* Prologue {{{1 */
#include "config.h"
#include "gapplication.h"
#include "gapplicationcommandline.h"
#include "gsimpleactiongroup.h"
#include "gremoteactiongroup.h"
#include "gapplicationimpl.h"
#include "gactiongroup.h"
#include "gactionmap.h"
#include "gsettings.h"
#include "gnotification-private.h"
#include "gnotificationbackend.h"
#include "gdbusutils.h"
#include "gioenumtypes.h"
#include "gioenums.h"
#include "gfile.h"
#include "glib-private.h"
#include "glibintl.h"
#include "gmarshal-internal.h"
#include <string.h>
/**
* GApplication:
*
* `GApplication` is the core class for application support.
*
* A `GApplication` is the foundation of an application. It wraps some
* low-level platform-specific services and is intended to act as the
* foundation for higher-level application classes such as
* `GtkApplication` or `MxApplication`. In general, you should not use
* this class outside of a higher level framework.
*
* `GApplication` provides convenient life-cycle management by maintaining
* a "use count" for the primary application instance. The use count can
* be changed using [method@Gio.Application.hold] and
* [method@Gio.Application.release]. If it drops to zero, the application
* exits. Higher-level classes such as `GtkApplication` employ the use count
* to ensure that the application stays alive as long as it has any opened
* windows.
*
* Another feature that `GApplication` (optionally) provides is process
* uniqueness. Applications can make use of this functionality by
* providing a unique application ID. If given, only one application
* with this ID can be running at a time per session. The session
* concept is platform-dependent, but corresponds roughly to a graphical
* desktop login. When your application is launched again, its
* arguments are passed through platform communication to the already
* running program. The already running instance of the program is
* called the "primary instance"; for non-unique applications this is
* always the current instance. On Linux, the D-Bus session bus
* is used for communication.
*
* The use of `GApplication` differs from some other commonly-used
* uniqueness libraries (such as libunique) in important ways. The
* application is not expected to manually register itself and check
* if it is the primary instance. Instead, the main() function of a
* `GApplication` should do very little more than instantiating the
* application instance, possibly connecting signal handlers, then
* calling [method@Gio.Application.run]. All checks for uniqueness are done
* internally. If the application is the primary instance then the
* startup signal is emitted and the mainloop runs. If the application
* is not the primary instance then a signal is sent to the primary
* instance and [method@Gio.Application.run] promptly returns. See the code
* examples below.
*
* If used, the expected form of an application identifier is the
* same as that of a
* [D-Bus well-known bus name](https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus).
* Examples include: `com.example.MyApp`, `org.example.internal_apps.Calculator`,
* `org._7_zip.Archiver`.
* For details on valid application identifiers, see [func@Gio.Application.id_is_valid].
*
* On Linux, the application identifier is claimed as a well-known bus name
* on the user's session bus. This means that the uniqueness of your
* application is scoped to the current session. It also means that your
* application may provide additional services (through registration of other
* object paths) at that bus name. The registration of these object paths
* should be done with the shared GDBus session bus. Note that due to the
* internal architecture of GDBus, method calls can be dispatched at any time
* (even if a main loop is not running). For this reason, you must ensure that
* any object paths that you wish to register are registered before #GApplication
* attempts to acquire the bus name of your application (which happens in
* [method@Gio.Application.register]). Unfortunately, this means that you cannot
* use [property@Gio.Application:is-remote] to decide if you want to register
* object paths.
*
* `GApplication` also implements the [iface@Gio.ActionGroup] and [iface@Gio.ActionMap]
* interfaces and lets you easily export actions by adding them with
* [method@Gio.ActionMap.add_action]. When invoking an action by calling
* [method@Gio.ActionGroup.activate_action] on the application, it is always
* invoked in the primary instance. The actions are also exported on
* the session bus, and GIO provides the [class@Gio.DBusActionGroup] wrapper to
* conveniently access them remotely. GIO provides a [class@Gio.DBusMenuModel] wrapper
* for remote access to exported [class@Gio.MenuModel]s.
*
* Note: Due to the fact that actions are exported on the session bus,
* using `maybe` parameters is not supported, since D-Bus does not support
* `maybe` types.
*
* There is a number of different entry points into a `GApplication`:
*
* - via 'Activate' (i.e. just starting the application)
*
* - via 'Open' (i.e. opening some files)
*
* - by handling a command-line
*
* - via activating an action
*
* The [signal@Gio.Application::startup] signal lets you handle the application
* initialization for all of these in a single place.
*
* Regardless of which of these entry points is used to start the
* application, `GApplication` passes some ‘platform data’ from the
* launching instance to the primary instance, in the form of a
* [struct@GLib.Variant] dictionary mapping strings to variants. To use platform
* data, override the [vfunc@Gio.Application.before_emit] or
* [vfunc@Gio.Application.after_emit] virtual functions
* in your `GApplication` subclass. When dealing with
* [class@Gio.ApplicationCommandLine] objects, the platform data is
* directly available via [method@Gio.ApplicationCommandLine.get_cwd],
* [method@Gio.ApplicationCommandLine.get_environ] and
* [method@Gio.ApplicationCommandLine.get_platform_data].
*
* As the name indicates, the platform data may vary depending on the
* operating system, but it always includes the current directory (key
* `cwd`), and optionally the environment (ie the set of environment
* variables and their values) of the calling process (key `environ`).
* The environment is only added to the platform data if the
* `G_APPLICATION_SEND_ENVIRONMENT` flag is set. `GApplication` subclasses
* can add their own platform data by overriding the
* [vfunc@Gio.Application.add_platform_data] virtual function. For instance,
* `GtkApplication` adds startup notification data in this way.
*
* To parse commandline arguments you may handle the
* [signal@Gio.Application::command-line] signal or override the
* [vfunc@Gio.Application.local_command_line] virtual function, to parse them in
* either the primary instance or the local instance, respectively.
*
* For an example of opening files with a `GApplication`, see
* [gapplication-example-open.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-open.c).
*
* For an example of using actions with `GApplication`, see
* [gapplication-example-actions.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-actions.c).
*
* For an example of using extra D-Bus hooks with `GApplication`, see
* [gapplication-example-dbushooks.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-dbushooks.c).
*
* Since: 2.28
*/
/**
* GApplicationClass:
* @startup: invoked on the primary instance immediately after registration
* @shutdown: invoked only on the registered primary instance immediately
* after the main loop terminates
* @activate: invoked on the primary instance when an activation occurs
* @open: invoked on the primary instance when there are files to open
* @command_line: invoked on the primary instance when a command-line is
* not handled locally
* @local_command_line: invoked (locally). The virtual function has the chance
* to inspect (and possibly replace) command line arguments. See
* g_application_run() for more information. Also see the
* #GApplication::handle-local-options signal, which is a simpler
* alternative to handling some commandline options locally
* @before_emit: invoked on the primary instance before 'activate', 'open',
* 'command-line' or any action invocation, gets the 'platform data' from
* the calling instance. Must chain up
* @after_emit: invoked on the primary instance after 'activate', 'open',
* 'command-line' or any action invocation, gets the 'platform data' from
* the calling instance. Must chain up
* @add_platform_data: invoked (locally) to add 'platform data' to be sent to
* the primary instance when activating, opening or invoking actions. Must chain up
* @quit_mainloop: Used to be invoked on the primary instance when the use
* count of the application drops to zero (and after any inactivity
* timeout, if requested). Not used anymore since 2.32
* @run_mainloop: Used to be invoked on the primary instance from
* g_application_run() if the use-count is non-zero. Since 2.32,
* GApplication is iterating the main context directly and is not
* using @run_mainloop anymore
* @dbus_register: invoked locally during registration, if the application is
* using its D-Bus backend. You can use this to export extra objects on the
* bus, that need to exist before the application tries to own the bus name.
* The function is passed the #GDBusConnection to to session bus, and the
* object path that #GApplication will use to export its D-Bus API.
* If this function returns %TRUE, registration will proceed; otherwise
* registration will abort. Since: 2.34
* @dbus_unregister: invoked locally during unregistration, if the application
* is using its D-Bus backend. Use this to undo anything done by
* the @dbus_register vfunc. Since: 2.34
* @handle_local_options: invoked locally after the parsing of the commandline
* options has occurred. Since: 2.40
* @name_lost: invoked when another instance is taking over the name. Since: 2.60
*
* Virtual function table for #GApplication.
*
* Since: 2.28
*/
struct _GApplicationPrivate
{
GApplicationFlags flags;
gchar *id;
gchar *version;
gchar *resource_path;
GActionGroup *actions;
guint inactivity_timeout_id;
guint inactivity_timeout;
guint use_count;
guint busy_count;
guint is_registered : 1;
guint is_remote : 1;
guint did_startup : 1;
guint did_shutdown : 1;
guint must_quit_now : 1;
GRemoteActionGroup *remote_actions;
GApplicationImpl *impl;
GNotificationBackend *notifications;
/* GOptionContext support */
GOptionGroup *main_options;
GSList *option_groups;
GHashTable *packed_options;
gboolean options_parsed;
gchar *parameter_string;
gchar *summary;
gchar *description;
/* Allocated option strings, from g_application_add_main_option() */
GSList *option_strings;
};
enum
{
PROP_NONE,
PROP_APPLICATION_ID,
PROP_VERSION,
PROP_FLAGS,
PROP_RESOURCE_BASE_PATH,
PROP_IS_REGISTERED,
PROP_IS_REMOTE,
PROP_INACTIVITY_TIMEOUT,
PROP_ACTION_GROUP,
PROP_IS_BUSY
};
enum
{
SIGNAL_STARTUP,
SIGNAL_SHUTDOWN,
SIGNAL_ACTIVATE,
SIGNAL_OPEN,
SIGNAL_ACTION,
SIGNAL_COMMAND_LINE,
SIGNAL_HANDLE_LOCAL_OPTIONS,
SIGNAL_NAME_LOST,
NR_SIGNALS
};
static guint g_application_signals[NR_SIGNALS];
static void g_application_action_group_iface_init (GActionGroupInterface *);
static void g_application_action_map_iface_init (GActionMapInterface *);
G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
G_ADD_PRIVATE (GApplication)
G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
/* GApplicationExportedActions {{{1 */
/* We create a subclass of GSimpleActionGroup that implements
* GRemoteActionGroup and deals with the platform data using
* GApplication's before/after_emit vfuncs. This is the action group we
* will be exporting.
*
* We could implement GRemoteActionGroup on GApplication directly, but
* this would be potentially extremely confusing to have exposed as part
* of the public API of GApplication. We certainly don't want anyone in
* the same process to be calling these APIs...
*/
typedef GSimpleActionGroupClass GApplicationExportedActionsClass;
typedef struct
{
GSimpleActionGroup parent_instance;
GApplication *application;
} GApplicationExportedActions;
static GType g_application_exported_actions_get_type (void);
static void g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GApplicationExportedActions, g_application_exported_actions, G_TYPE_SIMPLE_ACTION_GROUP,
G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_application_exported_actions_iface_init))
static void
g_application_exported_actions_activate_action_full (GRemoteActionGroup *remote,
const gchar *action_name,
GVariant *parameter,
GVariant *platform_data)
{
GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
G_APPLICATION_GET_CLASS (exported->application)
->before_emit (exported->application, platform_data);
g_action_group_activate_action (G_ACTION_GROUP (exported), action_name, parameter);
G_APPLICATION_GET_CLASS (exported->application)
->after_emit (exported->application, platform_data);
}
static void
g_application_exported_actions_change_action_state_full (GRemoteActionGroup *remote,
const gchar *action_name,
GVariant *value,
GVariant *platform_data)
{
GApplicationExportedActions *exported = (GApplicationExportedActions *) remote;
G_APPLICATION_GET_CLASS (exported->application)
->before_emit (exported->application, platform_data);
g_action_group_change_action_state (G_ACTION_GROUP (exported), action_name, value);
G_APPLICATION_GET_CLASS (exported->application)
->after_emit (exported->application, platform_data);
}
static void
g_application_exported_actions_init (GApplicationExportedActions *actions)
{
}
static void
g_application_exported_actions_iface_init (GRemoteActionGroupInterface *iface)
{
iface->activate_action_full = g_application_exported_actions_activate_action_full;
iface->change_action_state_full = g_application_exported_actions_change_action_state_full;
}
static void
g_application_exported_actions_class_init (GApplicationExportedActionsClass *class)
{
}
static GActionGroup *
g_application_exported_actions_new (GApplication *application)
{
GApplicationExportedActions *actions;
actions = g_object_new (g_application_exported_actions_get_type (), NULL);
actions->application = application;
return G_ACTION_GROUP (actions);
}
/* Command line option handling {{{1 */
static void
free_option_entry (gpointer data)
{
GOptionEntry *entry = data;
switch (entry->arg)
{
case G_OPTION_ARG_STRING:
case G_OPTION_ARG_FILENAME:
g_free (*(gchar **) entry->arg_data);
break;
case G_OPTION_ARG_STRING_ARRAY:
case G_OPTION_ARG_FILENAME_ARRAY:
g_strfreev (*(gchar ***) entry->arg_data);
break;
default:
/* most things require no free... */
break;
}
/* ...except for the space that we allocated for it ourselves */
g_free (entry->arg_data);
g_slice_free (GOptionEntry, entry);
}
static void
g_application_pack_option_entries (GApplication *application,
GVariantDict *dict)
{
GHashTableIter iter;
gpointer item;
g_hash_table_iter_init (&iter, application->priv->packed_options);
while (g_hash_table_iter_next (&iter, NULL, &item))
{
GOptionEntry *entry = item;
GVariant *value = NULL;
switch (entry->arg)
{
case G_OPTION_ARG_NONE:
if (*(gboolean *) entry->arg_data != 2)
value = g_variant_new_boolean (*(gboolean *) entry->arg_data);
break;
case G_OPTION_ARG_STRING:
if (*(gchar **) entry->arg_data)
value = g_variant_new_string (*(gchar **) entry->arg_data);
break;
case G_OPTION_ARG_INT:
if (*(gint32 *) entry->arg_data)
value = g_variant_new_int32 (*(gint32 *) entry->arg_data);
break;
case G_OPTION_ARG_FILENAME:
if (*(gchar **) entry->arg_data)
value = g_variant_new_bytestring (*(gchar **) entry->arg_data);
break;
case G_OPTION_ARG_STRING_ARRAY:
if (*(gchar ***) entry->arg_data)
value = g_variant_new_strv (*(const gchar ***) entry->arg_data, -1);
break;
case G_OPTION_ARG_FILENAME_ARRAY:
if (*(gchar ***) entry->arg_data)
value = g_variant_new_bytestring_array (*(const gchar ***) entry->arg_data, -1);
break;
case G_OPTION_ARG_DOUBLE:
if (*(gdouble *) entry->arg_data != 0.0)
value = g_variant_new_double (*(gdouble *) entry->arg_data);
break;
case G_OPTION_ARG_INT64:
if (*(gint64 *) entry->arg_data)
value = g_variant_new_int64 (*(gint64 *) entry->arg_data);
break;
default:
g_assert_not_reached ();
}
if (value)
g_variant_dict_insert_value (dict, entry->long_name, value);
}
}
static GVariantDict *
g_application_parse_command_line (GApplication *application,
gchar ***arguments,
gboolean *print_version,
GError **error)
{
gboolean become_service = FALSE;
gchar *app_id = NULL;
gboolean replace = FALSE;
gboolean version = FALSE;
GVariantDict *dict = NULL;
GOptionContext *context;
GOptionGroup *gapplication_group;
/* Due to the memory management of GOptionGroup we can only parse
* options once. That's because once you add a group to the
* GOptionContext there is no way to get it back again. This is fine:
* local_command_line() should never get invoked more than once
* anyway. Add a sanity check just to be sure.
*/
g_return_val_if_fail (!application->priv->options_parsed, NULL);
context = g_option_context_new (application->priv->parameter_string);
g_option_context_set_summary (context, application->priv->summary);
g_option_context_set_description (context, application->priv->description);
gapplication_group = g_option_group_new ("gapplication",
_("GApplication Options:"), _("Show GApplication options"),
NULL, NULL);
g_option_group_set_translation_domain (gapplication_group, GETTEXT_PACKAGE);
g_option_context_add_group (context, gapplication_group);
/* If the application has not registered local options and it has
* G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
* their primary instance commandline handler may want to deal with
* the arguments. We must therefore ignore them.
*
* We must also ignore --help in this case since some applications
* will try to handle this from the remote side. See #737869.
*/
if (application->priv->main_options == NULL && (application->priv->flags & G_APPLICATION_HANDLES_COMMAND_LINE))
{
g_option_context_set_ignore_unknown_options (context, TRUE);
g_option_context_set_help_enabled (context, FALSE);
}
/* Add the main option group, if it exists */
if (application->priv->main_options)
{
/* This consumes the main_options */
g_option_context_set_main_group (context, application->priv->main_options);
application->priv->main_options = NULL;
}
/* Add any other option groups if they exist. Adding them to the
* context will consume them, so we free the list as we go...
*/
while (application->priv->option_groups)
{
g_option_context_add_group (context, application->priv->option_groups->data);
application->priv->option_groups = g_slist_delete_link (application->priv->option_groups,
application->priv->option_groups);
}
/* In the case that we are not explicitly marked as a service or a
* launcher then we want to add the "--gapplication-service" option to
* allow the process to be made into a service.
*/
if ((application->priv->flags & (G_APPLICATION_IS_SERVICE | G_APPLICATION_IS_LAUNCHER)) == 0)
{
GOptionEntry entries[] = {
{ "gapplication-service", '\0', 0, G_OPTION_ARG_NONE, &become_service,
N_("Enter GApplication service mode (use from D-Bus service files)"), NULL },
G_OPTION_ENTRY_NULL
};
g_option_group_add_entries (gapplication_group, entries);
}
/* Allow overriding the ID if the application allows it */
if (application->priv->flags & G_APPLICATION_CAN_OVERRIDE_APP_ID)
{
GOptionEntry entries[] = {
{ "gapplication-app-id", '\0', 0, G_OPTION_ARG_STRING, &app_id,
N_("Override the application’s ID"), NULL },
G_OPTION_ENTRY_NULL
};
g_option_group_add_entries (gapplication_group, entries);
}
if (application->priv->version)
{
GOptionEntry entries[] = {
{ "version", '\0', 0, G_OPTION_ARG_NONE, &version,
N_("Print the application version"), NULL },
G_OPTION_ENTRY_NULL
};
g_option_group_add_entries (gapplication_group, entries);
}
/* Allow replacing if the application allows it */
if (application->priv->flags & G_APPLICATION_ALLOW_REPLACEMENT)
{
GOptionEntry entries[] = {
{ "gapplication-replace", '\0', 0, G_OPTION_ARG_NONE, &replace,
N_("Replace the running instance"), NULL },
G_OPTION_ENTRY_NULL
};
g_option_group_add_entries (gapplication_group, entries);
}
/* Now we parse... */
if (!g_option_context_parse_strv (context, arguments, error))
goto out;
*print_version = version;
/* Check for --gapplication-service */
if (become_service)
application->priv->flags |= G_APPLICATION_IS_SERVICE;
/* Check for --gapplication-app-id */
if (app_id)
g_application_set_application_id (application, app_id);
/* Check for --gapplication-replace */
if (replace)
application->priv->flags |= G_APPLICATION_REPLACE;
dict = g_variant_dict_new (NULL);
if (application->priv->packed_options)
{
g_application_pack_option_entries (application, dict);
g_hash_table_unref (application->priv->packed_options);
application->priv->packed_options = NULL;
}
out:
/* Make sure we don't run again */
application->priv->options_parsed = TRUE;
g_option_context_free (context);
g_free (app_id);
return dict;
}
static void
add_packed_option (GApplication *application,
GOptionEntry *entry)
{
switch (entry->arg)
{
case G_OPTION_ARG_NONE:
entry->arg_data = g_new (gboolean, 1);
*(gboolean *) entry->arg_data = 2;
break;
case G_OPTION_ARG_INT:
entry->arg_data = g_new0 (gint, 1);
break;
case G_OPTION_ARG_STRING:
case G_OPTION_ARG_FILENAME:
case G_OPTION_ARG_STRING_ARRAY:
case G_OPTION_ARG_FILENAME_ARRAY:
entry->arg_data = g_new0 (gpointer, 1);
break;
case G_OPTION_ARG_INT64:
entry->arg_data = g_new0 (gint64, 1);
break;
case G_OPTION_ARG_DOUBLE:
entry->arg_data = g_new0 (gdouble, 1);
break;
default:
g_return_if_reached ();
}
if (!application->priv->packed_options)
application->priv->packed_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_option_entry);
g_hash_table_insert (application->priv->packed_options,
g_strdup (entry->long_name),
g_slice_dup (GOptionEntry, entry));
}
/**
* g_application_add_main_option_entries:
* @application: a #GApplication
* @entries: (array zero-terminated=1) (element-type GOptionEntry): the
* main options for the application
*
* Adds main option entries to be handled by @application.
*
* This function is comparable to g_option_context_add_main_entries().
*
* After the commandline arguments are parsed, the
* #GApplication::handle-local-options signal will be emitted. At this
* point, the application can inspect the values pointed to by @arg_data
* in the given #GOptionEntrys.
*
* Unlike #GOptionContext, #GApplication supports giving a %NULL
* @arg_data for a non-callback #GOptionEntry. This results in the
* argument in question being packed into a #GVariantDict which is also
* passed to #GApplication::handle-local-options, where it can be
* inspected and modified. If %G_APPLICATION_HANDLES_COMMAND_LINE is
* set, then the resulting dictionary is sent to the primary instance,
* where g_application_command_line_get_options_dict() will return it.
* As it has been passed outside the process at this point, the types of all
* values in the options dict must be checked before being used.
* This "packing" is done according to the type of the argument --
* booleans for normal flags, strings for strings, bytestrings for
* filenames, etc. The packing only occurs if the flag is given (ie: we
* do not pack a "false" #GVariant in the case that a flag is missing).
*
* In general, it is recommended that all commandline arguments are
* parsed locally. The options dictionary should then be used to
* transmit the result of the parsing to the primary instance, where
* g_variant_dict_lookup() can be used. For local options, it is
* possible to either use @arg_data in the usual way, or to consult (and
* potentially remove) the option from the options dictionary.
*
* This function is new in GLib 2.40. Before then, the only real choice
* was to send all of the commandline arguments (options and all) to the
* primary instance for handling. #GApplication ignored them completely
* on the local side. Calling this function "opts in" to the new
* behaviour, and in particular, means that unrecognized options will be
* treated as errors. Unrecognized options have never been ignored when
* %G_APPLICATION_HANDLES_COMMAND_LINE is unset.
*
* If #GApplication::handle-local-options needs to see the list of
* filenames, then the use of %G_OPTION_REMAINING is recommended. If
* @arg_data is %NULL then %G_OPTION_REMAINING can be used as a key into
* the options dictionary. If you do use %G_OPTION_REMAINING then you
* need to handle these arguments for yourself because once they are
* consumed, they will no longer be visible to the default handling
* (which treats them as filenames to be opened).
*
* It is important to use the proper GVariant format when retrieving
* the options with g_variant_dict_lookup():
*
* - for %G_OPTION_ARG_NONE, use `b`
* - for %G_OPTION_ARG_STRING, use `&s`
* - for %G_OPTION_ARG_INT, use `i`
* - for %G_OPTION_ARG_INT64, use `x`
* - for %G_OPTION_ARG_DOUBLE, use `d`
* - for %G_OPTION_ARG_FILENAME, use `^&ay`
* - for %G_OPTION_ARG_STRING_ARRAY, use `^a&s`
* - for %G_OPTION_ARG_FILENAME_ARRAY, use `^a&ay`
*
* Since: 2.40
*/
void
g_application_add_main_option_entries (GApplication *application,
const GOptionEntry *entries)
{
gint i;
g_return_if_fail (G_IS_APPLICATION (application));
g_return_if_fail (entries != NULL);
if (!application->priv->main_options)
{
application->priv->main_options = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
g_option_group_set_translation_domain (application->priv->main_options, NULL);
}
for (i = 0; entries[i].long_name; i++)
{
GOptionEntry my_entries[2] =
{
G_OPTION_ENTRY_NULL,
G_OPTION_ENTRY_NULL
};
my_entries[0] = entries[i];
if (!my_entries[0].arg_data)
add_packed_option (application, &my_entries[0]);
g_option_group_add_entries (application->priv->main_options, my_entries);
}
}
/**
* g_application_add_main_option:
* @application: the #GApplication
* @long_name: the long name of an option used to specify it in a commandline
* @short_name: the short name of an option
* @flags: flags from #GOptionFlags
* @arg: the type of the option, as a #GOptionArg
* @description: the description for the option in `--help` output
* @arg_description: (nullable): the placeholder to use for the extra argument
* parsed by the option in `--help` output
*
* Add an option to be handled by @application.
*
* Calling this function is the equivalent of calling
* g_application_add_main_option_entries() with a single #GOptionEntry
* that has its arg_data member set to %NULL.
*
* The parsed arguments will be packed into a #GVariantDict which
* is passed to #GApplication::handle-local-options. If
* %G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also
* be sent to the primary instance. See
* g_application_add_main_option_entries() for more details.
*
* See #GOptionEntry for more documentation of the arguments.
*
* Since: 2.42
**/
void
g_application_add_main_option (GApplication *application,
const char *long_name,
char short_name,
GOptionFlags flags,
GOptionArg arg,
const char *description,
const char *arg_description)
{
gchar *dup_string;
GOptionEntry my_entry[2] = {
{ NULL, short_name, flags, arg, NULL, NULL, NULL },
G_OPTION_ENTRY_NULL
};
g_return_if_fail (G_IS_APPLICATION (application));
g_return_if_fail (long_name != NULL);
g_return_if_fail (description != NULL);
my_entry[0].long_name = dup_string = g_strdup (long_name);
application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
my_entry[0].description = dup_string = g_strdup (description);
application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
my_entry[0].arg_description = dup_string = g_strdup (arg_description);
application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string);
g_application_add_main_option_entries (application, my_entry);
}
/**
* g_application_add_option_group:
* @application: the #GApplication
* @group: (transfer full): a #GOptionGroup
*
* Adds a #GOptionGroup to the commandline handling of @application.
*
* This function is comparable to g_option_context_add_group().
*
* Unlike g_application_add_main_option_entries(), this function does
* not deal with %NULL @arg_data and never transmits options to the
* primary instance.
*
* The reason for that is because, by the time the options arrive at the
* primary instance, it is typically too late to do anything with them.
* Taking the GTK option group as an example: GTK will already have been
* initialised by the time the #GApplication::command-line handler runs.
* In the case that this is not the first-running instance of the
* application, the existing instance may already have been running for
* a very long time.
*
* This means that the options from #GOptionGroup are only really usable
* in the case that the instance of the application being run is the
* first instance. Passing options like `--display=` or `--gdk-debug=`
* on future runs will have no effect on the existing primary instance.
*
* Calling this function will cause the options in the supplied option
* group to be parsed, but it does not cause you to be "opted in" to the
* new functionality whereby unrecognized options are rejected even if
* %G_APPLICATION_HANDLES_COMMAND_LINE was given.
*
* Since: 2.40
**/
void
g_application_add_option_group (GApplication *application,
GOptionGroup *group)
{
g_return_if_fail (G_IS_APPLICATION (application));
g_return_if_fail (group != NULL);
application->priv->option_groups = g_slist_prepend (application->priv->option_groups, group);
}
/**
* g_application_set_option_context_parameter_string:
* @application: the #GApplication
* @parameter_string: (nullable): a string which is displayed
* in the first line of `--help` output, after the usage summary `programname [OPTION...]`.
*
* Sets the parameter string to be used by the commandline handling of @application.
*
* This function registers the argument to be passed to g_option_context_new()
* when the internal #GOptionContext of @application is created.
*
* See g_option_context_new() for more information about @parameter_string.
*
* Since: 2.56
*/
void
g_application_set_option_context_parameter_string (GApplication *application,
const gchar *parameter_string)
{
g_return_if_fail (G_IS_APPLICATION (application));
g_free (application->priv->parameter_string);
application->priv->parameter_string = g_strdup (parameter_string);
}
/**
* g_application_set_option_context_summary:
* @application: the #GApplication
* @summary: (nullable): a string to be shown in `--help` output
* before the list of options, or %NULL
*
* Adds a summary to the @application option context.
*
* See g_option_context_set_summary() for more information.
*
* Since: 2.56
*/
void
g_application_set_option_context_summary (GApplication *application,
const gchar *summary)
{
g_return_if_fail (G_IS_APPLICATION (application));
g_free (application->priv->summary);
application->priv->summary = g_strdup (summary);
}
/**
* g_application_set_option_context_description:
* @application: the #GApplication
* @description: (nullable): a string to be shown in `--help` output
* after the list of options, or %NULL
*
* Adds a description to the @application option context.
*
* See g_option_context_set_description() for more information.
*
* Since: 2.56
*/
void
g_application_set_option_context_description (GApplication *application,
const gchar *description)
{
g_return_if_fail (G_IS_APPLICATION (application));
g_free (application->priv->description);
application->priv->description = g_strdup (description);
}
/* vfunc defaults {{{1 */
static void
g_application_real_before_emit (GApplication *application,
GVariant *platform_data)
{
}
static void
g_application_real_after_emit (GApplication *application,
GVariant *platform_data)
{
}
static void
g_application_real_startup (GApplication *application)
{
application->priv->did_startup = TRUE;
}
static void
g_application_real_shutdown (GApplication *application)
{
application->priv->did_shutdown = TRUE;
}
static void
g_application_real_activate (GApplication *application)
{
if (!g_signal_has_handler_pending (application,
g_application_signals[SIGNAL_ACTIVATE],
0, TRUE) &&
G_APPLICATION_GET_CLASS (application)->activate == g_application_real_activate)
{
static gboolean warned;
if (warned)
return;
g_warning ("Your application does not implement "
"g_application_activate() and has no handlers connected "
"to the 'activate' signal. It should do one of these.");
warned = TRUE;
}
}
static void
g_application_real_open (GApplication *application,
GFile **files,
gint n_files,
const gchar *hint)
{
if (!g_signal_has_handler_pending (application,
g_application_signals[SIGNAL_OPEN],
0, TRUE) &&
G_APPLICATION_GET_CLASS (application)->open == g_application_real_open)
{
static gboolean warned;
if (warned)
return;