[gnome-todo] task-list: Split save_task() into add_task() and update_task()



commit 93cdb13a4aeb27d90d41bd392326c1143232c9be
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Sep 12 14:58:57 2018 -0300

    task-list: Split save_task() into add_task() and update_task()
    
    This was probably one of the worst API design choices I've made
    ever, so I'm very happy to get rid of it.

 doc/reference/gnome-todo-sections.txt    |  3 +-
 plugins/eds/gtd-provider-eds.c           |  4 +-
 plugins/eds/gtd-task-list-eds.c          |  4 +-
 plugins/todo-txt/gtd-provider-todo-txt.c |  6 +--
 plugins/todoist/gtd-provider-todoist.c   |  4 +-
 src/gtd-task-list.c                      | 81 +++++++++++++++++++++-----------
 src/gtd-task-list.h                      |  5 +-
 tests/dummy-provider.c                   |  2 +-
 8 files changed, 71 insertions(+), 38 deletions(-)
---
diff --git a/doc/reference/gnome-todo-sections.txt b/doc/reference/gnome-todo-sections.txt
index 4231393..2c39d76 100644
--- a/doc/reference/gnome-todo-sections.txt
+++ b/doc/reference/gnome-todo-sections.txt
@@ -143,7 +143,8 @@ gtd_task_list_get_name
 gtd_task_list_set_name
 gtd_task_list_get_provider
 gtd_task_list_set_provider
-gtd_task_list_save_task
+gtd_task_list_add_task
+gtd_task_list_update_task
 gtd_task_list_remove_task
 gtd_task_list_contains
 GtdTaskList
diff --git a/plugins/eds/gtd-provider-eds.c b/plugins/eds/gtd-provider-eds.c
index 19290f3..2d961ae 100644
--- a/plugins/eds/gtd-provider-eds.c
+++ b/plugins/eds/gtd-provider-eds.c
@@ -301,6 +301,8 @@ on_task_modified_cb (ECalClient   *client,
 
   e_cal_client_modify_object_finish (client, result, &error);
 
+  gtd_task_list_update_task (gtd_task_get_list (task), task);
+
   REPORT_ERROR (_("An error occurred while modifying a task"), error);
 
   GTD_EXIT;
@@ -479,7 +481,7 @@ gtd_provider_eds_create_task (GtdProvider *provider,
   gtd_task_set_due_date (new_task, due_date);
   gtd_task_set_list (new_task, list);
 
-  gtd_task_list_save_task (list, new_task);
+  gtd_task_list_add_task (list, new_task);
 
   /* The task is not ready until we finish the operation */
   gtd_object_push_loading (GTD_OBJECT (self));
diff --git a/plugins/eds/gtd-task-list-eds.c b/plugins/eds/gtd-task-list-eds.c
index 3d0edef..e82aa5e 100644
--- a/plugins/eds/gtd-task-list-eds.c
+++ b/plugins/eds/gtd-task-list-eds.c
@@ -171,6 +171,8 @@ on_view_objects_added_cb (ECalClientView *view,
         {
           gtd_task_eds_set_component (GTD_TASK_EDS (task), component);
 
+          gtd_task_list_update_task (self, task);
+
           GTD_TRACE_MSG ("Updated task '%s' to tasklist '%s'",
                          gtd_task_get_title (task),
                          gtd_task_list_get_name (self));
@@ -182,7 +184,7 @@ on_view_objects_added_cb (ECalClientView *view,
       task = gtd_task_eds_new (component);
       gtd_task_set_list (task, self);
 
-      gtd_task_list_save_task (self, task);
+      gtd_task_list_add_task (self, task);
 
       GTD_TRACE_MSG ("Added task '%s' (%s) to tasklist '%s'",
                      gtd_task_get_title (task),
diff --git a/plugins/todo-txt/gtd-provider-todo-txt.c b/plugins/todo-txt/gtd-provider-todo-txt.c
index 9bd26f1..c3694d2 100644
--- a/plugins/todo-txt/gtd-provider-todo-txt.c
+++ b/plugins/todo-txt/gtd-provider-todo-txt.c
@@ -323,7 +323,7 @@ parse_task (GtdProviderTodoTxt  *self,
   g_ptr_array_add (tasks, task);
 
   gtd_task_set_list (task, list);
-  gtd_task_list_save_task (list, task);
+  gtd_task_list_add_task (list, task);
 
   g_hash_table_insert (self->tasks, (gpointer) gtd_object_get_uid (GTD_OBJECT (task)), task);
   self->task_counter++;
@@ -415,7 +415,7 @@ resolve_subtasks (GtdProviderTodoTxt *self,
             gtd_task_add_subtask (parent_task, task);
 
           gtd_task_set_list (task, list);
-          gtd_task_list_save_task (list, task);
+          gtd_task_list_add_task (list, task);
 
           g_queue_push_head (&tasks_stack, task);
 
@@ -687,7 +687,7 @@ gtd_provider_todo_txt_create_task (GtdProvider *provider,
   gtd_task_set_title (new_task, title);
   gtd_task_set_creation_date (new_task, g_date_time_new_now_local ());
 
-  gtd_task_list_save_task (list, new_task);
+  gtd_task_list_add_task (list, new_task);
 
   update_source (GTD_PROVIDER_TODO_TXT (provider));
 }
diff --git a/plugins/todoist/gtd-provider-todoist.c b/plugins/todoist/gtd-provider-todoist.c
index b488128..5b5c069 100644
--- a/plugins/todoist/gtd-provider-todoist.c
+++ b/plugins/todoist/gtd-provider-todoist.c
@@ -445,7 +445,7 @@ parse_tasks (GtdProviderTodoist *self,
             gtd_task_add_subtask (parent_task, task);
 
           gtd_task_set_list (task, list);
-          gtd_task_list_save_task (list, task);
+          gtd_task_list_add_task (list, task);
 
           g_queue_push_head (&tasks_stack, task);
 
@@ -1185,7 +1185,7 @@ gtd_provider_todoist_create_task (GtdProvider *provider,
   gtd_task_set_creation_date (new_task, creation_date);
   gtd_object_set_uid (GTD_OBJECT (new_task), temp_id);
 
-  gtd_task_list_save_task (list, new_task);
+  gtd_task_list_add_task (list, new_task);
 
   command = g_strdup_printf ("{                              \n"
                              "    \"type\": \"item_add\",    \n"
diff --git a/src/gtd-task-list.c b/src/gtd-task-list.c
index c037c06..eb1c4e0 100644
--- a/src/gtd-task-list.c
+++ b/src/gtd-task-list.c
@@ -585,49 +585,74 @@ gtd_task_list_set_provider (GtdTaskList *self,
 }
 
 /**
- * gtd_task_list_save_task:
+ * gtd_task_list_add_task:
  * @list: a #GtdTaskList
  * @task: a #GtdTask
  *
- * Adds or updates @task to @list if it's not already present.
+ * Adds @task to @list.
  */
 void
-gtd_task_list_save_task (GtdTaskList *list,
-                         GtdTask     *task)
+gtd_task_list_add_task (GtdTaskList *self,
+                        GtdTask     *task)
 {
   GtdTaskListPrivate *priv;
+  GSequenceIter *iter;
+  const gchar *uid;
 
-  g_assert (GTD_IS_TASK_LIST (list));
-  g_assert (GTD_IS_TASK (task));
+  g_return_if_fail (GTD_IS_TASK_LIST (self));
+  g_return_if_fail (GTD_IS_TASK (task));
 
-  priv = gtd_task_list_get_instance_private (list);
+  priv = gtd_task_list_get_instance_private (self);
 
-  if (gtd_task_list_contains (list, task))
-    {
-      g_signal_emit (list, signals[TASK_UPDATED], 0, task);
-    }
-  else
-    {
-      GSequenceIter *iter;
-      const gchar *uid;
+  g_return_if_fail (!gtd_task_list_contains (self, task));
 
-      uid = gtd_object_get_uid (GTD_OBJECT (task));
-      iter = g_sequence_insert_sorted (priv->sorted_tasks,
-                                       g_object_ref (task),
-                                       compare_tasks_cb,
-                                       NULL);
+  uid = gtd_object_get_uid (GTD_OBJECT (task));
+  iter = g_sequence_insert_sorted (priv->sorted_tasks,
+                                   g_object_ref (task),
+                                   compare_tasks_cb,
+                                   NULL);
 
-      g_hash_table_insert (priv->tasks, g_strdup (uid), iter);
+  g_hash_table_insert (priv->tasks, g_strdup (uid), iter);
 
-      g_signal_connect (task, "notify", G_CALLBACK (task_changed_cb), list);
+  g_signal_connect (task, "notify", G_CALLBACK (task_changed_cb), self);
 
-      g_list_model_items_changed (G_LIST_MODEL (list),
-                                  g_sequence_iter_get_position (iter),
-                                  0,
-                                  1);
+  g_list_model_items_changed (G_LIST_MODEL (self),
+                              g_sequence_iter_get_position (iter),
+                              0,
+                              1);
 
-      g_signal_emit (list, signals[TASK_ADDED], 0, task);
-    }
+  g_signal_emit (self, signals[TASK_ADDED], 0, task);
+}
+
+/**
+ * gtd_task_list_update_task:
+ * @list: a #GtdTaskList
+ * @task: a #GtdTask
+ *
+ * Updates @task at @list.
+ */
+void
+gtd_task_list_update_task (GtdTaskList *self,
+                           GtdTask     *task)
+{
+  GtdTaskListPrivate *priv;
+  GSequenceIter *iter;
+
+  g_return_if_fail (GTD_IS_TASK_LIST (self));
+  g_return_if_fail (GTD_IS_TASK (task));
+
+  priv = gtd_task_list_get_instance_private (self);
+
+  g_return_if_fail (gtd_task_list_contains (self, task));
+
+  iter = g_hash_table_lookup (priv->tasks, gtd_object_get_uid (GTD_OBJECT (task)));
+
+  g_list_model_items_changed (G_LIST_MODEL (self),
+                              g_sequence_iter_get_position (iter),
+                              1,
+                              1);
+
+  g_signal_emit (self, signals[TASK_UPDATED], 0, task);
 }
 
 /**
diff --git a/src/gtd-task-list.h b/src/gtd-task-list.h
index d9138b9..180ef9f 100644
--- a/src/gtd-task-list.h
+++ b/src/gtd-task-list.h
@@ -69,7 +69,10 @@ GtdProvider*            gtd_task_list_get_provider              (GtdTaskList
 void                    gtd_task_list_set_provider              (GtdTaskList            *self,
                                                                  GtdProvider            *provider);
 
-void                    gtd_task_list_save_task                 (GtdTaskList            *list,
+void                    gtd_task_list_add_task                  (GtdTaskList            *list,
+                                                                 GtdTask                *task);
+
+void                    gtd_task_list_update_task               (GtdTaskList            *list,
                                                                  GtdTask                *task);
 
 void                    gtd_task_list_remove_task               (GtdTaskList            *list,
diff --git a/tests/dummy-provider.c b/tests/dummy-provider.c
index 7936d0b..355000e 100644
--- a/tests/dummy-provider.c
+++ b/tests/dummy-provider.c
@@ -372,7 +372,7 @@ dummy_provider_generate_task_lists (DummyProvider *self)
           uuid = g_uuid_string_random ();
           gtd_object_set_uid (GTD_OBJECT (task), uuid);
 
-          gtd_task_list_save_task (new_list, task);
+          gtd_task_list_add_task (new_list, task);
         }
     }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]