Tweak GApplication docs
[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   for (i = 0; argv[i]; i++)
34     {
35       if (g_str_has_prefix (argv[i], "--local-"))
36         {
37           g_print ("handling argument %s locally\n", argv[i]);
38           for (j = i + 1; argv[j]; j++)
39             {
40               argv[j - 1] = argv[j];
41               argv[j] = NULL;
42             }
43         }
44     }
45
46   *exit_status = 0;
47
48   return FALSE;
49 }
50
51 typedef GApplication TestApplication;
52 typedef GApplicationClass TestApplicationClass;
53
54 G_DEFINE_TYPE (TestApplication, test_application, G_TYPE_APPLICATION)
55
56 static void
57 test_application_finalize (GObject *object)
58 {
59   G_OBJECT_CLASS (test_application_parent_class)->finalize (object);
60 }
61
62 static void
63 test_application_init (TestApplication *app)
64 {
65 }
66
67 static void
68 test_application_class_init (TestApplicationClass *class)
69 {
70   G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;
71 }
72
73 GApplication *
74 test_application_new (const gchar       *application_id,
75                       GApplicationFlags  flags)
76 {
77   g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
78
79   g_type_init ();
80
81   return g_object_new (test_application_get_type (),
82                        "application-id", application_id,
83                        "flags", flags,
84                        NULL);
85 }
86
87 int
88 main (int argc, char **argv)
89 {
90   GApplication *app;
91   int status;
92
93   app = test_application_new ("org.gtk.TestApplication", 0);
94   g_application_set_inactivity_timeout (app, 10000);
95   g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
96
97   status = g_application_run (app, argc, argv);
98
99   g_object_unref (app);
100
101   return status;
102 }