Client library: basic D-Bus backend
authorSam Thursfield <samthursfield@codethink.co.uk>
Tue, 5 Jul 2011 09:39:04 +0000 (10:39 +0100)
committerSam Thursfield <samthursfield@codethink.co.uk>
Tue, 5 Jul 2011 09:39:04 +0000 (10:39 +0100)
Makefile.am
libmoonshot/libmoonshot-common.c
libmoonshot/libmoonshot-common.h
libmoonshot/libmoonshot-dbus.c
libmoonshot/libmoonshot.h

index f76fd31..1f2d4f1 100644 (file)
@@ -121,11 +121,9 @@ libmoonshot_libmoonshot_la_SOURCES += libmoonshot/libmoonshot-dbus.c
 
 bin_PROGRAMS += src/dbus-client
 
-src_dbus_client_SOURCES = \
-        src/dbus-client.vala
-
-src_dbus_client_LDADD = \
-        $(moonshot_LIBS)
+src_dbus_client_SOURCES = src/dbus-client.vala
+src_dbus_client_CPPFLAGS = $(moonshot_CFLAGS) $(AM_CPPFLAGS)
+src_dbus_client_LDADD = $(moonshot_LIBS)
 
 CLEANFILES = $(dbusservice_DATA)
 EXTRA_DIST = $(dbusservice_in_files)
@@ -136,31 +134,19 @@ endif
 bin_PROGRAMS += src/moonshot-webp
 
 src_moonshot_webp_SOURCES = src/moonshot-webp-parser.vala
+src_moonshot_webp_CPPFLAGS = $(moonshot_CFLAGS) $(AM_CPPFLAGS)
 src_moonshot_webp_LDADD = $(moonshot_LIBS)
 
-if IPC_DBUS
-AM_VALAFLAGS += \
-       --pkg dbus-glib-1 \
-       --define=IPC_DBUS
-endif
-
-if IPC_MSRPC
-AM_VALAFLAGS += \
-       --pkg msrpc-1.0 \
-       --define=IPC_MSRPC
-endif
-
 if OS_WIN32
 moonshot_webp_CFLAGS = -mwindows
-AM_VALAFLAGS += --define=OS_WIN32
 endif
 
 
 noinst_PROGRAMS = tests/basic
 
-
-tests_basic_LDADD = ${top_builddir}/libmoonshot/libmoonshot.la
 tests_basic_SOURCES = tests/basic.c
+tests_basic_CPPFLAGS = $(libmoonshot_CFLAGS) $(AM_CPPFLAGS)
+tests_basic_LDADD = ${top_builddir}/libmoonshot/libmoonshot.la
 
 
 if OS_WIN32
index 4161528..bd5f635 100644 (file)
@@ -38,9 +38,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-MoonshotError *_moonshot_error_new (MoonshotErrorCode  code,
-                                    const char        *format,
-                                    ...)
+MoonshotError *moonshot_error_new (MoonshotErrorCode  code,
+                                   const char        *format,
+                                   ...)
 {
     MoonshotError *error;
     int            buffer_size;
index 0b4301e..c466364 100644 (file)
@@ -34,6 +34,6 @@
 
 #include "libmoonshot.h"
 
-MoonshotError *_moonshot_error_new (MoonshotErrorCode  code,
-                                    const char        *format,
-                                    ...);
+MoonshotError *moonshot_error_new (MoonshotErrorCode  code,
+                                   const char        *format,
+                                   ...);
index fcd19b4..04d6da6 100644 (file)
@@ -36,6 +36,7 @@
 #include <dbus/dbus.h>
 
 #include "libmoonshot.h"
+#include "libmoonshot-common.h"
 
 #define MOONSHOT_DBUS_NAME "org.janet.Moonshot"
 #define MOONSHOT_DBUS_PATH "/org/janet/moonshot"
  * dropped the DBus version of the library can be greatly simplified.
  */
 
-typedef struct {
-    char *nai;
-    char *password;
-    char *server_certificate_hash;
-    char *ca_certificate;
-    char *subject_name_constraint;
-    char *subject_alt_name_constraint;
-} MoonshotIdentityData;
-
-static MoonshotIdentityData *moonshot_identity_data_new ()
-{
-    return g_slice_new (MoonshotIdentityData);
-}
-
-static void moonshot_identity_data_free (void *data)
-{
-    g_slice_free (MoonshotIdentityData, data);
-}
+/* Note that ideally this library would not depend on GLib. This would be
+ * possible using libdbus directly and running our own message loop while
+ * waiting for calls.
+ */
 
 static DBusGProxy *moonshot_dbus_proxy = NULL;
 
-GQuark moonshot_error_quark (void)
-{
-    return g_quark_from_static_string ("moonshot-error-quark");
-}
-
-static DBusGProxy *dbus_connect (GError **g_error)
+static DBusGProxy *dbus_connect (MoonshotError **error)
 {
     DBusConnection  *connection;
     DBusError        dbus_error;
     DBusGConnection *g_connection;
     DBusGProxy      *g_proxy;
+    GError          *g_error;
     dbus_bool_t      name_has_owner;
 
-    g_return_val_if_fail (*g_error == NULL, NULL);
+    g_return_val_if_fail (*error == NULL, NULL);
 
     dbus_error_init (&dbus_error);
 
@@ -92,10 +75,9 @@ static DBusGProxy *dbus_connect (GError **g_error)
     connection = dbus_bus_get (DBUS_BUS_SESSION, &dbus_error);
 
     if (dbus_error_is_set (&dbus_error)) {
-        *g_error = g_error_new (MOONSHOT_ERROR,
-                                MOONSHOT_ERROR_DBUS_ERROR,
-                                "DBus error: %s",
-                                dbus_error.message);
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                     "DBus error: %s",
+                                     dbus_error.message);
         dbus_error_free (&dbus_error);
         return NULL;
     }
@@ -105,11 +87,9 @@ static DBusGProxy *dbus_connect (GError **g_error)
                                                &dbus_error);
 
     if (dbus_error_is_set (&dbus_error)) {
-        *g_error = g_error_new (MOONSHOT_ERROR,
-                                MOONSHOT_ERROR_DBUS_ERROR,
-                                "DBus error: %s",
-                                dbus_error.message);
-
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                     "DBus error: %s",
+                                     dbus_error.message);
         dbus_error_free (&dbus_error);
         return NULL;
     }
@@ -124,16 +104,14 @@ static DBusGProxy *dbus_connect (GError **g_error)
         if (dbus_error_is_set (&dbus_error)) {
             if (strcmp (dbus_error.name + 27, "ServiceUnknown") == 0) {
                 /* Missing .service file; the moonshot-ui install is broken */
-                *g_error = g_error_new (MOONSHOT_ERROR,
-                                        MOONSHOT_ERROR_SERVICE_NOT_FOUND,
-                                        "The Moonshot service was not found. "
-                                        "Please make sure that moonshot-ui is "
-                                        "correctly installed.");
+                *error = moonshot_error_new (MOONSHOT_ERROR_UNABLE_TO_START_SERVICE,
+                                             "The Moonshot service was not found. "
+                                             "Please make sure that moonshot-ui is "
+                                             "correctly installed.");
             } else {
-                *g_error = g_error_new (MOONSHOT_ERROR,
-                                        MOONSHOT_ERROR_DBUS_ERROR,
-                                        "DBus error: %s",
-                                        dbus_error.message);
+                *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                             "DBus error: %s",
+                                             dbus_error.message);
             }
             dbus_error_free (&dbus_error);
             return NULL;
@@ -141,183 +119,91 @@ static DBusGProxy *dbus_connect (GError **g_error)
     }
 
     /* Now the service should be running */
+    g_error = NULL;
 
-    g_connection = dbus_g_bus_get (DBUS_BUS_SESSION, g_error);
+    g_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &g_error);
 
-    if (*g_error != NULL)
+    if (g_error != NULL) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                     "DBus error: %s",
+                                     g_error->message);
+        g_error_free (g_error);
         return NULL;
+    }
 
     g_proxy = dbus_g_proxy_new_for_name_owner (g_connection,
                                                MOONSHOT_DBUS_NAME,
                                                MOONSHOT_DBUS_PATH,
                                                MOONSHOT_DBUS_NAME,
-                                               g_error);
+                                               &g_error);
 
-    return g_proxy; 
-}
-
-static void dbus_call_complete_cb (DBusGProxy     *proxy,
-                                   DBusGProxyCall *call_id,
-                                   void           *user_data)
-{
-    GError *error = NULL;
-    GSimpleAsyncResult   *token;
-    MoonshotIdentityData *identity_data;
-    gboolean              success;
-
-    token = G_SIMPLE_ASYNC_RESULT (user_data);
-    identity_data = moonshot_identity_data_new ();
-
-    dbus_g_proxy_end_call (moonshot_dbus_proxy,
-                           call_id,
-                           &error,
-                           G_TYPE_STRING, &identity_data->nai,
-                           G_TYPE_STRING, &identity_data->password,
-                           G_TYPE_STRING, &identity_data->server_certificate_hash,
-                           G_TYPE_STRING, &identity_data->ca_certificate,
-                           G_TYPE_STRING, &identity_data->subject_name_constraint,
-                           G_TYPE_STRING, &identity_data->subject_alt_name_constraint,
-                           G_TYPE_BOOLEAN, &success,
-                           G_TYPE_INVALID);
-
-    if (error != NULL) {
-        g_simple_async_result_set_from_error (token, error);
-    }
-    else
-    if (success == FALSE) {
-        error = g_error_new (MOONSHOT_ERROR,
-                             MOONSHOT_ERROR_NO_IDENTITY_SELECTED,
-                             "No matching identity was available");
-        g_simple_async_result_set_from_error (token, error);
-        g_error_free (error);
-    }
-    else {        
-        g_simple_async_result_set_op_res_gpointer (token,
-                                                   identity_data,
-                                                   moonshot_identity_data_free);
+    if (g_error != NULL) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                     "DBus error: %s",
+                                     g_error->message);
+        g_error_free (g_error);
+        return NULL;
     }
 
-    g_simple_async_result_complete (token);
-    g_object_unref (token);
+    return g_proxy; 
 }
 
-/**
- * moonshot_get_identity:
- * @cancellable: A #GCancellable, or %NULL.
- * @callback: A #GAsyncReadyCallback, which will be called when the
- *            operation completes, fails or is cancelled.
- * @user_data: Data to pass to @callback
- * @nai: Name and issuer constraint for the required identity, or %NULL.
- * @password: Password for the identity, or %NULL.
- * @service: Service constraint for the required identity, or %NULL.
- *
- * This function initiates a call to the Moonshot server to request an ID card.
- * The server will be activated if it is not already running. The user interface
- * will be displayed if there is more than one matching identity and the user 
- * will be asked to select one.
- *
- * When an identity has been selected, or the operation fails or is cancelled,
- * @callback will be run.
- *
- * Note that the actual IPC call may not be made until control returns to the
- * GLib main loop.
- */
-void moonshot_get_identity (GCancellable        *cancellable,
-                            GAsyncReadyCallback  callback,
-                            gpointer             user_data,
-                            const char          *nai,
-                            const char          *password,
-                            const char          *service)
+int moonshot_get_identity (const char     *nai,
+                           const char     *password,
+                           const char     *service,
+                           char          **nai_out,
+                           char          **password_out,
+                           char          **server_certificate_hash_out,
+                           char          **ca_certificate_out,
+                           char          **subject_name_constraint_out,
+                           char          **subject_alt_name_constraint_out,
+                           MoonshotError **error)
 {
-    DBusGProxyCall     *call_id;
-    GSimpleAsyncResult *result; 
-    GError *error = NULL;
+    GError   *g_error = NULL;
+    int success;
 
     if (moonshot_dbus_proxy == NULL)
-        moonshot_dbus_proxy = dbus_connect (&error);
-
-    if (moonshot_dbus_proxy == NULL) {
-        result = g_simple_async_result_new (NULL,
-                                            callback,
-                                            user_data,
-                                            moonshot_get_identity);
-        g_simple_async_result_set_from_error (result, error);
-        g_simple_async_result_complete_in_idle (result);
-        g_error_free (error);
+        moonshot_dbus_proxy = dbus_connect (error);
+
+    if (*error != NULL)
         return;
-    }
 
     g_return_if_fail (DBUS_IS_G_PROXY (moonshot_dbus_proxy));
 
-    result = g_simple_async_result_new (NULL,
-                                        callback,
-                                        user_data,
-                                        moonshot_get_identity);
-
-    call_id = dbus_g_proxy_begin_call (moonshot_dbus_proxy,
-                                       "GetIdentity",
-                                       dbus_call_complete_cb,
-                                       result, NULL,
-                                       G_TYPE_STRING, nai,
-                                       G_TYPE_STRING, password,
-                                       G_TYPE_STRING, service);
-}
-
-/**
- * moonshot_get_identity_finish:
- * @result: The #GAsyncResult which was passed to your callback.
- * @nai: A pointer to a string which receives the name and issuer of the
- *       selected identity.
- * @password: A pointer to a string which receives the password.
- * @server_certificate_hash: Receives a hash of the identity server's
- *                           certificate, or %NULL.
- * @ca_certificate: The CA certificate, if @server_certificate_hash was %NULL.
- * @subject_name_constraint: Set if @ca_certificate is set, otherwise %NULL.
- * @subject_alt_name_constraint: Set if @ca_certificate is set, otherwise %NULL.
- * @error: Return location for an error, or %NULL.
- *
- * Gets the details of the identity card that was selected, if any.
- *
- * There are two types of trust anchor that may be returned. If
- * @server_certificate_hash is non-empty, the remaining parameters will be
- * empty. Otherwise, the @ca_certificate parameter and the subject name
- * constraints will be returned.
- *
- * Return value: %TRUE if an identity was successfully selected, %FALSE on
- *               failure.
- */
-gboolean moonshot_get_identity_finish (GAsyncResult  *result,
-                                       char         **nai,
-                                       char         **password,
-                                       char         **server_certificate_hash,
-                                       char         **ca_certificate,
-                                       char         **subject_name_constraint,
-                                       char         **subject_alt_name_constraint,
-                                       GError       **error)
-{
-    MoonshotIdentityData *identity;
-
-    g_return_val_if_fail (g_simple_async_result_is_valid (result,
-                                                          NULL,
-                                                          moonshot_get_identity),
-                          FALSE);
-
-    if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
+    dbus_g_proxy_call (moonshot_dbus_proxy,
+                       "GetIdentity",
+                       &g_error,
+                       G_TYPE_STRING, nai,
+                       G_TYPE_STRING, password,
+                       G_TYPE_STRING, service,
+                       G_TYPE_INVALID,
+                       G_TYPE_STRING, nai_out,
+                       G_TYPE_STRING, password_out,
+                       G_TYPE_STRING, server_certificate_hash_out,
+                       G_TYPE_STRING, ca_certificate_out,
+                       G_TYPE_STRING, subject_name_constraint_out,
+                       G_TYPE_STRING, subject_alt_name_constraint_out,
+                       G_TYPE_BOOLEAN, &success,
+                       G_TYPE_INVALID);
+
+    if (g_error != NULL) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                     g_error->message);
         return FALSE;
+    }
 
-    identity = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
-
-    *nai = identity->nai;
-    *password = identity->password;
-    *server_certificate_hash = identity->server_certificate_hash;
-    *ca_certificate = identity->ca_certificate;
-    *subject_name_constraint = identity->subject_name_constraint;
-    *subject_alt_name_constraint = identity->subject_alt_name_constraint;
+    if (success == FALSE) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_NO_IDENTITY_SELECTED,
+                                     "No identity was returned by the Moonshot "
+                                     "user interface.");
+        return FALSE;
+    }
 
     return TRUE;
 }
 
 
+
     /**
      * Returns the default identity - most recently used.
      *
index 39118db..65924b7 100644 (file)
 #define __LIBMOONSHOT_H
 
 typedef enum {
-    MOONSHOT_ERROR_NO_IDENTITY_SELECTED,
-    MOONSHOT_ERROR_DBUS_ERROR,
     MOONSHOT_ERROR_UNABLE_TO_START_SERVICE,
-    MOONSHOT_ERROR_OS_ERROR
+    MOONSHOT_ERROR_NO_IDENTITY_SELECTED,
+    MOONSHOT_ERROR_OS_ERROR,
+    MOONSHOT_ERROR_IPC_ERROR
 } MoonshotErrorCode;
 
 typedef struct {