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 #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 
16 using namespace Akonadi::Server;
17 
18 SearchRequest::SearchRequest(const QByteArray &connectionId, SearchManager &searchManager, SearchTaskManager &agentSearchManager)
19  : mConnectionId(connectionId)
20  , mSearchManager(searchManager)
21  , mAgentSearchManager(agentSearchManager)
22 {
23 }
24 
25 SearchRequest::~SearchRequest()
26 {
27 }
28 
29 QByteArray SearchRequest::connectionId() const
30 {
31  return mConnectionId;
32 }
33 
34 void SearchRequest::setQuery(const QString &query)
35 {
36  mQuery = query;
37 }
38 
39 QString SearchRequest::query() const
40 {
41  return mQuery;
42 }
43 
44 void SearchRequest::setCollections(const QVector<qint64> &collectionsIds)
45 {
46  mCollections = collectionsIds;
47 }
48 
49 QVector<qint64> SearchRequest::collections() const
50 {
51  return mCollections;
52 }
53 
54 void SearchRequest::setMimeTypes(const QStringList &mimeTypes)
55 {
56  mMimeTypes = mimeTypes;
57 }
58 
59 QStringList SearchRequest::mimeTypes() const
60 {
61  return mMimeTypes;
62 }
63 
64 void SearchRequest::setRemoteSearch(bool remote)
65 {
66  mRemoteSearch = remote;
67 }
68 
69 bool SearchRequest::remoteSearch() const
70 {
71  return mRemoteSearch;
72 }
73 
74 void SearchRequest::setStoreResults(bool storeResults)
75 {
76  mStoreResults = storeResults;
77 }
78 
79 QSet<qint64> SearchRequest::results() const
80 {
81  return mResults;
82 }
83 
84 void SearchRequest::emitResults(const QSet<qint64> &results)
85 {
86  Q_EMIT resultsAvailable(results);
87  if (mStoreResults) {
88  mResults.unite(results);
89  }
90 }
91 
92 void SearchRequest::searchPlugins()
93 {
94  const QVector<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 
101 void 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 }
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
SearchManager creates and deletes persistent searches for all currently active search engines.
Definition: searchmanager.h:33
QStringList mimeTypes(Mode mode=Writing)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.