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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
#include <QSharedData>
#include <QBuffer>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include "appinfo.h"
#include "unpacker.h"
namespace {
struct ResourceEntry {
int index;
quint32 offset;
quint32 length;
quint32 crc;
};
}
struct AppInfoData : public QSharedData {
QUuid uuid;
QString shortName;
QString longName;
QString companyName;
int versionCode;
QString versionLabel;
bool watchface;
bool jskit;
AppInfo::Capabilities capabilities;
QHash<QString, int> keyInts;
QHash<int, QString> keyNames;
bool menuIcon;
int menuIconResource;
};
QLoggingCategory AppInfo::l("AppInfo");
AppInfo::AppInfo() : Bundle(), d(new AppInfoData)
{
d->versionCode = 0;
d->watchface = false;
d->jskit = false;
d->capabilities = 0;
d->menuIcon = false;
d->menuIconResource = -1;
}
AppInfo::AppInfo(const AppInfo &rhs) : Bundle(rhs), d(rhs.d)
{}
AppInfo::AppInfo(const Bundle &rhs) : Bundle(rhs), d(new AppInfoData)
{}
AppInfo &AppInfo::operator=(const AppInfo &rhs)
{
if (this != &rhs)
d.operator=(rhs.d);
return *this;
}
AppInfo::~AppInfo()
{}
bool AppInfo::isLocal() const
{
return ! path().isEmpty();
}
bool AppInfo::isValid() const
{
return ! d->uuid.isNull();
}
void AppInfo::setInvalid()
{
d->uuid = QUuid(); // Clear the uuid to force invalid app
}
QUuid AppInfo::uuid() const
{
return d->uuid;
}
QString AppInfo::shortName() const
{
return d->shortName;
}
QString AppInfo::longName() const
{
return d->longName;
}
QString AppInfo::companyName() const
{
return d->companyName;
}
int AppInfo::versionCode() const
{
return d->versionCode;
}
QString AppInfo::versionLabel() const
{
return d->versionLabel;
}
bool AppInfo::isWatchface() const
{
return d->watchface;
}
bool AppInfo::isJSKit() const
{
return d->jskit;
}
AppInfo::Capabilities AppInfo::capabilities() const
{
return d->capabilities;
}
void AppInfo::addAppKey(const QString &key, int value)
{
d->keyInts.insert(key, value);
d->keyNames.insert(value, key);
}
bool AppInfo::hasAppKeyValue(int value) const
{
return d->keyNames.contains(value);
}
QString AppInfo::appKeyForValue(int value) const
{
return d->keyNames.value(value);
}
bool AppInfo::hasAppKey(const QString &key) const
{
return d->keyInts.contains(key);
}
int AppInfo::valueForAppKey(const QString &key) const
{
return d->keyInts.value(key, -1);
}
bool AppInfo::hasMenuIcon() const
{
return d->menuIcon && d->menuIconResource >= 0;
}
QImage AppInfo::getMenuIconImage() const
{
if (hasMenuIcon()) {
QScopedPointer<QIODevice> imageRes(openFile(AppInfo::RESOURCES));
QByteArray data = extractFromResourcePack(imageRes.data(), d->menuIconResource);
if (!data.isEmpty()) {
return decodeResourceImage(data);
}
}
return QImage();
}
QByteArray AppInfo::getMenuIconPng() const
{
QByteArray data;
QBuffer buf(&data);
buf.open(QIODevice::WriteOnly);
getMenuIconImage().save(&buf, "PNG");
buf.close();
return data;
}
QString AppInfo::getJSApp() const
{
if (!isValid() || !isLocal()) return QString();
QScopedPointer<QIODevice> appJS(openFile(AppInfo::APPJS, QIODevice::Text));
if (!appJS) {
qCWarning(l) << "cannot find app" << d->shortName << "app.js";
return QString();
}
return QString::fromUtf8(appJS->readAll());
}
AppInfo AppInfo::fromPath(const QString &path)
{
AppInfo info(Bundle::fromPath(path));
if (!info.isValid()) {
return AppInfo();
}
QScopedPointer<QIODevice> appInfoJSON(info.openFile(AppInfo::INFO, QIODevice::Text));
if (!appInfoJSON) {
qCWarning(l) << "cannot find app" << path << "info json";
return AppInfo();
}
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(appInfoJSON->readAll(), &parseError);
if (parseError.error != QJsonParseError::NoError) {
qCWarning(l) << "cannot parse app" << path << "info json" << parseError.errorString();
return AppInfo();
}
appInfoJSON->close();
const QJsonObject root = doc.object();
info.d->uuid = QUuid(root["uuid"].toString());
info.d->shortName = root["shortName"].toString();
info.d->longName = root["longName"].toString();
info.d->companyName = root["companyName"].toString();
info.d->versionCode = root["versionCode"].toInt();
info.d->versionLabel = root["versionLabel"].toString();
const QJsonObject watchapp = root["watchapp"].toObject();
info.d->watchface = watchapp["watchface"].toBool();
info.d->jskit = info.fileExists(AppInfo::APPJS);
if (root.contains("capabilities")) {
const QJsonArray capabilities = root["capabilities"].toArray();
AppInfo::Capabilities caps = 0;
for (auto it = capabilities.constBegin(); it != capabilities.constEnd(); ++it) {
QString cap = (*it).toString();
if (cap == "location") caps |= AppInfo::Location;
if (cap == "configurable") caps |= AppInfo::Configurable;
}
info.d->capabilities = caps;
}
if (root.contains("appKeys")) {
const QJsonObject appkeys = root["appKeys"].toObject();
for (auto it = appkeys.constBegin(); it != appkeys.constEnd(); ++it) {
info.addAppKey(it.key(), it.value().toInt());
}
}
if (root.contains("resources")) {
const QJsonObject resources = root["resources"].toObject();
const QJsonArray media = resources["media"].toArray();
int index = 0;
for (auto it = media.constBegin(); it != media.constEnd(); ++it) {
const QJsonObject res = (*it).toObject();
const QJsonValue menuIcon = res["menuIcon"];
switch (menuIcon.type()) {
case QJsonValue::Bool:
info.d->menuIcon = menuIcon.toBool();
info.d->menuIconResource = index;
break;
case QJsonValue::String:
info.d->menuIcon = !menuIcon.toString().isEmpty();
info.d->menuIconResource = index;
break;
default:
break;
}
index++;
}
}
if (info.uuid().isNull() || info.shortName().isEmpty()) {
qCWarning(l) << "invalid or empty uuid/name in json of" << path;
return AppInfo();
}
return info;
}
AppInfo AppInfo::fromSlot(const BankManager::SlotInfo &slot)
{
AppInfo info;
info.d->uuid = QUuid::createUuid();
info.d->shortName = slot.name;
info.d->companyName = slot.company;
info.d->versionCode = slot.version;
info.d->capabilities = AppInfo::Capabilities(slot.flags);
return info;
}
QByteArray AppInfo::extractFromResourcePack(QIODevice *dev, int wanted_id) const
{
if (!dev) {
qCWarning(l) << "requested resource" << wanted_id
<< "from NULL resource file";
return QByteArray();
}
QByteArray data = dev->readAll();
Unpacker u(data);
int num_files = u.readLE<quint32>();
u.readLE<quint32>(); // crc for entire file
u.readLE<quint32>(); // timestamp
qCDebug(l) << "reading" << num_files << "resources";
QList<ResourceEntry> table;
for (int i = 0; i < num_files; i++) {
ResourceEntry e;
e.index = u.readLE<quint32>();
e.offset = u.readLE<quint32>();
e.length = u.readLE<quint32>();
e.crc = u.readLE<quint32>();
if (u.bad()) {
qCWarning(l) << "short read on resource file";
return QByteArray();
}
table.append(e);
}
if (wanted_id >= table.size()) {
qCWarning(l) << "specified resource does not exist";
return QByteArray();
}
const ResourceEntry &e = table[wanted_id];
int offset = 12 + 256 * 16 + e.offset;
return data.mid(offset, e.length);
}
QImage AppInfo::decodeResourceImage(const QByteArray &data) const
{
Unpacker u(data);
int scanline = u.readLE<quint16>();
u.skip(sizeof(quint16) + sizeof(quint32));
int width = u.readLE<quint16>();
int height = u.readLE<quint16>();
QImage img(width, height, QImage::Format_MonoLSB);
const uchar *src = reinterpret_cast<const uchar *>(&data.constData()[12]);
for (int line = 0; line < height; ++line) {
memcpy(img.scanLine(line), src, qMin(scanline, img.bytesPerLine()));
src += scanline;
}
return img;
}
|