ed8ddcdede471f202316887c3371c32d2d63035c
[platform/upstream/glib.git] / gio / tests / gapplication-example-cmdline2.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 static int
6 command_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);
14
15   for (i = 0; i < argc; i++)
16     g_print ("handling argument %s remotely\n", argv[i]);
17
18   g_strfreev (argv);
19
20   return 0;
21 }
22
23 static gboolean
24 test_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 (argv[i], "--local-"))
37         {
38           g_print ("handling argument %s locally\n", argv[i]);
39           g_free (argv[i]);
40           for (j = i; argv[j]; j++)
41             argv[j] = argv[j + 1];
42         }
43       else
44         {
45           g_print ("not handling argument %s locally\n", argv[i]);
46           i++;
47         }
48     }
49
50   *exit_status = 0;
51
52   return FALSE;
53 }
54
55 typedef GApplication TestApplication;
56 typedef GApplicationClass TestApplicationClass;
57
58 G_DEFINE_TYPE (TestApplication, test_application, G_TYPE_APPLICATION)
59
60 static void
61 test_application_finalize (GObject *object)
62 {
63   G_OBJECT_CLASS (test_application_parent_class)->finalize (object);
64 }
65
66 static void
67 test_application_init (TestApplication *app)
68 {
69 }
70
71 static void
72 test_application_class_init (TestApplicationClass *class)
73 {
74   G_OBJECT_CLASS (class)->finalize = test_application_finalize;
75   G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;
76 }
77
78 GApplication *
79 test_application_new (const gchar       *application_id,
80                       GApplicationFlags  flags)
81 {
82   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
83
84   g_type_init ();
85
86   return g_object_new (test_application_get_type (),
87                        "application-id", application_id,
88                        "flags", flags,
89                        NULL);
90 }
91
92 int
93 main (int argc, char **argv)
94 {
95   GApplication *app;
96   int status;
97
98   app = test_application_new ("org.gtk.TestApplication", 0);
99   g_application_set_inactivity_timeout (app, 10000);
100   g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
101
102   status = g_application_run (app, argc, argv);
103
104   g_object_unref (app);
105
106   return status;
107 }