Chronoshare should still watch the filesystem even the connection is closed.

Used only one ioService to control connection and file watch

Change-Id: Ib54d5f839acecc89f7f2391bb8b2590eac362ffe
diff --git a/gui/chronosharegui.cpp b/gui/chronosharegui.cpp
index ef4af73..8df191f 100644
--- a/gui/chronosharegui.cpp
+++ b/gui/chronosharegui.cpp
@@ -107,7 +107,8 @@
   m_trayIcon->show();
 
   // load settings
-  if (!loadSettings()) {
+  if (!loadSettings() || m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
+      m_sharedFolderName == "" || m_dirPath.isNull() || m_dirPath == "") {
     // prompt user to choose folder
     openMessageBox("First Time Setup",
                    "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
@@ -116,15 +117,7 @@
     viewSettings();
   }
   else {
-    if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
-        m_sharedFolderName == "") {
-      openMessageBox("First Time Setup",
-                     "To activate ChronoShare, please configure your username and shared folder name.");
-      viewSettings();
-    }
-    else {
-      startBackend();
-    }
+    startBackend();
   }
 }
 
@@ -146,6 +139,10 @@
     m_watcher.reset();    // stop filewatching ASAP
     m_dispatcher.reset(); // stop dispatcher ASAP, but after watcher(to prevent triggering callbacks
                           // on deleted object)
+    m_ioServiceWork.reset();
+    m_ioSerciceManager->handle_stop();
+    m_NetworkThread.join();
+    delete m_ioSerciceManager;
   }
 
   fs::path realPathToFolder(m_dirPath.toStdString());
@@ -164,6 +161,21 @@
   m_watcher.reset(new FsWatcher(*m_ioService, realPathToFolder.string().c_str(),
                                 bind(&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher.get(), _1),
                                 bind(&Dispatcher::Did_LocalFile_Delete, m_dispatcher.get(), _1)));
+  try {
+    m_ioSerciceManager = new IoServiceManager(*m_ioService);
+    m_NetworkThread = std::thread(&IoServiceManager::run, m_ioSerciceManager);
+  }
+  catch (const std::exception& e) {
+    _LOG_ERROR("Start IO service or Face failed");
+    openWarningMessageBox("", "WARNING: Cannot allocate thread for face and io_service!",
+                          QString("Starting chronoshare failed"
+                                  "Exception caused: %1")
+                            .arg(e.what()));
+    // stop filewatching ASAP
+    m_watcher.reset();
+    m_dispatcher.reset();
+    return;
+  }
 
   if (m_httpServer != 0) {
     // no need to restart webserver if it already exists
@@ -176,18 +188,14 @@
       m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
       m_httpServerThread = std::thread(&http::server::server::run, m_httpServer);
     }
-    catch (std::exception& e) {
+    catch (const std::exception& e) {
       _LOG_ERROR("Start http server failed");
       m_httpServer = 0; // just to make sure
-      QMessageBox msgBox;
-      msgBox.setText("WARNING: Cannot start http server!");
-      msgBox.setIcon(QMessageBox::Warning);
-      msgBox.setInformativeText(
-        QString("Starting http server failed. You will not be able to check history from web "
-                "brower. Exception caused: %1")
-          .arg(e.what()));
-      msgBox.setStandardButtons(QMessageBox::Ok);
-      msgBox.exec();
+      openWarningMessageBox("WARNING", "WARNING: Cannot start http server!",
+                            QString("Starting http server failed. You will "
+                                    "not be able to check history from web "
+                                    "brower. Exception caused: %1")
+                              .arg(e.what()));
     }
   }
   else {
@@ -203,6 +211,12 @@
   // stop dispatcher ASAP, but after watcher to prevent triggering callbacks on the deleted object
   m_dispatcher.reset();
 
+  m_ioServiceWork.reset();
+  m_ioSerciceManager->handle_stop();
+  m_chronoshareThread.join();
+  m_NetworkThread.join();
+  delete m_ioSerciceManager;
+
   if (m_httpServer != 0) {
     m_httpServer->handle_stop();
     m_httpServerThread.join();
@@ -259,6 +273,20 @@
 }
 
 void
+ChronoShareGui::openWarningMessageBox(QString title, QString text, QString infotext)
+{
+  QMessageBox messageBox(this);
+  messageBox.setWindowTitle(title);
+  messageBox.setText(text);
+  messageBox.setInformativeText(infotext);
+
+  messageBox.setIcon(QMessageBox::Warning);
+  messageBox.setStandardButtons(QMessageBox::Ok);
+
+  messageBox.exec();
+}
+
+void
 ChronoShareGui::createActionsAndMenu()
 {
   _LOG_DEBUG("Create actions");