Update German translation
[glib.git] / gio / tests / basic-application.c
blob94100eb5fe64717cc39c4969adca97d1e94cfbd0
1 #include <gio/gio.h>
2 #include <string.h>
4 static void
5 new_activated (GSimpleAction *action,
6 GVariant *parameter,
7 gpointer user_data)
9 GApplication *app = user_data;
11 g_application_activate (app);
14 static void
15 quit_activated (GSimpleAction *action,
16 GVariant *parameter,
17 gpointer user_data)
19 GApplication *app = user_data;
21 g_application_quit (app);
24 static void
25 action1_activated (GSimpleAction *action,
26 GVariant *parameter,
27 gpointer user_data)
29 g_print ("activate action1\n");
32 static void
33 action2_activated (GSimpleAction *action,
34 GVariant *parameter,
35 gpointer user_data)
37 GVariant *state;
39 state = g_action_get_state (G_ACTION (action));
40 g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
41 g_print ("activate action2 %d\n", !g_variant_get_boolean (state));
42 g_variant_unref (state);
45 static void
46 change_action2 (GSimpleAction *action,
47 GVariant *state,
48 gpointer user_data)
50 g_print ("change action2 %d\n", g_variant_get_boolean (state));
53 static void
54 startup (GApplication *app)
56 static GActionEntry actions[] = {
57 { "new", new_activated, NULL, NULL, NULL },
58 { "quit", quit_activated, NULL, NULL, NULL },
59 { "action1", action1_activated, NULL, NULL, NULL },
60 { "action2", action2_activated, "b", "false", change_action2 }
63 g_action_map_add_action_entries (G_ACTION_MAP (app),
64 actions, G_N_ELEMENTS (actions),
65 app);
68 static void
69 activate (GApplication *application)
71 g_application_hold (application);
72 g_print ("activated\n");
73 g_application_release (application);
76 static void
77 open (GApplication *application,
78 GFile **files,
79 gint n_files,
80 const gchar *hint)
82 gint i;
84 g_application_hold (application);
86 g_print ("open");
87 for (i = 0; i < n_files; i++)
89 gchar *uri = g_file_get_uri (files[i]);
90 g_print (" %s", uri);
91 g_free (uri);
93 g_print ("\n");
95 g_application_release (application);
98 static int
99 command_line (GApplication *application,
100 GApplicationCommandLine *cmdline)
102 gchar **argv;
103 gint argc;
104 gint i;
106 g_application_hold (application);
107 argv = g_application_command_line_get_arguments (cmdline, &argc);
109 if (argc > 1)
111 if (g_strcmp0 (argv[1], "echo") == 0)
113 g_print ("cmdline");
114 for (i = 0; i < argc; i++)
115 g_print (" %s", argv[i]);
116 g_print ("\n");
118 else if (g_strcmp0 (argv[1], "env") == 0)
120 const gchar * const *env;
122 env = g_application_command_line_get_environ (cmdline);
123 g_print ("environment");
124 for (i = 0; env[i]; i++)
125 if (g_str_has_prefix (env[i], "TEST="))
126 g_print (" %s", env[i]);
127 g_print ("\n");
129 else if (g_strcmp0 (argv[1], "getenv") == 0)
131 g_print ("getenv TEST=%s\n", g_application_command_line_getenv (cmdline, "TEST"));
133 else if (g_strcmp0 (argv[1], "print") == 0)
135 g_application_command_line_print (cmdline, "print %s\n", argv[2]);
137 else if (g_strcmp0 (argv[1], "printerr") == 0)
139 g_application_command_line_printerr (cmdline, "printerr %s\n", argv[2]);
141 else if (g_strcmp0 (argv[1], "file") == 0)
143 GFile *file;
145 file = g_application_command_line_create_file_for_arg (cmdline, argv[2]);
146 g_print ("file %s\n", g_file_get_path (file));
147 g_object_unref (file);
149 else if (g_strcmp0 (argv[1], "properties") == 0)
151 gboolean remote;
152 GVariant *data;
154 g_object_get (cmdline,
155 "is-remote", &remote,
156 NULL);
158 data = g_application_command_line_get_platform_data (cmdline);
159 g_assert (remote);
160 g_assert (g_variant_is_of_type (data, G_VARIANT_TYPE ("a{sv}")));
161 g_variant_unref (data);
162 g_print ("properties ok\n");
164 else if (g_strcmp0 (argv[1], "cwd") == 0)
166 g_print ("cwd %s\n", g_application_command_line_get_cwd (cmdline));
168 else if (g_strcmp0 (argv[1], "busy") == 0)
170 g_application_mark_busy (g_application_get_default ());
171 g_print ("busy\n");
173 else if (g_strcmp0 (argv[1], "idle") == 0)
175 g_application_unmark_busy (g_application_get_default ());
176 g_print ("idle\n");
178 else if (g_strcmp0 (argv[1], "stdin") == 0)
180 GInputStream *stream;
182 stream = g_application_command_line_get_stdin (cmdline);
184 g_assert (stream == NULL || G_IS_INPUT_STREAM (stream));
185 g_object_unref (stream);
187 g_print ("stdin ok\n");
189 else
190 g_print ("unexpected command: %s\n", argv[1]);
192 else
193 g_print ("got ./cmd %d\n", g_application_command_line_get_is_remote (cmdline));
195 g_strfreev (argv);
196 g_application_release (application);
198 return 0;
201 static gboolean
202 action_cb (gpointer data)
204 gchar **argv = data;
205 GApplication *app;
206 gchar **actions;
207 gint i;
209 if (g_strcmp0 (argv[1], "./actions") == 0)
211 app = g_application_get_default ();
213 if (g_strcmp0 (argv[2], "list") == 0)
215 g_print ("actions");
216 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
217 for (i = 0; actions[i]; i++)
218 g_print (" %s", actions[i]);
219 g_print ("\n");
220 g_strfreev (actions);
222 else if (g_strcmp0 (argv[2], "activate") == 0)
224 g_action_group_activate_action (G_ACTION_GROUP (app),
225 "action1", NULL);
227 else if (g_strcmp0 (argv[2], "set-state") == 0)
229 g_action_group_change_action_state (G_ACTION_GROUP (app),
230 "action2",
231 g_variant_new_boolean (TRUE));
233 g_application_release (app);
236 return G_SOURCE_REMOVE;
240 main (int argc, char **argv)
242 GApplication *app;
243 int status;
245 app = g_application_new ("org.gtk.TestApplication",
246 G_APPLICATION_SEND_ENVIRONMENT |
247 (g_strcmp0 (argv[1], "./cmd") == 0
248 ? G_APPLICATION_HANDLES_COMMAND_LINE
249 : G_APPLICATION_HANDLES_OPEN));
250 g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
251 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
252 g_signal_connect (app, "open", G_CALLBACK (open), NULL);
253 g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
254 #ifdef STANDALONE
255 g_application_set_inactivity_timeout (app, 10000);
256 #else
257 g_application_set_inactivity_timeout (app, 1000);
258 #endif
260 if (g_strcmp0 (argv[1], "./actions") == 0)
262 g_application_set_inactivity_timeout (app, 0);
263 g_application_hold (app);
264 g_idle_add (action_cb, argv);
267 status = g_application_run (app, argc - 1, argv + 1);
269 g_object_unref (app);
271 g_print ("exit status: %d\n", status);
273 return 0;