Akonadi

searchhandler.cpp
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2009 Tobias Koenig <[email protected]> *
3  * *
4  * SPDX-License-Identifier: LGPL-2.0-or-later *
5  ***************************************************************************/
6 
7 #include "searchhandler.h"
8 
9 #include "akonadi.h"
10 #include "akonadiserver_search_debug.h"
11 #include "connection.h"
12 #include "handlerhelper.h"
13 #include "itemfetchhelper.h"
14 #include "search/agentsearchengine.h"
15 #include "search/searchmanager.h"
16 #include "search/searchrequest.h"
17 #include "searchhelper.h"
18 
19 using namespace Akonadi;
20 using namespace Akonadi::Server;
21 
22 SearchHandler::SearchHandler(AkonadiServer &akonadi)
23  : Handler(akonadi)
24 {
25 }
26 
27 bool SearchHandler::parseStream()
28 {
29  const auto &cmd = Protocol::cmdCast<Protocol::SearchCommand>(m_command);
30 
31  if (cmd.query().isEmpty()) {
32  return failureResponse("No query specified");
33  }
34 
35  QVector<qint64> collectionIds;
36  bool recursive = cmd.recursive();
37 
38  if (cmd.collections().isEmpty() || cmd.collections() == QVector<qint64>{0LL}) {
39  collectionIds << 0;
40  recursive = true;
41  }
42 
43  QVector<qint64> collections = collectionIds;
44  if (recursive) {
45  collections += SearchHelper::matchSubcollectionsByMimeType(collectionIds, cmd.mimeTypes());
46  }
47 
48  qCDebug(AKONADISERVER_SEARCH_LOG) << "SEARCH:";
49  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tQuery:" << cmd.query();
50  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tMimeTypes:" << cmd.mimeTypes();
51  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tCollections:" << collections;
52  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tRemote:" << cmd.remote();
53  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tRecursive" << recursive;
54 
55  if (collections.isEmpty()) {
56  return successResponse<Protocol::SearchResponse>();
57  }
58 
59  mItemFetchScope = cmd.itemFetchScope();
60  mTagFetchScope = cmd.tagFetchScope();
61 
62  SearchRequest request(connection()->sessionId(), akonadi().searchManager(), akonadi().agentSearchManager());
63  request.setCollections(collections);
64  request.setMimeTypes(cmd.mimeTypes());
65  request.setQuery(cmd.query());
66  request.setRemoteSearch(cmd.remote());
67  QObject::connect(&request, &SearchRequest::resultsAvailable, [this](const QSet<qint64> &results) {
68  processResults(results);
69  });
70  request.exec();
71 
72  // qCDebug(AKONADISERVER_SEARCH_LOG) << "\tResult:" << uids;
73  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tResult:" << mAllResults.count() << "matches";
74 
75  return successResponse<Protocol::SearchResponse>();
76 }
77 
78 void SearchHandler::processResults(const QSet<qint64> &results)
79 {
80  QSet<qint64> newResults = results;
81  newResults.subtract(mAllResults);
82  mAllResults.unite(newResults);
83 
84  if (newResults.isEmpty()) {
85  return;
86  }
87 
88  ImapSet imapSet;
89  imapSet.add(newResults);
90 
91  Scope scope;
92  scope.setUidSet(imapSet);
93 
94  ItemFetchHelper fetchHelper(connection(), scope, mItemFetchScope, mTagFetchScope, akonadi());
95  fetchHelper.fetchItems();
96 }
bool isEmpty() const const
QSet< T > & subtract(const QSet< T > &other)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:39
QSet< T > & unite(const QSet< T > &other)
bool isEmpty() const const
Helper integration between Akonadi and Qt.
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.