Akonadi

searchresulthandler.cpp
1 /*
2  * Copyright (C) 2013 Daniel Vrátil <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "searchresulthandler.h"
21 
22 #include "connection.h"
23 #include "akonadi.h"
24 #include "storage/querybuilder.h"
25 #include "storage/itemqueryhelper.h"
26 #include "search/searchtaskmanager.h"
27 
28 #include <private/imapset_p.h>
29 #include <private/scope_p.h>
30 
31 using namespace Akonadi;
32 using namespace Akonadi::Server;
33 
34 SearchResultHandler::SearchResultHandler(AkonadiServer &akonadi)
35  : Handler(akonadi)
36 {}
37 
38 bool SearchResultHandler::parseStream()
39 {
40  const auto &cmd = Protocol::cmdCast<Protocol::SearchResultCommand>(m_command);
41 
42  if (!checkScopeConstraints(cmd.result(), Scope::Uid | Scope::Rid)) {
43  return fail(cmd.searchId(), QStringLiteral("Only UID or RID scopes are allowed in SEARECH_RESULT"));
44  }
45 
46  QSet<qint64> ids;
47  if (cmd.result().scope() == Scope::Rid && !cmd.result().isEmpty()) {
48  QueryBuilder qb(PimItem::tableName());
49  qb.addColumn(PimItem::idFullColumnName());
50  ItemQueryHelper::remoteIdToQuery(cmd.result().ridSet(), connection()->context(), qb);
51  qb.addValueCondition(PimItem::collectionIdFullColumnName(), Query::Equals, cmd.collectionId());
52 
53  if (!qb.exec()) {
54  return fail(cmd.searchId(), QStringLiteral("Failed to convert RID to UID"));
55  }
56 
57  QSqlQuery query = qb.query();
58  while (query.next()) {
59  ids << query.value(0).toLongLong();
60  }
61  query.finish();
62  } else if (cmd.result().scope() == Scope::Uid && !cmd.result().isEmpty()) {
63  const ImapSet result = cmd.result().uidSet();
64  const ImapInterval::List lstInterval = result.intervals();
65  for (const ImapInterval &interval : lstInterval) {
66  for (qint64 i = interval.begin(), end = interval.end(); i <= end; ++i) {
67  ids.insert(i);
68  }
69  }
70  }
71  akonadi().agentSearchManager().pushResults(cmd.searchId(), ids, connection());
72 
73  return successResponse<Protocol::SearchResultResponse>();
74 }
75 
76 bool SearchResultHandler::fail(const QByteArray &searchId, const QString &error)
77 {
78  akonadi().agentSearchManager().pushResults(searchId, QSet<qint64>(), connection());
79  return failureResponse(error);
80 }
qlonglong toLongLong(bool *ok) const const
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.
QSqlQuery & query()
Returns the query, only valid after exec().
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:48
void addColumn(const QString &col)
Adds the given column to a select query.
QVariant value(int index) const const
Helper integration between Akonadi and Qt.
Helper class to construct arbitrary SQL queries.
Definition: querybuilder.h:45
bool exec()
Executes the query, returns true on success.
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.