[gnome-todo] todo-txt: move file-monitor from todo_txt_plugin to todo_txt_provider



commit e6e36962ce417f4f1cea44749299a1ea6b7994e4
Author: Rohit Kaushik <kaushikrohit325 gmail com>
Date:   Tue May 23 18:58:35 2017 +0530

    todo-txt: move file-monitor from todo_txt_plugin to todo_txt_provider
    
    We are initalising file-monitor in plugin class, but since we will
    move the reload-function completely to gtd-provider-todo-txt,
    there is no need for keeping an extra reference in plugin class.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781079

 plugins/todo-txt/gtd-plugin-todo-txt.c   |   79 ------------------------------
 plugins/todo-txt/gtd-plugin-todo-txt.h   |    6 --
 plugins/todo-txt/gtd-provider-todo-txt.c |   68 +++++++++-----------------
 plugins/todo-txt/gtd-provider-todo-txt.h |    3 -
 4 files changed, 23 insertions(+), 133 deletions(-)
---
diff --git a/plugins/todo-txt/gtd-plugin-todo-txt.c b/plugins/todo-txt/gtd-plugin-todo-txt.c
index 14f2ed8..50d2bca 100644
--- a/plugins/todo-txt/gtd-plugin-todo-txt.c
+++ b/plugins/todo-txt/gtd-plugin-todo-txt.c
@@ -34,7 +34,6 @@ struct _GtdPluginTodoTxt
   PeasExtensionBase   parent;
 
   GFile              *source_file;
-  GFileMonitor       *monitor;
 
   GSettings          *settings;
 
@@ -59,78 +58,6 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtdPluginTodoTxt, gtd_plugin_todo_txt, PEAS_TYPE
                                 G_IMPLEMENT_INTERFACE_DYNAMIC (GTD_TYPE_ACTIVATABLE,
                                                                gtd_activatable_iface_init))
 
-void
-gtd_plugin_todo_txt_monitor_source (GFileMonitor      *monitor,
-                                    GFile             *first,
-                                    GFile             *second,
-                                    GFileMonitorEvent  event,
-                                    gpointer           data)
-{
-  GtdProviderTodoTxt *provider;
-  GtdPluginTodoTxt *self;
-  GError *error = NULL;
-
-  self = data;
-
-  provider = self->providers->data;
-
-  g_list_free_full (self->providers, g_object_unref);
-  self->providers = NULL;
-
-  g_signal_emit_by_name (self, "provider-removed", provider);
-
-  if (event == G_FILE_MONITOR_EVENT_DELETED)
-    {
-      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);
-}
-
-static void
-gtd_plugin_todo_txt_load_source_monitor (GtdPluginTodoTxt *self)
-{
-  GError *file_monitor = NULL;
-
-  self->monitor = g_file_monitor_file (self->source_file,
-                                       G_FILE_MONITOR_WATCH_MOVES,
-                                       NULL,
-                                       &file_monitor);
-
-  if (file_monitor)
-    {
-      gtd_manager_emit_error_message (gtd_manager_get_default (),
-                                      _("Error while opening the file monitor. Todo.txt will not be 
monitored"),
-                                      file_monitor->message,
-                                      NULL,
-                                      NULL);
-      g_clear_error (&file_monitor);
-    }
-  else
-    {
-      gtd_provider_todo_txt_set_monitor (self->providers->data, self->monitor);
-      g_signal_connect (self->monitor, "changed", G_CALLBACK (gtd_plugin_todo_txt_monitor_source), self);
-    }
-}
-
 static gboolean
 gtd_plugin_todo_txt_set_default_source (GtdPluginTodoTxt *self)
 {
@@ -215,8 +142,6 @@ gtd_plugin_todo_txt_activate (GtdActivatable *activatable)
   self->providers = g_list_append (self->providers, provider);
   g_signal_emit_by_name (self, "provider-added", provider);
 
-  gtd_plugin_todo_txt_load_source_monitor (self);
-
   g_free (source);
   g_clear_error (&error);
 }
@@ -316,8 +241,6 @@ gtd_plugin_todo_txt_source_changed_finished_cb (GtdPluginTodoTxt *self)
   provider = gtd_provider_todo_txt_new (self->source_file);
   self->providers = g_list_append (self->providers, provider);
 
-  gtd_plugin_todo_txt_load_source_monitor (self);
-
   g_signal_emit_by_name (self, "provider-added", provider);
 
   g_free (source);
@@ -333,7 +256,6 @@ gtd_plugin_todo_txt_source_changed_cb (GtkWidget *preference_panel,
 
   self = GTD_PLUGIN_TODO_TXT (user_data);
 
-  g_clear_object (&self->monitor);
   g_clear_object (&self->source_file);
 
   g_settings_set_string (self->settings,
@@ -359,7 +281,6 @@ gtd_plugin_todo_txt_finalize (GObject *object)
 {
   GtdPluginTodoTxt *self = (GtdPluginTodoTxt *) object;
 
-  g_clear_object (&self->monitor);
   g_clear_object (&self->source_file);
   g_list_free_full (self->providers, g_object_unref);
   self->providers = NULL;
diff --git a/plugins/todo-txt/gtd-plugin-todo-txt.h b/plugins/todo-txt/gtd-plugin-todo-txt.h
index 2e16834..e4e4bbf 100644
--- a/plugins/todo-txt/gtd-plugin-todo-txt.h
+++ b/plugins/todo-txt/gtd-plugin-todo-txt.h
@@ -30,12 +30,6 @@ G_DECLARE_FINAL_TYPE (GtdPluginTodoTxt, gtd_plugin_todo_txt, GTD, PLUGIN_TODO_TX
 
 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);
-
 G_END_DECLS
 
 #endif /* GTD_TODO_TXT_PLUGIN_H */
diff --git a/plugins/todo-txt/gtd-provider-todo-txt.c b/plugins/todo-txt/gtd-provider-todo-txt.c
index d7db273..3881585 100644
--- a/plugins/todo-txt/gtd-provider-todo-txt.c
+++ b/plugins/todo-txt/gtd-provider-todo-txt.c
@@ -353,6 +353,28 @@ gtd_provider_todo_txt_load_source (GtdProviderTodoTxt *self)
 }
 
 static void
+gtd_provider_todo_txt_load_source_monitor (GtdProviderTodoTxt *self)
+{
+  GError *error = NULL;
+
+  self->monitor = g_file_monitor_file (self->source_file,
+                                       G_FILE_MONITOR_WATCH_MOVES,
+                                       NULL,
+                                       &error);
+
+  if (error)
+    {
+      gtd_manager_emit_error_message (gtd_manager_get_default (),
+                                      _("Error while opening the file monitor. Todo.txt will not be 
monitored"),
+                                      error->message,
+                                      NULL,
+                                      NULL);
+      g_clear_error (&error);
+      return;
+    }
+}
+
+static void
 gtd_provider_todo_txt_create_task (GtdProvider *provider,
                                    GtdTask     *task)
 {
@@ -371,9 +393,6 @@ gtd_provider_todo_txt_create_task (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);
-
   list = gtd_task_get_list (task);
   list_name = gtd_task_list_get_name (list);
   task_description = gtd_task_get_title (task);
@@ -426,9 +445,6 @@ gtd_provider_todo_txt_create_task (GtdProvider *provider,
                          NULL,
                          NULL);
 out:
-  if (self->monitor)
-    g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
-
   g_free (task_line);
 }
 
@@ -452,9 +468,6 @@ gtd_provider_todo_txt_update_task (GtdProvider *provider,
   line_number = 0;
   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);
-
   line_to_update = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "line"));
 
   g_return_if_fail (G_IS_FILE (self->source_file));
@@ -560,9 +573,6 @@ gtd_provider_todo_txt_update_task (GtdProvider *provider,
   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);
 }
 
 static void
@@ -588,9 +598,6 @@ gtd_provider_todo_txt_remove_task (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);
-
   line_number_to_remove = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "line"));
   readstream = g_file_read (self->source_file,
                             NULL,
@@ -676,9 +683,6 @@ gtd_provider_todo_txt_remove_task (GtdProvider *provider,
   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);
 }
 
 static void
@@ -698,9 +702,6 @@ gtd_provider_todo_txt_create_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);
-
   write_stream = g_file_append_to (self->source_file,
                                   G_FILE_CREATE_REPLACE_DESTINATION,
                                   NULL,
@@ -746,9 +747,6 @@ gtd_provider_todo_txt_create_task_list (GtdProvider *provider,
 
 out:
   g_free (put);
-
-  if (self->monitor)
-    g_signal_handlers_unblock_by_func (self->monitor, gtd_plugin_todo_txt_monitor_source, self);
 }
 
 static void
@@ -772,9 +770,6 @@ 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);
-
   stored_list_name = g_object_get_data (G_OBJECT (list), "line");
   current_list_name = gtd_task_list_get_name (list);
 
@@ -884,9 +879,6 @@ gtd_provider_todo_txt_update_task_list (GtdProvider *provider,
 
   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);
 }
 
 static void
@@ -912,9 +904,6 @@ gtd_provider_todo_txt_remove_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);
-
   readstream = g_file_read (self->source_file,
                             NULL,
                             &error);
@@ -994,9 +983,6 @@ gtd_provider_todo_txt_remove_task_list (GtdProvider *provider,
   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);
 }
 
 static GList*
@@ -1116,6 +1102,7 @@ gtd_provider_todo_txt_set_property (GObject      *object,
     case PROP_SOURCE:
       self->source_file = g_value_dup_object (value);
       gtd_provider_todo_txt_load_source (self);
+      gtd_provider_todo_txt_load_source_monitor (self);
       break;
 
     default:
@@ -1160,12 +1147,3 @@ gtd_provider_todo_txt_init (GtdProviderTodoTxt *self)
   /* icon */
   self->icon = G_ICON (g_themed_icon_new_with_default_fallbacks ("computer-symbolic"));
 }
-
-void
-gtd_provider_todo_txt_set_monitor (GtdProviderTodoTxt *self,
-                                   GFileMonitor       *monitor)
-{
-  g_return_if_fail (GTD_IS_PROVIDER_TODO_TXT (self));
-
-  self->monitor = monitor;
-}
diff --git a/plugins/todo-txt/gtd-provider-todo-txt.h b/plugins/todo-txt/gtd-provider-todo-txt.h
index c97d7ac..a4c0705 100644
--- a/plugins/todo-txt/gtd-provider-todo-txt.h
+++ b/plugins/todo-txt/gtd-provider-todo-txt.h
@@ -31,9 +31,6 @@ G_DECLARE_FINAL_TYPE (GtdProviderTodoTxt, gtd_provider_todo_txt, GTD, PROVIDER_T
 
 GtdProviderTodoTxt*    gtd_provider_todo_txt_new                     (GFile         *source_file);
 
-void                   gtd_provider_todo_txt_set_monitor             (GtdProviderTodoTxt *self,
-                                                                      GFileMonitor       *monitor);
-
 G_END_DECLS
 
 #endif /* GTD_PROVIDER_TODO_TXT_H */


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