Adapt leaf Category names for kns backend, and ensure correct filters

Previously, categories would use the name in KNSRC files (which was
not intended as a display name as much as a handy human friendly
index, in place of the database indices used on the remote server),
and this patch adapts the categories to reflect the intended display
name for those categories after the knewstuff engine has been
initialised. It further ensures that categories are set correctly
for kns resources upon creation.

Differential Revision: https://phabricator.kde.org/D19899
parent 356fe501
......@@ -124,6 +124,12 @@ QString Category::name() const
return m_name;
}
void Category::setName(const QString& name)
{
m_name = name;
Q_EMIT nameChanged();
}
QString Category::icon() const
{
return m_iconString;
......
......@@ -44,7 +44,7 @@ class DISCOVERCOMMON_EXPORT Category : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QString name READ name CONSTANT)
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Q_PROPERTY(QString icon READ icon CONSTANT)
Q_PROPERTY(QObject* parent READ parent CONSTANT)
Q_PROPERTY(QUrl decoration READ decoration CONSTANT)
......@@ -55,6 +55,9 @@ public:
~Category() override;
QString name() const;
// You should never attempt to change the name of anything that is not a leaf category
// as the results could be potentially detremental to the function of the category filters
void setName(const QString& name);
QString icon() const;
QVector<QPair<FilterType, QString> > andFilters() const;
void setAndFilter(QVector<QPair<FilterType, QString> > filters);
......@@ -79,6 +82,7 @@ public:
Q_SIGNALS:
void subCategoriesChanged();
void nameChanged();
private:
QString m_name;
......
......@@ -115,15 +115,6 @@ KNSBackend::KNSBackend(QObject* parent, const QString& iconName, const QString &
setFetching(true);
m_engine = new KNSCore::Engine(this);
connect(m_engine, &KNSCore::Engine::signalErrorCode, this, &KNSBackend::signalErrorCode);
connect(m_engine, &KNSCore::Engine::signalEntriesLoaded, this, &KNSBackend::receivedEntries, Qt::QueuedConnection);
connect(m_engine, &KNSCore::Engine::signalEntryChanged, this, &KNSBackend::statusChanged, Qt::QueuedConnection);
connect(m_engine, &KNSCore::Engine::signalEntryDetailsLoaded, this, &KNSBackend::statusChanged);
connect(m_engine, &KNSCore::Engine::signalProvidersLoaded, this, &KNSBackend::fetchInstalled);
m_engine->setPageSize(100);
m_engine->init(m_name);
// This ensures we have something to track when checking after the initialization timeout
connect(this, &KNSBackend::initialized, this, [this](){ m_initialized = true; });
// If we have not initialized in 60 seconds, consider this KNS backend invalid
......@@ -151,6 +142,26 @@ KNSBackend::KNSBackend(QObject* parent, const QString& iconName, const QString &
for(const auto &cat: cats)
categories << new Category(cat, {}, { {CategoryFilter, cat } }, backendName, {}, {}, true);
}
m_engine = new KNSCore::Engine(this);
connect(m_engine, &KNSCore::Engine::signalErrorCode, this, &KNSBackend::signalErrorCode);
connect(m_engine, &KNSCore::Engine::signalEntriesLoaded, this, &KNSBackend::receivedEntries, Qt::QueuedConnection);
connect(m_engine, &KNSCore::Engine::signalEntryChanged, this, &KNSBackend::statusChanged, Qt::QueuedConnection);
connect(m_engine, &KNSCore::Engine::signalEntryDetailsLoaded, this, &KNSBackend::statusChanged);
connect(m_engine, &KNSCore::Engine::signalProvidersLoaded, this, &KNSBackend::fetchInstalled);
connect(m_engine, &KNSCore::Engine::signalCategoriesMetadataLoded, this, [categories](const QList< KNSCore::Provider::CategoryMetadata>& categoryMetadatas){
for (const KNSCore::Provider::CategoryMetadata& category : categoryMetadatas) {
for (Category* cat : categories) {
if (cat->orFilters().count() > 0 && cat->orFilters().first().second == category.name) {
cat->setName(category.displayName);
break;
}
}
}
});
m_engine->setPageSize(100);
m_engine->init(m_name);
static const QString knsrcApplications = QLatin1String("storekdeapps.knsrc");
if(knsrcApplications == fileName) {
......@@ -246,7 +257,16 @@ 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 + QStringList{entry.category()}, this);
QStringList categories{name(), m_rootCategories.first()->name()};
const auto cats = m_engine->categoriesMetadata();
const int catIndex = kIndexOf(cats, [&entry](const KNSCore::Provider::CategoryMetadata& cat){ return entry.category() == cat.id; });
if (catIndex > -1) {
categories << cats.at(catIndex).name;
}
if(m_hasApplications) {
categories << QLatin1String("Application");
}
r = new KNSResource(entry, categories, this);
m_resourcesByName.insert(entry.uniqueId(), r);
} else {
r->setEntry(entry);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment