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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "extensionsmodel.h"
#include "extensionmanagertr.h"
#include "remotespec.h"
#include <utils/algorithm.h>
#include <utils/hostosinfo.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <extensionsystem/iplugin.h>
#include <extensionsystem/pluginspec.h>
#include <extensionsystem/pluginview.h>
#include <extensionsystem/pluginmanager.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardItemModel>
#include <QVersionNumber>
using namespace ExtensionSystem;
using namespace Core;
using namespace Utils;
namespace ExtensionManager::Internal {
Q_LOGGING_CATEGORY(modelLog, "qtc.extensionmanager.model", QtWarningMsg)
class ExtensionsModelPrivate
{
public:
void addUnlistedLocalPlugins();
static QVariant dataFromRemotePack(const RemoteSpec *spec, int role);
static QVariant dataFromRemotePlugin(const RemoteSpec *spec, int role);
QVariant dataFromRemoteExtension(int index, int role) const;
QVariant dataFromLocalPlugin(int index, int role) const;
//QJsonArray responseItems;
PluginSpecs localPlugins;
std::vector<std::unique_ptr<RemoteSpec>> remotePlugins;
};
void ExtensionsModelPrivate::addUnlistedLocalPlugins()
{
QSet<QString> remoteIds = Utils::transform<QSet>(remotePlugins, &RemoteSpec::id);
localPlugins = Utils::filtered(PluginManager::plugins(), [&remoteIds](const PluginSpec *plugin) {
return !remoteIds.contains(plugin->id());
});
qCDebug(modelLog) << "Number of extensions from JSON:" << remotePlugins.size();
qCDebug(modelLog) << "Number of added local plugins:" << localPlugins.count();
}
QString joinedStringList(const QJsonValue &json)
{
if (json.isArray()) {
const QStringList lines = json.toVariant().toStringList();
return lines.join("\n");
}
return json.toString();
}
QString descriptionWithLinks(const QString &description, const QString &url,
const QString &documentationUrl)
{
QStringList fragments;
const QString mdLink("[%1](%2)");
if (!url.isEmpty())
fragments.append(mdLink.arg(url).arg(url));
if (!documentationUrl.isEmpty())
fragments.append(mdLink.arg(Tr::tr("Documentation")).arg(documentationUrl));
if (!fragments.isEmpty())
fragments.prepend("### " + Tr::tr("More Information"));
fragments.prepend(description);
return fragments.join("\n\n");
}
QVariant ExtensionsModelPrivate::dataFromRemoteExtension(int index, int role) const
{
QTC_ASSERT(index >= 0 && size_t(index) < remotePlugins.size(), return {});
const RemoteSpec *remoteSpec = remotePlugins.at(index).get();
switch (role) {
case RoleSpec:
return QVariant::fromValue(remoteSpec);
case Qt::DisplayRole:
case RoleName:
return remoteSpec->displayName();
case RoleDownloadCount:
return remoteSpec->downloads();
case RoleFullId:
return QString("%1.%2").arg(remoteSpec->vendorId()).arg(remoteSpec->id());
case RoleId:
return remoteSpec->id();
case RoleDateUpdated:
return remoteSpec->updatedAt();
case RoleStatus:
return remoteSpec->statusString();
case RoleTags:
return remoteSpec->tags();
case RoleVendor:
return remoteSpec->vendor();
case RoleVendorId:
return remoteSpec->vendorId();
case RoleCopyright:
return remoteSpec->copyright();
case RoleDownloadUrl: {
for (const auto &source : remoteSpec->sources()) {
if (!source.platform)
return source.url;
if (source.platform->os == HostOsInfo::hostOs()
&& source.platform->architecture == HostOsInfo::hostArchitecture())
return source.url;
}
return {};
}
case RoleDependencies: {
QStringList dependencies
= Utils::transform(remoteSpec->dependencies(), &PluginDependency::id);
return dependencies;
}
case RolePlatforms: {
QStringList platforms
= Utils::transform<QStringList>(remoteSpec->sources(), [](const Source &s) -> QString {
if (!s.platform)
return Tr::tr("Platform agnostic");
const QString name = customOsTypeToString(s.platform->os);
const QString architecture = customOsArchToString(s.platform->architecture);
return name + " " + architecture;
});
platforms.sort(Qt::CaseInsensitive);
return Utils::filteredUnique(platforms);
}
case RoleVersion:
return remoteSpec->version();
case RoleItemType:
if (remoteSpec->isPack())
return ItemTypePack;
return ItemTypeExtension;
case RoleDescriptionLong: {
const QString description = remoteSpec->longDescription();
const QString url = remoteSpec->url();
const QString documentationUrl = remoteSpec->documentationUrl();
return descriptionWithLinks(description, url, documentationUrl);
}
case RoleDescriptionShort:
return remoteSpec->description();
case RolePlugins:
return remoteSpec->packPluginIds();
}
return {};
}
QVariant ExtensionsModelPrivate::dataFromLocalPlugin(int index, int role) const
{
const PluginSpec *pluginSpec = localPlugins.at(index);
switch (role) {
case RoleSpec:
return QVariant::fromValue(pluginSpec);
case Qt::DisplayRole:
case RoleName:
return pluginSpec->displayName();
case RoleCopyright:
return pluginSpec->copyright();
case RoleDependencies: {
const QStringList dependencies
= transform(pluginSpec->dependencies(), &PluginDependency::id);
return dependencies;
}
case RoleDescriptionLong:
return descriptionWithLinks(pluginSpec->longDescription(), pluginSpec->url(),
pluginSpec->documentationUrl());
case RoleDescriptionShort:
return pluginSpec->description();
case RoleFullId:
return QString("%1.%2").arg(pluginSpec->vendorId()).arg(pluginSpec->id());
case RoleId:
return pluginSpec->id();
case RoleItemType:
return ItemTypeExtension;
case RolePlatforms: {
const QString platformsPattern = pluginSpec->platformSpecification().pattern();
const QStringList platforms = platformsPattern.isEmpty()
? QStringList({customOsTypeToString(OsTypeMac),
customOsTypeToString(OsTypeWindows),
customOsTypeToString(OsTypeLinux)})
: QStringList(platformsPattern);
return platforms;
}
#ifdef QTC_SHOW_BUILD_DATE
case RoleDateUpdated:
return QDate::fromString(QLatin1String(__DATE__), "MMM dd yyyy");
#endif
case RoleVendor:
return pluginSpec->vendor();
case RoleVendorId:
return pluginSpec->vendorId();
default:
break;
}
return {};
}
ExtensionsModel::ExtensionsModel(QObject *parent)
: QAbstractListModel(parent)
, d(new ExtensionsModelPrivate)
{
}
ExtensionsModel::~ExtensionsModel()
{
delete d;
}
int ExtensionsModel::rowCount([[maybe_unused]] const QModelIndex &parent) const
{
return int(d->remotePlugins.size() + d->localPlugins.count());
}
static QString badgeText(const QModelIndex &index)
{
if (index.data(RoleDownloadUrl).isNull())
return {};
const PluginSpec *ps = PluginManager::specById(index.data(RoleId).toString());
if (!ps)
return Tr::tr("New");
const QVersionNumber remoteVersion = QVersionNumber::fromString(
index.data(RoleVersion).toString());
const QVersionNumber localVersion = QVersionNumber::fromString(ps->version());
return remoteVersion > localVersion ? Tr::tr("Updated") : QString();
}
ExtensionState extensionState(const QModelIndex &index)
{
if (index.data(RoleItemType) != ItemTypeExtension)
return None;
const PluginSpec *ps = PluginManager::specById(index.data(RoleId).toString());
if (!ps)
return NotInstalled;
return ps->isEffectivelyEnabled() ? InstalledEnabled : InstalledDisabled;
}
static QString searchText(const QModelIndex &index)
{
QStringList searchTexts;
searchTexts.append(index.data(RoleName).toString());
searchTexts.append(index.data(RoleTags).toStringList());
searchTexts.append(index.data(RoleDescriptionShort).toString());
searchTexts.append(index.data(RoleDescriptionLong).toString());
searchTexts.append(index.data(RoleVendor).toString());
return searchTexts.join(" ");
}
QVariant ExtensionsModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case RoleBadge:
return badgeText(index);
case RoleExtensionState:
return extensionState(index);
case RoleSearchText:
return searchText(index);
default:
break;
}
const bool isRemoteExtension = size_t(index.row()) < d->remotePlugins.size();
const int itemIndex = int(index.row() - (isRemoteExtension ? 0 : d->remotePlugins.size()));
return isRemoteExtension ? d->dataFromRemoteExtension(itemIndex, role)
: d->dataFromLocalPlugin(itemIndex, role);
}
QModelIndex ExtensionsModel::indexOfId(const QString &extensionId) const
{
const int localIndex = indexOf(d->localPlugins, equal(&PluginSpec::id, extensionId));
if (localIndex >= 0)
return index(int(d->remotePlugins.size() + localIndex));
for (int remoteIndex = 0;
const std::unique_ptr<RemoteSpec> &spec : std::as_const(d->remotePlugins)) {
if (spec->id() == extensionId)
return index(remoteIndex);
++remoteIndex;
}
return {};
}
void ExtensionsModel::setRepositoryPaths(const FilePaths &paths)
{
beginResetModel();
d->remotePlugins.clear();
const auto scanPath = [this](const FilePath &path) {
if (path.isEmpty())
return;
FilePath registryPath = path / "registry";
if (!registryPath.isReadableDir()) {
// Github has one top-level directory in its zip, so lets check if thats the case ...
const FilePaths firstLevelEntries = path.dirEntries(QDir::Dirs | QDir::NoDotAndDotDot);
if (firstLevelEntries.size() == 1)
registryPath = firstLevelEntries.first() / "registry";
if (!registryPath.isReadableDir()) {
qCWarning(modelLog) << "Registry path not readable:" << registryPath;
return;
}
}
registryPath.iterateDirectory(
[this](const FilePath &item) -> IterationPolicy {
const auto contents = item.fileContents();
if (!contents) {
qCWarning(modelLog) << "Failed to read file:" << item << contents.error();
return IterationPolicy::Continue;
}
QJsonParseError error;
const QJsonObject obj = QJsonDocument::fromJson(*contents, &error).object();
if (error.error != QJsonParseError::NoError) {
qCWarning(modelLog) << "Failed to parse JSON" << item.toUserOutput() << ":"
<< error.errorString();
return IterationPolicy::Continue;
}
std::unique_ptr<RemoteSpec> remoteSpec(new RemoteSpec());
auto result = remoteSpec->fromJson(obj);
if (!result) {
qCWarning(modelLog) << "Failed to read remote extension" << item.toUserOutput()
<< ":" << result.error();
return IterationPolicy::Continue;
}
for (auto it = d->remotePlugins.begin(); it != d->remotePlugins.end(); ++it) {
if ((*it)->id() == remoteSpec->id()) {
std::swap(*it, remoteSpec);
return IterationPolicy::Continue;
}
}
d->remotePlugins.push_back(std::move(remoteSpec));
return IterationPolicy::Continue;
},
FileFilter({"extension.json"}, QDir::Files, QDirIterator::Subdirectories));
};
for (const FilePath &path : paths)
scanPath(path);
d->addUnlistedLocalPlugins();
endResetModel();
}
QString customOsTypeToString(OsType osType)
{
switch (osType) {
case OsTypeWindows:
return "Windows";
case OsTypeLinux:
return "Linux";
case OsTypeMac:
return "macOS";
case OsTypeOtherUnix:
return "Other Unix";
case OsTypeOther:
default:
return "Other";
}
}
QString customOsArchToString(OsArch osArch)
{
switch (osArch) {
case OsArchX86:
return "x86";
case OsArchAMD64:
return "x86_64";
case OsArchItanium:
return "ia64";
case OsArchArm:
return "arm";
case OsArchArm64:
return "arm64";
case OsArchUnknown:
break;
}
return "Unknown";
}
QString statusDisplayString(const QModelIndex &index)
{
const QString statusString = index.data(RoleStatus).toString();
return statusString != "published" ? statusString : QString();
}
} // ExtensionManager::Internal
|