From 581b9705bd0ee79e6bc12dffca6394d60a9738fc Mon Sep 17 00:00:00 2001
From: David Fairbrother <DavidFair@users.noreply.github.com>
Date: Wed, 27 Jun 2018 17:00:30 +0100
Subject: [PATCH] Re #0 Move project recovery thread to project recovery

---
 MantidPlot/CMakeLists.txt                     |  4 +--
 MantidPlot/src/ApplicationWindow.cpp          |  7 ++--
 MantidPlot/src/ApplicationWindow.h            |  4 +--
 ...RecoveryThread.cpp => ProjectRecovery.cpp} | 36 +++++++++----------
 ...jectRecoveryThread.h => ProjectRecovery.h} | 17 ++++-----
 5 files changed, 31 insertions(+), 37 deletions(-)
 rename MantidPlot/src/{ProjectRecoveryThread.cpp => ProjectRecovery.cpp} (90%)
 rename MantidPlot/src/{ProjectRecoveryThread.h => ProjectRecovery.h} (88%)

diff --git a/MantidPlot/CMakeLists.txt b/MantidPlot/CMakeLists.txt
index 9257b87ab9f..8d50becc6f5 100644
--- a/MantidPlot/CMakeLists.txt
+++ b/MantidPlot/CMakeLists.txt
@@ -91,7 +91,7 @@ set ( QTIPLOT_SRCS src/ApplicationWindow.cpp
                    src/PluginFit.cpp
                    src/PolynomFitDialog.cpp
                    src/PolynomialFit.cpp
-                   src/ProjectRecoveryThread.cpp
+                   src/ProjectRecovery.cpp
                    src/ProjectSaveView.cpp
                    src/ProjectSerialiser.cpp
                    src/PythonScript.cpp
@@ -291,7 +291,7 @@ set ( QTIPLOT_HDRS src/ApplicationWindow.h
                    src/PluginFit.h
                    src/PolynomFitDialog.h
                    src/PolynomialFit.h
-                   src/ProjectRecoveryThread.h
+                   src/ProjectRecovery.h
                    src/ProjectSerialiser.h
                    src/ProjectSaveView.h
                    src/PythonScript.h
diff --git a/MantidPlot/src/ApplicationWindow.cpp b/MantidPlot/src/ApplicationWindow.cpp
index b283f33e35f..f627c33df50 100644
--- a/MantidPlot/src/ApplicationWindow.cpp
+++ b/MantidPlot/src/ApplicationWindow.cpp
@@ -91,6 +91,7 @@
 #include "PlotWizard.h"
 #include "PolynomFitDialog.h"
 #include "PolynomialFit.h"
+#include "ProjectRecovery.h"
 #include "ProjectSerialiser.h"
 #include "QwtErrorPlotCurve.h"
 #include "QwtHistogram.h"
@@ -253,7 +254,7 @@ ApplicationWindow::ApplicationWindow(bool factorySettings,
                                      const QStringList &args)
     : QMainWindow(), Scripted(ScriptingLangManager::newEnv(this)),
       blockWindowActivation(false), m_enableQtiPlotFitting(false),
-      m_projectRecoveryThread(this), m_exitCode(0),
+      m_ProjectRecovery(this), m_exitCode(0),
 #ifdef Q_OS_MAC // Mac
       settings(QSettings::IniFormat, QSettings::UserScope, "Mantid",
                "MantidPlot")
@@ -262,7 +263,7 @@ ApplicationWindow::ApplicationWindow(bool factorySettings,
 #endif
 {
   init(factorySettings, args);
-  m_projectRecoveryThread.startProjectSaving();
+  m_ProjectRecovery.startProjectSaving();
 }
 
 /**
@@ -9773,7 +9774,7 @@ void ApplicationWindow::closeEvent(QCloseEvent *ce) {
 
   // Stop background saving thread, so it doesn't try to use a destroyed
   // resource
-  m_projectRecoveryThread.stopProjectSaving();
+  m_ProjectRecovery.stopProjectSaving();
 
   // Close the remaining MDI windows. The Python API is required to be active
   // when the MDI window destructor is called so that those references can be
diff --git a/MantidPlot/src/ApplicationWindow.h b/MantidPlot/src/ApplicationWindow.h
index d5ee353e18d..b6c9447ec83 100644
--- a/MantidPlot/src/ApplicationWindow.h
+++ b/MantidPlot/src/ApplicationWindow.h
@@ -45,7 +45,7 @@ Description          : QtiPlot's main window
 #include "MantidQtWidgets/Common/HelpWindow.h"
 #include "MantidQtWidgets/Common/IProjectSerialisable.h"
 
-#include "ProjectRecoveryThread.h"
+#include "ProjectRecovery.h"
 #include "ProjectSaveView.h"
 #include "Script.h"
 #include "Scripted.h"
@@ -1637,7 +1637,7 @@ private:
   bool m_shuttingDown{false};
 
   /// Owns a thread which automatically triggers project recovery for the GUI
-  MantidQt::API::ProjectRecoveryThread m_projectRecoveryThread;
+  MantidQt::ProjectRecovery m_ProjectRecovery;
 
 #ifdef SHARED_MENUBAR
   QMenuBar *m_sharedMenuBar; ///< Pointer to the shared menubar
diff --git a/MantidPlot/src/ProjectRecoveryThread.cpp b/MantidPlot/src/ProjectRecovery.cpp
similarity index 90%
rename from MantidPlot/src/ProjectRecoveryThread.cpp
rename to MantidPlot/src/ProjectRecovery.cpp
index e22082964ad..67f40d63d0c 100644
--- a/MantidPlot/src/ProjectRecoveryThread.cpp
+++ b/MantidPlot/src/ProjectRecovery.cpp
@@ -1,4 +1,4 @@
-#include "ProjectRecoveryThread.h"
+#include "ProjectRecovery.h"
 
 #include "ApplicationWindow.h"
 #include "Folder.h"
@@ -15,7 +15,8 @@
 #include "Poco/NObserver.h"
 
 #include "Poco/Path.h"
-#include "qmetaobject.h"
+
+#include <QMetaObject>
 
 #include <chrono>
 #include <condition_variable>
@@ -112,32 +113,31 @@ const std::chrono::seconds TIME_BETWEEN_SAVING(SAVING_TIME);
 } // namespace
 
 namespace MantidQt {
-namespace API {
 
 /**
- * Constructs a new ProjectRecoveryThread, a class which encapsulates
+ * Constructs a new ProjectRecovery, a class which encapsulates
  * a background thread to save periodically. This does not start the
  * background thread though
  *
  * @param windowHandle :: Pointer to the main application window
  */
-ProjectRecoveryThread::ProjectRecoveryThread(ApplicationWindow *windowHandle)
+ProjectRecovery::ProjectRecovery(ApplicationWindow *windowHandle)
     : m_backgroundSavingThread(), m_stopBackgroundThread(true),
-      m_configKeyObserver(*this, &ProjectRecoveryThread::configKeyChanged),
+      m_configKeyObserver(*this, &ProjectRecovery::configKeyChanged),
       m_windowPtr(windowHandle) {}
 
 /// Destructor which also stops any background threads currently in progress
-ProjectRecoveryThread::~ProjectRecoveryThread() { stopProjectSaving(); }
+ProjectRecovery::~ProjectRecovery() { stopProjectSaving(); }
 
 /// Returns a background thread with the current object captured inside it
-std::thread ProjectRecoveryThread::createBackgroundThread() {
+std::thread ProjectRecovery::createBackgroundThread() {
   // Using a lambda helps the compiler deduce the this pointer
   // otherwise the resolution is ambiguous
   return std::thread([this] { projectSavingThreadWrapper(); });
 }
 
 /// Callback for POCO when a config change had fired for the enabled key
-void ProjectRecoveryThread::configKeyChanged(
+void ProjectRecovery::configKeyChanged(
     Mantid::Kernel::ConfigValChangeNotification_ptr notif) {
   if (notif->key() != (SAVING_ENABLED_CONFIG_KEY)) {
     return;
@@ -155,8 +155,7 @@ void ProjectRecoveryThread::configKeyChanged(
  * folder. This is based on the configuration key which
  * indicates how many points to keep
  */
-void ProjectRecoveryThread::deleteExistingCheckpoints(
-    size_t checkpointsToKeep) {
+void ProjectRecovery::deleteExistingCheckpoints(size_t checkpointsToKeep) {
   static auto workingFolder = getRecoveryFolder();
   Poco::Path recoveryPath;
   if (!recoveryPath.tryParse(workingFolder)) {
@@ -201,7 +200,7 @@ void ProjectRecoveryThread::deleteExistingCheckpoints(
 }
 
 /// Starts a background thread which saves out the project periodically
-void ProjectRecoveryThread::startProjectSaving() {
+void ProjectRecovery::startProjectSaving() {
   // Close the existing thread first
   stopProjectSaving();
 
@@ -219,7 +218,7 @@ void ProjectRecoveryThread::startProjectSaving() {
 }
 
 /// Stops any existing background threads which are running
-void ProjectRecoveryThread::stopProjectSaving() {
+void ProjectRecovery::stopProjectSaving() {
   {
     std::lock_guard<std::mutex> lock(m_notifierMutex);
     m_stopBackgroundThread = true;
@@ -233,7 +232,7 @@ void ProjectRecoveryThread::stopProjectSaving() {
 
 /// Top level thread wrapper which catches all exceptions to gracefully handle
 /// them
-void ProjectRecoveryThread::projectSavingThreadWrapper() {
+void ProjectRecovery::projectSavingThreadWrapper() {
   try {
     projectSavingThread();
   } catch (std::exception const &e) {
@@ -252,7 +251,7 @@ void ProjectRecoveryThread::projectSavingThreadWrapper() {
  * exit early. After the timeout elapses, if the thread has not been
  * requested to exit, it will save the project out
  */
-void ProjectRecoveryThread::projectSavingThread() {
+void ProjectRecovery::projectSavingThread() {
   while (!m_stopBackgroundThread) {
     std::unique_lock<std::mutex> lock(m_notifierMutex);
     // The condition variable releases the lock until the var changes
@@ -287,8 +286,7 @@ void ProjectRecoveryThread::projectSavingThread() {
  * @param projectDestFile :: The full path to write to
  * @throws If saving fails in the main GUI thread
  */
-void ProjectRecoveryThread::saveOpenWindows(
-    const std::string &projectDestFile) {
+void ProjectRecovery::saveOpenWindows(const std::string &projectDestFile) {
   bool saveCompleted = false;
   if (!QMetaObject::invokeMethod(m_windowPtr, "saveProjectRecovery",
                                  Qt::BlockingQueuedConnection,
@@ -310,8 +308,7 @@ void ProjectRecoveryThread::saveOpenWindows(
  * @param historyDestFolder:: The folder to write all histories to
  * @throw If saving fails in the script
  */
-void ProjectRecoveryThread::saveWsHistories(
-    const std::string &historyDestFolder) {
+void ProjectRecovery::saveWsHistories(const std::string &historyDestFolder) {
   QString projectSavingCode =
       "from mantid.simpleapi import write_all_workspaces_histories\n"
       "write_all_workspaces_histories(\"" +
@@ -322,5 +319,4 @@ void ProjectRecoveryThread::saveWsHistories(
   }
 }
 
-} // namespace API
 } // namespace MantidQt
diff --git a/MantidPlot/src/ProjectRecoveryThread.h b/MantidPlot/src/ProjectRecovery.h
similarity index 88%
rename from MantidPlot/src/ProjectRecoveryThread.h
rename to MantidPlot/src/ProjectRecovery.h
index fda63e5f096..fd16966b2a4 100644
--- a/MantidPlot/src/ProjectRecoveryThread.h
+++ b/MantidPlot/src/ProjectRecovery.h
@@ -1,5 +1,5 @@
-#ifndef PROJECT_RECOVERY_THREAD_H_
-#define PROJECT_RECOVERY_THREAD_H_
+#ifndef PROJECT_RECOVERY_H_
+#define PROJECT_RECOVERY_H_
 
 #include "MantidKernel/ConfigService.h"
 
@@ -42,13 +42,12 @@ File change history is stored at: <https://github.com/mantidproject/mantid>
 */
 
 namespace MantidQt {
-namespace API {
-class ProjectRecoveryThread {
+class ProjectRecovery {
 public:
   /// Constructor
-  explicit ProjectRecoveryThread(ApplicationWindow *windowHandle);
+  explicit ProjectRecovery(ApplicationWindow *windowHandle);
   /// Destructor the ensures background thread stops
-  ~ProjectRecoveryThread();
+  ~ProjectRecovery();
 
   /// Starts the background thread
   void startProjectSaving();
@@ -85,15 +84,13 @@ private:
   std::condition_variable m_threadNotifier;
 
   /// Config observer to monitor the key
-  Poco::NObserver<ProjectRecoveryThread,
-                  Mantid::Kernel::ConfigValChangeNotification>
+  Poco::NObserver<ProjectRecovery, Mantid::Kernel::ConfigValChangeNotification>
       m_configKeyObserver;
 
   /// Pointer to main GUI window
   ApplicationWindow *m_windowPtr;
 };
 
-} // namespace API
 } // namespace MantidQt
 
-#endif // PROJECT_RECOVERY_THREAD_H_
+#endif // PROJECT_RECOVERY_H_
-- 
GitLab