Skip to content

Commit fd942ba

Browse files
authored
Load and save serial key from system and user scopes (symless#35)
* Refactor LicenseSettings to manage user and system settings for serial key * Use QSettings include * Remove unused constants from LicenseSettings * Fix formatting of debug output in LicenseSettings constructor * Remove redundant QSettings include from LicenseSettings header * Fix inheritance declaration for LicenseSettings class * Pass 'this' pointer to QSettings constructor in LicenseSettings for proper parent-child relationship
1 parent 93909df commit fd942ba

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/lib/synergy/gui/license/LicenseSettings.cpp

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,76 @@
1717

1818
#include "LicenseSettings.h"
1919

20+
#include <QSettings>
21+
#include <QtCore>
22+
2023
namespace synergy::gui::license {
2124

22-
void LicenseSettings::load() { m_serialKey = value("serialKey").toString(); }
25+
const auto kSerialKeySettingKey = "serialKey";
26+
27+
#if defined(Q_OS_UNIX)
28+
const auto kUnixSystemConfigPath = "/usr/local/etc/";
29+
#endif
30+
31+
QString getSystemSettingsBaseDir();
32+
33+
LicenseSettings::LicenseSettings() {
34+
m_pUserSettings = new QSettings(QSettings::Scope::UserScope, this);
35+
qDebug().noquote() << "user license settings path:"
36+
<< m_pUserSettings->fileName();
37+
38+
QSettings::setPath(
39+
QSettings::Format::IniFormat, QSettings::Scope::SystemScope,
40+
getSystemSettingsBaseDir());
41+
m_pSystemSettings = new QSettings(
42+
QSettings::Format::IniFormat, QSettings::Scope::SystemScope,
43+
QCoreApplication::organizationName(), QCoreApplication::applicationName(),
44+
this);
45+
qDebug().noquote() << "system license settings path:"
46+
<< m_pSystemSettings->fileName();
47+
}
48+
49+
void LicenseSettings::load() {
50+
if (m_pSystemSettings->contains(kSerialKeySettingKey)) {
51+
qDebug("loading serial key from system settings");
52+
m_serialKey = m_pSystemSettings->value(kSerialKeySettingKey).toString();
53+
} else if (m_pUserSettings->contains(kSerialKeySettingKey)) {
54+
qDebug("loading serial key from user settings");
55+
m_serialKey = m_pUserSettings->value(kSerialKeySettingKey).toString();
56+
} else {
57+
qDebug("no serial key found in settings");
58+
}
59+
}
2360

2461
void LicenseSettings::save() {
25-
setValue("serialKey", m_serialKey);
26-
sync();
62+
63+
if (m_pSystemSettings->isWritable()) {
64+
qDebug("saving serial key to system settings");
65+
m_pSystemSettings->setValue(kSerialKeySettingKey, m_serialKey);
66+
m_pSystemSettings->sync();
67+
} else {
68+
qWarning("not saving serial key to system settings, not writable");
69+
}
70+
71+
qDebug("saving serial key to user settings");
72+
m_pUserSettings->setValue(kSerialKeySettingKey, m_serialKey);
73+
m_pUserSettings->sync();
74+
}
75+
76+
/**
77+
* Important: Cloned from upstream `QSettingsProxy.cpp`
78+
*/
79+
QString getSystemSettingsBaseDir() {
80+
#if defined(Q_OS_WIN)
81+
return QCoreApplication::applicationDirPath();
82+
#elif defined(Q_OS_UNIX)
83+
// Qt already adds application and filename to the end of the path.
84+
// On macOS, it would be nice to use /Library dir, but qt has no elevate
85+
// system.
86+
return kUnixSystemConfigPath;
87+
#else
88+
#error "unsupported platform"
89+
#endif
2790
}
2891

2992
} // namespace synergy::gui::license

src/lib/synergy/gui/license/LicenseSettings.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
namespace synergy::gui::license {
2424

25-
class LicenseSettings : public QSettings {
25+
class LicenseSettings : public QObject {
26+
Q_OBJECT
2627
public:
27-
LicenseSettings() = default;
28+
LicenseSettings();
2829
virtual ~LicenseSettings() = default;
2930
void load();
3031
void save();
@@ -34,6 +35,8 @@ class LicenseSettings : public QSettings {
3435

3536
private:
3637
QString m_serialKey;
38+
QSettings *m_pUserSettings;
39+
QSettings *m_pSystemSettings;
3740
};
3841

3942
} // namespace synergy::gui::license

0 commit comments

Comments
 (0)