Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gapplication-example-actions.c
blob7aaa41ef34e8458403e1f44e5f107834b6baf014
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 static void
6 activate (GApplication *application)
8 g_application_hold (application);
9 g_print ("activated\n");
10 g_application_release (application);
13 static void
14 activate_action (GAction *action,
15 GVariant *parameter,
16 gpointer data)
18 GApplication *application = G_APPLICATION (data);
20 g_application_hold (application);
21 g_print ("action %s activated\n", g_action_get_name (action));
22 g_application_release (application);
25 static void
26 activate_toggle_action (GSimpleAction *action,
27 GVariant *parameter,
28 gpointer data)
30 GApplication *application = G_APPLICATION (data);
31 GVariant *state;
32 gboolean b;
34 g_print ("action %s activated\n", g_action_get_name (G_ACTION (action)));
36 g_application_hold (application);
37 state = g_action_get_state (G_ACTION (action));
38 b = g_variant_get_boolean (state);
39 g_variant_unref (state);
40 g_simple_action_set_state (action, g_variant_new_boolean (!b));
41 g_print ("state change %d -> %d\n", b, !b);
42 g_application_release (application);
45 static void
46 add_actions (GApplication *app)
48 GSimpleAction *action;
50 action = g_simple_action_new ("simple-action", NULL);
51 g_signal_connect (action, "activate", G_CALLBACK (activate_action), app);
52 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
53 g_object_unref (action);
55 action = g_simple_action_new_stateful ("toggle-action", NULL,
56 g_variant_new_boolean (FALSE));
57 g_signal_connect (action, "activate", G_CALLBACK (activate_toggle_action), app);
58 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
59 g_object_unref (action);
62 static void
63 describe_and_activate_action (GActionGroup *group,
64 const gchar *name)
66 const GVariantType *param_type;
67 GVariant *state;
68 gboolean enabled;
69 gchar *tmp;
71 param_type = g_action_group_get_action_parameter_type (group, name);
72 state = g_action_group_get_action_state (group, name);
73 enabled = g_action_group_get_action_enabled (group, name);
75 g_print ("action name: %s\n", name);
76 tmp = param_type ? g_variant_type_dup_string (param_type) : NULL;
77 g_print ("parameter type: %s\n", tmp ? tmp : "<none>");
78 g_free (tmp);
79 g_print ("state type: %s\n",
80 state ? g_variant_get_type_string (state) : "<none>");
81 tmp = state ? g_variant_print (state, FALSE) : NULL;
82 g_print ("state: %s\n", tmp ? tmp : "<none>");
83 g_free (tmp);
84 g_print ("enabled: %s\n", enabled ? "true" : "false");
86 if (state != NULL)
87 g_variant_unref (state);
89 g_action_group_activate_action (group, name, NULL);
92 int
93 main (int argc, char **argv)
95 GApplication *app;
96 int status;
98 app = g_application_new ("org.gtk.TestApplication", 0);
99 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
100 g_application_set_inactivity_timeout (app, 10000);
102 add_actions (app);
104 if (argc > 1 && strcmp (argv[1], "--simple-action") == 0)
106 g_application_register (app, NULL, NULL);
107 describe_and_activate_action (G_ACTION_GROUP (app), "simple-action");
108 exit (0);
110 else if (argc > 1 && strcmp (argv[1], "--toggle-action") == 0)
112 g_application_register (app, NULL, NULL);
113 describe_and_activate_action (G_ACTION_GROUP (app), "toggle-action");
114 exit (0);
117 status = g_application_run (app, argc, argv);
119 g_object_unref (app);
121 return status;