[glib/wip/actions: 11/11] GApplication: use GDBusActionGroup



commit 6a1bd6a9b1b06a914838426c3d3ef93ee389638f
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Jul 1 12:52:11 2011 +0100

    GApplication: use GDBusActionGroup
    
    instead of the internal version of the same

 gio/gapplication.c          |   80 ++-------------
 gio/gapplicationimpl-dbus.c |  234 ++-----------------------------------------
 gio/gapplicationimpl.h      |   14 +---
 gio/gio.symbols             |    6 +
 4 files changed, 26 insertions(+), 308 deletions(-)
---
diff --git a/gio/gapplication.c b/gio/gapplication.c
index da63924..9e2056b 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -158,7 +158,7 @@ struct _GApplicationPrivate
   guint              is_registered : 1;
   guint              is_remote : 1;
 
-  GHashTable        *remote_actions;  /* string -> RemoteActionInfo */
+  GActionGroup      *remote_actions;
   GApplicationImpl  *impl;
 };
 
@@ -1343,8 +1343,7 @@ g_application_has_action (GActionGroup *action_group,
   g_return_val_if_fail (application->priv->is_registered, FALSE);
 
   if (application->priv->remote_actions != NULL)
-    return g_hash_table_lookup (application->priv->remote_actions,
-                                action_name) != NULL;
+    return g_action_group_has_action (application->priv->remote_actions, action_name);
 
   return application->priv->actions &&
          g_action_group_has_action (application->priv->actions, action_name);
@@ -1358,23 +1357,7 @@ g_application_list_actions (GActionGroup *action_group)
   g_return_val_if_fail (application->priv->is_registered, NULL);
 
   if (application->priv->remote_actions != NULL)
-    {
-      GHashTableIter iter;
-      gint n, i = 0;
-      gchar **keys;
-      gpointer key;
-
-      n = g_hash_table_size (application->priv->remote_actions);
-      keys = g_new (gchar *, n + 1);
-
-      g_hash_table_iter_init (&iter, application->priv->remote_actions);
-      while (g_hash_table_iter_next (&iter, &key, NULL))
-        keys[i++] = g_strdup (key);
-      g_assert_cmpint (i, ==, n);
-      keys[n] = NULL;
-
-      return keys;
-    }
+    return g_action_group_list_actions (application->priv->remote_actions);
 
   else if (application->priv->actions != NULL)
     return g_action_group_list_actions (application->priv->actions);
@@ -1395,14 +1378,7 @@ g_application_get_action_enabled (GActionGroup *action_group,
   g_return_val_if_fail (application->priv->is_registered, FALSE);
 
   if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
-
-      return info && info->enabled;
-    }
+    return g_action_group_get_action_enabled (application->priv->remote_actions, action_name);
 
   return g_action_group_get_action_enabled (application->priv->actions,
                                             action_name);
@@ -1419,17 +1395,7 @@ g_application_get_action_parameter_type (GActionGroup *action_group,
   g_return_val_if_fail (application->priv->is_registered, NULL);
 
   if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
-
-      if (info)
-        return info->parameter_type;
-      else
-        return NULL;
-    }
+    return g_action_group_get_action_parameter_type (application->priv->remote_actions, action_name);
 
   return g_action_group_get_action_parameter_type (application->priv->actions,
                                                    action_name);
@@ -1446,17 +1412,7 @@ g_application_get_action_state_type (GActionGroup *action_group,
   g_return_val_if_fail (application->priv->is_registered, NULL);
 
   if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
-
-      if (info && info->state)
-        return g_variant_get_type (info->state);
-      else
-        return NULL;
-    }
+    return g_action_group_get_action_state_type (application->priv->remote_actions, action_name);
 
   return g_action_group_get_action_state_type (application->priv->actions,
                                                action_name);
@@ -1473,17 +1429,7 @@ g_application_get_action_state (GActionGroup *action_group,
   g_return_val_if_fail (application->priv->is_registered, NULL);
 
   if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
-
-      if (info && info->state)
-        return g_variant_ref (info->state);
-      else
-        return NULL;
-    }
+    return g_action_group_get_action_state (application->priv->remote_actions, action_name);
 
   return g_action_group_get_action_state (application->priv->actions,
                                           action_name);
@@ -1500,10 +1446,8 @@ g_application_change_action_state (GActionGroup *action_group,
                     application->priv->actions != NULL);
   g_return_if_fail (application->priv->is_registered);
 
-  if (application->priv->is_remote)
-    g_application_impl_change_action_state (application->priv->impl,
-                                            action_name, value,
-                                            get_platform_data (application));
+  if (application->priv->remote_actions)
+    return g_action_group_change_action_state (application->priv->remote_actions, action_name, value);
 
   else
     g_action_group_change_action_state (application->priv->actions,
@@ -1521,10 +1465,8 @@ g_application_activate_action (GActionGroup *action_group,
                     application->priv->actions != NULL);
   g_return_if_fail (application->priv->is_registered);
 
-  if (application->priv->is_remote)
-    g_application_impl_activate_action (application->priv->impl,
-                                        action_name, parameter,
-                                        get_platform_data (application));
+  if (application->priv->remote_actions)
+    return g_action_group_change_action_state (application->priv->remote_actions, action_name, parameter);
 
   else
     g_action_group_activate_action (application->priv->actions,
diff --git a/gio/gapplicationimpl-dbus.c b/gio/gapplicationimpl-dbus.c
index 8aeeec2..57f1aa9 100644
--- a/gio/gapplicationimpl-dbus.c
+++ b/gio/gapplicationimpl-dbus.c
@@ -23,6 +23,7 @@
 
 #include "gactiongroup.h"
 #include "gactiongroupexporter.h"
+#include "gdbusactiongroup.h"
 #include "gapplication.h"
 #include "gfile.h"
 #include "gdbusconnection.h"
@@ -118,9 +119,6 @@ struct _GApplicationImpl
   guint            object_id;
   gboolean         actions_exported;
   gpointer         app;
-
-  GHashTable      *actions;
-  guint            signal_id;
 };
 
 
@@ -253,165 +251,18 @@ g_application_impl_destroy (GApplicationImpl *impl)
   g_slice_free (GApplicationImpl, impl);
 }
 
-static void
-remote_action_info_free (gpointer user_data)
-{
-  RemoteActionInfo *info = user_data;
-
-  g_free (info->name);
-
-  if (info->state)
-    g_variant_unref (info->state);
-
-  if (info->parameter_type)
-    g_variant_type_free (info->parameter_type);
-
-  g_slice_free (RemoteActionInfo, info);
-}
-
-RemoteActionInfo *
-remote_action_info_new_from_iter (GVariantIter *iter)
-{
-  RemoteActionInfo *info;
-  const gchar *param_str;
-  gboolean enabled;
-  GVariant *state;
-  gchar *name;
-
-  if (!g_variant_iter_next (iter, "{&s(bg av)}", &name,
-                            &enabled, &param_str, &state))
-    return NULL;
-
-  info = g_slice_new (RemoteActionInfo);
-  info->name = name;
-  info->enabled = enabled;
-
-  if (g_variant_n_children (state))
-    g_variant_get_child (state, 0, "v", &info->state);
-  else
-    info->state = NULL;
-  g_variant_unref (state);
-
-  if (param_str[0])
-    info->parameter_type = g_variant_type_copy ((GVariantType *) param_str);
-  else
-    info->parameter_type = NULL;
-
-  return info;
-}
-
-static void
-g_application_impl_action_signal (GDBusConnection *connection,
-                                  const gchar     *sender_name,
-                                  const gchar     *object_path,
-                                  const gchar     *interface_name,
-                                  const gchar     *signal_name,
-                                  GVariant        *parameters,
-                                  gpointer         user_data)
-{
-  GApplicationImpl *impl = user_data;
-  GActionGroup *action_group;
-
-  action_group = G_ACTION_GROUP (impl->app);
-
-  if (g_str_equal (signal_name, "Changed") &&
-      g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(asa{sb}a{sv}a{s(bgav)})")))
-    {
-      /* Removes */
-      {
-        GVariantIter *iter;
-        const gchar *name;
-
-        g_variant_get_child (parameters, 0, "as", &iter);
-        while (g_variant_iter_next (iter, "&s", &name))
-          {
-            if (g_hash_table_lookup (impl->actions, name))
-              {
-                g_hash_table_remove (impl->actions, name);
-                g_action_group_action_removed (action_group, name);
-              }
-          }
-        g_variant_iter_free (iter);
-      }
-
-      /* Enable changes */
-      {
-        GVariantIter *iter;
-        const gchar *name;
-        gboolean enabled;
-
-        g_variant_get_child (parameters, 1, "a{sb}", &iter);
-        while (g_variant_iter_next (iter, "{&sb}", &name, &enabled))
-          {
-            RemoteActionInfo *info;
-
-            info = g_hash_table_lookup (impl->actions, name);
-
-            if (info && info->enabled != enabled)
-              {
-                info->enabled = enabled;
-                g_action_group_action_enabled_changed (action_group,
-                                                       name, enabled);
-              }
-          }
-        g_variant_iter_free (iter);
-      }
-
-      /* State changes */
-      {
-        GVariantIter *iter;
-        const gchar *name;
-        GVariant *state;
-
-        g_variant_get_child (parameters, 2, "a{sv}", &iter);
-        while (g_variant_iter_next (iter, "{&sv}", &name, &state))
-          {
-            RemoteActionInfo *info;
-
-            info = g_hash_table_lookup (impl->actions, name);
-
-            if (info && info->state && !g_variant_equal (state, info->state) &&
-                g_variant_is_of_type (state, g_variant_get_type (info->state)))
-              {
-                g_variant_unref (info->state);
-                info->state = g_variant_ref (state);
-
-                g_action_group_action_state_changed (action_group, name, state);
-              }
-
-            g_variant_unref (state);
-          }
-        g_variant_iter_free (iter);
-      }
-
-      /* Additions */
-      {
-        RemoteActionInfo *info;
-        GVariantIter *iter;
-
-        g_variant_get_child (parameters, 3, "a{s(bgav)}", &iter);
-        while ((info = remote_action_info_new_from_iter (iter)))
-          {
-            if (!g_hash_table_lookup (impl->actions, info->name))
-              g_hash_table_replace (impl->actions, info->name, info);
-            else
-              remote_action_info_free (info);
-          }
-      }
-    }
-}
-
 GApplicationImpl *
 g_application_impl_register (GApplication       *application,
                              const gchar        *appid,
                              GApplicationFlags   flags,
-                             GHashTable        **remote_actions,
+                             GActionGroup      **remote_actions,
                              GCancellable       *cancellable,
                              GError            **error)
 {
   const static GDBusInterfaceVTable vtable = {
     g_application_impl_method_call
   };
+  GDBusActionGroup *actions;
   GApplicationImpl *impl;
   GVariant *reply;
   guint32 rval;
@@ -545,22 +396,10 @@ g_application_impl_register (GApplication       *application,
    * This also serves as a mechanism to ensure that the primary exists
    * (ie: DBus service files installed correctly, etc).
    */
-  impl->signal_id =
-    g_dbus_connection_signal_subscribe (impl->session_bus, impl->bus_name,
-                                        "org.gtk.Actions", NULL,
-                                        impl->object_path, NULL,
-                                        G_DBUS_SIGNAL_FLAGS_NONE,
-                                        g_application_impl_action_signal,
-                                        impl, NULL);
-
-  reply = g_dbus_connection_call_sync (impl->session_bus, impl->bus_name,
-                                       impl->object_path, "org.gtk.Actions",
-                                       "DescribeAll", NULL,
-                                       G_VARIANT_TYPE ("(a{s(bgav)})"),
-                                       G_DBUS_CALL_FLAGS_NONE, -1,
-                                       cancellable, error);
-
-  if (reply == NULL)
+  actions = g_dbus_action_group_new_sync (impl->session_bus, impl->bus_name, impl->object_path,
+                                          G_DBUS_ACTION_GROUP_FLAGS_NONE, NULL, error);
+
+  if (actions == NULL)
     {
       /* The primary appears not to exist.  Fail the registration. */
       g_object_unref (impl->session_bus);
@@ -572,24 +411,7 @@ g_application_impl_register (GApplication       *application,
       return NULL;
     }
 
-  /* Create and populate the hashtable */
-  {
-    RemoteActionInfo *info;
-    GVariant *descriptions;
-    GVariantIter iter;
-
-    impl->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                           NULL, remote_action_info_free);
-    descriptions = g_variant_get_child_value (reply, 0);
-    g_variant_iter_init (&iter, descriptions);
-
-    while ((info = remote_action_info_new_from_iter (&iter)))
-      g_hash_table_replace (impl->actions, info->name, info);
-
-    g_variant_unref (descriptions);
-
-    *remote_actions = impl->actions;
-  }
+  *remote_actions = G_ACTION_GROUP (actions);
 
   return impl;
 }
@@ -741,46 +563,6 @@ g_application_impl_command_line (GApplicationImpl  *impl,
 }
 
 void
-g_application_impl_change_action_state (GApplicationImpl *impl,
-                                        const gchar      *action_name,
-                                        GVariant         *value,
-                                        GVariant         *platform_data)
-{
-  g_dbus_connection_call (impl->session_bus,
-                          impl->bus_name,
-                          impl->object_path,
-                          "org.gtk.Actions",
-                          "SetState",
-                          g_variant_new ("(sv a{sv})", action_name,
-                                         value, platform_data),
-                          NULL, 0, -1, NULL, NULL, NULL);
-}
-
-void
-g_application_impl_activate_action (GApplicationImpl *impl,
-                                    const gchar      *action_name,
-                                    GVariant         *parameter,
-                                    GVariant         *platform_data)
-{
-  GVariant *param;
-
-  if (parameter)
-    parameter = g_variant_new_variant (parameter);
-
-  param = g_variant_new_array (G_VARIANT_TYPE_VARIANT,
-                               &parameter, parameter != NULL);
-
-  g_dbus_connection_call (impl->session_bus,
-                          impl->bus_name,
-                          impl->object_path,
-                          "org.gtk.Actions",
-                          "Activate",
-                          g_variant_new ("(s av@a{sv})", action_name,
-                                         param, platform_data),
-                          NULL, 0, -1, NULL, NULL, NULL);
-}
-
-void
 g_application_impl_flush (GApplicationImpl *impl)
 {
   g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
diff --git a/gio/gapplicationimpl.h b/gio/gapplicationimpl.h
index 95f0b76..0072c54 100644
--- a/gio/gapplicationimpl.h
+++ b/gio/gapplicationimpl.h
@@ -18,7 +18,7 @@ G_GNUC_INTERNAL
 GApplicationImpl *      g_application_impl_register                     (GApplication       *application,
                                                                          const gchar        *appid,
                                                                          GApplicationFlags   flags,
-                                                                         GHashTable        **remote_actions,
+                                                                         GActionGroup      **remote_actions,
                                                                          GCancellable       *cancellable,
                                                                          GError            **error);
 
@@ -39,16 +39,4 @@ int                     g_application_impl_command_line                 (GApplic
                                                                          GVariant           *platform_data);
 
 G_GNUC_INTERNAL
-void                    g_application_impl_change_action_state          (GApplicationImpl   *impl,
-                                                                         const gchar        *action_name,
-                                                                         GVariant           *value,
-                                                                         GVariant           *platform_data);
-
-G_GNUC_INTERNAL
-void                    g_application_impl_activate_action              (GApplicationImpl   *impl,
-                                                                         const gchar        *action_name,
-                                                                         GVariant           *parameter,
-                                                                         GVariant           *platform_data);
-
-G_GNUC_INTERNAL
 void                    g_application_impl_flush                        (GApplicationImpl   *impl);
diff --git a/gio/gio.symbols b/gio/gio.symbols
index ffdaf30..8c9717e 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -1401,6 +1401,12 @@ g_simple_action_new
 g_simple_action_new_stateful
 g_simple_action_set_enabled
 g_simple_action_set_state
+g_dbus_action_group_flags_get_type
+g_dbus_action_group_get_type
+g_dbus_action_group_inject
+g_dbus_action_group_new
+g_dbus_action_group_new_finish
+g_dbus_action_group_new_sync
 g_pollable_input_stream_get_type
 g_pollable_input_stream_can_poll
 g_pollable_input_stream_create_source



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]