Akonadi

searchcreatehandler.cpp
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2006 Tobias Koenig <[email protected]> *
3  * *
4  * SPDX-License-Identifier: LGPL-2.0-or-later *
5  ***************************************************************************/
6 
7 #include "searchcreatehandler.h"
8 
9 
10 #include "akonadi.h"
11 #include "connection.h"
12 #include "handlerhelper.h"
13 #include "storage/datastore.h"
14 #include "storage/entity.h"
15 #include "storage/transaction.h"
16 #include "search/searchmanager.h"
17 #include <shared/akranges.h>
18 #include "akonadiserver_debug.h"
19 
20 using namespace Akonadi;
21 using namespace Akonadi::Server;
22 using namespace AkRanges;
23 
24 SearchCreateHandler::SearchCreateHandler(AkonadiServer &akonadi)
25  : Handler(akonadi)
26 {}
27 
29 {
30  const auto &cmd = Protocol::cmdCast<Protocol::StoreSearchCommand>(m_command);
31 
32  if (cmd.name().isEmpty()) {
33  return failureResponse("No name specified");
34  }
35 
36  if (cmd.query().isEmpty()) {
37  return failureResponse("No query specified");
38  }
39 
40  DataStore *db = connection()->storageBackend();
41  Transaction transaction(db, QStringLiteral("SEARCH PERSISTENT"));
42 
43  QStringList queryAttributes;
44 
45  if (cmd.remote()) {
46  queryAttributes << QStringLiteral(AKONADI_PARAM_REMOTE);
47  }
48  if (cmd.recursive()) {
49  queryAttributes << QStringLiteral(AKONADI_PARAM_RECURSIVE);
50  }
51 
52  QVector<qint64> queryColIds = cmd.queryCollections();
53  std::sort(queryColIds.begin(), queryColIds.end());
54  const auto queryCollections = queryColIds | Views::transform([](const auto id) { return QString::number(id); }) | Actions::toQList;
55 
56  Collection col;
57  col.setQueryString(cmd.query());
58  col.setQueryAttributes(queryAttributes.join(QLatin1Char(' ')));
59  col.setQueryCollections(queryCollections.join(QLatin1Char(' ')));
60  col.setParentId(1); // search root
61  col.setResourceId(1); // search resource
62  col.setName(cmd.name());
63  col.setIsVirtual(true);
64 
65  const QStringList lstMimeTypes = cmd.mimeTypes();
66  if (!db->appendCollection(col, lstMimeTypes, {{"AccessRights", "luD"}})) {
67  return failureResponse("Unable to create persistent search");
68  }
69 
70  if (!transaction.commit()) {
71  return failureResponse("Unable to commit transaction");
72  }
73 
74  akonadi().searchManager().updateSearch(col);
75 
76  sendResponse(HandlerHelper::fetchCollectionsResponse(akonadi(), col));
77  return successResponse<Protocol::StoreSearchResponse>();
78 }
QVector::iterator begin()
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:35
Represents a collection of PIM items.
Definition: collection.h:63
QString join(const QString &separator) const const
void setName(const QString &name)
Sets the i18n&#39;ed name of the collection.
Definition: collection.cpp:224
QString number(int n, int base)
Helper class for DataStore transaction handling.
Definition: transaction.h:24
static Protocol::FetchCollectionsResponse fetchCollectionsResponse(AkonadiServer &akonadi, const Collection &col)
Returns the protocol representation of the given collection.
Helper integration between Akonadi and Qt.
This class handles all the database access.
Definition: datastore.h:95
QVector::iterator end()
bool parseStream() override
Parse and handle the IMAP message using the streaming parser.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 23:16:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.