File indexing completed on 2024-11-10 04:40:53

0001 /*
0002     SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "akthread.h"
0011 
0012 #include <QList>
0013 #include <QMutex>
0014 #include <QSet>
0015 
0016 class QTimer;
0017 class QPluginLoader;
0018 
0019 namespace Akonadi
0020 {
0021 class AbstractSearchPlugin;
0022 
0023 namespace Server
0024 {
0025 class AbstractSearchEngine;
0026 class Collection;
0027 class SearchTaskManager;
0028 
0029 /**
0030  * SearchManager creates and deletes persistent searches for all currently
0031  * active search engines.
0032  */
0033 class SearchManager : public AkThread
0034 {
0035     Q_OBJECT
0036     Q_CLASSINFO("D-Bus Interface", "org.freedesktop.Akonadi.SearchManager")
0037 
0038 protected:
0039     /**
0040      * Create a new search manager with the given @p searchEngines.
0041      *
0042      * Use AkThread::create() to create and start a new SearchManager thread.
0043      **/
0044     explicit SearchManager(const QStringList &searchEngines, SearchTaskManager &agentSearchManager);
0045 
0046 public:
0047     ~SearchManager() override;
0048 
0049     /**
0050      * Updates the search query asynchronously. Returns immediately
0051      */
0052     virtual void updateSearchAsync(const Collection &collection);
0053 
0054     /**
0055      * Updates the search query synchronously.
0056      */
0057     virtual void updateSearch(const Collection &collection);
0058 
0059     /**
0060      * Returns currently available search plugins.
0061      */
0062     virtual QList<AbstractSearchPlugin *> searchPlugins() const;
0063 
0064 public Q_SLOTS:
0065     virtual void scheduleSearchUpdate();
0066 
0067     /**
0068      * This is called via D-Bus from AgentManager to register an agent with
0069      * search interface.
0070      */
0071     virtual void registerInstance(const QString &id);
0072 
0073     /**
0074      * This is called via D-Bus from AgentManager to unregister an agent with
0075      * search interface.
0076      */
0077     virtual void unregisterInstance(const QString &id);
0078 
0079 private Q_SLOTS:
0080     void searchUpdateTimeout();
0081     void searchUpdateResultsAvailable(const QSet<qint64> &results);
0082 
0083     /**
0084      * Actual implementation of search updates.
0085      *
0086      * This method has to be called using QMetaObject::invokeMethod.
0087      */
0088     void updateSearchImpl(const Akonadi::Server::Collection &collection);
0089 
0090 private:
0091     void init() override;
0092     void quit() override;
0093 
0094     // Called from main thread
0095     void loadSearchPlugins();
0096     // Called from manager thread
0097     void initSearchPlugins();
0098 
0099     SearchTaskManager &mAgentSearchManager;
0100     QStringList mEngineNames;
0101     QList<QPluginLoader *> mPluginLoaders;
0102     QList<AbstractSearchEngine *> mEngines;
0103     QList<AbstractSearchPlugin *> mPlugins;
0104 
0105     QTimer *mSearchUpdateTimer = nullptr;
0106 
0107     QMutex mLock;
0108     QSet<qint64> mUpdatingCollections;
0109 };
0110 
0111 } // namespace Server
0112 } // namespace Akonadi