[gnote] Add file system synchronization addin



commit bc92d032a09d03747e9d7bb41242b5d683b6d0cf
Author: Aurimas Äernius <aurisc4 gmail com>
Date:   Tue Jan 24 23:52:20 2012 +0200

    Add file system synchronization addin
    
    An addin to synchronize with local folder.

 configure.ac                                       |    1 +
 src/addins/Makefile.am                             |    1 +
 src/addins/filesystemsyncservice/Makefile.am       |    9 +
 .../filesystemsyncserviceaddin.cpp                 |  267 ++++++++++++++++++++
 .../filesystemsyncserviceaddin.hpp                 |   83 ++++++
 5 files changed, 361 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2b659fc..c8df9c8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -181,6 +181,7 @@ src/addins/addins.mk
 src/addins/backlinks/Makefile
 src/addins/bugzilla/Makefile
 src/addins/exporttohtml/Makefile
+src/addins/filesystemsyncservice/Makefile
 src/addins/fixedwidth/Makefile
 src/addins/inserttimestamp/Makefile
 src/addins/noteoftheday/Makefile
diff --git a/src/addins/Makefile.am b/src/addins/Makefile.am
index dfa933c..125fbba 100644
--- a/src/addins/Makefile.am
+++ b/src/addins/Makefile.am
@@ -4,6 +4,7 @@
 SUBDIRS = backlinks \
 	bugzilla \
 	exporttohtml \
+	filesystemsyncservice \
 	fixedwidth \
 	inserttimestamp \
 	noteoftheday \
diff --git a/src/addins/filesystemsyncservice/Makefile.am b/src/addins/filesystemsyncservice/Makefile.am
new file mode 100644
index 0000000..69c0452
--- /dev/null
+++ b/src/addins/filesystemsyncservice/Makefile.am
@@ -0,0 +1,9 @@
+
+include $(builddir)/../addins.mk
+
+addinsdir = $(ADDINSDIR)
+addins_LTLIBRARIES = filesystemsyncservice.la
+
+
+filesystemsyncservice_la_SOURCES = filesystemsyncserviceaddin.hpp filesystemsyncserviceaddin.cpp \
+	$(NULL)
diff --git a/src/addins/filesystemsyncservice/filesystemsyncserviceaddin.cpp b/src/addins/filesystemsyncservice/filesystemsyncserviceaddin.cpp
new file mode 100644
index 0000000..4370caf
--- /dev/null
+++ b/src/addins/filesystemsyncservice/filesystemsyncserviceaddin.cpp
@@ -0,0 +1,267 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2012 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 <fstream>
+#include <stdexcept>
+
+#include <boost/lexical_cast.hpp>
+#include <glibmm/i18n.h>
+#include <gtkmm/label.h>
+#include <gtkmm/table.h>
+
+#include "debug.hpp"
+#include "filesystemsyncserviceaddin.hpp"
+#include "preferences.hpp"
+#include "sharp/directory.hpp"
+#include "sharp/files.hpp"
+#include "synchronization/filesystemsyncserver.hpp"
+
+
+namespace filesystemsyncservice {
+
+FileSystemSyncServiceModule::FileSystemSyncServiceModule()
+{
+  ADD_INTERFACE_IMPL(FileSystemSyncServiceAddin);
+}
+const char * FileSystemSyncServiceModule::id() const
+{
+  return "FileSystemSyncServiceAddin";
+}
+const char * FileSystemSyncServiceModule::name() const
+{
+  return _("Local Directory Sync Service Add-in");
+}
+const char * FileSystemSyncServiceModule::description() const
+{
+  return _("Synchronize Gnote Notes to a local file system path");
+}
+const char * FileSystemSyncServiceModule::authors() const
+{
+  return _("Aurimas Cernius and the Tomboy Project");
+}
+int FileSystemSyncServiceModule::category() const
+{
+  return gnote::ADDIN_CATEGORY_SYNCHRONIZATION;
+}
+const char * FileSystemSyncServiceModule::version() const
+{
+  return "0.1";
+}
+
+
+
+
+FileSystemSyncServiceAddin::FileSystemSyncServiceAddin()
+  : m_initialized(false)
+{
+}
+
+void FileSystemSyncServiceAddin::initialize()
+{
+  m_initialized = true;
+}
+
+void FileSystemSyncServiceAddin::shutdown()
+{
+}
+
+gnote::sync::SyncServer::Ptr FileSystemSyncServiceAddin::create_sync_server()
+{
+  gnote::sync::SyncServer::Ptr server;
+
+  std::string syncPath;
+  if(get_config_settings(syncPath)) {
+    m_path = syncPath;
+    if(sharp::directory_exists(m_path) == false) {
+      sharp::directory_create(m_path);
+    }
+
+    server = gnote::sync::FileSystemSyncServer::create(m_path);
+  }
+  else {
+    throw std::logic_error("FileSystemSyncServiceAddin.create_sync_server() called without being configured");
+  }
+
+  return server;
+}
+
+
+void FileSystemSyncServiceAddin::post_sync_cleanup()
+{
+  // Nothing to do
+}
+
+
+Gtk::Widget *FileSystemSyncServiceAddin::create_preferences_control(EventHandler requiredPrefChanged)
+{
+  Gtk::Table *table = new Gtk::Table(1, 2, false);
+  table->set_row_spacings(5);
+  table->set_col_spacings(10);
+
+  // Read settings out of gconf
+  std::string syncPath;
+  if(get_config_settings(syncPath) == false) {
+    syncPath = "";
+  }
+
+  Gtk::Label *l = new Gtk::Label(_("_Folder Path:"), true);
+  l->property_xalign() = 1;
+  table->attach(*l, 0, 1, 0, 1,
+                Gtk::FILL,
+                Gtk::EXPAND | Gtk::FILL,
+                0, 0);
+
+  m_path_button = new Gtk::FileChooserButton(_("Select Synchronization Folder..."),
+    Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
+  m_path_button->signal_current_folder_changed().connect(requiredPrefChanged);
+  l->set_mnemonic_widget(*m_path_button);
+  m_path_button->set_filename(syncPath);
+
+  table->attach(*m_path_button, 1, 2, 0, 1,
+                Gtk::EXPAND | Gtk::FILL,
+                Gtk::EXPAND | Gtk::FILL,
+                0, 0);
+
+  table->show_all();
+  return table;
+}
+
+
+bool FileSystemSyncServiceAddin::save_configuration()
+{
+  std::string syncPath = m_path_button->get_filename();
+
+  if(syncPath == "") {
+    // TODO: Figure out a way to send the error back to the client
+    DBG_OUT("The path is empty");
+    throw gnote::sync::GnoteSyncException(_("Folder path field is empty."));
+  }
+
+  // Attempt to create the path and fail if we can't
+  if(sharp::directory_exists(syncPath) == false) {
+    if(!sharp::directory_create(syncPath)) {
+      DBG_OUT("Could not create \"%s\"", syncPath.c_str());
+      throw gnote::sync::GnoteSyncException(_("Specified folder path does not exist, and Gnote was unable to create it."));
+    }
+  }
+  else {
+    // Test creating/writing/deleting a file
+    // FIXME: Should throw gnote::sync::GnoteSyncException once string changes are OK again
+    std::string testPathBase = Glib::build_filename(syncPath, "test");
+    std::string testPath = testPathBase;
+    int count = 0;
+
+    // Get unique new file name
+    while(sharp::file_exists(testPath)) {
+      testPath = testPathBase + boost::lexical_cast<std::string>(++count);
+    }
+
+    // Test ability to create and write
+    std::string testLine = "Testing write capabilities.";
+    std::ofstream fout(testPath.c_str());
+    if(fout.is_open()) {
+      fout << testLine;
+      fout.close();
+    }
+
+    // Test ability to read
+    bool testFileFound = false;
+    std::list<std::string> files;
+    sharp::directory_get_files(syncPath, files);
+    for(std::list<std::string>::iterator iter = files.begin(); iter != files.end(); ++iter) {
+      if(*iter == testPath) {
+        testFileFound = true;
+        break;
+      }
+    }
+    if(!testFileFound) {
+      ; // TODO: Throw gnote::sync::GnoteSyncException
+    }
+    std::ifstream fin(testPath.c_str());
+    if(fin.is_open()) {
+      std::string line;
+      std::getline(fin, line);
+      fin.close();
+      if(line != testLine) {
+        ; // TODO: Throw gnote::sync::GnoteSyncException
+      }
+    }
+
+    // Test ability to delete
+    sharp::file_delete(testPath);
+  }
+
+  m_path = syncPath;
+
+  // TODO: Try to create and delete a file.  If it fails, this should fail
+  gnote::Preferences::obj().get_schema_settings(
+    gnote::Preferences::SCHEMA_SYNC)->set_string(gnote::Preferences::SYNC_LOCAL_PATH, m_path);
+
+  return true;
+}
+
+
+void FileSystemSyncServiceAddin::reset_configuration()
+{
+  gnote::Preferences::obj().get_schema_settings(
+    gnote::Preferences::SCHEMA_SYNC)->set_string(gnote::Preferences::SYNC_LOCAL_PATH, "");
+}
+
+
+bool FileSystemSyncServiceAddin::is_configured()
+{
+  return gnote::Preferences::obj().get_schema_settings(
+    gnote::Preferences::SCHEMA_SYNC)->get_string(gnote::Preferences::SYNC_LOCAL_PATH) != "";
+}
+
+
+std::string FileSystemSyncServiceAddin::name()
+{
+  return _("Local Folder");
+}
+
+
+std::string FileSystemSyncServiceAddin::id()
+{
+  return "local";
+}
+
+
+bool FileSystemSyncServiceAddin::is_supported()
+{
+  return true;
+}
+
+
+bool FileSystemSyncServiceAddin::initialized()
+{
+  return m_initialized;
+}
+
+
+bool FileSystemSyncServiceAddin::get_config_settings(std::string & syncPath)
+{
+  syncPath = gnote::Preferences::obj().get_schema_settings(
+    gnote::Preferences::SCHEMA_SYNC)->get_string(gnote::Preferences::SYNC_LOCAL_PATH);
+
+  return syncPath != "";
+}
+
+}
diff --git a/src/addins/filesystemsyncservice/filesystemsyncserviceaddin.hpp b/src/addins/filesystemsyncservice/filesystemsyncserviceaddin.hpp
new file mode 100644
index 0000000..99f101f
--- /dev/null
+++ b/src/addins/filesystemsyncservice/filesystemsyncserviceaddin.hpp
@@ -0,0 +1,83 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2012 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 _FILESYSTEM_SYNC_SERVICE_ADDIN_
+#define _FILESYSTEM_SYNC_SERVICE_ADDIN_
+
+#include <gtkmm/filechooserbutton.h>
+
+#include "sharp/dynamicmodule.hpp"
+#include "synchronization/syncserviceaddin.hpp"
+
+
+
+namespace filesystemsyncservice {
+
+class FileSystemSyncServiceModule
+  : public sharp::DynamicModule
+{
+public:
+  FileSystemSyncServiceModule();
+  virtual const char * id() const;
+  virtual const char * name() const;
+  virtual const char * description() const;
+  virtual const char * authors() const;
+  virtual int          category() const;
+  virtual const char * version() const;
+};
+
+
+DECLARE_MODULE(FileSystemSyncServiceModule)
+
+
+class FileSystemSyncServiceAddin
+  : public gnote::sync::SyncServiceAddin
+{
+public:
+  static FileSystemSyncServiceAddin *create()
+    {
+      return new FileSystemSyncServiceAddin;
+    }
+  FileSystemSyncServiceAddin();
+
+  virtual void initialize();
+  virtual void shutdown();
+
+  virtual gnote::sync::SyncServer::Ptr create_sync_server();
+  virtual void post_sync_cleanup();
+  virtual Gtk::Widget *create_preferences_control(EventHandler requiredPrefChanged);
+  virtual bool save_configuration();
+  virtual void reset_configuration();
+  virtual bool is_configured();
+  virtual std::string name();
+  virtual std::string id();
+  virtual bool is_supported();
+  virtual bool initialized();
+private:
+  bool get_config_settings(std::string & syncPath);
+
+  Gtk::FileChooserButton *m_path_button;
+  std::string m_path;
+  bool m_initialized;
+};
+
+}
+
+#endif



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