diff options
author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2009-04-29 21:36:05 +0000 |
---|---|---|
committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2009-04-29 21:36:05 +0000 |
commit | 19c258847109d04d65eaddc07532c250ab7df5dc (patch) | |
tree | bac70025f879cb465702be77ba49bad271edab1b /rbutil/rbutilqt/base/rbsettings.cpp | |
parent | 22656911028459a9d21b1253860750720b25c2b6 (diff) | |
download | rockbox-19c258847109d04d65eaddc07532c250ab7df5dc.tar.gz rockbox-19c258847109d04d65eaddc07532c250ab7df5dc.zip |
Move Settings class to base.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20827 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'rbutil/rbutilqt/base/rbsettings.cpp')
-rw-r--r-- | rbutil/rbutilqt/base/rbsettings.cpp | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/base/rbsettings.cpp b/rbutil/rbutilqt/base/rbsettings.cpp new file mode 100644 index 0000000000..12654cd288 --- /dev/null +++ b/rbutil/rbutilqt/base/rbsettings.cpp | |||
@@ -0,0 +1,285 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2007 by Dominik Wenger | ||
10 | * $Id$ | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include "rbsettings.h" | ||
21 | |||
22 | #include <QSettings> | ||
23 | |||
24 | #if defined(Q_OS_LINUX) | ||
25 | #include <unistd.h> | ||
26 | #endif | ||
27 | |||
28 | // device settings | ||
29 | const static struct { | ||
30 | RbSettings::SystemSettings setting; | ||
31 | const char* name; | ||
32 | const char* def; | ||
33 | } SystemSettingsList[] = { | ||
34 | { RbSettings::ManualUrl, "manual_url", "" }, | ||
35 | { RbSettings::BleedingUrl, "bleeding_url", "" }, | ||
36 | { RbSettings::BootloaderUrl, "bootloader_url", "" }, | ||
37 | { RbSettings::BootloaderInfoUrl, "bootloader_info_url", "" }, | ||
38 | { RbSettings::FontUrl, "font_url", "" }, | ||
39 | { RbSettings::VoiceUrl, "voice_url", "" }, | ||
40 | { RbSettings::DoomUrl, "doom_url", "" }, | ||
41 | { RbSettings::ReleaseUrl, "release_url", "" }, | ||
42 | { RbSettings::DailyUrl, "daily_url", "" }, | ||
43 | { RbSettings::ServerConfUrl, "server_conf_url", "" }, | ||
44 | { RbSettings::GenlangUrl, "genlang_url", "" }, | ||
45 | { RbSettings::ThemesUrl, "themes_url", "" }, | ||
46 | { RbSettings::BleedingInfo, "bleeding_info", "" }, | ||
47 | { RbSettings::CurPlatformName, ":platform:/name", "" }, | ||
48 | { RbSettings::CurManual, ":platform:/manual", "rockbox-:platform:" }, | ||
49 | { RbSettings::CurBootloaderMethod, ":platform:/bootloadermethod", "none" }, | ||
50 | { RbSettings::CurBootloaderName, ":platform:/bootloadername", "" }, | ||
51 | { RbSettings::CurBootloaderFile, ":platform:/bootloaderfile", "" }, | ||
52 | { RbSettings::CurEncoder, ":platform:/encoder", "" }, | ||
53 | { RbSettings::CurResolution, ":platform:/resolution", "" }, | ||
54 | { RbSettings::CurBrand, ":platform:/brand", "" }, | ||
55 | { RbSettings::CurName, ":platform:/name", "" }, | ||
56 | { RbSettings::CurBuildserverModel, ":platform:/buildserver_modelname", "" }, | ||
57 | { RbSettings::CurConfigureModel, ":platform:/configure_modelname", "" }, | ||
58 | { RbSettings::CurTargetId, ":platform:/targetid", "" }, | ||
59 | }; | ||
60 | |||
61 | // user settings | ||
62 | const static struct { | ||
63 | RbSettings::UserSettings setting; | ||
64 | const char* name; | ||
65 | const char* def; | ||
66 | } UserSettingsList[] = { | ||
67 | { RbSettings::RbutilVersion, "rbutil_version", "" }, | ||
68 | { RbSettings::CurrentPlatform, "platform", "" }, | ||
69 | { RbSettings::Mountpoint, "mountpoint", "" }, | ||
70 | { RbSettings::CachePath, "cachepath", "" }, | ||
71 | { RbSettings::Build, "build", "" }, | ||
72 | { RbSettings::ProxyType, "proxytype", "" }, | ||
73 | { RbSettings::Proxy, "proxy", "" }, | ||
74 | { RbSettings::OfPath, "ofpath", "" }, | ||
75 | { RbSettings::Platform, "platform", "" }, | ||
76 | { RbSettings::Language, "lang", "" }, | ||
77 | { RbSettings::Tts, "tts", "" }, | ||
78 | { RbSettings::LastTalkedFolder, "last_talked_folder", "" }, | ||
79 | { RbSettings::VoiceLanguage, "voicelanguage", "" }, | ||
80 | { RbSettings::TtsLanguage, ":tts:/language", "" }, | ||
81 | { RbSettings::TtsOptions, ":tts:/options", "" }, | ||
82 | { RbSettings::TtsPath, ":tts:/path", "" }, | ||
83 | { RbSettings::TtsVoice, ":tts:/voice", "" }, | ||
84 | { RbSettings::EncoderPath, ":encoder:/encoderpath", "" }, | ||
85 | { RbSettings::EncoderOptions, ":encoder:/encoderoptions", "" }, | ||
86 | { RbSettings::CacheOffline, "offline", "false" }, | ||
87 | { RbSettings::CacheDisabled, "cachedisable", "false" }, | ||
88 | { RbSettings::TtsUseSapi4, "sapi/useSapi4", "false" }, | ||
89 | { RbSettings::EncoderNarrowBand, ":encoder:/narrowband", "false" }, | ||
90 | { RbSettings::WavtrimThreshold, "wavtrimthreshold", "500"}, | ||
91 | { RbSettings::TtsSpeed, ":tts:/speed", "0" }, | ||
92 | { RbSettings::EncoderComplexity, ":encoder:/complexity", "10" }, | ||
93 | { RbSettings::EncoderQuality, ":encoder:/quality", "8.0" }, | ||
94 | { RbSettings::EncoderVolume, ":encoder:/volume", "1.0" }, | ||
95 | }; | ||
96 | |||
97 | void RbSettings::open() | ||
98 | { | ||
99 | // only use built-in rbutil.ini | ||
100 | systemSettings = new QSettings(":/ini/rbutil.ini", QSettings::IniFormat, 0); | ||
101 | // portable installation: | ||
102 | // check for a configuration file in the program folder. | ||
103 | QFileInfo config; | ||
104 | config.setFile(QCoreApplication::instance()->applicationDirPath() | ||
105 | + "/RockboxUtility.ini"); | ||
106 | if(config.isFile()) | ||
107 | { | ||
108 | userSettings = new QSettings(QCoreApplication::instance()->applicationDirPath() | ||
109 | + "/RockboxUtility.ini", QSettings::IniFormat, this); | ||
110 | qDebug() << "config: portable"; | ||
111 | } | ||
112 | else | ||
113 | { | ||
114 | userSettings = new QSettings(QSettings::IniFormat, | ||
115 | QSettings::UserScope, "rockbox.org", "RockboxUtility",this); | ||
116 | qDebug() << "config: system"; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | void RbSettings::sync() | ||
121 | { | ||
122 | userSettings->sync(); | ||
123 | #if defined(Q_OS_LINUX) | ||
124 | // when using sudo it runs rbutil with uid 0 but unfortunately without a | ||
125 | // proper login shell, thus the configuration file gets placed in the | ||
126 | // calling users $HOME. This in turn will cause issues if trying to | ||
127 | // run rbutil later as user. Try to detect this case via the environment | ||
128 | // variable SUDO_UID and SUDO_GID and if set chown the user config file. | ||
129 | if(getuid() == 0) | ||
130 | { | ||
131 | char* realuser = getenv("SUDO_UID"); | ||
132 | char* realgroup = getenv("SUDO_GID"); | ||
133 | if(realuser != NULL && realgroup != NULL) | ||
134 | { | ||
135 | int realuid = atoi(realuser); | ||
136 | int realgid = atoi(realgroup); | ||
137 | // chown is attribute warn_unused_result, but in case this fails | ||
138 | // we can't do anything useful about it. Notifying the user | ||
139 | // is somewhat pointless. Add hack to suppress compiler warning. | ||
140 | if(chown(qPrintable(userSettings->fileName()), realuid, realgid)); | ||
141 | } | ||
142 | } | ||
143 | #endif | ||
144 | } | ||
145 | |||
146 | |||
147 | QVariant RbSettings::value(enum SystemSettings setting) | ||
148 | { | ||
149 | // locate setting item | ||
150 | int i = 0; | ||
151 | while(SystemSettingsList[i].setting != setting) | ||
152 | i++; | ||
153 | |||
154 | QString s = constructSettingPath(SystemSettingsList[i].name); | ||
155 | QString d = SystemSettingsList[i].def; | ||
156 | d.replace(":platform:", userSettings->value("platform").toString()); | ||
157 | qDebug() << "[Settings] GET S:" << s << systemSettings->value(s, d).toString(); | ||
158 | return systemSettings->value(s, d); | ||
159 | } | ||
160 | |||
161 | |||
162 | QString RbSettings::userSettingFilename() | ||
163 | { | ||
164 | return userSettings->fileName(); | ||
165 | } | ||
166 | |||
167 | |||
168 | QVariant RbSettings::subValue(QString& sub, enum UserSettings setting) | ||
169 | { | ||
170 | // locate setting item | ||
171 | int i = 0; | ||
172 | while(UserSettingsList[i].setting != setting) | ||
173 | i++; | ||
174 | |||
175 | QString s = constructSettingPath(UserSettingsList[i].name, sub); | ||
176 | qDebug() << "[Settings] GET U:" << s << userSettings->value(s).toString(); | ||
177 | return userSettings->value(s, UserSettingsList[i].def); | ||
178 | } | ||
179 | |||
180 | |||
181 | void RbSettings::setSubValue(QString& sub, enum UserSettings setting, QVariant value) | ||
182 | { | ||
183 | // locate setting item | ||
184 | int i = 0; | ||
185 | while(UserSettingsList[i].setting != setting) | ||
186 | i++; | ||
187 | |||
188 | QString s = constructSettingPath(UserSettingsList[i].name, sub); | ||
189 | qDebug() << "[Settings] SET U:" << s << userSettings->value(s).toString(); | ||
190 | userSettings->setValue(s, value); | ||
191 | } | ||
192 | |||
193 | |||
194 | |||
195 | QStringList RbSettings::platforms() | ||
196 | { | ||
197 | QStringList result; | ||
198 | systemSettings->beginGroup("platforms"); | ||
199 | QStringList a = systemSettings->childKeys(); | ||
200 | for(int i = 0; i < a.size(); i++) | ||
201 | { | ||
202 | result.append(systemSettings->value(a.at(i), "null").toString()); | ||
203 | } | ||
204 | systemSettings->endGroup(); | ||
205 | return result; | ||
206 | } | ||
207 | |||
208 | QStringList RbSettings::languages() | ||
209 | { | ||
210 | QStringList result; | ||
211 | systemSettings->beginGroup("languages"); | ||
212 | QStringList a = systemSettings->childKeys(); | ||
213 | for(int i = 0; i < a.size(); i++) | ||
214 | { | ||
215 | result.append(systemSettings->value(a.at(i), "null").toString()); | ||
216 | } | ||
217 | systemSettings->endGroup(); | ||
218 | return result; | ||
219 | } | ||
220 | |||
221 | QString RbSettings::name(QString platform) | ||
222 | { | ||
223 | return systemSettings->value(platform + "/name").toString(); | ||
224 | } | ||
225 | |||
226 | QString RbSettings::brand(QString platform) | ||
227 | { | ||
228 | return systemSettings->value(platform + "/brand").toString(); | ||
229 | } | ||
230 | |||
231 | QMap<int, QString> RbSettings::usbIdMap(enum MapType type) | ||
232 | { | ||
233 | QMap<int, QString> map; | ||
234 | // get a list of ID -> target name | ||
235 | QStringList platforms; | ||
236 | systemSettings->beginGroup("platforms"); | ||
237 | platforms = systemSettings->childKeys(); | ||
238 | systemSettings->endGroup(); | ||
239 | |||
240 | QString t; | ||
241 | switch(type) { | ||
242 | case MapDevice: | ||
243 | t = "usbid"; | ||
244 | break; | ||
245 | case MapError: | ||
246 | t = "usberror"; | ||
247 | break; | ||
248 | case MapIncompatible: | ||
249 | t = "usbincompat"; | ||
250 | break; | ||
251 | } | ||
252 | |||
253 | for(int i = 0; i < platforms.size(); i++) | ||
254 | { | ||
255 | systemSettings->beginGroup("platforms"); | ||
256 | QString target = systemSettings->value(platforms.at(i)).toString(); | ||
257 | systemSettings->endGroup(); | ||
258 | systemSettings->beginGroup(target); | ||
259 | QStringList ids = systemSettings->value(t).toStringList(); | ||
260 | int j = ids.size(); | ||
261 | while(j--) | ||
262 | map.insert(ids.at(j).toInt(0, 16), target); | ||
263 | |||
264 | systemSettings->endGroup(); | ||
265 | } | ||
266 | return map; | ||
267 | } | ||
268 | |||
269 | |||
270 | QString RbSettings::constructSettingPath(QString path, QString substitute) | ||
271 | { | ||
272 | QString platform = userSettings->value("platform").toString(); | ||
273 | if(!substitute.isEmpty()) { | ||
274 | path.replace(":tts:", substitute); | ||
275 | path.replace(":encoder:", substitute); | ||
276 | } | ||
277 | else { | ||
278 | path.replace(":tts:", userSettings->value("tts").toString()); | ||
279 | path.replace(":encoder:", systemSettings->value(platform + "/encoder").toString()); | ||
280 | } | ||
281 | path.replace(":platform:", platform); | ||
282 | |||
283 | return path; | ||
284 | } | ||
285 | |||