Add a basic task cache
[milk] / src / milk-auth.c
index 96155c5..c36db6a 100644 (file)
@@ -216,6 +216,50 @@ milk_auth_timeline_create (MilkAuth  *auth,
         return rtm_glib_timelines_create (priv->rtm_glib, error);
 }
 
+/* FIXME: we probably really want this to be async (but sequencable) */
+GList*
+milk_auth_tasks_add (MilkAuth  *auth,
+                     char      *timeline,
+                     GList     *names)
+{
+        gboolean success = TRUE;
+        MilkAuthPrivate *priv;
+        GList *l;
+        GList *tasks = NULL;
+        GError *error = NULL;
+
+        g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
+        g_return_val_if_fail (names, NULL);
+
+        priv = MILK_AUTH_PRIVATE (auth);
+
+        for (l = names; l; l = l->next) {
+                RtmTask *task;
+
+                /* FIXME: cut this */
+                g_debug ("trying to send task with name '%s'", l->data);
+
+                /* XXX: this uses Smart Add parsing; make this user-settable? */
+                /* XXX: the cast to char* is actually a bug in the rtm-glib API
+                 */
+                task = rtm_glib_tasks_add (priv->rtm_glib, timeline,
+                                l->data, NULL, TRUE, &error);
+                if (task) {
+                        /* FIXME: cut this */
+                        g_debug (G_STRLOC ": added task with ID '%s'",
+                                        rtm_task_get_id (task));
+
+                        tasks = g_list_prepend (tasks, task);
+                } else {
+                        g_warning ("failed to add some tasks: %s",
+                                        error->message);
+                        g_clear_error (&error);
+                }
+        }
+
+        return tasks;
+}
+
 RtmTask*
 milk_auth_task_add (MilkAuth    *auth,
                     char        *timeline,
@@ -229,11 +273,94 @@ milk_auth_task_add (MilkAuth    *auth,
         priv = MILK_AUTH_PRIVATE (auth);
 
         /* XXX: this uses Smart Add parsing; make this user-settable? */
-        /* FIXME: the cast to char* is actually a bug in the rtm-glib API */
+        /* XXX: the cast to char* is actually a bug in the rtm-glib API */
         return rtm_glib_tasks_add (priv->rtm_glib, timeline, (char*) name, NULL,
                         TRUE, error);
 }
 
+/* FIXME: we probably really want this to be async (but sequencable) */
+GList*
+milk_auth_tasks_send_changes (MilkAuth *auth,
+                              char     *timeline,
+                              GList    *tasks)
+{
+        gboolean success = TRUE;
+        MilkAuthPrivate *priv;
+        GList *l;
+        GList *tasks_sent = NULL;
+        GError *error = NULL;
+
+        g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
+        g_return_val_if_fail (tasks, NULL);
+
+        priv = MILK_AUTH_PRIVATE (auth);
+
+        for (l = tasks; l; l = l->next) {
+                RtmTask *task = l->data;
+
+                GTimeVal *tv;
+
+                /* If any of these conditions fail, libsoup ends up exploding
+                 * in a segfault (blindly de-reffing NULL); better to be safe
+                 * than sorry */
+                if (!rtm_task_get_list_id (task)) {
+                        g_warning (G_STRLOC ": task doesn't have a list ID; "
+                                        "skipping...");
+                        continue;
+                }
+
+                if (!rtm_task_get_taskseries_id (task)) {
+                        g_warning (G_STRLOC ": task doesn't have a taskseries "
+                                        "ID; skipping...");
+                        continue;
+                }
+
+                if (!rtm_task_get_id (task)) {
+                        g_warning (G_STRLOC ": task doesn't have an ID; "
+                                        "skipping...");
+                        continue;
+                }
+
+                tv = rtm_task_get_due_date (task);
+                if (tv) {
+                        char *due_str;
+                        char *tid;
+
+                        due_str = g_time_val_to_iso8601 (tv);
+
+                        /* FIXME: cut this */
+                        g_debug ("going to set due string: '%s'", due_str);
+
+                        /* XXX: this uses Smart Add parsing; make this
+                         * user-settable? */
+                        /* XXX: the cast to char* is actually a bug in the
+                         * rtm-glib API */
+                        tid = rtm_glib_tasks_set_due_date (priv->rtm_glib, timeline,
+                                        task, due_str,
+                                        /* FIXME: set this appropriately */
+                                        FALSE,
+                                        FALSE, &error);
+
+                        if (tid) {
+                                /* FIXME: this should be a set, not a list --
+                                 * we'll add each task to the set if it's been
+                                 * changed since the last send */
+                                tasks_sent = g_list_prepend (tasks_sent, task);
+                        } else {
+                                g_warning ("failed to add some tasks: %s",
+                                                error->message);
+                                g_clear_error (&error);
+                        }
+
+                        g_free (tid);
+                }
+
+                /* FIXME: handle all the other attributes, not just the date */
+        }
+
+        return tasks_sent;
+}
+
 char*
 milk_auth_task_complete (MilkAuth  *auth,
                          char      *timeline,
@@ -264,6 +391,12 @@ milk_auth_task_delete (MilkAuth  *auth,
         return rtm_glib_tasks_delete (priv->rtm_glib, timeline, task, error);
 }
 
+/* FIXME: why does this (or something above it) totally fail if we don't have a
+ * working Internet connection / resolv.conf is mangled? */
+/* FIXME: instead of this manual call, listen to the connection manager
+ * transitions -- see this:
+ * http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide/Using_Connectivity_Components/Maemo_Connectivity#Libconic_Usage
+ */
 void
 milk_auth_log_in (MilkAuth *auth)
 {