File indexing completed on 2025-01-05 04:59:40

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004  */
0005 
0006 
0007 #include "akonadidatasourcequeries.h"
0008 
0009 #include "akonadistoragesettings.h"
0010 
0011 using namespace Akonadi;
0012 
0013 DataSourceQueries::DataSourceQueries(const StorageInterface::Ptr &storage,
0014                                      const SerializerInterface::Ptr &serializer,
0015                                      const MonitorInterface::Ptr &monitor)
0016     : m_serializer(serializer),
0017       m_helpers(new LiveQueryHelpers(serializer, storage)),
0018       m_integrator(new LiveQueryIntegrator(serializer, monitor))
0019 {
0020     m_integrator->addRemoveHandler([this] (const Collection &collection) {
0021         m_findChildren.remove(collection.id());
0022     });
0023 }
0024 
0025 bool DataSourceQueries::isDefaultSource(Domain::DataSource::Ptr source) const
0026 {
0027     auto sourceCollection = m_serializer->createCollectionFromDataSource(source);
0028     return sourceCollection == StorageSettings::instance().defaultCollection();
0029 }
0030 
0031 void DataSourceQueries::changeDefaultSource(Domain::DataSource::Ptr source)
0032 {
0033     auto sourceCollection = m_serializer->createCollectionFromDataSource(source);
0034     StorageSettings::instance().setDefaultCollection(sourceCollection);
0035 }
0036 
0037 DataSourceQueries::DataSourceResult::Ptr DataSourceQueries::findTopLevel() const
0038 {
0039     auto fetch = m_helpers->fetchCollections(Collection::root(), const_cast<DataSourceQueries*>(this));
0040     auto predicate = createFetchPredicate(Collection::root());
0041     m_integrator->bind("DataSourceQueries::findTopLevel", m_findTopLevel, fetch, predicate);
0042     return m_findTopLevel->result();
0043 }
0044 
0045 DataSourceQueries::DataSourceResult::Ptr DataSourceQueries::findChildren(Domain::DataSource::Ptr source) const
0046 {
0047     Collection root = m_serializer->createCollectionFromDataSource(source);
0048     auto &query = m_findChildren[root.id()];
0049     auto fetch = m_helpers->fetchCollections(root, const_cast<DataSourceQueries*>(this));
0050     auto predicate = createFetchPredicate(root);
0051     m_integrator->bind("DataSourceQueries::findChildren", query, fetch, predicate);
0052     return query->result();
0053 }
0054 
0055 DataSourceQueries::DataSourceResult::Ptr DataSourceQueries::findAllSelected() const
0056 {
0057     auto fetch = m_helpers->fetchAllCollections(const_cast<DataSourceQueries*>(this));
0058     auto predicate = [this] (const Akonadi::Collection &collection) {
0059         return collection.isValid() && m_serializer->isSelectedCollection(collection);
0060     };
0061     m_integrator->bind("DataSourceQueries::findAllSelected", m_findAllSelected, fetch, predicate,
0062                        Akonadi::SerializerInterface::FullPath);
0063     return m_findAllSelected->result();
0064 }
0065 
0066 DataSourceQueries::ProjectResult::Ptr DataSourceQueries::findProjects(Domain::DataSource::Ptr source) const
0067 {
0068     Collection root = m_serializer->createCollectionFromDataSource(source);
0069     auto &query = m_findProjects[root.id()];
0070     auto fetch = m_helpers->fetchItems(root, const_cast<DataSourceQueries*>(this));
0071     auto predicate = [this, root] (const Akonadi::Item &item) {
0072         return root == item.parentCollection()
0073             && m_serializer->isProjectItem(item);
0074     };
0075     m_integrator->bind("DataSourceQueries::findProjects", query, fetch, predicate);
0076     return query->result();
0077 }
0078 
0079 DataSourceQueries::CollectionInputQuery::PredicateFunction DataSourceQueries::createFetchPredicate(const Collection &root) const
0080 {
0081     return [root] (const Collection &collection) {
0082         return collection.isValid()
0083             && collection.parentCollection() == root;
0084     };
0085 }
0086 
0087 #include "moc_akonadidatasourcequeries.cpp"