AppPackageKitResource.cpp 6.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/***************************************************************************
 *   Copyright © 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com>       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2 of        *
 *   the License or (at your option) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "AppPackageKitResource.h"
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
22
#include <AppStreamQt/screenshot.h>
23
#include <AppStreamQt/icon.h>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
24 25
#include <AppStreamQt/image.h>
#include <AppStreamQt/release.h>
26
#include <appstream/AppStreamUtils.h>
27
#include <KLocalizedString>
28
#include <KToolInvocation>
29
#include <QIcon>
30
#include <QProcess>
31
#include <QStandardPaths>
32
#include <QDebug>
33
#include "config-paths.h"
34

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
35
AppPackageKitResource::AppPackageKitResource(const AppStream::Component& data, const QString &packageName, PackageKitBackend* parent)
36
    : PackageKitResource(packageName, QString(), parent)
37
    , m_appdata(data)
38
{
39
    Q_ASSERT(data.isValid());
40
}
41 42 43

QString AppPackageKitResource::name()
{
44
    return m_appdata.name();
45 46
}

47
QString AppPackageKitResource::longDescription()
48
{
49 50 51 52 53
    const auto desc = m_appdata.description();
    if (!desc.isEmpty())
        return desc;

    return PackageKitResource::longDescription();
54 55
}

56
static QIcon componentIcon(const AppStream::Component &comp)
57
{
58
    QIcon ret;
59
    const auto icons = comp.icons();
60
    if (icons.isEmpty()) {
61
        ret = QIcon::fromTheme(QStringLiteral("package-x-generic"));
62
    } else foreach(const AppStream::Icon &icon, icons) {
63
        QStringList stock;
64 65 66 67 68 69 70
        switch(icon.kind()) {
            case AppStream::Icon::KindLocal:
                ret.addFile(icon.url().toLocalFile(), icon.size());
                break;
            case AppStream::Icon::KindCached:
                ret.addFile(icon.url().toLocalFile(), icon.size());
                break;
71 72 73
            case AppStream::Icon::KindStock:
                stock += icon.name();
                break;
74 75 76
            default:
                break;
        }
77 78 79
        if (ret.isNull() && !stock.isEmpty()) {
            ret = QIcon::fromTheme(stock.first());
        }
80
    }
81
    return ret;
82 83
}

84 85 86 87 88
QVariant AppPackageKitResource::icon() const
{
    return componentIcon(m_appdata);
}

89 90
QString AppPackageKitResource::license()
{
91 92
    const auto license = m_appdata.projectLicense();
    return license.isEmpty() ? PackageKitResource::license() : license;
93 94
}

95 96
QStringList AppPackageKitResource::mimetypes() const
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
97
    return m_appdata.provided(AppStream::Provided::KindMimetype).items();
98 99 100 101
}

QStringList AppPackageKitResource::categories()
{
102
    auto cats = m_appdata.categories();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
103
    if (m_appdata.kind() != AppStream::Component::KindAddon)
104
        cats.append(QStringLiteral("Application"));
105
    return cats;
106 107
}

108 109
QString AppPackageKitResource::comment()
{
110 111 112 113 114
    const auto summary = m_appdata.summary();
    if (!summary.isEmpty())
        return summary;

    return PackageKitResource::comment();
115 116
}

117 118 119 120 121
QString AppPackageKitResource::appstreamId() const
{
    return m_appdata.id();
}

122
QUrl AppPackageKitResource::homepage()
123
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
124
    return m_appdata.url(AppStream::Component::UrlKindHomepage);
125 126
}

127 128
bool AppPackageKitResource::isTechnical() const
{
129 130
    static QString desktop = QString::fromUtf8(qgetenv("XDG_CURRENT_DESKTOP"));
    return !m_appdata.compulsoryForDesktops().contains(desktop);
131
}
132

133 134 135 136
void AppPackageKitResource::fetchScreenshots()
{
    QList<QUrl> thumbnails, screenshots;

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
137
    Q_FOREACH (const AppStream::Screenshot &s, m_appdata.screenshots()) {
138 139
        const QUrl thumbnail = AppStreamUtils::imageOfKind(s.images(), AppStream::Image::KindThumbnail);
        const QUrl plain = AppStreamUtils::imageOfKind(s.images(), AppStream::Image::KindSource);
140 141 142 143 144 145 146 147 148 149
        if (plain.isEmpty())
            qWarning() << "invalid screenshot for" << name();

        screenshots << plain;
        thumbnails << (thumbnail.isEmpty() ? plain : thumbnail);
    }

    Q_EMIT screenshotsFetched(thumbnails, screenshots);
}

150 151 152 153
QStringList AppPackageKitResource::allPackageNames() const
{
    return m_appdata.packageNames();
}
154 155 156 157 158 159 160 161 162 163 164 165

QList<PackageState> AppPackageKitResource::addonsInformation()
{
    const PackageKitBackend* p = static_cast<PackageKitBackend*>(parent());
    const QVector<AppPackageKitResource*> res = p->extendedBy(m_appdata.id());

    QList<PackageState> ret;
    Q_FOREACH (AppPackageKitResource* r, res) {
        ret += PackageState(r->appstreamId(), r->name(), r->comment(), r->isInstalled());
    }
    return ret;
}
166 167 168 169 170

QStringList AppPackageKitResource::extends() const
{
    return m_appdata.extends();
}
171 172 173

void AppPackageKitResource::fetchChangelog()
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
174 175 176 177 178 179
    QString changelog;
    for(const auto& rel: m_appdata.releases()) {
        changelog += QStringLiteral("<h3>") + rel.version() + QStringLiteral("</h3>");
        changelog += QStringLiteral("<p>") + rel.description() + QStringLiteral("</p>");
    }
    emit changelogFetched(changelog);
180
}
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
181 182 183 184 185 186 187 188 189 190 191 192

void AppPackageKitResource::invokeApplication() const
{
    const QStringList exes = m_appdata.provided(AppStream::Provided::KindBinary).items();
    if (exes.isEmpty()) {
        const auto servicePath = QStandardPaths::locate(QStandardPaths::ApplicationsLocation, m_appdata.id());
        QProcess::startDetached(QStringLiteral(CMAKE_INSTALL_FULL_LIBEXECDIR_KF5 "/discover/runservice"), {servicePath});
    } else {
        QProcess::startDetached(exes.constFirst());
    }
}