This repository has been archived by the owner on Jan 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configmanager.cpp
291 lines (238 loc) · 9.17 KB
/
configmanager.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "configmanager.hpp"
#include <iostream>
#include <QCoreApplication>
#include <QStandardPaths>
#include <Utils/pathexpander.hpp>
ConfigManager::ConfigManager()
{
// app config location
this->m_configdir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QDir::separator() +
qApp->organizationName() + QDir::separator() +
qApp->applicationName() + QDir::separator();
// config file location
this->m_configfile = this->m_configdir + "conf.ini";
// load config file if available and possible
QFile configfile(this->m_configfile);
if (configfile.exists())
{
QFileInfo fileinfo(this->m_configfile);
if (fileinfo.isReadable())
this->loadSettings();
else this->loadDefaultSettings();
}
else
{
// attempt to create the config location
// if it fails use the default settings instead and don't create a QFileSystemWatcher object
QDir dir(this->m_configdir);
if (!dir.mkpath(dir.absolutePath()))
{
std::cerr << "\033[1;38;2;173;54;0mFATAL!\033[0m Unable to create or read the configuration directory at:\n"
" " << qUtf8Printable(this->m_configdir) << "\n"
"Check if you have the sufficient permissions to create or access this directory.\n"
"Continue with default settings...\n" << std::endl;
this->loadDefaultSettings();
}
else this->createDefaultConfigFile();
}
// expand environment variables in library.rootpath
this->m_config.put(this->keyStr(Key::LibRootPath),
expandPath(
QString::fromUtf8(
this->m_config.get<std::string>(this->keyStr(Key::LibRootPath)).c_str()
)
).toUtf8().constData()
);
}
ConfigManager::~ConfigManager()
{
this->m_configdir.clear();
this->m_configfile.clear();
this->m_config.clear();
}
QString ConfigManager::value(Key key) const
{
return QString::fromUtf8(this->m_config.get<std::string>(this->keyStr(key)).c_str());
}
bool ConfigManager::boolean(Key key) const
{
QString b = QString::fromUtf8(this->m_config.get<std::string>(this->keyStr(key)).c_str());
if (QString::compare(b, "true", Qt::CaseInsensitive) == 0 ||
QString::compare(b, "1") == 0)
{
b.clear();
return true;
}
b.clear();
return false;
}
QString ConfigManager::playerFor(const QString &fileformat) const
{
try
{
// player.(fileformat)_player
return QString::fromUtf8(this->m_config.get<std::string>(("player."+fileformat+"_player").toUtf8().constData()).c_str());
}
catch (...)
{
return QString();
}
}
QString ConfigManager::configDir() const
{
return this->m_configdir;
}
void ConfigManager::createDefaultConfigFile()
{
this->loadDefaultSettings();
try
{
boost::property_tree::ini_parser::write_ini(qUtf8Printable(this->m_configfile), this->m_config);
}
catch (...) { }
}
void ConfigManager::loadDefaultSettings()
{
#define BoostPtreePut(KeyName) \
this->m_config.put(this->keyStr(KeyName), this->keyDefaultValue(KeyName))
BoostPtreePut(Key::CmdAudio);
BoostPtreePut(Key::CmdVideo);
BoostPtreePut(Key::CmdModule);
BoostPtreePut(Key::CmdPlaylist);
BoostPtreePut(Key::CmdPlaylistFileLoad);
BoostPtreePut(Key::CmdSearch);
BoostPtreePut(Key::CmdBrowse);
BoostPtreePut(Key::CmdRandom);
BoostPtreePut(Key::CmdShuffle);
BoostPtreePut(Key::CmdRepeat);
BoostPtreePut(Key::CmdHistory);
BoostPtreePut(Key::CmdStatistics);
BoostPtreePut(Key::CmdExit);
BoostPtreePut(Key::CmdRescan);
BoostPtreePut(Key::LibRootPath);
BoostPtreePut(Key::LibAudioFormats);
BoostPtreePut(Key::LibVideoFormats);
BoostPtreePut(Key::LibModuleFormats);
BoostPtreePut(Key::LibPrefixDeletionPatterns);
BoostPtreePut(Key::PlayerAudio);
BoostPtreePut(Key::PlayerVideo);
BoostPtreePut(Key::PlayerModule);
BoostPtreePut(Key::ToolBrowser);
BoostPtreePut(Key::HistIgnore);
#undef BoostPtreePut
}
void ConfigManager::loadSettings()
{
try
{
boost::property_tree::ini_parser::read_ini(qUtf8Printable(this->m_configfile), this->m_config);
}
catch (...)
{
this->loadDefaultSettings();
return;
}
this->addIfMissing(Key::CmdAudio);
this->addIfMissing(Key::CmdVideo);
this->addIfMissing(Key::CmdModule);
this->addIfMissing(Key::CmdPlaylist);
this->addIfMissing(Key::CmdPlaylistFileLoad);
this->addIfMissing(Key::CmdSearch);
this->addIfMissing(Key::CmdBrowse);
this->addIfMissing(Key::CmdRandom);
this->addIfMissing(Key::CmdShuffle);
this->addIfMissing(Key::CmdRepeat);
this->addIfMissing(Key::CmdHistory);
this->addIfMissing(Key::CmdStatistics);
this->addIfMissing(Key::CmdExit);
this->addIfMissing(Key::CmdRescan);
this->addIfMissing(Key::LibRootPath);
this->addIfMissing(Key::LibAudioFormats);
this->addIfMissing(Key::LibVideoFormats);
this->addIfMissing(Key::LibModuleFormats);
this->addIfMissing(Key::LibPrefixDeletionPatterns);
this->addIfMissing(Key::PlayerAudio);
this->addIfMissing(Key::PlayerVideo);
this->addIfMissing(Key::PlayerModule);
this->addIfMissing(Key::ToolBrowser);
this->addIfMissing(Key::HistIgnore);
// try to write back the complete config file
try
{
boost::property_tree::ini_parser::write_ini(qUtf8Printable(this->m_configfile), this->m_config);
}
catch (...) { /* ignore errors */ }
}
std::string ConfigManager::keyStr(Key key)
{
switch (key)
{
case Key::CmdAudio: return "commands.audio"; break;
case Key::CmdVideo: return "commands.video"; break;
case Key::CmdModule: return "commands.module"; break;
case Key::CmdPlaylist: return "commands.playlist"; break;
case Key::CmdPlaylistFileLoad: return "commands.playlist_fileload"; break;
case Key::CmdSearch: return "commands.search"; break;
case Key::CmdBrowse: return "commands.browse"; break;
case Key::CmdRandom: return "commands.random"; break;
case Key::CmdShuffle: return "commands.shuffle"; break;
case Key::CmdRepeat: return "commands.repeat"; break;
case Key::CmdHistory: return "commands.history"; break;
case Key::CmdStatistics: return "commands.statistics"; break;
case Key::CmdExit: return "commands.exit"; break;
case Key::CmdRescan: return "commands.rescan"; break;
case Key::LibRootPath: return "library.rootpath"; break;
case Key::LibAudioFormats: return "library.audioformats"; break;
case Key::LibVideoFormats: return "library.videoformats"; break;
case Key::LibModuleFormats: return "library.moduleformats"; break;
case Key::LibPrefixDeletionPatterns: return "library.prefixdeletionpatterns"; break;
case Key::PlayerAudio: return "player.audioplayer"; break;
case Key::PlayerVideo: return "player.videoplayer"; break;
case Key::PlayerModule: return "player.modplayer"; break;
case Key::ToolBrowser: return "tools.browser"; break;
case Key::HistIgnore: return "history.histignore"; break;
}
return std::string();
}
std::string ConfigManager::keyDefaultValue(Key key)
{
switch (key)
{
case Key::CmdAudio: return "audio"; break;
case Key::CmdVideo: return "video"; break;
case Key::CmdModule: return "module"; break;
case Key::CmdPlaylist: return "playlist"; break;
case Key::CmdPlaylistFileLoad: return "load"; break;
case Key::CmdSearch: return "search"; break;
case Key::CmdBrowse: return "browse"; break;
case Key::CmdRandom: return "random"; break;
case Key::CmdShuffle: return "shuffle"; break;
case Key::CmdRepeat: return "repeat"; break;
case Key::CmdHistory: return "history"; break;
case Key::CmdStatistics: return "statistics"; break;
case Key::CmdExit: return "exit"; break;
case Key::CmdRescan: return "rescan"; break;
case Key::LibRootPath: return "$HOME"; break;
case Key::LibAudioFormats: return "wav, flac, tta, aiff, ape, pcm, alac, dts, m4a, ogg, mka, wma, asf, ra, aac, mp3"; break;
case Key::LibVideoFormats: return "mp4, h264, h263, ts, m2ts, mov, ogm, avi, bk2, bnk, mkv, wmv, rv"; break;
case Key::LibModuleFormats: return "xm, it, mod, med, sid, s3m"; break;
case Key::LibPrefixDeletionPatterns: return "Music/;Video/;Videos/"; break;
case Key::PlayerAudio: return "mplayer -novideo -really-quiet %f"; break;
case Key::PlayerVideo: return "mplayer -fs -really-quiet %f"; break;
case Key::PlayerModule: return "xmp %f"; break;
case Key::ToolBrowser: return "xdg-open"; break;
case Key::HistIgnore: return "statistics*;browse*;exit*;rescan*;history*;random;shuffle"; break;
}
return std::string();
}
void ConfigManager::addIfMissing(Key key)
{
try
{
(void) this->m_config.get<std::string>(this->keyStr(key));
}
catch (...)
{
this->m_config.put(this->keyStr(key), this->keyDefaultValue(key));
}
}