[gnote] Make SyncManager non-singleton



commit da2c37b8e75b00b326ff89654cfce188feedda73
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Mon Oct 21 20:08:12 2019 +0300

    Make SyncManager non-singleton

 src/gnote.cpp                        |  9 +++++++--
 src/gnote.hpp                        |  6 ++++++
 src/ignote.hpp                       |  4 ++++
 src/preferencesdialog.cpp            |  4 ++--
 src/synchronization/isyncmanager.hpp |  1 -
 src/synchronization/silentui.cpp     |  3 ++-
 src/synchronization/syncdialog.cpp   | 10 +++++-----
 src/synchronization/syncmanager.cpp  | 22 ++++++++--------------
 src/synchronization/syncmanager.hpp  |  7 +------
 9 files changed, 35 insertions(+), 31 deletions(-)
---
diff --git a/src/gnote.cpp b/src/gnote.cpp
index 8d88f044..da72dd34 100644
--- a/src/gnote.cpp
+++ b/src/gnote.cpp
@@ -52,7 +52,6 @@
 #include "sharp/streamreader.hpp"
 #include "sharp/files.hpp"
 #include "notebooks/notebookmanager.hpp"
-#include "synchronization/syncmanager.hpp"
 
 
 namespace gnote {
@@ -60,6 +59,8 @@ namespace gnote {
   Gnote::Gnote()
     : Gtk::Application("org.gnome.Gnote", Gio::APPLICATION_HANDLES_COMMAND_LINE)
     , m_manager(NULL)
+    , m_notebook_manager(NULL)
+    , m_sync_manager(NULL)
     , m_is_background(false)
     , m_is_shell_search(false)
     , m_prefsdlg(NULL)
@@ -74,6 +75,9 @@ namespace gnote {
     if(m_notebook_manager) {
       delete m_notebook_manager;
     }
+    if(m_sync_manager) {
+      delete m_sync_manager;
+    }
     delete m_manager;
   }
 
@@ -148,7 +152,8 @@ namespace gnote {
     m_manager->init(note_path);
     m_notebook_manager = new notebooks::NotebookManager(default_note_manager());
     m_action_manager.init();
-    sync::SyncManager::init(default_note_manager());
+    m_sync_manager = new sync::SyncManager(default_note_manager());
+    m_sync_manager->init();
 
     m_manager->get_addin_manager().initialize_application_addins();
   }
diff --git a/src/gnote.hpp b/src/gnote.hpp
index 4575390d..b930cd67 100644
--- a/src/gnote.hpp
+++ b/src/gnote.hpp
@@ -35,6 +35,7 @@
 #include "preferences.hpp"
 #include "remotecontrolproxy.hpp"
 #include "synchronization/syncdialog.hpp"
+#include "synchronization/syncmanager.hpp"
 
 namespace gnote {
 
@@ -133,6 +134,10 @@ public:
     {
       return *m_notebook_manager;
     }
+  virtual sync::SyncManager & sync_manager() override
+    {
+      return *m_sync_manager;
+    }
   virtual Preferences & preferences()
     {
       return m_preferences;
@@ -181,6 +186,7 @@ private:
   notebooks::NotebookManager *m_notebook_manager;
   Preferences m_preferences;
   ActionManager m_action_manager;
+  sync::SyncManager *m_sync_manager;
   Glib::RefPtr<Gtk::IconTheme> m_icon_theme;
   bool m_is_background;
   bool m_is_shell_search;
diff --git a/src/ignote.hpp b/src/ignote.hpp
index 7c602936..f9e804c3 100644
--- a/src/ignote.hpp
+++ b/src/ignote.hpp
@@ -31,6 +31,9 @@ class Preferences;
 namespace notebooks {
   class NotebookManager;
 }
+namespace sync {
+  class ISyncManager;
+}
 
 
 class IGnote
@@ -47,6 +50,7 @@ public:
   virtual IActionManager & action_manager() = 0;
   virtual IconManager & icon_manager() = 0;
   virtual notebooks::NotebookManager & notebook_manager() = 0;
+  virtual sync::ISyncManager & sync_manager() = 0;
   virtual Preferences & preferences() = 0;
 
   virtual MainWindow & get_main_window() = 0;
diff --git a/src/preferencesdialog.cpp b/src/preferencesdialog.cpp
index 696618aa..0fa77520 100644
--- a/src/preferencesdialog.cpp
+++ b/src/preferencesdialog.cpp
@@ -1173,7 +1173,7 @@ namespace gnote {
     // Reset conflict handling behavior
     settings->set_int(Preferences::SYNC_CONFIGURED_CONFLICT_BEHAVIOR, 
DEFAULT_SYNC_CONFIGURED_CONFLICT_BEHAVIOR);
 
-    sync::ISyncManager::obj().reset_client();
+    IGnote::obj().sync_manager().reset_client();
 
     m_sync_addin_combo->set_sensitive(true);
     m_sync_addin_combo->unset_active();
@@ -1224,7 +1224,7 @@ namespace gnote {
       m_reset_sync_addin_button->set_sensitive(true);
       m_save_sync_addin_button->set_sensitive(false);
 
-      sync::ISyncManager::obj().reset_client();
+      IGnote::obj().sync_manager().reset_client();
 
       // Give the user a visual letting them know that connecting
       // was successful.
diff --git a/src/synchronization/isyncmanager.hpp b/src/synchronization/isyncmanager.hpp
index 90d0ecda..1a6eee70 100644
--- a/src/synchronization/isyncmanager.hpp
+++ b/src/synchronization/isyncmanager.hpp
@@ -62,7 +62,6 @@ public:
 };
 
 class ISyncManager
-  : public base::Singleton<ISyncManager>
 {
 public:
   virtual ~ISyncManager();
diff --git a/src/synchronization/silentui.cpp b/src/synchronization/silentui.cpp
index e0330741..ba3dada7 100644
--- a/src/synchronization/silentui.cpp
+++ b/src/synchronization/silentui.cpp
@@ -19,6 +19,7 @@
 
 
 #include "debug.hpp"
+#include "ignote.hpp"
 #include "isyncmanager.hpp"
 #include "silentui.hpp"
 
@@ -83,7 +84,7 @@ namespace sync {
     if(localConflictNote->id() != remoteNote.m_uuid) {
       m_manager.delete_note(localConflictNote);
     }
-    ISyncManager::obj().resolve_conflict(OVERWRITE_EXISTING);
+    IGnote::obj().sync_manager().resolve_conflict(OVERWRITE_EXISTING);
   }
 
 
diff --git a/src/synchronization/syncdialog.cpp b/src/synchronization/syncdialog.cpp
index 978769ed..cceeca7e 100644
--- a/src/synchronization/syncdialog.cpp
+++ b/src/synchronization/syncdialog.cpp
@@ -367,7 +367,7 @@ void SyncDialog::on_realize()
 {
   Gtk::Dialog::on_realize();
 
-  SyncState state = ISyncManager::obj().state();
+  SyncState state = IGnote::obj().sync_manager().state();
   if(state == IDLE) {
     // Kick off a timer to keep the progress bar going
     //m_progress_barTimeoutId = GLib.Timeout.Add (500, OnPulseProgressBar);
@@ -376,7 +376,7 @@ void SyncDialog::on_realize()
     timeout->attach();
 
     // Kick off a new synchronization
-    ISyncManager::obj().perform_synchronization(this->shared_from_this());
+    IGnote::obj().sync_manager().perform_synchronization(this->shared_from_this());
   }
   else {
     // Adjust the GUI accordingly
@@ -387,7 +387,7 @@ void SyncDialog::on_realize()
 
 bool SyncDialog::on_pulse_progress_bar()
 {
-  if(ISyncManager::obj().state() == IDLE) {
+  if(IGnote::obj().sync_manager().state() == IDLE) {
     return false;
   }
 
@@ -632,7 +632,7 @@ void SyncDialog::note_conflict_detected_(
     SyncTitleConflictDialog conflictDlg(localConflictNote, noteUpdateTitles);
     Gtk::ResponseType reponse = Gtk::RESPONSE_OK;
 
-    bool noteSyncBitsMatch = ISyncManager::obj().synchronized_note_xml_matches(
+    bool noteSyncBitsMatch = IGnote::obj().sync_manager().synchronized_note_xml_matches(
       localConflictNote->get_complete_note_xml(), remoteNote.m_xml_content);
 
     // If the synchronized note content is in conflict
@@ -689,7 +689,7 @@ void SyncDialog::note_conflict_detected_(
     conflictDlg.hide();
 
     // Let the SyncManager continue
-    ISyncManager::obj().resolve_conflict(/*localConflictNote, */resolution);
+    IGnote::obj().sync_manager().resolve_conflict(/*localConflictNote, */resolution);
   }
   catch(std::exception & e) {
     *mainThreadException = new std::exception(e);
diff --git a/src/synchronization/syncmanager.cpp b/src/synchronization/syncmanager.cpp
index d1ed77b2..82ed1605 100644
--- a/src/synchronization/syncmanager.cpp
+++ b/src/synchronization/syncmanager.cpp
@@ -49,23 +49,16 @@ namespace sync {
   }
 
 
-  void SyncManager::init(NoteManagerBase & m)
+  void SyncManager::init()
   {
-    SyncManager *manager = new SyncManager(m);
-    manager->_init(m);
-  }
-
-
-  void SyncManager::_init(NoteManagerBase & manager)
-  {
-    m_client = GnoteSyncClient::create(manager);
+    m_client = GnoteSyncClient::create(m_note_manager);
     // Add a "Synchronize Notes" to Gnote's Application Menu
     IActionManager & am(IGnote::obj().action_manager());
     am.add_app_action("sync-notes");
     am.add_app_menu_item(APP_SECTION_MANAGE, 200, _("Synchronize Notes"), "app.sync-notes");
 
     // Initialize all the SyncServiceAddins
-    initialize_sync_service_addins(manager);
+    initialize_sync_service_addins(m_note_manager);
 
     connect_system_signals();
 
@@ -133,11 +126,12 @@ namespace sync {
   void SyncManager::synchronization_thread()
   {
     struct finally {
+      SyncManager & manager;
       SyncServiceAddin *addin;
-      finally() : addin(NULL){}
+      finally(SyncManager & m) : manager(m), addin(NULL){}
       ~finally()
       {
-        SyncManager::_obj().m_sync_thread = NULL;
+        manager.m_sync_thread = NULL;
         try {
           if(addin) {
             addin->post_sync_cleanup();
@@ -147,7 +141,7 @@ namespace sync {
           ERR_OUT(_("Error cleaning up addin after synchronization: %s"), e.what());
         }
       }
-    } f;
+    } f(*this);
     SyncServer::Ptr server;
     try {
       f.addin = get_configured_sync_service();
@@ -694,7 +688,7 @@ namespace sync {
       // Delete notes locally that have been deleted on the server
       for(const NoteBase::Ptr & iter : localNotes) {
         Note::Ptr note = std::static_pointer_cast<Note>(iter);
-       if(SyncManager::_obj().m_client->get_revision(note) != -1
+       if(m_client->get_revision(note) != -1
           && std::find(serverNotes.begin(), serverNotes.end(), note->id()) == serverNotes.end()) {
          if(m_sync_ui != 0) {
            m_sync_ui->note_synchronized(note->get_title(), DELETE_FROM_CLIENT);
diff --git a/src/synchronization/syncmanager.hpp b/src/synchronization/syncmanager.hpp
index 9cdce2dd..1368bd6e 100644
--- a/src/synchronization/syncmanager.hpp
+++ b/src/synchronization/syncmanager.hpp
@@ -40,7 +40,7 @@ namespace sync {
   {
   public:
     SyncManager(NoteManagerBase &);
-    static void init(NoteManagerBase &);
+    void init();
     virtual void reset_client() override;
     virtual void perform_synchronization(const SyncUI::Ptr & sync_ui) override;
     void synchronization_thread();
@@ -68,11 +68,6 @@ namespace sync {
     SyncClient::Ptr m_client;
     SyncUI::Ptr m_sync_ui;
   private:
-    static SyncManager & _obj()
-      {
-        return static_cast<SyncManager&>(obj());
-      }
-    void _init(NoteManagerBase &);
     void handle_note_saved_or_deleted(const NoteBase::Ptr & note);
     void handle_note_buffer_changed(const NoteBase::Ptr & note);
     void preferences_setting_changed(const Glib::ustring & key);


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