Akonadi

searchresulthandler.cpp
1 /*
2  * SPDX-FileCopyrightText: 2013 Daniel Vr├ítil <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "searchresulthandler.h"
9 
10 #include "akonadi.h"
11 #include "connection.h"
12 #include "search/searchtaskmanager.h"
13 #include "storage/itemqueryhelper.h"
14 #include "storage/querybuilder.h"
15 
16 #include <private/imapset_p.h>
17 #include <private/scope_p.h>
18 
19 using namespace Akonadi;
20 using namespace Akonadi::Server;
21 
22 SearchResultHandler::SearchResultHandler(AkonadiServer &akonadi)
23  : Handler(akonadi)
24 {
25 }
26 
27 bool SearchResultHandler::parseStream()
28 {
29  const auto &cmd = Protocol::cmdCast<Protocol::SearchResultCommand>(m_command);
30 
31  if (!checkScopeConstraints(cmd.result(), Scope::Uid | Scope::Rid)) {
32  return fail(cmd.searchId(), QStringLiteral("Only UID or RID scopes are allowed in SEARECH_RESULT"));
33  }
34 
35  QSet<qint64> ids;
36  if (cmd.result().scope() == Scope::Rid && !cmd.result().isEmpty()) {
37  QueryBuilder qb(PimItem::tableName());
38  qb.addColumn(PimItem::idFullColumnName());
39  ItemQueryHelper::remoteIdToQuery(cmd.result().ridSet(), connection()->context(), qb);
40  qb.addValueCondition(PimItem::collectionIdFullColumnName(), Query::Equals, cmd.collectionId());
41 
42  if (!qb.exec()) {
43  return fail(cmd.searchId(), QStringLiteral("Failed to convert RID to UID"));
44  }
45 
46  QSqlQuery query = qb.query();
47  while (query.next()) {
48  ids << query.value(0).toLongLong();
49  }
50  query.finish();
51  } else if (cmd.result().scope() == Scope::Uid && !cmd.result().isEmpty()) {
52  const ImapSet result = cmd.result().uidSet();
53  const ImapInterval::List lstInterval = result.intervals();
54  for (const ImapInterval &interval : lstInterval) {
55  for (qint64 i = interval.begin(), end = interval.end(); i <= end; ++i) {
56  ids.insert(i);
57  }
58  }
59  }
60  akonadi().agentSearchManager().pushResults(cmd.searchId(), ids, connection());
61 
62  return successResponse<Protocol::SearchResultResponse>();
63 }
64 
65 bool SearchResultHandler::fail(const QByteArray &searchId, const QString &error)
66 {
67  akonadi().agentSearchManager().pushResults(searchId, QSet<qint64>(), connection());
68  return failureResponse(error);
69 }
void addColumn(const QString &col)
Adds the given column to a select query.
void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, ConditionType type=WhereCondition)
Add a WHERE or HAVING condition which compares a column with a given value.
bool exec()
Executes the query, returns true on success.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:39
QSet::iterator insert(const T &value)
QSqlQuery & query()
Returns the query, only valid after exec().
T value(int i) const const
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:31
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.