1#include <gio/gio.h>
2#include <stdlib.h>
3#include <string.h>
4
5static int
6command_line (GApplication *application,
7 GApplicationCommandLine *cmdline)
8{
9 gchar **argv;
10 gint argc;
11 gint i;
12
13 argv = g_application_command_line_get_arguments (cmdline, argc: &argc);
14
15 for (i = 0; i < argc; i++)
16 g_print (format: "handling argument %s remotely\n", argv[i]);
17
18 g_strfreev (str_array: argv);
19
20 return 0;
21}
22
23static gboolean
24test_local_cmdline (GApplication *application,
25 gchar ***arguments,
26 gint *exit_status)
27{
28 gint i, j;
29 gchar **argv;
30
31 argv = *arguments;
32
33 i = 1;
34 while (argv[i])
35 {
36 if (g_str_has_prefix (str: argv[i], prefix: "--local-"))
37 {
38 g_print (format: "handling argument %s locally\n", argv[i]);
39 g_free (mem: argv[i]);
40 for (j = i; argv[j]; j++)
41 argv[j] = argv[j + 1];
42 }
43 else
44 {
45 g_print (format: "not handling argument %s locally\n", argv[i]);
46 i++;
47 }
48 }
49
50 *exit_status = 0;
51
52 return FALSE;
53}
54
55typedef GApplication TestApplication;
56typedef GApplicationClass TestApplicationClass;
57
58static GType test_application_get_type (void);
59G_DEFINE_TYPE (TestApplication, test_application, G_TYPE_APPLICATION)
60
61static void
62test_application_finalize (GObject *object)
63{
64 G_OBJECT_CLASS (test_application_parent_class)->finalize (object);
65}
66
67static void
68test_application_init (TestApplication *app)
69{
70}
71
72static void
73test_application_class_init (TestApplicationClass *class)
74{
75 G_OBJECT_CLASS (class)->finalize = test_application_finalize;
76 G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;
77}
78
79static GApplication *
80test_application_new (const gchar *application_id,
81 GApplicationFlags flags)
82{
83 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
84
85 return g_object_new (object_type: test_application_get_type (),
86 first_property_name: "application-id", application_id,
87 "flags", flags,
88 NULL);
89}
90
91int
92main (int argc, char **argv)
93{
94 GApplication *app;
95 int status;
96
97 app = test_application_new (application_id: "org.gtk.TestApplication", flags: 0);
98 g_application_set_inactivity_timeout (application: app, inactivity_timeout: 10000);
99 g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
100
101 status = g_application_run (application: app, argc, argv);
102
103 g_object_unref (object: app);
104
105 return status;
106}
107

source code of gtk/subprojects/glib/gio/tests/gapplication-example-cmdline2.c