diff --git a/libdiscover/resources/ResourcesModel.cpp b/libdiscover/resources/ResourcesModel.cpp index 0622262e..3faf2750 100644 --- a/libdiscover/resources/ResourcesModel.cpp +++ b/libdiscover/resources/ResourcesModel.cpp @@ -1,403 +1,399 @@ /*************************************************************************** * Copyright © 2012 Aleix Pol Gonzalez * * * * 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 . * ***************************************************************************/ #include "ResourcesModel.h" #include "AbstractResource.h" #include "resources/AbstractResourcesBackend.h" #include "resources/AbstractBackendUpdater.h" #include #include #include #include #include #include "Transaction/TransactionModel.h" #include "Category/CategoryModel.h" #include "utils.h" #include #include #include #include #include #include #include #include ResourcesModel *ResourcesModel::s_self = nullptr; ResourcesModel *ResourcesModel::global() { if(!s_self) s_self = new ResourcesModel; return s_self; } ResourcesModel::ResourcesModel(QObject* parent, bool load) : QObject(parent) , m_initializingBackends(0) , m_actionCollection(nullptr) , m_currentApplicationBackend(nullptr) { init(load); connect(this, &ResourcesModel::allInitialized, this, &ResourcesModel::fetchingChanged); connect(this, &ResourcesModel::backendsChanged, this, &ResourcesModel::initApplicationsBackend); } void ResourcesModel::init(bool load) { Q_ASSERT(!s_self); Q_ASSERT(QCoreApplication::instance()->thread()==QThread::currentThread()); if(load) QMetaObject::invokeMethod(this, "registerAllBackends", Qt::QueuedConnection); QAction* updateAction = new QAction(this); updateAction->setIcon(QIcon::fromTheme(QStringLiteral("system-software-update"))); updateAction->setText(i18nc("@action Checks the Internet for updates", "Check for Updates")); updateAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); connect(this, &ResourcesModel::fetchingChanged, updateAction, [updateAction, this](){ updateAction->setEnabled(!isFetching()); }); connect(updateAction, &QAction::triggered, this, &ResourcesModel::checkForUpdates); m_ownActions += updateAction; } ResourcesModel::ResourcesModel(const QString& backendName, QObject* parent) : ResourcesModel(parent, false) { s_self = this; registerBackendByName(backendName); } ResourcesModel::~ResourcesModel() { qDeleteAll(m_backends); } void ResourcesModel::addResourcesBackend(AbstractResourcesBackend* backend) { Q_ASSERT(!m_backends.contains(backend)); if(!backend->isValid()) { qWarning() << "Discarding invalid backend" << backend->name(); CategoryModel::global()->blacklistPlugin(backend->name()); backend->deleteLater(); return; } m_backends += backend; if(!backend->isFetching()) { if (backend->updatesCount() > 0) emit updatesCountChanged(); } else { m_initializingBackends++; } if(m_actionCollection) backend->integrateActions(m_actionCollection); connect(backend, &AbstractResourcesBackend::fetchingChanged, this, &ResourcesModel::callerFetchingChanged); connect(backend, &AbstractResourcesBackend::allDataChanged, this, &ResourcesModel::updateCaller); connect(backend, &AbstractResourcesBackend::resourcesChanged, this, &ResourcesModel::resourceDataChanged); connect(backend, &AbstractResourcesBackend::updatesCountChanged, this, &ResourcesModel::updatesCountChanged); connect(backend, &AbstractResourcesBackend::resourceRemoved, this, &ResourcesModel::resourceRemoved); connect(backend, &AbstractResourcesBackend::passiveMessage, this, &ResourcesModel::passiveMessage); connect(backend->backendUpdater(), &AbstractBackendUpdater::progressingChanged, this, &ResourcesModel::fetchingChanged); if(m_initializingBackends==0) emit allInitialized(); else emit fetchingChanged(); } void ResourcesModel::callerFetchingChanged() { AbstractResourcesBackend* backend = qobject_cast(sender()); if (!backend->isValid()) { qWarning() << "Discarding invalid backend" << backend->name(); int idx = m_backends.indexOf(backend); Q_ASSERT(idx>=0); m_backends.removeAt(idx); // Q_EMIT backendsChanged(); CategoryModel::global()->blacklistPlugin(backend->name()); backend->deleteLater(); return; } if(backend->isFetching()) { m_initializingBackends++; emit fetchingChanged(); } else { m_initializingBackends--; if(m_initializingBackends==0) emit allInitialized(); else emit fetchingChanged(); } } void ResourcesModel::updateCaller(const QVector& properties) { AbstractResourcesBackend* backend = qobject_cast(sender()); Q_EMIT backendDataChanged(backend, properties); } QVector< AbstractResourcesBackend* > ResourcesModel::backends() const { return m_backends; } int ResourcesModel::updatesCount() const { int ret = 0; foreach(AbstractResourcesBackend* backend, m_backends) { ret += backend->updatesCount(); } return ret; } void ResourcesModel::installApplication(AbstractResource* app) { TransactionModel::global()->addTransaction(app->backend()->installApplication(app)); } void ResourcesModel::installApplication(AbstractResource* app, const AddonList& addons) { TransactionModel::global()->addTransaction(app->backend()->installApplication(app, addons)); } void ResourcesModel::removeApplication(AbstractResource* app) { TransactionModel::global()->addTransaction(app->backend()->removeApplication(app)); } void ResourcesModel::registerAllBackends() { DiscoverBackendsFactory f; const auto backends = f.allBackends(); if(m_initializingBackends==0 && backends.isEmpty()) { qWarning() << "Couldn't find any backends"; emit allInitialized(); } else { foreach(AbstractResourcesBackend* b, backends) { addResourcesBackend(b); } emit backendsChanged(); } } void ResourcesModel::registerBackendByName(const QString& name) { DiscoverBackendsFactory f; for(auto b : f.backend(name)) addResourcesBackend(b); emit backendsChanged(); } void ResourcesModel::integrateActions(KActionCollection* w) { Q_ASSERT(w->thread()==thread()); m_actionCollection = w; setParent(w); foreach(AbstractResourcesBackend* b, m_backends) { b->integrateActions(w); } } bool ResourcesModel::isFetching() const { foreach(AbstractResourcesBackend* b, m_backends) { // isFetching should sort of be enough. However, sometimes the backend itself // will still be operating on things, which from a model point of view would // still mean something going on. So, interpret that as fetching as well, for // the purposes of this property. if(b->isFetching() || (b->backendUpdater() && b->backendUpdater()->isProgressing())) { return true; } } return false; } QList ResourcesModel::messageActions() const { QList ret = m_ownActions; foreach(AbstractResourcesBackend* b, m_backends) { ret += b->messageActions(); } Q_ASSERT(!ret.contains(nullptr)); return ret; } bool ResourcesModel::isBusy() const { return TransactionModel::global()->rowCount() > 0; } bool ResourcesModel::isExtended(const QString& id) { bool ret = true; foreach (AbstractResourcesBackend* backend, m_backends) { ret = backend->extends().contains(id); if (ret) break; } return ret; } AggregatedResultsStream::AggregatedResultsStream(const QSet& streams) : ResultsStream(QStringLiteral("AggregatedResultsStream")) { Q_ASSERT(!streams.contains(nullptr)); if (streams.isEmpty()) { qWarning() << "no streams to aggregate!!"; QTimer::singleShot(0, this, &AggregatedResultsStream::clear); } for (auto stream: streams) { connect(stream, &ResultsStream::resourcesFound, this, &AggregatedResultsStream::addResults); connect(stream, &QObject::destroyed, this, &AggregatedResultsStream::destruction); m_streams << stream; } m_delayedEmission.setInterval(0); connect(&m_delayedEmission, &QTimer::timeout, this, &AggregatedResultsStream::emitResults); } void AggregatedResultsStream::addResults(const QVector& res) { m_results += res; m_delayedEmission.start(); } void AggregatedResultsStream::emitResults() { if (!m_results.isEmpty()) { Q_EMIT resourcesFound(m_results); m_results.clear(); } m_delayedEmission.setInterval(m_delayedEmission.interval() + 100); m_delayedEmission.stop(); } void AggregatedResultsStream::destruction(QObject* obj) { m_streams.remove(obj); clear(); } void AggregatedResultsStream::clear() { if (m_streams.isEmpty()) { emitResults(); Q_EMIT finished(); deleteLater(); } } AggregatedResultsStream * ResourcesModel::findResourceByPackageName(const QUrl& search) { QSet streams; foreach(auto backend, m_backends) { streams << backend->findResourceByPackageName(search); } return new AggregatedResultsStream(streams); } AggregatedResultsStream* ResourcesModel::search(const AbstractResourcesBackend::Filters& search) { QSet streams; - - const bool allBackends = search.allBackends; foreach(auto backend, m_backends) { - if (!backend->hasApplications() || ResourcesModel::global()->currentApplicationBackend() == backend || allBackends) { - streams << backend->search(search); - } + streams << backend->search(search); } return new AggregatedResultsStream(streams); } AbstractResource* ResourcesModel::resourceForFile(const QUrl& file) { AbstractResource* ret = nullptr; foreach(auto backend, m_backends) { ret = backend->resourceForFile(file); if (ret) break; } return ret; } void ResourcesModel::checkForUpdates() { for(auto backend: qAsConst(m_backends)) backend->checkForUpdates(); } QVector ResourcesModel::applicationBackends() const { return kFilter>(m_backends, [](AbstractResourcesBackend* b){ return b->hasApplications(); }); } QVariantList ResourcesModel::applicationBackendsVariant() const { return kTransform(applicationBackends(), [](AbstractResourcesBackend* b) {return QVariant::fromValue(b);}); } AbstractResourcesBackend* ResourcesModel::currentApplicationBackend() const { return m_currentApplicationBackend; } void ResourcesModel::setCurrentApplicationBackend(AbstractResourcesBackend* backend, bool write) { if (backend != m_currentApplicationBackend) { if (write) { KConfigGroup settings(KSharedConfig::openConfig(), "ResourcesModel"); if (backend) settings.writeEntry("currentApplicationBackend", backend->name()); else settings.deleteEntry("currentApplicationBackend"); } qDebug() << "setting currentApplicationBackend" << backend; m_currentApplicationBackend = backend; Q_EMIT currentApplicationBackendChanged(backend); } } void ResourcesModel::initApplicationsBackend() { KConfigGroup settings(KSharedConfig::openConfig(), "ResourcesModel"); const QString name = settings.readEntry("currentApplicationBackend", QStringLiteral("packagekit-backend")); const auto backends = applicationBackends(); auto idx = kIndexOf(backends, [name](AbstractResourcesBackend* b) { return b->name() == name; }); if (idx<0) { idx = kIndexOf(backends, [](AbstractResourcesBackend* b) { return b->hasApplications(); }); qDebug() << "falling back applications backend to" << idx; } setCurrentApplicationBackend(backends.value(idx, nullptr), false); } diff --git a/libdiscover/resources/ResourcesProxyModel.cpp b/libdiscover/resources/ResourcesProxyModel.cpp index 8fad9d4f..af9779f0 100644 --- a/libdiscover/resources/ResourcesProxyModel.cpp +++ b/libdiscover/resources/ResourcesProxyModel.cpp @@ -1,486 +1,542 @@ /*************************************************************************** * Copyright © 2010 Jonathan Thomas * * Copyright © 2012 Aleix Pol Gonzalez * * * * 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 . * ***************************************************************************/ #include "ResourcesProxyModel.h" #include #include #include #include "ResourcesModel.h" #include "AbstractResource.h" #include "AbstractResourcesBackend.h" #include #include #include ResourcesProxyModel::ResourcesProxyModel(QObject *parent) : QAbstractListModel(parent) , m_sortRole(NameRole) , m_sortOrder(Qt::AscendingOrder) , m_sortByRelevancy(false) , m_roles({ { NameRole, "name" }, { IconRole, "icon" }, { CommentRole, "comment" }, { StateRole, "state" }, { RatingRole, "rating" }, { RatingPointsRole, "ratingPoints" }, { RatingCountRole, "ratingCount" }, { SortableRatingRole, "sortableRating" }, { InstalledRole, "isInstalled" }, { ApplicationRole, "application" }, { OriginRole, "origin" }, { DisplayOriginRole, "displayOrigin" }, { CanUpgrade, "canUpgrade" }, { PackageNameRole, "packageName" }, { IsTechnicalRole, "isTechnical" }, { CategoryRole, "category" }, { CategoryDisplayRole, "categoryDisplay" }, { SectionRole, "section" }, { MimeTypes, "mimetypes" }, { LongDescriptionRole, "longDescription" }, { SizeRole, "size" } }) , m_currentStream(nullptr) { // new ModelTest(this, this); connect(ResourcesModel::global(), &ResourcesModel::backendsChanged, this, &ResourcesProxyModel::invalidateFilter); connect(ResourcesModel::global(), &ResourcesModel::backendDataChanged, this, &ResourcesProxyModel::refreshBackend); connect(ResourcesModel::global(), &ResourcesModel::resourceDataChanged, this, &ResourcesProxyModel::refreshResource); connect(ResourcesModel::global(), &ResourcesModel::resourceRemoved, this, &ResourcesProxyModel::removeResource); } void ResourcesProxyModel::componentComplete() { m_setup = true; invalidateFilter(); } QHash ResourcesProxyModel::roleNames() const { return m_roles; } void ResourcesProxyModel::setSortRole(Roles sortRole) { if (sortRole != m_sortRole) { Q_ASSERT(roleNames().contains(sortRole)); m_sortRole = sortRole; Q_EMIT sortRoleChanged(sortRole); invalidateSorting(); } } void ResourcesProxyModel::setSortOrder(Qt::SortOrder sortOrder) { if (sortOrder != m_sortOrder) { m_sortOrder = sortOrder; Q_EMIT sortRoleChanged(sortOrder); invalidateSorting(); } } void ResourcesProxyModel::setSearch(const QString &_searchText) { // 1-character searches are painfully slow. >= 2 chars are fine, though const QString searchText = _searchText.count() <= 1 ? QString() : _searchText; const bool diff = searchText != m_filters.search; if (diff) { m_filters.search = searchText; m_sortByRelevancy = !searchText.isEmpty(); invalidateFilter(); Q_EMIT searchChanged(m_filters.search); } } +void ResourcesProxyModel::removeDuplicates(QVector& resources) +{ + const auto cab = ResourcesModel::global()->currentApplicationBackend(); + QHash::iterator> storedIds; + for(auto it = m_displayedResources.begin(); it != m_displayedResources.end(); ) + { + const auto appstreamid = (*it)->appstreamId(); + if (appstreamid.isEmpty()) { + ++it; + continue; + } + auto at = storedIds.find(appstreamid); + if (at == storedIds.end()) { + storedIds[appstreamid] = it; + ++it; + } else { + qWarning() << "We should have sanitized the displayed resources. There is a bug"; + Q_UNREACHABLE(); + } + } + + QHash::iterator> ids; + for(auto it = resources.begin(); it != resources.end(); ) + { + const auto appstreamid = (*it)->appstreamId(); + if (appstreamid.isEmpty()) { + ++it; + continue; + } + auto at = storedIds.find(appstreamid); + if (at == storedIds.end()) { + auto at = ids.find(appstreamid); + if (at == ids.end()) { + ids[appstreamid] = it; + ++it; + } else { + if ((*it)->backend() == cab) { + qSwap(*it, **at); + } + it = resources.erase(it); + } + } else { + if ((*it)->backend() == cab) { + **at = *it; + auto pos = index(*at - m_displayedResources.begin(), 0); + dataChanged(pos, pos); + } + it = resources.erase(it); + } + } +} + void ResourcesProxyModel::addResources(const QVector& _res) { auto res = _res; m_filters.filterJustInCase(res); if (res.isEmpty()) return; + if (!m_filters.allBackends) { + removeDuplicates(res); + } if (!m_sortByRelevancy) qSort(res.begin(), res.end(), [this](AbstractResource* res, AbstractResource* res2){ return lessThan(res, res2); }); sortedInsertion(res); fetchSubcategories(); + } void ResourcesProxyModel::invalidateSorting() { if (m_displayedResources.isEmpty()) return; if (!m_sortByRelevancy) { beginResetModel(); qSort(m_displayedResources.begin(), m_displayedResources.end(), [this](AbstractResource* res, AbstractResource* res2){ return lessThan(res, res2); }); endResetModel(); } } QString ResourcesProxyModel::lastSearch() const { return m_filters.search; } void ResourcesProxyModel::setOriginFilter(const QString &origin) { if (origin == originFilter()) return; if(origin.isEmpty()) m_filters.roles.remove("origin"); else m_filters.roles.insert("origin", origin); invalidateFilter(); } QString ResourcesProxyModel::originFilter() const { return m_filters.roles.value("origin").toString(); } void ResourcesProxyModel::setFiltersFromCategory(Category *category) { if(category==m_filters.category) return; m_filters.category = category; invalidateFilter(); emit categoryChanged(); } void ResourcesProxyModel::fetchSubcategories() { const auto cats = m_filters.category ? m_filters.category->subCategories() : CategoryModel::global()->rootCategories(); const int count = rowCount(); QSet done; for (int i=0; icategoryObjects()); } const QVariantList ret = kTransform(done, [](Category* cat) { return QVariant::fromValue(cat); }); if (ret != m_subcategories) { m_subcategories = ret; Q_EMIT subcategoriesChanged(m_subcategories); } } QVariantList ResourcesProxyModel::subcategories() const { return m_subcategories; } void ResourcesProxyModel::invalidateFilter() { if (!m_setup || ResourcesModel::global()->backends().isEmpty()) { return; } if (m_currentStream) { qWarning() << "last stream isn't over yet" << m_filters << this; delete m_currentStream; } m_currentStream = ResourcesModel::global()->search(m_filters); beginResetModel(); m_displayedResources.clear(); endResetModel(); connect(m_currentStream, &AggregatedResultsStream::resourcesFound, this, [this](const QVector& resources) { addResources(resources); }); connect(m_currentStream, &AggregatedResultsStream::finished, this, [this]() { m_currentStream = nullptr; Q_EMIT busyChanged(false); }); Q_EMIT busyChanged(true); } int ResourcesProxyModel::rowCount(const QModelIndex& parent) const { return parent.isValid() ? 0 : m_displayedResources.count(); } bool ResourcesProxyModel::lessThan(AbstractResource* leftPackage, AbstractResource* rightPackage) const { auto role = m_sortRole; Qt::SortOrder order = m_sortOrder; QVariant leftValue; QVariant rightValue; //if we're comparing two equal values, we want the model sorted by application name if(role != NameRole) { leftValue = roleToValue(leftPackage, role); rightValue = roleToValue(rightPackage, role); if (leftValue == rightValue) { role = NameRole; order = Qt::DescendingOrder; } } bool ret; if(role == NameRole) { ret = leftPackage->nameSortKey().compare(rightPackage->nameSortKey()) < 0; } else if(role == CanUpgrade) { ret = leftValue.toBool(); } else { ret = leftValue < rightValue; } return ret != (order != Qt::AscendingOrder); } Category* ResourcesProxyModel::filteredCategory() const { return m_filters.category; } void ResourcesProxyModel::setStateFilter(AbstractResource::State s) { if (s != m_filters.state) { m_filters.state = s; invalidateFilter(); emit stateFilterChanged(); } } AbstractResource::State ResourcesProxyModel::stateFilter() const { return m_filters.state; } QString ResourcesProxyModel::mimeTypeFilter() const { return m_filters.mimetype; } void ResourcesProxyModel::setMimeTypeFilter(const QString& mime) { if (m_filters.mimetype != mime) { m_filters.mimetype = mime; invalidateFilter(); } } QString ResourcesProxyModel::extends() const { return m_filters.extends; } void ResourcesProxyModel::setExtends(const QString& extends) { if (m_filters.extends != extends) { m_filters.extends = extends; invalidateFilter(); } } QUrl ResourcesProxyModel::resourcesUrl() const { return m_filters.resourceUrl; } void ResourcesProxyModel::setResourcesUrl(const QUrl& resourcesUrl) { if (m_filters.resourceUrl != resourcesUrl) { m_filters.resourceUrl = resourcesUrl; invalidateFilter(); } } bool ResourcesProxyModel::allBackends() const { return m_filters.allBackends; } void ResourcesProxyModel::setAllBackends(bool allBackends) { m_filters.allBackends = allBackends; } QVariant ResourcesProxyModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) { return QVariant(); } AbstractResource* const resource = m_displayedResources[index.row()]; return roleToValue(resource, role); } QVariant ResourcesProxyModel::roleToValue(AbstractResource* resource, int role) const { switch(role) { case ApplicationRole: return qVariantFromValue(resource); case RatingPointsRole: case RatingRole: case RatingCountRole: case SortableRatingRole: { Rating* const rating = resource->rating(); const int idx = Rating::staticMetaObject.indexOfProperty(roleNames().value(role).constData()); Q_ASSERT(idx >= 0); auto prop = Rating::staticMetaObject.property(idx); if (rating) return prop.read(rating); else { QVariant val(0); val.convert(prop.type()); return val; } } case Qt::DecorationRole: case Qt::DisplayRole: case Qt::StatusTipRole: case Qt::ToolTipRole: return QVariant(); default: { QByteArray roleText = roleNames().value(role); if(Q_UNLIKELY(roleText.isEmpty())) { qDebug() << "unsupported role" << role; return {}; } static const QMetaObject* m = &AbstractResource::staticMetaObject; int propidx = roleText.isEmpty() ? -1 : m->indexOfProperty(roleText.constData()); if(Q_UNLIKELY(propidx < 0)) { qWarning() << "unknown role:" << role << roleText; return QVariant(); } else return m->property(propidx).read(resource); } } } void ResourcesProxyModel::sortedInsertion(const QVector & resources) { Q_ASSERT(!resources.isEmpty()); if (m_sortByRelevancy) { int rows = rowCount(); beginInsertRows({}, rows, rows+resources.count()-1); m_displayedResources += resources; endInsertRows(); return; } - int newIdx = 0; for(auto resource: resources) { + int newIdx = 0; const auto finder = [this, resource](AbstractResource* res){ return lessThan(resource, res); }; const auto it = std::find_if(m_displayedResources.constBegin() + newIdx, m_displayedResources.constEnd(), finder); newIdx = it == m_displayedResources.constEnd() ? m_displayedResources.count() : (it - m_displayedResources.constBegin()); beginInsertRows({}, newIdx, newIdx); m_displayedResources.insert(newIdx, resource); endInsertRows(); } } void ResourcesProxyModel::refreshResource(AbstractResource* resource, const QVector& properties) { const auto residx = m_displayedResources.indexOf(resource); if (residx<0) { if (!m_sortByRelevancy && m_filters.shouldFilter(resource)) { sortedInsertion({resource}); } return; } if (!m_filters.shouldFilter(resource)) { beginRemoveRows({}, residx, residx); m_displayedResources.removeAt(residx); endRemoveRows(); return; } const QModelIndex idx = index(residx, 0); Q_ASSERT(idx.isValid()); const auto roles = propertiesToRoles(properties); if (roles.contains(m_sortRole)) { beginRemoveRows({}, residx, residx); m_displayedResources.removeAt(residx); endRemoveRows(); sortedInsertion({resource}); } else emit dataChanged(idx, idx, roles); } void ResourcesProxyModel::removeResource(AbstractResource* resource) { const auto residx = m_displayedResources.indexOf(resource); if (residx < 0) return; beginRemoveRows({}, residx, residx); m_displayedResources.removeAt(residx); endRemoveRows(); } void ResourcesProxyModel::refreshBackend(AbstractResourcesBackend* backend, const QVector& properties) { auto roles = propertiesToRoles(properties); const int count = m_displayedResources.count(); bool found = false; for(int i = 0; ibackend()) continue; int j = i+1; for(; jbackend(); ++j) {} Q_EMIT dataChanged(index(i, 0), index(j-1, 0), roles); i = j; found = true; } if (found && properties.contains(m_roles.value(m_sortRole))) { invalidateSorting(); } } QVector ResourcesProxyModel::propertiesToRoles(const QVector& properties) const { QVector roles = kTransform>(properties, [this](const QByteArray& arr) { return roleNames().key(arr, -1); }); roles.removeAll(-1); return roles; } int ResourcesProxyModel::indexOf(AbstractResource* res) { return m_displayedResources.indexOf(res); } AbstractResource * ResourcesProxyModel::resourceAt(int row) const { return m_displayedResources[row]; } diff --git a/libdiscover/resources/ResourcesProxyModel.h b/libdiscover/resources/ResourcesProxyModel.h index bf09bc9e..0297ab38 100644 --- a/libdiscover/resources/ResourcesProxyModel.h +++ b/libdiscover/resources/ResourcesProxyModel.h @@ -1,163 +1,164 @@ /*************************************************************************** * Copyright © 2010 Jonathan Thomas * * Copyright © 2012 Aleix Pol Gonzalez * * * * 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 . * ***************************************************************************/ #ifndef RESOURCESPROXYMODEL_H #define RESOURCESPROXYMODEL_H #include #include #include #include #include #include "discovercommon_export.h" #include "AbstractResource.h" #include "AbstractResourcesBackend.h" class Transaction; class AggregatedResultsStream; class DISCOVERCOMMON_EXPORT ResourcesProxyModel : public QAbstractListModel, public QQmlParserStatus { Q_OBJECT Q_INTERFACES(QQmlParserStatus) Q_PROPERTY(Roles sortRole READ sortRole WRITE setSortRole NOTIFY sortRoleChanged) Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged) Q_PROPERTY(Category* filteredCategory READ filteredCategory WRITE setFiltersFromCategory NOTIFY categoryChanged) Q_PROPERTY(QString originFilter READ originFilter WRITE setOriginFilter) Q_PROPERTY(AbstractResource::State stateFilter READ stateFilter WRITE setStateFilter NOTIFY stateFilterChanged) Q_PROPERTY(QString mimeTypeFilter READ mimeTypeFilter WRITE setMimeTypeFilter) Q_PROPERTY(QString search READ lastSearch WRITE setSearch NOTIFY searchChanged) Q_PROPERTY(QUrl resourcesUrl READ resourcesUrl WRITE setResourcesUrl NOTIFY resourcesUrlChanged) Q_PROPERTY(QString extends READ extends WRITE setExtends) Q_PROPERTY(bool allBackends READ allBackends WRITE setAllBackends) Q_PROPERTY(QVariantList subcategories READ subcategories NOTIFY subcategoriesChanged) Q_PROPERTY(bool isBusy READ isBusy NOTIFY busyChanged) public: explicit ResourcesProxyModel(QObject* parent = nullptr); enum Roles { NameRole = Qt::UserRole, IconRole, CommentRole, StateRole, RatingRole, RatingPointsRole, RatingCountRole, SortableRatingRole, InstalledRole, ApplicationRole, OriginRole, DisplayOriginRole, CanUpgrade, PackageNameRole, IsTechnicalRole, CategoryRole, CategoryDisplayRole, SectionRole, MimeTypes, SizeRole, LongDescriptionRole }; Q_ENUM(Roles) QHash roleNames() const override; void setSearch(const QString &text); QString lastSearch() const; void setOriginFilter(const QString &origin); QString originFilter() const; void setFiltersFromCategory(Category *category); void setStateFilter(AbstractResource::State s); AbstractResource::State stateFilter() const; void setSortRole(Roles sortRole); Roles sortRole() const { return m_sortRole; } void setSortOrder(Qt::SortOrder sortOrder); Qt::SortOrder sortOrder() const { return m_sortOrder; } Category* filteredCategory() const; QString mimeTypeFilter() const; void setMimeTypeFilter(const QString& mime); QString extends() const; void setExtends(const QString &extends); QUrl resourcesUrl() const; void setResourcesUrl(const QUrl& resourcesUrl); bool allBackends() const; void setAllBackends(bool allBackends); QVariantList subcategories() const; QVariant data(const QModelIndex & index, int role) const override; int rowCount(const QModelIndex & parent = {}) const override; Q_SCRIPTABLE int indexOf(AbstractResource* res); Q_SCRIPTABLE AbstractResource* resourceAt(int row) const; bool isBusy() const { return m_currentStream != nullptr; } bool lessThan(AbstractResource* rl, AbstractResource* rr) const; void invalidateFilter(); void invalidateSorting(); void classBegin() override {} void componentComplete() override; private Q_SLOTS: void refreshBackend(AbstractResourcesBackend* backend, const QVector& properties); void refreshResource(AbstractResource* resource, const QVector& properties); void removeResource(AbstractResource* resource); private: void sortedInsertion(const QVector &res); QVariant roleToValue(AbstractResource* res, int role) const; QVector propertiesToRoles(const QVector& properties) const; void addResources(const QVector &res); void fetchSubcategories(); + void removeDuplicates(QVector& newResources); Roles m_sortRole; Qt::SortOrder m_sortOrder; bool m_sortByRelevancy; bool m_setup = false; AbstractResourcesBackend::Filters m_filters; QVariantList m_subcategories; QVector m_displayedResources; const QHash m_roles; AggregatedResultsStream* m_currentStream; Q_SIGNALS: void busyChanged(bool isBusy); void sortRoleChanged(int sortRole); void sortOrderChanged(Qt::SortOrder order); void categoryChanged(); void stateFilterChanged(); void searchChanged(const QString &search); void subcategoriesChanged(const QVariantList &subcategories); void resourcesUrlChanged(const QUrl &url); }; #endif