Rephrase and refactor serial warning message #4716

This commit is contained in:
Jerry (Xinyu Hou) 2015-11-17 14:17:00 -08:00
parent 18d23d6f89
commit f8e9047c36
5 changed files with 89 additions and 61 deletions

View File

@ -698,21 +698,18 @@ QString MainWindow::appPath(const QString& name)
bool MainWindow::serverArgs(QStringList& args, QString& app) bool MainWindow::serverArgs(QStringList& args, QString& app)
{ {
SubscriptionManager subscriptionManager; int edition;
SubscriptionManager subscriptionManager(this, edition);
if (subscriptionManager.checkSubscriptionExist()) if (subscriptionManager.checkSubscriptionExist())
{ {
int edition; if (!subscriptionManager.checkSubscription()) {
int state = subscriptionManager.checkSubscription(edition); return false;
}
else {
setEdition(edition);
}
}
if (state == kInvalid) {
return false;
}
else if (state == kExpired) {
QMessageBox::warning(this, tr("Subscription is expired"),
tr("Your subscription is expired. Please purchase."));
return false;
}
}
app = appPath(appConfig().synergysName()); app = appPath(appConfig().synergysName());

View File

@ -127,15 +127,9 @@ bool SetupWizard::validateCurrentPage()
} }
else { else {
// create subscription file in profile directory // create subscription file in profile directory
SubscriptionManager subscriptionManager; SubscriptionManager subscriptionManager(this, m_Edition);
bool r = subscriptionManager.activateSerial(m_pLineEditSerialKey->text(), m_Edition); if (!subscriptionManager.activateSerial(m_pLineEditSerialKey->text())) {
if (!r) { return false;
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;
} }
m_pPluginPage->setEdition(m_Edition); m_pPluginPage->setEdition(m_Edition);

View File

@ -24,13 +24,16 @@
#include <QMessageBox> #include <QMessageBox>
#include <QFile> #include <QFile>
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; CoreInterface coreInterface;
QString output; QString output;
@ -41,17 +44,18 @@ bool SubscriptionManager::activateSerial(const QString& serial, int& edition)
catch (std::exception& e) catch (std::exception& e)
{ {
m_ErrorMessage = e.what(); m_ErrorMessage = e.what();
checkError(m_ErrorMessage);
return false; return false;
} }
edition = getEditionType(output); checkOutput(output);
return true; return true;
} }
int SubscriptionManager::checkSubscription(int& edition) bool SubscriptionManager::checkSubscription()
{ {
edition = Unknown; m_Edition = Unknown;
CoreInterface coreInterface; CoreInterface coreInterface;
QString output; QString output;
try try
@ -61,21 +65,13 @@ int SubscriptionManager::checkSubscription(int& edition)
catch (std::exception& e) catch (std::exception& e)
{ {
m_ErrorMessage = e.what(); m_ErrorMessage = e.what();
checkError(m_ErrorMessage);
if (m_ErrorMessage.contains("subscription has expired")) { return false;
return kExpired;
} }
return kInvalid; checkOutput(output);
}
if (output.contains("subscription will expire soon")) { return true;
return kExpiredSoon;
}
edition = getEditionType(output);
return kValid;
} }
bool SubscriptionManager::checkSubscriptionExist() bool SubscriptionManager::checkSubscriptionExist()
@ -86,17 +82,51 @@ bool SubscriptionManager::checkSubscriptionExist()
return QFile::exists(subscriptionFilename); return QFile::exists(subscriptionFilename);
} }
int SubscriptionManager::getEditionType(QString& string) void SubscriptionManager::checkError(QString& error)
{ {
if (string.contains("pro subscription valid")) { if (error.contains("trial has expired")) {
return Pro; QMessageBox::warning(m_pParent, tr("Subscription warning"),
tr("Your trial has expired. Click <a href='https://synergy-project.org/account/'>here</a> to purchase"));
} }
else if (string.contains("basic subscription valid")) { else {
return Basic; 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));
} }
else if (string.contains("trial subscription valid")) {
return Trial;
} }
return Unknown; 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 <a href='https://synergy-project.org/account/'>here</a> to purchase")
.arg(dayLeft)
.arg(dayLeft == "1" ? "day" : "days"));
}
}
} }

View File

@ -22,16 +22,21 @@
class SubscriptionManager : public QWidget class SubscriptionManager : public QWidget
{ {
public: public:
SubscriptionManager(); SubscriptionManager(QWidget* parent, int& edition);
bool activateSerial(const QString& serial, int& edition); bool activateSerial(const QString& serial);
int checkSubscription(int& edition); bool checkSubscription();
bool checkSubscriptionExist(); bool checkSubscriptionExist();
QString getLastError(){ return m_ErrorMessage; } QString getLastError(){ return m_ErrorMessage; }
private: private:
int getEditionType(QString& string); void checkError(QString& error);
void checkOutput(QString& output);
void getEditionType(QString& output);
void checkExpiring(QString& output);
private: private:
QString m_ErrorMessage; QString m_ErrorMessage;
QWidget* m_pParent;
int& m_Edition;
}; };

View File

@ -151,17 +151,19 @@ SubscriptionManager::parsePlainSerial(const String& plainText, SubscriptionKey&
sscanf(parts.at(6).c_str(), "%d", &key.m_warnTime); sscanf(parts.at(6).c_str(), "%d", &key.m_warnTime);
sscanf(parts.at(7).c_str(), "%d", &key.m_expireTime); sscanf(parts.at(7).c_str(), "%d", &key.m_expireTime);
// TODO: use Arch time // only limit to trial version
if (time(0) > key.m_expireTime && if (key.m_type == "trial") {
key.m_type == "trial") { if (time(0) > key.m_expireTime) {
throw XSubscription(synergy::string::sprintf( throw XSubscription("trial has expired");
"%s subscription has expired", }
key.m_type.c_str())); else if (time(0) > key.m_warnTime) {
int secLeft = key.m_expireTime - static_cast<int>(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"));
} }
else if (time(0) > key.m_warnTime &&
key.m_type == "trial") {
LOG((CLOG_WARN "%s subscription will expire soon",
key.m_type.c_str()));
} }
const char* userText = (key.m_userLimit == 1) ? "user" : "users"; const char* userText = (key.m_userLimit == 1) ? "user" : "users";