X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fmilk-main-window.c;h=c4d7bb2592b123d04eae9c6fd277e73215117687;hb=HEAD;hp=70d53a59c83def5c1db04fdea031eb85805a538e;hpb=1f2ac8914917faa01dcce1de239c8324cebc4cc7;p=milk diff --git a/src/milk-main-window.c b/src/milk-main-window.c index 70d53a5..c4d7bb2 100644 --- a/src/milk-main-window.c +++ b/src/milk-main-window.c @@ -23,9 +23,10 @@ #include #include #include +#include #include "milk-main-window.h" -#include "milk-task.h" +#include "milk-cache.h" #include "milk-task-model.h" G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW) @@ -33,8 +34,14 @@ 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 { + MilkCache *cache; + GtkWidget *app_menu; GtkWidget *main_vbox; @@ -96,32 +103,195 @@ 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 priority_plus_clicked_cb (GtkButton *button, MilkMainWindow *window) { - g_debug ("FIXME: implement 'priority plus' 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) { + const char *priority; + const char *priority_new; + + priority = rtm_task_get_priority (tasks->data); + + if (FALSE) { + } else if (!g_strcmp0 (priority, "1")) { + priority_new = "1"; + } else if (!g_strcmp0 (priority, "2")) { + priority_new = "1"; + } else if (!g_strcmp0 (priority, "3")) { + priority_new = "2"; + } else if (!g_strcmp0 (priority, "N")) { + priority_new = "3"; + } else { + g_warning ("unknown priority '%s'; setting to " + "unprioritized", priority); + priority_new = "N"; + } + + milk_cache_task_set_priority (priv->cache, timeline, + tasks->data, priority_new, NULL); + tasks = g_list_delete_link (tasks, tasks); + } + } + + /* XXX: it would be nicer to select the tasks in their new positions */ + hildon_touch_selector_unselect_all ( + HILDON_TOUCH_SELECTOR (priv->task_view), + TASK_VIEW_COLUMN_TITLE); } static void priority_minus_clicked_cb (GtkButton *button, MilkMainWindow *window) { - g_debug ("FIXME: implement 'priority minus' 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) { + const char *priority; + const char *priority_new; + + priority = rtm_task_get_priority (tasks->data); + + if (FALSE) { + } else if (!g_strcmp0 (priority, "1")) { + priority_new = "2"; + } else if (!g_strcmp0 (priority, "2")) { + priority_new = "3"; + } else if (!g_strcmp0 (priority, "3")) { + priority_new = "N"; + } else if (!g_strcmp0 (priority, "N")) { + priority_new = "N"; + } else { + g_warning ("unknown priority '%s'; setting to " + "unprioritized", priority); + priority_new = "N"; + } + + milk_cache_task_set_priority (priv->cache, timeline, + tasks->data, priority_new, NULL); + tasks = g_list_delete_link (tasks, tasks); + } + } + + /* XXX: it would be nicer to select the tasks in their new positions */ + hildon_touch_selector_unselect_all ( + HILDON_TOUCH_SELECTOR (priv->task_view), + TASK_VIEW_COLUMN_TITLE); } static void @@ -144,6 +314,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) @@ -170,6 +411,8 @@ task_view_selection_changed_cb (HildonTouchSelector *view, else gtk_widget_hide (w); } + + g_list_free (rows); } static GtkWidget* @@ -216,25 +459,69 @@ 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) { - MilkTask *task; - char *title; + RtmTask *task; + const char *priority; + GdkColor color = {}; + gboolean color_set = TRUE; gtk_tree_model_get ( model, iter, MILK_TASK_MODEL_COLUMN_TASK, &task, -1); - g_object_get (task, "title", &title, NULL); - g_object_set (renderer, "text", title, NULL); + 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; + + 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_free (title); g_object_unref (task); } +static gboolean +begin_cache_idle (MilkMainWindow *window) +{ + MilkMainWindowPrivate *priv; + + priv = MILK_MAIN_WINDOW_PRIVATE (window); + + milk_cache_authenticate (priv->cache); + + return FALSE; +} + static void milk_main_window_constructed (GObject* object) { @@ -258,8 +545,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 @@ -267,19 +558,32 @@ milk_main_window_constructed (GObject* object) 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, - (GtkTreeCellDataFunc) contact_column_render_func, self, - NULL); + (GtkCellLayoutDataFunc) name_column_render_func, + self, NULL); + g_object_unref (model); hildon_touch_selector_set_column_selection_mode ( @@ -302,6 +606,13 @@ milk_main_window_constructed (GObject* object) priv->app_menu = create_menu (self); 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_cache_idle, self); } static void @@ -325,8 +636,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; }