summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/playerbuildinfo.cpp
blob: fb8b121e8f6d5cc8d930aebaf8c6a53e31e2d97c (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2020 by Dominik Riebeling
 *
 * 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 "playerbuildinfo.h"
#include "rbsettings.h"
#include "Logger.h"

PlayerBuildInfo* PlayerBuildInfo::infoInstance = nullptr;

PlayerBuildInfo* PlayerBuildInfo::instance()
{
    if (infoInstance == nullptr) {
        infoInstance = new PlayerBuildInfo();
    }
    return infoInstance;
}

// server infos
const static struct {
    PlayerBuildInfo::BuildInfo item;
    const char* name;
} ServerInfoList[] = {
    { PlayerBuildInfo::BuildVoiceLangs,   "voices/:version:"    },
    { PlayerBuildInfo::BuildVersion,      ":build:/:target:"     },
    { PlayerBuildInfo::BuildUrl,          ":build:/build_url"   },
    { PlayerBuildInfo::BuildVoiceUrl,     ":build:/voice_url"   },
    { PlayerBuildInfo::BuildManualUrl,    ":build:/manual_url"  },
    { PlayerBuildInfo::BuildSourceUrl,    ":build:/source_url"  },
    { PlayerBuildInfo::BuildFontUrl,      ":build:/font_url"    },

    // other URLs -- those are not directly related to the build, but handled here.
    { PlayerBuildInfo::DoomUrl,           "other/doom_url"      },
    { PlayerBuildInfo::Duke3DUrl,         "other/duke3d_url"    },
    { PlayerBuildInfo::PuzzFontsUrl,      "other/puzzfonts_url" },
    { PlayerBuildInfo::QuakeUrl,          "other/quake_url"     },
    { PlayerBuildInfo::Wolf3DUrl,         "other/wolf3d_url"    },
    { PlayerBuildInfo::XWorldUrl,         "other/xworld_url"    },
    { PlayerBuildInfo::MidiPatchsetUrl,   "other/patcheset_url" },
};

const static struct {
    PlayerBuildInfo::DeviceInfo item;
    const char* name;
} PlayerInfoList[] = {
    { PlayerBuildInfo::BuildStatus,       "status/:target:"            },
    { PlayerBuildInfo::DisplayName,       ":target:/name"              },
    { PlayerBuildInfo::BootloaderMethod,  ":target:/bootloadermethod"  },
    { PlayerBuildInfo::BootloaderName,    ":target:/bootloadername"    },
    { PlayerBuildInfo::BootloaderFile,    ":target:/bootloaderfile"    },
    { PlayerBuildInfo::BootloaderFilter,  ":target:/bootloaderfilter"  },
    { PlayerBuildInfo::Encoder,           ":target:/encoder"           },
    { PlayerBuildInfo::Brand,             ":target:/brand"             },
    { PlayerBuildInfo::PlayerPicture,     ":target:/playerpic"         },
};

const static struct {
    PlayerBuildInfo::SystemUrl item;
    const char* name;
} PlayerSystemUrls[] = {
    { PlayerBuildInfo::BootloaderUrl,     "bootloader/download_url" },
    { PlayerBuildInfo::BuildInfoUrl,      "build_info_url"          },
    { PlayerBuildInfo::GenlangUrl,        "genlang_url"             },
    { PlayerBuildInfo::ThemesUrl,         "themes_url"              },
    { PlayerBuildInfo::ThemesInfoUrl,     "themes_info_url"         },
    { PlayerBuildInfo::RbutilUrl,         "rbutil_url"              },
};

PlayerBuildInfo::PlayerBuildInfo() :
     serverInfo(nullptr),
     playerInfo(":/ini/rbutil.ini", QSettings::IniFormat)
{

}

void PlayerBuildInfo::setBuildInfo(QString file)
{
    if (serverInfo)
        delete serverInfo;
    LOG_INFO() << "updated:" << file;
    serverInfo = new QSettings(file, QSettings::IniFormat);
}

QVariant PlayerBuildInfo::value(BuildInfo item, BuildType type)
{
    // locate setting item in server info file
    int i = 0;
    while(ServerInfoList[i].item != item)
        i++;

    // split of variant for target.
    // we can have an optional variant part in the target string.
    // For build info we don't use that.
    QString target = RbSettings::value(RbSettings::CurrentPlatform).toString().split('.').at(0);

    QString s = ServerInfoList[i].name;
    s.replace(":target:", target);
    QString v;
    switch(type) {
    case TypeRelease:
        v = "release";
        break;
    case TypeCandidate:
        v = "release-candidate";
        break;
    case TypeDaily:
        v = "daily";
        break;
    case TypeDevel:
        v = "development";
        break;
    }

    QVariant result = QString();
    if (!serverInfo)
        return result;
    QStringList version = serverInfo->value(v + "/" + target, "").toStringList();
    s.replace(":build:", v);
    s.replace(":version:", version.at(0));

    // get value from server build-info
    // we need to get a version string, otherwise the data is invalid.
    // For invalid data return an empty string.
    if(version.at(0).isEmpty()) {
        LOG_INFO() << s << "(version invalid)";
        return result;
    }
    if(!s.isEmpty())
        result = serverInfo->value(s);

    // depending on the actual value we need more replacements.
    switch(item) {
    case BuildVersion:
        result = result.toStringList().at(0);
        break;

    case BuildUrl:
        if(version.size() > 1) {
            // version info has an URL appended. Takes precendence.
            result = version.at(1);
        }
        break;

    case BuildVoiceLangs:
        if (type == TypeDaily)
            s = "voices/daily";
        result = serverInfo->value(s);
        break;

    case BuildManualUrl:
    {
        // special case: if playerInfo has a non-empty manualname entry for the
        // target, use that as target for the manual name.
        QString manualtarget = playerInfo.value(target + "/manualname", "").toString();
        if(!manualtarget.isEmpty())
            target = manualtarget;
        break;
    }

    default:
        break;
    }
    // if the value is a string we can replace some patterns.
    // if we cannot convert it (f.e. for a QStringList) we leave as-is, since
    // the conversion would return an empty type.
    if (result.canConvert(QMetaType::QString))
        result = result.toString()
                    .replace("%TARGET%", target)
                    .replace("%VERSION%", version.at(0));

    LOG_INFO() << "B:" << s << result;
    return result;
}

QVariant PlayerBuildInfo::value(DeviceInfo item, QString target)
{
    // locate setting item in server info file
    int i = 0;
    while(PlayerInfoList[i].item != item)
        i++;

    // split of variant for target.
    // we can have an optional variant part in the target string.
    // For device info we use this.
    if (target.isEmpty())
        target = RbSettings::value(RbSettings::CurrentPlatform).toString();

    QVariant result = QString();

    QString s = PlayerInfoList[i].name;
    s.replace(":target:", target);

    switch(item) {
    case BuildStatus:
    {
        // build status is the only value that doesn't depend on the version
        // but the selected target instead.
        bool ok = false;
        if (serverInfo)
            result = serverInfo->value(s).toInt(&ok);
        if (!ok)
            result = -1;
        break;
    }

    default:
        result = playerInfo.value(s);
        break;
    }

    LOG_INFO() << "T:" << s << result;
    return result;
}

QVariant PlayerBuildInfo::value(SystemUrl item)
{
    // locate setting item in server info file
    int i = 0;
    while(PlayerSystemUrls[i].item != item)
        i++;

    QVariant result = playerInfo.value(PlayerSystemUrls[i].name);
    LOG_INFO() << "U:" << PlayerSystemUrls[i].name << result;
    return result;
}


QString PlayerBuildInfo::statusAsString(QString platform)
{
    QString result;
    switch(value(BuildStatus, platform).toInt())
    {
    case STATUS_RETIRED:
        result = tr("Stable (Retired)");
        break;
    case STATUS_UNUSABLE:
        result = tr("Unusable");
        break;
    case STATUS_UNSTABLE:
        result = tr("Unstable");
        break;
    case STATUS_STABLE:
        result = tr("Stable");
        break;
    default:
        result = tr("Unknown");
        break;
    }

    return result;
}