SnapResource.cpp 9.58 KB
Newer Older
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
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 "SnapResource.h"
22
#include "SnapBackend.h"
23
#include <QDebug>
24
#include <QProcess>
25
26
#include <QBuffer>
#include <QImageReader>
27
28
29
#include <QStandardItemModel>
#include <KLocalizedString>
#include <utils.h>
30
#include <QProcess>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
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
QDebug operator<<(QDebug debug, const QSnapdPlug& plug)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "QSnapdPlug(";
    debug.nospace() << "name:" << plug.name() << ',';
    debug.nospace() << "snap:" << plug.snap() << ',';
    debug.nospace() << "label:" << plug.label() << ',';
    debug.nospace() << "interface:" << plug.interface() << ',';
    debug.nospace() << "connectionCount:" << plug.connectionCount();
    debug.nospace() << ')';
    return debug;
}

QDebug operator<<(QDebug debug, const QSnapdSlot& slot)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "QSnapdSlot(";
    debug.nospace() << "name:" << slot.name() << ',';
    debug.nospace() << "label:" << slot.label() << ',';
    debug.nospace() << "snap:" << slot.snap() << ',';
    debug.nospace() << "interface:" << slot.interface() << ',';
    debug.nospace() << "connectionCount:" << slot.connectionCount();
    debug.nospace() << ')';
    return debug;
}

QDebug operator<<(QDebug debug, const QSnapdPlug* plug)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "*" << *plug;
    return debug;
}

QDebug operator<<(QDebug debug, const QSnapdSlot* slot)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "*" << *slot;
    return debug;
}

72
73
const QStringList SnapResource::m_objects({ QStringLiteral("qrc:/snapui/PermissionsButton.qml") });

74
75
SnapResource::SnapResource(QSharedPointer<QSnapdSnap> snap, AbstractResource::State state, SnapBackend* backend)
    : AbstractResource(backend)
76
    , m_state(state)
77
    , m_snap(snap)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
78
{
79
    setObjectName(snap->name());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
80
81
82
83
}

QString SnapResource::availableVersion() const
{
84
    return installedVersion();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
85
86
87
88
}

QStringList SnapResource::categories()
{
89
    return { QStringLiteral("Application") };
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
90
91
92
93
}

QString SnapResource::comment()
{
94
    return m_snap->summary();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
95
96
97
98
}

int SnapResource::size()
{
99
100
//     return isInstalled() ? m_snap->installedSize() : m_snap->downloadSize();
    return m_snap->downloadSize();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
101
102
103
104
}

QVariant SnapResource::icon() const
{
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    if (m_icon.isNull()) {
        m_icon = [this]() -> QVariant {
            const auto iconPath = m_snap->icon();
            if (iconPath.isEmpty())
                return QStringLiteral("package-x-generic");

            if (!iconPath.startsWith(QLatin1Char('/')))
                return QUrl(iconPath);

            auto backend = qobject_cast<SnapBackend*>(parent());
            auto req = backend->client()->getIcon(packageName());
            connect(req, &QSnapdGetIconRequest::complete, this, &SnapResource::gotIcon);
            req->runAsync();
            return {};
        }();
    }
    return m_icon;
}
123

124
125
126
void SnapResource::gotIcon()
{
    auto req = qobject_cast<QSnapdGetIconRequest*>(sender());
127
    if (req->error()) {
128
129
        qWarning() << "icon error" << req->errorString();
        return;
130
131
132
133
134
135
136
137
    }

    auto icon = req->icon();

    QBuffer buffer;
    buffer.setData(icon->data());
    QImageReader reader(&buffer);

138
139
140
141
142
    auto theIcon = QVariant::fromValue<QImage>(reader.read());
    if (theIcon != m_icon) {
        m_icon = theIcon;
        iconChanged();
    }
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
143
144
145
146
}

QString SnapResource::installedVersion() const
{
147
    return m_snap->version();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
148
149
150
151
}

QString SnapResource::license()
{
152
    return m_snap->license();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
153
154
155
156
}

QString SnapResource::longDescription()
{
157
    return m_snap->description();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
158
159
}

160
QString SnapResource::name() const
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
161
{
162
    return m_snap->title().isEmpty() ? m_snap->name() : m_snap->title();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
163
164
165
166
}

QString SnapResource::origin() const
{
167
    return QStringLiteral("snappy:") + m_snap->channel();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
168
169
170
171
}

QString SnapResource::packageName() const
{
172
    return m_snap->name();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
173
174
175
176
177
178
179
180
181
}

QString SnapResource::section()
{
    return QStringLiteral("snap");
}

AbstractResource::State SnapResource::state()
{
182
    return m_state;
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
183
184
}

185
186
187
188
189
190
191
192
void SnapResource::setState(AbstractResource::State state)
{
    if (m_state != state) {
        m_state = state;
        Q_EMIT stateChanged();
    }
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
193
194
void SnapResource::fetchChangelog()
{
195
    QString log;
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
196
197
198
199
200
    emit changelogFetched(log);
}

void SnapResource::fetchScreenshots()
{
201
202
203
204
205
206
    QList<QUrl> screenshots;
    for(int i = 0, c = m_snap->screenshotCount(); i<c; ++i) {
        QScopedPointer<QSnapdScreenshot> screenshot(m_snap->screenshot(i));
        screenshots << QUrl(screenshot->url());
    }
    Q_EMIT screenshotsFetched(screenshots, screenshots);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
207
208
209
210
}

void SnapResource::invokeApplication() const
{
211
    QProcess::startDetached(QStringLiteral("snap"), {QStringLiteral("run"), packageName()});
212
213
214
215
}

bool SnapResource::isTechnical() const
{
216
    return m_snap->snapType() != QLatin1String("app");
217
}
218

219
220
221
222
223
224
225
226
227
228
229
void SnapResource::setSnap(const QSharedPointer<QSnapdSnap>& snap)
{
    Q_ASSERT(snap->name() == m_snap->name());
    if (m_snap == snap)
        return;

    const bool newSize = m_snap->installedSize() != snap->installedSize() || m_snap->downloadSize() != snap->downloadSize();
    m_snap = snap;
    if (newSize)
        Q_EMIT sizeChanged();
}
230
231
232
233
234

QDate SnapResource::releaseDate() const
{
    return {};
}
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

class PlugsModel : public QStandardItemModel
{
public:
    enum Roles {
        PlugNameRole = Qt::UserRole + 1,
        SlotSnapRole,
        SlotNameRole
    };

    PlugsModel(QSnapdSnap* snap, SnapBackend* backend, QObject* parent)
        : QStandardItemModel(parent)
        , m_snap(snap)
        , m_backend(backend)
    {
        setItemRoleNames(roleNames().unite(
            { {Qt::CheckStateRole, "checked"} }
        ));

        auto req = backend->client()->getInterfaces();
        req->runSync();

        QHash<QString, QVector<QSnapdSlot*>> slotsForInterface;
        for (int i = 0; i<req->slotCount(); ++i) {
            const auto slot = req->slot(i);
            slot->setParent(this);
            slotsForInterface[slot->interface()].append(slot);

        }

        for (int i = 0; i<req->plugCount(); ++i) {
            const QScopedPointer<QSnapdPlug> plug(req->plug(i));
            if (plug->snap() == m_snap->name()) {
                for (auto slot: slotsForInterface[plug->interface()]) {
                    auto item = new QStandardItem;
                    if (plug->label().isEmpty())
                        item->setText(plug->name());
                    else
                        item->setText(i18n("%1 - %2", plug->name(), plug->label()));

                    item->setCheckable(true);
                    item->setCheckState(plug->connectionCount()>0 ? Qt::Checked : Qt::Unchecked);
                    item->setData(plug->name(), PlugNameRole);
                    item->setData(slot->snap(), SlotSnapRole);
                    item->setData(slot->name(), SlotNameRole);
                    appendRow(item);
                }
            }
        }
    }

private:
    bool setData(const QModelIndex & index, const QVariant & value, int role) override {
        if (role != Qt::CheckStateRole)
            return QStandardItemModel::setData(index, value, role);

        auto item = itemFromIndex(index);
        Q_ASSERT(item);
        const QString plugName = item->data(PlugNameRole).toString();
        const QString slotSnap = item->data(SlotSnapRole).toString();
        const QString slotName = item->data(SlotNameRole).toString();

        QSnapdRequest* req;

        if (item->checkState() == Qt::Checked) {
            req = m_backend->client()->connectInterface(m_snap->name(), plugName, slotSnap, slotName);
        } else {
            req = m_backend->client()->disconnectInterface(m_snap->name(), plugName, slotSnap, slotName);
        }
        req->runSync();
        if (req->error()) {
            qWarning() << "snapd error" << req->errorString();
        }
        return req->error() == QSnapdRequest::NoError;
    }

    QSnapdSnap* const m_snap;
    SnapBackend* const m_backend;
};

QAbstractItemModel* SnapResource::plugs(QObject* p)
{
    if (!isInstalled())
        return new QStandardItemModel(p);


    return new PlugsModel(m_snap.data(), qobject_cast<SnapBackend*>(parent()), p);
}
323
324
325
326
327
328

QString SnapResource::appstreamId() const
{
    const auto ids = m_snap->commonIds();
    return ids.isEmpty() ? QLatin1String("com.snap.") + m_snap->name() : ids.first();
}