summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/rbsettings.cpp
blob: fec68b5336e13907d55e64aea0fb89e5c83ab05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2007 by Dominik Wenger
 *   $Id$
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "rbsettings.h"

#include <QSettings>

#if defined(Q_OS_LINUX)
#include <unistd.h>
#endif


// device settings
const static struct {
    RbSettings::SystemSettings setting;
    const char* name;
    const char* def;
} SystemSettingsList[] = {
    { RbSettings::ManualUrl,            "manual_url",           "" },
    { RbSettings::BleedingUrl,          "bleeding_url",         "" },
    { RbSettings::BootloaderUrl,        "bootloader_url",       "" },
    { RbSettings::BootloaderInfoUrl,    "bootloader_info_url",  "" },
    { RbSettings::FontUrl,              "font_url",             "" },
    { RbSettings::VoiceUrl,             "voice_url",            "" },
    { RbSettings::DoomUrl,              "doom_url",             "" },
    { RbSettings::ReleaseUrl,           "release_url",          "" },
    { RbSettings::DailyUrl,             "daily_url",            "" },
    { RbSettings::ServerConfUrl,        "server_conf_url",      "" },
    { RbSettings::GenlangUrl,           "genlang_url",          "" },
    { RbSettings::ThemesUrl,            "themes_url",           "" },
    { RbSettings::BleedingInfo,         "bleeding_info",        "" },
    { RbSettings::CurPlatformName,      ":platform:/name",      "" },
    { RbSettings::CurManual,            ":platform:/manualname","rockbox-:platform:" },
    { RbSettings::CurBootloaderMethod,  ":platform:/bootloadermethod", "none" },
    { RbSettings::CurBootloaderName,    ":platform:/bootloadername", "" },
    { RbSettings::CurBootloaderFile,    ":platform:/bootloaderfile", "" },
    { RbSettings::CurEncoder,           ":platform:/encoder",   "" },
    { RbSettings::CurResolution,        ":platform:/resolution", "" },
    { RbSettings::CurBrand,             ":platform:/brand",     "" },
    { RbSettings::CurName,              ":platform:/name",      "" },
    { RbSettings::CurBuildserverModel,  ":platform:/buildserver_modelname", "" },
    { RbSettings::CurConfigureModel,    ":platform:/configure_modelname", "" },
    { RbSettings::CurTargetId,          ":platform:/targetid",  "" },
};

// user settings
const static struct {
    RbSettings::UserSettings setting;
    const char* name;
    const char* def;
} UserSettingsList[] = {
    { RbSettings::RbutilVersion,        "rbutil_version",       "" },
    { RbSettings::CurrentPlatform,      "platform",             "" },
    { RbSettings::Mountpoint,           "mountpoint",           "" },
    { RbSettings::CachePath,            "cachepath",            "" },
    { RbSettings::Build,                "build",                "" },
    { RbSettings::ProxyType,            "proxytype",            "" },
    { RbSettings::Proxy,                "proxy",                "" },
    { RbSettings::OfPath,               "ofpath",               "" },
    { RbSettings::Platform,             "platform",             "" },
    { RbSettings::Language,             "lang",                 "" },
    { RbSettings::Tts,                  "tts",                  "" },
    { RbSettings::LastTalkedFolder,     "last_talked_folder",   "" },
    { RbSettings::VoiceLanguage,        "voicelanguage",        "" },
    { RbSettings::TtsLanguage,          ":tts:/language",       "" },
    { RbSettings::TtsOptions,           ":tts:/options",        "" },
    { RbSettings::TtsPath,              ":tts:/path",           "" },
    { RbSettings::TtsVoice,             ":tts:/voice",          "" },
    { RbSettings::EncoderPath,          ":encoder:/encoderpath",        "" },
    { RbSettings::EncoderOptions,       ":encoder:/encoderoptions",     "" },
    { RbSettings::CacheOffline,         "offline",              "false" },
    { RbSettings::CacheDisabled,        "cachedisable",         "false" },
    { RbSettings::TtsUseSapi4,          "sapi/useSapi4",        "false" },
    { RbSettings::EncoderNarrowBand,    ":encoder:/narrowband", "false" },
    { RbSettings::WavtrimThreshold,     "wavtrimthreshold",     "500"},
    { RbSettings::TtsSpeed,             ":tts:/speed",          "0" },
    { RbSettings::EncoderComplexity,    ":encoder:/complexity", "10" },
    { RbSettings::EncoderQuality,       ":encoder:/quality",    "8.0" },
    { RbSettings::EncoderVolume,        ":encoder:/volume",     "1.0" },
};

//! pointer to setting object to NULL
QSettings* RbSettings::systemSettings = NULL;
QSettings* RbSettings::userSettings = NULL;

void RbSettings::ensureRbSettingsExists()
{
    //check and create settings object
    if(systemSettings == NULL)
    {
        // only use built-in rbutil.ini
        systemSettings = new QSettings(":/ini/rbutil.ini", QSettings::IniFormat, 0);
    }

    if(userSettings == NULL)
    {
        // portable installation:
        // check for a configuration file in the program folder.
        QFileInfo config;
        config.setFile(QCoreApplication::instance()->applicationDirPath()
            + "/RockboxUtility.ini");
        if(config.isFile())
        {
            userSettings = new QSettings(QCoreApplication::instance()->applicationDirPath()
                + "/RockboxUtility.ini", QSettings::IniFormat, NULL);
            qDebug() << "config: portable";
        }
        else
        {
            userSettings = new QSettings(QSettings::IniFormat,
            QSettings::UserScope, "rockbox.org", "RockboxUtility",NULL);
            qDebug() << "config: system";
        }
    }
}

void RbSettings::sync()
{
    ensureRbSettingsExists();

    userSettings->sync();
#if defined(Q_OS_LINUX)
    // when using sudo it runs rbutil with uid 0 but unfortunately without a
    // proper login shell, thus the configuration file gets placed in the
    // calling users $HOME. This in turn will cause issues if trying to
    // run rbutil later as user. Try to detect this case via the environment
    // variable SUDO_UID and SUDO_GID and if set chown the user config file.
    if(getuid() == 0)
    {
        char* realuser = getenv("SUDO_UID");
        char* realgroup = getenv("SUDO_GID");
        if(realuser != NULL && realgroup != NULL)
        {
            int realuid = atoi(realuser);
            int realgid = atoi(realgroup);
            // chown is attribute warn_unused_result, but in case this fails
            // we can't do anything useful about it. Notifying the user
            // is somewhat pointless. Add hack to suppress compiler warning.
            if(chown(qPrintable(userSettings->fileName()), realuid, realgid));
        }
    }
#endif
}

QString RbSettings::userSettingFilename()
{
    ensureRbSettingsExists();
    return userSettings->fileName();
}

QVariant RbSettings::value(enum SystemSettings setting)
{
    ensureRbSettingsExists();

    // locate setting item
    int i = 0;
    while(SystemSettingsList[i].setting != setting)
        i++;

    QString s = constructSettingPath(SystemSettingsList[i].name);
    QString d = SystemSettingsList[i].def;
    d.replace(":platform:", userSettings->value("platform").toString());
    qDebug() << "[Settings] GET S:" << s << systemSettings->value(s, d).toString();
    return systemSettings->value(s, d);
}

QVariant RbSettings::value(enum UserSettings setting)
{
    QString empty;
    return subValue(empty, setting);
}

QVariant RbSettings::subValue(QString sub, enum UserSettings setting)
{
    ensureRbSettingsExists();

    // locate setting item
    int i = 0;
    while(UserSettingsList[i].setting != setting)
        i++;

    QString s = constructSettingPath(UserSettingsList[i].name, sub);
    qDebug() << "[Settings] GET U:" << s << userSettings->value(s).toString();
    return userSettings->value(s, UserSettingsList[i].def);
}

void RbSettings::setValue(enum UserSettings setting , QVariant value)
{
   QString empty;
   return setSubValue(empty, setting, value);
}

void RbSettings::setSubValue(QString sub, enum UserSettings setting, QVariant value)
{
    ensureRbSettingsExists();

    // locate setting item
    int i = 0;
    while(UserSettingsList[i].setting != setting)
        i++;

    QString s = constructSettingPath(UserSettingsList[i].name, sub);
    qDebug() << "[Settings] SET U:" << s << userSettings->value(s).toString();
    userSettings->setValue(s, value);
}

QStringList RbSettings::platforms()
{
    ensureRbSettingsExists();

    QStringList result;
    systemSettings->beginGroup("platforms");
    QStringList a = systemSettings->childKeys();
    for(int i = 0; i < a.size(); i++)
    {
        result.append(systemSettings->value(a.at(i), "null").toString());
    }
    systemSettings->endGroup();
    return result;
}

QStringList RbSettings::languages()
{
    ensureRbSettingsExists();

    QStringList result;
    systemSettings->beginGroup("languages");
    QStringList a = systemSettings->childKeys();
    for(int i = 0; i < a.size(); i++)
    {
        result.append(systemSettings->value(a.at(i), "null").toString());
    }
    systemSettings->endGroup();
    return result;
}

QString RbSettings::name(QString platform)
{
    ensureRbSettingsExists();
    return systemSettings->value(platform + "/name").toString();
}

QString RbSettings::brand(QString platform)
{
    ensureRbSettingsExists();
    return systemSettings->value(platform + "/brand").toString();
}

QMap<int, QString> RbSettings::usbIdMap(enum MapType type)
{
    ensureRbSettingsExists();

    QMap<int, QString> map;
    // get a list of ID -> target name
    QStringList platforms;
    systemSettings->beginGroup("platforms");
    platforms = systemSettings->childKeys();
    systemSettings->endGroup();

    QString t;
    switch(type) {
        case MapDevice:
            t = "usbid";
            break;
        case MapError:
            t = "usberror";
            break;
        case MapIncompatible:
            t = "usbincompat";
            break;
    }

    for(int i = 0; i < platforms.size(); i++)
    {
        systemSettings->beginGroup("platforms");
        QString target = systemSettings->value(platforms.at(i)).toString();
        systemSettings->endGroup();
        systemSettings->beginGroup(target);
        QStringList ids = systemSettings->value(t).toStringList();
        int j = ids.size();
        while(j--)
            map.insert(ids.at(j).toInt(0, 16), target);

        systemSettings->endGroup();
    }
    return map;
}


QString RbSettings::constructSettingPath(QString path, QString substitute)
{
    QString platform = userSettings->value("platform").toString();
    if(!substitute.isEmpty()) {
        path.replace(":tts:", substitute);
        path.replace(":encoder:", substitute);
    }
    else {
        path.replace(":tts:", userSettings->value("tts").toString());
        path.replace(":encoder:", systemSettings->value(platform + "/encoder").toString());
    }
    path.replace(":platform:", platform);

    return path;
}