1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #include "rbsettings.h"
20 #include "systeminfo.h"
23 #if defined(Q_OS_LINUX)
29 RbSettings::UserSettings setting
;
32 } UserSettingsList
[] = {
33 { RbSettings::RbutilVersion
, "rbutil_version", "" },
34 { RbSettings::CurrentPlatform
, "platform", "" },
35 { RbSettings::Mountpoint
, "mountpoint", "" },
36 { RbSettings::CachePath
, "cachepath", "" },
37 { RbSettings::Build
, "build", "" },
38 { RbSettings::ProxyType
, "proxytype", "" },
39 { RbSettings::Proxy
, "proxy", "" },
40 { RbSettings::OfPath
, "ofpath", "" },
41 { RbSettings::Platform
, "platform", "" },
42 { RbSettings::Language
, "lang", "" },
43 { RbSettings::BackupPath
, "backuppath", "" },
44 { RbSettings::InstallRockbox
, "install_rockbox", "true" },
45 { RbSettings::InstallFonts
, "install_fonts", "true" },
46 { RbSettings::InstallThemes
, "install_themes", "false" },
47 { RbSettings::InstallGamefiles
, "install_gamefiles", "true" },
48 #if defined(Q_OS_WIN32)
49 { RbSettings::Tts
, "tts", "sapi" },
50 #elif defined(Q_OS_MACX)
51 { RbSettings::Tts
, "tts", "carbon" },
53 { RbSettings::Tts
, "tts", "espeak" },
55 { RbSettings::UseTtsCorrections
, "use_tts_corrections", "true" },
56 { RbSettings::TalkFolders
, "talk_folders", "" },
57 { RbSettings::TalkProcessFiles
, "talk_process_files", "true" },
58 { RbSettings::TalkProcessFolders
, "talk_process_folders", "true" },
59 { RbSettings::TalkRecursive
, "talk_recursive", "true" },
60 { RbSettings::TalkSkipExisting
, "talk_skip_existing", "true" },
61 { RbSettings::TalkStripExtensions
, "talk_strip_extensions","true" },
62 { RbSettings::TalkIgnoreFiles
, "talk_ignore_files", "false" },
63 { RbSettings::TalkIgnoreWildcards
, "talk_ignore_wildcards","" },
64 { RbSettings::VoiceLanguage
, "voicelanguage", "" },
65 { RbSettings::TtsLanguage
, ":tts:/language", "" },
66 { RbSettings::TtsOptions
, ":tts:/options", "" },
67 { RbSettings::TtsPitch
, ":tts:/pitch", "0" },
68 { RbSettings::TtsPath
, ":tts:/path", "" },
69 { RbSettings::TtsVoice
, ":tts:/voice", "" },
70 { RbSettings::EncoderPath
, ":encoder:/encoderpath", "" },
71 { RbSettings::EncoderOptions
, ":encoder:/encoderoptions", "" },
72 { RbSettings::CacheOffline
, "offline", "false" },
73 { RbSettings::CacheDisabled
, "cachedisable", "false" },
74 { RbSettings::TtsUseSapi4
, "sapi/useSapi4", "false" },
75 { RbSettings::EncoderNarrowBand
, ":encoder:/narrowband", "false" },
76 { RbSettings::WavtrimThreshold
, "wavtrimthreshold", "500"},
77 { RbSettings::TtsSpeed
, ":tts:/speed", "175" },
78 { RbSettings::EncoderComplexity
, ":encoder:/complexity", "10" },
79 { RbSettings::EncoderQuality
, ":encoder:/quality", "-1.0" },
80 { RbSettings::EncoderVolume
, ":encoder:/volume", "1.0" },
83 //! pointer to setting object to NULL
84 QSettings
* RbSettings::userSettings
= NULL
;
86 void RbSettings::ensureRbSettingsExists()
88 if(userSettings
== NULL
)
90 // portable installation:
91 // check for a configuration file in the program folder.
93 config
.setFile(QCoreApplication::instance()->applicationDirPath()
94 + "/RockboxUtility.ini");
97 userSettings
= new QSettings(QCoreApplication::instance()->applicationDirPath()
98 + "/RockboxUtility.ini", QSettings::IniFormat
, NULL
);
99 qDebug() << "[Settings] configuration: portable";
103 userSettings
= new QSettings(QSettings::IniFormat
,
104 QSettings::UserScope
, "rockbox.org", "RockboxUtility",NULL
);
105 qDebug() << "[Settings] configuration: system";
110 void RbSettings::sync()
112 ensureRbSettingsExists();
114 userSettings
->sync();
115 #if defined(Q_OS_LINUX)
116 // when using sudo it runs rbutil with uid 0 but unfortunately without a
117 // proper login shell, thus the configuration file gets placed in the
118 // calling users $HOME. This in turn will cause issues if trying to
119 // run rbutil later as user. Try to detect this case via the environment
120 // variable SUDO_UID and SUDO_GID and if set chown the user config file.
123 char* realuser
= getenv("SUDO_UID");
124 char* realgroup
= getenv("SUDO_GID");
125 if(realuser
!= NULL
&& realgroup
!= NULL
)
127 int realuid
= atoi(realuser
);
128 int realgid
= atoi(realgroup
);
129 // chown is attribute warn_unused_result, but in case this fails
130 // we can't do anything useful about it. Notifying the user
131 // is somewhat pointless. Add hack to suppress compiler warning.
132 if(chown(qPrintable(userSettings
->fileName()), realuid
, realgid
))
139 QString
RbSettings::userSettingFilename()
141 ensureRbSettingsExists();
142 return userSettings
->fileName();
145 QVariant
RbSettings::value(enum UserSettings setting
)
148 return subValue(empty
, setting
);
151 QVariant
RbSettings::subValue(QString sub
, enum UserSettings setting
)
153 ensureRbSettingsExists();
155 // locate setting item
157 while(UserSettingsList
[i
].setting
!= setting
)
160 QString s
= constructSettingPath(UserSettingsList
[i
].name
, sub
);
161 qDebug() << "[Settings] GET U:" << s
<< userSettings
->value(s
).toString();
162 return userSettings
->value(s
, UserSettingsList
[i
].def
);
165 void RbSettings::setValue(enum UserSettings setting
, QVariant value
)
168 return setSubValue(empty
, setting
, value
);
171 void RbSettings::setSubValue(QString sub
, enum UserSettings setting
, QVariant value
)
173 ensureRbSettingsExists();
175 // locate setting item
177 while(UserSettingsList
[i
].setting
!= setting
)
180 QString s
= constructSettingPath(UserSettingsList
[i
].name
, sub
);
181 userSettings
->setValue(s
, value
);
182 qDebug() << "[Settings] SET U:" << s
<< userSettings
->value(s
).toString();
185 QString
RbSettings::constructSettingPath(QString path
, QString substitute
)
187 // anything to substitute?
188 if(path
.contains(':')) {
189 QString platform
= userSettings
->value("platform").toString();
190 if(!substitute
.isEmpty()) {
191 path
.replace(":tts:", substitute
);
192 path
.replace(":encoder:", substitute
);
195 path
.replace(":tts:", userSettings
->value("tts").toString());
196 path
.replace(":encoder:", SystemInfo::platformValue(platform
,SystemInfo::CurEncoder
).toString());
198 path
.replace(":platform:", platform
);