Support adding a new task from the entry box
[milk] / src / milk-main-window.c
index d72b983..c9a1817 100644 (file)
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <hildon/hildon.h>
+#include <rtm-glib/rtm-glib.h>
 
 #include "milk-main-window.h"
 #include "milk-auth.h"
-#include "milk-task.h"
 #include "milk-task-model.h"
 
 G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW)
@@ -34,6 +34,10 @@ 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;
@@ -99,18 +103,110 @@ 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)
 {
-        g_debug ("FIXME: implement 'complete' action");
+        MilkMainWindowPrivate *priv;
+        GList *rows;
+        GtkTreeModel *model;
+        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);
+
+        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);
+                }
+        }
 }
 
+/* XXX: high latency until we have a cache; see the note for
+ * complete_clicked_cb() */
 static void
 delete_clicked_cb (GtkButton      *button,
                    MilkMainWindow *window)
 {
-        g_debug ("FIXME: implement 'delete' action");
+        MilkMainWindowPrivate *priv;
+        GList *rows;
+        GtkTreeModel *model;
+        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);
+
+        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);
+                }
+        }
 }
 
 static void
@@ -147,6 +243,73 @@ 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_auth_timeline_create (priv->auth, &error);
+
+                if (error) {
+                        g_warning (G_STRLOC ": failed to create a timeline: %s",
+                                error->message);
+                        g_clear_error (&error);
+                } else {
+                        RtmTask *task;
+
+                        task = milk_auth_task_add (priv->auth, 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);
+                        } 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)
@@ -173,6 +336,8 @@ task_view_selection_changed_cb (HildonTouchSelector *view,
                 else
                         gtk_widget_hide (w);
         }
+
+        g_list_free (rows);
 }
 
 static GtkWidget*
@@ -225,19 +390,29 @@ contact_column_render_func (GtkCellLayout   *cell_layout,
                             GtkTreeIter     *iter,
                             gpointer         user_data)
 {
-        MilkTask *task;
-        char *title;
+        RtmTask *task;
 
         gtk_tree_model_get (
                         model, iter, MILK_TASK_MODEL_COLUMN_TASK, &task, -1);
+        g_object_set (renderer, "text", rtm_task_get_name (task), NULL);
 
-        g_object_get (task, "title", &title, NULL);
-        g_object_set (renderer, "text", title, NULL);
-
-        g_free (title);
         g_object_unref (task);
 }
 
+static gboolean
+begin_auth_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);
+
+        return FALSE;
+}
+
 static void
 milk_main_window_constructed (GObject* object)
 {
@@ -261,13 +436,18 @@ 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
          */
-        model = GTK_TREE_MODEL (milk_task_model_new ());
+        priv->auth = milk_auth_get_default ();
+        model = GTK_TREE_MODEL (milk_task_model_new (priv->auth));
         w = hildon_touch_selector_new ();
 
 
@@ -306,14 +486,9 @@ milk_main_window_constructed (GObject* object)
         hildon_window_set_app_menu (
                         HILDON_WINDOW (self), HILDON_APP_MENU (priv->app_menu));
 
-        /* FIXME: is there a better place for this? */
-        priv->auth = milk_auth_get_default ();
-        /* FIXME: plug this into the task model */
-
-        /* FIXME: cut this */
-        g_debug ("trying to run the milk auth demo");
-        /* FIXME: cut this */
-        milk_auth_run_demo (priv->auth);
+        /* break a cyclical dependency by doing this after the window is
+         * constructed */
+        g_idle_add ((GSourceFunc) begin_auth_idle, self);
 }
 
 static void
@@ -337,8 +512,11 @@ milk_main_window_init (MilkMainWindow *self)
 }
 
 GtkWidget*
-milk_main_window_new ()
+milk_main_window_get_default ()
 {
-        return g_object_new (MILK_TYPE_MAIN_WINDOW,
-                             NULL);
+        if (!default_window) {
+                default_window = g_object_new (MILK_TYPE_MAIN_WINDOW, NULL);
+        }
+
+        return default_window;
 }