1 /*
2     SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
3     SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #pragma once
9 
10 #include "akthread.h"
11 
12 #include <QMutex>
13 #include <QSet>
14 #include <QVector>
15 
16 class QTimer;
17 class QPluginLoader;
18 
19 namespace Akonadi
20 {
21 class AbstractSearchPlugin;
22 
23 namespace Server
24 {
25 class AbstractSearchEngine;
26 class Collection;
27 class SearchTaskManager;
28 
29 /**
30  * SearchManager creates and deletes persistent searches for all currently
31  * active search engines.
32  */
33 class SearchManager : public AkThread
34 {
35     Q_OBJECT
36     Q_CLASSINFO("D-Bus Interface", "org.freedesktop.Akonadi.SearchManager")
37 
38 public:
39     /** Create a new search manager with the given @p searchEngines. */
40     explicit SearchManager(const QStringList &searchEngines, SearchTaskManager &agentSearchManager);
41 
42     ~SearchManager() override;
43 
44     /**
45      * Updates the search query asynchronously. Returns immediately
46      */
47     virtual void updateSearchAsync(const Collection &collection);
48 
49     /**
50      * Updates the search query synchronously.
51      */
52     virtual void updateSearch(const Collection &collection);
53 
54     /**
55      * Returns currently available search plugins.
56      */
57     virtual QVector<AbstractSearchPlugin *> searchPlugins() const;
58 
59 public Q_SLOTS:
60     virtual void scheduleSearchUpdate();
61 
62     /**
63      * This is called via D-Bus from AgentManager to register an agent with
64      * search interface.
65      */
66     virtual void registerInstance(const QString &id);
67 
68     /**
69      * This is called via D-Bus from AgentManager to unregister an agent with
70      * search interface.
71      */
72     virtual void unregisterInstance(const QString &id);
73 
74 private Q_SLOTS:
75     void searchUpdateTimeout();
76     void searchUpdateResultsAvailable(const QSet<qint64> &results);
77 
78     /**
79      * Actual implementation of search updates.
80      *
81      * This method has to be called using QMetaObject::invokeMethod.
82      */
83     void updateSearchImpl(const Akonadi::Server::Collection &collection);
84 
85 private:
86     void init() override;
87     void quit() override;
88 
89     // Called from main thread
90     void loadSearchPlugins();
91     // Called from manager thread
92     void initSearchPlugins();
93 
94     SearchTaskManager &mAgentSearchManager;
95     QStringList mEngineNames;
96     QVector<QPluginLoader *> mPluginLoaders;
97     QVector<AbstractSearchEngine *> mEngines;
98     QVector<AbstractSearchPlugin *> mPlugins;
99 
100     QTimer *mSearchUpdateTimer = nullptr;
101 
102     QMutex mLock;
103     QSet<qint64> mUpdatingCollections;
104 };
105 
106 } // namespace Server
107 } // namespace Akonadi
108 
109