Akonadi

searchhandler.cpp
1 /***************************************************************************
2  * Copyright (C) 2009 by Tobias Koenig <[email protected]> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU Library General Public License as *
6  * published by the Free Software Foundation; either version 2 of the *
7  * License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Library General Public *
15  * License along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  ***************************************************************************/
19 
20 #include "searchhandler.h"
21 
22 #include "akonadi.h"
23 #include "connection.h"
24 #include "itemfetchhelper.h"
25 #include "handlerhelper.h"
26 #include "search/agentsearchengine.h"
27 #include "searchhelper.h"
28 #include "search/searchrequest.h"
29 #include "search/searchmanager.h"
30 #include "akonadiserver_search_debug.h"
31 
32 using namespace Akonadi;
33 using namespace Akonadi::Server;
34 
35 SearchHandler::SearchHandler(AkonadiServer &akonadi)
36  : Handler(akonadi)
37 {}
38 
39 bool SearchHandler::parseStream()
40 {
41  const auto &cmd = Protocol::cmdCast<Protocol::SearchCommand>(m_command);
42 
43  if (cmd.query().isEmpty()) {
44  return failureResponse("No query specified");
45  }
46 
47  QVector<qint64> collectionIds;
48  bool recursive = cmd.recursive();
49 
50  if (cmd.collections().isEmpty() || cmd.collections() == QVector<qint64> { 0ll }) {
51  collectionIds << 0;
52  recursive = true;
53  }
54 
55  QVector<qint64> collections = collectionIds;
56  if (recursive) {
57  collections += SearchHelper::matchSubcollectionsByMimeType(collectionIds, cmd.mimeTypes());
58  }
59 
60  qCDebug(AKONADISERVER_SEARCH_LOG) << "SEARCH:";
61  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tQuery:" << cmd.query();
62  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tMimeTypes:" << cmd.mimeTypes();
63  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tCollections:" << collections;
64  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tRemote:" << cmd.remote();
65  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tRecursive" << recursive;
66 
67  if (collections.isEmpty()) {
68  return successResponse<Protocol::SearchResponse>();
69  }
70 
71  mItemFetchScope = cmd.itemFetchScope();
72  mTagFetchScope = cmd.tagFetchScope();
73 
74  SearchRequest request(connection()->sessionId(), akonadi().searchManager(), akonadi().agentSearchManager());
75  request.setCollections(collections);
76  request.setMimeTypes(cmd.mimeTypes());
77  request.setQuery(cmd.query());
78  request.setRemoteSearch(cmd.remote());
79  QObject::connect(&request, &SearchRequest::resultsAvailable,
80  [this](const QSet<qint64> &results) {
81  processResults(results);
82  });
83  request.exec();
84 
85  //qCDebug(AKONADISERVER_SEARCH_LOG) << "\tResult:" << uids;
86  qCDebug(AKONADISERVER_SEARCH_LOG) << "\tResult:" << mAllResults.count() << "matches";
87 
88  return successResponse<Protocol::SearchResponse>();
89 }
90 
91 void SearchHandler::processResults(const QSet<qint64> &results)
92 {
93  QSet<qint64> newResults = results;
94  newResults.subtract(mAllResults);
95  mAllResults.unite(newResults);
96 
97  if (newResults.isEmpty()) {
98  return;
99  }
100 
101  ImapSet imapSet;
102  imapSet.add(newResults);
103 
104  Scope scope;
105  scope.setUidSet(imapSet);
106 
107  ItemFetchHelper fetchHelper(connection(), scope, mItemFetchScope, mTagFetchScope, akonadi());
108  fetchHelper.fetchItems();
109 }
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:48
QSet< T > & subtract(const QSet< T > &other)
QSet< T > & unite(const QSet< T > &other)
bool isEmpty() const const
Helper integration between Akonadi and Qt.
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.