[gnome-todo] todo-txt: another batchload of fixes



commit 321695a86eb66e0df496bbbd02d7ec4cbf1ffa39
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Feb 14 13:12:43 2017 -0200

    todo-txt: another batchload of fixes

 plugins/todo-txt/gtd-plugin-todo-txt.c   |    4 +-
 plugins/todo-txt/gtd-plugin-todo-txt.h   |   12 +-
 plugins/todo-txt/gtd-provider-todo-txt.c |  860 ++++++++++++++----------------
 plugins/todo-txt/gtd-todo-txt-parser.c   |   43 +-
 plugins/todo-txt/gtd-todo-txt-parser.h   |   38 +-
 5 files changed, 450 insertions(+), 507 deletions(-)
---
diff --git a/plugins/todo-txt/gtd-plugin-todo-txt.c b/plugins/todo-txt/gtd-plugin-todo-txt.c
index bbaff4d..2e1eefe 100644
--- a/plugins/todo-txt/gtd-plugin-todo-txt.c
+++ b/plugins/todo-txt/gtd-plugin-todo-txt.c
@@ -124,7 +124,7 @@ gtd_plugin_todo_txt_set_default_source (GtdPluginTodoTxt *self)
   if (dir_open_error)
     {
       gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening default todo.txt directory"),
+                                      _("Error while opening the default Todo.txt directory"),
                                       dir_open_error->message);
 
       g_clear_error (&dir_open_error);
@@ -142,7 +142,7 @@ gtd_plugin_todo_txt_set_default_source (GtdPluginTodoTxt *self)
       if (error)
         {
           gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                          _("Error converting default todo.txt path to uri"),
+                                          _("Error while converting the default Todo.txt path to an URI"),
                                           error->message);
 
           g_clear_error (&error);
diff --git a/plugins/todo-txt/gtd-plugin-todo-txt.h b/plugins/todo-txt/gtd-plugin-todo-txt.h
index 8fb1995..2e16834 100644
--- a/plugins/todo-txt/gtd-plugin-todo-txt.h
+++ b/plugins/todo-txt/gtd-plugin-todo-txt.h
@@ -28,13 +28,13 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GtdPluginTodoTxt, gtd_plugin_todo_txt, GTD, PLUGIN_TODO_TXT, PeasExtensionBase)
 
-G_MODULE_EXPORT void  gtd_plugin_todo_txt_register_types      (PeasObjectModule   *module);
+G_MODULE_EXPORT void  gtd_plugin_todo_txt_register_types         (PeasObjectModule   *module);
 
-void     gtd_plugin_todo_txt_monitor_source      (GFileMonitor      *monitor,
-                                                  GFile             *first,
-                                                  GFile             *second,
-                                                  GFileMonitorEvent  event,
-                                                  gpointer           data);
+void     gtd_plugin_todo_txt_monitor_source                      (GFileMonitor       *monitor,
+                                                                  GFile              *first,
+                                                                  GFile              *second,
+                                                                  GFileMonitorEvent   event,
+                                                                  gpointer            data);
 
 G_END_DECLS
 
diff --git a/plugins/todo-txt/gtd-provider-todo-txt.c b/plugins/todo-txt/gtd-provider-todo-txt.c
index 9a0ab53..0828b21 100644
--- a/plugins/todo-txt/gtd-provider-todo-txt.c
+++ b/plugins/todo-txt/gtd-provider-todo-txt.c
@@ -123,6 +123,19 @@ gtd_provider_todo_txt_create_new_task (void)
 }
 
 static void
+emit_generic_error (GError *error)
+{
+  g_warning ("%s: %s: %s",
+             G_STRFUNC,
+             _("Error opening Todo.txt"),
+             error->message);
+
+  gtd_manager_emit_error_message (gtd_manager_get_default (),
+                                  _("Error opening Todo.txt"),
+                                  error->message);
+}
+
+static void
 gtd_provider_todo_txt_load_task (TaskData           *td,
                                  GtdProviderTodoTxt *self,
                                  gint                line_number)
@@ -259,86 +272,80 @@ static void
 gtd_provider_todo_txt_load_source (GtdProviderTodoTxt *self)
 {
   GFileInputStream *readstream;
-  GError *error = NULL;
+  GDataInputStream *reader;
+  TaskData *td;
+  GError *line_read_error;
+  GError *error;
   gint line_number = 0;
+  gchar *line_read;
 
   g_return_if_fail (G_IS_FILE (self->source_file));
 
+  td = NULL;
+  error = line_read_error = NULL;
   readstream = g_file_read (self->source_file,
                             NULL,
                             &error);
 
-  if (!error)
+  if (error)
     {
-      GDataInputStream *reader;
-      gchar *line_read;
-      GError *line_read_error = NULL;
-      TaskData *td = NULL;
+      emit_generic_error (error);
+      g_error_free (error);
+      return;
+    }
 
-      reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
-      self->no_of_lines = 0;
 
-      while (!line_read_error)
-        {
-          GList *tokens = NULL;
-          gboolean valid;
-          line_read = g_data_input_stream_read_line (reader,
-                                                     NULL,
-                                                     NULL,
-                                                     &line_read_error);
+  reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
+  self->no_of_lines = 0;
 
-          if (!line_read_error)
-            {
-              if (!line_read)
-                break;
+  while (!line_read_error)
+    {
+      GList *tokens = NULL;
+      gboolean valid;
 
-              line_number++;
-              self->no_of_lines++;
+      line_read = g_data_input_stream_read_line (reader,
+                                                 NULL,
+                                                 NULL,
+                                                 &line_read_error);
 
-              tokens = gtd_todo_txt_parser_tokenize (g_strdup(line_read));
-              valid = gtd_todo_txt_parser_validate_token_format (tokens);
+      if (!line_read_error)
+        {
+          if (!line_read)
+            break;
 
-              if (valid)
-                {
-                  td = gtd_todo_txt_parser_parse_tokens (tokens);
+          line_number++;
+          self->no_of_lines++;
 
-                  if (strcmp (gtd_todo_txt_parser_task_data_get_task_list_name (td), &(line_read[1])) == 0)
-                    gtd_provider_todo_txt_create_empty_list (self, g_strdup (&(line_read[1])));
-                  else
-                    gtd_provider_todo_txt_load_task (td, self, line_number);
-                }
-            }
-          else
-            {
-              g_warning ("%s: %s: %s",
-                         G_STRFUNC,
-                         _("Error reading line from todo.txt"),
-                         line_read_error->message);
+          tokens = gtd_todo_txt_parser_tokenize (line_read);
+          valid = gtd_todo_txt_parser_validate_token_format (tokens);
 
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                              _("Error reading line from Todo.txt"),
-                                              line_read_error->message);
-              g_error_free (line_read_error);
+          if (valid)
+            {
+              td = gtd_todo_txt_parser_parse_tokens (tokens);
 
-              return;
+              if (strcmp (gtd_todo_txt_parser_task_data_get_task_list_name (td), &(line_read[1])) == 0)
+                gtd_provider_todo_txt_create_empty_list (self, g_strdup (&(line_read[1])));
+              else
+                gtd_provider_todo_txt_load_task (td, self, line_number);
             }
+        }
+      else
+        {
+          g_warning ("%s: %s: %s",
+                     G_STRFUNC,
+                     _("Error while reading a line from Todo.txt"),
+                     line_read_error->message);
+
+          gtd_manager_emit_error_message (gtd_manager_get_default (),
+                                          _("Error while reading a line from Todo.txt"),
+                                          line_read_error->message);
+          g_error_free (line_read_error);
 
-          g_list_free_full (tokens, g_free);
-          g_free (line_read);
+          return;
         }
-    }
-  else
-    {
-      g_warning ("%s: %s: %s",
-                 G_STRFUNC,
-                 _("Error opening todo.txt file"),
-                 error->message);
 
-      gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening todo.txt file"),
-                                      error->message);
-      g_error_free (error);
-      return;
+      g_list_free_full (tokens, g_free);
+      g_free (line_read);
     }
 }
 
@@ -348,19 +355,21 @@ gtd_provider_todo_txt_create_task (GtdProvider *provider,
 {
   GtdProviderTodoTxt *self;
   GFileOutputStream *write_stream;
+  GDataOutputStream *writer;
   GtdTaskList *list;
+  GError *write_error;
   GError *error;
   const gchar *list_name;
   const gchar *task_description;
   gchar *task_line;
 
   self = GTD_PROVIDER_TODO_TXT (provider);
-  error = NULL;
+  error = write_error = NULL;
 
   g_return_if_fail (G_IS_FILE (self->source_file));
 
   if (self->monitor)
-    g_signal_handlers_block_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_block_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 
   list = gtd_task_get_list (task);
   list_name = gtd_task_list_get_name (list);
@@ -373,58 +382,48 @@ gtd_provider_todo_txt_create_task (GtdProvider *provider,
                                   NULL,
                                   &error);
 
-  if(!error)
+  if (error)
     {
-      GDataOutputStream *writer;
-      GError *write_error = NULL;
+      emit_generic_error (error);
+      g_error_free (error);
+      return;
+    }
 
-      writer = g_data_output_stream_new (G_OUTPUT_STREAM (write_stream));
+  writer = g_data_output_stream_new (G_OUTPUT_STREAM (write_stream));
 
-      g_data_output_stream_put_string (writer,
-                                       task_line,
-                                       NULL,
-                                       &write_error);
+  g_data_output_stream_put_string (writer,
+                                   task_line,
+                                   NULL,
+                                   &write_error);
 
-      if(write_error)
-        {
-          g_warning ("%s: %s: %s",
-                     G_STRFUNC,
-                     _("Error adding task to Todo.txt"),
-                     write_error->message);
-
-          gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                          _("Error adding task to Todo.txt"),
-                                          write_error->message);
-          g_error_free (write_error);
-          return;
-        }
-      else
-        {
-          g_hash_table_insert (self->root_tasks,
-                               (gpointer) task_description,
-                               task);
-          self->no_of_lines++;
-          g_object_set_data (G_OBJECT (task), "line", GINT_TO_POINTER (self->no_of_lines));
-        }
-
-      g_output_stream_close (G_OUTPUT_STREAM (writer),
-                             NULL,
-                             NULL);
-    }
-  else
+  if(write_error)
     {
       g_warning ("%s: %s: %s",
                  G_STRFUNC,
-                 _("Error opening Todo.txt"),
-                 error->message);
+                 _("Error while adding a task to Todo.txt"),
+                 write_error->message);
 
       gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening Todo.txt"),
-                                      error->message);
-      g_error_free (error);
+                                      _("Error while adding a task to Todo.txt"),
+                                      write_error->message);
+      g_error_free (write_error);
+      return;
+    }
+  else
+    {
+      g_hash_table_insert (self->root_tasks,
+                           (gpointer) task_description,
+                           task);
+      self->no_of_lines++;
+      g_object_set_data (G_OBJECT (task), "line", GINT_TO_POINTER (self->no_of_lines));
     }
+
+  g_output_stream_close (G_OUTPUT_STREAM (writer),
+                         NULL,
+                         NULL);
+
   if (self->monitor)
-    g_signal_handlers_unblock_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 
   g_free (task_line);
 }
@@ -437,17 +436,20 @@ gtd_provider_todo_txt_update_task (GtdProvider *provider,
   GFileInputStream *readstream;
   GFileOutputStream *outstream;
   GDataOutputStream *writer;
+  GDataInputStream *reader;
+  GError *line_read_error;
   GError *error;
   GError *write_error;
+  gchar *line_read;
   gint line_to_update;
   gint line_number;
 
   self = GTD_PROVIDER_TODO_TXT (provider);
   line_number = 0;
-  error = write_error = NULL;
+  error = write_error = line_read_error = NULL;
 
   if (self->monitor)
-    g_signal_handlers_block_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_block_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 
   line_to_update = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "line"));
 
@@ -463,111 +465,100 @@ gtd_provider_todo_txt_update_task (GtdProvider *provider,
                               NULL,
                               &error);
 
-  if (!error)
+  if (error)
     {
-      GDataInputStream *reader;
-      GError *line_read_error = NULL;
-      gchar *line_read;
+      emit_generic_error (error);
+      g_error_free (error);
+      return;
+    }
 
-      reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
-      writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
+  reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
+  writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
 
-      while (!line_read_error)
-        {
-          gboolean valid;
-          GList *tokens = NULL;
+  while (!line_read_error)
+    {
+      gboolean valid;
+      GList *tokens = NULL;
 
-          line_read = g_data_input_stream_read_line (reader,
-                                                     NULL,
-                                                     NULL,
-                                                     &line_read_error);
+      line_read = g_data_input_stream_read_line (reader,
+                                                 NULL,
+                                                 NULL,
+                                                 &line_read_error);
 
-          if (!line_read_error)
-            {
-              if (!line_read)
-                break;
+      if (!line_read_error)
+        {
+          if (!line_read)
+            break;
 
-              line_number++;
+          line_number++;
 
-              tokens = gtd_todo_txt_parser_tokenize (g_strdup (line_read));
-              valid = gtd_todo_txt_parser_validate_token_format (tokens);
+          tokens = gtd_todo_txt_parser_tokenize (line_read);
+          valid = gtd_todo_txt_parser_validate_token_format (tokens);
 
-              if (valid &&
-                  line_number == line_to_update)
-                {
-                  GList *update_tokens;
-                  GList *it;
+          if (valid &&
+              line_number == line_to_update)
+            {
+              GList *update_tokens;
+              GList *it;
 
-                  update_tokens = gtd_todo_txt_parser_get_task_line (task);
-                  it = NULL;
-                  for (it = update_tokens; it != NULL; it = it->next)
+              update_tokens = gtd_todo_txt_parser_get_task_line (task);
+              it = NULL;
+              for (it = update_tokens; it != NULL; it = it->next)
+                {
+                  g_data_output_stream_put_string (writer,
+                                                   it->data,
+                                                   NULL,
+                                                   &write_error);
+                  if (it->next == NULL)
                     {
                       g_data_output_stream_put_string (writer,
-                                                       it->data,
+                                                       "\n",
                                                        NULL,
                                                        &write_error);
-                      if (it->next == NULL)
-                        {
-                          g_data_output_stream_put_string (writer,
-                                                           "\n",
-                                                           NULL,
-                                                           &write_error);
-                        }
-                      else
-                        g_data_output_stream_put_string (writer,
-                                                         " ",
-                                                         NULL,
-                                                         &write_error);
                     }
-
-                  g_list_free_full (update_tokens, g_free);
+                  else
+                    g_data_output_stream_put_string (writer,
+                                                     " ",
+                                                     NULL,
+                                                     &write_error);
                 }
 
+              g_list_free_full (update_tokens, g_free);
+            }
 
-              else
-                {
-                  line_read = strcat (line_read, "\n");
 
-                  g_data_output_stream_put_string (writer,
-                                                   line_read,
-                                                   NULL,
-                                                   &write_error);
-                }
-            }
           else
             {
-              g_warning ("%s: %s: %s",
-                         G_STRFUNC,
-                         _("Error reading tasks from Todo.txt"),
-                         line_read_error->message);
-
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                              _("Error reading tasks from Todo.txt"),
-                                              line_read_error->message);
-              g_error_free (line_read_error);
+              line_read = strcat (line_read, "\n");
+
+              g_data_output_stream_put_string (writer,
+                                               line_read,
+                                               NULL,
+                                               &write_error);
             }
+        }
+      else
+        {
+          g_warning ("%s: %s: %s",
+                     G_STRFUNC,
+                     _("Error while reading tasks from Todo.txt"),
+                     line_read_error->message);
 
-          g_list_free_full (tokens, g_free);
+          gtd_manager_emit_error_message (gtd_manager_get_default (),
+                                          _("Error while reading tasks from Todo.txt"),
+                                          line_read_error->message);
+          g_error_free (line_read_error);
         }
 
-      g_output_stream_close (G_OUTPUT_STREAM (writer),
-                             NULL,
-                             NULL);
+      g_list_free_full (tokens, g_free);
     }
-  else
-    {
-      g_warning ("%s: %s: %s",
-                 G_STRFUNC,
-                 _("Error opening Todo.txt"),
-                 error->message);
 
-      gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening Todo.txt"),
-                                      error->message);
-      g_error_free (error);
-    }
+  g_output_stream_close (G_OUTPUT_STREAM (writer),
+                         NULL,
+                         NULL);
+
   if (self->monitor)
-    g_signal_handlers_unblock_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 }
 
 static void
@@ -578,19 +569,23 @@ gtd_provider_todo_txt_remove_task (GtdProvider *provider,
   GFileInputStream *readstream;
   GFileOutputStream *outstream;
   GDataOutputStream *writer;
+  GDataInputStream *reader;
   GError *error;
   GError *write_error;
+  GError *line_read_error;
+  gchar *line_read;
+  gboolean skip;
   gint line_number;
   gint line_number_to_remove;
 
   self = GTD_PROVIDER_TODO_TXT (provider);
-  error = write_error = NULL;
+  error = write_error = line_read_error = NULL;
   line_number = 0;
 
   g_return_if_fail (G_IS_FILE (self->source_file));
 
   if (self->monitor)
-    g_signal_handlers_block_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_block_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 
   line_number_to_remove = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "line"));
   readstream = g_file_read (self->source_file,
@@ -603,92 +598,80 @@ gtd_provider_todo_txt_remove_task (GtdProvider *provider,
                               NULL,
                               &error);
 
-  if (!error)
+  if (error)
     {
-      GDataInputStream *reader;
-      GError *line_read_error = NULL;
-      gchar *line_read;
-      gboolean skip;
-
-      reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
-      writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
-
-      while (!line_read_error)
-        {
-          TaskData *td;
-          gboolean valid;
-          GList *tokens = NULL;
-
-          line_read = g_data_input_stream_read_line (reader,
-                                                     NULL,
-                                                     NULL,
-                                                     &line_read_error);
+      emit_generic_error (error);
+      g_error_free (error);
+      return;
+    }
 
-          skip = FALSE;
+  reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
+  writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
 
-          if (!line_read_error)
-            {
-              if (!line_read)
-                break;
-              line_number++;
+  while (!line_read_error)
+    {
+      TaskData *td;
+      gboolean valid;
+      GList *tokens = NULL;
 
-              tokens = gtd_todo_txt_parser_tokenize (g_strdup(line_read));
-              valid = gtd_todo_txt_parser_validate_token_format (tokens);
+      line_read = g_data_input_stream_read_line (reader,
+                                                 NULL,
+                                                 NULL,
+                                                 &line_read_error);
 
-              if (valid)
-                td = gtd_todo_txt_parser_parse_tokens (tokens);
-              else
-                td = NULL;
+      skip = FALSE;
 
-              if (line_number == line_number_to_remove)
-                {
-                  skip = TRUE;
-                  self->no_of_lines--;
-                }
+      if (!line_read_error)
+        {
+          if (!line_read)
+            break;
+          line_number++;
 
-              if (!skip)
-                {
-                  line_read = strcat (line_read, "\n");
+          tokens = gtd_todo_txt_parser_tokenize (line_read);
+          valid = gtd_todo_txt_parser_validate_token_format (tokens);
 
-                  g_data_output_stream_put_string (writer,
-                                                   line_read,
-                                                   NULL,
-                                                  &write_error);
-                }
-            }
+          if (valid)
+            td = gtd_todo_txt_parser_parse_tokens (tokens);
           else
+            td = NULL;
+
+          if (line_number == line_number_to_remove)
             {
-              g_warning ("%s: %s: %s",
-                         G_STRFUNC,
-                         _("Error reading tasks from Todo.txt"),
-                         line_read_error->message);
+              skip = TRUE;
+              self->no_of_lines--;
+            }
 
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                              _("Error reading tasks from Todo.txt"),
-                                              line_read_error->message);
-              g_error_free (line_read_error);
+          if (!skip)
+            {
+              line_read = strcat (line_read, "\n");
 
-              return;
+              g_data_output_stream_put_string (writer,
+                                               line_read,
+                                               NULL,
+                                              &write_error);
             }
+
           g_free (td);
         }
+      else
+        {
+          g_warning ("%s: %s: %s",
+                     G_STRFUNC,
+                     _("Error while reading tasks from Todo.txt"),
+                     line_read_error->message);
 
-      g_output_stream_close (G_OUTPUT_STREAM (writer),
-                             NULL,
-                             NULL);
+          gtd_manager_emit_error_message (gtd_manager_get_default (),
+                                          _("Error while reading tasks from Todo.txt"),
+                                          line_read_error->message);
+          g_error_free (line_read_error);
+          return;
+        }
     }
-  else
-    {
-      g_warning ("%s: %s: %s",
-                 G_STRFUNC,
-                 _("Error opening Todo.txt"),
-                 error->message);
 
-      gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening Todo.txt"),
-                                      error->message);
-      g_error_free (error);
-    }
+  g_output_stream_close (G_OUTPUT_STREAM (writer),
+                         NULL,
+                         NULL);
+
   if (self->monitor)
     g_signal_handlers_unblock_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 }
@@ -699,11 +682,14 @@ gtd_provider_todo_txt_create_task_list (GtdProvider *provider,
 {
   GtdProviderTodoTxt *self;
   GFileOutputStream  *write_stream;
+  GDataOutputStream *writer;
+  GError *write_error;
   GError *error;
+  gchar *put;
   const gchar *name;
 
   self = GTD_PROVIDER_TODO_TXT (provider);
-  error = NULL;
+  error = write_error = NULL;
 
   g_return_if_fail (G_IS_FILE (self->source_file));
 
@@ -715,60 +701,47 @@ gtd_provider_todo_txt_create_task_list (GtdProvider *provider,
                                   NULL,
                                   &error);
 
-  if(!error)
+  if (error)
     {
-      GDataOutputStream *writer;
-      GError *write_error = NULL;
-      gchar *put;
-
-      name = gtd_task_list_get_name (list);
-      writer = g_data_output_stream_new (G_OUTPUT_STREAM (write_stream));
+      emit_generic_error (error);
+      g_error_free (error);
+      return;
+    }
 
-      put = g_strconcat ("@", name, "\n", NULL);
+  name = gtd_task_list_get_name (list);
+  writer = g_data_output_stream_new (G_OUTPUT_STREAM (write_stream));
 
-      g_data_output_stream_put_string (writer,
-                                       put,
-                                       NULL,
-                                       &write_error);
+  put = g_strconcat ("@", name, "\n", NULL);
 
-      if(write_error)
-        {
-          g_warning ("%s: %s: %s",
-                     G_STRFUNC,
-                     _("Error creating Todo.txt list"),
-                     write_error->message);
+  g_data_output_stream_put_string (writer,
+                                   put,
+                                   NULL,
+                                   &write_error);
 
-          gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                          _("Error creating todo.txt list"),
-                                          write_error->message);
-          g_error_free (write_error);
-          return;
-        }
-      else
-        {
-          self->tasklists = g_list_append (self->tasklists,
-                                           list);
-          g_hash_table_insert (self->lists, (gpointer) name, list);
-          g_signal_emit_by_name (self, "list-added", list);
-          self->no_of_lines++;
-        }
-
-      g_free (put);
-    }
-  else
+  if (write_error)
     {
       g_warning ("%s: %s: %s",
                  G_STRFUNC,
-                 _("Error opening Todo.txt"),
-                 error->message);
+                 _("Error while creating a Todo.txt list"),
+                 write_error->message);
 
       gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening Todo.txt"),
-                                      error->message);
-      g_error_free (error);
+                                      _("Error while creating a Todo.txt list"),
+                                      write_error->message);
+      g_error_free (write_error);
+      goto out;
     }
+
+  self->tasklists = g_list_append (self->tasklists, list);
+  g_hash_table_insert (self->lists, (gpointer) name, list);
+  g_signal_emit_by_name (self, "list-added", list);
+  self->no_of_lines++;
+
+out:
+  g_free (put);
+
   if (self->monitor)
-    g_signal_handlers_unblock_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 }
 
 static void
@@ -779,10 +752,13 @@ gtd_provider_todo_txt_update_task_list (GtdProvider *provider,
   GFileInputStream *readstream;
   GFileOutputStream *outstream;
   GDataOutputStream *writer;
+  GDataInputStream *reader;
+  GError *line_read_error = NULL;
   GError *error;
   GError *write_error;
   const gchar *current_list_name;
   gchar *stored_list_name;
+  gchar *line_read;
 
   self = GTD_PROVIDER_TODO_TXT (provider);
   error = write_error = NULL;
@@ -790,7 +766,7 @@ gtd_provider_todo_txt_update_task_list (GtdProvider *provider,
   g_return_if_fail (G_IS_FILE (self->source_file));
 
   if (self->monitor)
-    g_signal_handlers_block_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_block_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 
   stored_list_name = g_object_get_data (G_OBJECT (list), "line");
   current_list_name = gtd_task_list_get_name (list);
@@ -805,124 +781,106 @@ gtd_provider_todo_txt_update_task_list (GtdProvider *provider,
                               NULL,
                               &error);
 
-  if (!error)
+  if (error)
     {
-      GDataInputStream *reader;
-      GError *line_read_error = NULL;
-      char *line_read;
+      emit_generic_error (error);
+      g_error_free (error);
+      return;
+    }
 
-      reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
-      writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
+  reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
+  writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
 
-      while (!line_read_error)
-        {
-          gboolean valid;
-          GList *tokens;
-          TaskData *td = NULL;
+  while (!line_read_error)
+    {
+      gboolean valid;
+      GList *tokens;
+      TaskData *td = NULL;
 
-          tokens = NULL;
-          line_read = g_data_input_stream_read_line (reader,
-                                                     NULL,
-                                                     NULL,
-                                                     &line_read_error);
+      tokens = NULL;
+      line_read = g_data_input_stream_read_line (reader,
+                                                 NULL,
+                                                 NULL,
+                                                 &line_read_error);
 
-          if (!line_read_error)
-            {
-              if (!line_read)
-                break;
+      if (!line_read_error)
+        {
+          if (!line_read)
+            break;
 
-              tokens = gtd_todo_txt_parser_tokenize (g_strdup(line_read));
-              valid = gtd_todo_txt_parser_validate_token_format (tokens);
+          tokens = gtd_todo_txt_parser_tokenize (line_read);
+          valid = gtd_todo_txt_parser_validate_token_format (tokens);
 
-              if (valid)
-                td = gtd_todo_txt_parser_parse_tokens (tokens);
+          if (valid)
+            td = gtd_todo_txt_parser_parse_tokens (tokens);
 
-              if (valid &&
-                  strcmp (stored_list_name, current_list_name)&&
-                  !(strcmp (gtd_todo_txt_parser_task_data_get_task_list_name (td), stored_list_name)))
-                {
-                  GList *update_tokens;
-                  GList *it;
+          if (valid &&
+              strcmp (stored_list_name, current_list_name)&&
+              !(strcmp (gtd_todo_txt_parser_task_data_get_task_list_name (td), stored_list_name)))
+            {
+              GList *update_tokens;
+              GList *it;
 
-                  update_tokens = gtd_todo_txt_parser_get_list_updated_token (list, g_strdup (line_read));
-                  it = NULL;
+              update_tokens = gtd_todo_txt_parser_get_list_updated_token (list, g_strdup (line_read));
+              it = NULL;
 
-                  for (it = update_tokens; it != NULL; it = it->next)
+              for (it = update_tokens; it != NULL; it = it->next)
+                {
+                  g_data_output_stream_put_string (writer,
+                                                   it->data,
+                                                   NULL,
+                                                   &write_error);
+                  if (it->next == NULL)
                     {
                       g_data_output_stream_put_string (writer,
-                                                       it->data,
+                                                       "\n",
                                                        NULL,
                                                        &write_error);
-                      if (it->next == NULL)
-                        {
-                          g_data_output_stream_put_string (writer,
-                                                           "\n",
-                                                           NULL,
-                                                           &write_error);
-                        }
-                      else
-                        g_data_output_stream_put_string (writer,
-                                                         " ",
-                                                         NULL,
-                                                         &write_error);
                     }
-                  g_list_free_full (update_tokens, g_free);
+                  else
+                    g_data_output_stream_put_string (writer,
+                                                     " ",
+                                                     NULL,
+                                                     &write_error);
                 }
+              g_list_free_full (update_tokens, g_free);
+            }
 
 
-              else
-                {
-                  line_read = strcat (line_read, "\n");
-
-                  g_data_output_stream_put_string (writer,
-                                                   line_read,
-                                                   NULL,
-                                                   &write_error);
-                }
-            }
           else
             {
-              g_warning ("%s: %s: %s",
-                         G_STRFUNC,
-                         _("Error while reading tasks from Todo.txt"),
-                         line_read_error->message);
-
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                             _("Error while reading tasks from Todo.txt"),
-                                               line_read_error->message);
-              g_error_free (line_read_error);
+              line_read = strcat (line_read, "\n");
+
+              g_data_output_stream_put_string (writer,
+                                               line_read,
+                                               NULL,
+                                               &write_error);
             }
+        }
+      else
+        {
+          g_warning ("%s: %s: %s",
+                     G_STRFUNC,
+                     _("Error while reading tasks from Todo.txt"),
+                     line_read_error->message);
 
-          g_list_free_full (tokens, g_free);
+          gtd_manager_emit_error_message (gtd_manager_get_default (),
+                                         _("Error while reading tasks from Todo.txt"),
+                                           line_read_error->message);
+          g_error_free (line_read_error);
         }
 
-      g_output_stream_close (G_OUTPUT_STREAM (writer),
-                             NULL,
-                             NULL);
-      g_input_stream_close (G_INPUT_STREAM (reader),
-                            NULL,
-                            NULL);
+      g_list_free_full (tokens, g_free);
     }
-  else
-    {
-      g_warning ("%s: %s: %s",
-                 G_STRFUNC,
-                 _("Error opening Todo.txt"),
-                 error->message);
 
-      gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening Todo.txt"),
-                                      error->message);
-      g_error_free (error);
-    }
-  g_input_stream_close (G_INPUT_STREAM (readstream),
-                        NULL,
-                        NULL);
-  g_output_stream_close (G_OUTPUT_STREAM (outstream),
-                         NULL,
-                         NULL);
+  g_output_stream_close (G_OUTPUT_STREAM (writer), NULL, NULL);
+  g_input_stream_close (G_INPUT_STREAM (reader), NULL, NULL);
+
+  g_output_stream_close (G_OUTPUT_STREAM (outstream), NULL, NULL);
+  g_input_stream_close (G_INPUT_STREAM (readstream), NULL, NULL);
+
   if (self->monitor)
-    g_signal_handlers_unblock_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 }
 
 static void
@@ -934,9 +892,13 @@ gtd_provider_todo_txt_remove_task_list (GtdProvider *provider,
   GFileOutputStream *outstream;
   GDataOutputStream *writer;
   GFileInputStream *readstream;
+  GDataInputStream *reader;
+  GError *line_read_error = NULL;
   GError *write_error;
   GError *error;
   const gchar  *list_name;
+  gboolean skip;
+  gchar *line;
 
   self = GTD_PROVIDER_TODO_TXT (provider);
   error = write_error = NULL;
@@ -957,88 +919,78 @@ gtd_provider_todo_txt_remove_task_list (GtdProvider *provider,
                               NULL,
                               &error);
 
-  if (!error)
+  if (error)
     {
-      GDataInputStream *reader;
-      GError *line_read_error = NULL;
-      char *line;
-      gboolean skip;
+      emit_generic_error (error);
+      g_error_free (error);
+      return;
+    }
 
-      reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
-      writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
+  reader = g_data_input_stream_new (G_INPUT_STREAM (readstream));
+  writer = g_data_output_stream_new (G_OUTPUT_STREAM (outstream));
 
-      while (!line_read_error)
-        {
-          TaskData *td = NULL;
-          gboolean valid;
-          GList *tokens = NULL;
-
-          line = g_data_input_stream_read_line (reader,
-                                                NULL,
-                                                NULL,
-                                                &line_read_error);
+  while (!line_read_error)
+    {
+      TaskData *td = NULL;
+      gboolean valid;
+      GList *tokens = NULL;
 
-          skip = FALSE;
+      line = g_data_input_stream_read_line (reader,
+                                            NULL,
+                                            NULL,
+                                            &line_read_error);
 
-          if (!line_read_error)
-            {
-              if (!line)
-                break;
+      skip = FALSE;
 
-              tokens = gtd_todo_txt_parser_tokenize (g_strdup(line));
-              valid = gtd_todo_txt_parser_validate_token_format (tokens);
+      if (!line_read_error)
+        {
+          if (!line)
+            break;
 
-              if (valid)
-                td = gtd_todo_txt_parser_parse_tokens (tokens);
+          tokens = gtd_todo_txt_parser_tokenize (line);
+          valid = gtd_todo_txt_parser_validate_token_format (tokens);
 
-              if (valid && strcmp (gtd_todo_txt_parser_task_data_get_task_list_name (td), list_name) == 0)
-                {
-                  self->no_of_lines--;
-                  skip = TRUE;
-                }
-
-              if (!skip)
-                {
-                  line = strcat (line, "\n");
+          if (valid)
+            td = gtd_todo_txt_parser_parse_tokens (tokens);
 
-                  g_data_output_stream_put_string (writer,
-                                                   line,
-                                                   NULL,
-                                                   &write_error);
-                }
+          if (valid && strcmp (gtd_todo_txt_parser_task_data_get_task_list_name (td), list_name) == 0)
+            {
+              self->no_of_lines--;
+              skip = TRUE;
             }
-          else
+
+          if (!skip)
             {
-              g_warning ("%s: %s: %s",
-                         G_STRFUNC,
-                         _("Error reading task-lists from Todo.txt"),
-                         line_read_error->message);
-
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                              _("Error reading task-lists from Todo.txt"),
-                                              line_read_error->message);
-              g_error_free (line_read_error);
+              line = strcat (line, "\n");
+
+              g_data_output_stream_put_string (writer,
+                                               line,
+                                               NULL,
+                                               &write_error);
             }
-          g_free (td);
         }
-      g_output_stream_close (G_OUTPUT_STREAM (writer),
-                             NULL,
-                             NULL);
-    }
-  else
-    {
-      g_warning ("%s: %s: %s",
-                 G_STRFUNC,
-                 _("Error opening todo.txt file"),
-                 error->message);
+      else
+        {
+          g_warning ("%s: %s: %s",
+                     G_STRFUNC,
+                     _("Error reading task-lists from Todo.txt"),
+                     line_read_error->message);
 
-      gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error opening Todo.txt"),
-                                      error->message);
-      g_error_free (error);
+          gtd_manager_emit_error_message (gtd_manager_get_default (),
+                                          _("Error reading task-lists from Todo.txt"),
+                                          line_read_error->message);
+          g_error_free (line_read_error);
+        }
+
+      g_free (td);
     }
+
+  g_output_stream_close (G_OUTPUT_STREAM (writer),
+                         NULL,
+                         NULL);
+
   if (self->monitor)
-    g_signal_handlers_unblock_by_func(self->monitor, gtd_plugin_todo_txt_monitor_source, self);
+    g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 }
 
 static GList*
@@ -1093,9 +1045,9 @@ gtd_provider_todo_txt_finalize (GObject *object)
   g_clear_pointer (&self->lists, g_hash_table_destroy);
   g_clear_pointer (&self->root_tasks, g_hash_table_destroy);
   g_clear_pointer (&self->tasklists, g_clear_object);
-  g_clear_pointer (&self->source, g_free);
-  g_clear_object (&self->source_file);
+  g_clear_pointer (&self->source_file, g_free);
   g_clear_object (&self->icon);
+  g_clear_pointer (&self->source, g_free);
 
   G_OBJECT_CLASS (gtd_provider_todo_txt_parent_class)->finalize (object);
 }
diff --git a/plugins/todo-txt/gtd-todo-txt-parser.c b/plugins/todo-txt/gtd-todo-txt-parser.c
index 7ba6ba7..9639939 100644
--- a/plugins/todo-txt/gtd-todo-txt-parser.c
+++ b/plugins/todo-txt/gtd-todo-txt-parser.c
@@ -317,28 +317,22 @@ gtd_todo_txt_parser_validate_token_format (GList *tokens)
         {
         case TASK_COMPLETE:
           last_read = TASK_COMPLETE;
+
           if (position != 1)
-            {
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                  _("Task completion token x should be at the start of the line"),
-                                  _("Skipping this line"));
-              return FALSE;
-            }
+            return FALSE;
           else
             complete_tk = TRUE;
+
           break;
 
         case TASK_PRIORITY:
           last_read = TASK_PRIORITY;
+
           if (position != (complete_tk + 1))
-            {
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                  _("Task priority should be at the start of the line"),
-                                  _("Skipping this line"));
-              return FALSE;
-            }
+            return FALSE;
           else
             priority_tk = TRUE;
+
           break;
 
         case TASK_DATE:
@@ -346,6 +340,7 @@ gtd_todo_txt_parser_validate_token_format (GList *tokens)
 
           if (position != (complete_tk + priority_tk + 1))
             return FALSE;
+
           if (!gtd_todo_txt_parser_is_date (it->data))
             {
               gtd_manager_emit_error_message (gtd_manager_get_default (),
@@ -371,19 +366,16 @@ gtd_todo_txt_parser_validate_token_format (GList *tokens)
 
         case TASK_DUE_DATE:
           last_read = TASK_DUE_DATE;
+
           if (!gtd_todo_txt_parser_is_date (&str[4]))
-            {
-              gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                             _("Incorrect due date"),
-                                             _("Please make sure the due date in Todo.txt is valid. Tasks 
with invalid date are not loaded"));
-              return FALSE;
-            }
+            return FALSE;
+
           break;
 
         default:
           gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                       _("Unrecognized token in Todo.txt line"),
-                                       _("To Do cannot recognize some tags in your Todo.txt file. Some tasks 
may not be loaded"));
+                                          _("Unrecognized token in Todo.txt line"),
+                                          _("To Do cannot recognize some tags in your Todo.txt file. Some 
tasks may not be loaded"));
           return FALSE;
           break;
         }
@@ -392,8 +384,8 @@ gtd_todo_txt_parser_validate_token_format (GList *tokens)
   if (!task_list_name_tk)
     {
       gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                    _("No task list found for some tasks"),
-                                    _("Some of the tasks in your Todo.txt file do not have a task list. To 
Do supports tasks with a task list. Please add a list to all your tasks"));
+                                      _("No task list found for some tasks"),
+                                      _("Some of the tasks in your Todo.txt file do not have a task list. To 
Do supports tasks with a task list. Please add a list to all your tasks"));
       return FALSE;
     }
 
@@ -401,7 +393,7 @@ gtd_todo_txt_parser_validate_token_format (GList *tokens)
 }
 
 GList*
-gtd_todo_txt_parser_tokenize (gchar *line)
+gtd_todo_txt_parser_tokenize (const gchar *line)
 {
   GList *tokens = NULL;
   gchar **token = NULL;
@@ -414,9 +406,8 @@ gtd_todo_txt_parser_tokenize (gchar *line)
     tokens = g_list_prepend (tokens, g_strdup(*token));
     token++;
   }
-  tokens = g_list_reverse (tokens);
 
-  g_free (line);
+  tokens = g_list_reverse (tokens);
 
   return tokens;
 }
@@ -430,7 +421,7 @@ gtd_todo_txt_parser_get_list_updated_token (GtdTaskList *list,
   GList *tokens = NULL;
   GList *it = NULL;
 
-  tokens = gtd_todo_txt_parser_tokenize (g_strdup(line));
+  tokens = gtd_todo_txt_parser_tokenize (line);
 
   for (it = tokens; it != NULL; it = it->next)
     {
diff --git a/plugins/todo-txt/gtd-todo-txt-parser.h b/plugins/todo-txt/gtd-todo-txt-parser.h
index 65eb749..dfeb081 100644
--- a/plugins/todo-txt/gtd-todo-txt-parser.h
+++ b/plugins/todo-txt/gtd-todo-txt-parser.h
@@ -30,43 +30,43 @@ typedef struct _TaskData TaskData;
 
 G_DECLARE_FINAL_TYPE (GtdTodoTxtParser, gtd_todo_txt_parser, GTD, TODO_TXT_PARSER, GtdObject)
 
-gint          gtd_todo_txt_parser_get_priority                    (gchar       *token);
+gint          gtd_todo_txt_parser_get_priority                    (gchar             *token);
 
-GDateTime*    gtd_todo_txt_parser_get_date                        (gchar       *token);
+GDateTime*    gtd_todo_txt_parser_get_date                        (gchar             *token);
 
-gboolean      gtd_todo_txt_parser_is_date                         (gchar       *dt);
+gboolean      gtd_todo_txt_parser_is_date                         (gchar             *dt);
 
-gboolean      gtd_todo_txt_parser_is_word                         (gchar       *token);
+gboolean      gtd_todo_txt_parser_is_word                         (gchar             *token);
 
-gint          gtd_todo_txt_parser_get_token_id                    (gchar       *token,
-                                                                   gint         last_read);
+gint          gtd_todo_txt_parser_get_token_id                    (gchar             *token,
+                                                                   gint               last_read);
 
-TaskData*     gtd_todo_txt_parser_parse_tokens                    (GList       *tk);
+TaskData*     gtd_todo_txt_parser_parse_tokens                    (GList             *tk);
 
-gboolean      gtd_todo_txt_parser_validate_token_format           (GList       *tokens);
+gboolean      gtd_todo_txt_parser_validate_token_format           (GList             *tokens);
 
-GList*        gtd_todo_txt_parser_tokenize                        (gchar       *line);
+GList*        gtd_todo_txt_parser_tokenize                        (const gchar       *line);
 
-GList*        gtd_todo_txt_parser_get_list_updated_token          (GtdTaskList *list,
-                                                                   gchar       *line);
+GList*        gtd_todo_txt_parser_get_list_updated_token          (GtdTaskList       *list,
+                                                                   gchar             *line);
 
-GList*        gtd_todo_txt_parser_get_task_line                   (GtdTask     *task);
+GList*        gtd_todo_txt_parser_get_task_line                   (GtdTask           *task);
 
 /*Accessor Methods for TaskData Strcuture*/
 
-gchar*        gtd_todo_txt_parser_task_data_get_root_task_name    (TaskData    *td);
+gchar*        gtd_todo_txt_parser_task_data_get_root_task_name    (TaskData          *td);
 
-gchar*        gtd_todo_txt_parser_task_data_get_task_list_name    (TaskData    *td);
+gchar*        gtd_todo_txt_parser_task_data_get_task_list_name    (TaskData          *td);
 
-gchar*        gtd_todo_txt_parser_task_data_get_title             (TaskData    *td);
+gchar*        gtd_todo_txt_parser_task_data_get_title             (TaskData          *td);
 
-GDateTime*    gtd_todo_txt_parser_task_data_get_due_date          (TaskData    *td);
+GDateTime*    gtd_todo_txt_parser_task_data_get_due_date          (TaskData          *td);
 
-gboolean      gtd_todo_txt_parser_task_data_is_subtask            (TaskData    *td);
+gboolean      gtd_todo_txt_parser_task_data_is_subtask            (TaskData          *td);
 
-gboolean      gtd_todo_txt_parser_task_data_is_task_completed     (TaskData    *td);
+gboolean      gtd_todo_txt_parser_task_data_is_task_completed     (TaskData          *td);
 
-gint          gtd_todo_txt_parser_task_data_get_priority          (TaskData    *td);
+gint          gtd_todo_txt_parser_task_data_get_priority          (TaskData          *td);
 
 G_END_DECLS
 


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