Akonadi

searchrequest.cpp
1/*
2 SPDX-FileCopyrightText: 2013, 2014 Daniel Vrátil <dvratil@redhat.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "searchrequest.h"
8
9#include "abstractsearchplugin.h"
10#include "akonadi.h"
11#include "akonadiserver_search_debug.h"
12#include "connection.h"
13#include "searchmanager.h"
14#include "searchtaskmanager.h"
15
16using namespace Akonadi::Server;
17
18SearchRequest::SearchRequest(const QByteArray &connectionId, SearchManager &searchManager, SearchTaskManager &agentSearchManager)
19 : mConnectionId(connectionId)
20 , mSearchManager(searchManager)
21 , mAgentSearchManager(agentSearchManager)
22{
23}
24
25SearchRequest::~SearchRequest()
26{
27}
28
29QByteArray SearchRequest::connectionId() const
30{
31 return mConnectionId;
32}
33
34void SearchRequest::setQuery(const QString &query)
35{
36 mQuery = query;
37}
38
39QString SearchRequest::query() const
40{
41 return mQuery;
42}
43
44void SearchRequest::setCollections(const QList<qint64> &collectionsIds)
45{
46 mCollections = collectionsIds;
47}
48
49QList<qint64> SearchRequest::collections() const
50{
51 return mCollections;
52}
53
54void SearchRequest::setMimeTypes(const QStringList &mimeTypes)
55{
56 mMimeTypes = mimeTypes;
57}
58
59QStringList SearchRequest::mimeTypes() const
60{
61 return mMimeTypes;
62}
63
64void SearchRequest::setRemoteSearch(bool remote)
65{
66 mRemoteSearch = remote;
67}
68
69bool SearchRequest::remoteSearch() const
70{
71 return mRemoteSearch;
72}
73
74void SearchRequest::setStoreResults(bool storeResults)
75{
76 mStoreResults = storeResults;
77}
78
79QSet<qint64> SearchRequest::results() const
80{
81 return mResults;
82}
83
84void SearchRequest::emitResults(const QSet<qint64> &results)
85{
86 Q_EMIT resultsAvailable(results);
87 if (mStoreResults) {
88 mResults.unite(results);
89 }
90}
91
92void SearchRequest::searchPlugins()
93{
94 const QList<AbstractSearchPlugin *> plugins = mSearchManager.searchPlugins();
95 for (AbstractSearchPlugin *plugin : plugins) {
96 const QSet<qint64> result = plugin->search(mQuery, mCollections, mMimeTypes);
97 emitResults(result);
98 }
99}
100
101void SearchRequest::exec()
102{
103 qCInfo(AKONADISERVER_SEARCH_LOG) << "Executing search" << mConnectionId;
104
105 // TODO should we move this to the AgentSearchManager as well? If we keep it here the agents can be searched in parallel
106 // since the plugin search is executed in this thread directly.
107 searchPlugins();
108
109 // If remote search is disabled, just finish here after searching the plugins
110 if (!mRemoteSearch) {
111 qCInfo(AKONADISERVER_SEARCH_LOG) << "Search " << mConnectionId << "done (without remote search)";
112 return;
113 }
114
115 SearchTask task;
116 task.id = mConnectionId;
117 task.query = mQuery;
118 task.mimeTypes = mMimeTypes;
119 task.collections = mCollections;
120 task.complete = false;
121
122 mAgentSearchManager.addTask(&task);
123
124 task.sharedLock.lock();
125 for (;;) {
126 if (task.complete) {
127 break;
128 }
129
130 task.notifier.wait(&task.sharedLock);
131
132 qCDebug(AKONADISERVER_SEARCH_LOG) << task.pendingResults.count() << "search results available in search" << task.id;
133 if (!task.pendingResults.isEmpty()) {
134 emitResults(task.pendingResults);
135 }
136 task.pendingResults.clear();
137 }
138
139 if (!task.pendingResults.isEmpty()) {
140 emitResults(task.pendingResults);
141 }
142 task.sharedLock.unlock();
143
144 qCInfo(AKONADISERVER_SEARCH_LOG) << "Search" << mConnectionId << "done (with remote search)";
145}
146
147#include "moc_searchrequest.cpp"
3rd party applications can install a search plugin for Akonadi server to provide access to their sear...
SearchManager creates and deletes persistent searches for all currently active search engines.
virtual QList< AbstractSearchPlugin * > searchPlugins() const
Returns currently available search plugins.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
Q_EMITQ_EMIT
QSet< T > & unite(const QSet< T > &other)
QTaskBuilder< Task > task(Task &&task)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.