FlatpakResource.cpp 17.4 KB
Newer Older
1
2
3
4
5
6
7
/*
 *   SPDX-FileCopyrightText: 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
 *   SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com>
 *
 *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

Jan Grulich's avatar
Jan Grulich committed
8
#include "FlatpakResource.h"
9
#include "FlatpakBackend.h"
10
#include "FlatpakSourcesBackend.h"
11
#include "config-paths.h"
Jan Grulich's avatar
Jan Grulich committed
12
13
14

#include <Transaction/AddonList.h>

15
16
#include <AppStreamQt/icon.h>
#include <AppStreamQt/screenshot.h>
17
#include <appstream/AppStreamUtils.h>
18

19
#include <KFormat>
Jan Grulich's avatar
Jan Grulich committed
20
21
#include <KLocalizedString>

Alexander Lohnau's avatar
Alexander Lohnau committed
22
#include <AppStreamQt/release.h>
Jan Grulich's avatar
Jan Grulich committed
23
24
#include <QDebug>
#include <QDesktopServices>
Alexander Lohnau's avatar
Alexander Lohnau committed
25
#include <QDir>
Jan Grulich's avatar
Jan Grulich committed
26
#include <QFileInfo>
Alexander Lohnau's avatar
Alexander Lohnau committed
27
#include <QIcon>
28
29
30
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
Alexander Lohnau's avatar
Alexander Lohnau committed
31
#include <QProcess>
Jan Grulich's avatar
Jan Grulich committed
32
33
#include <QStringList>
#include <QTimer>
34
#include <QUrlQuery>
Jan Grulich's avatar
Jan Grulich committed
35

36
37
38
static QString iconCachePath(const AppStream::Icon &icon)
{
    Q_ASSERT(icon.kind() == AppStream::Icon::KindRemote);
39
    return QStringLiteral("%1/icons/%2").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), icon.url().fileName());
40
41
}

42
43
const QStringList FlatpakResource::m_objects{QStringLiteral("qrc:/qml/FlatpakAttention.qml")};

Alexander Lohnau's avatar
Alexander Lohnau committed
44
FlatpakResource::FlatpakResource(const AppStream::Component &component, FlatpakInstallation *installation, FlatpakBackend *parent)
Jan Grulich's avatar
Jan Grulich committed
45
    : AbstractResource(parent)
46
    , m_appdata(component)
47
    , m_id({component.id(), QString(), QString()})
48
49
    , m_downloadSize(0)
    , m_installedSize(0)
Alexander Lohnau's avatar
Alexander Lohnau committed
50
    , m_propertyStates({{DownloadSize, NotKnownYet}, {InstalledSize, NotKnownYet}, {RequiredRuntime, NotKnownYet}})
51
    , m_state(AbstractResource::None)
52
    , m_installation(installation)
Jan Grulich's avatar
Jan Grulich committed
53
{
54
    setObjectName(packageName());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
55

56
    // Start fetching remote icons during initialization
57
    const auto icons = m_appdata.icons();
58
    if (!icons.isEmpty()) {
Jonah Brüchert's avatar
Jonah Brüchert committed
59
        for (const AppStream::Icon &icon : icons) {
60
            if (icon.kind() == AppStream::Icon::KindRemote) {
61
                const QString fileName = iconCachePath(icon);
62
                if (!QFileInfo::exists(fileName)) {
63
64
65
                    const QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
                    // Create $HOME/.cache/discover/icons folder
                    cacheDir.mkdir(QStringLiteral("icons"));
66
                    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
Alexander Lohnau's avatar
Alexander Lohnau committed
67
                    connect(manager, &QNetworkAccessManager::finished, this, [this, icon, fileName, manager](QNetworkReply *reply) {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
                        if (reply->error() == QNetworkReply::NoError) {
                            QByteArray iconData = reply->readAll();
                            QFile file(fileName);
                            if (file.open(QIODevice::WriteOnly)) {
                                file.write(iconData);
                            }
                            file.close();
                            Q_EMIT iconChanged();
                        }
                        manager->deleteLater();
                    });
                    manager->get(QNetworkRequest(icon.url()));
                }
            }
        }
    }
Jan Grulich's avatar
Jan Grulich committed
84
85
}

86
AppStream::Component FlatpakResource::appstreamComponent() const
Jan Grulich's avatar
Jan Grulich committed
87
{
88
    return m_appdata;
Jan Grulich's avatar
Jan Grulich committed
89
90
91
92
}

QList<PackageState> FlatpakResource::addonsInformation()
{
93
    return {};
Jan Grulich's avatar
Jan Grulich committed
94
95
96
97
}

QString FlatpakResource::availableVersion() const
{
98
99
100
    QString theBranch = branch();
    if (theBranch.isEmpty()) {
        theBranch = i18n("Unknown");
Jan Grulich's avatar
Jan Grulich committed
101
102
    }

103
104
    if (!m_availableVersion.isEmpty()) {
        return i18nc("version (branch)", "%1 (%2)", m_availableVersion, theBranch);
105
106
107
    }

    return theBranch;
Jan Grulich's avatar
Jan Grulich committed
108
109
110
111
}

QString FlatpakResource::appstreamId() const
{
112
    return m_id.id;
Jan Grulich's avatar
Jan Grulich committed
113
114
115
116
}

QString FlatpakResource::arch() const
{
117
    return m_id.arch;
Jan Grulich's avatar
Jan Grulich committed
118
119
}

120
121
QString FlatpakResource::branch() const
{
122
    return m_id.branch;
123
124
}

Jan Grulich's avatar
Jan Grulich committed
125
126
bool FlatpakResource::canExecute() const
{
127
    return (m_type == DesktopApp && (m_state == AbstractResource::Installed || m_state == AbstractResource::Upgradeable));
Jan Grulich's avatar
Jan Grulich committed
128
129
}

Alexander Lohnau's avatar
Alexander Lohnau committed
130
void FlatpakResource::updateFromRef(FlatpakRef *ref)
131
132
133
134
135
{
    setArch(QString::fromUtf8(flatpak_ref_get_arch(ref)));
    setBranch(QString::fromUtf8(flatpak_ref_get_branch(ref)));
    setCommit(QString::fromUtf8(flatpak_ref_get_commit(ref)));
    setFlatpakName(QString::fromUtf8(flatpak_ref_get_name(ref)));
Alexander Lohnau's avatar
Alexander Lohnau committed
136
    setType(flatpak_ref_get_kind(ref) == FLATPAK_REF_KIND_APP ? DesktopApp : extends().isEmpty() ? Runtime : Extension);
137
    setObjectName(packageName());
138
139
}

140
141
142
143
144
145
146
147
148
149
150
151
void FlatpakResource::updateFromAppStream()
{
    const QString refstr = m_appdata.bundle(AppStream::Bundle::KindFlatpak).id();
    g_autoptr(GError) localError = nullptr;
    g_autoptr(FlatpakRef) ref = flatpak_ref_parse(refstr.toUtf8().constData(), &localError);
    if (!ref) {
        qDebug() << "failed to obtain ref" << refstr << localError->message;
        return;
    }
    updateFromRef(ref);
}

152
153
154
155
156
QString FlatpakResource::ref() const
{
    return typeAsString() + QLatin1Char('/') + flatpakName() + QLatin1Char('/') + arch() + QLatin1Char('/') + branch();
}

Jan Grulich's avatar
Jan Grulich committed
157
158
QStringList FlatpakResource::categories()
{
159
160
    auto cats = m_appdata.categories();
    if (m_appdata.kind() != AppStream::Component::KindAddon)
161
162
        cats.append(QStringLiteral("Application"));
    return cats;
Jan Grulich's avatar
Jan Grulich committed
163
164
165
166
}

QString FlatpakResource::comment()
{
167
    const auto summary = m_appdata.summary();
168
169
170
171
172
    if (!summary.isEmpty()) {
        return summary;
    }

    return QString();
Jan Grulich's avatar
Jan Grulich committed
173
174
175
176
177
178
179
}

QString FlatpakResource::commit() const
{
    return m_commit;
}

180
quint64 FlatpakResource::downloadSize() const
181
182
183
184
{
    return m_downloadSize;
}

Jan Grulich's avatar
Jan Grulich committed
185
186
187
QVariant FlatpakResource::icon() const
{
    QIcon ret;
188
    const auto icons = m_appdata.icons();
Jan Grulich's avatar
Jan Grulich committed
189

Jan Grulich's avatar
Jan Grulich committed
190
191
192
    if (!m_bundledIcon.isNull()) {
        ret = QIcon(m_bundledIcon);
    } else if (icons.isEmpty()) {
Jan Grulich's avatar
Jan Grulich committed
193
        ret = QIcon::fromTheme(QStringLiteral("package-x-generic"));
Alexander Lohnau's avatar
Alexander Lohnau committed
194
    } else
Jonah Brüchert's avatar
Jonah Brüchert committed
195
        for (const AppStream::Icon &icon : icons) {
Alexander Lohnau's avatar
Alexander Lohnau committed
196
            switch (icon.kind()) {
197
            case AppStream::Icon::KindLocal:
198
199
200
            case AppStream::Icon::KindCached:
                ret.addFile(icon.url().toLocalFile(), icon.size());
                break;
201
202
203
204
205
206
            case AppStream::Icon::KindStock: {
                const auto ret = QIcon::fromTheme(icon.name());
                if (!ret.isNull())
                    return ret;
                break;
            }
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
207
            case AppStream::Icon::KindRemote: {
208
                const QString fileName = iconCachePath(icon);
209
                if (QFileInfo::exists(fileName)) {
210
                    ret.addFile(fileName, icon.size());
211
                }
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
212
213
214
215
                break;
            }
            case AppStream::Icon::KindUnknown:
                break;
Alexander Lohnau's avatar
Alexander Lohnau committed
216
            }
Jan Grulich's avatar
Jan Grulich committed
217
218
        }

219
220
    if (ret.isNull()) {
        ret = QIcon::fromTheme(QStringLiteral("package-x-generic"));
Jan Grulich's avatar
Jan Grulich committed
221
    }
222

Jan Grulich's avatar
Jan Grulich committed
223
224
225
226
227
    return ret;
}

QString FlatpakResource::installedVersion() const
{
228
    QString version = branch();
Jan Grulich's avatar
Jan Grulich committed
229
230
231
232
    if (version.isEmpty()) {
        version = i18n("Unknown");
    }

233
    if (auto ref = qobject_cast<FlatpakBackend *>(backend())->getInstalledRefForApp(this)) {
234
235
236
237
        const char *appdataVersion = flatpak_installed_ref_get_appdata_version(ref);
        if (appdataVersion) {
            return i18nc("version (branch)", "%1 (%2)", appdataVersion, version);
        }
238
    }
Jan Grulich's avatar
Jan Grulich committed
239
240
241
    return version;
}

242
quint64 FlatpakResource::installedSize() const
243
244
245
246
{
    return m_installedSize;
}

247
AbstractResource::Type FlatpakResource::type() const
Jan Grulich's avatar
Jan Grulich committed
248
{
249
    switch (m_type) {
Alexander Lohnau's avatar
Alexander Lohnau committed
250
251
252
253
254
255
    case FlatpakResource::Runtime:
        return Technical;
    case FlatpakResource::Extension:
        return Addon;
    default:
        return Application;
256
    }
Jan Grulich's avatar
Jan Grulich committed
257
258
259
260
}

QUrl FlatpakResource::homepage()
{
261
    return m_appdata.url(AppStream::Component::UrlKindHomepage);
Jan Grulich's avatar
Jan Grulich committed
262
263
}

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
QUrl FlatpakResource::helpURL()
{
    return m_appdata.url(AppStream::Component::UrlKindHelp);
}

QUrl FlatpakResource::bugURL()
{
    return m_appdata.url(AppStream::Component::UrlKindBugtracker);
}

QUrl FlatpakResource::donationURL()
{
    return m_appdata.url(AppStream::Component::UrlKindDonation);
}

279
FlatpakResource::FlatpakFileType FlatpakResource::flatpakFileType() const
Jan Grulich's avatar
Jan Grulich committed
280
281
282
283
{
    return m_flatpakFileType;
}

Jan Grulich's avatar
Jan Grulich committed
284
285
QString FlatpakResource::flatpakName() const
{
286
287
288
    // If the flatpak name is not known (known only for installed apps), then use
    // appstream id instead;
    if (m_flatpakName.isEmpty()) {
289
        return m_id.id;
290
291
    }

Jan Grulich's avatar
Jan Grulich committed
292
293
294
    return m_flatpakName;
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
295
QJsonArray FlatpakResource::licenses()
Jan Grulich's avatar
Jan Grulich committed
296
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
297
    return AppStreamUtils::licenses(m_appdata);
Jan Grulich's avatar
Jan Grulich committed
298
299
300
301
}

QString FlatpakResource::longDescription()
{
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    QString description = m_appdata.description();

    return description;
}

QString FlatpakResource::attentionText() const
{
    if (m_flatpakFileType == FlatpakResource::FileFlatpakRef) {
        QUrl loc = m_resourceLocation;
        loc.setPath({});
        loc.setQuery(QUrlQuery());
        return xi18nc("@info",
                      "<para>This application comes from \"%1\" (hosted at <link>%2</link>). Other software in this repository will also be made be available "
                      "in Discover "
                      "when the application is "
                      "installed.</para>",
                      m_origin,
                      loc.toDisplayString());
    }
    return {};
Jan Grulich's avatar
Jan Grulich committed
322
323
}

324
QString FlatpakResource::name() const
Jan Grulich's avatar
Jan Grulich committed
325
{
326
    QString name = m_appdata.name();
Jan Grulich's avatar
Jan Grulich committed
327
    if (name.isEmpty()) {
328
        name = flatpakName();
Jan Grulich's avatar
Jan Grulich committed
329
330
    }

Jan Grulich's avatar
Jan Grulich committed
331
332
333
334
    if (name.startsWith(QLatin1String("(Nightly) "))) {
        return name.mid(10);
    }

Jan Grulich's avatar
Jan Grulich committed
335
336
337
338
339
    return name;
}

QString FlatpakResource::origin() const
{
340
    return m_origin;
Jan Grulich's avatar
Jan Grulich committed
341
342
343
344
}

QString FlatpakResource::packageName() const
{
345
    return flatpakName() + QLatin1Char('/') + arch() + QLatin1Char('/') + branch();
Jan Grulich's avatar
Jan Grulich committed
346
347
}

348
349
350
351
352
FlatpakResource::PropertyState FlatpakResource::propertyState(FlatpakResource::PropertyKind kind) const
{
    return m_propertyStates[kind];
}

Jan Grulich's avatar
Jan Grulich committed
353
354
355
356
357
QUrl FlatpakResource::resourceFile() const
{
    return m_resourceFile;
}

358
359
360
361
362
QString FlatpakResource::runtime() const
{
    return m_runtime;
}

Jan Grulich's avatar
Jan Grulich committed
363
364
365
366
367
QString FlatpakResource::section()
{
    return QString();
}

368
quint64 FlatpakResource::size()
Jan Grulich's avatar
Jan Grulich committed
369
{
370
371
372
373
374
    if (m_state == Installed) {
        return m_installedSize;
    } else {
        return m_downloadSize;
    }
Jan Grulich's avatar
Jan Grulich committed
375
376
}

377
378
379
380
QString FlatpakResource::sizeDescription()
{
    KFormat f;
    if (!isInstalled() || canUpgrade()) {
Alexander Lohnau's avatar
Alexander Lohnau committed
381
382
383
        if (propertyState(DownloadSize) == NotKnownYet || propertyState(InstalledSize) == NotKnownYet || propertyState(DownloadSize) == Fetching
            || propertyState(InstalledSize) == Fetching) {
            qobject_cast<FlatpakBackend *>(backend())->updateAppSize(this);
384
            return i18n("Retrieving size information");
385
        } else if (propertyState(DownloadSize) == UnknownOrFailed || propertyState(InstalledSize) == UnknownOrFailed) {
386
387
388
389
            return i18n("Unknown size");
        } else {
            return i18nc("@info app size", "%1 to download, %2 on disk", f.formatByteSize(downloadSize()), f.formatByteSize(installedSize()));
        }
390
    } else {
391
        if (propertyState(InstalledSize) == NotKnownYet || propertyState(InstalledSize) == Fetching) {
392
            return i18n("Retrieving size information");
393
        } else if (propertyState(InstalledSize) == UnknownOrFailed) {
394
395
396
397
            return i18n("Unknown size");
        } else {
            return i18nc("@info app size", "%1 on disk", f.formatByteSize(installedSize()));
        }
398
399
400
    }
}

Jan Grulich's avatar
Jan Grulich committed
401
402
AbstractResource::State FlatpakResource::state()
{
403
    return m_state;
Jan Grulich's avatar
Jan Grulich committed
404
405
}

406
FlatpakResource::ResourceType FlatpakResource::resourceType() const
407
{
408
    return m_type;
409
410
411
}

QString FlatpakResource::typeAsString() const
Jan Grulich's avatar
Jan Grulich committed
412
{
413
    switch (m_type) {
Alexander Lohnau's avatar
Alexander Lohnau committed
414
415
416
417
418
419
420
    case FlatpakResource::Runtime:
    case FlatpakResource::Extension:
        return QLatin1String("runtime");
    case FlatpakResource::DesktopApp:
    case FlatpakResource::Source:
    default:
        return QLatin1String("app");
Jan Grulich's avatar
Jan Grulich committed
421
    }
422
423
}

424
FlatpakResource::Id FlatpakResource::uniqueId() const
425
{
426
    return m_id;
Jan Grulich's avatar
Jan Grulich committed
427
428
429
430
}

void FlatpakResource::invokeApplication() const
{
431
    const QString desktopFile = installPath() + QLatin1String("/export/share/applications/") + appstreamId();
432
433
434
435
436
437
    const QString runservice = QStringLiteral(CMAKE_INSTALL_FULL_LIBEXECDIR_KF5 "/discover/runservice");
    if (QFile::exists(desktopFile) && QFile::exists(runservice)) {
        QProcess::startDetached(runservice, {desktopFile});
        return;
    }

Jan Grulich's avatar
Jan Grulich committed
438
439
440
    g_autoptr(GCancellable) cancellable = g_cancellable_new();
    g_autoptr(GError) localError = nullptr;

441
    if (!flatpak_installation_launch(m_installation,
442
443
444
                                     flatpakName().toUtf8().constData(),
                                     arch().toUtf8().constData(),
                                     branch().toUtf8().constData(),
445
                                     nullptr,
Jan Grulich's avatar
Jan Grulich committed
446
447
                                     cancellable,
                                     &localError)) {
448
        qWarning() << "Failed to launch " << m_appdata.name() << ": " << localError->message;
Jan Grulich's avatar
Jan Grulich committed
449
450
451
452
453
    }
}

void FlatpakResource::fetchChangelog()
{
454
    Q_EMIT changelogFetched(AppStreamUtils::changelogToHtml(m_appdata));
Jan Grulich's avatar
Jan Grulich committed
455
456
457
458
}

void FlatpakResource::fetchScreenshots()
{
459
460
    const auto sc = AppStreamUtils::fetchScreenshots(m_appdata);
    Q_EMIT screenshotsFetched(sc.first, sc.second);
Jan Grulich's avatar
Jan Grulich committed
461
462
463
464
}

void FlatpakResource::setArch(const QString &arch)
{
465
    m_id.arch = arch;
Jan Grulich's avatar
Jan Grulich committed
466
467
}

468
469
void FlatpakResource::setBranch(const QString &branch)
{
470
    m_id.branch = branch;
471
472
}

Jan Grulich's avatar
Jan Grulich committed
473
474
475
476
477
void FlatpakResource::setBundledIcon(const QPixmap &pixmap)
{
    m_bundledIcon = pixmap;
}

Jan Grulich's avatar
Jan Grulich committed
478
479
480
481
482
void FlatpakResource::setCommit(const QString &commit)
{
    m_commit = commit;
}

483
void FlatpakResource::setDownloadSize(quint64 size)
484
485
{
    m_downloadSize = size;
486
487
488

    setPropertyState(DownloadSize, AlreadyKnown);

489
    Q_EMIT sizeChanged();
490
491
}

492
void FlatpakResource::setFlatpakFileType(FlatpakFileType fileType)
Jan Grulich's avatar
Jan Grulich committed
493
494
495
496
{
    m_flatpakFileType = fileType;
}

Jan Grulich's avatar
Jan Grulich committed
497
498
499
500
501
void FlatpakResource::setFlatpakName(const QString &name)
{
    m_flatpakName = name;
}

502
503
504
505
506
void FlatpakResource::setIconPath(const QString &path)
{
    m_iconPath = path;
}

507
void FlatpakResource::setInstalledSize(quint64 size)
508
509
{
    m_installedSize = size;
510
511
512

    setPropertyState(InstalledSize, AlreadyKnown);

513
    Q_EMIT sizeChanged();
514
515
}

516
517
void FlatpakResource::setOrigin(const QString &origin)
{
518
    m_origin = origin;
519
520
}

521
void FlatpakResource::setPropertyState(FlatpakResource::PropertyKind kind, FlatpakResource::PropertyState newState)
522
{
Alexander Lohnau's avatar
Alexander Lohnau committed
523
    auto &state = m_propertyStates[kind];
524
525
    if (state != newState) {
        state = newState;
526

527
528
        Q_EMIT propertyStateChanged(kind, newState);
    }
529
530
}

Jan Grulich's avatar
Jan Grulich committed
531
532
533
534
535
void FlatpakResource::setResourceFile(const QUrl &url)
{
    m_resourceFile = url;
}

536
537
538
void FlatpakResource::setRuntime(const QString &runtime)
{
    m_runtime = runtime;
539
540

    setPropertyState(RequiredRuntime, AlreadyKnown);
541
542
}

543
void FlatpakResource::setState(AbstractResource::State state, bool shouldEmit)
Jan Grulich's avatar
Jan Grulich committed
544
{
545
546
    if (m_state != state) {
        m_state = state;
Jan Grulich's avatar
Jan Grulich committed
547

548
        if (shouldEmit && qobject_cast<FlatpakBackend *>(backend())->isTracked(this)) {
549
            Q_EMIT stateChanged();
550
        }
551
    }
Jan Grulich's avatar
Jan Grulich committed
552
553
}

554
555
void FlatpakResource::setType(FlatpakResource::ResourceType type)
{
556
    m_type = type;
557
558
}

559
560
QString FlatpakResource::installationPath() const
{
561
    return installationPath(m_installation);
562
563
}

Alexander Lohnau's avatar
Alexander Lohnau committed
564
QString FlatpakResource::installationPath(FlatpakInstallation *flatpakInstallation)
565
566
{
    g_autoptr(GFile) path = flatpak_installation_get_path(flatpakInstallation);
567
568
    g_autofree char *path_str = g_file_get_path(path);
    return QString::fromUtf8(path_str);
569
}
570

571
572
QString FlatpakResource::installPath() const
{
573
    return installationPath() + QStringLiteral("/app/%1/%2/%3/active").arg(flatpakName(), arch(), branch());
574
575
}

576
577
578
579
QUrl FlatpakResource::url() const
{
    return m_resourceFile.isEmpty() ? QUrl(QStringLiteral("appstream://") + appstreamId()) : m_resourceFile;
}
580
581
582
583
584
585
586
587
588
589

QDate FlatpakResource::releaseDate() const
{
    if (!m_appdata.releases().isEmpty()) {
        auto release = m_appdata.releases().constFirst();
        return release.timestamp().date();
    }

    return {};
}
590
591
592

QString FlatpakResource::sourceIcon() const
{
Alexander Lohnau's avatar
Alexander Lohnau committed
593
    const auto sourceItem = qobject_cast<FlatpakBackend *>(backend())->sources()->sourceById(origin());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
594
595
596
597
598
599
    if (!sourceItem) {
        qWarning() << "Could not find source " << origin();
        return QStringLiteral("flatpak-discover");
    }

    const auto iconUrl = sourceItem->data(FlatpakSourcesBackend::IconUrlRole).toString();
600
601
602
    if (iconUrl.isEmpty())
        return QStringLiteral("flatpak-discover");
    return iconUrl;
603
}
604

Alexander Lohnau's avatar
Alexander Lohnau committed
605
QString FlatpakResource::author() const
606
607
608
{
    return m_appdata.developerName();
}
609
610
611
612
613

QStringList FlatpakResource::extends() const
{
    return m_appdata.extends();
}
614
615
616

QSet<QString> FlatpakResource::alternativeAppstreamIds() const
{
Alexander Lohnau's avatar
Alexander Lohnau committed
617
    const AppStream::Provided::Kind AppStream_Provided_KindId = (AppStream::Provided::Kind)12; // Should be AppStream::Provided::KindId when released
618
619
620
621
    const auto ret = m_appdata.provided(AppStream_Provided_KindId).items();

    return QSet<QString>(ret.begin(), ret.end());
}
622
623
624
625
626

QStringList FlatpakResource::mimetypes() const
{
    return m_appdata.provided(AppStream::Provided::KindMimetype).items();
}
627
628
629
630

QString FlatpakResource::versionString()
{
    QString version;
631
632
633
    if (resourceType() == Source) {
        return {};
    }
634
635
    if (isInstalled()) {
        auto ref = qobject_cast<FlatpakBackend *>(backend())->getInstalledRefForApp(this);
636
637
638
        if (ref) {
            version = flatpak_installed_ref_get_appdata_version(ref);
        }
639
640
641
642
643
644
645
646
647
    } else if (!m_appdata.releases().isEmpty()) {
        auto release = m_appdata.releases().constFirst();
        version = release.version();
    } else {
        version = m_id.branch;
    }

    return AppStreamUtils::versionString(version, m_appdata);
}