-
Notifications
You must be signed in to change notification settings - Fork 16
/
chumpackage.cpp
333 lines (279 loc) · 10.8 KB
/
chumpackage.cpp
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
#include "chumpackage.h"
#include "projectgithub.h"
#include "projectgitlab.h"
#include "projectforgejo.h"
#include <PackageKit/Daemon>
#include <PackageKit/Details>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRegularExpression>
#include <yaml-cpp/yaml.h>
using namespace PackageKit;
#define SET_IF_EMPTY(var, role, value) { \
if (!var.isEmpty() || var==value) return; \
var = value; \
emit updated(m_id, role); \
}
ChumPackage::ChumPackage(QObject *parent)
: QObject{parent}
{
m_issue_info = new LoadableObject(this);
m_issues = new LoadableObject(this);
m_release_info = new LoadableObject(this);
m_releases = new LoadableObject(this);
}
ChumPackage::ChumPackage(const QString &id, QObject *parent)
: ChumPackage(parent)
{
m_id = id;
}
static QString nameFormatting(const QString &name, const QString &login) {
if (!login.isEmpty() && !name.isEmpty())
return QStringLiteral("%1 (%2)").arg(name, login);
if (!login.isEmpty()) return login;
if (!name.isEmpty()) return name;
return QString();
}
QString ChumPackage::developer() const {
return nameFormatting(m_developer_name, m_developer_login);
}
QString ChumPackage::packager() const {
return nameFormatting(m_packager_name, m_packager_login);
}
bool ChumPackage::installed() const {
return !m_installed_version.isEmpty();
}
LoadableObject* ChumPackage::issue(const QString &id) {
if (m_project != nullptr)
m_project->issue(id, m_issue_info);
else
m_issue_info->setEmpty();
return m_issue_info;
}
LoadableObject* ChumPackage::issues() {
if (m_issues->ready()) return m_issues;
if (m_project != nullptr)
m_project->issues(m_issues);
else
m_issues->setEmpty();
return m_issues;
}
LoadableObject* ChumPackage::release(const QString &id) {
if (m_project != nullptr)
m_project->release(id, m_release_info);
else
m_release_info->setEmpty();
return m_release_info;
}
LoadableObject* ChumPackage::releases() {
if (m_releases->ready()) return m_releases;
if (m_project != nullptr)
m_project->releases(m_releases);
else
m_releases->setEmpty();
return m_releases;
}
void ChumPackage::setPkidLatest(const QString &pkid) {
if (m_pkid_latest == pkid) return;
m_pkid_latest = pkid;
if (m_pkid_latest.isEmpty()) {
m_details_update = false;
return;
}
m_details_update = true;
}
void ChumPackage::setUpdateAvailable(bool up) {
if (up == m_update_available) return;
m_update_available = up;
emit updated(m_id, PackageUpdateAvailableRole);
}
void ChumPackage::setDetails(const PackageKit::Details &v) {
m_details_update = false;
m_available_version = Daemon::packageVersion(v.packageId());
m_description = v.description();
m_summary = v.summary();
m_url = v.url();
m_license = v.license();
m_size = v.size();
// derive name
QString pname = Daemon::packageName(m_pkid_latest);
m_package_name = pname;
m_name = QString{};
QStringList nparts = pname.split('-');
bool is_app = false;
bool is_lib = false;
if (nparts.size() > 0) {
if (nparts.first() == QLatin1String("harbour")) {
is_app = true;
nparts.removeFirst();
} else if (nparts.first() == QLatin1String("openrepos"))
nparts.removeFirst();
else if (nparts.first().startsWith(QLatin1String("lib"))) {
is_lib = true;
nparts.first() = nparts.first().mid(3);
//% "Library"
nparts.insert(1, qtTrId("chum-desc-library"));
}
if (nparts.last() == QLatin1String("devel")) {
is_lib = true;
//% "Development"
nparts.last() = qtTrId("chum-desc-development");
}
}
for (const QString& b: nparts)
m_name += b.at(0).toUpper() + b.mid(1).toLower() + " ";
m_name = m_name.trimmed();
// parse description
QStringList descLines = m_description.split(QRegularExpression("(?m)^\\s*$"), QString::SkipEmptyParts);
QByteArray metainjson;
YAML::Node metayaml;
if (descLines.size() > 0) {
try {
metayaml = YAML::Load(descLines.last().toStdString());
} catch(const YAML::ParserException &e) {
// ignore it, probably not chum section to start with
}
}
if (!metayaml.IsNull() && metayaml.size() > 0) {
YAML::Emitter emitter;
emitter << YAML::DoubleQuoted << YAML::Flow << YAML::BeginSeq << metayaml;
std::string out(emitter.c_str() + 1); // Strip leading [ character
metainjson = QByteArray::fromStdString(out);
metainjson = metainjson.replace("~", "\"\"");
//remove yaml from list
descLines.pop_back();
}
// drop newlines from description paragraphs
for (int i=0; i < descLines.length(); ++i)
descLines[i] = descLines[i].replace('\n', ' ').simplified();
// Reconstruct the description
m_description = descLines.join("\n\n");
// Parse metadata
QJsonObject json{QJsonDocument::fromJson(metainjson).object()};
if (json.value("PackageName").isUndefined()) {
m_name = json.value("Title").toString(m_name);
} else {
m_name = json.value("PackageName").toString(m_name); // spec v0 legacy
}
QString typestr = json.value("Type").toString(is_app ?
QStringLiteral("desktop-application") :
QStringLiteral("generic"));
if (typestr == QStringLiteral("desktop-application")) m_type = PackageApplicationDesktop;
else if (typestr == QStringLiteral("console-application")) m_type = PackageApplicationConsole;
else m_type = PackageGeneric;
m_developer_name = json.value("DeveloperName").toString();
m_developer_name_from_spec = !m_developer_name.isEmpty();
m_packager_name = json.value("PackagedBy").toString();
if (m_packager_name.isEmpty())
m_packager_name = json.value("PackagerName").toString(); // spec v0 legacy
m_packager_name_from_spec = !m_packager_name.isEmpty();
m_categories = json.value("Categories").toVariant().toStringList();
// guess category only if it is empty
if (is_lib && m_categories.isEmpty())
m_categories.push_back(QStringLiteral("Library"));
if (m_categories.isEmpty()) m_categories.push_back(QStringLiteral("Other"));
m_repo_url = json.value("Custom").toObject().value("Repo").toString();
m_packaging_repo_url = json.value("Custom").toObject().value("PackagingRepo").toString();
m_description_md_url = json.value("Custom").toObject().value("DescriptionMD").toString();
m_icon = json.value("PackageIcon").toString();
if (m_icon.isEmpty()) m_icon = json.value("Icon").toString(); // spec v0 legacy
m_screenshots = json.value("Screenshots").toVariant().toStringList();
if (json.value("Url").isUndefined()) { // spec v0 legacy
m_url = json.value("Links").toObject().value("Homepage").toString(m_url);
m_url_forum = json.value("Links").toObject().value("Help").toString();
m_url_issues = json.value("Links").toObject().value("Bugtracker").toString();
m_donation = json.value("Links").toObject().value("Donation").toString();
} else {
m_url = json.value("Url").toObject().value("Homepage").toString(m_url);
m_url_forum = json.value("Url").toObject().value("Help").toString();
m_url_issues = json.value("Url").toObject().value("Bugtracker").toString();
m_donation = json.value("Url").toObject().value("Donation").toString();
}
for (const QString &u: {m_packaging_repo_url, m_repo_url, m_url}) {
if (ProjectGitHub::isProject(u))
m_project = new ProjectGitHub(u, this);
else if (ProjectGitLab::isProject(u))
m_project = new ProjectGitLab(u, this);
else if (ProjectForgejo::isProject(u))
m_project = new ProjectForgejo(u, this);
if (m_project) break;
}
emit updated(m_id, PackageRefreshRole);
}
void ChumPackage::clearInstalled() {
setPkidInstalled(QString{});
}
void ChumPackage::setPkidInstalled(const QString &pkid) {
if (m_pkid_installed == pkid) return;
m_pkid_installed = pkid;
setInstalledVersion(Daemon::packageVersion(pkid));
}
void ChumPackage::setInstalledVersion(const QString &v)
{
if (v == m_installed_version) return;
m_installed_version = v;
emit updated(m_id, PackageInstalledVersionRole);
emit updated(m_id, PackageInstalledRole);
if (type() == PackageApplicationDesktop && installed()) {
auto trfl = Daemon::getFiles(m_pkid_installed);
connect(trfl, &Transaction::files, this, [this](
const QString &packageID, const QStringList &filenames)
{
for (auto f : filenames) {
if (f.endsWith(QStringLiteral(".desktop")))
{
m_desktopFile = f;
emit updated(m_id, PackageDesktopFileRole);
break;
}
}
});
}
}
void ChumPackage::setDeveloperLogin(const QString &login) {
// If developer name from the spec file was used, then do not set a separate login name.
// As it is impossible to provide a separate login name via RPM spec file, this prevents
// conflicting records,
if (m_developer_name_from_spec) return;
SET_IF_EMPTY(m_developer_login, PackageDeveloperRole, login);
}
void ChumPackage::setDeveloperName(const QString &name) {
SET_IF_EMPTY(m_developer_name, PackageDeveloperRole, name);
}
void ChumPackage::setPackagerLogin(const QString &login) {
// If packager name from the spec file was used, then do not set a separate packager name.
// As it is impossible to provide a separate packager name via RPM spec file, this prevents
// conflicting records.
if (m_packager_name_from_spec) return;
SET_IF_EMPTY(m_packager_login, PackagePackagerRole, login);
}
void ChumPackage::setPackagerName(const QString &name) {
SET_IF_EMPTY(m_packager_name, PackagePackagerRole, name);
}
void ChumPackage::setUrl(const QString &url) {
SET_IF_EMPTY(m_url, PackageOtherRole, url);
}
void ChumPackage::setUrlForum(const QString &url) {
SET_IF_EMPTY(m_url_forum, PackageOtherRole, url);
}
void ChumPackage::setUrlIssues(const QString &url) {
SET_IF_EMPTY(m_url_issues, PackageOtherRole, url);
}
void ChumPackage::setForksCount(int count) {
m_forks_count = count;
emit updated(m_id, PackageOtherRole);
}
void ChumPackage::setIssuesCount(int count) {
m_issues_count = count;
emit updated(m_id, PackageOtherRole);
}
void ChumPackage::setReleasesCount(int count) {
m_releases_count = count;
emit updated(m_id, PackageOtherRole);
}
void ChumPackage::setStarsCount(int count) {
m_stars_count = count;
emit updated(m_id, PackageStarsCountRole);
}