[gnome-todo] todo-txt: rework on plugin



commit 15a70bb1aebbe1ae31e586650125e5db376879a8
Author: Rohit Kaushik <kaushikrohit325 gmail com>
Date:   Thu May 25 21:10:56 2017 +0530

    todo-txt: rework on plugin
    
    Currently, the source is set after the plugin object is initialized,
    but this should be done in init itself so the first time the plugin is
    laoded, the provider is automatically loaded and we dont have to send a
    provider-added signal.
    Other Changes are addition of function to set source, this helps in
    reducing code redundancy and makes code easier and better.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781079

 plugins/todo-txt/gtd-plugin-todo-txt.c |  164 +++++++++++++-------------------
 1 files changed, 67 insertions(+), 97 deletions(-)
---
diff --git a/plugins/todo-txt/gtd-plugin-todo-txt.c b/plugins/todo-txt/gtd-plugin-todo-txt.c
index 50d2bca..bb488b5 100644
--- a/plugins/todo-txt/gtd-plugin-todo-txt.c
+++ b/plugins/todo-txt/gtd-plugin-todo-txt.c
@@ -58,92 +58,13 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtdPluginTodoTxt, gtd_plugin_todo_txt, PEAS_TYPE
                                 G_IMPLEMENT_INTERFACE_DYNAMIC (GTD_TYPE_ACTIVATABLE,
                                                                gtd_activatable_iface_init))
 
-static gboolean
-gtd_plugin_todo_txt_set_default_source (GtdPluginTodoTxt *self)
-{
-  g_autofree gchar *default_file;
-  GError *error;
-
-  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;
-
-  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,
-                                      NULL,
-                                      NULL);
-
-      g_clear_error (&error);
-      return FALSE;
-    }
-
-  return TRUE;
-}
-
 /*
  * GtdActivatable interface implementation
  */
 static void
 gtd_plugin_todo_txt_activate (GtdActivatable *activatable)
 {
-  GtdPluginTodoTxt *self;
-  GtdProviderTodoTxt *provider;
-  gchar *source;
-  GError *error = NULL;
-
-  self = GTD_PLUGIN_TODO_TXT (activatable);
-  source = g_settings_get_string (self->settings, "file");
-
-  if (!source || source[0] == '\0')
-    {
-      if (!gtd_plugin_todo_txt_set_default_source (self))
-        return;
-    }
-  else
-    {
-      self->source_file = g_file_new_for_uri (source);
-    }
-
-  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,
-                                          NULL,
-                                          NULL);
-
-          g_clear_error (&error);
-          return;
-        }
-    }
-
-  provider = gtd_provider_todo_txt_new (self->source_file);
-
-  self->providers = g_list_append (self->providers, provider);
-  g_signal_emit_by_name (self, "provider-added", provider);
-
-  g_free (source);
-  g_clear_error (&error);
+  ;
 }
 
 static void
@@ -177,7 +98,6 @@ static GList*
 gtd_plugin_todo_txt_get_providers (GtdActivatable *activatable)
 {
   GtdPluginTodoTxt *plugin = GTD_PLUGIN_TODO_TXT (activatable);
-
   return plugin->providers;
 }
 
@@ -196,22 +116,54 @@ gtd_activatable_iface_init (GtdActivatableInterface *iface)
  * Init
  */
 
-static void
-gtd_plugin_todo_txt_source_changed_finished_cb (GtdPluginTodoTxt *self)
+static gboolean
+gtd_plugin_todo_txt_set_default_source (GtdPluginTodoTxt *self)
 {
-  GtdProviderTodoTxt *provider;
-  gchar *source;
-  GError *error = NULL;
+  g_autofree gchar *default_file;
+  GError *error;
+
+  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;
+
+  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,
+                                      NULL,
+                                      NULL);
+
+      g_clear_error (&error);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+static gboolean
+gtd_plugin_todo_txt_set_source (GtdPluginTodoTxt *self)
+{
+  GError *error;
+  gchar  *source;
+
+  error = NULL;
   source = g_settings_get_string (self->settings, "file");
 
   if (!source || source[0] == '\0')
     {
-      gboolean set;
-      set = gtd_plugin_todo_txt_set_default_source (self);
-
-      if(!set)
-        return;
+      if (!gtd_plugin_todo_txt_set_default_source (self))
+        return FALSE;
     }
   else
     {
@@ -234,17 +186,28 @@ gtd_plugin_todo_txt_source_changed_finished_cb (GtdPluginTodoTxt *self)
                                           NULL);
 
           g_clear_error (&error);
-          return;
+          return FALSE;
         }
     }
 
+  return TRUE;
+}
+
+static void
+gtd_plugin_todo_txt_source_changed_finished_cb (GtdPluginTodoTxt *self)
+{
+  GtdProviderTodoTxt *provider;
+  gboolean set;
+
+  set = gtd_plugin_todo_txt_set_source (self);
+
+  if (!set)
+    return;
+
   provider = gtd_provider_todo_txt_new (self->source_file);
   self->providers = g_list_append (self->providers, provider);
 
   g_signal_emit_by_name (self, "provider-added", provider);
-
-  g_free (source);
-  g_clear_error (&error);
 }
 
 static void
@@ -275,13 +238,11 @@ gtd_plugin_todo_txt_source_changed_cb (GtkWidget *preference_panel,
   gtd_plugin_todo_txt_source_changed_finished_cb (self);
 }
 
-
 static void
 gtd_plugin_todo_txt_finalize (GObject *object)
 {
   GtdPluginTodoTxt *self = (GtdPluginTodoTxt *) object;
 
-  g_clear_object (&self->source_file);
   g_list_free_full (self->providers, g_object_unref);
   self->providers = NULL;
 
@@ -322,11 +283,20 @@ gtd_plugin_todo_txt_class_init (GtdPluginTodoTxtClass *klass)
 static void
 gtd_plugin_todo_txt_init (GtdPluginTodoTxt *self)
 {
+  GtdProviderTodoTxt *provider;
   GtkWidget *label;
+  gboolean   set;
 
   self->settings = g_settings_new ("org.gnome.todo.plugins.todo-txt");
+  set = gtd_plugin_todo_txt_set_source (self);
   self->providers = NULL;
 
+  if (set)
+    {
+      provider = gtd_provider_todo_txt_new (self->source_file);
+      self->providers = g_list_append (self->providers, provider);
+    }
+
   /* Preferences */
   self->preferences_box = g_object_new (GTK_TYPE_BOX,
                                         "margin", 18,


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