Clean up improper #includes
[platform/upstream/glib.git] / gio / tests / testapp.c
1 #include <gio/gio.h>
2 #include <gstdio.h>
3 #include <string.h>
4
5 #ifdef G_OS_UNIX
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #endif
9
10 static gboolean action3_added = FALSE;
11
12 static void
13 on_app_action (GApplication *application,
14                const gchar  *action_name,
15                GVariant     *platform_data)
16 {
17   if (strcmp (action_name, "action1") == 0)
18     exit (1);
19   else  if (strcmp (action_name, "action2") == 0)
20     {
21       if (action3_added)
22         g_application_remove_action (application, "action3");
23       else
24         g_application_add_action (application, "action3", "An extra action");
25       action3_added = !action3_added;
26     }
27 }
28
29 static void
30 on_app_activated (GApplication  *application,
31                   GVariant      *args,
32                   GVariant      *platform_data)
33 {
34   GVariantIter iter;
35   const char *key;
36   GVariant *value;
37   char *cwd;
38
39   cwd = g_get_current_dir ();
40   g_variant_iter_init (&iter, platform_data);
41   while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
42     {
43       const char *activate_cwd;
44
45       if (strcmp (key, "cwd") != 0)
46         continue;
47
48       activate_cwd = g_variant_get_bytestring (value);
49       g_assert_cmpstr (cwd, ==, activate_cwd);
50       g_variant_unref (value);
51     }
52
53   g_free (cwd);
54 }
55
56 static gboolean
57 on_monitor_fd_io (GIOChannel *source,
58                   GIOCondition condition,
59                   gpointer data)
60 {
61   exit (0);
62   return FALSE;
63 }
64
65 int
66 main (int argc, char *argv[])
67 {
68   GApplication *app;
69
70 #ifdef G_OS_UNIX
71   {
72     const char *slave_fd_env = g_getenv ("_G_TEST_SLAVE_FD");
73     if (slave_fd_env)
74       {
75         int slave_fd = atoi (slave_fd_env);
76         fcntl (slave_fd, F_SETFD, FD_CLOEXEC);
77         g_io_add_watch (g_io_channel_unix_new (slave_fd), G_IO_HUP | G_IO_ERR,
78                         on_monitor_fd_io, NULL);
79       }
80   }
81 #endif
82
83   app = g_application_unregistered_try_new ("org.gtk.test.app", 
84                                             argc, argv, NULL);
85   if (!(argc > 1 && strcmp (argv[1], "--non-unique") == 0))
86     {
87       if (!g_application_register (app))
88         exit (0);
89     }
90
91   if (g_application_is_remote (app))
92     {
93       g_application_invoke_action (app, "action1", 0);
94     }
95   else
96     {
97       g_application_add_action (app, "action1", "Action1");
98       g_application_add_action (app, "action2", "Action2");
99       g_signal_connect (app, "action-with-data",
100                         G_CALLBACK (on_app_action), NULL);
101       g_signal_connect (app, "prepare-activation",
102                         G_CALLBACK (on_app_activated), NULL);
103       g_application_run (app);
104     }
105
106   return 0;
107 }