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
|
#include "pvsSettingsManager.h"
#include "TextFile.h"
PVSSettingsManager* PVSSettingsManager::getManager()
{
if (myself)
return myself;
else
return myself = new PVSSettingsManager;
}
void PVSSettingsManager::setConfigFile(QString path)
{
if (path.size() && fileExists(path))
{
_path = path;
_parseFile(_path);
}
else
ConsoleLog writeError(QString("Can't open config file \"").append(QString(path).append("\"")));
}
bool PVSSettingsManager::hasEntry(QString name)
{
for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
{
if ((*it).first.compare(name) == 0)
{
return true;
}
}
return false;
}
QString PVSSettingsManager::getEntryString(QString name)
{
for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
{
if ((*it).first.compare(name) == 0)
{
return (*it).second;
}
}
return QString();
}
void PVSSettingsManager::writeEntry(QString name, QString value)
{
if (name.size() && value.size())
return;
bool unique = true;
for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
{
if ((*it).first.compare(name) == 0)
{
unique = false;
(*it).second = value;
break;
}
}
if (unique)
{
SettingsEntry tmp(name, value);
settingsList.push_back(tmp);
}
}
PVSSettingsManager* PVSSettingsManager::myself = NULL;
PVSSettingsManager::PVSSettingsManager()
{
}
void PVSSettingsManager::setConfigs()
{
//default settings
_configs.setValue("Chat/chatstate", "on");
_configs.setValue("Chat/chatmode", "bossmode");
_configs.setValue("Room/roomId", "0");
_configs.setValue("VNC/permit", "off");
_configs.setValue("VNC/quality", "high");
_configs.sync();
}
void PVSSettingsManager::reWriteConfigs(QString set, QString val)
{
_configs.setValue(set, val);
_configs.sync();
}
void PVSSettingsManager::readConfigs(QString sett, QString vall)
{
//TODO: read the config file..
_configs.value("Chat/chatstate").toBool();
_configs.value("Chat/chatmode").toString();
_configs.value("Room/room").toInt();
_configs.value("VNC/permit").toBool();
_configs.value("VNC/quality").toString();
}
void PVSSettingsManager::_parseFile(QString path)
{
QString line;
TextFile file(path);
SettingsList tmpList;
if (file.good())
{
while (!file.eof())
{
line = file.readLine();
if (!(line.length() <=1)) // ignore blank
{
if (!(line[0] == '#' || line[0] == '/' || line[0] == '[')) // ignore comments and section headers
{
SettingsEntry tmp = _parseLine(line);
if (tmp.first.size() && tmp.second.size())
{
bool unique = true;
for (SettingsIter it = tmpList.begin(); it != tmpList.end(); it++)
{
if ((*it).first.compare(tmp.first) == 0)
{
unique = false;
break;
}
}
if (unique)
tmpList.push_back(tmp);
}
}
}
}
}
else
{
ConsoleLog writeError(QString("No configfile \"").append(QString(path).append("\" found or file corrupt.")));
}
if (tmpList.size())
settingsList = tmpList;
}
#ifdef verbose
ConsoleLog writeLine(QString("Dumping Config Content of ").append(QString(path).append(" : ")));
for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
{
ConsoleLog writeLine(QString("Option: ").append(QString((*it).first).append(QString(" | Value: ").append((*it).second))));
}
ConsoleLog writeLine(QString("End of ").append(QString(path).append(".")));
#endif
SettingsEntry PVSSettingsManager::_parseLine(QString line)
{
QString name;
QString value;
name = lineSplitter(line, "=\n\t", true);
value = lineSplitter(line, "=\n\t", false);
if (!(name.size() && value.size()))
return SettingsEntry("","");
// remove whitespaces in front of option name
for (int i = 0; i < name.size(); i++)
{
if (name[i] == '\t' || name[i] == ' ')
{
name.remove(i, 1);
i--;
}
else
break;
}
// whitespaces after the value are trimmed by the lineSplitter
SettingsEntry tmp(name, value);
return tmp;
}
|