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 #include "akonadi.h"
10 #include "akonadiserver_debug.h"
11 #include "connection.h"
12 #include "handlerhelper.h"
13 #include "search/searchmanager.h"
14 #include "storage/datastore.h"
15 #include "storage/entity.h"
16 #include "storage/transaction.h"
17 #include <shared/akranges.h>
18 
19 using namespace Akonadi;
20 using namespace Akonadi::Server;
21 using namespace AkRanges;
22 
23 SearchCreateHandler::SearchCreateHandler(AkonadiServer &akonadi)
24  : Handler(akonadi)
25 {
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) {
55  return QString::number(id);
56  })
57  | Actions::toQList;
58 
59  Collection col;
60  col.setQueryString(cmd.query());
61  col.setQueryAttributes(queryAttributes.join(QLatin1Char(' ')));
62  col.setQueryCollections(queryCollections.join(QLatin1Char(' ')));
63  col.setParentId(1); // search root
64  col.setResourceId(1); // search resource
65  col.setName(cmd.name());
66  col.setIsVirtual(true);
67 
68  const QStringList lstMimeTypes = cmd.mimeTypes();
69  if (!db->appendCollection(col, lstMimeTypes, {{"AccessRights", "luD"}})) {
70  return failureResponse("Unable to create persistent search");
71  }
72 
73  if (!transaction.commit()) {
74  return failureResponse("Unable to commit transaction");
75  }
76 
77  akonadi().searchManager().updateSearch(col);
78 
79  sendResponse(HandlerHelper::fetchCollectionsResponse(akonadi(), col));
80  return successResponse<Protocol::StoreSearchResponse>();
81 }
This class handles all the database access.
Definition: datastore.h:94
Helper class for DataStore transaction handling.
Definition: transaction.h:22
QString number(int n, int base)
QVector::iterator begin()
Represents a collection of PIM items.
Definition: collection.h:61
bool parseStream() override
Parse and handle the IMAP message using the streaming parser.
QString join(const QString &separator) const const
QVector::iterator end()
static Protocol::FetchCollectionsResponse fetchCollectionsResponse(AkonadiServer &akonadi, const Collection &col)
Returns the protocol representation of the given collection.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:39
void setName(const QString &name)
Sets the i18n'ed name of the collection.
Definition: collection.cpp:221
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 8 2022 04:03:04 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.