Try harder to explain GApplicationCommandline
[platform/upstream/glib.git] / gio / tests / gapplication-example-cmdline3.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 static gboolean
6 my_cmdline_handler (gpointer data)
7 {
8   GApplicationCommandLine *cmdline = data;
9   gchar **argv;
10   gint argc;
11   gint i;
12
13   argv = g_application_command_line_get_arguments (cmdline, &argc);
14
15   g_application_command_line_print (cmdline,
16                                     "This text is written back\n"
17                                     "to stdout of the caller\n");
18
19   for (i = 0; i < argc; i++)
20     g_print ("argument %d: %s\n", i, argv[i]);
21
22   g_strfreev (argv);
23
24   g_application_command_line_set_exit_status (cmdline, 1);
25
26   /* we are done handling this commandline */
27   g_object_unref (cmdline);
28
29   return FALSE;
30 }
31
32 static int
33 command_line (GApplication            *application,
34               GApplicationCommandLine *cmdline)
35 {
36   /* keep the application running until we are done with this commandline */
37   g_application_hold (application);
38
39   g_object_set_data_full (G_OBJECT (cmdline),
40                           "application", application,
41                           (GDestroyNotify)g_application_release);
42
43   g_object_ref (cmdline);
44   g_idle_add (my_cmdline_handler, cmdline);
45
46   return 0;
47 }
48
49 int
50 main (int argc, char **argv)
51 {
52   GApplication *app;
53   int status;
54
55   app = g_application_new ("org.gtk.TestApplication",
56                            G_APPLICATION_HANDLES_COMMAND_LINE);
57   g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
58   g_application_set_inactivity_timeout (app, 10000);
59
60   status = g_application_run (app, argc, argv);
61
62   g_object_unref (app);
63
64   return status;
65 }