#5657 Rename edition to activeLicense. WSFs

This commit is contained in:
Andrew Nelless 2016-10-15 15:30:28 +01:00
parent 3b98a7b785
commit 4ad7c7fe39
7 changed files with 1317 additions and 1309 deletions

View File

@ -14,67 +14,67 @@
#include <QThread> #include <QThread>
#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->edition() == Edition::kUnregistered) { if (m_subscriptionManager->activeLicense() == Edition::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;
QString error; QString error;
m_appConfig->activationHasRun(true);
m_appConfig->saveSettings();
try { m_appConfig->activationHasRun(true);
QString serialKey = ui->m_pTextEditSerialKey->toPlainText(); m_appConfig->saveSettings();
m_subscriptionManager->setSerialKey(serialKey);
}
catch (std::exception& e) {
message.critical(this, "Unknown Error",
tr("An error occurred while trying to activate Synergy. "
"Please contact the helpdesk, and provide the "
"following details.\n\n%1").arg(e.what()));
refreshSerialKey();
return;
}
if (m_subscriptionManager->edition() != Edition::kUnregistered) { try {
message.information(this, "Activated!", QString serialKey = ui->m_pTextEditSerialKey->toPlainText();
tr("Thanks for activating %1!").arg m_subscriptionManager->setSerialKey(serialKey);
(getEditionName(m_subscriptionManager->edition()))); }
} catch (std::exception& e) {
QDialog::accept(); message.critical(this, "Unknown Error",
tr("An error occurred while trying to activate Synergy. "
"Please contact the helpdesk, and provide the "
"following details.\n\n%1").arg(e.what()));
refreshSerialKey();
return;
}
if (m_subscriptionManager->activeLicense() != Edition::kUnregistered) {
message.information(this, "Activated!",
tr("Thanks for activating %1!").arg
(getEditionName(m_subscriptionManager->activeLicense())));
}
QDialog::accept();
} }

File diff suppressed because it is too large Load Diff

View File

@ -62,174 +62,174 @@ class SubscriptionManager;
class MainWindow : public QMainWindow, public Ui::MainWindowBase class MainWindow : public QMainWindow, public Ui::MainWindowBase
{ {
Q_OBJECT Q_OBJECT
friend class QSynergyApplication; friend class QSynergyApplication;
friend class SetupWizard; friend class SetupWizard;
friend class ActivationDialog; friend class ActivationDialog;
friend class SettingsDialog; friend class SettingsDialog;
public:
enum qSynergyState
{
synergyDisconnected,
synergyConnecting,
synergyConnected,
synergyTransfering
};
enum qSynergyType public:
{ enum qSynergyState
synergyClient, {
synergyServer synergyDisconnected,
}; synergyConnecting,
synergyConnected,
synergyTransfering
};
enum qLevel { enum qSynergyType
Error, {
Info synergyClient,
}; synergyServer
};
enum qRuningState { enum qLevel {
kStarted, Error,
kStopped Info
}; };
public: enum qRuningState {
MainWindow(QSettings& settings, AppConfig& appConfig, kStarted,
SubscriptionManager& subscriptionManager); kStopped
~MainWindow(); };
public: public:
void setVisible(bool visible); MainWindow(QSettings& settings, AppConfig& appConfig,
int synergyType() const { return m_pGroupClient->isChecked() ? synergyClient : synergyServer; } SubscriptionManager& subscriptionManager);
int synergyState() const { return m_SynergyState; } ~MainWindow();
QString hostname() const { return m_pLineEditHostname->text(); }
QString configFilename();
QString address();
QString appPath(const QString& name);
void open();
void clearLog();
VersionChecker& versionChecker() { return m_VersionChecker; }
QString getScreenName();
ServerConfig& serverConfig() { return m_ServerConfig; }
void showConfigureServer(const QString& message);
void showConfigureServer() { showConfigureServer(""); }
void autoAddScreen(const QString name);
void updateZeroconfService();
void serverDetected(const QString name);
void updateLocalFingerprint();
SubscriptionManager& subscriptionManager() const;
public slots: public:
void setEdition(Edition edition); void setVisible(bool visible);
void beginTrial(); int synergyType() const { return m_pGroupClient->isChecked() ? synergyClient : synergyServer; }
void endTrial(); int synergyState() const { return m_SynergyState; }
void appendLogRaw(const QString& text); QString hostname() const { return m_pLineEditHostname->text(); }
void appendLogInfo(const QString& text); QString configFilename();
void appendLogDebug(const QString& text); QString address();
void appendLogError(const QString& text); QString appPath(const QString& name);
void startSynergy(); void open();
void clearLog();
VersionChecker& versionChecker() { return m_VersionChecker; }
QString getScreenName();
ServerConfig& serverConfig() { return m_ServerConfig; }
void showConfigureServer(const QString& message);
void showConfigureServer() { showConfigureServer(""); }
void autoAddScreen(const QString name);
void updateZeroconfService();
void serverDetected(const QString name);
void updateLocalFingerprint();
SubscriptionManager& subscriptionManager() const;
protected slots: public slots:
void sslToggled(bool enabled); void setEdition(Edition edition);
void on_m_pGroupClient_toggled(bool on); void beginTrial(bool isExpiring);
void on_m_pGroupServer_toggled(bool on); void endTrial(bool isExpired);
bool on_m_pButtonBrowseConfigFile_clicked(); void appendLogRaw(const QString& text);
void on_m_pButtonConfigureServer_clicked(); void appendLogInfo(const QString& text);
bool on_m_pActionSave_triggered(); void appendLogDebug(const QString& text);
void on_m_pActionAbout_triggered(); void appendLogError(const QString& text);
void on_m_pActionSettings_triggered(); void startSynergy();
void on_m_pActivate_triggered();
void synergyFinished(int exitCode, QProcess::ExitStatus);
void trayActivated(QSystemTrayIcon::ActivationReason reason);
void stopSynergy();
void logOutput();
void logError();
void updateFound(const QString& version);
void bonjourInstallFinished();
protected: protected slots:
QSettings& settings() { return m_Settings; } void sslToggled(bool enabled);
AppConfig& appConfig() { return *m_AppConfig; } void on_m_pGroupClient_toggled(bool on);
QProcess* synergyProcess() { return m_pSynergy; } void on_m_pGroupServer_toggled(bool on);
void setSynergyProcess(QProcess* p) { m_pSynergy = p; } bool on_m_pButtonBrowseConfigFile_clicked();
void initConnections(); void on_m_pButtonConfigureServer_clicked();
void createMenuBar(); bool on_m_pActionSave_triggered();
void createStatusBar(); void on_m_pActionAbout_triggered();
void createTrayIcon(); void on_m_pActionSettings_triggered();
void loadSettings(); void on_m_pActivate_triggered();
void saveSettings(); void synergyFinished(int exitCode, QProcess::ExitStatus);
void setIcon(qSynergyState state); void trayActivated(QSystemTrayIcon::ActivationReason reason);
void setSynergyState(qSynergyState state); void stopSynergy();
bool checkForApp(int which, QString& app); void logOutput();
bool clientArgs(QStringList& args, QString& app); void logError();
bool serverArgs(QStringList& args, QString& app); void updateFound(const QString& version);
void setStatus(const QString& status); void bonjourInstallFinished();
void sendIpcMessage(qIpcMessageType type, const char* buffer, bool showErrors);
void onModeChanged(bool startDesktop, bool applyService); protected:
void updateStateFromLogLine(const QString& line); QSettings& settings() { return m_Settings; }
QString getIPAddresses(); AppConfig& appConfig() { return *m_AppConfig; }
void stopService(); QProcess* synergyProcess() { return m_pSynergy; }
void stopDesktop(); void setSynergyProcess(QProcess* p) { m_pSynergy = p; }
void changeEvent(QEvent* event); void initConnections();
void retranslateMenuBar(); void createMenuBar();
void createStatusBar();
void createTrayIcon();
void loadSettings();
void saveSettings();
void setIcon(qSynergyState state);
void setSynergyState(qSynergyState state);
bool checkForApp(int which, QString& app);
bool clientArgs(QStringList& args, QString& app);
bool serverArgs(QStringList& args, QString& app);
void setStatus(const QString& status);
void sendIpcMessage(qIpcMessageType type, const char* buffer, bool showErrors);
void onModeChanged(bool startDesktop, bool applyService);
void updateStateFromLogLine(const QString& line);
QString getIPAddresses();
void stopService();
void stopDesktop();
void changeEvent(QEvent* event);
void retranslateMenuBar();
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
bool isServiceRunning(QString name); bool isServiceRunning(QString name);
#else #else
bool isServiceRunning(); bool isServiceRunning();
#endif #endif
bool isBonjourRunning(); bool isBonjourRunning();
void downloadBonjour(); void downloadBonjour();
void promptAutoConfig(); void promptAutoConfig();
QString getProfileRootForArg(); QString getProfileRootForArg();
void checkConnected(const QString& line); void checkConnected(const QString& line);
void checkFingerprint(const QString& line); void checkFingerprint(const QString& line);
bool autoHide(); bool autoHide();
QString getTimeStamp(); QString getTimeStamp();
void restartSynergy(); void restartSynergy();
void proofreadInfo(); void proofreadInfo();
void showEvent (QShowEvent*); void showEvent (QShowEvent*);
private: private:
QSettings& m_Settings; QSettings& m_Settings;
AppConfig* m_AppConfig; AppConfig* m_AppConfig;
SubscriptionManager* m_SubscriptionManager; SubscriptionManager* m_SubscriptionManager;
QProcess* m_pSynergy; QProcess* m_pSynergy;
int m_SynergyState; int m_SynergyState;
ServerConfig m_ServerConfig; ServerConfig m_ServerConfig;
QTemporaryFile* m_pTempConfigFile; QTemporaryFile* m_pTempConfigFile;
QSystemTrayIcon* m_pTrayIcon; QSystemTrayIcon* m_pTrayIcon;
QMenu* m_pTrayIconMenu; QMenu* m_pTrayIconMenu;
bool m_AlreadyHidden; bool m_AlreadyHidden;
VersionChecker m_VersionChecker; VersionChecker m_VersionChecker;
IpcClient m_IpcClient; IpcClient m_IpcClient;
QMenuBar* m_pMenuBar; QMenuBar* m_pMenuBar;
QMenu* m_pMenuFile; QMenu* m_pMenuFile;
QMenu* m_pMenuEdit; QMenu* m_pMenuEdit;
QMenu* m_pMenuWindow; QMenu* m_pMenuWindow;
QMenu* m_pMenuHelp; QMenu* m_pMenuHelp;
ZeroconfService* m_pZeroconfService; ZeroconfService* m_pZeroconfService;
DataDownloader* m_pDataDownloader; DataDownloader* m_pDataDownloader;
QMessageBox* m_DownloadMessageBox; QMessageBox* m_DownloadMessageBox;
QAbstractButton* m_pCancelButton; QAbstractButton* m_pCancelButton;
QMutex m_UpdateZeroconfMutex; QMutex m_UpdateZeroconfMutex;
bool m_SuppressAutoConfigWarning; bool m_SuppressAutoConfigWarning;
CommandProcess* m_BonjourInstall; CommandProcess* m_BonjourInstall;
bool m_SuppressEmptyServerWarning; bool m_SuppressEmptyServerWarning;
qRuningState m_ExpectedRunningState; qRuningState m_ExpectedRunningState;
QMutex m_StopDesktopMutex; QMutex m_StopDesktopMutex;
SslCertificate* m_pSslCertificate; SslCertificate* m_pSslCertificate;
private slots: private slots:
void on_m_pCheckBoxAutoConfig_toggled(bool checked); void on_m_pCheckBoxAutoConfig_toggled(bool checked);
void on_m_pComboServerList_currentIndexChanged(QString ); void on_m_pComboServerList_currentIndexChanged(QString );
void on_m_pButtonApply_clicked(); void on_m_pButtonApply_clicked();
void installBonjour(); void installBonjour();
void on_windowShown(); void on_windowShown();
signals: signals:
void windowShown(); void windowShown();
}; };
#endif #endif

View File

@ -22,75 +22,82 @@
#include <QThread> #include <QThread>
SubscriptionManager::SubscriptionManager(AppConfig* appConfig) : SubscriptionManager::SubscriptionManager(AppConfig* appConfig) :
m_AppConfig(appConfig), m_AppConfig(appConfig),
m_serialKey(appConfig->edition()) { m_serialKey(appConfig->edition()) {
try { try {
setSerialKey(m_AppConfig->serialKey()); setSerialKey(m_AppConfig->serialKey());
} catch (...) { } catch (...) {
m_AppConfig->setSerialKey(""); m_AppConfig->setSerialKey("");
} m_AppConfig->saveSettings();
}
} }
SerialKey SerialKey
SubscriptionManager::setSerialKey(QString serialKeyString) SubscriptionManager::setSerialKey(QString serialKeyString)
{ {
SerialKey serialKey (serialKeyString.toStdString()); SerialKey serialKey (serialKeyString.toStdString());
if (!serialKey.isValid (::time(0))) { if (!serialKey.isValid (::time(0))) {
throw std::runtime_error ("Invalid serial key"); throw std::runtime_error ("Invalid serial key");
} }
if (serialKey != m_serialKey) { if (serialKey != m_serialKey) {
using std::swap; using std::swap;
swap (serialKey, m_serialKey); swap (serialKey, m_serialKey);
m_AppConfig->setSerialKey (serialKeyString);
notifyActivation ("serial:" + serialKeyString);
emit serialKeyChanged (m_serialKey);
if (m_serialKey.edition() != serialKey.edition()) { m_AppConfig->setSerialKey (serialKeyString);
m_AppConfig->setEdition (m_serialKey.edition()); notifyActivation ("serial:" + serialKeyString);
emit editionChanged (m_serialKey.edition()); emit serialKeyChanged (m_serialKey);
}
if (m_serialKey.isTrial() != serialKey.isTrial()) { if (m_serialKey.edition() != serialKey.edition()) {
if (m_serialKey.isTrial()) { m_AppConfig->setEdition (m_serialKey.edition());
emit beginTrial(); emit editionChanged (m_serialKey.edition());
} else { }
emit endTrial();
}
}
}
return serialKey; if (serialKey.isTrial()) {
emit endTrial(false);
}
if (m_serialKey.isTrial()) {
emit beginTrial(m_serialKey.isExpiring(::time(0)));
}
m_AppConfig->saveSettings();
}
return serialKey;
} }
Edition SubscriptionManager::edition() const Edition SubscriptionManager::activeLicense() const
{ {
return m_serialKey.edition(); return m_serialKey.edition();
} }
bool SubscriptionManager::isTrial() const void SubscriptionManager::update() const
{ {
return m_serialKey.isTrial(); emit serialKeyChanged (m_serialKey);
emit editionChanged (m_serialKey.edition());
if (m_serialKey.isTrial()) {
emit beginTrial(m_serialKey.isExpiring(::time(0)));
}
} }
void SubscriptionManager::skipActivation() void SubscriptionManager::skipActivation()
{ {
notifyActivation ("skip:unknown"); notifyActivation ("skip:unknown");
} }
void SubscriptionManager::notifyActivation(QString identity) void SubscriptionManager::notifyActivation(QString identity)
{ {
ActivationNotifier* notifier = new ActivationNotifier(); ActivationNotifier* notifier = new ActivationNotifier();
notifier->setIdentity(identity); notifier->setIdentity(identity);
QThread* thread = new QThread(); QThread* thread = new QThread();
connect(notifier, SIGNAL(finished()), thread, SLOT(quit())); connect(notifier, SIGNAL(finished()), thread, SLOT(quit()));
connect(notifier, SIGNAL(finished()), notifier, SLOT(deleteLater())); connect(notifier, SIGNAL(finished()), notifier, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
notifier->moveToThread(thread); notifier->moveToThread(thread);
thread->start(); thread->start();
QMetaObject::invokeMethod(notifier, "notify", Qt::QueuedConnection); QMetaObject::invokeMethod(notifier, "notify", Qt::QueuedConnection);
} }

View File

@ -25,25 +25,25 @@ class AppConfig;
class SubscriptionManager: public QObject class SubscriptionManager: public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
SubscriptionManager(AppConfig* appConfig); SubscriptionManager(AppConfig* appConfig);
SerialKey setSerialKey(QString serialKey); SerialKey setSerialKey(QString serialKey);
Edition edition() const; void update() const;
bool isTrial() const; Edition activeLicense() const;
void skipActivation(); void skipActivation();
private: private:
void notifyActivation(QString identity); void notifyActivation(QString identity);
private: private:
AppConfig* m_AppConfig; AppConfig* m_AppConfig;
SerialKey m_serialKey; SerialKey m_serialKey;
signals: signals:
void serialKeyChanged (SerialKey); void serialKeyChanged (SerialKey) const;
void editionChanged (Edition); void editionChanged (Edition) const;
void beginTrial (); void beginTrial (bool expiring) const;
void endTrial (); void endTrial (bool expired) const;
}; };

View File

@ -1,11 +1,11 @@
/* /*
* synergy -- mouse and keyboard sharing utility * synergy -- mouse and keyboard sharing utility
* Copyright (C) 2016 Symless Ltd. * Copyright (C) 2016 Symless Ltd.
* *
* This package is free software; you can redistribute it and/or * This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file. * found in the file LICENSE that should have accompanied this file.
* *
* This package is distributed in the hope that it will be useful, * This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -26,194 +26,194 @@
using namespace std; using namespace std;
SerialKey::SerialKey(Edition edition): SerialKey::SerialKey(Edition edition):
m_userLimit(1), m_userLimit(1),
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) m_valid(true)
{ {
} }
SerialKey::SerialKey(std::string serial) : SerialKey::SerialKey(std::string serial) :
m_userLimit(1), m_userLimit(1),
m_warnTime(0), m_warnTime(0),
m_expireTime(0), m_expireTime(0),
m_edition(Edition::kBasic), m_edition(Edition::kBasic),
m_trial(true), m_trial(true),
m_valid(false) m_valid(false)
{ {
string plainText = decode(serial); string plainText = decode(serial);
if (!plainText.empty()) { if (!plainText.empty()) {
parse(plainText); parse(plainText);
} }
} }
bool bool
SerialKey::isValid(time_t currentTime) const SerialKey::isValid(time_t currentTime) const
{ {
bool result = false; bool result = false;
if (m_valid) { if (m_valid) {
if (m_trial) { if (m_trial) {
if (currentTime < m_expireTime) { if (currentTime < m_expireTime) {
result = true; result = true;
} }
} }
else { else {
result = true; result = true;
} }
} }
return result; 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_valid) {
if (m_warnTime <= currentTime && currentTime < m_expireTime) { if (m_warnTime <= currentTime && currentTime < m_expireTime) {
result = true; result = true;
} }
} }
return result; return result;
} }
bool bool
SerialKey::isExpired(time_t currentTime) const SerialKey::isExpired(time_t currentTime) const
{ {
bool result = false; bool result = false;
if (m_valid) { if (m_valid) {
if (m_expireTime <= currentTime) { if (m_expireTime <= currentTime) {
result = true; result = true;
} }
} }
return result; return result;
} }
bool bool
SerialKey::isTrial() const SerialKey::isTrial() const
{ {
return m_trial; return m_trial;
} }
Edition Edition
SerialKey::edition() const SerialKey::edition() const
{ {
return m_edition; return m_edition;
} }
time_t time_t
SerialKey::daysLeft(time_t currentTime) const SerialKey::daysLeft(time_t currentTime) const
{ {
unsigned long long timeLeft = 0; unsigned long long timeLeft = 0;
unsigned long long const day = 60 * 60 * 24; unsigned long long const day = 60 * 60 * 24;
if (currentTime < m_expireTime) {
timeLeft = m_expireTime - currentTime;
}
unsigned long long dayLeft = 0; if (currentTime < m_expireTime) {
dayLeft = timeLeft % day != 0 ? 1 : 0; timeLeft = m_expireTime - currentTime;
}
return timeLeft / day + dayLeft;
unsigned long long dayLeft = 0;
dayLeft = timeLeft % day != 0 ? 1 : 0;
return timeLeft / day + dayLeft;
} }
std::string std::string
SerialKey::decode(const std::string& serial) const SerialKey::decode(const std::string& serial) const
{ {
static const char* const lut = "0123456789ABCDEF"; static const char* const lut = "0123456789ABCDEF";
string output; string output;
size_t len = serial.length(); size_t len = serial.length();
if (len & 1) { if (len & 1) {
return output; return output;
} }
output.reserve(len / 2); output.reserve(len / 2);
for (size_t i = 0; i < len; i += 2) { for (size_t i = 0; i < len; i += 2) {
char a = serial[i]; char a = serial[i];
char b = serial[i + 1]; char b = serial[i + 1];
const char* p = std::lower_bound(lut, lut + 16, a); const char* p = std::lower_bound(lut, lut + 16, a);
const char* q = std::lower_bound(lut, lut + 16, b); const char* q = std::lower_bound(lut, lut + 16, b);
if (*q != b || *p != a) { if (*q != b || *p != a) {
return output; return output;
} }
output.push_back(static_cast<char>(((p - lut) << 4) | (q - lut))); output.push_back(static_cast<char>(((p - lut) << 4) | (q - lut)));
} }
return output; return output;
} }
void void
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);
// check for parity chars { and }, record parity result, then remove them.
if (parityStart == "{" && parityEnd == "}") {
plainSerial = plainSerial.substr(1, plainSerial.length() - 2);
// tokenize serialised subscription.
vector<string> parts;
std::string::size_type pos = 0;
bool look = true;
while (look) {
std::string::size_type start = pos;
pos = plainSerial.find(";", pos);
if (pos == string::npos) {
pos = plainSerial.length();
look = false;
}
parts.push_back(plainSerial.substr(start, pos - start));
pos += 1;
}
if ((parts.size() == 8) // check for parity chars { and }, record parity result, then remove them.
&& (parts.at(0).find("v1") != string::npos)) { if (parityStart == "{" && parityEnd == "}") {
// e.g.: {v1;basic;Bob;1;email;company name;1398297600;1398384000} plainSerial = plainSerial.substr(1, plainSerial.length() - 2);
m_edition = getEdition(parts.at(1));
m_name = parts.at(2); // tokenize serialised subscription.
m_trial = false; vector<string> parts;
sscanf(parts.at(3).c_str(), "%d", &m_userLimit); std::string::size_type pos = 0;
m_email = parts.at(4); bool look = true;
m_company = parts.at(5); while (look) {
sscanf(parts.at(6).c_str(), "%lld", &m_warnTime); std::string::size_type start = pos;
sscanf(parts.at(7).c_str(), "%lld", &m_expireTime); pos = plainSerial.find(";", pos);
m_valid = true; if (pos == string::npos) {
} pos = plainSerial.length();
else if ((parts.size() == 9) look = false;
&& (parts.at(0).find("v2") != string::npos)) { }
// e.g.: {v2;trial;basic;Bob;1;email;company name;1398297600;1398384000} parts.push_back(plainSerial.substr(start, pos - start));
m_trial = parts.at(1) == "trial" ? true : false; pos += 1;
m_edition = getEdition(parts.at(2)); }
m_name = parts.at(3);
sscanf(parts.at(4).c_str(), "%d", &m_userLimit); if ((parts.size() == 8)
m_email = parts.at(5); && (parts.at(0).find("v1") != string::npos)) {
m_company = parts.at(6); // e.g.: {v1;basic;Bob;1;email;company name;1398297600;1398384000}
sscanf(parts.at(7).c_str(), "%lld", &m_warnTime); m_edition = parseEdition(parts.at(1));
sscanf(parts.at(8).c_str(), "%lld", &m_expireTime); m_name = parts.at(2);
m_valid = true; m_trial = false;
} sscanf(parts.at(3).c_str(), "%d", &m_userLimit);
} m_email = parts.at(4);
m_company = parts.at(5);
sscanf(parts.at(6).c_str(), "%lld", &m_warnTime);
sscanf(parts.at(7).c_str(), "%lld", &m_expireTime);
m_valid = true;
}
else if ((parts.size() == 9)
&& (parts.at(0).find("v2") != string::npos)) {
// e.g.: {v2;trial;basic;Bob;1;email;company name;1398297600;1398384000}
m_trial = parts.at(1) == "trial" ? true : false;
m_edition = parseEdition(parts.at(2));
m_name = parts.at(3);
sscanf(parts.at(4).c_str(), "%d", &m_userLimit);
m_email = parts.at(5);
m_company = parts.at(6);
sscanf(parts.at(7).c_str(), "%lld", &m_warnTime);
sscanf(parts.at(8).c_str(), "%lld", &m_expireTime);
m_valid = true;
}
}
} }
Edition Edition
SerialKey::getEdition(std::string editionStr) SerialKey::parseEdition(std::string editionStr)
{ {
Edition e = Edition::kBasic; Edition e = Edition::kBasic;
if (editionStr == "pro") { if (editionStr == "pro") {
e = Edition::kPro; e = Edition::kPro;
} }
return e; return e;
} }

View File

@ -1,11 +1,11 @@
/* /*
* synergy -- mouse and keyboard sharing utility * synergy -- mouse and keyboard sharing utility
* Copyright (C) 2016 Symless Ltd. * Copyright (C) 2016 Symless Ltd.
* *
* This package is free software; you can redistribute it and/or * This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file. * found in the file LICENSE that should have accompanied this file.
* *
* This package is distributed in the hope that it will be useful, * This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -26,61 +26,61 @@
#endif #endif
class SerialKey { class SerialKey {
friend bool operator== (SerialKey const&, SerialKey const&); friend bool operator== (SerialKey const&, SerialKey const&);
public: public:
explicit SerialKey(Edition edition = Edition::kUnregistered); explicit SerialKey(Edition edition = Edition::kUnregistered);
explicit SerialKey(std::string serial); explicit SerialKey(std::string serial);
bool isValid(time_t currentTime) const; 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;
time_t daysLeft(time_t currentTime) const; time_t daysLeft(time_t currentTime) const;
Edition edition() const; Edition edition() const;
private: private:
std::string decode(const std::string& serial) const; std::string decode(const std::string& serial) const;
void parse(std::string plainSerial); void parse(std::string plainSerial);
Edition getEdition(std::string editionStr); Edition parseEdition(std::string editionStr);
#ifdef TEST_ENV #ifdef TEST_ENV
private: private:
FRIEND_TEST(SerialKeyTests, decode_empty_returnEmptyString); FRIEND_TEST(SerialKeyTests, decode_empty_returnEmptyString);
FRIEND_TEST(SerialKeyTests, decode_invalidDigit_returnEmptyString); FRIEND_TEST(SerialKeyTests, decode_invalidDigit_returnEmptyString);
FRIEND_TEST(SerialKeyTests, decode_validSerial_returnPlainText); FRIEND_TEST(SerialKeyTests, decode_validSerial_returnPlainText);
FRIEND_TEST(SerialKeyTests, parse_noParty_invalid); FRIEND_TEST(SerialKeyTests, parse_noParty_invalid);
FRIEND_TEST(SerialKeyTests, parse_invalidPartsLenghth_invalid); FRIEND_TEST(SerialKeyTests, parse_invalidPartsLenghth_invalid);
FRIEND_TEST(SerialKeyTests, parse_validV1Serial_valid); FRIEND_TEST(SerialKeyTests, parse_validV1Serial_valid);
FRIEND_TEST(SerialKeyTests, parse_validV2Serial_valid); FRIEND_TEST(SerialKeyTests, parse_validV2Serial_valid);
#endif #endif
private: private:
std::string m_name; std::string m_name;
std::string m_email; std::string m_email;
std::string m_company; std::string m_company;
unsigned m_userLimit; unsigned m_userLimit;
unsigned long long m_warnTime; unsigned long long m_warnTime;
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; bool m_valid;
}; };
inline bool inline bool
operator== (SerialKey const& lhs, SerialKey const& rhs) { operator== (SerialKey const& lhs, SerialKey const& rhs) {
return (lhs.m_name == rhs.m_name) && return (lhs.m_name == rhs.m_name) &&
(lhs.m_email == rhs.m_email) && (lhs.m_email == rhs.m_email) &&
(lhs.m_company == rhs.m_company) && (lhs.m_company == rhs.m_company) &&
(lhs.m_userLimit == rhs.m_userLimit) && (lhs.m_userLimit == rhs.m_userLimit) &&
(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); (lhs.m_valid == rhs.m_valid);
} }
inline bool inline bool
operator!= (SerialKey const& lhs, SerialKey const& rhs) { operator!= (SerialKey const& lhs, SerialKey const& rhs) {
return !(lhs == rhs); return !(lhs == rhs);
} }