]> vaikene.ee Git - evaf/blobdiff - src/libs/Common/app.cpp
Warning fixes and copyright update.
[evaf] / src / libs / Common / app.cpp
index 53efce95d319ad6cc12eea546d5b9f1189202275..b907a4b4ebe05c7749d026a4e52fd9d9a598cb14 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Application interface implementation
  * @author Enar Vaikene
  *
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2019 Enar Vaikene
  *
  * This file is part of the eVaf C++ cross-platform application development framework.
  *
@@ -19,7 +19,9 @@
 
 #include "app.h"
 #include "globals.h"
-#include "registry.h"
+#include "iregistry.h"
+#include "ieventqueue.h"
+#include "event.h"
 #include "version.h"
 
 #include <QtCore>
 
 using namespace eVaf::Common;
 
+namespace
+{
+    static Internal::App * singleton = nullptr;
+}
+
 iApp * iApp::instance()
 {
-    static Internal::App singleton;
-    return &singleton;
+    if (nullptr == singleton)
+    {
+        singleton = new Internal::App;
+    }
+    return singleton;
 }
 
 char const * const iApp::EV_QUIT = "iApp::quit";
@@ -45,17 +55,31 @@ char const * const iApp::EV_TERMINATING = "iApp::terminating";
 
 using namespace eVaf::Common::Internal;
 
+void App::destroyInstance()
+{
+    if (nullptr != singleton)
+    {
+        delete singleton;
+        singleton = nullptr;
+    }
+}
+
 App::App()
     : iApp()
     , mReady(false)
     , mName(VER_PRODUCT_NAME_STR)
+    , mEvQuit(0)
+    , mEvRestart(0)
+    , mEvReady(0)
+    , mEvTerminating(0)
 {
     setObjectName(QString("%1.iApp").arg(VER_MODULE_NAME_STR));
-
+    EVAF_INFO("%s-App created", VER_MODULE_NAME_STR);
 }
 
 App::~App()
 {
+    EVAF_INFO("%s-App destroyed", VER_MODULE_NAME_STR);
 }
 
 bool App::init()
@@ -63,6 +87,12 @@ bool App::init()
     // Register our interface
     iRegistry::instance()->registerInterface("iApp", this);
 
+    // Register events
+    mEvQuit = iEventQueue::instance()->subscribeEvent(iEventQueue::instance()->registerEvent(EV_QUIT), this);
+    mEvRestart = iEventQueue::instance()->subscribeEvent(iEventQueue::instance()->registerEvent(EV_RESTART), this);
+    mEvReady = iEventQueue::instance()->registerEvent(EV_READY);
+    mEvTerminating = iEventQueue::instance()->registerEvent(EV_TERMINATING);
+
     // Set the default application name and language
     mName = VER_PRODUCT_NAME_STR;
     mLanguage = QLocale::system().name();
@@ -176,10 +206,25 @@ bool App::init()
     return true;
 }
 
+bool App::event(QEvent * e)
+{
+    if (e->type() == Event::eVafEvent) {
+        Event * event = static_cast<Event *>(e);
+        if (event->id() == mEvQuit)
+            quit();
+        else if (event->id() == mEvRestart)
+            restart();
+
+        return false;
+    }
+    else
+        return iApp::event(e);
+}
+
 QString const App::dataRootDir() const
 {
     if (mDataRootDir.isEmpty()) {
-#ifdef Q_OS_LINUX
+#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
         QString dataLoc = QDir::homePath();
         if (!dataLoc.endsWith('/'))
             dataLoc.append('/');
@@ -247,6 +292,14 @@ QString const App::xmlFileName() const
     return mXmlFile;
 }
 
+int App::exec()
+{
+    setReady(true);
+    int const rval = QCoreApplication::exec();
+    setReady(false);
+    return rval;
+}
+
 void App::restart()
 {
     QCoreApplication::exit(RC_Restart);
@@ -256,3 +309,15 @@ void App::quit(bool err)
 {
     QCoreApplication::exit(err ? RC_Error : RC_Quit);
 }
+
+void App::setReady(bool value)
+{
+    if (mReady != value) {
+        mReady = value;
+        iEventQueue::instance()->broadcastEvent(new Event(mReady ? mEvReady : mEvTerminating));
+        if (mReady)
+            emit ready();
+        else
+            emit terminating();
+    }
+}