Make priority sorting work
[milk] / src / milk-main-window.c
index 89104f7..9754c45 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)
@@ -34,11 +34,13 @@ G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW)
 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
 #define MILK_MAIN_WINDOW_PRIVATE(o) ((MILK_MAIN_WINDOW ((o)))->priv)
 
+#define NEW_TASK_PLACEHOLDER_TEXT "Enter a new task..."
+
 static GtkWidget *default_window = NULL;
 
 struct _MilkMainWindowPrivate
 {
-        MilkAuth *auth;
+        MilkCache *cache;
 
         GtkWidget *app_menu;
 
@@ -101,18 +103,91 @@ new_task_clicked_cb (GtkButton      *button,
         g_debug ("FIXME: implement 'new task' action");
 }
 
+static GList*
+get_selected_tasks (MilkMainWindow *window)
+{
+        MilkMainWindowPrivate *priv;
+        GList *rows;
+        GtkTreeModel *model;
+        GList *tasks = 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);
+
+        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)
 {
-        g_debug ("FIXME: implement 'complete' action");
+        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 (tasks) {
+                        milk_cache_task_complete (priv->cache, timeline,
+                                        tasks->data, &error);
+                        tasks = g_list_delete_link (tasks, tasks);
+                }
+        }
 }
 
 static void
 delete_clicked_cb (GtkButton      *button,
                    MilkMainWindow *window)
 {
-        g_debug ("FIXME: implement 'delete' action");
+        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 (tasks) {
+                        milk_cache_task_delete (priv->cache, timeline,
+                                        tasks->data, &error);
+                        tasks = g_list_delete_link (tasks, tasks);
+                }
+        }
 }
 
 static void
@@ -149,6 +224,77 @@ static MenuItem menu_items_selection_required[] = {
 };
 
 static void
+new_task_entry_activated_cb (GtkEntry       *entry,
+                             MilkMainWindow *window)
+{
+        MilkMainWindowPrivate *priv;
+        char *name;
+
+        priv = MILK_MAIN_WINDOW_PRIVATE (window);
+
+        name = g_strdup (gtk_entry_get_text (entry));
+
+        /* Strip the contents of leading and trailing whitespace, and add as a
+         * new task if the result is non-empty */
+        if (g_strcmp0 (g_strstrip (name), "")) {
+                char *timeline;
+                GError *error = NULL;
+
+                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 {
+                        RtmTask *task;
+
+                        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);
+                                g_clear_error (&error);
+                        }
+                }
+        }
+
+        g_free (name);
+}
+
+static gboolean
+new_task_entry_key_press_event_cb (GtkEntry       *entry,
+                                   GdkEventKey    *event,
+                                   MilkMainWindow *window)
+{
+        MilkMainWindowPrivate *priv;
+
+        priv = MILK_MAIN_WINDOW_PRIVATE (window);
+
+        if (!event || event->type != GDK_KEY_PRESS) {
+                return FALSE;
+        }
+
+        switch (event->keyval) {
+                case GDK_KP_Enter:
+                case GDK_Return:
+                        new_task_entry_activated_cb (entry, window);
+                        return TRUE;
+        }
+
+        return FALSE;
+}
+
+static void
 task_view_selection_changed_cb (HildonTouchSelector *view,
                                 gint                 column,
                                 MilkMainWindow      *window)
@@ -175,6 +321,8 @@ task_view_selection_changed_cb (HildonTouchSelector *view,
                 else
                         gtk_widget_hide (w);
         }
+
+        g_list_free (rows);
 }
 
 static GtkWidget*
@@ -221,11 +369,47 @@ create_menu (gpointer user_data)
 }
 
 static void
-contact_column_render_func (GtkCellLayout   *cell_layout,
-                            GtkCellRenderer *renderer,
-                            GtkTreeModel    *model,
-                            GtkTreeIter     *iter,
-                            gpointer         user_data)
+priority_column_render_func (GtkCellLayout   *cell_layout,
+                             GtkCellRenderer *renderer,
+                             GtkTreeModel    *model,
+                             GtkTreeIter     *iter,
+                             gpointer         user_data)
+{
+        RtmTask *task;
+        const char *priority;
+        GdkColor color = {};
+        gboolean color_set = TRUE;
+
+        gtk_tree_model_get (
+                        model, iter, MILK_TASK_MODEL_COLUMN_TASK, &task, -1);
+
+        priority = rtm_task_get_priority (task);
+
+        if (FALSE) {
+        } else if (!g_strcmp0 (priority, "1")) {
+                gdk_color_parse ("#ea5200", &color);
+        } else if (!g_strcmp0 (priority, "2")) {
+                gdk_color_parse ("#0060bf", &color);
+        } else if (!g_strcmp0 (priority, "3")) {
+                gdk_color_parse ("#359aff", &color);
+        } else {
+                color_set = FALSE;
+        }
+
+        g_object_set (renderer,
+                        "cell-background-gdk", color_set ? &color : NULL,
+                        "cell-background-set", color_set,
+                        NULL);
+
+        g_object_unref (task);
+}
+
+static void
+name_column_render_func (GtkCellLayout   *cell_layout,
+                         GtkCellRenderer *renderer,
+                         GtkTreeModel    *model,
+                         GtkTreeIter     *iter,
+                         gpointer         user_data)
 {
         RtmTask *task;
 
@@ -237,15 +421,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);
 
-        /* FIXME: cut this */
-        g_debug ("trying to run the milk auth demo");
-        milk_auth_log_in (priv->auth);
+        milk_cache_authenticate (priv->cache);
 
         return FALSE;
 }
@@ -273,29 +455,45 @@ milk_main_window_constructed (GObject* object)
         /* FIXME: change this to hildon_gtk_entry_set_placeholder_text() is
          * fixed, since this is deprecated */
         hildon_entry_set_placeholder (HILDON_ENTRY (w),
-                        _("Enter a new task..."));
+                        _(NEW_TASK_PLACEHOLDER_TEXT));
         priv->new_task_entry = w;
+        g_signal_connect (G_OBJECT (w), "activate",
+                        G_CALLBACK (new_task_entry_activated_cb), self);
+        g_signal_connect (G_OBJECT (w), "key-press-event",
+                        G_CALLBACK (new_task_entry_key_press_event_cb), self);
 
         /*
          * 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 ();
 
+        col = hildon_touch_selector_append_column (
+                        HILDON_TOUCH_SELECTOR (w), model, NULL, NULL);
 
         renderer = gtk_cell_renderer_text_new ();
         g_object_set (renderer,
-                        "ellipsize", PANGO_ELLIPSIZE_END,
+                        "width", HILDON_ICON_PIXEL_SIZE_XSMALL,
+                        "width-chars", 1,
                         NULL);
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (col), renderer, FALSE);
+        gtk_cell_layout_set_cell_data_func (
+                        GTK_CELL_LAYOUT (col), renderer,
+                        (GtkCellLayoutDataFunc) priority_column_render_func,
+                        self, NULL);
 
-        col = hildon_touch_selector_append_column (
-                        HILDON_TOUCH_SELECTOR (w), model, NULL, NULL);
+        renderer = gtk_cell_renderer_text_new ();
+        g_object_set (renderer,
+                        "ellipsize", PANGO_ELLIPSIZE_END,
+                        "alignment", PANGO_ALIGN_LEFT,
+                        "xpad", HILDON_MARGIN_DEFAULT,
+                        NULL);
         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (col), renderer, TRUE);
         gtk_cell_layout_set_cell_data_func (
                         GTK_CELL_LAYOUT (col), renderer,
-                        (GtkCellLayoutDataFunc) contact_column_render_func,
+                        (GtkCellLayoutDataFunc) name_column_render_func,
                         self, NULL);
+
         g_object_unref (model);
 
         hildon_touch_selector_set_column_selection_mode (
@@ -319,9 +517,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