logic: Show error message when license expired

* moved all license checking logic to LicenseChecker
* create LicenseChecker instance in app
* Notify MainView with MessageForceEnterLicense
This commit is contained in:
Eberhard Graether
2016-04-22 13:58:13 +02:00
parent 73973cb4a3
commit a6e57a6564
15 changed files with 221 additions and 140 deletions
-6
View File
@@ -18,8 +18,6 @@
#include "qt/window/QtMainWindow.h" #include "qt/window/QtMainWindow.h"
#include "version.h" #include "version.h"
#include "settings/ProjectSettings.h"
void init() void init()
{ {
std::shared_ptr<ConsoleLogger> consoleLogger = std::make_shared<ConsoleLogger>(); std::shared_ptr<ConsoleLogger> consoleLogger = std::make_shared<ConsoleLogger>();
@@ -59,7 +57,6 @@ int main(int argc, char *argv[])
std::shared_ptr<Application> app = Application::create(version); std::shared_ptr<Application> app = Application::create(version);
std::shared_ptr<LicenseChecker> checker = LicenseChecker::getInstance(); std::shared_ptr<LicenseChecker> checker = LicenseChecker::getInstance();
checker->setApp(app.get());
if (commandLineParser.startedWithLicense()) if (commandLineParser.startedWithLicense())
{ {
@@ -93,9 +90,6 @@ int main(int argc, char *argv[])
utility::loadFontsFromDirectory(ResourcePaths::getFontsPath(), ".otf"); utility::loadFontsFromDirectory(ResourcePaths::getFontsPath(), ".otf");
std::shared_ptr<Application> app = Application::create(version, &viewFactory, &networkFactory); std::shared_ptr<Application> app = Application::create(version, &viewFactory, &networkFactory);
std::shared_ptr<LicenseChecker> checker = LicenseChecker::getInstance();
checker->setApp(app.get());
commandLineParser.projectLoad(); commandLineParser.projectLoad();
return qtApp.exec(); return qtApp.exec();
+2 -8
View File
@@ -15,6 +15,7 @@
#include "component/view/MainView.h" #include "component/view/MainView.h"
#include "component/view/ViewFactory.h" #include "component/view/ViewFactory.h"
#include "data/StorageCache.h" #include "data/StorageCache.h"
#include "LicenseChecker.h"
#include "settings/ApplicationSettings.h" #include "settings/ApplicationSettings.h"
#include "settings/ColorScheme.h" #include "settings/ColorScheme.h"
@@ -73,6 +74,7 @@ void Application::loadSettings()
Application::Application(bool withGUI) Application::Application(bool withGUI)
: m_hasGUI(withGUI) : m_hasGUI(withGUI)
{ {
LicenseChecker::createInstance();
} }
Application::~Application() Application::~Application()
@@ -135,14 +137,6 @@ void Application::saveProject(const FilePath& projectSettingsFilePath)
} }
} }
void Application::forceEnterLicense()
{
if (m_hasGUI)
{
m_mainView->forceLicenseScreen();
}
}
void Application::handleMessage(MessageActivateWindow* message) void Application::handleMessage(MessageActivateWindow* message)
{ {
if (m_hasGUI) if (m_hasGUI)
-1
View File
@@ -36,7 +36,6 @@ public:
void loadProject(const FilePath& projectSettingsFilePath); void loadProject(const FilePath& projectSettingsFilePath);
void refreshProject(); void refreshProject();
void saveProject(const FilePath& projectSettingsFilePath); void saveProject(const FilePath& projectSettingsFilePath);
void forceEnterLicense();
bool hasGUI(); bool hasGUI();
private: private:
+1
View File
@@ -236,6 +236,7 @@ add_files(
utility/messaging/type/MessageFlushUpdates.h utility/messaging/type/MessageFlushUpdates.h
utility/messaging/type/MessageFocusIn.h utility/messaging/type/MessageFocusIn.h
utility/messaging/type/MessageFocusOut.h utility/messaging/type/MessageFocusOut.h
utility/messaging/type/MessageForceEnterLicense.h
utility/messaging/type/MessageGraphNodeBundleSplit.h utility/messaging/type/MessageGraphNodeBundleSplit.h
utility/messaging/type/MessageGraphNodeExpand.h utility/messaging/type/MessageGraphNodeExpand.h
utility/messaging/type/MessageGraphNodeMove.h utility/messaging/type/MessageGraphNodeMove.h
+110 -53
View File
@@ -1,14 +1,14 @@
#include "LicenseChecker.h" #include "LicenseChecker.h"
#include "utility/AppPath.h"
#include "utility/messaging/type/MessageForceEnterLicense.h"
#include "utility/messaging/type/MessageStatus.h" #include "utility/messaging/type/MessageStatus.h"
#include "isTrial.h"
#include "License.h" #include "License.h"
#include "Application.h"
#include "PublicKey.h" #include "PublicKey.h"
#include "settings/ApplicationSettings.h" #include "settings/ApplicationSettings.h"
#include "utility/AppPath.h"
void LicenseChecker::createInstance() void LicenseChecker::createInstance()
{ {
if (!s_instance) if (!s_instance)
@@ -28,79 +28,119 @@ LicenseChecker::~LicenseChecker()
{ {
} }
void LicenseChecker::setApp(Application* app) std::string LicenseChecker::getCurrentLicenseString() const
{ {
m_app = app; License license;
bool isLoaded = license.loadFromEncodedString(
ApplicationSettings::getInstance()->getLicenseString(), AppPath::getAppPath());
if (isLoaded)
{
return license.getLicenseString();
}
return "";
}
void LicenseChecker::saveCurrentLicenseString(const std::string& licenseString) const
{
License license;
bool isLoaded = license.loadFromString(licenseString);
if (!isLoaded)
{
return;
}
ApplicationSettings* appSettings = ApplicationSettings::getInstance().get();
std::string appPath(AppPath::getAppPath());
appSettings->setLicenseString(license.getLicenseEncodedString(appPath));
appSettings->setLicenseCheck(license.hashLocation(FilePath(appPath).absolute().str()));
appSettings->save();
} }
bool LicenseChecker::isCurrentLicenseValid() bool LicenseChecker::isCurrentLicenseValid()
{ {
MessageStatus("preparing...", false, true).dispatch(); return checkCurrentLicense() == LICENSE_VALID;
}
bool valid = false; LicenseChecker::LicenseState LicenseChecker::checkCurrentLicense() const
do {
ApplicationSettings* appSettings = ApplicationSettings::getInstance().get();
std::string licenseCheck = appSettings->getLicenseCheck();
std::string appPath(AppPath::getAppPath());
std::string licenseString = appSettings->getLicenseString();
if (licenseString.size() == 0)
{ {
ApplicationSettings* appSettings = ApplicationSettings::getInstance().get(); return LICENSE_EMPTY;
std::string licenseCheck = appSettings->getLicenseCheck();
std::string appPath = AppPath::getAppPath(); // for easier debugging...
FilePath p(appPath);
if (!License::checkLocation(p.absolute().str(), licenseCheck))
{
break;
}
std::string licenseString = appSettings->getLicenseString();
if (licenseString.size() == 0)
{
break;
}
License license;
bool isLoaded = license.loadFromEncodedString(licenseString,AppPath::getAppPath());
if (!isLoaded)
{
break;
}
license.loadPublicKeyFromString(PublicKey);
valid = license.isValid();
if (license.isExpired())
{
valid = false;
}
} }
while (false);
MessageStatus("ready").dispatch(); if (!License::checkLocation(FilePath(appPath).absolute().str(), licenseCheck))
{
return LICENSE_MOVED;
}
return valid; License license;
bool isLoaded = license.loadFromEncodedString(licenseString, appPath);
if (!isLoaded)
{
return LICENSE_MOVED;
}
return checkLicense(license);
}
LicenseChecker::LicenseState LicenseChecker::checkLicenseString(const std::string licenseString) const
{
if (licenseString.size() == 0)
{
return LICENSE_EMPTY;
}
License license;
bool isLoaded = license.loadFromString(licenseString);
if (!isLoaded)
{
return LICENSE_MALFORMED;
}
license.print();
return checkLicense(license);
} }
LicenseChecker::LicenseChecker() LicenseChecker::LicenseChecker()
: m_app(nullptr) : m_forcedLicenseEntering(false)
, m_forcedLicenseEntering(false)
{ {
} }
void LicenseChecker::handleMessage(MessageDispatchWhenLicenseValid* message) void LicenseChecker::handleMessage(MessageDispatchWhenLicenseValid* message)
{ {
if (m_app != nullptr && !isCurrentLicenseValid()) if (!isTrial())
{ {
m_pendingMessage = message->content; MessageStatus("preparing...", false, true).dispatch();
if (!m_forcedLicenseEntering) LicenseState state = checkCurrentLicense();
MessageStatus("ready").dispatch();
if (state != LICENSE_VALID)
{ {
m_app->forceEnterLicense(); m_pendingMessage = message->content;
m_forcedLicenseEntering = true;
if (!m_forcedLicenseEntering)
{
m_forcedLicenseEntering = true;
MessageForceEnterLicense(state == LICENSE_EXPIRED).dispatch();
}
return;
} }
} }
else
{ message->content->dispatch();
message->content->dispatch();
}
} }
void LicenseChecker::handleMessage(MessageEnteredLicense* message) void LicenseChecker::handleMessage(MessageEnteredLicense* message)
@@ -114,4 +154,21 @@ void LicenseChecker::handleMessage(MessageEnteredLicense* message)
} }
} }
LicenseChecker::LicenseState LicenseChecker::checkLicense(License& license) const
{
license.loadPublicKeyFromString(PublicKey);
if (license.isExpired())
{
return LICENSE_EXPIRED;
}
if (license.isValid())
{
return LICENSE_VALID;
}
return LICENSE_INVALID;
}
std::shared_ptr<LicenseChecker> LicenseChecker::s_instance; std::shared_ptr<LicenseChecker> LicenseChecker::s_instance;
+17 -3
View File
@@ -5,21 +5,34 @@
#include "utility/messaging/type/MessageEnteredLicense.h" #include "utility/messaging/type/MessageEnteredLicense.h"
#include "utility/messaging/type/MessageDispatchWhenLicenseValid.h" #include "utility/messaging/type/MessageDispatchWhenLicenseValid.h"
class Application; class License;
class LicenseChecker class LicenseChecker
: public MessageListener<MessageDispatchWhenLicenseValid> : public MessageListener<MessageDispatchWhenLicenseValid>
, public MessageListener<MessageEnteredLicense> , public MessageListener<MessageEnteredLicense>
{ {
public: public:
enum LicenseState
{
LICENSE_EMPTY,
LICENSE_MOVED,
LICENSE_MALFORMED,
LICENSE_INVALID,
LICENSE_EXPIRED,
LICENSE_VALID
};
static void createInstance(); static void createInstance();
static std::shared_ptr<LicenseChecker> getInstance(); static std::shared_ptr<LicenseChecker> getInstance();
~LicenseChecker(); ~LicenseChecker();
void setApp(Application* app); std::string getCurrentLicenseString() const;
void saveCurrentLicenseString(const std::string& licenseString) const;
bool isCurrentLicenseValid(); bool isCurrentLicenseValid();
LicenseState checkCurrentLicense() const;
LicenseState checkLicenseString(const std::string licenseString) const;
private: private:
LicenseChecker(); LicenseChecker();
@@ -29,9 +42,10 @@ private:
void handleMessage(MessageDispatchWhenLicenseValid* message); void handleMessage(MessageDispatchWhenLicenseValid* message);
void handleMessage(MessageEnteredLicense* message); void handleMessage(MessageEnteredLicense* message);
LicenseState checkLicense(License& license) const;
static std::shared_ptr<LicenseChecker> s_instance; static std::shared_ptr<LicenseChecker> s_instance;
Application* m_app;
std::shared_ptr<MessageBase> m_pendingMessage; std::shared_ptr<MessageBase> m_pendingMessage;
bool m_forcedLicenseEntering; bool m_forcedLicenseEntering;
}; };
-1
View File
@@ -16,7 +16,6 @@ public:
virtual void setTitle(const std::string& title) = 0; virtual void setTitle(const std::string& title) = 0;
virtual void activateWindow() = 0; virtual void activateWindow() = 0;
virtual void updateRecentProjectMenu() = 0; virtual void updateRecentProjectMenu() = 0;
virtual void forceLicenseScreen() = 0;
}; };
#endif // MAIN_VIEW_H #endif // MAIN_VIEW_H
@@ -0,0 +1,24 @@
#ifndef MESSAGE_FORCE_ENTER_LICENSE_H
#define MESSAGE_FORCE_ENTER_LICENSE_H
#include "utility/messaging/Message.h"
class MessageForceEnterLicense
: public Message<MessageForceEnterLicense>
{
public:
MessageForceEnterLicense(bool licenseExpired)
: licenseExpired(licenseExpired)
{
setSendAsTask(false);
}
static const std::string getStaticType()
{
return "MessageForceEnterLicense";
}
bool licenseExpired;
};
#endif // MESSAGE_FORCE_ENTER_LICENSE_H
+5 -5
View File
@@ -11,7 +11,7 @@ QtMainView::QtMainView()
, m_setTitleFunctor(std::bind(&QtMainView::doSetTitle, this, std::placeholders::_1)) , m_setTitleFunctor(std::bind(&QtMainView::doSetTitle, this, std::placeholders::_1))
, m_activateWindowFunctor(std::bind(&QtMainView::doActivateWindow, this)) , m_activateWindowFunctor(std::bind(&QtMainView::doActivateWindow, this))
, m_updateRecentProjectMenuFunctor(std::bind(&QtMainView::doUpdateRecentProjectMenu, this)) , m_updateRecentProjectMenuFunctor(std::bind(&QtMainView::doUpdateRecentProjectMenu, this))
, m_forceLicenseScreenFunctor(std::bind(&QtMainView::doForceLicenseScreen, this)) , m_forceLicenseScreenFunctor(std::bind(&QtMainView::doForceLicenseScreen, this, std::placeholders::_1))
{ {
m_window = std::make_shared<QtMainWindow>(); m_window = std::make_shared<QtMainWindow>();
m_window->show(); m_window->show();
@@ -89,9 +89,9 @@ void QtMainView::updateRecentProjectMenu()
m_updateRecentProjectMenuFunctor(); m_updateRecentProjectMenuFunctor();
} }
void QtMainView::forceLicenseScreen() void QtMainView::handleMessage(MessageForceEnterLicense* message)
{ {
m_forceLicenseScreenFunctor(); m_forceLicenseScreenFunctor(message->licenseExpired);
} }
void QtMainView::handleMessage(MessageProjectNew* message) void QtMainView::handleMessage(MessageProjectNew* message)
@@ -140,7 +140,7 @@ void QtMainView::doUpdateRecentProjectMenu()
m_window->updateRecentProjectMenu(); m_window->updateRecentProjectMenu();
} }
void QtMainView::doForceLicenseScreen() void QtMainView::doForceLicenseScreen(bool expired)
{ {
m_window->forceEnterLicense(); m_window->forceEnterLicense(expired);
} }
+5 -3
View File
@@ -10,6 +10,7 @@
#include "qt/utility/QtThreadedFunctor.h" #include "qt/utility/QtThreadedFunctor.h"
#include "utility/messaging/MessageListener.h" #include "utility/messaging/MessageListener.h"
#include "utility/messaging/type/MessageForceEnterLicense.h"
#include "utility/messaging/type/MessageProjectNew.h" #include "utility/messaging/type/MessageProjectNew.h"
#include "utility/messaging/type/MessageShowStartScreen.h" #include "utility/messaging/type/MessageShowStartScreen.h"
@@ -18,6 +19,7 @@ class View;
class QtMainView class QtMainView
: public MainView : public MainView
, public MessageListener<MessageForceEnterLicense>
, public MessageListener<MessageProjectNew> , public MessageListener<MessageProjectNew>
, public MessageListener<MessageShowStartScreen> , public MessageListener<MessageShowStartScreen>
{ {
@@ -43,9 +45,9 @@ public:
virtual void setTitle(const std::string& title); virtual void setTitle(const std::string& title);
virtual void activateWindow(); virtual void activateWindow();
virtual void updateRecentProjectMenu(); virtual void updateRecentProjectMenu();
virtual void forceLicenseScreen();
private: private:
void handleMessage(MessageForceEnterLicense* message);
void handleMessage(MessageProjectNew* message); void handleMessage(MessageProjectNew* message);
void handleMessage(MessageShowStartScreen* message); void handleMessage(MessageShowStartScreen* message);
@@ -55,7 +57,7 @@ private:
void doSetTitle(const std::string& title); void doSetTitle(const std::string& title);
void doActivateWindow(); void doActivateWindow();
void doUpdateRecentProjectMenu(); void doUpdateRecentProjectMenu();
void doForceLicenseScreen(); void doForceLicenseScreen(bool expired);
std::shared_ptr<QtMainWindow> m_window; std::shared_ptr<QtMainWindow> m_window;
std::vector<View*> m_views; std::vector<View*> m_views;
@@ -66,7 +68,7 @@ private:
QtThreadedFunctor<const std::string&> m_setTitleFunctor; QtThreadedFunctor<const std::string&> m_setTitleFunctor;
QtThreadedFunctor<> m_activateWindowFunctor; QtThreadedFunctor<> m_activateWindowFunctor;
QtThreadedFunctor<> m_updateRecentProjectMenuFunctor; QtThreadedFunctor<> m_updateRecentProjectMenuFunctor;
QtThreadedFunctor<> m_forceLicenseScreenFunctor; QtThreadedFunctor<bool> m_forceLicenseScreenFunctor;
}; };
#endif // QT_MAIN_VIEW_H #endif // QT_MAIN_VIEW_H
+44 -53
View File
@@ -5,11 +5,8 @@
#include <QLabel> #include <QLabel>
#include <QTextEdit> #include <QTextEdit>
#include "License.h" #include "LicenseChecker.h"
#include "PublicKey.h"
#include "qt/utility/utilityQt.h" #include "qt/utility/utilityQt.h"
#include "settings/ApplicationSettings.h"
#include "utility/AppPath.h"
#include "utility/file/FilePath.h" #include "utility/file/FilePath.h"
#include "utility/messaging/type/MessageEnteredLicense.h" #include "utility/messaging/type/MessageEnteredLicense.h"
#include "utility/ResourcePaths.h" #include "utility/ResourcePaths.h"
@@ -42,17 +39,19 @@ void QtLicense::load()
{ {
clear(); clear();
License license; std::string licenseString = LicenseChecker::getInstance()->getCurrentLicenseString();
bool isLoaded = license.loadFromEncodedString(
ApplicationSettings::getInstance()->getLicenseString(), AppPath::getAppPath());
if (!isLoaded)
{
return;
}
if (m_licenseText) if (licenseString.size() && m_licenseText)
{ {
m_licenseText->setText(license.getLicenseString().c_str()); m_licenseText->setText(licenseString.c_str());
}
}
void QtLicense::setErrorMessage(const QString& errorMessage)
{
if (m_errorLabel)
{
m_errorLabel->setText(errorMessage);
} }
} }
@@ -136,47 +135,39 @@ void QtLicense::handleClose()
void QtLicense::handleNext() void QtLicense::handleNext()
{ {
std::string licenseString = m_licenseText->toPlainText().toStdString(); std::string licenseString = m_licenseText->toPlainText().toStdString();
if (licenseString.size() == 0)
LicenseChecker* checker = LicenseChecker::getInstance().get();
LicenseChecker::LicenseState state = checker->checkLicenseString(licenseString);
std::string errorString;
switch (state)
{ {
m_errorLabel->setText("No licence key was entered."); case LicenseChecker::LICENSE_EMPTY:
return; errorString = "No licence key was entered.";
break;
case LicenseChecker::LICENSE_MOVED:
case LicenseChecker::LICENSE_MALFORMED:
errorString = "The entered license key is malformed.";
break;
case LicenseChecker::LICENSE_INVALID:
errorString = "The entered license key is invalid.";
break;
case LicenseChecker::LICENSE_EXPIRED:
errorString = "The entered license key is expired.";
break;
case LicenseChecker::LICENSE_VALID:
{
checker->saveCurrentLicenseString(licenseString);
MessageEnteredLicense().dispatch();
setCancelAble(true);
m_errorLabel->setText(" ");
emit finished();
return;
}
} }
License license; m_errorLabel->setText(errorString.c_str());
bool isLoaded = license.loadFromString(licenseString);
if (!isLoaded)
{
m_errorLabel->setText("The entered license key is malformed.");
return;
}
license.loadPublicKeyFromString(PublicKey);
license.print();
if (license.isExpired())
{
m_errorLabel->setText("The entered license key is expired");
return;
}
else if (license.isValid())
{
ApplicationSettings* appSettings = ApplicationSettings::getInstance().get();
std::string appLocation = AppPath::getAppPath();
appSettings->setLicenseString(license.getLicenseEncodedString(appLocation));
FilePath p(appLocation);
appSettings->setLicenseCheck(license.hashLocation(p.absolute().str()));
appSettings->save();
MessageEnteredLicense().dispatch();
}
else
{
m_errorLabel->setText("The entered license key is invalid.");
return;
}
m_errorLabel->setText(" ");
setCancelAble(true);
emit finished();
} }
+2
View File
@@ -19,6 +19,8 @@ public:
void clear(); void clear();
void load(); void load();
void setErrorMessage(const QString& errorMessage);
protected: protected:
// QtWindow implementation // QtWindow implementation
virtual void populateWindow(QWidget* widget) override; virtual void populateWindow(QWidget* widget) override;
+10 -2
View File
@@ -262,7 +262,7 @@ void QtMainWindow::saveLayout()
settings.setValue("DOCK_LOCATIONS", this->saveState()); settings.setValue("DOCK_LOCATIONS", this->saveState());
} }
void QtMainWindow::forceEnterLicense() void QtMainWindow::forceEnterLicense(bool expired)
{ {
enterLicense(); enterLicense();
@@ -272,7 +272,15 @@ void QtMainWindow::forceEnterLicense()
LOG_ERROR("No enter license window on top of stack"); LOG_ERROR("No enter license window on top of stack");
return; return;
} }
enterLicenseWindow->clear();
if (expired)
{
enterLicenseWindow->setErrorMessage("The license key is expired.");
}
else
{
enterLicenseWindow->clear();
}
this->setEnabled(false); this->setEnabled(false);
enterLicenseWindow->setEnabled(true); enterLicenseWindow->setEnabled(true);
+1 -1
View File
@@ -85,7 +85,7 @@ public:
void loadLayout(); void loadLayout();
void saveLayout(); void saveLayout();
void forceEnterLicense(); void forceEnterLicense(bool expired);
protected: protected:
bool event(QEvent* event); bool event(QEvent* event);
-4
View File
@@ -8,7 +8,6 @@
#include "Application.h" #include "Application.h"
#include "includes.h" // defines 'void setup(int argc, char *argv[])' #include "includes.h" // defines 'void setup(int argc, char *argv[])'
#include "LicenseChecker.h"
#include "qt/network/QtNetworkFactory.h" #include "qt/network/QtNetworkFactory.h"
#include "qt/QtApplication.h" #include "qt/QtApplication.h"
#include "qt/utility/utilityQt.h" #include "qt/utility/utilityQt.h"
@@ -16,7 +15,6 @@
#include "qt/window/QtMainWindow.h" #include "qt/window/QtMainWindow.h"
#include "version.h" #include "version.h"
void init() void init()
{ {
std::shared_ptr<ConsoleLogger> consoleLogger = std::make_shared<ConsoleLogger>(); std::shared_ptr<ConsoleLogger> consoleLogger = std::make_shared<ConsoleLogger>();
@@ -53,7 +51,5 @@ int main(int argc, char *argv[])
std::shared_ptr<Application> app = Application::create(version, &viewFactory, &networkFactory); std::shared_ptr<Application> app = Application::create(version, &viewFactory, &networkFactory);
LicenseChecker::createInstance();
return qtApp.exec(); return qtApp.exec();
} }