summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleix Pol <[email protected]>2017-01-10 00:04:35 +0100
committerAleix Pol <[email protected]>2017-01-10 01:14:11 +0100
commit06bbe4bd21711e16dc39d2c6c7bdfa866c5529cb (patch)
treed0966bb19f1b2f46241c8be1cf9aaaee481bf00d
parenta0fa5c6e0f411f351887ae04291ff2a2753ef901 (diff)
Properly implement resourceByPackateName for KNS
-rw-r--r--libdiscover/backends/KNSBackend/CMakeLists.txt2
-rw-r--r--libdiscover/backends/KNSBackend/KNSBackend.cpp83
-rw-r--r--libdiscover/backends/KNSBackend/KNSBackend.h17
-rw-r--r--libdiscover/backends/KNSBackend/KNSResource.cpp43
-rw-r--r--libdiscover/backends/KNSBackend/KNSResource.h10
-rw-r--r--libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp7
-rw-r--r--libdiscover/backends/KNSBackend/tests/KNSBackendTest.h2
7 files changed, 96 insertions, 68 deletions
diff --git a/libdiscover/backends/KNSBackend/CMakeLists.txt b/libdiscover/backends/KNSBackend/CMakeLists.txt
index 7dbdc4f5..1371561 100644
--- a/libdiscover/backends/KNSBackend/CMakeLists.txt
+++ b/libdiscover/backends/KNSBackend/CMakeLists.txt
@@ -5,7 +5,7 @@ add_library(kns-backend MODULE
KNSResource.cpp
KNSReviews.cpp)
-target_link_libraries(kns-backend Discover::Common KF5::ConfigCore KF5::Attica KF5::NewStuff)
+target_link_libraries(kns-backend Discover::Common KF5::ConfigCore KF5::Attica KF5::NewStuffCore KF5::WidgetsAddons)
install(TARGETS kns-backend DESTINATION ${PLUGIN_INSTALL_DIR}/discover)
install(FILES custom/discover_ktexteditor_codesnippets_core.knsrc DESTINATION ${KDE_INSTALL_CONFDIR})
diff --git a/libdiscover/backends/KNSBackend/KNSBackend.cpp b/libdiscover/backends/KNSBackend/KNSBackend.cpp
index c4aace9..41691d5 100644
--- a/libdiscover/backends/KNSBackend/KNSBackend.cpp
+++ b/libdiscover/backends/KNSBackend/KNSBackend.cpp
@@ -31,7 +31,7 @@
#include <attica/providermanager.h>
// KDE includes
-#include <kns3/downloadmanager.h>
+#include <KNSCore/Engine>
#include <KConfigGroup>
#include <KDesktopFile>
#include <KLocalizedString>
@@ -96,12 +96,14 @@ KNSBackend::KNSBackend(QObject* parent, const QString& iconName, const QString &
setFetching(true);
- m_manager = new KNS3::DownloadManager(m_name, this);
- connect(m_manager, &KNS3::DownloadManager::errorFound, this, [this](const QString &error) { qWarning() << "kns error" << this << error; });
- connect(m_manager, &KNS3::DownloadManager::searchResult, this, &KNSBackend::receivedEntries);
- connect(m_manager, &KNS3::DownloadManager::entryStatusChanged, this, &KNSBackend::statusChanged);
+ m_engine = new KNSCore::Engine(this);
+ m_engine->init(m_name);
+ connect(m_engine, &KNSCore::Engine::signalError, this, [this](const QString &error) { qWarning() << "kns error" << this << error; });
+ connect(m_engine, &KNSCore::Engine::signalEntriesLoaded, this, &KNSBackend::receivedEntries);
+ connect(m_engine, &KNSCore::Engine::signalEntryChanged, this, &KNSBackend::statusChanged);
+ connect(m_engine, &KNSCore::Engine::signalEntryDetailsLoaded, this, &KNSBackend::statusChanged);
m_page = -1;
- m_manager->checkForInstalled();
+ connect(m_engine, &KNSCore::Engine::signalProvidersLoaded, m_engine, &KNSCore::Engine::checkForInstalled);
m_responsePending = true;
const QVector<QPair<FilterType, QString>> filters = { {CategoryFilter, fileName } };
@@ -149,7 +151,19 @@ bool KNSBackend::isValid() const
return m_isValid;
}
-void KNSBackend::receivedEntries(const KNS3::Entry::List& entries)
+KNSResource* KNSBackend::resourceForEntry(const KNSCore::EntryInternal& entry)
+{
+ KNSResource* r = static_cast<KNSResource*>(m_resourcesByName.value(entry.uniqueId()));
+ if (!r) {
+ r = new KNSResource(entry, m_categories, this);
+ m_resourcesByName.insert(entry.uniqueId(), r);
+ } else {
+ r->setEntry(entry);
+ }
+ return r;
+}
+
+void KNSBackend::receivedEntries(const KNSCore::EntryInternal::List& entries)
{
m_responsePending = false;
QTimer::singleShot(0, this, &KNSBackend::availableForQueries);
@@ -162,30 +176,21 @@ void KNSBackend::receivedEntries(const KNS3::Entry::List& entries)
QVector<AbstractResource*> resources;
resources.reserve(entries.count());
- foreach(const KNS3::Entry& entry, entries) {
- auto r = m_resourcesByName.value(entry.id());
- if (!r) {
- r = new KNSResource(entry, m_categories, this);
- m_resourcesByName.insert(entry.id(), r);
- }
- resources += r;
+ foreach(const KNSCore::EntryInternal& entry, entries) {
+ resources += resourceForEntry(entry);
}
// qDebug() << "received" << this << m_resourcesByName.count();
Q_EMIT receivedResources(resources);
if (m_page >= 0) {
++m_page;
- m_manager->search(m_page);
+ m_engine->requestData(m_page, 100);
m_responsePending = true;
}
}
-void KNSBackend::statusChanged(const KNS3::Entry& entry)
+void KNSBackend::statusChanged(const KNSCore::EntryInternal& entry)
{
- KNSResource* r = qobject_cast<KNSResource*>(m_resourcesByName.value(entry.id()));
- if(r)
- r->setEntry(entry);
- else
- qWarning() << "unknown entry changed" << entry.id() << entry.name();
+ resourceForEntry(entry);
}
class KNSTransaction : public Transaction
@@ -193,21 +198,21 @@ class KNSTransaction : public Transaction
public:
KNSTransaction(QObject* parent, KNSResource* res, Transaction::Role role)
: Transaction(parent, res, role)
- , m_id(res->entry().id())
+ , m_id(res->entry().uniqueId())
{
TransactionModel::global()->addTransaction(this);
setCancellable(false);
auto manager = res->knsBackend()->downloadManager();
- connect(manager, &KNS3::DownloadManager::entryStatusChanged, this, &KNSTransaction::anEntryChanged);
+ connect(manager, &KNSCore::Engine::signalEntryChanged, this, &KNSTransaction::anEntryChanged);
}
- void anEntryChanged(const KNS3::Entry& entry) {
- if (entry.id() == m_id) {
+ void anEntryChanged(const KNSCore::EntryInternal& entry) {
+ if (entry.uniqueId() == m_id) {
switch (entry.status()) {
case KNS3::Entry::Invalid:
- qWarning() << "invalid status for" << entry.id() << entry.status();
+ qWarning() << "invalid status for" << entry.uniqueId() << entry.status();
break;
case KNS3::Entry::Installing:
case KNS3::Entry::Updating:
@@ -243,13 +248,13 @@ void KNSBackend::removeApplication(AbstractResource* app)
{
auto res = qobject_cast<KNSResource*>(app);
new KNSTransaction(this, res, Transaction::RemoveRole);
- m_manager->uninstallEntry(res->entry());
+ m_engine->uninstall(res->entry());
}
void KNSBackend::installApplication(AbstractResource* app)
{
auto res = qobject_cast<KNSResource*>(app);
- m_manager->installEntry(res->entry());
+ m_engine->install(res->entry());
new KNSTransaction(this, res, Transaction::InstallRole);
}
@@ -296,8 +301,8 @@ ResultsStream * KNSBackend::searchStream(const QString &searchText)
auto stream = new ResultsStream(QStringLiteral("KNS-search-")+name());
auto start = [this, stream, searchText]() {
- m_manager->setSearchTerm(searchText);
- m_manager->search(0);
+ m_engine->setSearchTerm(searchText);
+ m_engine->requestData(0, 100);
m_responsePending = true;
m_page = 0;
connect(this, &KNSBackend::receivedResources, stream, &ResultsStream::resourcesFound);
@@ -326,14 +331,18 @@ ResultsStream * KNSBackend::findResourceByPackageName(const QUrl& search)
const auto entryid = pathParts.at(1);
auto stream = new ResultsStream(QStringLiteral("KNS-byname-")+entryid);
- auto start = [this, stream, entryid]() {
- qDebug() << "stststst";
- m_manager->fetchEntryById(entryid);
+
+ auto start = [this, entryid, stream]() {
m_responsePending = true;
- m_page = 0;
- connect(this, &KNSBackend::receivedResources, stream, &ResultsStream::resourcesFound);
- connect(this, &KNSBackend::searchFinished, stream, &ResultsStream::deleteLater);
- connect(this, &KNSBackend::startingSearch, stream, &ResultsStream::deleteLater);
+ m_engine->fetchEntryById(entryid);
+ connect(m_engine, &KNSCore::Engine::signalEntryDetailsLoaded, stream, [this, stream, entryid](const KNSCore::EntryInternal &entry) {
+ if (entry.uniqueId() == entryid) {
+ stream->resourcesFound({resourceForEntry(entry)});
+ }
+ m_responsePending = false;
+ QTimer::singleShot(0, this, &KNSBackend::availableForQueries);
+ stream->deleteLater();
+ });
};
if (m_responsePending) {
connect(this, &KNSBackend::availableForQueries, stream, start);
diff --git a/libdiscover/backends/KNSBackend/KNSBackend.h b/libdiscover/backends/KNSBackend/KNSBackend.h
index f2d1b13..1ece8b0 100644
--- a/libdiscover/backends/KNSBackend/KNSBackend.h
+++ b/libdiscover/backends/KNSBackend/KNSBackend.h
@@ -21,20 +21,18 @@
#ifndef KNSBACKEND_H
#define KNSBACKEND_H
-// KDE includes
-#include <KNewStuff3/kns3/entry.h>
+#include <KNSCore/EntryInternal>
-// DiscoverCommon includes
#include <resources/AbstractResourcesBackend.h>
#include "Transaction/AddonList.h"
-
#include "discovercommon_export.h"
class KConfigGroup;
class KNSReviews;
+class KNSResource;
class StandardBackendUpdater;
-namespace KNS3 { class DownloadManager; }
+namespace KNSCore { class Engine; }
class DISCOVERCOMMON_EXPORT KNSBackend : public AbstractResourcesBackend
{
@@ -62,7 +60,7 @@ public:
QString iconName() const { return m_iconName; }
- KNS3::DownloadManager* downloadManager() const { return m_manager; }
+ KNSCore::Engine* downloadManager() const { return m_engine; }
Q_SIGNALS:
void receivedResources(const QVector<AbstractResource*> &resources);
@@ -71,10 +69,11 @@ Q_SIGNALS:
void availableForQueries();
public Q_SLOTS:
- void receivedEntries(const KNS3::Entry::List& entries);
- void statusChanged(const KNS3::Entry& entry);
+ void receivedEntries(const KNSCore::EntryInternal::List& entries);
+ void statusChanged(const KNSCore::EntryInternal& entry);
private:
+ KNSResource* resourceForEntry(const KNSCore::EntryInternal& entry);
void setFetching(bool f);
void markInvalid(const QString &message);
ResultsStream* searchStream(const QString &searchText);
@@ -82,7 +81,7 @@ private:
bool m_responsePending = false;
bool m_fetching;
bool m_isValid;
- KNS3::DownloadManager* m_manager;
+ KNSCore::Engine* m_engine;
QHash<QString, AbstractResource*> m_resourcesByName;
int m_page;
KNSReviews* const m_reviews;
diff --git a/libdiscover/backends/KNSBackend/KNSResource.cpp b/libdiscover/backends/KNSBackend/KNSResource.cpp
index 3319c86..e0b430d 100644
--- a/libdiscover/backends/KNSBackend/KNSResource.cpp
+++ b/libdiscover/backends/KNSBackend/KNSResource.cpp
@@ -23,7 +23,7 @@
#include <QRegularExpression>
#include <knewstuff_version.h>
-KNSResource::KNSResource(const KNS3::Entry& entry, QStringList categories, KNSBackend* parent)
+KNSResource::KNSResource(const KNSCore::EntryInternal& entry, QStringList categories, KNSBackend* parent)
: AbstractResource(parent)
, m_categories(std::move(categories))
, m_entry(entry)
@@ -99,7 +99,7 @@ QString KNSResource::name()
QString KNSResource::packageName() const
{
- return m_entry.id();
+ return m_entry.uniqueId();
}
QStringList KNSResource::categories()
@@ -109,32 +109,26 @@ QStringList KNSResource::categories()
QUrl KNSResource::homepage()
{
- return m_entry.url();
-}
-
-template <class T>
-static T firstIfExists(const QList<T> &list)
-{
- return list.isEmpty() ? T() : list.at(0);
+ return m_entry.homepage();
}
QUrl KNSResource::thumbnailUrl()
{
- return firstIfExists(m_entry.previewThumbnails());
+ return QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewSmall1));
}
QUrl KNSResource::screenshotUrl()
{
- return firstIfExists(m_entry.previewImages());
-}
+ return QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewBig1));
-void KNSResource::setEntry(const KNS3::Entry& entry)
+}
+void KNSResource::setEntry(const KNSCore::EntryInternal& entry)
{
m_entry = entry;
Q_EMIT stateChanged();
}
-KNS3::Entry KNSResource::entry() const
+KNSCore::EntryInternal KNSResource::entry() const
{
return m_entry;
}
@@ -146,7 +140,8 @@ QString KNSResource::license()
int KNSResource::size()
{
- return m_entry.size();
+ const auto downloadInfo = m_entry.downloadLinkInformationList();
+ return downloadInfo.isEmpty() ? 0 : downloadInfo.at(0).size;
}
QString KNSResource::installedVersion() const
@@ -169,9 +164,25 @@ QString KNSResource::section()
return m_entry.category();
}
+static void appendIfValid(QList<QUrl>& list, const QUrl &value)
+{
+ if (value.isValid() && !value.isEmpty())
+ list << value;
+}
+
void KNSResource::fetchScreenshots()
{
- emit screenshotsFetched(m_entry.previewThumbnails(), m_entry.previewImages());
+ QList<QUrl> preview;
+ appendIfValid(preview, QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewSmall1)));
+ appendIfValid(preview, QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewSmall2)));
+ appendIfValid(preview, QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewSmall3)));
+
+ QList<QUrl> screenshots;
+ appendIfValid(screenshots, QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewBig1)));
+ appendIfValid(screenshots, QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewBig2)));
+ appendIfValid(screenshots, QUrl(m_entry.previewUrl(KNSCore::EntryInternal::PreviewBig3)));
+
+ emit screenshotsFetched(preview, screenshots);
}
void KNSResource::fetchChangelog()
diff --git a/libdiscover/backends/KNSBackend/KNSResource.h b/libdiscover/backends/KNSBackend/KNSResource.h
index 3392d09..ea47921 100644
--- a/libdiscover/backends/KNSBackend/KNSResource.h
+++ b/libdiscover/backends/KNSBackend/KNSResource.h
@@ -22,7 +22,7 @@
#define KNSRESOURCE_H
#include <resources/AbstractResource.h>
-#include <kns3/entry.h>
+#include <KNSCore/EntryInternal>
#include <attica/content.h>
#include "discovercommon_export.h"
@@ -32,7 +32,7 @@ class DISCOVERCOMMON_EXPORT KNSResource : public AbstractResource
{
Q_OBJECT
public:
- explicit KNSResource(const KNS3::Entry& c, QStringList categories, KNSBackend* parent);
+ explicit KNSResource(const KNSCore::EntryInternal & c, QStringList categories, KNSBackend* parent);
~KNSResource() override;
AbstractResource::State state() override;
@@ -58,12 +58,12 @@ public:
KNSBackend* knsBackend() const;
- void setEntry(const KNS3::Entry& entry);
- KNS3::Entry entry() const;
+ void setEntry(const KNSCore::EntryInternal& entry);
+ KNSCore::EntryInternal entry() const;
private:
const QStringList m_categories;
- KNS3::Entry m_entry;
+ KNSCore::EntryInternal m_entry;
};
#endif // KNSRESOURCE_H
diff --git a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp
index 1d7fd8b..69b3897 100644
--- a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp
+++ b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp
@@ -130,3 +130,10 @@ void KNSBackendTest::reviewsArrived(AbstractResource* r, const QList< Review* >&
m_r = r;
m_revs = revs;
}
+
+void KNSBackendTest::testResourceByUrl()
+{
+ const auto res = getResources(ResourcesModel::global()->findResourceByPackageName(QUrl(QStringLiteral("kns://plasma-themes.knsrc/api.kde-look.org/1136471"))));
+ QCOMPARE(res.count(), 1);
+}
+
diff --git a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h
index 4502325..17ad7af 100644
--- a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h
+++ b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h
@@ -26,6 +26,7 @@
class AbstractResourcesBackend;
class AbstractResource;
+class ResultsStream;
class Review;
class Category;
@@ -38,6 +39,7 @@ class KNSBackendTest : public QObject
private Q_SLOTS:
void testRetrieval();
void testReviews();
+ void testResourceByUrl();
public Q_SLOTS:
void reviewsArrived(AbstractResource *r, const QList<Review *>& revs);