Add a basic task cache
[milk] / src / milk-main-window.c
index e159b2b..abf9ec7 100644 (file)
@@ -26,7 +26,7 @@
 #include <rtm-glib/rtm-glib.h>
 
 #include "milk-main-window.h"
-#include "milk-auth.h"
+#include "milk-cache.h"
 #include "milk-task-model.h"
 
 G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW)
@@ -40,7 +40,7 @@ static GtkWidget *default_window = NULL;
 
 struct _MilkMainWindowPrivate
 {
-        MilkAuth *auth;
+        MilkCache *cache;
 
         GtkWidget *app_menu;
 
@@ -103,19 +103,13 @@ new_task_clicked_cb (GtkButton      *button,
         g_debug ("FIXME: implement 'new task' action");
 }
 
-/* XXX: The latency between clicking "complete" and actually removing the task
- * from the view after polling the server is very long, so there's an obvious
- * lag -- it will be completely transparent (and look very fast) as soon as
- * we've got a cache in place */
-static void
-complete_clicked_cb (GtkButton      *button,
-                     MilkMainWindow *window)
+static GList*
+get_selected_tasks (MilkMainWindow *window)
 {
         MilkMainWindowPrivate *priv;
         GList *rows;
         GtkTreeModel *model;
-        char *timeline;
-        GError *error = NULL;
+        GList *tasks = NULL;
 
         priv = MILK_MAIN_WINDOW_PRIVATE (window);
 
@@ -126,85 +120,72 @@ complete_clicked_cb (GtkButton      *button,
                         HILDON_TOUCH_SELECTOR (priv->task_view),
                         TASK_VIEW_COLUMN_TITLE);
 
-        timeline = milk_auth_timeline_create (priv->auth, &error);
+        while (rows) {
+                GtkTreeIter iter;
+                RtmTask *task;
+
+                gtk_tree_model_get_iter (model, &iter, rows->data);
+                gtk_tree_model_get (model, &iter,
+                                MILK_TASK_MODEL_COLUMN_TASK, &task,
+                                -1);
+
+                tasks = g_list_prepend (tasks, task);
+                rows = g_list_delete_link (rows, rows);
+        }
+
+        return tasks;
+}
+
+static void
+complete_clicked_cb (GtkButton      *button,
+                     MilkMainWindow *window)
+{
+        MilkMainWindowPrivate *priv;
+        GList *tasks;
+        char *timeline;
+        GError *error = NULL;
+
+        priv = MILK_MAIN_WINDOW_PRIVATE (window);
+
+        tasks = get_selected_tasks (window);
+        timeline = milk_cache_timeline_create (priv->cache, &error);
 
         if (error) {
                 g_warning (G_STRLOC ": failed to create a timeline: %s",
                            error->message);
                 g_clear_error (&error);
         } else {
-                while (rows) {
-                        GtkTreeIter iter;
-                        RtmTask *task;
-
-                        gtk_tree_model_get_iter (model, &iter, rows->data);
-                        gtk_tree_model_get (model, &iter,
-                                        MILK_TASK_MODEL_COLUMN_TASK, &task,
-                                        -1);
-
-                        milk_auth_task_complete (priv->auth, timeline, task,
-                                        &error);
-                        if (error != NULL) {
-                                g_warning (G_STRLOC ": failed to complete task "
-                                                "%s: %s",
-                                                rtm_task_get_id (task),
-                                                error->message);
-                                g_clear_error (&error);
-                        }
-
-                        rows = g_list_delete_link (rows, rows);
+                while (tasks) {
+                        milk_cache_task_complete (priv->cache, timeline,
+                                        tasks->data, &error);
+                        tasks = g_list_delete_link (tasks, tasks);
                 }
         }
 }
 
-/* XXX: high latency until we have a cache; see the note for
- * complete_clicked_cb() */
 static void
 delete_clicked_cb (GtkButton      *button,
                    MilkMainWindow *window)
 {
         MilkMainWindowPrivate *priv;
-        GList *rows;
-        GtkTreeModel *model;
+        GList *tasks;
         char *timeline;
         GError *error = NULL;
 
         priv = MILK_MAIN_WINDOW_PRIVATE (window);
 
-        rows = hildon_touch_selector_get_selected_rows (
-                        HILDON_TOUCH_SELECTOR (priv->task_view),
-                        TASK_VIEW_COLUMN_TITLE);
-        model = hildon_touch_selector_get_model (
-                        HILDON_TOUCH_SELECTOR (priv->task_view),
-                        TASK_VIEW_COLUMN_TITLE);
-
-        timeline = milk_auth_timeline_create (priv->auth, &error);
+        tasks = get_selected_tasks (window);
+        timeline = milk_cache_timeline_create (priv->cache, &error);
 
         if (error) {
                 g_warning (G_STRLOC ": failed to create a timeline: %s",
                            error->message);
                 g_clear_error (&error);
         } else {
-                while (rows) {
-                        GtkTreeIter iter;
-                        RtmTask *task;
-
-                        gtk_tree_model_get_iter (model, &iter, rows->data);
-                        gtk_tree_model_get (model, &iter,
-                                        MILK_TASK_MODEL_COLUMN_TASK, &task,
-                                        -1);
-
-                        milk_auth_task_delete (priv->auth, timeline, task,
-                                        &error);
-                        if (error != NULL) {
-                                g_warning (G_STRLOC ": failed to delete task "
-                                                "%s: %s",
-                                                rtm_task_get_id (task),
-                                                error->message);
-                                g_clear_error (&error);
-                        }
-
-                        rows = g_list_delete_link (rows, rows);
+                while (tasks) {
+                        milk_cache_task_delete (priv->cache, timeline,
+                                        tasks->data, &error);
+                        tasks = g_list_delete_link (tasks, tasks);
                 }
         }
 }
@@ -259,7 +240,7 @@ new_task_entry_activated_cb (GtkEntry       *entry,
                 char *timeline;
                 GError *error = NULL;
 
-                timeline = milk_auth_timeline_create (priv->auth, &error);
+                timeline = milk_cache_timeline_create (priv->cache, &error);
 
                 if (error) {
                         g_warning (G_STRLOC ": failed to create a timeline: %s",
@@ -268,13 +249,17 @@ new_task_entry_activated_cb (GtkEntry       *entry,
                 } else {
                         RtmTask *task;
 
-                        task = milk_auth_task_add (priv->auth, timeline, name,
+                        task = milk_cache_task_add (priv->cache, timeline, name,
                                         &error);
                         if (task) {
                                 /* empty out the entry and show its placeholder
                                  * text */
                                 gtk_entry_set_text (entry, "");
                                 gtk_widget_grab_focus (priv->task_view);
+
+                                /* FIXME: we should probably scroll to this new
+                                 * task in the model view, if it's not currently
+                                 * visible (and highlight only it in any case */
                         } else {
                                 g_warning (G_STRLOC ": failed to add task: %s",
                                                 error->message);
@@ -400,13 +385,13 @@ contact_column_render_func (GtkCellLayout   *cell_layout,
 }
 
 static gboolean
-begin_auth_idle (MilkMainWindow *window)
+begin_cache_idle (MilkMainWindow *window)
 {
         MilkMainWindowPrivate *priv;
 
         priv = MILK_MAIN_WINDOW_PRIVATE (window);
 
-        milk_auth_log_in (priv->auth);
+        milk_cache_authenticate (priv->cache);
 
         return FALSE;
 }
@@ -444,11 +429,9 @@ milk_main_window_constructed (GObject* object)
         /*
          * Task List
          */
-        priv->auth = milk_auth_get_default ();
-        model = GTK_TREE_MODEL (milk_task_model_new (priv->auth));
+        model = GTK_TREE_MODEL (milk_task_model_new ());
         w = hildon_touch_selector_new ();
 
-
         renderer = gtk_cell_renderer_text_new ();
         g_object_set (renderer,
                         "ellipsize", PANGO_ELLIPSIZE_END,
@@ -484,9 +467,12 @@ milk_main_window_constructed (GObject* object)
         hildon_window_set_app_menu (
                         HILDON_WINDOW (self), HILDON_APP_MENU (priv->app_menu));
 
+        /* set up the cache */
+        priv->cache = milk_cache_get_default ();
+
         /* break a cyclical dependency by doing this after the window is
          * constructed */
-        g_idle_add ((GSourceFunc) begin_auth_idle, self);
+        g_idle_add ((GSourceFunc) begin_cache_idle, self);
 }
 
 static void