#5657 Remove SerialKey::m_valid

This commit is contained in:
Andrew Nelless 2016-10-17 17:08:26 +01:00
parent c7dc198d82
commit 8b4d7abfb0
4 changed files with 934 additions and 950 deletions

View File

@ -15,60 +15,60 @@
#include <iostream> #include <iostream>
ActivationDialog::ActivationDialog(QWidget* parent, AppConfig& appConfig, ActivationDialog::ActivationDialog(QWidget* parent, AppConfig& appConfig,
SubscriptionManager& subscriptionManager) : SubscriptionManager& subscriptionManager) :
QDialog(parent), QDialog(parent),
ui(new Ui::ActivationDialog), ui(new Ui::ActivationDialog),
m_appConfig(&appConfig), m_appConfig(&appConfig),
m_subscriptionManager (&subscriptionManager) m_subscriptionManager (&subscriptionManager)
{ {
ui->setupUi(this); ui->setupUi(this);
refreshSerialKey(); refreshSerialKey();
} }
void ActivationDialog::refreshSerialKey() void ActivationDialog::refreshSerialKey()
{ {
ui->m_pTextEditSerialKey->setText(m_appConfig->serialKey()); ui->m_pTextEditSerialKey->setText(m_appConfig->serialKey());
ui->m_pTextEditSerialKey->setFocus(); ui->m_pTextEditSerialKey->setFocus();
ui->m_pTextEditSerialKey->moveCursor(QTextCursor::End); ui->m_pTextEditSerialKey->moveCursor(QTextCursor::End);
} }
ActivationDialog::~ActivationDialog() ActivationDialog::~ActivationDialog()
{ {
delete ui; delete ui;
} }
void ActivationDialog::reject() void ActivationDialog::reject()
{ {
if (m_subscriptionManager->activeEdition() == kUnregistered) { if (m_subscriptionManager->activeEdition() == kUnregistered) {
CancelActivationDialog cancelActivationDialog(this); CancelActivationDialog cancelActivationDialog(this);
if (QDialog::Accepted == cancelActivationDialog.exec()) { if (QDialog::Accepted == cancelActivationDialog.exec()) {
m_subscriptionManager->skipActivation(); m_subscriptionManager->skipActivation();
m_appConfig->activationHasRun(true); m_appConfig->activationHasRun(true);
m_appConfig->saveSettings(); m_appConfig->saveSettings();
} }
} }
QDialog::reject(); QDialog::reject();
} }
void ActivationDialog::accept() void ActivationDialog::accept()
{ {
QMessageBox message; QMessageBox message;
m_appConfig->activationHasRun(true); m_appConfig->activationHasRun(true);
m_appConfig->saveSettings(); m_appConfig->saveSettings();
std::pair<bool, QString> result; std::pair<bool, QString> result;
try { try {
QString serialKey = ui->m_pTextEditSerialKey->toPlainText(); QString serialKey = ui->m_pTextEditSerialKey->toPlainText();
result = m_subscriptionManager->setSerialKey(serialKey); result = m_subscriptionManager->setSerialKey(serialKey);
} }
catch (std::exception& e) { catch (std::exception& e) {
message.critical(this, "Unknown Error", message.critical(this, "Unknown Error",
tr("An error occurred while trying to activate Synergy. " tr("An error occurred while trying to activate Synergy. "
"Please contact the helpdesk, and provide the " "Please contact the helpdesk, and provide the "
"following information:\n\n%1").arg(e.what())); "following information:\n\n%1").arg(e.what()));
refreshSerialKey(); refreshSerialKey();
return; return;
} }
if (!result.first) { if (!result.first) {
message.critical(this, "Activation failed", message.critical(this, "Activation failed",
@ -77,11 +77,19 @@ void ActivationDialog::accept()
return; return;
} }
if (m_subscriptionManager->activeEdition() != kUnregistered) { Edition edition = m_subscriptionManager->activeEdition();
message.information(this, "Activated!", if (edition != kUnregistered) {
tr("Thanks for activating %1!").arg if (m_subscriptionManager->serialKey().isTrial()) {
(m_subscriptionManager->activeEditionName())); message.information(this, "Thanks!",
} tr("Thanks for trying %1!").arg
(m_subscriptionManager->getEditionName(edition)));
}
else {
message.information(this, "Activated!",
tr("Thanks for activating %1!").arg
(m_subscriptionManager->getEditionName(edition)));
}
}
QDialog::accept(); QDialog::accept();
} }

File diff suppressed because it is too large Load Diff

View File

@ -33,8 +33,7 @@ SerialKey::SerialKey(Edition edition):
m_warnTime(ULLONG_MAX), m_warnTime(ULLONG_MAX),
m_expireTime(ULLONG_MAX), m_expireTime(ULLONG_MAX),
m_edition(edition), m_edition(edition),
m_trial(false), m_trial(false)
m_valid(true)
{ {
} }
@ -43,46 +42,25 @@ SerialKey::SerialKey(std::string serial) :
m_warnTime(0), m_warnTime(0),
m_expireTime(0), m_expireTime(0),
m_edition(kBasic), m_edition(kBasic),
m_trial(true), m_trial(true)
m_valid(false)
{ {
string plainText = decode(serial); string plainText = decode(serial);
bool valid = false;
if (!plainText.empty()) { if (!plainText.empty()) {
parse(plainText); valid = parse(plainText);
} }
if (!m_valid) { if (!valid) {
throw std::runtime_error ("Invalid serial key"); throw std::runtime_error ("Invalid serial key");
} }
} }
bool
SerialKey::isValid(time_t currentTime) const
{
bool result = false;
if (m_valid) {
if (m_trial) {
if (currentTime < m_expireTime) {
result = true;
}
}
else {
result = true;
}
}
return result;
}
bool bool
SerialKey::isExpiring(time_t currentTime) const SerialKey::isExpiring(time_t currentTime) const
{ {
bool result = false; bool result = false;
if (m_valid) { if (m_warnTime <= currentTime && currentTime < m_expireTime) {
if (m_warnTime <= currentTime && currentTime < m_expireTime) { result = true;
result = true;
}
} }
return result; return result;
@ -93,12 +71,11 @@ SerialKey::isExpired(time_t currentTime) const
{ {
bool result = false; bool result = false;
if (m_valid) { if (m_expireTime <= currentTime) {
if (m_expireTime <= currentTime) { result = true;
result = true;
}
} }
return result; return result;
} }
@ -209,13 +186,13 @@ SerialKey::decode(const std::string& serial)
return output; return output;
} }
void bool
SerialKey::parse(std::string plainSerial) SerialKey::parse(std::string plainSerial)
{ {
string parityStart = plainSerial.substr(0, 1); string parityStart = plainSerial.substr(0, 1);
string parityEnd = plainSerial.substr(plainSerial.length() - 1, 1); string parityEnd = plainSerial.substr(plainSerial.length() - 1, 1);
m_valid = false; bool valid = false;
// check for parity chars { and }, record parity result, then remove them. // check for parity chars { and }, record parity result, then remove them.
if (parityStart == "{" && parityEnd == "}") { if (parityStart == "{" && parityEnd == "}") {
@ -247,7 +224,7 @@ SerialKey::parse(std::string plainSerial)
m_company = parts.at(5); m_company = parts.at(5);
sscanf(parts.at(6).c_str(), "%lld", &m_warnTime); sscanf(parts.at(6).c_str(), "%lld", &m_warnTime);
sscanf(parts.at(7).c_str(), "%lld", &m_expireTime); sscanf(parts.at(7).c_str(), "%lld", &m_expireTime);
m_valid = true; valid = true;
} }
else if ((parts.size() == 9) else if ((parts.size() == 9)
&& (parts.at(0).find("v2") != string::npos)) { && (parts.at(0).find("v2") != string::npos)) {
@ -260,9 +237,11 @@ SerialKey::parse(std::string plainSerial)
m_company = parts.at(6); m_company = parts.at(6);
sscanf(parts.at(7).c_str(), "%lld", &m_warnTime); sscanf(parts.at(7).c_str(), "%lld", &m_warnTime);
sscanf(parts.at(8).c_str(), "%lld", &m_expireTime); sscanf(parts.at(8).c_str(), "%lld", &m_expireTime);
m_valid = true; valid = true;
} }
} }
return valid;
} }
Edition Edition

View File

@ -31,7 +31,6 @@ public:
explicit SerialKey(Edition edition = kUnregistered); explicit SerialKey(Edition edition = kUnregistered);
explicit SerialKey(std::string serial); explicit SerialKey(std::string serial);
bool isValid(time_t currentTime) const;
bool isExpiring(time_t currentTime) const; bool isExpiring(time_t currentTime) const;
bool isExpired(time_t currentTime) const; bool isExpired(time_t currentTime) const;
bool isTrial() const; bool isTrial() const;
@ -44,7 +43,7 @@ public:
static Edition parseEdition(const std::string& editionStr); static Edition parseEdition(const std::string& editionStr);
private: private:
void parse(std::string plainSerial); bool parse(std::string plainSerial);
std::string editionString() const; std::string editionString() const;
#ifdef TEST_ENV #ifdef TEST_ENV
@ -67,7 +66,6 @@ private:
unsigned long long m_expireTime; unsigned long long m_expireTime;
Edition m_edition; Edition m_edition;
bool m_trial; bool m_trial;
bool m_valid;
}; };
@ -80,8 +78,7 @@ operator== (SerialKey const& lhs, SerialKey const& rhs) {
(lhs.m_warnTime == rhs.m_warnTime) && (lhs.m_warnTime == rhs.m_warnTime) &&
(lhs.m_expireTime == rhs.m_expireTime) && (lhs.m_expireTime == rhs.m_expireTime) &&
(lhs.m_edition == rhs.m_edition) && (lhs.m_edition == rhs.m_edition) &&
(lhs.m_trial == rhs.m_trial) && (lhs.m_trial == rhs.m_trial);
(lhs.m_valid == rhs.m_valid);
} }
inline bool inline bool