Skip to content

Commit cbff648

Browse files
committed
feat: introduce class SystemConfig
1 parent ea1bada commit cbff648

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

src/gui/newaccountwizard/urlpagecontroller.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "networkadapters/determineauthtypeadapter.h"
1919
#include "networkadapters/discoverwebfingerserviceadapter.h"
2020
#include "networkadapters/resolveurladapter.h"
21+
#include "systemconfig.h"
2122
#include "theme.h"
2223

2324
#include <QLabel>
@@ -44,10 +45,8 @@ UrlPageController::UrlPageController(QWizardPage *page, AccessManager *accessMan
4445
QString serverUrl = Theme::instance()->overrideServerUrlV2();
4546
if (serverUrl.isEmpty()) {
4647
// respect global pre-configuration
47-
// TODO: move to own class or make it part of ConfigFile?
48-
const QSettings settings(QSettings::SystemScope, this);
49-
allowServerUrlChange = settings.value("Setup/AllowServerUrlChange", true).toBool();
50-
serverUrl = settings.value("Setup/ServerUrl", QString()).toString();
48+
allowServerUrlChange = SystemConfig::allowServerUrlChange();
49+
serverUrl = SystemConfig::serverUrl();
5150
}
5251

5352
// no server url was given by any means, so the user has to provide one

src/libsync/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ set(libsync_SRCS
5252
abstractcorejob.cpp
5353

5454
appprovider.cpp
55+
systemconfig.cpp
5556
)
5657

5758
if(WIN32)

src/libsync/systemconfig.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
#include "common/asserts.h"
3+
#include "common/utility.h"
4+
#include "systemconfig.h"
5+
#include "theme.h"
6+
7+
#include <QFile>
8+
#include <QSettings>
9+
#include <QOperatingSystemVersion>
10+
11+
namespace OCC {
12+
13+
namespace chrono = std::chrono;
14+
15+
QVariant SystemConfig::value(QAnyStringView key, const QVariant &defaultValue)
16+
{
17+
auto format = Utility::isWindows() ? QSettings::NativeFormat : QSettings::IniFormat;
18+
QSettings system(configPath(QOperatingSystemVersion::currentType(), *Theme::instance()), format);
19+
20+
return system.value(key, defaultValue);
21+
}
22+
QString SystemConfig::configPath(const QOperatingSystemVersion::OSType& os, const Theme& theme)
23+
{
24+
if (os == QOperatingSystemVersion::Windows) {
25+
return QString("(HKEY_LOCAL_MACHINE\\Software\\%1\\%2)").arg(theme.vendor(), theme.appNameGUI());
26+
}
27+
if (os == QOperatingSystemVersion::MacOS) {
28+
return QString("/Library/Preferences/%1.ini").arg(theme.orgDomainName());
29+
}
30+
31+
return QString("/etc/%1/%1.ini").arg(theme.appName());
32+
}
33+
34+
bool SystemConfig::allowServerUrlChange()
35+
{
36+
return value("Setup/AllowServerUrlChange", true).toBool();
37+
}
38+
39+
QString SystemConfig::serverUrl()
40+
{
41+
return value("Setup/ServerUrl", QString()).toString();
42+
}
43+
}

src/libsync/systemconfig.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#pragma once
3+
4+
#include "owncloudlib.h"
5+
#include "theme.h"
6+
7+
#include <QOperatingSystemVersion>
8+
#include <QString>
9+
#include <QVariant>
10+
11+
12+
namespace OCC {
13+
14+
/**
15+
* @brief The SystemConfig class
16+
* @ingroup libsync
17+
*/
18+
class OWNCLOUDSYNC_EXPORT SystemConfig
19+
{
20+
public:
21+
// Access system configuration
22+
static bool allowServerUrlChange();
23+
static QString serverUrl();
24+
25+
// General purpose function
26+
static QVariant value(QAnyStringView key, const QVariant &defaultValue);
27+
static QString configPath(const QOperatingSystemVersion::OSType& os, const Theme& theme);
28+
29+
};
30+
}

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ endif()
2222
owncloud_add_test(ExcludedFiles)
2323

2424
owncloud_add_test(Utility)
25+
owncloud_add_test(SystemConfig ../src/libsync/owncloudtheme.cpp)
2526

2627
owncloud_add_test(SyncEngine)
2728
owncloud_add_test(SyncMove)

test/testsystemconfig.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#include <QtTest>
4+
5+
#include "testutils/testutils.h"
6+
7+
#include "libsync/owncloudtheme.h"
8+
#include "libsync/systemconfig.h"
9+
10+
class TestSystemConfig : public QObject
11+
{
12+
Q_OBJECT
13+
14+
private Q_SLOTS:
15+
void testConfigPath()
16+
{
17+
auto t = OCC::ownCloudTheme();
18+
QCOMPARE(OCC::SystemConfig::configPath(QOperatingSystemVersion::Windows, t), QString("(HKEY_LOCAL_MACHINE\\Software\\ownCloud\\Desktop)"));
19+
QCOMPARE(OCC::SystemConfig::configPath(QOperatingSystemVersion::MacOS, t), QString("/Library/Preferences/com.owncloud.desktop.ini"));
20+
QCOMPARE(OCC::SystemConfig::configPath(QOperatingSystemVersion::Unknown, t), QString("/etc/owncloud/owncloud.ini"));
21+
}
22+
};
23+
24+
QTEST_GUILESS_MAIN(TestSystemConfig)
25+
#include "testsystemconfig.moc"

0 commit comments

Comments
 (0)