GAction: make GObject properties read-only
authorRyan Lortie <desrt@desrt.ca>
Wed, 29 Jun 2011 16:13:58 +0000 (17:13 +0100)
committerRobert Ancell <robert.ancell@canonical.com>
Wed, 29 Jun 2011 16:32:21 +0000 (17:32 +0100)
GAction is a read-only interface (as is visible by the lack of _set() functions
on its API).  The properties on the interface currently force implementors to
support writing of the properties at construct time, however.

Lift that restriction.

Take advantage of this from GSimpleAction by nuking the set_property
function and setting the fields directly in the constructor.

gio/gaction.c
gio/gsimpleaction.c

index 228537b..8789d08 100644 (file)
@@ -77,8 +77,7 @@ g_action_default_init (GActionInterface *iface)
                                                             P_("Action Name"),
                                                             P_("The name used to invoke the action"),
                                                             NULL,
-                                                            G_PARAM_READWRITE |
-                                                            G_PARAM_CONSTRUCT_ONLY |
+                                                            G_PARAM_READABLE |
                                                             G_PARAM_STATIC_STRINGS));
 
   /**
@@ -94,8 +93,7 @@ g_action_default_init (GActionInterface *iface)
                                                            P_("Parameter Type"),
                                                            P_("The type of GVariant passed to activate()"),
                                                            G_TYPE_VARIANT_TYPE,
-                                                           G_PARAM_READWRITE |
-                                                           G_PARAM_CONSTRUCT_ONLY |
+                                                           G_PARAM_READABLE |
                                                            G_PARAM_STATIC_STRINGS));
 
   /**
@@ -113,8 +111,7 @@ g_action_default_init (GActionInterface *iface)
                                                              P_("Enabled"),
                                                              P_("If the action can be activated"),
                                                              TRUE,
-                                                             G_PARAM_CONSTRUCT |
-                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_READABLE |
                                                              G_PARAM_STATIC_STRINGS));
 
   /**
index bf8dd73..d6cf423 100644 (file)
@@ -208,35 +208,6 @@ g_simple_action_activate (GAction  *action,
 }
 
 static void
-g_simple_action_set_property (GObject      *object,
-                              guint         prop_id,
-                              const GValue *value,
-                              GParamSpec   *pspec)
-{
-  GSimpleAction *simple = G_SIMPLE_ACTION (object);
-
-  switch (prop_id)
-    {
-    case PROP_NAME:
-      g_assert (simple->name == NULL);
-      simple->name = g_value_dup_string (value);
-      break;
-
-    case PROP_PARAMETER_TYPE:
-      g_assert (simple->parameter_type == NULL);
-      simple->parameter_type = g_value_dup_boxed (value);
-      break;
-
-    case PROP_ENABLED:
-      g_simple_action_set_enabled (simple, g_value_get_boolean (value));
-      break;
-
-    default:
-      g_assert_not_reached ();
-    }
-}
-
-static void
 g_simple_action_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
@@ -310,7 +281,6 @@ g_simple_action_class_init (GSimpleActionClass *class)
   GObjectClass *object_class = G_OBJECT_CLASS (class);
 
   object_class->get_property = g_simple_action_get_property;
-  object_class->set_property = g_simple_action_set_property;
   object_class->finalize = g_simple_action_finalize;
 
   /**
@@ -397,8 +367,7 @@ g_simple_action_class_init (GSimpleActionClass *class)
                                                         P_("Action Name"),
                                                         P_("The name used to invoke the action"),
                                                         NULL,
-                                                        G_PARAM_READWRITE |
-                                                        G_PARAM_CONSTRUCT_ONLY |
+                                                        G_PARAM_READABLE |
                                                         G_PARAM_STATIC_STRINGS));
 
   /**
@@ -414,8 +383,7 @@ g_simple_action_class_init (GSimpleActionClass *class)
                                                        P_("Parameter Type"),
                                                        P_("The type of GVariant passed to activate()"),
                                                        G_TYPE_VARIANT_TYPE,
-                                                       G_PARAM_READWRITE |
-                                                       G_PARAM_CONSTRUCT_ONLY |
+                                                       G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));
 
   /**
@@ -433,8 +401,7 @@ g_simple_action_class_init (GSimpleActionClass *class)
                                                          P_("Enabled"),
                                                          P_("If the action can be activated"),
                                                          TRUE,
-                                                         G_PARAM_CONSTRUCT |
-                                                         G_PARAM_READWRITE |
+                                                         G_PARAM_READABLE |
                                                          G_PARAM_STATIC_STRINGS));
 
   /**
@@ -516,10 +483,17 @@ GSimpleAction *
 g_simple_action_new (const gchar        *name,
                      const GVariantType *parameter_type)
 {
-  return g_object_new (G_TYPE_SIMPLE_ACTION,
-                       "name", name,
-                       "parameter-type", parameter_type,
-                       NULL);
+  GSimpleAction *simple;
+
+  simple = g_object_new (G_TYPE_SIMPLE_ACTION, NULL);
+  simple->name = g_strdup (name);
+
+  if (parameter_type)
+    simple->parameter_type = g_variant_type_copy (parameter_type);
+
+  simple->enabled = TRUE;
+
+  return simple;
 }
 
 /**
@@ -546,13 +520,15 @@ g_simple_action_new_stateful (const gchar        *name,
 {
   GSimpleAction *simple;
 
-  simple = g_object_new (G_TYPE_SIMPLE_ACTION,
-                         "name", name,
-                         "parameter-type", parameter_type,
-                         NULL);
+  simple = g_object_new (G_TYPE_SIMPLE_ACTION, NULL);
+  simple->name = g_strdup (name);
+
+  if (parameter_type)
+    simple->parameter_type = g_variant_type_copy (parameter_type);
+
+  simple->state = g_variant_ref_sink (state);
 
-  if (state)
-    simple->state = g_variant_ref_sink (state);
+  simple->enabled = TRUE;
 
   return simple;
 }