[liboobs] Add oobs_object_authenticate() and GError reporting



commit 363ab689dedd524dc0a352090d7d4c701fa3c17a
Author: Milan Bouchet-Valat <nalimilan club fr>
Date:   Sat Jan 16 16:16:09 2010 +0100

    Add oobs_object_authenticate() and GError reporting
    
    The System Tools Backends 2.9.1 support a new authenticate() method called for all modules, with the interface org.freedeskop.SystemToolsBackends.Authentication. Support this via a new oobs_object_authenticate() function that works generically for all OobsObjects. Remove oobs_object_get_authentication_action() which was only used with OobsSelfConfig, since we should not have to deal with PolicyKit directly.
    
    Add general support for GError reporting, which is only used in the new method for now. Update documentation to show it.

 configure.in                    |    2 +-
 doc/reference/liboobs-docs.sgml |    7 +--
 oobs/Makefile.am                |    2 +
 oobs/oobs-object.c              |   80 ++++++++++++++++++++++++++++-----------
 oobs/oobs-object.h              |    5 +-
 oobs/oobs-selfconfig.c          |   10 -----
 oobs/oobs.h                     |    1 +
 7 files changed, 66 insertions(+), 41 deletions(-)
---
diff --git a/configure.in b/configure.in
index 3080b5b..9bf5752 100644
--- a/configure.in
+++ b/configure.in
@@ -33,7 +33,7 @@ AC_SUBST(LT_AGE)
 
 GLIB_REQUIRED=2.14.0
 DBUS_REQUIRED=0.70
-STB_REQUIRED=2.9
+STB_REQUIRED=2.9.1
 HAL_REQUIRED=0.5.9
 
 dnl set gettext stuff
diff --git a/doc/reference/liboobs-docs.sgml b/doc/reference/liboobs-docs.sgml
index da5d26c..2931989 100644
--- a/doc/reference/liboobs-docs.sgml
+++ b/doc/reference/liboobs-docs.sgml
@@ -10,14 +10,11 @@
   </bookinfo>
 
   <chapter>
-    <title>Session management</title>
+    <title>Common API</title>
     <xi:include href="xml/oobs-session.xml"/>
-  </chapter>
-
-  <chapter>
-    <title>Liboobs data management</title>
     <xi:include href="xml/oobs-object.xml"/>
     <xi:include href="xml/oobs-list.xml"/>
+    <xi:include href="xml/oobs-error.xml"/>
   </chapter>
 
   <chapter>
diff --git a/oobs/Makefile.am b/oobs/Makefile.am
index f518033..2158089 100644
--- a/oobs/Makefile.am
+++ b/oobs/Makefile.am
@@ -17,6 +17,7 @@ oobs_public_headers = 	\
 	oobs-session.h		\
 	oobs-object.h		\
 	oobs-list.h		\
+	oobs-error.h		\
 	oobs-defines.h		\
 	oobs-enum-types.h	\
 	oobs-share.h		\
@@ -63,6 +64,7 @@ oobs_built_sources = \
 
 liboobs_1_la_SOURCES = \
 	utils.c				\
+	oobs-error.c			\
 	oobs-session.c			\
 	oobs-object.c			\
 	oobs-list.c			\
diff --git a/oobs/oobs-object.c b/oobs/oobs-object.c
index f3bd73b..2ccb00c 100644
--- a/oobs/oobs-object.c
+++ b/oobs/oobs-object.c
@@ -24,6 +24,8 @@
 #include "oobs-object-private.h"
 #include "oobs-session.h"
 #include "oobs-session-private.h"
+#include "oobs-error.h"
+#include "utils.h"
 
 /**
  * SECTION:oobs-object
@@ -33,7 +35,6 @@
  **/
 
 #define OOBS_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OOBS_TYPE_OBJECT, OobsObjectPrivate))
-#define POLKIT_ACTION "org.freedesktop.systemtoolsbackends.set"
 
 typedef struct _OobsObjectPrivate OobsObjectPrivate;
 typedef struct _OobsObjectAsyncCallbackData OobsObjectAsyncCallbackData;
@@ -86,8 +87,6 @@ static void oobs_object_get_property (GObject       *object,
 				      GValue        *value,
 				      GParamSpec    *pspec);
 
-static const gchar * oobs_object_real_get_authentication_action (OobsObject *object);
-
 static void connect_object_to_session (OobsObject *object);
 
 enum
@@ -120,8 +119,6 @@ oobs_object_class_init (OobsObjectClass *class)
   object_class->set_property = oobs_object_set_property;
   object_class->finalize     = oobs_object_finalize;
 
-  class->get_authentication_action = oobs_object_real_get_authentication_action;
-
   /* some object types don't need to be singletons, they override this */
   class->singleton = TRUE;
 
@@ -345,12 +342,6 @@ oobs_object_get_property (GObject      *object,
     }
 }
 
-static const gchar *
-oobs_object_real_get_authentication_action (OobsObject *object)
-{
-  return POLKIT_ACTION;
-}
-
 DBusMessage*
 _oobs_object_get_dbus_message (OobsObject *object)
 {
@@ -409,7 +400,7 @@ run_message (OobsObject  *object,
 
   if (!oobs_session_get_connected (priv->session))
     {
-      g_warning ("could send message, OobsSession hasn't connected to the bus");
+      g_warning ("Could not send message, OobsSession hasn't connected to the bus");
       return NULL;
     }
 
@@ -902,21 +893,66 @@ oobs_object_ensure_update (OobsObject *object)
 }
 
 /**
- * oobs_object_get_authentication_action:
+ * oobs_object_authenticate:
  * @object: An #OobsObject.
+ * @error: Return location for error or NULL.
  *
- * Returns the PolicyKit action required to be able to modify this object configuration.
+ * Performs a PolicyKit authentication via the backends for the action
+ * required by the given object. User interaction will occur synchronously
+ * if needed.
  *
- * Return Value: string defining the PolicyKit action
- *               required to modify objects in the session.
+ * You may want to check the returned error for %OOBS_ERROR_AUTHENTICATION_CANCELLED,
+ * in which case you should avoid showing an error dialog to the user.
+ *
+ * Return Value: %TRUE if allowed to commit @object, %FALSE otherwise.
  **/
-G_CONST_RETURN gchar *
-oobs_object_get_authentication_action (OobsObject *object)
+gboolean
+oobs_object_authenticate (OobsObject *object,
+                          GError    **error)
 {
-  OobsObjectClass *class;
+  OobsObjectPrivate *priv;
+  DBusConnection    *connection;
+  DBusMessage       *message;
+  DBusMessage       *reply;
+  DBusMessageIter   iter;
+  gboolean result;
+
+  g_return_val_if_fail (OOBS_IS_OBJECT (object), FALSE);
+
+  priv = OOBS_OBJECT_GET_PRIVATE (object);
 
-  g_return_val_if_fail (OOBS_IS_OBJECT (object), NULL);
+  message = dbus_message_new_method_call (OOBS_DBUS_DESTINATION, priv->path,
+                                          "org.freedesktop.SystemToolsBackends.Authentication",
+                                          "authenticate");
+
+  if (!oobs_session_get_connected (priv->session))
+    {
+      g_warning ("Could not send message, OobsSession hasn't connected to the bus");
+      return FALSE;
+    }
+
+  connection = _oobs_session_get_connection_bus (priv->session);
+  reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &priv->dbus_error);
+
+  if (dbus_error_is_set (&priv->dbus_error))
+    {
+      if (dbus_error_has_name (&priv->dbus_error,
+                               "org.freedesktop.SystemToolsBackends.AuthenticationCancelled"))
+	g_set_error_literal (error, OOBS_ERROR,
+	                     OOBS_ERROR_AUTHENTICATION_CANCELLED,
+	                     priv->dbus_error.message);
+      else
+	g_set_error_literal (error, OOBS_ERROR,
+	                     OOBS_ERROR_AUTHENTICATION_FAILED,
+	                     priv->dbus_error.message);
+
+      dbus_error_free (&priv->dbus_error);
+      return FALSE;
+    }
+
+  dbus_message_iter_init (reply, &iter);
+  result = utils_get_boolean (&iter);
+
+  return result;
 
-  class = OOBS_OBJECT_GET_CLASS (object);
-  return class->get_authentication_action (object);
 }
diff --git a/oobs/oobs-object.h b/oobs/oobs-object.h
index d4ee1e4..8f595af 100644
--- a/oobs/oobs-object.h
+++ b/oobs/oobs-object.h
@@ -34,7 +34,6 @@ G_BEGIN_DECLS
 #define OOBS_IS_OBJECT_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((o),    OOBS_TYPE_OBJECT))
 #define OOBS_OBJECT_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o),  OOBS_TYPE_OBJECT, OobsObjectClass))
 
-
 typedef struct _OobsObject      OobsObject;
 typedef struct _OobsObjectClass OobsObjectClass;
 
@@ -53,7 +52,6 @@ struct _OobsObjectClass
   /* virtual methods */
   void (*commit) (OobsObject *object);
   void (*update) (OobsObject *object);
-  const gchar * (*get_authentication_action) (OobsObject *object);
 
   /* signals */
   void (*updated)   (OobsObject *object);
@@ -97,7 +95,8 @@ void        oobs_object_process_requests (OobsObject *object);
 gboolean    oobs_object_has_updated      (OobsObject *object);
 void        oobs_object_ensure_update    (OobsObject *object);
 
-G_CONST_RETURN gchar * oobs_object_get_authentication_action (OobsObject *object);
+gboolean    oobs_object_authenticate (OobsObject *object,
+                                      GError    **error);
 
 
 G_END_DECLS
diff --git a/oobs/oobs-selfconfig.c b/oobs/oobs-selfconfig.c
index 33239f0..d3b52ae 100644
--- a/oobs/oobs-selfconfig.c
+++ b/oobs/oobs-selfconfig.c
@@ -37,7 +37,6 @@
 
 #define SELF_CONFIG_REMOTE_OBJECT "SelfConfig2"
 #define OOBS_SELF_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OOBS_TYPE_SELF_CONFIG, OobsSelfConfigPrivate))
-#define POLICY_KIT_SELF_ACTION "org.freedesktop.systemtoolsbackends.self.set"
 
 typedef struct _OobsSelfConfigPrivate OobsSelfConfigPrivate;
 
@@ -55,8 +54,6 @@ static void oobs_self_config_finalize    (GObject             *object);
 static void oobs_self_config_update     (OobsObject   *object);
 static void oobs_self_config_commit     (OobsObject   *object);
 
-static const gchar * oobs_self_config_get_authentication_action (OobsObject *object);
-
 
 G_DEFINE_TYPE (OobsSelfConfig, oobs_self_config, OOBS_TYPE_OBJECT);
 
@@ -72,7 +69,6 @@ oobs_self_config_class_init (OobsSelfConfigClass *class)
 
   oobs_object_class->commit  = oobs_self_config_commit;
   oobs_object_class->update  = oobs_self_config_update;
-  oobs_object_class->get_authentication_action = oobs_self_config_get_authentication_action;
 
   g_type_class_add_private (object_class,
 			    sizeof (OobsSelfConfigPrivate));
@@ -198,12 +194,6 @@ oobs_self_config_commit (OobsObject *object)
   utils_append_string (&iter, "");
 }
 
-static const gchar *
-oobs_self_config_get_authentication_action (OobsObject *object)
-{
-  return POLICY_KIT_SELF_ACTION;
-}
-
 /**
  * oobs_self_config_get:
  *
diff --git a/oobs/oobs.h b/oobs/oobs.h
index 349bc29..977df6e 100644
--- a/oobs/oobs.h
+++ b/oobs/oobs.h
@@ -24,6 +24,7 @@
 #include <glib-object.h>
 #include <oobs/oobs-object.h>
 #include <oobs/oobs-enum-types.h>
+#include <oobs/oobs-error.h>
 #include <oobs/oobs-session.h>
 #include <oobs/oobs-list.h>
 #include <oobs/oobs-defines.h>



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