[gnome-todo] todo-txt: improve code
- From: Georges Basile Stavracas Neto <gbsneto src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-todo] todo-txt: improve code
- Date: Thu, 20 Apr 2017 13:08:50 +0000 (UTC)
commit 082e89e3460c33550325304650e9f4c64c018ccf
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date: Thu Apr 20 10:08:23 2017 -0300
todo-txt: improve code
plugins/todo-txt/gtd-plugin-todo-txt.c | 59 +--
plugins/todo-txt/gtd-provider-todo-txt.c | 388 ++++++++++-----------
2 files changed, 208 insertions(+), 239 deletions(-)
---
diff --git a/plugins/todo-txt/gtd-plugin-todo-txt.c b/plugins/todo-txt/gtd-plugin-todo-txt.c
index edaa9d6..fd95eb0 100644
--- a/plugins/todo-txt/gtd-plugin-todo-txt.c
+++ b/plugins/todo-txt/gtd-plugin-todo-txt.c
@@ -96,6 +96,7 @@ gtd_plugin_todo_txt_monitor_source (GFileMonitor *monitor,
return;
}
}
+
provider = gtd_provider_todo_txt_new (self->source_file);
self->providers = g_list_append (self->providers, provider);
@@ -129,45 +130,33 @@ gtd_plugin_todo_txt_load_source_monitor (GtdPluginTodoTxt *self)
static gboolean
gtd_plugin_todo_txt_set_default_source (GtdPluginTodoTxt *self)
{
- gchar *default_file;
+ g_autofree gchar *default_file;
GError *error;
- default_file = g_build_filename (g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS), "todo.txt", NULL);
error = NULL;
+ default_file = g_build_filename (g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS),
+ "todo.txt",
+ NULL);
+ self->source_file = g_file_new_for_path (default_file);
+
+ if (g_file_query_exists (self->source_file, NULL))
+ return TRUE;
- default_file = g_filename_to_uri (default_file, NULL, &error);
+ g_file_create (self->source_file,
+ G_FILE_CREATE_NONE,
+ NULL,
+ &error);
if (error)
{
gtd_manager_emit_error_message (gtd_manager_get_default (),
- _("Error while converting the default Todo.txt path to an URI"),
+ _("Cannot create Todo.txt file"),
error->message);
g_clear_error (&error);
return FALSE;
}
- else
- {
- self->source_file = g_file_new_for_uri (default_file);
- }
-
- if (!g_file_query_exists (self->source_file, NULL))
- {
- g_file_create (self->source_file,
- G_FILE_CREATE_NONE,
- NULL,
- &error);
- if (error)
- {
- gtd_manager_emit_error_message (gtd_manager_get_default (),
- _("Cannot create Todo.txt file"),
- error->message);
-
- g_clear_error (&error);
- return FALSE;
- }
- }
return TRUE;
}
@@ -187,10 +176,7 @@ gtd_plugin_todo_txt_activate (GtdActivatable *activatable)
if (!source || source[0] == '\0')
{
- gboolean set;
- set = gtd_plugin_todo_txt_set_default_source (self);
-
- if (!set)
+ if (!gtd_plugin_todo_txt_set_default_source (self))
return;
}
else
@@ -418,19 +404,15 @@ gtd_plugin_todo_txt_init (GtdPluginTodoTxt *self)
"orientation", GTK_ORIENTATION_VERTICAL,
NULL);
label = gtk_label_new (_("Select a Todo.txt-formatted file:"));
- self->preferences = gtk_file_chooser_button_new (_("Select a file"),
- GTK_FILE_CHOOSER_ACTION_OPEN);
+ self->preferences = gtk_file_chooser_button_new (_("Select a file"), GTK_FILE_CHOOSER_ACTION_OPEN);
+
+ gtk_widget_set_size_request (GTK_WIDGET (self->preferences_box), 300, 0);
- gtk_widget_set_size_request (GTK_WIDGET (self->preferences_box),
- 300,
- 0);
gtk_container_add (GTK_CONTAINER (self->preferences_box), label);
gtk_container_add (GTK_CONTAINER (self->preferences_box), self->preferences);
- gtk_widget_set_halign (GTK_WIDGET (self->preferences_box),
- GTK_ALIGN_CENTER);
- gtk_widget_set_valign (GTK_WIDGET (self->preferences_box),
- GTK_ALIGN_CENTER);
+ gtk_widget_set_halign (GTK_WIDGET (self->preferences_box), GTK_ALIGN_CENTER);
+ gtk_widget_set_valign (GTK_WIDGET (self->preferences_box), GTK_ALIGN_CENTER);
gtk_widget_show_all (self->preferences_box);
@@ -444,7 +426,6 @@ gtd_plugin_todo_txt_init (GtdPluginTodoTxt *self)
static void
gtd_plugin_todo_txt_class_finalize (GtdPluginTodoTxtClass *klass)
{
-
}
G_MODULE_EXPORT void
diff --git a/plugins/todo-txt/gtd-provider-todo-txt.c b/plugins/todo-txt/gtd-provider-todo-txt.c
index 494787f..3661e81 100644
--- a/plugins/todo-txt/gtd-provider-todo-txt.c
+++ b/plugins/todo-txt/gtd-provider-todo-txt.c
@@ -165,7 +165,9 @@ gtd_provider_todo_txt_load_task (TaskData *td,
GtdTask *sub_task;
if (g_hash_table_contains (self->lists, task_list_name))
- task_list = g_hash_table_lookup (self->lists, task_list_name);
+ {
+ task_list = g_hash_table_lookup (self->lists, task_list_name);
+ }
else
{
task_list = gtd_task_list_new (GTD_PROVIDER (self));
@@ -248,23 +250,22 @@ gtd_provider_todo_txt_load_task (TaskData *td,
static void
gtd_provider_todo_txt_create_empty_list (GtdProviderTodoTxt *self,
- gchar *line)
+ gchar *name)
{
GtdTaskList *task_list;
- if (g_hash_table_contains (self->lists, line))
+ if (g_hash_table_contains (self->lists, name))
return;
task_list = gtd_task_list_new (GTD_PROVIDER (self));
gtd_task_list_set_is_removable (task_list, TRUE);
- gtd_task_list_set_name (task_list, line);
- self->tasklists = g_list_append (self->tasklists,
- task_list);
- g_object_set_data (G_OBJECT (task_list), "line", line);
+ gtd_task_list_set_name (task_list, name);
+ self->tasklists = g_list_append (self->tasklists, task_list);
+ g_object_set_data (G_OBJECT (task_list), "line", name);
g_signal_emit_by_name (self, "list-added", task_list);
- g_hash_table_insert (self->lists, line, task_list);
+ g_hash_table_insert (self->lists, name, task_list);
}
static void
@@ -307,28 +308,7 @@ gtd_provider_todo_txt_load_source (GtdProviderTodoTxt *self)
NULL,
&line_read_error);
- if (!line_read_error)
- {
- if (!line_read)
- break;
-
- line_number++;
- self->no_of_lines++;
-
- 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 (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
+ if (line_read_error)
{
g_warning ("%s: %s: %s",
G_STRFUNC,
@@ -343,6 +323,25 @@ gtd_provider_todo_txt_load_source (GtdProviderTodoTxt *self)
return;
}
+ if (!line_read)
+ break;
+
+ line_number++;
+ self->no_of_lines++;
+
+ 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 (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);
+ }
+
g_list_free_full (tokens, g_free);
g_free (line_read);
}
@@ -395,7 +394,7 @@ gtd_provider_todo_txt_create_task (GtdProvider *provider,
NULL,
&write_error);
- if(write_error)
+ if (write_error)
{
g_warning ("%s: %s: %s",
G_STRFUNC,
@@ -406,21 +405,20 @@ gtd_provider_todo_txt_create_task (GtdProvider *provider,
_("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));
+
+ goto out;
}
+ 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);
-
+out:
if (self->monitor)
g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
@@ -484,69 +482,67 @@ gtd_provider_todo_txt_update_task (GtdProvider *provider,
NULL,
&line_read_error);
- if (!line_read_error)
+ if (line_read_error)
{
- if (!line_read)
- break;
+ 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_number++;
+ if (!line_read)
+ break;
- tokens = gtd_todo_txt_parser_tokenize (line_read);
- valid = gtd_todo_txt_parser_validate_token_format (tokens);
+ line_number++;
- if (valid &&
- line_number == line_to_update)
- {
- GList *update_tokens;
- GList *it;
+ 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;
- 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 while reading tasks from Todo.txt"),
- line_read_error->message);
+ line_read = strcat (line_read, "\n");
- 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_data_output_stream_put_string (writer,
+ line_read,
+ NULL,
+ &write_error);
}
g_list_free_full (tokens, g_free);
@@ -620,39 +616,7 @@ gtd_provider_todo_txt_remove_task (GtdProvider *provider,
skip = FALSE;
- if (!line_read_error)
- {
- if (!line_read)
- break;
- line_number++;
-
- 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);
- else
- td = NULL;
-
- if (line_number == line_number_to_remove)
- {
- skip = TRUE;
- self->no_of_lines--;
- }
-
- if (!skip)
- {
- line_read = strcat (line_read, "\n");
-
- g_data_output_stream_put_string (writer,
- line_read,
- NULL,
- &write_error);
- }
-
- g_free (td);
- }
- else
+ if (line_read_error)
{
g_warning ("%s: %s: %s",
G_STRFUNC,
@@ -665,6 +629,37 @@ gtd_provider_todo_txt_remove_task (GtdProvider *provider,
g_error_free (line_read_error);
return;
}
+
+ if (!line_read)
+ break;
+
+ line_number++;
+
+ 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);
+ else
+ td = NULL;
+
+ if (line_number == line_number_to_remove)
+ {
+ skip = TRUE;
+ self->no_of_lines--;
+ }
+
+ if (!skip)
+ {
+ line_read = strcat (line_read, "\n");
+
+ g_data_output_stream_put_string (writer,
+ line_read,
+ NULL,
+ &write_error);
+ }
+
+ g_free (td);
}
g_output_stream_close (G_OUTPUT_STREAM (writer),
@@ -802,71 +797,68 @@ gtd_provider_todo_txt_update_task_list (GtdProvider *provider,
NULL,
&line_read_error);
- if (!line_read_error)
+ if (line_read_error)
{
- if (!line_read)
- break;
+ g_warning ("%s: %s: %s",
+ G_STRFUNC,
+ _("Error while reading tasks 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 while reading tasks from Todo.txt"),
+ line_read_error->message);
+ g_error_free (line_read_error);
+ continue;
+ }
- if (valid)
- td = gtd_todo_txt_parser_parse_tokens (tokens);
+ if (!line_read)
+ break;
- 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;
+ tokens = gtd_todo_txt_parser_tokenize (line_read);
+ valid = gtd_todo_txt_parser_validate_token_format (tokens);
- update_tokens = gtd_todo_txt_parser_get_list_updated_token (list, g_strdup (line_read));
- it = NULL;
+ if (valid)
+ td = gtd_todo_txt_parser_parse_tokens (tokens);
- 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,
- "\n",
- NULL,
- &write_error);
- }
- else
- g_data_output_stream_put_string (writer,
- " ",
- NULL,
- &write_error);
- }
- g_list_free_full (update_tokens, g_free);
- }
+ 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;
- else
+ for (it = update_tokens; it != NULL; it = it->next)
{
- line_read = strcat (line_read, "\n");
-
g_data_output_stream_put_string (writer,
- line_read,
+ it->data,
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_warning ("%s: %s: %s",
- G_STRFUNC,
- _("Error while reading tasks from Todo.txt"),
- line_read_error->message);
+ line_read = strcat (line_read, "\n");
- 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_data_output_stream_put_string (writer,
+ line_read,
+ NULL,
+ &write_error);
}
g_list_free_full (tokens, g_free);
@@ -941,34 +933,7 @@ gtd_provider_todo_txt_remove_task_list (GtdProvider *provider,
skip = FALSE;
- if (!line_read_error)
- {
- if (!line)
- break;
-
- tokens = gtd_todo_txt_parser_tokenize (line);
- valid = gtd_todo_txt_parser_validate_token_format (tokens);
-
- if (valid)
- td = gtd_todo_txt_parser_parse_tokens (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");
-
- g_data_output_stream_put_string (writer,
- line,
- NULL,
- &write_error);
- }
- }
- else
+ if (line_read_error)
{
g_warning ("%s: %s: %s",
G_STRFUNC,
@@ -981,6 +946,31 @@ gtd_provider_todo_txt_remove_task_list (GtdProvider *provider,
g_error_free (line_read_error);
}
+ if (!line)
+ break;
+
+ tokens = gtd_todo_txt_parser_tokenize (line);
+ valid = gtd_todo_txt_parser_validate_token_format (tokens);
+
+ if (valid)
+ td = gtd_todo_txt_parser_parse_tokens (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");
+
+ g_data_output_stream_put_string (writer,
+ line,
+ NULL,
+ &write_error);
+ }
+
g_free (td);
}
@@ -1138,10 +1128,8 @@ gtd_provider_todo_txt_init (GtdProviderTodoTxt *self)
gtd_object_set_ready (GTD_OBJECT (self), TRUE);
self->no_of_lines = 0;
- self->lists = g_hash_table_new ((GHashFunc) g_str_hash,
- (GEqualFunc) g_str_equal);
- self->root_tasks = g_hash_table_new ((GHashFunc) g_str_hash,
- (GEqualFunc) g_str_equal);
+ self->lists = g_hash_table_new ((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal);
+ self->root_tasks = g_hash_table_new ((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal);
/* icon */
self->icon = G_ICON (g_themed_icon_new_with_default_fallbacks ("computer-symbolic"));
diff --git a/plugins/todo-txt/org.gnome.todo.txt.gschema.valid
b/plugins/todo-txt/org.gnome.todo.txt.gschema.valid
new file mode 100644
index 0000000..e69de29
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]