Akonadi

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

KDE's Doxygen guidelines are available online.