diff --git a/src/akonadi/akonadidatasourcequeries.cpp b/src/akonadi/akonadidatasourcequeries.cpp index a4466ec2..2cdf4eaa 100644 --- a/src/akonadi/akonadidatasourcequeries.cpp +++ b/src/akonadi/akonadidatasourcequeries.cpp @@ -1,136 +1,136 @@ /* This file is part of Zanshin Copyright 2014 Kevin Ottens 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "akonadidatasourcequeries.h" #include "akonadistoragesettings.h" using namespace Akonadi; DataSourceQueries::DataSourceQueries(StorageInterface::FetchContentTypes contentTypes, const StorageInterface::Ptr &storage, const SerializerInterface::Ptr &serializer, const MonitorInterface::Ptr &monitor) : m_contentTypes(contentTypes), m_serializer(serializer), m_helpers(new LiveQueryHelpers(serializer, storage)), m_integrator(new LiveQueryIntegrator(serializer, monitor)) { m_integrator->addRemoveHandler([this] (const Collection &collection) { m_findChildren.remove(collection.id()); m_findSearchChildren.remove(collection.id()); }); } bool DataSourceQueries::isDefaultSource(Domain::DataSource::Ptr source) const { auto sourceCollection = m_serializer->createCollectionFromDataSource(source); if (m_contentTypes == StorageInterface::Tasks) return sourceCollection == StorageSettings::instance().defaultTaskCollection(); else if (m_contentTypes == StorageInterface::Notes) return sourceCollection == StorageSettings::instance().defaultNoteCollection(); else return false; } void DataSourceQueries::changeDefaultSource(Domain::DataSource::Ptr source) { auto sourceCollection = m_serializer->createCollectionFromDataSource(source); if (m_contentTypes == StorageInterface::Tasks) { StorageSettings::instance().setDefaultTaskCollection(sourceCollection); } else if (m_contentTypes == StorageInterface::Notes) { StorageSettings::instance().setDefaultNoteCollection(sourceCollection); } } DataSourceQueries::DataSourceResult::Ptr DataSourceQueries::findTopLevel() const { auto fetch = m_helpers->fetchCollections(Collection::root(), m_contentTypes); auto predicate = createFetchPredicate(Collection::root()); m_integrator->bind(m_findTopLevel, fetch, predicate); return m_findTopLevel->result(); } DataSourceQueries::DataSourceResult::Ptr DataSourceQueries::findChildren(Domain::DataSource::Ptr source) const { Collection root = m_serializer->createCollectionFromDataSource(source); auto &query = m_findChildren[root.id()]; auto fetch = m_helpers->fetchCollections(root, m_contentTypes); auto predicate = createFetchPredicate(root); m_integrator->bind(query, fetch, predicate); return query->result(); } QString DataSourceQueries::searchTerm() const { return m_searchTerm; } void DataSourceQueries::setSearchTerm(const QString &term) { if (m_searchTerm == term) return; m_searchTerm = term; if (m_findSearchTopLevel) { m_findSearchTopLevel->reset(); } - foreach(auto query, m_findSearchChildren.values()) + foreach (auto query, m_findSearchChildren) query->reset(); } DataSourceQueries::DataSourceResult::Ptr DataSourceQueries::findSearchTopLevel() const { auto fetch = m_helpers->searchCollections(Collection::root(), &m_searchTerm, m_contentTypes); auto predicate = createSearchPredicate(Collection::root()); m_integrator->bind(m_findSearchTopLevel, fetch, predicate); return m_findSearchTopLevel->result(); } DataSourceQueries::DataSourceResult::Ptr DataSourceQueries::findSearchChildren(Domain::DataSource::Ptr source) const { Collection root = m_serializer->createCollectionFromDataSource(source); auto &query = m_findSearchChildren[root.id()]; auto fetch = m_helpers->searchCollections(root, &m_searchTerm, m_contentTypes); auto predicate = createSearchPredicate(root); m_integrator->bind(query, fetch, predicate); return query->result(); } DataSourceQueries::CollectionInputQuery::PredicateFunction DataSourceQueries::createFetchPredicate(const Collection &root) const { return [this, root] (const Collection &collection) { return collection.isValid() && collection.parentCollection() == root && m_serializer->isListedCollection(collection); }; } DataSourceQueries::CollectionInputQuery::PredicateFunction DataSourceQueries::createSearchPredicate(const Collection &root) const { return [root] (const Collection &collection) { return collection.isValid() && collection.parentCollection() == root; }; } diff --git a/src/akonadi/akonadilivequeryhelpers.cpp b/src/akonadi/akonadilivequeryhelpers.cpp index 8ebbf2f7..8856da6d 100644 --- a/src/akonadi/akonadilivequeryhelpers.cpp +++ b/src/akonadi/akonadilivequeryhelpers.cpp @@ -1,203 +1,203 @@ /* This file is part of Zanshin Copyright 2015 Kevin Ottens 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "akonadilivequeryhelpers.h" #include "akonadi/akonadicollectionfetchjobinterface.h" #include "akonadi/akonadicollectionsearchjobinterface.h" #include "akonadi/akonadiitemfetchjobinterface.h" #include "akonadi/akonaditagfetchjobinterface.h" #include "utils/jobhandler.h" using namespace Akonadi; LiveQueryHelpers::LiveQueryHelpers(const SerializerInterface::Ptr &serializer, const StorageInterface::Ptr &storage) : m_serializer(serializer), m_storage(storage) { } LiveQueryHelpers::CollectionFetchFunction LiveQueryHelpers::fetchAllCollections(StorageInterface::FetchContentTypes contentTypes) const { auto storage = m_storage; return [storage, contentTypes] (const Domain::LiveQueryInput::AddFunction &add) { auto job = storage->fetchCollections(Collection::root(), StorageInterface::Recursive, contentTypes); Utils::JobHandler::install(job->kjob(), [job, add] { if (job->kjob()->error()) return; foreach (const auto &collection, job->collections()) add(collection); }); }; } LiveQueryHelpers::CollectionFetchFunction LiveQueryHelpers::fetchCollections(const Collection &root, StorageInterface::FetchContentTypes contentTypes) const { auto storage = m_storage; return [storage, contentTypes, root] (const Domain::LiveQueryInput::AddFunction &add) { auto job = storage->fetchCollections(root, StorageInterface::Recursive, contentTypes); Utils::JobHandler::install(job->kjob(), [root, job, add] { if (job->kjob()->error()) return; auto directChildren = QHash(); foreach (const auto &collection, job->collections()) { auto directChild = collection; while (directChild.parentCollection() != root) directChild = directChild.parentCollection(); if (!directChildren.contains(directChild.id())) directChildren[directChild.id()] = directChild; } - foreach (const auto &directChild, directChildren.values()) + foreach (const auto &directChild, directChildren) add(directChild); }); }; } LiveQueryHelpers::CollectionFetchFunction LiveQueryHelpers::searchCollections(const Collection &root, const QString *searchTerm, StorageInterface::FetchContentTypes contentTypes) const { auto storage = m_storage; return [storage, contentTypes, searchTerm, root] (const Domain::LiveQueryInput::AddFunction &add) { if (searchTerm->isEmpty()) return; auto job = storage->searchCollections(*searchTerm, contentTypes); Utils::JobHandler::install(job->kjob(), [root, job, add] { if (job->kjob()->error()) return; auto directChildren = QHash(); foreach (const auto &collection, job->collections()) { auto directChild = collection; while (directChild.parentCollection() != root && directChild.parentCollection().isValid()) directChild = directChild.parentCollection(); if (directChild.parentCollection() != root) continue; if (!directChildren.contains(directChild.id())) directChildren[directChild.id()] = directChild; } - foreach (const auto &directChild, directChildren.values()) + foreach (const auto &directChild, directChildren) add(directChild); }); }; } LiveQueryHelpers::ItemFetchFunction LiveQueryHelpers::fetchItems(StorageInterface::FetchContentTypes contentTypes) const { auto serializer = m_serializer; auto storage = m_storage; return [serializer, storage, contentTypes] (const Domain::LiveQueryInput::AddFunction &add) { auto job = storage->fetchCollections(Akonadi::Collection::root(), StorageInterface::Recursive, contentTypes); Utils::JobHandler::install(job->kjob(), [serializer, storage, job, add] { if (job->kjob()->error() != KJob::NoError) return; foreach (const auto &collection, job->collections()) { if (!serializer->isSelectedCollection(collection)) continue; auto job = storage->fetchItems(collection); Utils::JobHandler::install(job->kjob(), [job, add] { if (job->kjob()->error() != KJob::NoError) return; foreach (const auto &item, job->items()) add(item); }); } }); }; } LiveQueryHelpers::ItemFetchFunction LiveQueryHelpers::fetchItems(const Tag &tag) const { // TODO: Qt5, use the proper implementation once we got a working akonadi #if 0 auto storage = m_storage; return [storage, tag] (const Domain::LiveQueryInput::AddFunction &add) { auto job = storage->fetchTagItems(tag); Utils::JobHandler::install(job->kjob(), [job, add] { if (job->kjob()->error() != KJob::NoError) return; foreach (const auto &item, job->items()) add(item); }); }; #else auto fetchFunction = fetchItems(StorageInterface::Tasks | StorageInterface::Notes); return [tag, fetchFunction] (const Domain::LiveQueryInput::AddFunction &add) { auto filterAdd = [tag, add] (const Item &item) { if (item.tags().contains(tag)) add(item); }; fetchFunction(filterAdd); }; #endif } LiveQueryHelpers::ItemFetchFunction LiveQueryHelpers::fetchSiblings(const Item &item) const { auto storage = m_storage; return [storage, item] (const Domain::LiveQueryInput::AddFunction &add) { auto job = storage->fetchItem(item); Utils::JobHandler::install(job->kjob(), [storage, job, add] { if (job->kjob()->error() != KJob::NoError) return; Q_ASSERT(job->items().size() == 1); auto item = job->items()[0]; Q_ASSERT(item.parentCollection().isValid()); auto job = storage->fetchItems(item.parentCollection()); Utils::JobHandler::install(job->kjob(), [job, add] { if (job->kjob()->error() != KJob::NoError) return; foreach (const auto &item, job->items()) add(item); }); }); }; } LiveQueryHelpers::TagFetchFunction LiveQueryHelpers::fetchTags() const { auto storage = m_storage; return [storage] (const Domain::LiveQueryInput::AddFunction &add) { auto job = storage->fetchTags(); Utils::JobHandler::install(job->kjob(), [job, add] { foreach (const auto &tag, job->tags()) add(tag); }); }; }