[gnote] Refactor directory testing to a separate method



commit 26cbb755ea344b974596d6a700afece1bca71012
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Sep 14 12:35:37 2019 +0300

    Refactor directory testing to a separate method

 .../gvfssyncservice/gvfssyncserviceaddin.cpp       | 99 ++++++++++++++--------
 .../gvfssyncservice/gvfssyncserviceaddin.hpp       |  1 +
 2 files changed, 65 insertions(+), 35 deletions(-)
---
diff --git a/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp 
b/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp
index 1ad049c4..7aac576d 100644
--- a/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp
+++ b/src/addins/gvfssyncservice/gvfssyncserviceaddin.cpp
@@ -240,41 +240,9 @@ bool GvfsSyncServiceAddin::save_configuration(const sigc::slot<void, bool, Glib:
       auto path = Gio::File::create_for_uri(sync_uri);
       if(!mount(path))
         throw gnote::sync::GnoteSyncException(_("Could not mount the path: %s. Please, check your 
settings"));
-      if(sharp::directory_exists(path) == false) {
-        if(!sharp::directory_create(path)) {
-          DBG_OUT("Could not create \"%s\"", sync_uri.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
-        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()) {
-          throw gnote::sync::GnoteSyncException("Failure writing test file");
-        }
-        Glib::ustring line = sharp::file_read_all_text(test_path);
-        if(line != test_line) {
-          throw gnote::sync::GnoteSyncException("Failure when checking test file contents");
-        }
-
-        // Test ability to delete
-        if(!test_path->remove()) {
-          throw gnote::sync::GnoteSyncException("Failure when trying to remove test file");
-        }
+      Glib::ustring error;
+      if(!test_sync_directory(path, sync_uri, error)) {
+        throw gnote::sync::GnoteSyncException(error.c_str());
       }
 
       unmount();
@@ -300,6 +268,67 @@ 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()
 {
   gnote::Preferences::obj().get_schema_settings(
diff --git a/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp 
b/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp
index 4d6b810b..937e0658 100644
--- a/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp
+++ b/src/addins/gvfssyncservice/gvfssyncserviceaddin.hpp
@@ -68,6 +68,7 @@ private:
   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::ustring m_uri;
   Gtk::Entry *m_uri_entry;


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