diff options
author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-15 21:04:28 +0100 |
---|---|---|
committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-24 18:05:53 +0100 |
commit | c876d3bbefe0dc00c27ca0c12d29da5874946962 (patch) | |
tree | 69f468a185a369b01998314bc3ecc19b70f4fcaa /utils/rbutilqt/base/rbsettings.cpp | |
parent | 6c6f0757d7a902feb293be165d1490c42bc8e7ad (diff) | |
download | rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.tar.gz rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.zip |
rbutil: Merge rbutil with utils folder.
rbutil uses several components from the utils folder, and can be
considered part of utils too. Having it in a separate folder is an
arbitrary split that doesn't help anymore these days, so merge them.
This also allows other utils to easily use libtools.make without the
need to navigate to a different folder.
Change-Id: I3fc2f4de19e3e776553efb5dea5f779dfec0dc21
Diffstat (limited to 'utils/rbutilqt/base/rbsettings.cpp')
-rw-r--r-- | utils/rbutilqt/base/rbsettings.cpp | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/utils/rbutilqt/base/rbsettings.cpp b/utils/rbutilqt/base/rbsettings.cpp new file mode 100644 index 0000000000..8eaa01f7f6 --- /dev/null +++ b/utils/rbutilqt/base/rbsettings.cpp | |||
@@ -0,0 +1,207 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2007 by Dominik Wenger | ||
10 | * | ||
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. | ||
13 | * | ||
14 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
15 | * KIND, either express or implied. | ||
16 | * | ||
17 | ****************************************************************************/ | ||
18 | |||
19 | #include "rbsettings.h" | ||
20 | #include "playerbuildinfo.h" | ||
21 | #include <QSettings> | ||
22 | #include "Logger.h" | ||
23 | |||
24 | #if defined(Q_OS_LINUX) | ||
25 | #include <unistd.h> | ||
26 | #endif | ||
27 | |||
28 | // user settings | ||
29 | const static struct { | ||
30 | RbSettings::UserSettings setting; | ||
31 | const char* name; | ||
32 | const char* def; | ||
33 | } UserSettingsList[] = { | ||
34 | { RbSettings::RbutilVersion, "rbutil_version", "" }, | ||
35 | { RbSettings::ShowChangelog, "show_changelog", "false" }, | ||
36 | { RbSettings::CurrentPlatform, "platform", "" }, | ||
37 | { RbSettings::Mountpoint, "mountpoint", "" }, | ||
38 | { RbSettings::CachePath, "cachepath", "" }, | ||
39 | { RbSettings::Build, "build", "" }, | ||
40 | { RbSettings::ProxyType, "proxytype", "" }, | ||
41 | { RbSettings::Proxy, "proxy", "" }, | ||
42 | { RbSettings::OfPath, "ofpath", "" }, | ||
43 | { RbSettings::Platform, "platform", "" }, | ||
44 | { RbSettings::Language, "lang", "" }, | ||
45 | { RbSettings::BackupPath, "backuppath", "" }, | ||
46 | { RbSettings::InstallRockbox, "install_rockbox", "true" }, | ||
47 | { RbSettings::InstallFonts, "install_fonts", "true" }, | ||
48 | { RbSettings::InstallThemes, "install_themes", "false" }, | ||
49 | { RbSettings::InstallPluginData, "install_plugin_data", "true" }, | ||
50 | { RbSettings::InstallVoice, "install_voice", "false" }, | ||
51 | { RbSettings::InstallManual, "install_manual", "false" }, | ||
52 | #if defined(Q_OS_WIN32) | ||
53 | { RbSettings::Tts, "tts", "sapi" }, | ||
54 | #elif defined(Q_OS_MACX) | ||
55 | { RbSettings::Tts, "tts", "carbon" }, | ||
56 | #else | ||
57 | { RbSettings::Tts, "tts", "espeak" }, | ||
58 | #endif | ||
59 | { RbSettings::UseTtsCorrections, "use_tts_corrections", "true" }, | ||
60 | { RbSettings::TalkFolders, "talk_folders", "" }, | ||
61 | { RbSettings::TalkProcessFiles, "talk_process_files", "true" }, | ||
62 | { RbSettings::TalkProcessFolders, "talk_process_folders", "true" }, | ||
63 | { RbSettings::TalkRecursive, "talk_recursive", "true" }, | ||
64 | { RbSettings::TalkSkipExisting, "talk_skip_existing", "true" }, | ||
65 | { RbSettings::TalkStripExtensions, "talk_strip_extensions","true" }, | ||
66 | { RbSettings::TalkIgnoreFiles, "talk_ignore_files", "false" }, | ||
67 | { RbSettings::TalkIgnoreWildcards, "talk_ignore_wildcards","" }, | ||
68 | { RbSettings::VoiceLanguage, "voicelanguage", "" }, | ||
69 | { RbSettings::TtsLanguage, ":tts:/language", "" }, | ||
70 | { RbSettings::TtsOptions, ":tts:/options", "" }, | ||
71 | { RbSettings::TtsPitch, ":tts:/pitch", "0" }, | ||
72 | { RbSettings::TtsPath, ":tts:/path", "" }, | ||
73 | { RbSettings::TtsVoice, ":tts:/voice", "" }, | ||
74 | { RbSettings::EncoderPath, ":encoder:/encoderpath", "" }, | ||
75 | { RbSettings::EncoderOptions, ":encoder:/encoderoptions", "" }, | ||
76 | { RbSettings::CacheDisabled, "cachedisable", "false" }, | ||
77 | { RbSettings::TtsUseSapi4, "sapi/useSapi4", "false" }, | ||
78 | { RbSettings::EncoderNarrowBand, ":encoder:/narrowband", "false" }, | ||
79 | { RbSettings::WavtrimThreshold, "wavtrimthreshold", "500"}, | ||
80 | { RbSettings::TtsSpeed, ":tts:/speed", "175" }, | ||
81 | { RbSettings::EncoderComplexity, ":encoder:/complexity", "10" }, | ||
82 | { RbSettings::EncoderQuality, ":encoder:/quality", "-1.0" }, | ||
83 | { RbSettings::EncoderVolume, ":encoder:/volume", "1.0" }, | ||
84 | }; | ||
85 | |||
86 | //! pointer to setting object to NULL | ||
87 | QSettings* RbSettings::userSettings = nullptr; | ||
88 | |||
89 | void RbSettings::ensureRbSettingsExists() | ||
90 | { | ||
91 | if(userSettings == nullptr) | ||
92 | { | ||
93 | // portable installation: | ||
94 | // check for a configuration file in the program folder. | ||
95 | QFileInfo config; | ||
96 | config.setFile(QCoreApplication::instance()->applicationDirPath() | ||
97 | + "/RockboxUtility.ini"); | ||
98 | if(config.isFile()) | ||
99 | { | ||
100 | userSettings = new QSettings(QCoreApplication::instance()->applicationDirPath() | ||
101 | + "/RockboxUtility.ini", QSettings::IniFormat, nullptr); | ||
102 | LOG_INFO() << "configuration: portable"; | ||
103 | } | ||
104 | else | ||
105 | { | ||
106 | userSettings = new QSettings(QSettings::IniFormat, | ||
107 | QSettings::UserScope, "rockbox.org", "RockboxUtility",nullptr); | ||
108 | LOG_INFO() << "configuration: system"; | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | void RbSettings::sync() | ||
114 | { | ||
115 | ensureRbSettingsExists(); | ||
116 | |||
117 | userSettings->sync(); | ||
118 | #if defined(Q_OS_LINUX) | ||
119 | // when using sudo it runs rbutil with uid 0 but unfortunately without a | ||
120 | // proper login shell, thus the configuration file gets placed in the | ||
121 | // calling users $HOME. This in turn will cause issues if trying to | ||
122 | // run rbutil later as user. Try to detect this case via the environment | ||
123 | // variable SUDO_UID and SUDO_GID and if set chown the user config file. | ||
124 | if(getuid() == 0) | ||
125 | { | ||
126 | char* realuser = getenv("SUDO_UID"); | ||
127 | char* realgroup = getenv("SUDO_GID"); | ||
128 | if(realuser != nullptr && realgroup != nullptr) | ||
129 | { | ||
130 | int realuid = atoi(realuser); | ||
131 | int realgid = atoi(realgroup); | ||
132 | // chown is attribute warn_unused_result, but in case this fails | ||
133 | // we can't do anything useful about it. Notifying the user | ||
134 | // is somewhat pointless. Add hack to suppress compiler warning. | ||
135 | if(chown(qPrintable(userSettings->fileName()), realuid, realgid)) | ||
136 | { } | ||
137 | } | ||
138 | } | ||
139 | #endif | ||
140 | } | ||
141 | |||
142 | QString RbSettings::userSettingFilename() | ||
143 | { | ||
144 | ensureRbSettingsExists(); | ||
145 | return userSettings->fileName(); | ||
146 | } | ||
147 | |||
148 | QVariant RbSettings::value(enum UserSettings setting) | ||
149 | { | ||
150 | QString empty; | ||
151 | return subValue(empty, setting); | ||
152 | } | ||
153 | |||
154 | QVariant RbSettings::subValue(QString sub, enum UserSettings setting) | ||
155 | { | ||
156 | ensureRbSettingsExists(); | ||
157 | |||
158 | // locate setting item | ||
159 | int i = 0; | ||
160 | while(UserSettingsList[i].setting != setting) | ||
161 | i++; | ||
162 | |||
163 | QString s = constructSettingPath(UserSettingsList[i].name, sub); | ||
164 | LOG_INFO() << "GET U:" << s << userSettings->value(s).toString(); | ||
165 | return userSettings->value(s, UserSettingsList[i].def); | ||
166 | } | ||
167 | |||
168 | void RbSettings::setValue(enum UserSettings setting , QVariant value) | ||
169 | { | ||
170 | QString empty; | ||
171 | return setSubValue(empty, setting, value); | ||
172 | } | ||
173 | |||
174 | void RbSettings::setSubValue(QString sub, enum UserSettings setting, QVariant value) | ||
175 | { | ||
176 | ensureRbSettingsExists(); | ||
177 | |||
178 | // locate setting item | ||
179 | int i = 0; | ||
180 | while(UserSettingsList[i].setting != setting) | ||
181 | i++; | ||
182 | |||
183 | QString s = constructSettingPath(UserSettingsList[i].name, sub); | ||
184 | userSettings->setValue(s, value); | ||
185 | LOG_INFO() << "SET U:" << s << userSettings->value(s).toString(); | ||
186 | } | ||
187 | |||
188 | QString RbSettings::constructSettingPath(QString path, QString substitute) | ||
189 | { | ||
190 | // anything to substitute? | ||
191 | if(path.contains(':')) { | ||
192 | QString platform = userSettings->value("platform").toString(); | ||
193 | if(!substitute.isEmpty()) { | ||
194 | path.replace(":tts:", substitute); | ||
195 | path.replace(":encoder:", substitute); | ||
196 | } | ||
197 | else { | ||
198 | path.replace(":tts:", userSettings->value("tts").toString()); | ||
199 | path.replace(":encoder:", PlayerBuildInfo::instance()->value( | ||
200 | PlayerBuildInfo::Encoder, platform).toString()); | ||
201 | } | ||
202 | path.replace(":platform:", platform); | ||
203 | } | ||
204 | |||
205 | return path; | ||
206 | } | ||
207 | |||