2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public
13 * License along with this program; if not, write to the
14 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
15 * Boston, MA 02110-1301 USA
17 * Authors: Travis Reitter <treitter@gmail.com>
23 #include <glib/gi18n.h>
25 #include <hildon/hildon.h>
26 #include <rtm-glib/rtm-glib.h>
28 #include "milk-main-window.h"
29 #include "milk-auth.h"
30 #include "milk-task-model.h"
32 G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW)
34 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
35 #define MILK_MAIN_WINDOW_PRIVATE(o) ((MILK_MAIN_WINDOW ((o)))->priv)
37 static GtkWidget *default_window = NULL;
39 struct _MilkMainWindowPrivate
47 GtkWidget *new_task_entry;
49 GtkWidget *task_selector;
53 TASK_VIEW_COLUMN_TITLE,
58 const char *display_name;
64 milk_main_window_get_property (GObject *object,
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
78 milk_main_window_set_property (GObject *object,
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
92 milk_main_window_dispose (GObject *object)
94 G_OBJECT_CLASS (milk_main_window_parent_class)->dispose (object);
98 new_task_clicked_cb (GtkButton *button,
99 MilkMainWindow *window)
101 g_debug ("FIXME: implement 'new task' action");
104 /* XXX: The latency between clicking "complete" and actually removing the task
105 * from the view after polling the server is very long, so there's an obvious
106 * lag -- it will be completely transparent (and look very fast) as soon as
107 * we've got a cache in place */
109 complete_clicked_cb (GtkButton *button,
110 MilkMainWindow *window)
112 MilkMainWindowPrivate *priv;
116 GError *error = NULL;
118 priv = MILK_MAIN_WINDOW_PRIVATE (window);
120 rows = hildon_touch_selector_get_selected_rows (
121 HILDON_TOUCH_SELECTOR (priv->task_view),
122 TASK_VIEW_COLUMN_TITLE);
123 model = hildon_touch_selector_get_model (
124 HILDON_TOUCH_SELECTOR (priv->task_view),
125 TASK_VIEW_COLUMN_TITLE);
127 timeline = milk_auth_timeline_create (priv->auth, &error);
130 g_warning (G_STRLOC ": failed to create a timeline: %s",
132 g_clear_error (&error);
138 gtk_tree_model_get_iter (model, &iter, rows->data);
139 gtk_tree_model_get (model, &iter,
140 MILK_TASK_MODEL_COLUMN_TASK, &task,
143 milk_auth_task_complete (priv->auth, timeline, task,
146 g_warning (G_STRLOC ": failed to complete task "
148 rtm_task_get_id (task),
150 g_clear_error (&error);
153 rows = g_list_delete_link (rows, rows);
158 /* XXX: high latency until we have a cache; see the note for
159 * complete_clicked_cb() */
161 delete_clicked_cb (GtkButton *button,
162 MilkMainWindow *window)
164 MilkMainWindowPrivate *priv;
168 GError *error = NULL;
170 priv = MILK_MAIN_WINDOW_PRIVATE (window);
172 rows = hildon_touch_selector_get_selected_rows (
173 HILDON_TOUCH_SELECTOR (priv->task_view),
174 TASK_VIEW_COLUMN_TITLE);
175 model = hildon_touch_selector_get_model (
176 HILDON_TOUCH_SELECTOR (priv->task_view),
177 TASK_VIEW_COLUMN_TITLE);
179 timeline = milk_auth_timeline_create (priv->auth, &error);
182 g_warning (G_STRLOC ": failed to create a timeline: %s",
184 g_clear_error (&error);
190 gtk_tree_model_get_iter (model, &iter, rows->data);
191 gtk_tree_model_get (model, &iter,
192 MILK_TASK_MODEL_COLUMN_TASK, &task,
195 milk_auth_task_delete (priv->auth, timeline, task,
198 g_warning (G_STRLOC ": failed to delete task "
200 rtm_task_get_id (task),
202 g_clear_error (&error);
205 rows = g_list_delete_link (rows, rows);
211 priority_plus_clicked_cb (GtkButton *button,
212 MilkMainWindow *window)
214 g_debug ("FIXME: implement 'priority plus' action");
218 priority_minus_clicked_cb (GtkButton *button,
219 MilkMainWindow *window)
221 g_debug ("FIXME: implement 'priority minus' action");
225 edit_clicked_cb (GtkButton *button,
226 MilkMainWindow *window)
228 g_debug ("FIXME: implement 'edit' action");
231 static MenuItem menu_items_always_shown[] = {
232 {"New Task", "menu-item-new-task", new_task_clicked_cb},
235 static MenuItem menu_items_selection_required[] = {
236 {"Edit", "menu-item-edit", edit_clicked_cb},
237 {"Priority +", "menu-item-priority_plus", priority_plus_clicked_cb},
238 {"Priority -", "menu-item-priority_minus", priority_minus_clicked_cb},
239 {"Complete", "menu-item-complete", complete_clicked_cb},
240 {"Delete", "menu-item-delete", delete_clicked_cb},
244 task_view_selection_changed_cb (HildonTouchSelector *view,
246 MilkMainWindow *window)
248 MilkMainWindowPrivate *priv;
250 gboolean show = FALSE;
253 priv = MILK_MAIN_WINDOW_PRIVATE (window);
255 rows = hildon_touch_selector_get_selected_rows (view, column);
256 show = (g_list_length (rows) > 0);
258 for (i = 0; i < G_N_ELEMENTS (menu_items_selection_required); i++) {
261 w = g_object_get_data (
262 G_OBJECT (priv->app_menu),
263 menu_items_selection_required[i].id);
275 create_menu (gpointer user_data)
278 MenuItem *menu_array;
282 menu = HILDON_APP_MENU (hildon_app_menu_new ());
284 menu_array = menu_items_always_shown;
285 length = G_N_ELEMENTS (menu_items_always_shown);
286 for (i = 0; i < length; i++) {
287 w = hildon_button_new_with_text (
288 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
289 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
290 _(menu_array[i].display_name), "");
291 g_signal_connect (w, "clicked",
292 G_CALLBACK (menu_array[i].callback), user_data);
293 g_object_set_data (G_OBJECT (menu), menu_array[i].id, w);
294 hildon_app_menu_append (menu, GTK_BUTTON (w));
298 menu_array = menu_items_selection_required;
299 length = G_N_ELEMENTS (menu_items_selection_required);
300 for (i = 0; i < length; i++) {
301 w = hildon_button_new_with_text (
302 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
303 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
304 menu_array[i].display_name, "");
305 g_signal_connect (w, "clicked",
306 G_CALLBACK (menu_array[i].callback), user_data);
307 g_object_set_data (G_OBJECT (menu), menu_array[i].id, w);
308 hildon_app_menu_append (menu, GTK_BUTTON (w));
312 gtk_widget_show (GTK_WIDGET (menu));
314 return GTK_WIDGET (menu);
318 contact_column_render_func (GtkCellLayout *cell_layout,
319 GtkCellRenderer *renderer,
327 model, iter, MILK_TASK_MODEL_COLUMN_TASK, &task, -1);
328 g_object_set (renderer, "text", rtm_task_get_name (task), NULL);
330 g_object_unref (task);
334 begin_auth_idle (MilkMainWindow *window)
336 MilkMainWindowPrivate *priv;
338 priv = MILK_MAIN_WINDOW_PRIVATE (window);
340 /* FIXME: cut this */
341 g_debug ("trying to run the milk auth demo");
342 milk_auth_log_in (priv->auth);
348 milk_main_window_constructed (GObject* object)
350 MilkMainWindow *self = MILK_MAIN_WINDOW (object);
351 MilkMainWindowPrivate *priv = MILK_MAIN_WINDOW_PRIVATE (object);
354 GtkCellRenderer *renderer;
355 HildonTouchSelectorColumn *col;
357 w = gtk_vbox_new (FALSE, HILDON_MARGIN_DEFAULT);
358 gtk_container_add (GTK_CONTAINER (self), w);
364 w = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
365 gtk_box_pack_start (GTK_BOX (priv->main_vbox), w, FALSE, FALSE, 0);
367 /* FIXME: change this to hildon_gtk_entry_set_placeholder_text() is
368 * fixed, since this is deprecated */
369 hildon_entry_set_placeholder (HILDON_ENTRY (w),
370 _("Enter a new task..."));
371 priv->new_task_entry = w;
376 priv->auth = milk_auth_get_default ();
377 model = GTK_TREE_MODEL (milk_task_model_new (priv->auth));
378 w = hildon_touch_selector_new ();
381 renderer = gtk_cell_renderer_text_new ();
382 g_object_set (renderer,
383 "ellipsize", PANGO_ELLIPSIZE_END,
386 col = hildon_touch_selector_append_column (
387 HILDON_TOUCH_SELECTOR (w), model, NULL, NULL);
388 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (col), renderer, TRUE);
389 gtk_cell_layout_set_cell_data_func (
390 GTK_CELL_LAYOUT (col), renderer,
391 (GtkCellLayoutDataFunc) contact_column_render_func,
393 g_object_unref (model);
395 hildon_touch_selector_set_column_selection_mode (
396 HILDON_TOUCH_SELECTOR (w),
397 HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
398 hildon_touch_selector_set_hildon_ui_mode (
399 HILDON_TOUCH_SELECTOR (w), HILDON_UI_MODE_EDIT);
400 hildon_touch_selector_unselect_all (
401 HILDON_TOUCH_SELECTOR (w), TASK_VIEW_COLUMN_TITLE);
403 gtk_box_pack_start (GTK_BOX (priv->main_vbox), w, TRUE, TRUE, 0);
404 g_object_set (w, "can-focus", TRUE, NULL);
405 gtk_widget_grab_focus (w);
408 G_OBJECT (w), "changed",
409 G_CALLBACK (task_view_selection_changed_cb), self);
412 priv->app_menu = create_menu (self);
413 hildon_window_set_app_menu (
414 HILDON_WINDOW (self), HILDON_APP_MENU (priv->app_menu));
416 /* break a cyclical dependency by doing this after the window is
418 g_idle_add ((GSourceFunc) begin_auth_idle, self);
422 milk_main_window_class_init (MilkMainWindowClass *klass)
424 GObjectClass *object_class = G_OBJECT_CLASS (klass);
426 g_type_class_add_private (klass, sizeof (MilkMainWindowPrivate));
428 object_class->get_property = milk_main_window_get_property;
429 object_class->set_property = milk_main_window_set_property;
430 object_class->constructed = milk_main_window_constructed;
431 object_class->dispose = milk_main_window_dispose;
435 milk_main_window_init (MilkMainWindow *self)
437 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
438 self, MILK_TYPE_MAIN_WINDOW, MilkMainWindowPrivate);
442 milk_main_window_get_default ()
444 if (!default_window) {
445 default_window = g_object_new (MILK_TYPE_MAIN_WINDOW, NULL);
448 return default_window;