From f8e9047c36e5db0ebd42445248e86ca3c61cb72e Mon Sep 17 00:00:00 2001 From: "Jerry (Xinyu Hou)" Date: Tue, 17 Nov 2015 14:17:00 -0800 Subject: [PATCH] Rephrase and refactor serial warning message #4716 --- src/gui/src/MainWindow.cpp | 15 ++--- src/gui/src/SetupWizard.cpp | 12 +--- src/gui/src/SubscriptionManager.cpp | 86 +++++++++++++++++-------- src/gui/src/SubscriptionManager.h | 13 ++-- src/lib/synergy/SubscriptionManager.cpp | 24 +++---- 5 files changed, 89 insertions(+), 61 deletions(-) diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 572695bc..2576c0e0 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -698,22 +698,19 @@ QString MainWindow::appPath(const QString& name) bool MainWindow::serverArgs(QStringList& args, QString& app) { - SubscriptionManager subscriptionManager; + int edition; + SubscriptionManager subscriptionManager(this, edition); if (subscriptionManager.checkSubscriptionExist()) { - int edition; - int state = subscriptionManager.checkSubscription(edition); - - if (state == kInvalid) { + if (!subscriptionManager.checkSubscription()) { return false; } - else if (state == kExpired) { - QMessageBox::warning(this, tr("Subscription is expired"), - tr("Your subscription is expired. Please purchase.")); - return false; + else { + setEdition(edition); } } + app = appPath(appConfig().synergysName()); if (!QFile::exists(app)) diff --git a/src/gui/src/SetupWizard.cpp b/src/gui/src/SetupWizard.cpp index ae41c89b..e97030d8 100644 --- a/src/gui/src/SetupWizard.cpp +++ b/src/gui/src/SetupWizard.cpp @@ -127,15 +127,9 @@ bool SetupWizard::validateCurrentPage() } else { // create subscription file in profile directory - SubscriptionManager subscriptionManager; - bool r = subscriptionManager.activateSerial(m_pLineEditSerialKey->text(), m_Edition); - if (!r) { - message.setText(tr("An error occurred while trying to activate using a serial key. " - "Please contact the helpdesk, and provide the " - "following details.\n\n%1").arg(subscriptionManager.getLastError())); - message.exec(); - - return r; + SubscriptionManager subscriptionManager(this, m_Edition); + if (!subscriptionManager.activateSerial(m_pLineEditSerialKey->text())) { + return false; } m_pPluginPage->setEdition(m_Edition); diff --git a/src/gui/src/SubscriptionManager.cpp b/src/gui/src/SubscriptionManager.cpp index 0173248a..b9b0f5ff 100644 --- a/src/gui/src/SubscriptionManager.cpp +++ b/src/gui/src/SubscriptionManager.cpp @@ -24,13 +24,16 @@ #include #include -SubscriptionManager::SubscriptionManager() +SubscriptionManager::SubscriptionManager(QWidget* parent, int& edition) : + m_pParent(parent), + m_Edition(edition) { + } -bool SubscriptionManager::activateSerial(const QString& serial, int& edition) +bool SubscriptionManager::activateSerial(const QString& serial) { - edition = Unknown; + m_Edition = Unknown; CoreInterface coreInterface; QString output; @@ -41,17 +44,18 @@ bool SubscriptionManager::activateSerial(const QString& serial, int& edition) catch (std::exception& e) { m_ErrorMessage = e.what(); + checkError(m_ErrorMessage); return false; } - edition = getEditionType(output); + checkOutput(output); return true; } -int SubscriptionManager::checkSubscription(int& edition) +bool SubscriptionManager::checkSubscription() { - edition = Unknown; + m_Edition = Unknown; CoreInterface coreInterface; QString output; try @@ -61,21 +65,13 @@ int SubscriptionManager::checkSubscription(int& edition) catch (std::exception& e) { m_ErrorMessage = e.what(); - - if (m_ErrorMessage.contains("subscription has expired")) { - return kExpired; - } - - return kInvalid; + checkError(m_ErrorMessage); + return false; } - if (output.contains("subscription will expire soon")) { - return kExpiredSoon; - } + checkOutput(output); - edition = getEditionType(output); - - return kValid; + return true; } bool SubscriptionManager::checkSubscriptionExist() @@ -86,17 +82,51 @@ bool SubscriptionManager::checkSubscriptionExist() return QFile::exists(subscriptionFilename); } -int SubscriptionManager::getEditionType(QString& string) +void SubscriptionManager::checkError(QString& error) { - if (string.contains("pro subscription valid")) { - return Pro; + if (error.contains("trial has expired")) { + QMessageBox::warning(m_pParent, tr("Subscription warning"), + tr("Your trial has expired. Click here to purchase")); } - else if (string.contains("basic subscription valid")) { - return Basic; + else { + QMessageBox::warning(m_pParent, tr("Subscription error"), + tr("An error occurred while trying to activate using a serial key. " + "Please contact the helpdesk, and provide the " + "following details.\n\n%1").arg(error)); + } +} + +void SubscriptionManager::checkOutput(QString& output) +{ + getEditionType(output); + checkExpiring(output); +} + +void SubscriptionManager::getEditionType(QString& output) +{ + if (output.contains("pro subscription valid")) { + m_Edition = Pro; + } + else if (output.contains("basic subscription valid")) { + m_Edition = Basic; + } + else if (output.contains("trial subscription valid")) { + m_Edition = Trial; + } +} + +void SubscriptionManager::checkExpiring(QString& output) +{ + if (output.contains("trial will end in")) { + QRegExp dayLeftRegex(".*trial will end in ([0-9]+) day.*"); + if (dayLeftRegex.exactMatch(output)) { + QString dayLeft = dayLeftRegex.cap(1); + + // TODO: warn user once a day + QMessageBox::warning(m_pParent, tr("Subscription warning"), + tr("Your trial will end in %1 %2. Click here to purchase") + .arg(dayLeft) + .arg(dayLeft == "1" ? "day" : "days")); + } } - else if (string.contains("trial subscription valid")) { - return Trial; - } - - return Unknown; } diff --git a/src/gui/src/SubscriptionManager.h b/src/gui/src/SubscriptionManager.h index 1af14ad6..bc272ef4 100644 --- a/src/gui/src/SubscriptionManager.h +++ b/src/gui/src/SubscriptionManager.h @@ -22,16 +22,21 @@ class SubscriptionManager : public QWidget { public: - SubscriptionManager(); + SubscriptionManager(QWidget* parent, int& edition); - bool activateSerial(const QString& serial, int& edition); - int checkSubscription(int& edition); + bool activateSerial(const QString& serial); + bool checkSubscription(); bool checkSubscriptionExist(); QString getLastError(){ return m_ErrorMessage; } private: - int getEditionType(QString& string); + void checkError(QString& error); + void checkOutput(QString& output); + void getEditionType(QString& output); + void checkExpiring(QString& output); private: QString m_ErrorMessage; + QWidget* m_pParent; + int& m_Edition; }; diff --git a/src/lib/synergy/SubscriptionManager.cpp b/src/lib/synergy/SubscriptionManager.cpp index a657a21e..78207dc2 100644 --- a/src/lib/synergy/SubscriptionManager.cpp +++ b/src/lib/synergy/SubscriptionManager.cpp @@ -151,17 +151,19 @@ SubscriptionManager::parsePlainSerial(const String& plainText, SubscriptionKey& sscanf(parts.at(6).c_str(), "%d", &key.m_warnTime); sscanf(parts.at(7).c_str(), "%d", &key.m_expireTime); - // TODO: use Arch time - if (time(0) > key.m_expireTime && - key.m_type == "trial") { - throw XSubscription(synergy::string::sprintf( - "%s subscription has expired", - key.m_type.c_str())); - } - else if (time(0) > key.m_warnTime && - key.m_type == "trial") { - LOG((CLOG_WARN "%s subscription will expire soon", - key.m_type.c_str())); + // only limit to trial version + if (key.m_type == "trial") { + if (time(0) > key.m_expireTime) { + throw XSubscription("trial has expired"); + } + else if (time(0) > key.m_warnTime) { + int secLeft = key.m_expireTime - static_cast(time(0)); + const int spd = 60 * 60 * 24; + int dayLeft = secLeft / spd + 1; + LOG((CLOG_NOTE "trial will end in %d %s", + dayLeft, + dayLeft == 1 ? "day" : "days")); + } } const char* userText = (key.m_userLimit == 1) ? "user" : "users";