Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 0230adc5 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Search in all app backends in parallel rather than just the preferred

Changes the search strategy into one that will fetch all backends in
parallel. When a same resource is reported twice, the one coming from the
backend selected as preferable for applications will be used.
Duplication matching is done using the appstream id, so it's very important
that application backends support it.
parent 14ca7161
......@@ -329,12 +329,8 @@ AggregatedResultsStream * ResourcesModel::findResourceByPackageName(const QUrl&
AggregatedResultsStream* ResourcesModel::search(const AbstractResourcesBackend::Filters& search)
{
QSet<ResultsStream*> 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);
}
......
......@@ -116,6 +116,58 @@ void ResourcesProxyModel::setSearch(const QString &_searchText)
}
}
void ResourcesProxyModel::removeDuplicates(QVector<AbstractResource *>& resources)
{
const auto cab = ResourcesModel::global()->currentApplicationBackend();
QHash<QString, QVector<AbstractResource *>::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<QString, QVector<AbstractResource *>::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<AbstractResource *>& _res)
{
auto res = _res;
......@@ -124,11 +176,15 @@ void ResourcesProxyModel::addResources(const QVector<AbstractResource *>& _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()
......@@ -391,8 +447,8 @@ void ResourcesProxyModel::sortedInsertion(const QVector<AbstractResource*> & res
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());
......
......@@ -135,6 +135,7 @@ private:
QVector<int> propertiesToRoles(const QVector<QByteArray>& properties) const;
void addResources(const QVector<AbstractResource*> &res);
void fetchSubcategories();
void removeDuplicates(QVector<AbstractResource *>& newResources);
Roles m_sortRole;
Qt::SortOrder m_sortOrder;
......
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