-
Notifications
You must be signed in to change notification settings - Fork 2
/
iqconfig.cpp
141 lines (124 loc) · 3.9 KB
/
iqconfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* This file is part of IQ Notifier.
*
* IQ Notifier is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* IQ Notifier is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IQ Notifier. If not, see <http://www.gnu.org/licenses/>.
*/
#include "iqconfig.h"
#include <qt5xdg/XdgDirs>
#include <QDir>
#include <QFile>
#include <QFileInfo>
namespace
{
// TODO: refactor; taken from here: https://gist.github.com/ssendeavour/7324701
static bool copyRecursively(const QString &srcFilePath,
const QString &tgtFilePath)
{
QFileInfo srcFileInfo(srcFilePath);
if (srcFileInfo.isDir()) {
QDir targetDir(tgtFilePath);
targetDir.cdUp();
if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
return false;
QDir sourceDir(srcFilePath);
QStringList fileNames = sourceDir.entryList(
QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot |
QDir::Hidden | QDir::System);
for (const QString &fileName : fileNames) {
const QString newSrcFilePath =
srcFilePath + QLatin1Char('/') + fileName;
const QString newTgtFilePath =
tgtFilePath + QLatin1Char('/') + fileName;
if (!copyRecursively(newSrcFilePath, newTgtFilePath))
return false;
}
} else {
if (!QFile::copy(srcFilePath, tgtFilePath)) // NOLINT
return false;
}
return true;
}
} // anonymouse namespace
IQConfig::IQConfig(const QString &category_, const QString &fileName_)
: category{category_.isEmpty() ? "" : category_ + '/'}, fileName{fileName_},
settings{std::make_unique<QSettings>(getConfigFileName(),
QSettings::IniFormat)}
{
}
QVariant IQConfig::value(const QString &key, const QVariant &defaultValue) const
{
return settings->value(category + key, defaultValue);
}
void IQConfig::setValue(const QString &key, const QVariant &value)
{
settings->setValue(category + key, value);
}
#define IQ_MACRO_STRING(S) IQ_MACRO_STRING__(S)
#define IQ_MACRO_STRING__(S) #S
QString IQConfig::applicationName()
{
return QStringLiteral(IQ_MACRO_STRING(IQ_APP_NAME));
}
QString IQConfig::configDir()
{
static auto configDir = XdgDirs::configHome() + '/' + applicationName();
return configDir;
}
QString IQConfig::applicationVersion()
{
return QStringLiteral(IQ_MACRO_STRING(IQ_VERSION));
}
#undef IQ_MACRO_STRING__
#undef IQ_MACRO_STRING
QString IQConfig::getConfigFileName() const
{
auto config = configDir() + '/' + fileName;
QFileInfo config_file{config};
if (config_file.exists()) {
if (!config_file.isFile())
throw std::runtime_error{config.toStdString() +
" is not a valid config file"};
} else {
QDir dir;
dir.mkpath(configDir());
if (config.contains("/themes/default/theme"))
copyThemesFromShare(
QString{config}.replace("/default/theme", ""));
else
copyConfigFileFromExample(config);
}
return config;
}
bool IQConfig::copyConfigFileFromExample(const QString &destination) const
{
auto config_example_path =
"/usr/share/" + applicationName() + '/' + fileName + ".example";
QFile config_example_file{config_example_path};
if (!config_example_file.exists())
return false;
return config_example_file.copy(destination);
}
bool IQConfig::copyThemesFromShare(const QString &destination) const
{
auto shareThemesPath = "/usr/share/" + applicationName() + "/themes";
return copyRecursively(shareThemesPath, destination);
}
IQConfigurable::IQConfigurable(const QString &name) : name_{name}, config{name_}
{
}
const QString &IQConfigurable::name() const { return name_; }
bool IQConfigurable::isEnabled() const
{
return config.value("enabled", false).toBool();
}