-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.cpp
226 lines (179 loc) · 5.77 KB
/
configuration.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
#include <QFileInfo>
#include <QSettings>
#include <QReadWriteLock>
#include <QDebug>
#include <QStringList>
#include <QPluginLoader>
#include <QPair>
#include "configuration.h"
#include "logging.h"
QHash<QString, QVariant> Configuration::conf;
QHash<QString, QPair<QString, IPlugin*> > Configuration::plugins;
Configuration::Configuration(const QString &iniPath) : settingsPath(iniPath)
{
}
Configuration::~Configuration()
{
}
QReadWriteLock lock;
bool Configuration::read()
{
QFileInfo info(settingsPath);
if(!info.isReadable()){
return false;
}
QHash<QString, QString> confPlugins;
QSettings settings(settingsPath, QSettings::IniFormat);
qDebug() << "Settings filename:" << settings.fileName();
qDebug() << "Settings read:";
lock.lockForWrite();
foreach(const QString key, settings.allKeys()){
if(0 == key.indexOf("plugins/")){
//insert the URL (from which clients will communicate with the plugin) as the key
//and the name of the library as the value
confPlugins.insert(settings.value(key).toString(), key.right(key.size() - key.indexOf("/") - 1));
}
else if(-1 != key.indexOf("/")){
QString new_key = key.right(key.size() - key.indexOf("/") - 1);
QString section = key.left(key.indexOf("/"));
if(!conf.contains(new_key)){
conf[new_key] = QHash<QString, QVariant>();
}
QHash<QString, QVariant> t(conf[section].toHash());
t.insert(new_key, dereference(settings.value(key).toString(), settings));
conf[section] = t;
qDebug() << section << "/" << new_key << ":" << conf[section].toHash()[new_key];
qDebug() <<"to be:" << key << ":" << settings.value(key).toString()
<< "(" << dereference(settings.value(key).toString(), settings) << ")";
}
else{
conf[key] = settings.value(key);
qDebug() << key << ":" << conf[key];
}
}
if(!confPlugins.isEmpty()){
loadPlugins(confPlugins);
qDebug() << "Plugins read from config:" << plugins;
}
lock.unlock();
return true;
}
QVariant Configuration::get(const QString &key, QVariant defaultValue)
{
return conf.value(key, defaultValue);
}
QStringList Configuration::getPluginKeys()
{
return plugins.keys();
}
QPair<QString, IPlugin *> Configuration::getPluginInfo(const QString &key)
{
if(plugins.contains(key)){
return plugins[key];
}
return QPair<QString, IPlugin *>();
}
QString Configuration::getSettingsPath() const
{
QFileInfo info(settingsPath);
return info.absoluteFilePath();
}
bool Configuration::check() const
{
bool ok = true;
QFileInfo info(get("documentroot").toString());
if(!info.isReadable()){
QByteArray msg;
msg = info.absoluteFilePath().toLocal8Bit();
syslog(LOG_ERR, "The documentroot path (%s) is not readable!", msg.constData());
ok = false;
}
info.setFile(get("pluginroot").toString());
if(!info.isReadable()){
QByteArray msg;
msg = info.absoluteFilePath().toLocal8Bit();
syslog(LOG_ERR, "The pluginroot path (%s) is not readable!", msg.constData());
ok = false;
}
return ok;
}
void Configuration::loadPlugins(const QHash<QString, QString> &confPlugins)
{
const QString pluginRoot(conf["pluginroot"].toString());
QHash<QString, QString>::const_iterator i;
for(i = confPlugins.constBegin(); i != confPlugins.constEnd(); ++i){
QPluginLoader loader(pluginRoot + i.value());
#ifdef QT_DEBUG
qDebug() << "Loading from: " << loader.fileName();
#endif
QObject *p = loader.instance();
IPlugin *requestHandlerFactory = qobject_cast<IPlugin *>(p);
if(!requestHandlerFactory){
qDebug() << "Plugin (" << i.key() << ":" << i.value() << ") not loaded: " << loader.errorString();
continue;
}
plugins.insert(i.key(), qMakePair(getPluginName(i.value()), requestHandlerFactory));
}
}
QString Configuration::getPluginName(const QString &fullName) const
{
QString name(fullName);
name.replace(".so", "").replace(".dll", "");
if(0 == name.indexOf("lib")){
name.replace(0, 3, "");
}
return name;
}
QString Configuration::dereference(const QString &value, const QSettings &settings)
{
QString retval;
QString reference;
bool escape = false;
int wait_closing = false;
foreach(QChar c, value){
if(escape){
if(wait_closing){
reference += c;
}
else{
retval += c;
}
escape = false;
}
else if('`' == c){
escape = true;
}
else{
if('{' == c){
wait_closing = true;
}
else if(wait_closing && '}' == c){
wait_closing = false;
qDebug() << "Looking for:" << reference;
if(!settings.contains(reference)){
QByteArray msg;
msg = reference.toLocal8Bit();
syslog(LOG_WARNING, "Cannot resolve reference %s ", msg.constData());
}
else{
retval += settings.value(reference).toString();
}
reference.clear();
}
else if(0 == wait_closing){
retval += c;
}
else{
reference += c;
}
}
}
if(wait_closing){
QByteArray msg;
msg = value.toLocal8Bit();
syslog(LOG_WARNING, "Cannot parse: %s - missing '}'", msg.constData());
return value;
}
qDebug() << value << "=" << retval;
return retval;
}