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::CacheDisabled
, "cachedisable", "false" },
73 { RbSettings::TtsUseSapi4
, "sapi/useSapi4", "false" },
74 { RbSettings::EncoderNarrowBand
, ":encoder:/narrowband", "false" },
75 { RbSettings::WavtrimThreshold
, "wavtrimthreshold", "500"},
76 { RbSettings::TtsSpeed
, ":tts:/speed", "175" },
77 { RbSettings::EncoderComplexity
, ":encoder:/complexity", "10" },
78 { RbSettings::EncoderQuality
, ":encoder:/quality", "-1.0" },
79 { RbSettings::EncoderVolume
, ":encoder:/volume", "1.0" },
82 //! pointer to setting object to NULL
83 QSettings
* RbSettings::userSettings
= NULL
;
85 void RbSettings::ensureRbSettingsExists()
87 if(userSettings
== NULL
)
89 // portable installation:
90 // check for a configuration file in the program folder.
92 config
.setFile(QCoreApplication::instance()->applicationDirPath()
93 + "/RockboxUtility.ini");
96 userSettings
= new QSettings(QCoreApplication::instance()->applicationDirPath()
97 + "/RockboxUtility.ini", QSettings::IniFormat
, NULL
);
98 qDebug() << "[Settings] configuration: portable";
102 userSettings
= new QSettings(QSettings::IniFormat
,
103 QSettings::UserScope
, "rockbox.org", "RockboxUtility",NULL
);
104 qDebug() << "[Settings] configuration: system";
109 void RbSettings::sync()
111 ensureRbSettingsExists();
113 userSettings
->sync();
114 #if defined(Q_OS_LINUX)
115 // when using sudo it runs rbutil with uid 0 but unfortunately without a
116 // proper login shell, thus the configuration file gets placed in the
117 // calling users $HOME. This in turn will cause issues if trying to
118 // run rbutil later as user. Try to detect this case via the environment
119 // variable SUDO_UID and SUDO_GID and if set chown the user config file.
122 char* realuser
= getenv("SUDO_UID");
123 char* realgroup
= getenv("SUDO_GID");
124 if(realuser
!= NULL
&& realgroup
!= NULL
)
126 int realuid
= atoi(realuser
);
127 int realgid
= atoi(realgroup
);
128 // chown is attribute warn_unused_result, but in case this fails
129 // we can't do anything useful about it. Notifying the user
130 // is somewhat pointless. Add hack to suppress compiler warning.
131 if(chown(qPrintable(userSettings
->fileName()), realuid
, realgid
))
138 QString
RbSettings::userSettingFilename()
140 ensureRbSettingsExists();
141 return userSettings
->fileName();
144 QVariant
RbSettings::value(enum UserSettings setting
)
147 return subValue(empty
, setting
);
150 QVariant
RbSettings::subValue(QString sub
, enum UserSettings setting
)
152 ensureRbSettingsExists();
154 // locate setting item
156 while(UserSettingsList
[i
].setting
!= setting
)
159 QString s
= constructSettingPath(UserSettingsList
[i
].name
, sub
);
160 qDebug() << "[Settings] GET U:" << s
<< userSettings
->value(s
).toString();
161 return userSettings
->value(s
, UserSettingsList
[i
].def
);
164 void RbSettings::setValue(enum UserSettings setting
, QVariant value
)
167 return setSubValue(empty
, setting
, value
);
170 void RbSettings::setSubValue(QString sub
, enum UserSettings setting
, QVariant value
)
172 ensureRbSettingsExists();
174 // locate setting item
176 while(UserSettingsList
[i
].setting
!= setting
)
179 QString s
= constructSettingPath(UserSettingsList
[i
].name
, sub
);
180 userSettings
->setValue(s
, value
);
181 qDebug() << "[Settings] SET U:" << s
<< userSettings
->value(s
).toString();
184 QString
RbSettings::constructSettingPath(QString path
, QString substitute
)
186 // anything to substitute?
187 if(path
.contains(':')) {
188 QString platform
= userSettings
->value("platform").toString();
189 if(!substitute
.isEmpty()) {
190 path
.replace(":tts:", substitute
);
191 path
.replace(":encoder:", substitute
);
194 path
.replace(":tts:", userSettings
->value("tts").toString());
195 path
.replace(":encoder:", SystemInfo::platformValue(platform
,SystemInfo::CurEncoder
).toString());
197 path
.replace(":platform:", platform
);