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)
{
SubscriptionManager subscriptionManager;
int edition;
SubscriptionManager subscriptionManager(this, edition);
if (subscriptionManager.checkSubscriptionExist())
{
int edition;
int state = subscriptionManager.checkSubscription(edition);
if (!subscriptionManager.checkSubscription()) {
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());

View File

@ -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);

View File

@ -24,13 +24,16 @@
#include <QMessageBox>
#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;
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;
checkError(m_ErrorMessage);
return false;
}
return kInvalid;
}
checkOutput(output);
if (output.contains("subscription will expire soon")) {
return kExpiredSoon;
}
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 <a href='https://synergy-project.org/account/'>here</a> to purchase"));
}
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 <a href='https://synergy-project.org/account/'>here</a> to purchase")
.arg(dayLeft)
.arg(dayLeft == "1" ? "day" : "days"));
}
else if (string.contains("basic subscription valid")) {
return Basic;
}
else if (string.contains("trial subscription valid")) {
return Trial;
}
return Unknown;
}

View File

@ -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;
};

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(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()));
// 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<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";