1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "rbsettings.h"
21 #include "systeminfo.h"
24 #if defined(Q_OS_LINUX)
30 RbSettings::UserSettings setting
;
33 } UserSettingsList
[] = {
34 { RbSettings::RbutilVersion
, "rbutil_version", "" },
35 { RbSettings::CurrentPlatform
, "platform", "" },
36 { RbSettings::Mountpoint
, "mountpoint", "" },
37 { RbSettings::CachePath
, "cachepath", "" },
38 { RbSettings::Build
, "build", "" },
39 { RbSettings::ProxyType
, "proxytype", "" },
40 { RbSettings::Proxy
, "proxy", "" },
41 { RbSettings::OfPath
, "ofpath", "" },
42 { RbSettings::Platform
, "platform", "" },
43 { RbSettings::Language
, "lang", "" },
44 { RbSettings::Tts
, "tts", "" },
45 { RbSettings::LastTalkedFolder
, "last_talked_folder", "" },
46 { RbSettings::VoiceLanguage
, "voicelanguage", "" },
47 { RbSettings::TtsLanguage
, ":tts:/language", "" },
48 { RbSettings::TtsOptions
, ":tts:/options", "" },
49 { RbSettings::TtsPath
, ":tts:/path", "" },
50 { RbSettings::TtsVoice
, ":tts:/voice", "" },
51 { RbSettings::EncoderPath
, ":encoder:/encoderpath", "" },
52 { RbSettings::EncoderOptions
, ":encoder:/encoderoptions", "" },
53 { RbSettings::CacheOffline
, "offline", "false" },
54 { RbSettings::CacheDisabled
, "cachedisable", "false" },
55 { RbSettings::TtsUseSapi4
, "sapi/useSapi4", "false" },
56 { RbSettings::EncoderNarrowBand
, ":encoder:/narrowband", "false" },
57 { RbSettings::WavtrimThreshold
, "wavtrimthreshold", "500"},
58 { RbSettings::TtsSpeed
, ":tts:/speed", "175" },
59 { RbSettings::EncoderComplexity
, ":encoder:/complexity", "10" },
60 { RbSettings::EncoderQuality
, ":encoder:/quality", "8.0" },
61 { RbSettings::EncoderVolume
, ":encoder:/volume", "1.0" },
64 //! pointer to setting object to NULL
65 QSettings
* RbSettings::userSettings
= NULL
;
67 void RbSettings::ensureRbSettingsExists()
69 if(userSettings
== NULL
)
71 // portable installation:
72 // check for a configuration file in the program folder.
74 config
.setFile(QCoreApplication::instance()->applicationDirPath()
75 + "/RockboxUtility.ini");
78 userSettings
= new QSettings(QCoreApplication::instance()->applicationDirPath()
79 + "/RockboxUtility.ini", QSettings::IniFormat
, NULL
);
80 qDebug() << "[Settings] configuration: portable";
84 userSettings
= new QSettings(QSettings::IniFormat
,
85 QSettings::UserScope
, "rockbox.org", "RockboxUtility",NULL
);
86 qDebug() << "[Settings] configuration: system";
91 void RbSettings::sync()
93 ensureRbSettingsExists();
96 #if defined(Q_OS_LINUX)
97 // when using sudo it runs rbutil with uid 0 but unfortunately without a
98 // proper login shell, thus the configuration file gets placed in the
99 // calling users $HOME. This in turn will cause issues if trying to
100 // run rbutil later as user. Try to detect this case via the environment
101 // variable SUDO_UID and SUDO_GID and if set chown the user config file.
104 char* realuser
= getenv("SUDO_UID");
105 char* realgroup
= getenv("SUDO_GID");
106 if(realuser
!= NULL
&& realgroup
!= NULL
)
108 int realuid
= atoi(realuser
);
109 int realgid
= atoi(realgroup
);
110 // chown is attribute warn_unused_result, but in case this fails
111 // we can't do anything useful about it. Notifying the user
112 // is somewhat pointless. Add hack to suppress compiler warning.
113 if(chown(qPrintable(userSettings
->fileName()), realuid
, realgid
))
120 QString
RbSettings::userSettingFilename()
122 ensureRbSettingsExists();
123 return userSettings
->fileName();
126 QVariant
RbSettings::value(enum UserSettings setting
)
129 return subValue(empty
, setting
);
132 QVariant
RbSettings::subValue(QString sub
, enum UserSettings setting
)
134 ensureRbSettingsExists();
136 // locate setting item
138 while(UserSettingsList
[i
].setting
!= setting
)
141 QString s
= constructSettingPath(UserSettingsList
[i
].name
, sub
);
142 qDebug() << "[Settings] GET U:" << s
<< userSettings
->value(s
).toString();
143 return userSettings
->value(s
, UserSettingsList
[i
].def
);
146 void RbSettings::setValue(enum UserSettings setting
, QVariant value
)
149 return setSubValue(empty
, setting
, value
);
152 void RbSettings::setSubValue(QString sub
, enum UserSettings setting
, QVariant value
)
154 ensureRbSettingsExists();
156 // locate setting item
158 while(UserSettingsList
[i
].setting
!= setting
)
161 QString s
= constructSettingPath(UserSettingsList
[i
].name
, sub
);
162 userSettings
->setValue(s
, value
);
163 qDebug() << "[Settings] SET U:" << s
<< userSettings
->value(s
).toString();
166 QString
RbSettings::constructSettingPath(QString path
, QString substitute
)
168 // anything to substitute?
169 if(path
.contains(':')) {
170 QString platform
= userSettings
->value("platform").toString();
171 if(!substitute
.isEmpty()) {
172 path
.replace(":tts:", substitute
);
173 path
.replace(":encoder:", substitute
);
176 path
.replace(":tts:", userSettings
->value("tts").toString());
177 path
.replace(":encoder:", SystemInfo::platformValue(platform
,SystemInfo::CurEncoder
).toString());
179 path
.replace(":platform:", platform
);