[gnote] Refactor gvfs sync plugin to use new base gvfs sync



commit b3ab03f7d4215526b0598a01b04e59c66e924d63
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Feb 6 13:51:02 2021 +0200

    Refactor gvfs sync plugin to use new base gvfs sync

 .../gvfssyncservice/gvfssyncserviceaddin.cpp       | 208 ++-------------------
 .../gvfssyncservice/gvfssyncserviceaddin.hpp       |  20 +-
 2 files changed, 24 insertions(+), 204 deletions(-)
---
diff --git a/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp 
b/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp
index f5a6fe51..c7c542ef 100644
--- a/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp
+++ b/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2019,2020 Aurimas Cernius
+ * Copyright (C) 2019-2021 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
@@ -19,17 +19,15 @@
 
 
 #include <glibmm/i18n.h>
-#include <glibmm/miscutils.h>
+#include <glibmm/thread.h>
 #include <gtkmm/entry.h>
 #include <gtkmm/label.h>
 #include <gtkmm/table.h>
-#include <glibmm/thread.h>
 
 #include "debug.hpp"
 #include "gvfssyncserviceaddin.hpp"
 #include "ignote.hpp"
 #include "sharp/directory.hpp"
-#include "sharp/files.hpp"
 #include "synchronization/filesystemsyncserver.hpp"
 
 
@@ -49,25 +47,17 @@ GvfsSyncServiceModule::GvfsSyncServiceModule()
 
 GvfsSyncServiceAddin::GvfsSyncServiceAddin()
   : m_uri_entry(nullptr)
-  , m_initialized(false)
-  , m_enabled(false)
 {
 }
 
 void GvfsSyncServiceAddin::initialize()
 {
-  m_initialized = true;
-  m_enabled = true;
+  GvfsSyncService::initialize();
   if(!m_gvfs_settings) {
     m_gvfs_settings = Gio::Settings::create(SCHEMA_SYNC_GVFS);
   }
 }
 
-void GvfsSyncServiceAddin::shutdown()
-{
-  m_enabled = false;
-}
-
 gnote::sync::SyncServer *GvfsSyncServiceAddin::create_sync_server()
 {
   gnote::sync::SyncServer *server;
@@ -80,7 +70,8 @@ gnote::sync::SyncServer *GvfsSyncServiceAddin::create_sync_server()
     }
 
     auto path = Gio::File::create_for_uri(m_uri);
-    if(!mount(path)) {
+    auto root = get_root_dir(path);
+    if(!mount_sync(root)) {
       throw sharp::Exception(_("Failed to mount the folder"));
     }
     if(!path->query_exists())
@@ -96,107 +87,6 @@ gnote::sync::SyncServer *GvfsSyncServiceAddin::create_sync_server()
 }
 
 
-bool GvfsSyncServiceAddin::mount(const Glib::RefPtr<Gio::File> & path)
-{
-  bool ret = true, done = false;
-  Glib::Mutex mutex;
-  Glib::Cond cond;
-  mutex.lock();
-  if(mount_async(path, [&ret, &mutex, &cond, &done](bool result, const Glib::ustring&) {
-       mutex.lock();
-       ret = result;
-       done = true;
-       cond.signal();
-       mutex.unlock();
-     })) {
-    mutex.unlock();
-    return true;
-  }
-
-  while(!done) {
-    cond.wait(mutex);
-  }
-  mutex.unlock();
-  return ret;
-}
-
-bool GvfsSyncServiceAddin::mount_async(const Glib::RefPtr<Gio::File> & path, const sigc::slot<void, bool, 
Glib::ustring> & completed)
-{
-  try {
-    path->find_enclosing_mount();
-    return true;
-  }
-  catch(Gio::Error & e) {
-  }
-
-  auto root = path;
-  auto parent = root->get_parent();
-  while(parent) {
-    root = parent;
-    parent = root->get_parent();
-  }
-
-  root->mount_enclosing_volume([this, root, completed](Glib::RefPtr<Gio::AsyncResult> & result) {
-    try {
-      if(root->mount_enclosing_volume_finish(result)) {
-        m_mount = root->find_enclosing_mount();
-      }
-    }
-    catch(...) {
-    }
-
-    completed(bool(m_mount), "");
-  });
-
-  return false;
-}
-
-
-void GvfsSyncServiceAddin::unmount()
-{
-  if(!m_mount) {
-    return;
-  }
-
-  Glib::Mutex mutex;
-  Glib::Cond cond;
-  mutex.lock();
-  unmount_async([&mutex, &cond]{
-    mutex.lock();
-    cond.signal();
-    mutex.unlock();
-  });
-  cond.wait(mutex);
-  mutex.unlock();
-}
-
-
-void GvfsSyncServiceAddin::unmount_async(const sigc::slot<void> & completed)
-{
-  if(!m_mount) {
-    completed();
-    return;
-  }
-
-  m_mount->unmount([this, completed](Glib::RefPtr<Gio::AsyncResult> & result) {
-    try {
-      m_mount->unmount_finish(result);
-    }
-    catch(...) {
-    }
-
-    m_mount.reset();
-    completed();
-  });
-}
-
-
-void GvfsSyncServiceAddin::post_sync_cleanup()
-{
-  unmount();
-}
-
-
 Gtk::Widget *GvfsSyncServiceAddin::create_preferences_control(EventHandler required_pref_changed)
 {
   Gtk::Table *table = manage(new Gtk::Table(1, 3, false));
@@ -245,6 +135,7 @@ bool GvfsSyncServiceAddin::save_configuration(const sigc::slot<void, bool, Glib:
   }
 
   auto path = Gio::File::create_for_uri(sync_uri);
+  auto root = get_root_dir(path);
   auto on_mount_completed = [this, path, sync_uri, on_saved](bool success, Glib::ustring error) {
       if(success) {
         success = test_sync_directory(path, sync_uri, error);
@@ -257,8 +148,8 @@ bool GvfsSyncServiceAddin::save_configuration(const sigc::slot<void, bool, Glib:
         on_saved(success, error);
       });
   };
-  if(mount_async(path, on_mount_completed)) {
-    Glib::Thread::create([this, sync_uri, on_mount_completed]() {
+  if(mount_async(root, on_mount_completed)) {
+    Glib::Thread::create([this, on_mount_completed]() {
       on_mount_completed(true, "");
     }, false);
   }
@@ -267,67 +158,6 @@ bool GvfsSyncServiceAddin::save_configuration(const sigc::slot<void, bool, Glib:
 }
 
 
-bool GvfsSyncServiceAddin::test_sync_directory(const Glib::RefPtr<Gio::File> & path, const Glib::ustring & 
sync_uri, Glib::ustring & error)
-{
-  try {
-    if(sharp::directory_exists(path) == false) {
-      if(!sharp::directory_create(path)) {
-        DBG_OUT("Could not create \"%s\"", sync_uri.c_str());
-        error = _("Specified folder path does not exist, and Gnote was unable to create it.");
-        return false;
-      }
-    }
-    else {
-      // Test creating/writing/deleting a file
-      Glib::ustring test_path_base = Glib::build_filename(sync_uri, "test");
-      Glib::RefPtr<Gio::File> test_path = Gio::File::create_for_uri(test_path_base);
-      int count = 0;
-
-      // Get unique new file name
-      while(test_path->query_exists()) {
-        test_path = Gio::File::create_for_uri(test_path_base + TO_STRING(++count));
-      }
-
-      // Test ability to create and write
-      Glib::ustring test_line = "Testing write capabilities.";
-      auto stream = test_path->create_file();
-      stream->write(test_line);
-      stream->close();
-
-      if(!test_path->query_exists()) {
-        error = _("Failure writing test file");
-        return false;
-      }
-      Glib::ustring line = sharp::file_read_all_text(test_path);
-      if(line != test_line) {
-        error = _("Failure when checking test file contents");
-        return false;
-      }
-
-      // Test ability to delete
-      if(!test_path->remove()) {
-        error = _("Failure when trying to remove test file");
-        return false;
-      }
-    }
-
-    return true;
-  }
-  catch(Glib::Exception & e) {
-    error = e.what();
-    return false;
-  }
-  catch(std::exception & e) {
-    error = e.what();
-    return false;
-  }
-  catch(...) {
-    error = _("Unknown error");
-    return false;
-  }
-}
-
-
 void GvfsSyncServiceAddin::reset_configuration()
 {
   m_gvfs_settings->set_string(SYNC_GVFS_URI, "");
@@ -353,23 +183,25 @@ Glib::ustring GvfsSyncServiceAddin::id()
 }
 
 
-bool GvfsSyncServiceAddin::is_supported()
+bool GvfsSyncServiceAddin::get_config_settings(Glib::ustring & sync_path)
 {
-  return true;
-}
-
+  sync_path = m_gvfs_settings->get_string(SYNC_GVFS_URI);
 
-bool GvfsSyncServiceAddin::initialized()
-{
-  return m_initialized && m_enabled;
+  return sync_path != "";
 }
 
 
-bool GvfsSyncServiceAddin::get_config_settings(Glib::ustring & sync_path)
+Glib::RefPtr<Gio::File> GvfsSyncServiceAddin::get_root_dir(const Glib::RefPtr<Gio::File> & uri)
 {
-  sync_path = m_gvfs_settings->get_string(SYNC_GVFS_URI);
+  auto root = uri;
+  auto parent = root->get_parent();
+  while(parent) {
+    root = parent;
+    parent = root->get_parent();
+  }
 
-  return sync_path != "";
+  return root;
 }
 
+
 }
diff --git a/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp 
b/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp
index 84cde3b6..e9895260 100644
--- a/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp
+++ b/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2019-2020 Aurimas Cernius
+ * Copyright (C) 2019-2021 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,7 +24,7 @@
 #include <giomm/settings.h>
 
 #include "sharp/dynamicmodule.hpp"
-#include "synchronization/syncserviceaddin.hpp"
+#include "synchronization/gvfssyncservice.hpp"
 
 
 
@@ -42,7 +42,7 @@ DECLARE_MODULE(GvfsSyncServiceModule)
 
 
 class GvfsSyncServiceAddin
-  : public gnote::sync::SyncServiceAddin
+  : public gnote::sync::GvfsSyncService
 {
 public:
   static GvfsSyncServiceAddin *create()
@@ -52,32 +52,20 @@ public:
   GvfsSyncServiceAddin();
 
   virtual void initialize() override;
-  virtual void shutdown() override;
 
   virtual gnote::sync::SyncServer *create_sync_server() override;
-  virtual void post_sync_cleanup() override;
   virtual Gtk::Widget *create_preferences_control(EventHandler requiredPrefChanged) override;
   virtual bool save_configuration(const sigc::slot<void, bool, Glib::ustring> & on_saved) override;
   virtual void reset_configuration() override;
   virtual bool is_configured() override;
   virtual Glib::ustring name() override;
   virtual Glib::ustring id() override;
-  virtual bool is_supported() override;
-  virtual bool initialized() override;
 private:
+  static Glib::RefPtr<Gio::File> get_root_dir(const Glib::RefPtr<Gio::File> &);
   bool get_config_settings(Glib::ustring & sync_path);
-  bool mount(const Glib::RefPtr<Gio::File> & path);
-  bool mount_async(const Glib::RefPtr<Gio::File> & path, const sigc::slot<void, bool, Glib::ustring> & 
completed);
-  void unmount();
-  void unmount_async(const sigc::slot<void> & completed);
-  bool test_sync_directory(const Glib::RefPtr<Gio::File> & path, const Glib::ustring & sync_uri, 
Glib::ustring & error);
 
   Glib::RefPtr<Gio::Settings> m_gvfs_settings;
-  Glib::ustring m_uri;
   Gtk::Entry *m_uri_entry;
-  bool m_initialized;
-  bool m_enabled;
-  Glib::RefPtr<Gio::Mount> m_mount;
 };
 
 }


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