[gnote] Make note dir watcher interval configurable



commit a2c4177ea9bade019dbb6b63f29d08953ec901c1
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Jan 26 17:46:05 2014 +0200

    Make note dir watcher interval configurable

 data/org.gnome.gnote.gschema.xml.in                |    8 +++
 po/POTFILES.in                                     |    1 +
 src/addins/notedirectorywatcher/Makefile.am        |    5 ++-
 .../notedirectorywatcher.desktop.in                |    2 +-
 .../notedirectorywatcherapplicationaddin.cpp       |   39 ++++++++++++--
 .../notedirectorywatcherapplicationaddin.hpp       |    8 +++-
 .../notedirectorywatcherpreferences.cpp            |   52 ++++++++++++++++++++
 .../notedirectorywatcherpreferences.hpp            |   50 +++++++++++++++++++
 .../notedirectorywatcherpreferencesfactory.hpp     |   36 ++++++++++++++
 9 files changed, 192 insertions(+), 9 deletions(-)
---
diff --git a/data/org.gnome.gnote.gschema.xml.in b/data/org.gnome.gnote.gschema.xml.in
index 149c70b..eab1feb 100644
--- a/data/org.gnome.gnote.gschema.xml.in
+++ b/data/org.gnome.gnote.gschema.xml.in
@@ -134,6 +134,7 @@
     <child name="export-html" schema="org.gnome.gnote.export-html" />
     <child name="sync" schema="org.gnome.gnote.sync" />
     <child name="insert-timestamp" schema="org.gnome.gnote.insert-timestamp" />
+    <child name="note-directory-watcher" schema="org.gnome.gnote.note-directory-watcher" />
   </schema>
   <schema id="org.gnome.gnote.global-keybindings" path="/org/gnome/gnote/global-keybindings/">
     <key name="show-note-menu" type="s">
@@ -231,4 +232,11 @@
       <_description>The date format that is used for the timestamp. It follows the format of 
strftime(3).</_description>
     </key>
   </schema>
+  <schema id="org.gnome.gnote.note-directory-watcher" path="/org/gnome/gnote/note-directory-watcher/">
+    <key name="check-interval" type="i">
+      <default>5</default>
+      <_summary>Time between checks</_summary>
+      <_description>Time interval between note directory checks (in seconds). Min value is 5.</_description>
+    </key>
+  </schema>
 </schemalist>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 693c0e0..49d12a5 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -22,6 +22,7 @@ src/addins/inserttimestamp/inserttimestamp.desktop.in
 src/addins/inserttimestamp/inserttimestampnoteaddin.cpp
 src/addins/inserttimestamp/inserttimestamppreferences.cpp
 src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.cpp
+src/addins/notedirectorywatcher/notedirectorywatcherpreferences.cpp
 src/addins/notedirectorywatcher/notedirectorywatcher.desktop.in
 src/addins/noteoftheday/noteoftheday.cpp
 src/addins/noteoftheday/noteoftheday.desktop.in
diff --git a/src/addins/notedirectorywatcher/Makefile.am b/src/addins/notedirectorywatcher/Makefile.am
index c414af1..0e859ac 100644
--- a/src/addins/notedirectorywatcher/Makefile.am
+++ b/src/addins/notedirectorywatcher/Makefile.am
@@ -11,7 +11,10 @@ addins_LTLIBRARIES = notedirectorywatcher.la
 addins_DATA = $(desktop_files)
 
 
-notedirectorywatcher_la_SOURCES = notedirectorywatcherapplicationaddin.hpp 
notedirectorywatcherapplicationaddin.cpp \
+notedirectorywatcher_la_SOURCES = \
+       notedirectorywatcherapplicationaddin.hpp notedirectorywatcherapplicationaddin.cpp \
+       notedirectorywatcherpreferences.hpp notedirectorywatcherpreferences.cpp \
+       notedirectorywatcherpreferencesfactory.hpp \
        $(NULL)
 
 EXTRA_DIST = $(desktop_in_files)
diff --git a/src/addins/notedirectorywatcher/notedirectorywatcher.desktop.in 
b/src/addins/notedirectorywatcher/notedirectorywatcher.desktop.in
index 58a3c43..0dcc4c1 100644
--- a/src/addins/notedirectorywatcher/notedirectorywatcher.desktop.in
+++ b/src/addins/notedirectorywatcher/notedirectorywatcher.desktop.in
@@ -4,6 +4,6 @@ _Name=Note Directory Watcher
 _Description=Watch your Gnote note directory for changes to your notes.
 _Authors=Aurimas Černius and Tomboy original authors
 Category=Tools
-Version=0.2
+Version=0.3
 DefaultEnabled=false
 Module=notedirectorywatcher
diff --git a/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.cpp 
b/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.cpp
index 4e73397..a8d8fa8 100644
--- a/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.cpp
+++ b/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2013 Aurimas Cernius
+ * Copyright (C) 2012-2014 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@
 
 #include "debug.hpp"
 #include "notedirectorywatcherapplicationaddin.hpp"
+#include "notedirectorywatcherpreferencesfactory.hpp"
 #include "notemanager.hpp"
 #include "sharp/files.hpp"
 #include "sharp/string.hpp"
@@ -34,6 +35,7 @@ namespace notedirectorywatcher {
 NoteDirectoryWatcherModule::NoteDirectoryWatcherModule()
 {
   ADD_INTERFACE_IMPL(NoteDirectoryWatcherApplicationAddin);
+  ADD_INTERFACE_IMPL(NoteDirectoryWatcherPreferencesFactory);
 }
 
 
@@ -48,21 +50,30 @@ void NoteDirectoryWatcherApplicationAddin::initialize()
 {
   gnote::NoteManager & manager(note_manager());
   std::string note_path = manager.get_notes_dir();
-  manager.signal_note_saved
+  m_signal_note_saved_cid = manager.signal_note_saved
     .connect(sigc::mem_fun(*this, &NoteDirectoryWatcherApplicationAddin::handle_note_saved));
 
   Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(note_path);
   m_file_system_watcher = file->monitor_directory();
 
-  m_file_system_watcher->signal_changed()
+  m_signal_changed_cid = m_file_system_watcher->signal_changed()
     .connect(sigc::mem_fun(*this, &NoteDirectoryWatcherApplicationAddin::handle_file_system_change_event));
 
+  Glib::RefPtr<Gio::Settings> settings = 
gnote::Preferences::obj().get_schema_settings(SCHEMA_NOTE_DIRECTORY_WATCHER);
+  m_check_interval = settings->get_int(CHECK_INTERVAL);
+  sanitize_check_interval(settings);
+  m_signal_settings_changed_cid = settings->signal_changed()
+    .connect(sigc::mem_fun(*this, &NoteDirectoryWatcherApplicationAddin::on_settings_changed));
+
   m_initialized = true;
 }
 
 void NoteDirectoryWatcherApplicationAddin::shutdown()
 {
   m_file_system_watcher->cancel();
+  m_signal_note_saved_cid.disconnect();
+  m_signal_changed_cid.disconnect();
+  m_signal_settings_changed_cid.disconnect();
   m_initialized = false;
 }
 
@@ -127,7 +138,7 @@ void NoteDirectoryWatcherApplicationAddin::handle_file_system_change_event(
   {}
   m_lock.unlock();
 
-  Glib::RefPtr<Glib::TimeoutSource> timeout = Glib::TimeoutSource::create(5000);
+  Glib::RefPtr<Glib::TimeoutSource> timeout = Glib::TimeoutSource::create(m_check_interval * 1000);
   timeout->connect(sigc::mem_fun(*this, &NoteDirectoryWatcherApplicationAddin::handle_timeout));
   timeout->attach();
 }
@@ -152,9 +163,9 @@ bool NoteDirectoryWatcherApplicationAddin::handle_timeout()
         iter != m_file_change_records.end(); ++iter) {
       DBG_OUT("NoteDirectoryWatcher: Handling (timeout) %s", iter->first.c_str());
 
-      // Check that Note.Saved event didn't occur within 3 seconds of last write
+      // Check that Note.Saved event didn't occur within (check-interval -2) seconds of last write
       if(m_note_save_times.find(iter->first) != m_note_save_times.end() &&
-          std::abs((m_note_save_times[iter->first] - iter->second.last_change).total_seconds()) <= 3) {
+          std::abs((m_note_save_times[iter->first] - iter->second.last_change).total_seconds()) <= 
(m_check_interval - 2)) {
         DBG_OUT("NoteDirectoryWatcher: Ignoring (timeout) because it was probably a Gnote write");
         keysToRemove.push_back(iter->first);
         continue;
@@ -288,6 +299,22 @@ std::string NoteDirectoryWatcherApplicationAddin::make_uri(const std::string & n
   return "note://gnote/" + note_id;
 }
 
+void NoteDirectoryWatcherApplicationAddin::on_settings_changed(const Glib::ustring & key)
+{
+  if(key == CHECK_INTERVAL) {
+    Glib::RefPtr<Gio::Settings> settings = 
gnote::Preferences::obj().get_schema_settings(SCHEMA_NOTE_DIRECTORY_WATCHER);
+    m_check_interval = settings->get_int(key);
+    sanitize_check_interval(settings);
+  }
+}
+
+void NoteDirectoryWatcherApplicationAddin::sanitize_check_interval(const Glib::RefPtr<Gio::Settings> & 
settings)
+{
+  if(m_check_interval < 5) {
+    m_check_interval = 5;
+    settings->set_int(CHECK_INTERVAL, m_check_interval);
+  }
+}
 
 }
 
diff --git a/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.hpp 
b/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.hpp
index 0bd7bba..6885cc6 100644
--- a/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.hpp
+++ b/src/addins/notedirectorywatcher/notedirectorywatcherapplicationaddin.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2013 Aurimas Cernius
+ * Copyright (C) 2012-2014 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -75,12 +75,18 @@ private:
   bool handle_timeout();
   void delete_note(const std::string & note_id);
   void add_or_update_note(const std::string & note_id);
+  void on_settings_changed(const Glib::ustring & key);
+  void sanitize_check_interval(const Glib::RefPtr<Gio::Settings> & settings);
 
   Glib::RefPtr<Gio::FileMonitor> m_file_system_watcher;
 
   std::map<std::string, NoteFileChangeRecord> m_file_change_records;
   std::map<std::string, sharp::DateTime> m_note_save_times;
+  sigc::connection m_signal_note_saved_cid;
+  sigc::connection m_signal_changed_cid;
+  sigc::connection m_signal_settings_changed_cid;
   bool m_initialized;
+  int m_check_interval;
   Glib::Threads::Mutex m_lock;
 };
 
diff --git a/src/addins/notedirectorywatcher/notedirectorywatcherpreferences.cpp 
b/src/addins/notedirectorywatcher/notedirectorywatcherpreferences.cpp
new file mode 100644
index 0000000..309c182
--- /dev/null
+++ b/src/addins/notedirectorywatcher/notedirectorywatcherpreferences.cpp
@@ -0,0 +1,52 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2014 Aurimas Cernius
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <glibmm/i18n.h>
+
+#include "notedirectorywatcherpreferences.hpp"
+
+namespace notedirectorywatcher {
+
+const char *SCHEMA_NOTE_DIRECTORY_WATCHER = "org.gnome.gnote.note-directory-watcher";
+const char *CHECK_INTERVAL = "check-interval";
+
+
+NoteDirectoryWatcherPreferences::NoteDirectoryWatcherPreferences(gnote::NoteManager &)
+  : m_check_interval(1)
+{
+  Gtk::Label *label = manage(new Gtk::Label(_("_Directory check interval:"), true));
+  attach(*label, 0, 0, 1, 1);
+  m_check_interval.set_range(5, 300);
+  m_check_interval.set_increments(1, 5);
+  m_check_interval.signal_value_changed()
+    .connect(sigc::mem_fun(*this, &NoteDirectoryWatcherPreferences::on_interval_changed));
+  m_check_interval.set_value(
+    gnote::Preferences::obj().get_schema_settings(SCHEMA_NOTE_DIRECTORY_WATCHER)->get_int(CHECK_INTERVAL));
+  attach(m_check_interval, 1, 0, 1, 1);
+}
+
+void NoteDirectoryWatcherPreferences::on_interval_changed()
+{
+  gnote::Preferences::obj().get_schema_settings(SCHEMA_NOTE_DIRECTORY_WATCHER)->set_int(
+    CHECK_INTERVAL, m_check_interval.get_value_as_int());
+}
+
+}
+
diff --git a/src/addins/notedirectorywatcher/notedirectorywatcherpreferences.hpp 
b/src/addins/notedirectorywatcher/notedirectorywatcherpreferences.hpp
new file mode 100644
index 0000000..6c71a8d
--- /dev/null
+++ b/src/addins/notedirectorywatcher/notedirectorywatcherpreferences.hpp
@@ -0,0 +1,50 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2014 Aurimas Cernius
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _ADDINS_NOTE_DIRECTORY_WATCHER_PREFERENCES_
+#define _ADDINS_NOTE_DIRECTORY_WATCHER_PREFERENCES_
+
+#include <gtkmm/grid.h>
+#include <gtkmm/spinbutton.h>
+
+#include "notemanager.hpp"
+
+
+namespace notedirectorywatcher {
+
+extern const char *SCHEMA_NOTE_DIRECTORY_WATCHER;
+extern const char *CHECK_INTERVAL;
+
+
+class NoteDirectoryWatcherPreferences
+  : public Gtk::Grid
+{
+public:
+  NoteDirectoryWatcherPreferences(gnote::NoteManager &);
+private:
+  void on_interval_changed();
+
+  Gtk::SpinButton m_check_interval;
+};
+
+}
+
+#endif
+
diff --git a/src/addins/notedirectorywatcher/notedirectorywatcherpreferencesfactory.hpp 
b/src/addins/notedirectorywatcher/notedirectorywatcherpreferencesfactory.hpp
new file mode 100644
index 0000000..62d1c21
--- /dev/null
+++ b/src/addins/notedirectorywatcher/notedirectorywatcherpreferencesfactory.hpp
@@ -0,0 +1,36 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2014 Aurimas Cernius
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _ADDINS_NOTE_DIRECTORY_WATCHER_PREFERENCES_FACTORY_
+#define _ADDINS_NOTE_DIRECTORY_WATCHER_PREFERENCES_FACTORY_
+
+#include "addinpreferencefactory.hpp"
+#include "notedirectorywatcherpreferences.hpp"
+
+namespace notedirectorywatcher {
+
+class NoteDirectoryWatcherPreferencesFactory
+  : public gnote::AddinPreferenceFactory<NoteDirectoryWatcherPreferences>
+{};
+
+}
+
+#endif
+


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