| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | #include <gio/gio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | activate (GApplication *application) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |   g_application_hold (application); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  |   g_print ("activated\n"); | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |   g_application_release (application); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | activate_action (GAction  *action, | 
					
						
							|  |  |  |                  GVariant *parameter, | 
					
						
							|  |  |  |                  gpointer  data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |   GApplication *application = G_APPLICATION (data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_application_hold (application); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  |   g_print ("action %s activated\n", g_action_get_name (action)); | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |   g_application_release (application); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2011-06-29 10:11:59 +01:00
										 |  |  | activate_toggle_action (GSimpleAction *action, | 
					
						
							|  |  |  |                         GVariant      *parameter, | 
					
						
							|  |  |  |                         gpointer       data) | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |   GApplication *application = G_APPLICATION (data); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  |   GVariant *state; | 
					
						
							|  |  |  |   gboolean b; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-29 10:11:59 +01:00
										 |  |  |   g_print ("action %s activated\n", g_action_get_name (G_ACTION (action))); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |   g_application_hold (application); | 
					
						
							| 
									
										
										
										
											2011-06-29 10:11:59 +01:00
										 |  |  |   state = g_action_get_state (G_ACTION (action)); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  |   b = g_variant_get_boolean (state); | 
					
						
							|  |  |  |   g_variant_unref (state); | 
					
						
							| 
									
										
										
										
											2011-06-29 10:11:59 +01:00
										 |  |  |   g_simple_action_set_state (action, g_variant_new_boolean (!b)); | 
					
						
							| 
									
										
										
										
											2010-10-31 22:05:20 -04:00
										 |  |  |   g_print ("state change %d -> %d\n", b, !b); | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |   g_application_release (application); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | add_actions (GApplication *app) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GSimpleAction *action; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   action = g_simple_action_new ("simple-action", NULL); | 
					
						
							|  |  |  |   g_signal_connect (action, "activate", G_CALLBACK (activate_action), app); | 
					
						
							| 
									
										
										
										
											2011-12-07 22:31:10 -05:00
										 |  |  |   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action)); | 
					
						
							| 
									
										
										
										
											2010-10-31 20:33:02 -04:00
										 |  |  |   g_object_unref (action); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   action = g_simple_action_new_stateful ("toggle-action", NULL, | 
					
						
							|  |  |  |                                          g_variant_new_boolean (FALSE)); | 
					
						
							|  |  |  |   g_signal_connect (action, "activate", G_CALLBACK (activate_toggle_action), app); | 
					
						
							| 
									
										
										
										
											2011-12-07 22:31:10 -05:00
										 |  |  |   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action)); | 
					
						
							| 
									
										
										
										
											2010-10-31 20:33:02 -04:00
										 |  |  |   g_object_unref (action); | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | describe_and_activate_action (GActionGroup *group, | 
					
						
							|  |  |  |                               const gchar  *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const GVariantType *param_type; | 
					
						
							|  |  |  |   GVariant *state; | 
					
						
							|  |  |  |   gboolean enabled; | 
					
						
							|  |  |  |   gchar *tmp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   param_type = g_action_group_get_action_parameter_type (group, name); | 
					
						
							|  |  |  |   state = g_action_group_get_action_state (group, name); | 
					
						
							|  |  |  |   enabled = g_action_group_get_action_enabled (group, name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_print ("action name:      %s\n", name); | 
					
						
							|  |  |  |   tmp = param_type ? g_variant_type_dup_string (param_type) : NULL; | 
					
						
							|  |  |  |   g_print ("parameter type:   %s\n", tmp ? tmp : "<none>"); | 
					
						
							|  |  |  |   g_free (tmp); | 
					
						
							|  |  |  |   g_print ("state type:       %s\n", | 
					
						
							|  |  |  |            state ? g_variant_get_type_string (state) : "<none>"); | 
					
						
							|  |  |  |   tmp = state ? g_variant_print (state, FALSE) : NULL; | 
					
						
							|  |  |  |   g_print ("state:            %s\n", tmp ? tmp : "<none>"); | 
					
						
							|  |  |  |   g_free (tmp); | 
					
						
							|  |  |  |   g_print ("enabled:          %s\n", enabled ? "true" : "false"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (state != NULL) | 
					
						
							|  |  |  |     g_variant_unref (state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_action_group_activate_action (group, name, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  | int | 
					
						
							|  |  |  | main (int argc, char **argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GApplication *app; | 
					
						
							|  |  |  |   int status; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   app = g_application_new ("org.gtk.TestApplication", 0); | 
					
						
							|  |  |  |   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); | 
					
						
							|  |  |  |   g_application_set_inactivity_timeout (app, 10000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   add_actions (app); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-31 22:05:20 -04:00
										 |  |  |   if (argc > 1 && strcmp (argv[1], "--simple-action") == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_application_register (app, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |       describe_and_activate_action (G_ACTION_GROUP (app), "simple-action"); | 
					
						
							| 
									
										
										
										
											2010-10-31 22:05:20 -04:00
										 |  |  |       exit (0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if (argc > 1 && strcmp (argv[1], "--toggle-action") == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_application_register (app, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2011-05-18 16:12:04 -04:00
										 |  |  |       describe_and_activate_action (G_ACTION_GROUP (app), "toggle-action"); | 
					
						
							| 
									
										
										
										
											2010-10-31 22:05:20 -04:00
										 |  |  |       exit (0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-30 22:19:02 -04:00
										 |  |  |   status = g_application_run (app, argc, argv); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_object_unref (app); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return status; | 
					
						
							|  |  |  | } |