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
+2 -8
View File
@@ -15,6 +15,7 @@
#include "component/view/MainView.h"
#include "component/view/ViewFactory.h"
#include "data/StorageCache.h"
#include "LicenseChecker.h"
#include "settings/ApplicationSettings.h"
#include "settings/ColorScheme.h"
@@ -73,6 +74,7 @@ void Application::loadSettings()
Application::Application(bool withGUI)
: m_hasGUI(withGUI)
{
LicenseChecker::createInstance();
}
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)
{
if (m_hasGUI)
-1
View File
@@ -36,7 +36,6 @@ public:
void loadProject(const FilePath& projectSettingsFilePath);
void refreshProject();
void saveProject(const FilePath& projectSettingsFilePath);
void forceEnterLicense();
bool hasGUI();
private:
+1
View File
@@ -236,6 +236,7 @@ add_files(
utility/messaging/type/MessageFlushUpdates.h
utility/messaging/type/MessageFocusIn.h
utility/messaging/type/MessageFocusOut.h
utility/messaging/type/MessageForceEnterLicense.h
utility/messaging/type/MessageGraphNodeBundleSplit.h
utility/messaging/type/MessageGraphNodeExpand.h
utility/messaging/type/MessageGraphNodeMove.h
+110 -53
View File
@@ -1,14 +1,14 @@
#include "LicenseChecker.h"
#include "utility/AppPath.h"
#include "utility/messaging/type/MessageForceEnterLicense.h"
#include "utility/messaging/type/MessageStatus.h"
#include "isTrial.h"
#include "License.h"
#include "Application.h"
#include "PublicKey.h"
#include "settings/ApplicationSettings.h"
#include "utility/AppPath.h"
void LicenseChecker::createInstance()
{
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()
{
MessageStatus("preparing...", false, true).dispatch();
return checkCurrentLicense() == LICENSE_VALID;
}
bool valid = false;
do
LicenseChecker::LicenseState LicenseChecker::checkCurrentLicense() const
{
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();
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;
}
return LICENSE_EMPTY;
}
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()
: m_app(nullptr)
, m_forcedLicenseEntering(false)
: m_forcedLicenseEntering(false)
{
}
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_forcedLicenseEntering = true;
m_pendingMessage = message->content;
if (!m_forcedLicenseEntering)
{
m_forcedLicenseEntering = true;
MessageForceEnterLicense(state == LICENSE_EXPIRED).dispatch();
}
return;
}
}
else
{
message->content->dispatch();
}
message->content->dispatch();
}
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;
+17 -3
View File
@@ -5,21 +5,34 @@
#include "utility/messaging/type/MessageEnteredLicense.h"
#include "utility/messaging/type/MessageDispatchWhenLicenseValid.h"
class Application;
class License;
class LicenseChecker
: public MessageListener<MessageDispatchWhenLicenseValid>
, public MessageListener<MessageEnteredLicense>
{
public:
enum LicenseState
{
LICENSE_EMPTY,
LICENSE_MOVED,
LICENSE_MALFORMED,
LICENSE_INVALID,
LICENSE_EXPIRED,
LICENSE_VALID
};
static void createInstance();
static std::shared_ptr<LicenseChecker> getInstance();
~LicenseChecker();
void setApp(Application* app);
std::string getCurrentLicenseString() const;
void saveCurrentLicenseString(const std::string& licenseString) const;
bool isCurrentLicenseValid();
LicenseState checkCurrentLicense() const;
LicenseState checkLicenseString(const std::string licenseString) const;
private:
LicenseChecker();
@@ -29,9 +42,10 @@ private:
void handleMessage(MessageDispatchWhenLicenseValid* message);
void handleMessage(MessageEnteredLicense* message);
LicenseState checkLicense(License& license) const;
static std::shared_ptr<LicenseChecker> s_instance;
Application* m_app;
std::shared_ptr<MessageBase> m_pendingMessage;
bool m_forcedLicenseEntering;
};
-1
View File
@@ -16,7 +16,6 @@ public:
virtual void setTitle(const std::string& title) = 0;
virtual void activateWindow() = 0;
virtual void updateRecentProjectMenu() = 0;
virtual void forceLicenseScreen() = 0;
};
#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