[gnome-pilot/dbus] Move logic from orbit_daemon_glue.c to gpilot-daemon.c



commit 2225777db185a627b81fb0bb5a114732b6bd1b18
Author: Halton Huo <halton huo sun com>
Date:   Tue Mar 2 12:18:50 2010 +0800

    Move logic from orbit_daemon_glue.c to gpilot-daemon.c
    
    Done for gpilot_daemon_request_install(),
             gpilot_daemon_request_restore(),
             gpilot_daemon_request_conduit()
             gpilot_daemon_remove_request()

 gpilotd/.gpilot-daemon.h.swp |  Bin 20480 -> 0 bytes
 gpilotd/gpilot-daemon.c      |  242 +++++++++++++++++++++++++++++++++++++++---
 gpilotd/gpilot-daemon.h      |   64 +++++++-----
 gpilotd/gpilot-daemon.xml    |   42 ++++----
 4 files changed, 288 insertions(+), 60 deletions(-)
---
diff --git a/gpilotd/gpilot-daemon.c b/gpilotd/gpilot-daemon.c
index bd3f0b8..55e7031 100644
--- a/gpilotd/gpilot-daemon.c
+++ b/gpilotd/gpilot-daemon.c
@@ -27,6 +27,7 @@
 #include "gpilot-daemon.h"
 #include "gpilot-daemon-glue.h"
 #include "gnome-pilot-structures.h"
+#include "queue_io.h"
 
 static void     gpilot_daemon_class_init  (GpilotDaemonClass *klass);
 static void     gpilot_daemon_init        (GpilotDaemon      *daemon);
@@ -53,6 +54,60 @@ gpilot_daemon_pause_device (GPilotDevice *device,
         }
 }
 
+static gint
+match_pilot_and_name(const GPilotPilot *pilot,
+                     const gchar       *name)
+{
+        if(pilot) {
+                return g_ascii_strcasecmp (name, pilot->name);
+        }
+        return -1; 
+}
+
+static guint32
+pilot_id_from_name (const gchar   *name,
+                    GPilotContext *context)
+{
+        GList *pilot;
+        pilot = g_list_find_custom (context->pilots, (gpointer)name,
+                                    (GCompareFunc)match_pilot_and_name);
+        if(pilot)  
+                return ((GPilotPilot*)pilot->data)->pilot_id;
+        return 0;
+}
+
+GQuark
+gpilot_daemon_error_quark (void)
+{
+        static GQuark ret = 0;
+        if (ret == 0) {
+                ret = g_quark_from_static_string ("gpilot_daemon_error");
+        }
+
+        return ret;
+}
+
+#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
+
+GType
+gpilot_daemon_error_get_type (void)
+{
+        static GType etype = 0;
+
+        if (etype == 0) {
+                static const GEnumValue values[] = {
+                        ENUM_ENTRY (GPILOT_DAEMON_ERROR_GENERAL, "GeneralError"),
+                        { 0, 0, 0 }
+                };
+
+                g_assert (GPILOT_DAEMON_NUM_ERRORS == G_N_ELEMENTS (values) - 1);
+
+                etype = g_enum_register_static ("GpilotDaemonError", values);
+        }
+
+        return etype;
+}
+
 gboolean
 gpilot_daemon_pause (GpilotDaemon   *daemon,
                      gboolean        on_off,
@@ -100,47 +155,206 @@ gpilot_daemon_noop (GpilotDaemon   *daemon)
 /* request operations */
 gboolean
 gpilot_daemon_request_install (GpilotDaemon   *daemon,
-                               const char     *pilot_id,
-                               const char     *file_name,
+                               const char     *pilot_name,
+                               const char     *filename,
                                const char     *description,
                                Survival        survival,
                                unsigned long   timeout,
-                               unsigned long  *handler,
+                               unsigned long  *handle,
                                GError        **error)
 {
-        return TRUE;
+        GpilotDaemonPrivate *priv;
+        GPilotRequest        req;
+        gchar               *client_id;
+        gboolean             ret;
+        
+        ret = FALSE;
+        priv = daemon->priv;
+        /* TODO
+        GET_AND_CHECK_CLIENT_ID(client_id,cb,0); 
+         */
+                              
+        LOG (("request_install(pilot_name=%s (%d),filename=%s,survival=%d,timeout=%d)",                 
+            pilot_name,       
+            pilot_id_from_name (pilot_name, priv->gpilotd_context),
+            filename,
+            survival,
+            timeout));
+
+        req.type = GREQ_INSTALL;
+        req.pilot_id = pilot_id_from_name (pilot_name, priv->gpilotd_context);
+
+        if(req.pilot_id == 0) {
+                g_set_error (error,
+                             GPILOT_DAEMON_ERROR,
+                             GPILOT_DAEMON_ERROR_GENERAL,
+                             "Unknow pilot %s",
+                             pilot_name);
+                goto out;
+        }
+        if(access (filename, R_OK)) {
+                g_set_error (error,
+                             GPILOT_DAEMON_ERROR,
+                             GPILOT_DAEMON_ERROR_GENERAL,
+                             "Missing file %s",
+                             filename);
+                goto out;
+        }
+
+        req.timeout = survival==PERSISTENT?0:timeout;
+        req.cradle = NULL;
+        req.client_id = g_strdup (client_id);
+        req.parameters.install.filename = g_strdup (filename);
+        req.parameters.install.description = g_strdup (description);
+
+        *handle = gpc_queue_store_request (req);
+
+        ret = TRUE;
+ out:
+        g_free (client_id);
+        return ret;
 }
 
 gboolean
 gpilot_daemon_request_restore (GpilotDaemon   *daemon,
-                               const char     *pilot_id,
+                               const char     *pilot_name,
                                const char     *directory,
                                Survival        survival,
                                unsigned long   timeout,
-                               unsigned long  *handler,
+                               unsigned long  *handle,
                                GError        **error)
 {
-        return TRUE;
+        GpilotDaemonPrivate *priv;
+        GPilotRequest        req;
+        gchar               *client_id;
+        gboolean             ret;
+        
+        ret = FALSE;
+        priv = daemon->priv;
+
+        /* TODO
+        GET_AND_CHECK_CLIENT_ID(client_id,cb,0); 
+         */
+
+        LOG(("request_restore(pilot_name=%s (%d), directory=%s,survival=%d,timeout=%d)",
+            pilot_name,
+            pilot_id_from_name (pilot_name, priv->gpilotd_context),
+            directory,
+            survival,
+            timeout));
+
+        req.type = GREQ_RESTORE;
+        req.pilot_id = pilot_id_from_name (pilot_name, priv->gpilotd_context);
+        if(req.pilot_id == 0) {
+                g_set_error (error,
+                             GPILOT_DAEMON_ERROR,
+                             GPILOT_DAEMON_ERROR_GENERAL,
+                             "Unknow pilot %s",
+                             pilot_name);
+                goto out;
+        }
+        if (!g_file_test (directory, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
+                g_set_error (error,
+                             GPILOT_DAEMON_ERROR,
+                             GPILOT_DAEMON_ERROR_GENERAL,
+                             "Directory %s not accessible",
+                             directory);
+                goto out;
+        }
+
+        req.timeout = survival==PERSISTENT?0:timeout;
+        req.cradle = NULL;
+        req.client_id = g_strdup(client_id);
+
+        req.parameters.restore.directory = g_strdup(directory);
+
+        *handle = gpc_queue_store_request (req);
+
+        ret = TRUE;
+ out:
+        g_free(client_id);
+        return ret;
 }
 
 gboolean
 gpilot_daemon_request_conduit (GpilotDaemon   *daemon,
-                               const char     *pilot_id,
+                               const char     *pilot_name,
                                const char     *conduit_name,
                                ConduitOperation operation,
                                Survival        survival,
                                unsigned long   timeout,
-                               unsigned long  *handler,
+                               unsigned long  *handle,
                                GError        **error)
 {
-        return TRUE;
+        GpilotDaemonPrivate *priv;
+        GPilotRequest        req;
+        gchar               *client_id;
+        gboolean             ret;
+        
+        ret = FALSE;
+        priv = daemon->priv;
+
+        /* TODO
+        GET_AND_CHECK_CLIENT_ID(client_id,cb,0); 
+         */
+
+        LOG(("request_conduit(pilot=%s (%d), conduit=%s)",
+            pilot_name,
+            pilot_id_from_name (pilot_name, priv->gpilotd_context),
+            conduit_name));
+
+        req.pilot_id = pilot_id_from_name (pilot_name, priv->gpilotd_context);
+        if(req.pilot_id == 0) {
+                g_set_error (error,
+                             GPILOT_DAEMON_ERROR,
+                             GPILOT_DAEMON_ERROR_GENERAL,
+                             "Unknow pilot %s",
+                             pilot_name);
+                goto out;
+        }
+
+        req.type = GREQ_CONDUIT;
+        req.timeout = survival==PERSISTENT?0:timeout;
+        req.cradle = NULL;
+        req.client_id = g_strdup(client_id);
+
+        req.parameters.conduit.name = g_strdup (conduit_name);
+        switch (operation) {
+        case SYNCHRONIZE:
+                req.parameters.conduit.how = GnomePilotConduitSyncTypeSynchronize; break;
+        case COPY_FROM_PILOT:
+                req.parameters.conduit.how = GnomePilotConduitSyncTypeCopyFromPilot; break;
+        case COPY_TO_PILOT:
+                req.parameters.conduit.how = GnomePilotConduitSyncTypeCopyToPilot; break;
+        case MERGE_FROM_PILOT:
+                req.parameters.conduit.how = GnomePilotConduitSyncTypeMergeFromPilot; break;
+        case MERGE_TO_PILOT:
+                req.parameters.conduit.how = GnomePilotConduitSyncTypeMergeToPilot; break;
+        case CONDUIT_DEFAULT:
+                req.parameters.conduit.how = GnomePilotConduitSyncTypeCustom; break;
+        default:
+                g_set_error (error,
+                             GPILOT_DAEMON_ERROR,
+                             GPILOT_DAEMON_ERROR_GENERAL,
+                             "Unknow operation %d",
+                             operation);
+                break;
+        }
+
+        *handle = gpc_queue_store_request (req);
+
+        ret = TRUE;
+ out:
+        g_free(client_id);
+        return ret;
 }
 
 gboolean
 gpilot_daemon_remove_request (GpilotDaemon   *daemon,
-                              unsigned long  *handler,
+                              unsigned long   handle,
                               GError        **error)
 {
+        gpc_queue_purge_request_point (handle/65535, handle%65535);
         return TRUE;
 }
 
@@ -151,7 +365,7 @@ gpilot_daemon_get_system_info (GpilotDaemon   *daemon,
                                const char     *cradle,
                                Survival        survival,
                                unsigned long   timeout,
-                               unsigned long  *handler,
+                               unsigned long  *handle,
                                GError        **error)
 {
         return TRUE;
@@ -267,7 +481,7 @@ gpilot_daemon_get_user_info (GpilotDaemon   *daemon,
                              const char     *cradle,
                              Survival        survival,
                              unsigned long   timeout,
-                             unsigned long  *handler,
+                             unsigned long  *handle,
                              GError        **error)
 {
         return TRUE;
@@ -281,7 +495,7 @@ gpilot_daemon_set_user_info (GpilotDaemon   *daemon,
                              unsigned long   timeout,
                              unsigned long   uid,
                              const char     *username,
-                             unsigned long  *handler,
+                             unsigned long  *handle,
                              GError        **error)
 {
         return TRUE;
diff --git a/gpilotd/gpilot-daemon.h b/gpilotd/gpilot-daemon.h
index a37cf8e..2b8f11e 100644
--- a/gpilotd/gpilot-daemon.h
+++ b/gpilotd/gpilot-daemon.h
@@ -34,6 +34,24 @@ G_BEGIN_DECLS
 #define GPILOT_IS_DAEMON_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GPILOT_TYPE_DAEMON))
 #define GPILOT_DAEMON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GPILOT_TYPE_DAEMON, GpilotDaemonClass))
 
+#define LOG(x) g_message x
+
+typedef struct GpilotDaemonPrivate GpilotDaemonPrivate;
+typedef struct
+{
+        GObject            parent;
+        GpilotDaemonPrivate *priv;
+} GpilotDaemon;
+
+typedef struct
+{
+        GObjectClass   parent_class;
+        void          (* connected)     (GpilotDaemon   *gpilot,
+                                         const char     *pilot_id,
+                                         unsigned long  *uid,
+                                         const char     *user);
+} GpilotDaemonClass;
+
 typedef enum
 {
         IMMEDIATE,
@@ -63,24 +81,20 @@ typedef enum
         MERGE_TO_PILOT
 } ConduitOperation;
 
-typedef struct GpilotDaemonPrivate GpilotDaemonPrivate;
-typedef struct
+typedef enum
 {
-        GObject            parent;
-        GpilotDaemonPrivate *priv;
-} GpilotDaemon;
+        GPILOT_DAEMON_ERROR_GENERAL,
+        GPILOT_DAEMON_NUM_ERRORS
+} GpilotDaemonError;
 
-typedef struct
-{
-        GObjectClass   parent_class;
-        void          (* connected)     (GpilotDaemon   *gpilot,
-                                         const char     *pilot_id,
-                                         unsigned long  *uid,
-                                         const char     *user);
-} GpilotDaemonClass;
+#define GPILOT_DAEMON_ERROR gpilot_daemon_error_quark ()
+
+GType           gpilot_daemon_error_get_type       (void);
+#define GPILOT_DAEMON_TYPE_ERROR (gpilot_daemon_error_get_type ())
 
+GQuark          gpilot_daemon_error_quark          (void);
 GType           gpilot_daemon_get_type          (void);
-GpilotDaemon*   gpilot_daemon_new               ();
+GpilotDaemon*   gpilot_daemon_new               (void);
 
 /* exported to bus */
 
@@ -96,37 +110,37 @@ gboolean        gpilot_daemon_reread_config     (GpilotDaemon   *daemon,
 gboolean        gpilot_daemon_noop              (GpilotDaemon   *daemon);
 /* request operations */
 gboolean        gpilot_daemon_request_install   (GpilotDaemon   *daemon,
-                                                 const char     *pilot_id,
-                                                 const char     *file_name,
+                                                 const char     *pilot_name,
+                                                 const char     *filename,
                                                  const char     *description,
                                                  Survival        survival,
                                                  unsigned long   timeout,
-                                                 unsigned long  *handler,
+                                                 unsigned long  *handle,
                                                  GError        **error);
 gboolean        gpilot_daemon_request_restore   (GpilotDaemon   *daemon,
-                                                 const char     *pilot_id,
+                                                 const char     *pilot_name,
                                                  const char     *directory,
                                                  Survival        survival,
                                                  unsigned long   timeout,
-                                                 unsigned long  *handler,
+                                                 unsigned long  *handle,
                                                  GError        **error);
 gboolean        gpilot_daemon_request_conduit   (GpilotDaemon   *daemon,
-                                                 const char     *pilot_id,
+                                                 const char     *pilot_name,
                                                  const char     *conduit_name,
                                                  ConduitOperation operation,
                                                  Survival        survival,
                                                  unsigned long   timeout,
-                                                 unsigned long  *handler,
+                                                 unsigned long  *handle,
                                                  GError        **error);
 gboolean        gpilot_daemon_remove_request    (GpilotDaemon   *daemon,
-                                                 unsigned long  *handler,
+                                                 unsigned long   handle,
                                                  GError        **error);
 /* information operations */
 gboolean        gpilot_daemon_get_system_info   (GpilotDaemon   *daemon,
                                                  const char     *cradle,
                                                  Survival        survival,
                                                  unsigned long   timeout,
-                                                 unsigned long  *handler,
+                                                 unsigned long  *handle,
                                                  GError        **error);
 gboolean        gpilot_daemon_get_users         (GpilotDaemon   *daemon,
                                                  GPtrArray     **users,
@@ -185,7 +199,7 @@ gboolean        gpilot_daemon_get_user_info     (GpilotDaemon   *daemon,
                                                  const char     *cradle,
                                                  Survival        survival,
                                                  unsigned long   timeout,
-                                                 unsigned long  *handler,
+                                                 unsigned long  *handle,
                                                  GError        **error);
 gboolean        gpilot_daemon_set_user_info     (GpilotDaemon   *daemon,
                                                  const char     *cradle,
@@ -194,7 +208,7 @@ gboolean        gpilot_daemon_set_user_info     (GpilotDaemon   *daemon,
                                                  unsigned long   timeout,
                                                  unsigned long   uid,
                                                  const char     *username,
-                                                 unsigned long  *handler,
+                                                 unsigned long  *handle,
                                                  GError        **error);
 /* Notification operations */
 gboolean        gpilot_daemon_monitor           (GpilotDaemon   *daemon,
diff --git a/gpilotd/gpilot-daemon.xml b/gpilotd/gpilot-daemon.xml
index 160f3a9..4d1399a 100644
--- a/gpilotd/gpilot-daemon.xml
+++ b/gpilotd/gpilot-daemon.xml
@@ -27,12 +27,12 @@
     </method>
 
     <method name="RequestInstall">
-      <arg name="pilot_id" direction="in" type="s">
+      <arg name="pilot_name" direction="in" type="s">
         <doc:doc>
-          <doc:summary>The pilot id</doc:summary>
+          <doc:summary>The pilot name</doc:summary>
         </doc:doc>
       </arg>
-      <arg name="file_name" direction="in" type="s">
+      <arg name="filename" direction="in" type="s">
         <doc:doc>
           <doc:summary>The file name</doc:summary>
         </doc:doc>
@@ -52,9 +52,9 @@
           <doc:summary>The time out by second.</doc:summary>
         </doc:doc>
       </arg>
-      <arg name="handler" direction="out" type="u">
+      <arg name="handle" direction="out" type="u">
         <doc:doc>
-          <doc:summary>The returned handler.</doc:summary>
+          <doc:summary>The returned handle.</doc:summary>
         </doc:doc>
       </arg>
       <doc:doc>
@@ -65,9 +65,9 @@
     </method>
 
     <method name="RequestRestore">
-      <arg name="pilot_id" direction="in" type="s">
+      <arg name="pilot_name" direction="in" type="s">
         <doc:doc>
-          <doc:summary>The pilot id</doc:summary>
+          <doc:summary>The pilot name</doc:summary>
         </doc:doc>
       </arg>
       <arg name="directory" direction="in" type="s">
@@ -85,9 +85,9 @@
           <doc:summary>The time out by second.</doc:summary>
         </doc:doc>
       </arg>
-      <arg name="handler" direction="out" type="u">
+      <arg name="handle" direction="out" type="u">
         <doc:doc>
-          <doc:summary>The returned handler.</doc:summary>
+          <doc:summary>The returned handle.</doc:summary>
         </doc:doc>
       </arg>
       <doc:doc>
@@ -98,9 +98,9 @@
     </method>
 
     <method name="RequestConduit">
-      <arg name="pilot_id" direction="in" type="s">
+      <arg name="pilot_name" direction="in" type="s">
         <doc:doc>
-          <doc:summary>The pilot id</doc:summary>
+          <doc:summary>The pilot name</doc:summary>
         </doc:doc>
       </arg>
       <arg name="conduit_name" direction="in" type="s">
@@ -130,9 +130,9 @@
           <doc:summary>The time out by second.</doc:summary>
         </doc:doc>
       </arg>
-      <arg name="handler" direction="out" type="u">
+      <arg name="handle" direction="out" type="u">
         <doc:doc>
-          <doc:summary>The returned handler.</doc:summary>
+          <doc:summary>The returned handle.</doc:summary>
         </doc:doc>
       </arg>
       <doc:doc>
@@ -143,9 +143,9 @@
     </method>
 
     <method name="RemoveRequest">
-      <arg name="handler" direction="in" type="u">
+      <arg name="handle" direction="in" type="u">
         <doc:doc>
-          <doc:summary>The handler.</doc:summary>
+          <doc:summary>The handle.</doc:summary>
         </doc:doc>
       </arg>
       <doc:doc>
@@ -171,9 +171,9 @@
           <doc:summary>The time out by second.</doc:summary>
         </doc:doc>
       </arg>
-      <arg name="handler" direction="out" type="u">
+      <arg name="handle" direction="out" type="u">
         <doc:doc>
-          <doc:summary>The returned handler.</doc:summary>
+          <doc:summary>The returned handle.</doc:summary>
         </doc:doc>
       </arg>
       <doc:doc>
@@ -395,9 +395,9 @@
           <doc:summary>The time out by second.</doc:summary>
         </doc:doc>
       </arg>
-      <arg name="handler" direction="out" type="u">
+      <arg name="handle" direction="out" type="u">
         <doc:doc>
-          <doc:summary>The returned handler.</doc:summary>
+          <doc:summary>The returned handle.</doc:summary>
         </doc:doc>
       </arg>
       <doc:doc>
@@ -438,9 +438,9 @@
           <doc:summary>The returned user name.</doc:summary>
         </doc:doc>
       </arg>
-      <arg name="handler" direction="out" type="u">
+      <arg name="handle" direction="out" type="u">
         <doc:doc>
-          <doc:summary>The returned handler.</doc:summary>
+          <doc:summary>The returned handle.</doc:summary>
         </doc:doc>
       </arg>
       <doc:doc>



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