[gnome-todo] todo-txt: add support for completion/creation date



commit a4a7dd8ede7fce83ae11b45626d6db016e848173
Author: Rohit Kaushik <kaushikrohit325 gmail com>
Date:   Mon Jun 4 20:02:58 2018 +0530

    todo-txt: add support for completion/creation date

 plugins/todo-txt/gtd-provider-todo-txt.c | 21 +++++++++++++++
 plugins/todo-txt/gtd-todo-txt-parser.c   | 44 +++++++++++++++++++++++++++-----
 2 files changed, 59 insertions(+), 6 deletions(-)
---
diff --git a/plugins/todo-txt/gtd-provider-todo-txt.c b/plugins/todo-txt/gtd-provider-todo-txt.c
index 0c9620e..e29dd07 100644
--- a/plugins/todo-txt/gtd-provider-todo-txt.c
+++ b/plugins/todo-txt/gtd-provider-todo-txt.c
@@ -88,6 +88,8 @@ print_task (GString *output,
 {
   GtdTaskList *list;
   GDateTime *dt;
+  GDateTime *completion_dt;
+  GDateTime *creation_dt;
   const gchar *description;
   gint priority;
   gboolean is_complete;
@@ -97,6 +99,8 @@ print_task (GString *output,
   dt = gtd_task_get_due_date (task);
   list = gtd_task_get_list (task);
   description = gtd_task_get_description (task);
+  creation_dt = gtd_task_get_creation_date (task);
+  completion_dt = gtd_task_get_completion_date (task);
 
   if (is_complete)
     g_string_append (output, "x ");
@@ -111,6 +115,22 @@ print_task (GString *output,
         g_string_append (output, "(A) ");
     }
 
+  if (is_complete && completion_dt)
+    {
+      g_autofree gchar *formatted_time = g_date_time_format (completion_dt, "%F");
+      g_string_append_printf (output, "%s ", formatted_time);
+    }
+
+  /*
+   * Creation date is only specified if completion date is specified
+   * and it comes just after completion date as per todo.txt format
+   */
+  if (is_complete && completion_dt && creation_dt)
+    {
+      g_autofree gchar *formatted_time = g_date_time_format (creation_dt, "%F");
+      g_string_append_printf (output, "%s ", formatted_time);
+    }
+
   g_string_append_printf (output,
                           "%s @%s",
                           gtd_task_get_title (task),
@@ -578,6 +598,7 @@ gtd_provider_todo_txt_create_task (GtdProvider *provider,
   gtd_task_set_due_date (new_task, due_date);
   gtd_task_set_list (new_task, list);
   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);
 
diff --git a/plugins/todo-txt/gtd-todo-txt-parser.c b/plugins/todo-txt/gtd-todo-txt-parser.c
index ca00271..9eb7470 100644
--- a/plugins/todo-txt/gtd-todo-txt-parser.c
+++ b/plugins/todo-txt/gtd-todo-txt-parser.c
@@ -34,7 +34,8 @@ typedef enum
   TOKEN_HIDDEN,
   TOKEN_COMPLETE,
   TOKEN_PRIORITY,
-  TOKEN_DATE,
+  TOKEN_CREATION_DATE,
+  TOKEN_COMPLETION_DATE,
   TOKEN_TITLE,
   TOKEN_LIST_NAME,
   TOKEN_LIST_COLOR,
@@ -162,8 +163,15 @@ parse_token_id (const gchar           *token,
   if (token_length == 3 && token[0] == '(' && token[2] == ')')
     return TOKEN_PRIORITY;
 
-  if (!g_str_has_prefix (token , "due:") && is_date (token))
-    return TOKEN_DATE;
+  if ((state->last_token == TOKEN_PRIORITY ||
+       state-> last_token == TOKEN_COMPLETE) &&
+      !g_str_has_prefix (token , "due:") && is_date (token))
+    {
+      return TOKEN_COMPLETION_DATE;
+    }
+
+  if (state->last_token == TOKEN_COMPLETION_DATE && !g_str_has_prefix (token , "due:") && is_date (token))
+    return TOKEN_CREATION_DATE;
 
   if (g_str_has_prefix (token , "color:"))
     return TOKEN_LIST_COLOR;
@@ -181,7 +189,8 @@ parse_token_id (const gchar           *token,
     }
 
   if (state->last_token == TOKEN_START ||
-      state->last_token == TOKEN_DATE ||
+      state->last_token == TOKEN_CREATION_DATE ||
+      state->last_token == TOKEN_COMPLETION_DATE ||
       state->last_token == TOKEN_PRIORITY ||
       state->last_token == TOKEN_COMPLETE||
       state->last_token == TOKEN_TITLE)
@@ -259,8 +268,14 @@ gtd_todo_txt_parser_parse_task (GtdProvider  *provider,
           gtd_task_set_priority (task, parse_priority (token));
           break;
 
-        case TOKEN_DATE:
+        case TOKEN_CREATION_DATE:
+          dt = parse_date (token);
+          gtd_task_set_creation_date (task, dt);
+          break;
+
+        case TOKEN_COMPLETION_DATE:
           dt = parse_date (token);
+          gtd_task_todo_txt_set_completion_date (GTD_TASK_TODO_TXT (task), dt);
           break;
 
         case TOKEN_TITLE:
@@ -495,7 +510,7 @@ gtd_todo_txt_parser_get_line_type (const gchar  *line,
             line_type = GTD_TODO_TXT_LINE_TYPE_TASK;
           break;
 
-        case TOKEN_DATE:
+        case TOKEN_COMPLETION_DATE:
           if (state.last_token <= TOKEN_PRIORITY)
             {
               line_type = GTD_TODO_TXT_LINE_TYPE_TASK;
@@ -512,6 +527,23 @@ gtd_todo_txt_parser_get_line_type (const gchar  *line,
             }
           break;
 
+        case TOKEN_CREATION_DATE:
+          if (state.last_token == TOKEN_COMPLETION_DATE)
+            {
+              line_type = GTD_TODO_TXT_LINE_TYPE_TASK;
+
+              if (!is_date (token))
+                {
+                  g_set_error (error,
+                               GTD_TODO_TXT_PARSER_ERROR,
+                               GTD_TODO_TXT_PARSER_INVALID_DUE_DATE,
+                               "Invalid date found");
+
+                  GTD_RETURN (-1);
+                }
+            }
+          break;
+
         case TOKEN_TITLE:
           line_type = GTD_TODO_TXT_LINE_TYPE_TASK;
           break;


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