SnapBackend.cpp 8.33 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 "SnapBackend.h"
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
22
#include "SnapTransaction.h"
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
23
#include "SnapResource.h"
24
#include "appstream/AppStreamIntegration.h"
25
#include <appstream/AppStreamUtils.h>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
26
27
#include <resources/StandardBackendUpdater.h>
#include <resources/SourcesModel.h>
28
#include <Category/Category.h>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
29
#include <Transaction/Transaction.h>
30
#include <resources/StoredResultsStream.h>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
31
32
33
34
35
36
37
38
39
40

#include <KAboutData>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KConfigGroup>
#include <KSharedConfig>
#include <QDebug>
#include <QThread>
#include <QTimer>
#include <QAction>
41
#include <QStandardItemModel>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
42

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
43
44
#include "utils.h"

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
45
DISCOVER_BACKEND_PLUGIN(SnapBackend)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
46

47
48
49
class SnapSourcesBackend : public AbstractSourcesBackend
{
public:
50
    explicit SnapSourcesBackend(AbstractResourcesBackend * parent) : AbstractSourcesBackend(parent), m_model(new QStandardItemModel(this)) {
51
52
53
        auto it = new QStandardItem(i18n("Snap"));
        it->setData(QStringLiteral("Snap"), IdRole);
        m_model->appendRow(it);
54
55
56
57
58
59
    }

    QAbstractItemModel* sources() override { return m_model; }
    bool addSource(const QString& /*id*/) override { return false; }
    bool removeSource(const QString& /*id*/) override { return false;}
    QString idDescription() override { return QStringLiteral("Snap"); }
60
    QVariantList actions() const override { return {}; }
61
62
63
64
65
66
67
68

    bool supportsAdding() const override { return false; }
    bool canMoveSources() const override { return false; }

private:
    QStandardItemModel* const m_model;
};

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
69
70
71
SnapBackend::SnapBackend(QObject* parent)
    : AbstractResourcesBackend(parent)
    , m_updater(new StandardBackendUpdater(this))
72
    , m_reviews(AppStreamIntegration::global()->reviews())
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
73
{
74
75
76
    connect(m_reviews.data(), &OdrsReviewsBackend::ratingsReady, this, [this] {
        m_reviews->emitRatingFetched(this, kTransform<QList<AbstractResource*>>(m_resources.values(), [] (AbstractResource* r) { return r; }));
    });
77
78

    //make sure we populate the installed resources first
79
    refreshStates();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
80

81
    SourcesModel::global()->addSourcesBackend(new SnapSourcesBackend(this));
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
82
83
}

84
85
SnapBackend::~SnapBackend() = default;

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
86
87
88
89
90
int SnapBackend::updatesCount() const
{
    return m_updater->updatesCount();
}

91
92
static ResultsStream* voidStream() { return new ResultsStream(QStringLiteral("Snap-void"), {}); }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
93
ResultsStream * SnapBackend::search(const AbstractResourcesBackend::Filters& filters)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
94
{
95
96
97
    if (!filters.extends.isEmpty()) {
        return voidStream();
    } else if (!filters.resourceUrl.isEmpty()) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
98
        return findResourceByPackageName(filters.resourceUrl);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
99
100
    } else if (filters.category && filters.category->isAddons()) {
        return voidStream();
101
    } else if (filters.state >= AbstractResource::Installed || filters.origin == QLatin1String("Snap")) {
102
        std::function<bool(const QSharedPointer<QSnapdSnap>&)> f = [filters](const QSharedPointer<QSnapdSnap>& s) { return filters.search.isEmpty() || s->name().contains(filters.search, Qt::CaseInsensitive) || s->description().contains(filters.search, Qt::CaseInsensitive); };
103
        return populateWithFilter(m_client.getSnaps(), f);
104
    } else if (!filters.search.isEmpty()) {
105
        return populate(m_client.find(QSnapdClient::FindFlag::None, filters.search));
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
106
    }
107
    return voidStream();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
108
109
}

110
ResultsStream * SnapBackend::findResourceByPackageName(const QUrl& search)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
111
{
112
    Q_ASSERT(!search.host().isEmpty() || !AppStreamUtils::appstreamId(search).isEmpty());
113
114
    return search.scheme() == QLatin1String("snap")      ? populate(m_client.find(QSnapdClient::MatchName, search.host())) :
#ifdef SNAP_FIND_COMMON_ID
115
           search.scheme() == QLatin1String("appstream") ? populate(m_client.find(QSnapdClient::MatchCommonId, AppStreamUtils::appstreamId(search))) :
116
#endif
117
                voidStream();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
118
119
}

120
template <class T>
121
122
123
124
125
126
127
128
ResultsStream* SnapBackend::populate(T* snaps)
{
    std::function<bool(const QSharedPointer<QSnapdSnap>&)> acceptAll = [](const QSharedPointer<QSnapdSnap>&){ return true; };
    return populateWithFilter(snaps, acceptAll);
}

template <class T>
ResultsStream* SnapBackend::populateWithFilter(T* job, std::function<bool(const QSharedPointer<QSnapdSnap>& s)>& filter)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
129
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
130
    auto stream = new ResultsStream(QStringLiteral("Snap-populate"));
131

132
    connect(job, &T::complete, stream, [stream, this, job, filter]() {
133
134
        if (job->error()) {
            qDebug() << "error:" << job->error() << job->errorString();
135
            stream->finish();
136
137
            return;
        }
138

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
139
        QVector<AbstractResource*> ret;
140
141
        QVector<SnapResource*> resources;
        ret.reserve(job->snapCount());
142
        resources.reserve(job->snapCount());
143
        for (int i=0, c=job->snapCount(); i<c; ++i) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
144
            QSharedPointer<QSnapdSnap> snap(job->snap(i));
145
146
147
148

            if (!filter(snap))
                continue;

149
150
            const auto snapname = snap->name();
            SnapResource* res = m_resources.value(snapname);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
151
            if (!res) {
152
                res = new SnapResource(snap, AbstractResource::None, this);
153
                Q_ASSERT(res->packageName() == snapname);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
154
                resources += res;
155
            } else {
156
                res->setSnap(snap);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
157
158
            }
            ret += res;
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
159
160
        }

161
162
163
        foreach(SnapResource* res, resources)
            m_resources[res->packageName()] = res;

164
        if (!ret.isEmpty())
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
165
            Q_EMIT stream->resourcesFound(ret);
Jonathan Riddell's avatar
Jonathan Riddell committed
166
        stream->finish();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
167
    });
168
    job->runAsync();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
169
    return stream;
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
170
171
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
172
173
174
175
176
177
178
179
180
181
void SnapBackend::setFetching(bool fetching)
{
    if (m_fetching != fetching) {
        m_fetching = fetching;
        Q_EMIT fetchingChanged();
    } else {
        qWarning() << "fetching already on state" << fetching;
    }
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
182
183
184
185
186
187
188
AbstractBackendUpdater* SnapBackend::backendUpdater() const
{
    return m_updater;
}

AbstractReviewsBackend* SnapBackend::reviewsBackend() const
{
189
    return m_reviews.data();
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
190
191
}

192
Transaction* SnapBackend::installApplication(AbstractResource* app, const AddonList& addons)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
193
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
194
    Q_ASSERT(addons.isEmpty());
195
    return installApplication(app);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
196
197
}

198
Transaction* SnapBackend::installApplication(AbstractResource* _app)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
199
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
200
    auto app = qobject_cast<SnapResource*>(_app);
201
	return new SnapTransaction(&m_client, app, Transaction::InstallRole, AbstractResource::Installed);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
202
203
}

204
Transaction* SnapBackend::removeApplication(AbstractResource* _app)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
205
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
206
    auto app = qobject_cast<SnapResource*>(_app);
207
	return new SnapTransaction(&m_client, app, Transaction::RemoveRole, AbstractResource::None);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
208
209
}

210
211
212
213
214
QString SnapBackend::displayName() const
{
    return QStringLiteral("Snap");
}

215
216
void SnapBackend::refreshStates()
{
217
    auto ret = new StoredResultsStream({populate(m_client.getSnaps())});
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
218
    connect(ret, &StoredResultsStream::finishedResources, this, [this] (const QVector<AbstractResource*>& resources){
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
219
        for (auto res: qAsConst(m_resources)) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
220
            if (resources.contains(res))
221
222
223
224
225
                res->setState(AbstractResource::Installed);
            else
                res->setState(AbstractResource::None);
        }
    });
226
227
}

228
#include "SnapBackend.moc"