2009-07-14 Manuel Rego Casasnovas <mrego@igalia.com>
[tarefas:master.git] / ggtd / ggtd-tasks-window.c
1 /* ggtd-tasks-window.c
2  *
3  * Copyright (C) 2009 GTD Development Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  *
20  */
21
22 #include "ggtd-tasks-window.h"
23 #include "ggtd-edit-task-dialog.h"
24 #include <gtk/gtk.h>
25 #include <dbus/dbus-glib.h>
26 #include <string.h>
27 #include <glib/gi18n.h>
28 #include "config.h"
29 #include "daemon/gtd-error.h"
30 #include "daemon/gtd-defines.h"
31 #include "ggtd-utils.h"
32 #include <gconf/gconf-client.h>
33
34 /* Global variable wich stores the DBusGProxy object */
35 extern DBusGProxy *remote_object;
36
37 static void ggtd_tasks_window_finalize (GObject *object);
38 static void init_actions (GGTDTasksWindow *tasks_window);
39 static void create_layout (GGTDTasksWindow *self);
40 static void create_model (GGTDTasksWindow *tasks_window);
41 static void on_quit (GtkAction *action, GGTDTasksWindow *tasks_window);
42 void tasks_window_update_task (GGTDTasksWindow *tasks_window, GGTDTaskData *task_data);
43 void tasks_window_add_task (GGTDTasksWindow *tasks_window, GGTDTaskData *task_data);
44 gboolean get_task_data (GGTDTasksWindow *tasks_window, GGTDTaskData **task_data);
45 static void on_new_edit_task (GtkAction *action, GGTDTasksWindow *tasks_window);
46 static void on_change_task (GtkAction *action, GGTDTasksWindow *tasks_window);
47 static void on_tasks_row_activated_cb (GtkTreeView *tree_view,
48                                        GtkTreePath *path,
49                                        GtkTreeViewColumn *column,
50                                        gpointer user_data);
51 static void on_helpcontents (GtkAction *action,
52                              GGTDTasksWindow *tasks_window);
53 static void fill_tasks (GGTDTasksWindow *tasks_window);
54 static void on_back_to_list_view (GtkAction *action, GGTDTasksWindow *tasks_window);
55 static void on_changed_list_combo (GtkAction *action, GGTDTasksWindow *tasks_window);
56 static void on_changed_priority_combo (GtkAction *action, GGTDTasksWindow *tasks_window);
57 static void on_refresh (GtkAction *action, GGTDTasksWindow *tasks_window);
58 static void on_accounts (GtkAction *action, GGTDTasksWindow *tasks_window);
59 static void on_preferences (GtkAction *action, GGTDTasksWindow *tasks_window);
60
61 G_DEFINE_TYPE (GGTDTasksWindow, ggtd_tasks_window, GTK_TYPE_WINDOW)
62
63 #define GGTD_TASKS_WINDOW_GET_PRIVATE(o)                                 \
64   (G_TYPE_INSTANCE_GET_PRIVATE((o), GGTD_TYPE_TASKS_WINDOW, GGTDTasksWindowPrivate))
65
66 typedef struct _GGTDTasksWindowPrivate GGTDTasksWindowPrivate;
67
68 struct _GGTDTasksWindowPrivate {
69   GtkWidget *menu;
70   GtkToolbar *toolbar;
71   GtkUIManager *ui_manager;
72   GtkWidget *tasks_treeview;
73   GtkWidget *list_combo;
74   GtkWidget *priority_combo;
75   gchar *list_id;
76   gchar *priority;
77 };
78
79 enum {
80   PROP_0,
81   PROP_LIST_ID
82 };
83
84 enum {
85   COLUMN_LIST_ID,
86   COLUMN_TASKSERIES_ID,
87   COLUMN_TASK_ID,
88   COLUMN_TASK_NAME,
89   COLUMN_TASK_PRIORITY,
90   COLUMN_TASK_DUE,
91   N_COLUMNS
92 };
93
94 enum {
95   COLUMN_COMBO_LIST_ID,
96   COLUMN_COMBO_LIST_NAME,
97   N_COLUMNS_COMBO
98 };
99
100 static const GtkActionEntry tasks_window_action_entries [] = {
101   /* Gtd Menu */
102   {"Gtd", NULL, N_("_GTD")},
103   {"NewTask", GTK_STOCK_NEW, N_("_New Task"),"<control>N", N_("Create a new task"), G_CALLBACK (on_new_edit_task)},
104   {"EditTask", GTK_STOCK_EDIT, N_("_Edit Task"),"<control>E", N_("Edit selected task"), G_CALLBACK (on_new_edit_task)},
105   {"ReloadTasks", GTK_STOCK_REFRESH, N_("_Reload Tasks"),"<control>R", N_("Reload the task list"), G_CALLBACK (on_refresh)},
106   {"TaskDelete", GTK_STOCK_DELETE, N_("_Delete Task/s"),"<control>D", N_("Delete selected task/s"), G_CALLBACK (on_change_task)},
107   {"TaskComplete", GTK_STOCK_APPLY, N_("_Complete Task/s"),"<control>C", N_("Complete selected task/s"), G_CALLBACK (on_change_task)},
108   {"TaskUncomplete", GTK_STOCK_CANCEL, N_("_Uncomplete Task/s"),"<control>U", N_("Uncomplete selected task/s"), G_CALLBACK (on_change_task)},
109   {"TaskPostpone", GTK_STOCK_GO_FORWARD, N_("_Postpone Task/s"),"<control>P", N_("Postpone selected task/s"), G_CALLBACK (on_change_task)},
110   {"BackToListsView", GTK_STOCK_INDEX, N_("_Back to List View"),"<control>B", N_("Back to list view"), G_CALLBACK (on_back_to_list_view)},
111   {"Quit", GTK_STOCK_QUIT, NULL, NULL, N_("Quit application"), G_CALLBACK (on_quit)},
112
113    /* Settings Menu */
114   {"Settings", NULL, "_Settings"},
115   {"SettingsAccounts", GTK_STOCK_NEW, N_("_Accounts"),"<control>A", N_("Manage your accounts"), G_CALLBACK (on_accounts)},
116   {"SettingsPreferences", GTK_STOCK_PREFERENCES, N_("Pre_ferences"),"<control>f", N_("Manage your preferences"),  G_CALLBACK (on_preferences)},
117
118   /* Help Menu */
119   {"Help", NULL, N_("_Help")},
120   {"HelpContents", GTK_STOCK_INFO, N_("_Contents"), NULL, N_("Help for GGTD"), G_CALLBACK (on_helpcontents)},
121   {"HelpAbout", GTK_STOCK_ABOUT, N_("A_bout"), NULL, N_("About GGTD"), G_CALLBACK (ggtd_utils_helpabout)},
122 };
123
124 static void
125 ggtd_tasks_window_finalize (GObject *object)
126 {
127   G_OBJECT_CLASS (ggtd_tasks_window_parent_class)->finalize (object);
128 }
129
130 static void
131 ggtd_tasks_window_init (GGTDTasksWindow *tasks_window)
132 {
133   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
134
135   priv->menu = NULL;
136   priv->toolbar = NULL;
137   priv->tasks_treeview = NULL;
138   priv->list_combo = NULL;
139   priv->priority_combo = NULL;
140
141   gtk_window_set_default_size (GTK_WINDOW (tasks_window), 500, 400);
142
143   gtk_window_set_title (GTK_WINDOW (tasks_window), _("GNOME Getting Things Done"));
144
145   init_actions (tasks_window);
146   create_layout (tasks_window);
147
148   g_signal_connect (tasks_window,
149                     "delete_event",
150                     G_CALLBACK (gtk_main_quit),
151                     NULL);
152 }
153
154 static void
155 fill_tasks (GGTDTasksWindow *tasks_window)
156 {
157   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
158
159   GtkListStore *tasks_model;
160   GtkTreeIter iter;
161   GError *error = NULL;
162
163   GPtrArray *tasks;
164   GValueArray *task;
165   guint i;
166   const gchar *list_id;
167   const gchar *taskseries_id;
168   const gchar *task_id;
169   const gchar *task_name;
170   const gchar *task_priority;
171   const gchar *task_due;
172   gchar *markup_task_name;
173   gchar *markup_task_priority;
174   gchar *filter;
175
176   tasks_model =
177     GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tasks_treeview)));
178
179   gtk_list_store_clear (tasks_model);
180
181   if ((strcmp (priv->priority, "") == 0) || (strcmp (priv->priority, _("All")) == 0))
182     {
183       filter = g_strdup ("");
184     }
185   else
186     {
187       filter = g_strconcat ("priority:", priv->priority, NULL);
188     }
189
190   if (!dbus_g_proxy_call (remote_object, "TaskGetList", &error,
191                           G_TYPE_STRING, priv->list_id,
192                           G_TYPE_STRING, filter,
193                           G_TYPE_STRING, "",
194                           G_TYPE_INVALID,
195                           dbus_g_type_get_collection ("GPtrArray",
196                                                       GTD_DBUS_STRUCT_TASK),
197                           &tasks,
198                           G_TYPE_INVALID))
199     handle_gerror (_("Failed to get task"), error);
200   else
201     {
202       for (i = 0; i < tasks->len; i++)
203         {
204           task = g_ptr_array_index (tasks, i);
205
206           list_id = g_value_get_string
207             ((const GValue *) g_value_array_get_nth (task, 0));
208           taskseries_id = g_value_get_string
209             ((const GValue *) g_value_array_get_nth (task, 1));
210           task_id = g_value_get_string
211             ((const GValue *) g_value_array_get_nth (task, 2));
212           task_name = g_value_get_string
213             ((const GValue *) g_value_array_get_nth (task, 3));
214           task_priority = g_value_get_string
215             ((const GValue *) g_value_array_get_nth (task, 4));
216           task_due = g_value_get_string
217             ((const GValue *) g_value_array_get_nth (task, 5));
218
219           g_print ("List id %s, taskseries id: %s, task id: %s, name: %s\n", list_id, taskseries_id, task_id, task_name);
220
221           markup_task_name = g_markup_printf_escaped ("%s",
222                                                       task_name);
223
224           markup_task_priority = g_markup_printf_escaped ("%s",
225                                                           task_priority);
226
227           gtk_list_store_append (tasks_model, &iter);
228           gtk_list_store_set (tasks_model,
229                               &iter,
230                               COLUMN_LIST_ID, list_id,
231                               COLUMN_TASKSERIES_ID, taskseries_id,
232                               COLUMN_TASK_ID, task_id,
233                               COLUMN_TASK_NAME, markup_task_name,
234                               COLUMN_TASK_PRIORITY, markup_task_priority,
235                               COLUMN_TASK_DUE, task_due,
236                               -1);
237         }
238
239       g_ptr_array_free (tasks, TRUE);
240     }
241
242   g_free (filter);
243 }
244
245 static void
246 on_tasks_row_activated_cb (GtkTreeView *tree_view,
247                            GtkTreePath *path,
248                            GtkTreeViewColumn *column,
249                            gpointer user_data)
250 {
251   GtkWidget *message_window;
252   GGTDTasksWindow *tasks_window;
253
254   tasks_window = GGTD_TASKS_WINDOW (user_data);
255
256   /* TODO */
257 }
258
259 static void
260 ggtd_tasks_window_get_property (GObject *object,
261                                 guint prop_id,
262                                 GValue *value,
263                                 GParamSpec *pspec)
264 {
265   GGTDTasksWindow *tasks_window = GGTD_TASKS_WINDOW (object);
266   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
267
268   switch (prop_id)
269     {
270     case PROP_LIST_ID:
271       g_value_set_string (value, priv->list_id);
272       break;
273     default:
274       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275       break;
276     }
277 }
278
279 static void
280 ggtd_tasks_window_set_property (GObject *object,
281                                 guint prop_id,
282                                 const GValue *value,
283                                 GParamSpec *pspec)
284 {
285   GGTDTasksWindow *tasks_window = GGTD_TASKS_WINDOW (object);
286   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
287
288   switch (prop_id)
289     {
290     case PROP_LIST_ID:
291       priv->list_id = g_strdup (g_value_get_string (value));
292       g_print ("List_id: %s\n", priv->list_id);
293       break;
294     default:
295       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296       break;
297     }
298 }
299
300 static GObject *
301 ggtd_tasks_window_constructor (GType type,
302                                guint n_construct_properties,
303                                GObjectConstructParam *construct_params)
304 {
305        GObject *object;
306        GGTDTasksWindowPrivate *priv;
307
308        object = G_OBJECT_CLASS (ggtd_tasks_window_parent_class)->constructor (type,
309                                                                               n_construct_properties,
310                                                                               construct_params);
311
312        priv = GGTD_TASKS_WINDOW_GET_PRIVATE (GGTD_TASKS_WINDOW (object));
313
314        fill_list_combo (GGTD_TASKS_WINDOW (object));
315
316        return object;
317 }
318
319 static void
320 ggtd_tasks_window_class_init (GGTDTasksWindowClass *tasks_window_class)
321 {
322   GObjectClass *object_class;
323
324   g_type_class_add_private (tasks_window_class, sizeof (GGTDTasksWindowPrivate));
325
326   object_class = G_OBJECT_CLASS (tasks_window_class);
327
328   object_class->finalize = ggtd_tasks_window_finalize;
329   object_class->constructor = ggtd_tasks_window_constructor;
330   object_class->get_property = ggtd_tasks_window_get_property;
331   object_class->set_property = ggtd_tasks_window_set_property;
332
333   g_object_class_install_property (object_class,
334                                    PROP_LIST_ID,
335                                    g_param_spec_string ("list_id",
336                                                         "List ID",
337                                                         "Identifier of list selected at combo",
338                                                         "",
339                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
340
341 }
342
343 static void
344 init_actions (GGTDTasksWindow *self)
345 {
346   GtkActionGroup *action_group;
347   GError *error = NULL;
348   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (self);
349
350   priv->ui_manager = gtk_ui_manager_new ();
351   action_group = gtk_action_group_new ("GGTDTasksWindowActions");
352   gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
353   gtk_action_group_add_actions (action_group,
354                                 tasks_window_action_entries,
355                                 G_N_ELEMENTS (tasks_window_action_entries),
356                                 self);
357
358   gtk_ui_manager_insert_action_group (priv->ui_manager, action_group, 0);
359   gtk_window_add_accel_group (GTK_WINDOW (self),
360                               gtk_ui_manager_get_accel_group (priv->ui_manager));
361   g_object_unref (action_group);
362
363   gtk_ui_manager_add_ui_from_file (priv->ui_manager,
364                                    GGTD_UIDIR "ggtd-tasks-window.ui",
365                                    &error);
366   if (error != NULL)
367     {
368       g_warning (_("Couldn't merge main window ui: %s"), error->message);
369       g_error_free (error);
370       error = NULL;
371     }
372 }
373
374 static void
375 create_layout (GGTDTasksWindow *self)
376 {
377   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (self);
378   GtkWidget *main_vbox;
379   GtkWidget *sw_tasks;
380   GtkWidget *alignment;
381
382   GtkWidget *hbox;
383   GtkWidget *label_list_combo;
384   GtkWidget *label_priority_combo;
385   GtkTreeSelection *selection;
386
387   priv->toolbar =
388     GTK_TOOLBAR (gtk_ui_manager_get_widget (priv->ui_manager, "/ui/ToolBar"));
389   priv->tasks_treeview = gtk_tree_view_new ();
390
391   selection = gtk_tree_view_get_selection ((GtkTreeView *) priv->tasks_treeview);
392   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
393
394   main_vbox = gtk_vbox_new (FALSE, 0);
395   alignment = gtk_alignment_new (0, 0, 1, 1);
396   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
397                              0,
398                              0,
399                              0,
400                              0);
401
402   sw_tasks = gtk_scrolled_window_new (NULL, NULL);
403   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw_tasks),
404                                   GTK_POLICY_AUTOMATIC,
405                                   GTK_POLICY_AUTOMATIC);
406
407   g_signal_connect (priv->tasks_treeview,
408                     "row-activated",
409                     G_CALLBACK (on_tasks_row_activated_cb),
410                     self);
411
412
413   hbox = gtk_hbox_new (FALSE, 0);
414
415
416   label_list_combo = gtk_label_new (_("List"));
417
418   priv->list_combo = gtk_combo_box_new ();
419
420   g_signal_connect (priv->list_combo, "changed", G_CALLBACK (on_changed_list_combo), self);
421
422   ggtd_utils_add_atk_relation (priv->list_combo, label_list_combo, ATK_RELATION_LABEL_FOR);
423
424
425   priv->priority_combo = gtk_combo_box_new_text ();
426
427   label_priority_combo = gtk_label_new (_("Priority"));
428
429   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->priority_combo), _("All"));
430   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->priority_combo), "3");
431   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->priority_combo), "2");
432   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->priority_combo), "1");
433   gtk_combo_box_append_text (GTK_COMBO_BOX (priv->priority_combo), "N");
434
435   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->priority_combo), 0);
436   priv->priority = g_strdup (_("All"));
437
438   g_signal_connect (priv->priority_combo, "changed", G_CALLBACK (on_changed_priority_combo), self);
439
440   ggtd_utils_add_atk_relation (priv->priority_combo, label_priority_combo, ATK_RELATION_LABEL_FOR);
441
442
443   create_model (self);
444
445   priv->menu = gtk_ui_manager_get_widget (priv->ui_manager, "/ui/MenuBar");
446   gtk_box_pack_start (GTK_BOX (main_vbox), priv->menu, FALSE, FALSE, 0);
447   gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (priv->toolbar), FALSE, FALSE, 0);
448
449   gtk_box_pack_start (GTK_BOX (hbox), label_list_combo, FALSE, FALSE, 0);
450   gtk_box_pack_start (GTK_BOX (hbox), priv->list_combo, TRUE, TRUE, 0);
451   gtk_box_pack_start (GTK_BOX (hbox), label_priority_combo, FALSE, FALSE, 0);
452   gtk_box_pack_start (GTK_BOX (hbox), priv->priority_combo, TRUE, TRUE, 0);
453   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
454
455   gtk_box_pack_start (GTK_BOX (main_vbox), alignment, TRUE, TRUE, 0);
456   gtk_container_add (GTK_CONTAINER (alignment), sw_tasks);
457   gtk_container_add (GTK_CONTAINER (sw_tasks), priv->tasks_treeview);
458   gtk_container_add (GTK_CONTAINER (self), main_vbox);
459   gtk_widget_show_all (main_vbox);
460 }
461
462 static void
463 create_model (GGTDTasksWindow *tasks_window)
464 {
465   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
466
467   GtkListStore *tasks_model;
468   GtkListStore *lists_model;
469   GtkCellRenderer *renderer;
470   GtkTreeViewColumn *column;
471
472   g_object_set (GTK_TREE_VIEW (priv->tasks_treeview),
473                 "headers-visible", FALSE,
474                 "reorderable", TRUE,
475                 "rules-hint", TRUE,
476                 NULL);
477
478   tasks_model = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
479                                     G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
480                                     G_TYPE_STRING);
481   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tasks_treeview),
482                            GTK_TREE_MODEL (tasks_model));
483
484   renderer = gtk_cell_renderer_text_new ();
485   column = gtk_tree_view_column_new_with_attributes ("Task",
486                                                      renderer,
487                                                      "markup", COLUMN_TASK_NAME,
488                                                      NULL);
489   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tasks_treeview), column);
490   renderer = gtk_cell_renderer_text_new ();
491
492   column = gtk_tree_view_column_new_with_attributes ("Priority",
493                                                      renderer,
494                                                      "markup", COLUMN_TASK_PRIORITY,
495                                                      NULL);
496   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tasks_treeview), column);
497
498   lists_model = gtk_list_store_new (N_COLUMNS_COMBO, G_TYPE_STRING,
499                                     G_TYPE_STRING);
500   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->list_combo),
501                            GTK_TREE_MODEL (lists_model));
502
503   renderer = gtk_cell_renderer_text_new ();
504   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->list_combo),
505                               renderer,
506                               TRUE);
507   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->list_combo),
508                                  renderer,
509                                  "text",
510                                  COLUMN_COMBO_LIST_NAME);
511 }
512
513
514 static void
515 on_quit (GtkAction *action,
516          GGTDTasksWindow *tasks_window)
517 {
518   GtkWidget *dialog;
519
520   dialog = gtk_message_dialog_new (GTK_WINDOW (tasks_window),
521                                    GTK_DIALOG_MODAL |
522                                    GTK_DIALOG_DESTROY_WITH_PARENT,
523                                    GTK_MESSAGE_QUESTION,
524                                    GTK_BUTTONS_YES_NO,
525                                    _("Are you sure you want to quit?"));
526
527   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
528     {
529       gtk_main_quit ();
530     }
531
532   gtk_widget_destroy (dialog);
533 }
534
535 static void
536 on_helpcontents (GtkAction *action, GGTDTasksWindow *tasks_window)
537 {
538 }
539
540 static void
541 close_userinfo_window(GtkButton *button,
542                       gpointer   user_data)
543 {
544   gtk_widget_destroy(GTK_WIDGET(user_data));
545 }
546
547 GtkWidget *
548 ggtd_tasks_window_new (gchar *list_id)
549 {
550   GtkWidget *tasks_window = g_object_new (GGTD_TYPE_TASKS_WINDOW, "list_id", list_id, NULL);
551   return tasks_window;
552 }
553
554 void
555 fill_list_combo (GGTDTasksWindow *tasks_window)
556 {
557   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
558
559   GError *error = NULL;
560
561   GPtrArray *lists;
562   GValueArray *list;
563   guint i;
564   const gchar *list_id;
565   const gchar *list_name;
566
567   GtkListStore *list_store;
568   GtkTreeIter iter;
569
570   GConfClient *gconf_client;
571   gchar *default_account;
572
573   gconf_client = gconf_client_get_default();
574
575   default_account = gconf_client_get_string (gconf_client, "/apps/gtd/default_account", NULL);
576
577   if (g_strcmp0 (default_account, "EMPTY"))
578     {
579       
580       if (!dbus_g_proxy_call (remote_object, "ListGetList", &error,
581                               G_TYPE_INVALID,
582                               dbus_g_type_get_collection ("GPtrArray",
583                                                           GTD_DBUS_STRUCT_LIST),
584                               &lists,
585                               G_TYPE_INVALID))
586         handle_gerror (_("Failed to get list"), error);
587       else
588         {
589           
590           list_store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (priv->list_combo)));
591           gtk_list_store_clear (list_store);
592           
593           for (i = 0; i < lists->len; i++)
594             {
595               list = g_ptr_array_index(lists, i);
596               
597               list_id = g_value_get_string
598                 ((const GValue *) g_value_array_get_nth (list, 0));
599               list_name = g_value_get_string
600                 ((const GValue *) g_value_array_get_nth (list, 1));
601               
602               g_print ("List id: %s, name: %s\n", list_id, list_name);
603               
604               gtk_list_store_append (list_store, &iter);
605               gtk_list_store_set (list_store,
606                                   &iter,
607                                   COLUMN_COMBO_LIST_ID,
608                                   list_id,
609                                   COLUMN_COMBO_LIST_NAME,
610                                   list_name,
611                                   -1);
612               
613               if ((strcmp (priv->list_id, "") == 0) && (strcmp (list_name, "Inbox") == 0))
614                 {
615                   priv->list_id = (gchar *) list_id;
616                   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->list_combo),
617                                                  &iter);
618                 }
619               else
620                 {
621                   
622                   if (strcmp (priv->list_id, list_id) == 0)
623                     {
624                       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->list_combo),
625                                                      &iter);
626                     }
627                 }
628               
629             }
630           
631           g_ptr_array_free (lists, TRUE);
632         }
633     }  
634 }
635
636 void
637 tasks_window_update_task (GGTDTasksWindow *tasks_window, GGTDTaskData *task_data)
638 {
639   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
640
641   GtkTreeSelection *selection;
642   GList *selected_rows;
643   GtkTreeModel *model;
644   GtkTreePath *path;
645   GtkTreeIter iter;
646   GtkTreeRowReference *ref;
647
648   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tasks_treeview));
649   if (gtk_tree_selection_count_selected_rows (selection) == 1)
650     {
651       selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
652       path = selected_rows->data;
653
654       if (gtk_tree_model_get_iter (model, &iter, path))
655         {
656           gtk_list_store_set (GTK_LIST_STORE (model),
657                               &iter,
658                               COLUMN_LIST_ID, task_data->list_id,
659                               COLUMN_TASKSERIES_ID, task_data->taskseries_id,
660                               COLUMN_TASK_ID, task_data->task_id,
661                               COLUMN_TASK_NAME, task_data->name,
662                               COLUMN_TASK_PRIORITY, task_data->priority,
663                               COLUMN_TASK_DUE, task_data->due,
664                               -1);
665           g_print ("tasks_window_update_task: List id %s, taskseries id: %s, task id: %s, name: %s, priority: %s, due: %s\n",
666                    task_data->list_id, task_data->taskseries_id, task_data->task_id, task_data->name, task_data->priority, task_data->due);
667         }
668       else
669         {
670           g_print ("Error in getting the iter\n");
671         }
672     }
673 }
674
675 void
676 tasks_window_add_task (GGTDTasksWindow *tasks_window, GGTDTaskData *task_data)
677 {
678   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
679
680   GtkListStore *tasks_model;
681   GtkTreeIter iter;
682
683   tasks_model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tasks_treeview)));
684
685   gtk_list_store_append (tasks_model, &iter);
686   gtk_list_store_set (tasks_model,
687                       &iter,
688                       COLUMN_LIST_ID, task_data->list_id,
689                       COLUMN_TASKSERIES_ID, task_data->taskseries_id,
690                       COLUMN_TASK_ID, task_data->task_id,
691                       COLUMN_TASK_NAME, task_data->name,
692                       COLUMN_TASK_PRIORITY, task_data->priority,
693                       COLUMN_TASK_DUE, task_data->due,
694                       -1);
695   g_print ("tasks_window_add_task: List id %s, taskseries id: %s, task id: %s, name: %s, priority: %s, due: %s\n",
696            task_data->list_id, task_data->taskseries_id, task_data->task_id, task_data->name, task_data->priority, task_data->due);
697
698 }
699
700 gboolean
701 get_task_data (GGTDTasksWindow *tasks_window,
702                GGTDTaskData **task_data)
703 {
704   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
705   GtkTreeSelection *selection;
706   GList *selected_rows;
707   GtkTreeModel *model;
708   GtkTreePath *path;
709   GtkTreeIter iter;
710   GtkWidget *dialog;
711
712   gchar *list_id, *taskseries_id, *task_id, *name, *priority;
713
714   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tasks_treeview));
715   if (gtk_tree_selection_count_selected_rows (selection) == 1)
716     {
717       selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
718       path = selected_rows->data;
719
720       if (gtk_tree_model_get_iter (model, &iter, path))
721         {
722           gtk_tree_model_get (model, &iter,
723                               COLUMN_LIST_ID, &((*task_data)->list_id),
724                               COLUMN_TASKSERIES_ID, &((*task_data)->taskseries_id),
725                               COLUMN_TASK_ID, &((*task_data)->task_id),
726                               COLUMN_TASK_NAME, &((*task_data)->name),
727                               COLUMN_TASK_PRIORITY, &((*task_data)->priority),
728                               COLUMN_TASK_DUE, &((*task_data)->due),
729                               -1);
730           g_print ("get_task_data: List id %s, taskseries id: %s, task id: %s, name: %s, priority: %s , due:%s\n",
731                    (*task_data)->list_id, (*task_data)->taskseries_id, (*task_data)->task_id,
732                    (*task_data)->name, (*task_data)->priority, (*task_data)->due);
733         }
734       gtk_tree_path_free (path);
735       g_list_free(selected_rows);
736
737       return TRUE;
738     }
739   else
740     {
741       dialog = gtk_message_dialog_new (GTK_WINDOW (tasks_window),
742                                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
743                                        GTK_MESSAGE_WARNING,
744                                        GTK_BUTTONS_OK,
745                                        _("Select one task please"));
746
747       gtk_dialog_run (GTK_DIALOG (dialog));
748       gtk_widget_destroy (dialog);
749
750       return FALSE;
751     }
752 }
753
754 static void
755 on_new_edit_task (GtkAction *action,
756                   GGTDTasksWindow *tasks_window)
757 {
758   const gchar *action_name;
759   GtkWidget *edittask_dialog = NULL;
760   GGTDTaskData *task_data = NULL;
761   gint result;
762
763   action_name = gtk_action_get_name (action);
764
765   task_data = g_slice_new0 (GGTDTaskData);
766
767   if (g_strcmp0 (action_name, "EditTask") == 0)
768     {
769       if (get_task_data (tasks_window, &task_data) == TRUE)
770         edittask_dialog = GTK_WIDGET (ggtd_edit_task_dialog (FALSE,
771                                                              &task_data, tasks_window));
772     }
773   else
774     edittask_dialog = GTK_WIDGET (ggtd_edit_task_dialog (TRUE,
775                                                          &task_data, tasks_window));
776
777   if (edittask_dialog != NULL)
778     {
779       result = gtk_dialog_run (GTK_DIALOG (edittask_dialog));
780
781       switch (result)
782         {
783         case GTK_RESPONSE_ACCEPT:
784           g_print ("Button OK clicked in dialog\n");
785           if (g_strcmp0 (action_name, "EditTask") == 0)
786             tasks_window_update_task (tasks_window, task_data);
787           else
788             tasks_window_add_task (tasks_window, task_data);
789           break;
790         case GTK_RESPONSE_DELETE_EVENT:
791           g_print ("Dialog closed\n");
792           break;
793         default:
794           if (g_strcmp0 (action_name, "EditTask") == 0)
795             tasks_window_update_task (tasks_window, task_data);
796           else
797             tasks_window_add_task (tasks_window, task_data);
798           break;
799         }
800       gtk_widget_destroy (GTK_WIDGET (edittask_dialog));
801     }
802   g_slice_free (GGTDTaskData, task_data);
803 }
804
805 static void
806 on_change_task (GtkAction *action,
807                 GGTDTasksWindow *tasks_window)
808 {
809   const gchar *action_name;
810   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
811   GtkTreeSelection  *selection;
812   GtkTreeModel *model;
813   GtkTreeIter iter;
814   GList *selected_rows, *current;
815   GtkTreePath *path;
816   GtkTreeRowReference *ref;
817
818   gchar *list_id, *taskseries_id, *task_id, *task_name, *task_priority;
819
820   GError *error = NULL;
821
822   action_name = gtk_action_get_name (action);
823   
824   g_print ("%s\n", action_name);  
825
826   selection = gtk_tree_view_get_selection ((GtkTreeView *) priv->tasks_treeview);
827   selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
828
829   if (selected_rows)
830     {
831       if (g_strcmp0 (action_name, "TaskDelete") == 0)
832         {
833           for (current = selected_rows; current != NULL; current = current->next)            
834             {
835               path = current->data;
836               ref = gtk_tree_row_reference_new (model, current->data);
837               current->data = ref;
838               gtk_tree_path_free (path);
839             }
840         }
841
842         for (current = selected_rows; current != NULL; current = current->next)
843           {
844             if (g_strcmp0 (action_name, "TaskDelete") == 0)
845               {
846                 ref = current->data;
847                 path = gtk_tree_row_reference_get_path (ref);
848               }
849             else
850               path = current->data;
851
852           if (gtk_tree_model_get_iter (model, &iter, path))
853             {
854               gtk_tree_model_get (model,
855                                   &iter,
856                                   COLUMN_LIST_ID, &list_id,
857                                   COLUMN_TASKSERIES_ID, &taskseries_id,
858                                   COLUMN_TASK_ID, &task_id,
859                                   COLUMN_TASK_NAME, &task_name,
860                                   COLUMN_TASK_PRIORITY, &task_priority,
861                                   -1);
862
863               g_print ("Action: %s ; task: %s\n", action_name, task_name);
864               g_print ("List id %s, taskseries id: %s, task id: %s, name: %s, priority: %s\n",
865                        list_id, taskseries_id, task_id, task_name, task_priority);
866
867               if (!dbus_g_proxy_call (remote_object, action_name, &error,
868                                       G_TYPE_STRING, list_id,
869                                       G_TYPE_STRING, taskseries_id,
870                                       G_TYPE_STRING, task_id,
871                                       G_TYPE_INVALID,
872                                       G_TYPE_INVALID))
873                 handle_gerror (_("Failed to change the task"), error);
874               else
875                 {
876                   if (g_strcmp0 (action_name, "TaskDelete") == 0)
877                     gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
878                   if (g_strcmp0 (action_name, "TaskComplete") == 0)
879                     {
880                       /* TODO */
881                     }
882                   if (g_strcmp0 (action_name, "TaskUnComplete") == 0)
883                     {
884                       /* TODO */
885                     }
886                   g_print ("Task %s\n", action_name);
887                 }
888
889               g_free (list_id);
890               g_free (taskseries_id);
891               g_free (task_id);
892               g_free (task_name);
893               g_free (task_priority);
894             }
895
896           gtk_tree_path_free (path);
897           gtk_tree_row_reference_free (ref);
898         }
899
900       g_list_free(selected_rows);
901
902     }
903 }
904
905 static void
906 on_back_to_list_view (GtkAction *action,
907                       GGTDTasksWindow *tasks_window)
908 {
909   GtkWidget *main_window = NULL;
910
911   main_window = ggtd_main_window_new();
912   gtk_widget_show_all (main_window);
913   gtk_widget_destroy(tasks_window);
914 }
915
916 static void
917 on_changed_list_combo (GtkAction *action,
918                        GGTDTasksWindow *tasks_window)
919 {
920   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
921   GtkListStore *list_store;
922   GtkTreeIter iter;
923   gchar *list_id;
924   gchar *list_name;
925   GtkTreeModel *tree_model;
926
927   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->list_combo), &iter))
928     {
929       list_store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (priv->list_combo)));
930       gtk_tree_model_get (GTK_TREE_MODEL (list_store),
931                           &iter,
932                           COLUMN_COMBO_LIST_ID,
933                           &list_id,
934                           COLUMN_COMBO_LIST_NAME,
935                           &list_name,
936                           -1);
937
938       g_print ("List id: %s, name: %s\n", list_id, list_name);
939
940       g_free (priv->list_id);
941       priv->list_id = list_id;
942
943       g_free (list_name);
944
945       fill_tasks (tasks_window);
946     }
947 }
948
949 static void
950 on_changed_priority_combo (GtkAction *action,
951                            GGTDTasksWindow *tasks_window)
952 {
953
954   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
955
956   gchar *priority = gtk_combo_box_get_active_text (GTK_COMBO_BOX (priv->priority_combo));
957
958   g_print ("Priority: %s\n", priority);
959
960   g_free (priv->priority);
961   priv->priority = priority;
962
963   fill_tasks (tasks_window);
964 }
965
966 static void
967 on_accounts (GtkAction *action, GGTDTasksWindow *tasks_window)
968 {
969   GtkWidget *account_window; 
970   account_window = (GtkWidget *) ggtd_accounts_dialog_new (tasks_window);
971   gtk_dialog_run (GTK_DIALOG (account_window));
972 }
973
974 static void
975 on_preferences (GtkAction *action, GGTDTasksWindow *tasks_window)
976 {
977   GtkWidget *preferences_window; 
978   preferences_window = (GtkWidget *) ggtd_settings_dialog_new (tasks_window);
979   gtk_dialog_run (GTK_DIALOG (preferences_window));
980 }
981
982
983 static void
984 on_refresh (GtkAction *action,
985             GGTDTasksWindow *tasks_window)
986 {
987   fill_list_combo (tasks_window);
988 }
989
990 void
991 clear_combo_list (GGTDTasksWindow *tasks_window)
992 {
993   GtkListStore *list_store;
994
995   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
996
997   list_store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (priv->list_combo)));
998   gtk_list_store_clear (list_store);
999 }
1000
1001 void
1002 clear_tasks (GGTDTasksWindow *tasks_window)
1003 {
1004   GGTDTasksWindowPrivate *priv = GGTD_TASKS_WINDOW_GET_PRIVATE (tasks_window);
1005
1006   GtkListStore *tasks_model;
1007
1008   tasks_model =
1009     GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tasks_treeview)));
1010
1011   gtk_list_store_clear (tasks_model);
1012 }