Update the task model with tasks from the server.
[milk] / src / milk-task-model.c
index 9a69c6a..9e20765 100644 (file)
@@ -23,6 +23,7 @@
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <hildon/hildon.h>
+#include <rtm-glib/rtm-glib.h>
 
 #include "milk-task-model.h"
 #include "milk-auth.h"
@@ -232,34 +233,36 @@ milk_task_model_iter_parent (GtkTreeModel *model,
 static void
 populate_model (MilkTaskModel *model)
 {
-        typedef struct {
-                const char *id;
-                const char *title;
-                gint priority;
-        } MilkTask_args;
-
-        /* FIXME: don't hard-code this */
-        static MilkTask_args tasks[] = {
-                { "0", "Walk the dog", 1},
-                { "2", "Make a Maemo 5 RTM client", 1},
-                { "6", "Stand on one foot", 3},
-                { "9", "Pick up some DVX ('cause it's so crisp)", 2},
-                { "5", "Finalize Halloween costume", 3},
-        };
-
         MilkTaskModelPrivate *priv = MILK_TASK_MODEL_PRIVATE (model);
+        GList *rtm_tasks;
+        GList *l;
         GtkTreeIter iter;
-        gint i;
 
-        /* FIXME: remove all existing rows */
-        /* FIXME: use the tasks from the auth */
+        gtk_list_store_clear (priv->store);
+
+        /* FIXME: poll for new tasks periodically -- there's rtm-glib API to
+         * optimize just fetching the latest ones */
+        rtm_tasks = milk_auth_get_tasks (priv->auth);
 
         /* Populate model */
-        for (i = 0; i < G_N_ELEMENTS (tasks); i++) {
+        for (l = rtm_tasks; l; l = g_list_delete_link (l, l)) {
+                RtmTask *rtm_task;
                 MilkTask *task;
 
-                task = milk_task_new (
-                                tasks[i].id, tasks[i].title, tasks[i].priority);
+                rtm_task = RTM_TASK (l->data);
+
+                /* XXX: if possible, avoid fetching these in the first place */
+                /* Skip tasks deleted or completed. */
+                if (rtm_task_get_completed_date (rtm_task) ||
+                    rtm_task_get_deleted_date (rtm_task)) {
+                        continue;
+                }
+
+                task = milk_task_new (rtm_task_get_id (rtm_task),
+                                rtm_task_get_name (rtm_task),
+                                /* FIXME: switch priority from int to string */
+                                g_ascii_strtod (rtm_task_get_priority
+                                                (rtm_task), NULL));
 
                 gtk_list_store_append (priv->store, &iter);
                 gtk_list_store_set (
@@ -269,6 +272,53 @@ populate_model (MilkTaskModel *model)
         }
 }
 
+static void
+auth_notify_cb (MilkAuth      *auth,
+                GParamSpec    *spec,
+                MilkTaskModel *model)
+{
+        if (milk_auth_get_state (auth) == MILK_AUTH_STATE_CONNECTED) {
+                populate_model (model);
+        }
+}
+
+static void
+row_changed_cb (GtkTreeModel  *model,
+                GtkTreePath   *path,
+                GtkTreeIter   *iter,
+                MilkTaskModel *self)
+{
+        gtk_tree_model_row_changed (GTK_TREE_MODEL (self), path, iter);
+}
+
+static void
+row_deleted_cb (GtkTreeModel  *model,
+                GtkTreePath   *path,
+                MilkTaskModel *self)
+{
+        gtk_tree_model_row_deleted (GTK_TREE_MODEL (self), path);
+}
+
+static void
+row_inserted_cb (GtkTreeModel  *model,
+                 GtkTreePath   *path,
+                 GtkTreeIter   *iter,
+                 MilkTaskModel *self)
+{
+        gtk_tree_model_row_inserted (GTK_TREE_MODEL (self), path, iter);
+}
+
+static void
+rows_reordered_cb (GtkTreeModel  *model,
+                   GtkTreePath   *path,
+                   GtkTreeIter   *iter,
+                   gint          *new_order,
+                   MilkTaskModel *self)
+{
+        gtk_tree_model_rows_reordered (GTK_TREE_MODEL (self), path, NULL,
+                        new_order);
+}
+
 void
 milk_task_model_set_auth (MilkTaskModel *model,
                           MilkAuth      *auth)
@@ -282,15 +332,17 @@ milk_task_model_set_auth (MilkTaskModel *model,
 
         priv = MILK_TASK_MODEL_PRIVATE (model);
 
-        /* FIXME: cut this */
-        g_debug ("authorizing and populating model with %p", auth);
-
         if (priv->auth) {
                 g_object_unref (priv->auth);
-                priv->auth = g_object_ref (auth);
         }
+        priv->auth = g_object_ref (auth);
 
-        populate_model (model);
+        if (milk_auth_get_state (priv->auth) == MILK_AUTH_STATE_CONNECTED) {
+                populate_model (model);
+        } else {
+                g_signal_connect (priv->auth, "notify::state",
+                                G_CALLBACK (auth_notify_cb), model);
+        }
 }
 
 static void
@@ -342,6 +394,15 @@ milk_task_model_dispose (GObject *object)
                 priv->auth = NULL;
         }
 
+        g_signal_handlers_disconnect_by_func (priv->store, row_changed_cb,
+                        object);
+        g_signal_handlers_disconnect_by_func (priv->store, row_deleted_cb,
+                        object);
+        g_signal_handlers_disconnect_by_func (priv->store, row_inserted_cb,
+                        object);
+        g_signal_handlers_disconnect_by_func (priv->store, rows_reordered_cb,
+                        object);
+
         if (priv->store) {
                 g_object_unref (priv->store);
                 priv->store = NULL;
@@ -383,6 +444,18 @@ milk_task_model_init (MilkTaskModel *self)
 
         priv->store = gtk_list_store_new (
                         MILK_TASK_MODEL_N_COLUMNS, MILK_TYPE_TASK);
+
+        g_signal_connect (priv->store, "row-changed",
+                        G_CALLBACK (row_changed_cb), self);
+
+        g_signal_connect (priv->store, "row-deleted",
+                        G_CALLBACK (row_deleted_cb), self);
+
+        g_signal_connect (priv->store, "row-inserted",
+                        G_CALLBACK (row_inserted_cb), self);
+
+        g_signal_connect (priv->store, "rows-reordered",
+                        G_CALLBACK (rows_reordered_cb), self);
 }
 
 static void