Support adding a new task from the entry box
authorTravis Reitter <treitter@gmail.com>
Wed, 2 Dec 2009 07:36:30 +0000 (23:36 -0800)
committerTravis Reitter <treitter@gmail.com>
Fri, 4 Dec 2009 06:01:13 +0000 (22:01 -0800)
src/milk-auth.c
src/milk-auth.h
src/milk-main-window.c

index 37343bf..96155c5 100644 (file)
@@ -216,6 +216,24 @@ milk_auth_timeline_create (MilkAuth  *auth,
         return rtm_glib_timelines_create (priv->rtm_glib, error);
 }
 
+RtmTask*
+milk_auth_task_add (MilkAuth    *auth,
+                    char        *timeline,
+                    const char  *name,
+                    GError     **error)
+{
+        MilkAuthPrivate *priv;
+
+        g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
+
+        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 */
+        return rtm_glib_tasks_add (priv->rtm_glib, timeline, (char*) name, NULL,
+                        TRUE, error);
+}
+
 char*
 milk_auth_task_complete (MilkAuth  *auth,
                          char      *timeline,
index 6f5b6c9..28faf3e 100644 (file)
@@ -77,6 +77,10 @@ GList*        milk_auth_get_tasks       (MilkAuth    *auth,
                                          GError     **error);
 char*         milk_auth_timeline_create (MilkAuth    *auth,
                                          GError     **error);
+RtmTask*      milk_auth_task_add        (MilkAuth    *auth,
+                                         char        *timeline,
+                                         const char  *name,
+                                         GError     **error);
 char*         milk_auth_task_complete   (MilkAuth    *auth,
                                          char        *timeline,
                                          RtmTask     *task,
index b22c24f..c9a1817 100644 (file)
@@ -34,6 +34,8 @@ 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
@@ -241,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)
@@ -367,8 +436,12 @@ 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