blob: 64061c89c5e295895750880a96b444af4a367afe (
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
|
#include "bundle.h"
#include <QVariantMap>
#include <QFileInfo>
#include <QDebug>
#include <QJsonParseError>
Bundle::Bundle(const QString &path):
m_path(path)
{
}
QString Bundle::path() const
{
return m_path;
}
QString Bundle::file(Bundle::FileType type, HardwarePlatform hardwarePlatform) const
{
// Those two will always be in the top level dir. HardwarePlatform is irrelevant.
switch (type) {
case FileTypeAppInfo:
return m_path + "/appInfo.js";
case FileTypeJsApp:
return m_path + "/pebble-js-app.js";
default:
;
}
// For all the others we have to find the manifest file
QList<QString> possibleDirs;
switch (hardwarePlatform) {
case HardwarePlatformAplite:
if (QFileInfo::exists(path() + "/aplite/")) {
possibleDirs.append("aplite");
}
possibleDirs.append("");
break;
case HardwarePlatformBasalt:
if (QFileInfo::exists(path() + "/basalt/")) {
possibleDirs.append("basalt");
}
possibleDirs.append("");
break;
case HardwarePlatformChalk:
if (QFileInfo::exists(path() + "/chalk/")) {
possibleDirs.append("chalk");
}
break;
default:
possibleDirs.append("");
;
}
QString manifestFilename;
QString subDir;
foreach (const QString &dir, possibleDirs) {
if (QFileInfo::exists(m_path + "/" + dir + "/manifest.json")) {
subDir = "/" + dir;
manifestFilename = m_path + subDir + "/manifest.json";
break;
}
}
if (manifestFilename.isEmpty()) {
qWarning() << "Error finding manifest.json";
return QString();
}
// We want the manifiest file. just return it without parsing it
if (type == FileTypeManifest) {
return manifestFilename;
}
QFile manifest(manifestFilename);
if (!manifest.open(QFile::ReadOnly)) {
qWarning() << "Error opening" << manifestFilename;
return QString();
}
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(manifest.readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qWarning() << "Error parsing" << manifestFilename;
return QString();
}
QVariantMap manifestMap = jsonDoc.toVariant().toMap();
switch (type) {
case FileTypeApplication:
return m_path + subDir + "/" + manifestMap.value("application").toMap().value("name").toString();
case FileTypeResources:
if (manifestMap.contains("resources")) {
return m_path + subDir + "/" + manifestMap.value("resources").toMap().value("name").toString();
}
break;
case FileTypeWorker:
if (manifestMap.contains("worker")) {
return m_path + subDir + "/" + manifestMap.value("worker").toMap().value("name").toString();
}
break;
case FileTypeFirmware:
if (manifestMap.contains("firmware")) {
return m_path + subDir + "/" + manifestMap.value("firmware").toMap().value("name").toString();
}
break;
default:
;
}
return QString();
}
quint32 Bundle::crc(Bundle::FileType type, HardwarePlatform hardwarePlatform) const
{
switch (type) {
case FileTypeAppInfo:
case FileTypeJsApp:
case FileTypeManifest:
qWarning() << "Cannot get crc for file type" << type;
return 0;
default: ;
}
QFile manifest(file(FileTypeManifest, hardwarePlatform));
if (!manifest.open(QFile::ReadOnly)) {
qWarning() << "Error opening manifest file";
return 0;
}
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(manifest.readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qWarning() << "Error parsing manifest file";
return 0;
}
QVariantMap manifestMap = jsonDoc.toVariant().toMap();
switch (type) {
case FileTypeApplication:
return manifestMap.value("application").toMap().value("crc").toUInt();
case FileTypeResources:
return manifestMap.value("resources").toMap().value("crc").toUInt();
case FileTypeWorker:
return manifestMap.value("worker").toMap().value("crc").toUInt();
case FileTypeFirmware:
return manifestMap.value("firmware").toMap().value("crc").toUInt();
default:
;
}
return 0;
}
|