glib/gio/tests/gapplication-example-actions.c
Ryan Lortie 5ff65d8695 Make 4 incompatible changes to the GAction API
This commit represents an API break to GAction in the following ways:

  - the 'set_state' entry in the GActionInterface vtable has been
    renamed to 'change_state'.  The number and order of vtable items has
    not otherwise changed.

  - g_action_set_state() has been renamed to g_action_change_state() to
    match the updated vtable entry.

  - the "state" property of the GAction interface has been changed to
    read-only to reflect the fact that g_action_set_state() no longer
    exists.

  - GSimpleActionClass has been hidden.  GSimpleAction can no longer be
    subclassed.

>> Rationale

g_action_set_state() has never been a true setter in the sense that
calling it will update the value of the "state" property.  It has always
been closer to "request 'state' to be changed to this value" with
semantics defined by the implementor of the interface.  This is why the
equivalent method in GActionGroup had its name changed from 'set' to
'change'.  This change makes the two interfaces more consistent and
removes any implication about the effect that calling set_state() should
have on the 'state' property.

>> Impact

This incompatible API break was undertaken only because I strongly
suspect that it will go entirely unnoticed.  If the break actually
affects anybody, then we will accommodate them (possibly going as far as
to revert this commit entirely).

The virtual table change only impacts implementors of GAction.  I
strongly suspect that this is nobody (except for GSimpleAction).

The hiding of GSimpleActionClass only impacts impacts subclasses of
GSimpleAction.  I strongly suspect that none of these exist.

The changing of the property to be read-only only affects people who
were trying to change the state by using GObject properties.  I strongly
suspect that this is nobody at all.

The removal of the g_action_set_state() call is the most dangerous, but
I still suspect that it will impact nobody outside of GLib.  If anybody
is impacted by this change then, at their request, I will reintroduce
the API as a deprecated alias for g_action_change_state().
2011-06-29 11:38:21 +01:00

129 lines
3.6 KiB
C

#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
static void
activate (GApplication *application)
{
g_application_hold (application);
g_print ("activated\n");
g_application_release (application);
}
static void
activate_action (GAction *action,
GVariant *parameter,
gpointer data)
{
GApplication *application = G_APPLICATION (data);
g_application_hold (application);
g_print ("action %s activated\n", g_action_get_name (action));
g_application_release (application);
}
static void
activate_toggle_action (GSimpleAction *action,
GVariant *parameter,
gpointer data)
{
GApplication *application = G_APPLICATION (data);
GVariant *state;
gboolean b;
g_print ("action %s activated\n", g_action_get_name (G_ACTION (action)));
g_application_hold (application);
state = g_action_get_state (G_ACTION (action));
b = g_variant_get_boolean (state);
g_variant_unref (state);
g_simple_action_set_state (action, g_variant_new_boolean (!b));
g_print ("state change %d -> %d\n", b, !b);
g_application_release (application);
}
static void
add_actions (GApplication *app)
{
GSimpleActionGroup *actions;
GSimpleAction *action;
actions = g_simple_action_group_new ();
action = g_simple_action_new ("simple-action", NULL);
g_signal_connect (action, "activate", G_CALLBACK (activate_action), app);
g_simple_action_group_insert (actions, G_ACTION (action));
g_object_unref (action);
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);
g_simple_action_group_insert (actions, G_ACTION (action));
g_object_unref (action);
g_application_set_action_group (app, G_ACTION_GROUP (actions));
g_object_unref (actions);
}
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);
}
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);
if (argc > 1 && strcmp (argv[1], "--simple-action") == 0)
{
g_application_register (app, NULL, NULL);
describe_and_activate_action (G_ACTION_GROUP (app), "simple-action");
exit (0);
}
else if (argc > 1 && strcmp (argv[1], "--toggle-action") == 0)
{
g_application_register (app, NULL, NULL);
describe_and_activate_action (G_ACTION_GROUP (app), "toggle-action");
exit (0);
}
status = g_application_run (app, argc, argv);
g_object_unref (app);
return status;
}