Fix a typo
[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 **args;
10   gchar **argv;
11   gint argc;
12   gint arg1;
13   gboolean arg2;
14   GOptionContext *context;
15   GOptionEntry entries[] = {
16     { "arg1", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL },
17     { "arg2", 0, 0, G_OPTION_ARG_NONE, &arg2, NULL, NULL },
18     { NULL }
19   };
20   GError *error;
21   gint i;
22
23   args = g_application_command_line_get_arguments (cmdline, &argc);
24
25   /* We have to make an extra copy of the array, since g_option_context_parse()
26    * assumes that it can remove strings from the array without freeing them.
27    */
28   argv = g_new (gchar*, argc + 1);
29   for (i = 0; i <= argc; i++)
30     argv[i] = args[i];
31
32   context = g_option_context_new (NULL);
33   g_option_context_add_main_entries (context, entries, NULL);
34
35   arg1 = 0;
36   arg2 = FALSE;
37   error = NULL;
38   if (!g_option_context_parse (context, &argc, &argv, &error))
39     {
40       g_application_command_line_printerr (cmdline, "%s\n", error->message);
41       g_error_free (error);
42       g_application_command_line_set_exit_status (cmdline, 1);
43     }
44   else
45     {
46       g_application_command_line_print (cmdline, "arg1 is %d and arg2 is %s\n",
47                                         arg1, arg2 ? "TRUE" : "FALSE");
48       g_application_command_line_set_exit_status (cmdline, 0);
49     }
50
51   g_free (argv);
52   g_strfreev (args);
53
54   g_option_context_free (context);
55
56   /* we are done handling this commandline */
57   g_object_unref (cmdline);
58
59   return FALSE;
60 }
61
62 static int
63 command_line (GApplication            *application,
64               GApplicationCommandLine *cmdline)
65 {
66   /* keep the application running until we are done with this commandline */
67   g_application_hold (application);
68
69   g_object_set_data_full (G_OBJECT (cmdline),
70                           "application", application,
71                           (GDestroyNotify)g_application_release);
72
73   g_object_ref (cmdline);
74   g_idle_add (my_cmdline_handler, cmdline);
75
76   return 0;
77 }
78
79 int
80 main (int argc, char **argv)
81 {
82   GApplication *app;
83   int status;
84
85   app = g_application_new ("org.gtk.TestApplication",
86                            G_APPLICATION_HANDLES_COMMAND_LINE);
87   g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
88   g_application_set_inactivity_timeout (app, 10000);
89
90   status = g_application_run (app, argc, argv);
91
92   g_object_unref (app);
93
94   return status;
95 }