Akonadi

searchcreatehandler.cpp
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> *
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 "shared/akranges.h"
15#include "storage/datastore.h"
16#include "storage/entity.h"
17#include "storage/transaction.h"
18
19using namespace Akonadi;
20using namespace Akonadi::Server;
21using namespace AkRanges;
22
23SearchCreateHandler::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 QList<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}
Represents a collection of PIM items.
Definition collection.h:62
void setName(const QString &name)
Sets the i18n'ed name of the collection.
This class handles all the database access.
Definition datastore.h:95
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:32
bool parseStream() override
Parse and handle the IMAP message using the streaming parser.
Helper class for DataStore transaction handling.
Definition transaction.h:23
Helper integration between Akonadi and Qt.
iterator begin()
iterator end()
QString number(double n, char format, int precision)
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 11:56:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.