Akonadi

handler.cpp
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2006 Till Adam <adam@kde.org> *
3 * *
4 * SPDX-License-Identifier: LGPL-2.0-or-later *
5 ***************************************************************************/
6#include "handler.h"
7
8#include "private/scope_p.h"
9
10#include "handler/collectioncopyhandler.h"
11#include "handler/collectioncreatehandler.h"
12#include "handler/collectiondeletehandler.h"
13#include "handler/collectionfetchhandler.h"
14#include "handler/collectionmodifyhandler.h"
15#include "handler/collectionmovehandler.h"
16#include "handler/collectionstatsfetchhandler.h"
17#include "handler/itemcopyhandler.h"
18#include "handler/itemcreatehandler.h"
19#include "handler/itemdeletehandler.h"
20#include "handler/itemfetchhandler.h"
21#include "handler/itemlinkhandler.h"
22#include "handler/itemmodifyhandler.h"
23#include "handler/itemmovehandler.h"
24#include "handler/loginhandler.h"
25#include "handler/logouthandler.h"
26#include "handler/resourceselecthandler.h"
27#include "handler/searchcreatehandler.h"
28#include "handler/searchhandler.h"
29#include "handler/searchresulthandler.h"
30#include "handler/tagcreatehandler.h"
31#include "handler/tagdeletehandler.h"
32#include "handler/tagfetchhandler.h"
33#include "handler/tagmodifyhandler.h"
34#include "handler/transactionhandler.h"
35#include "storage/querybuilder.h"
36
37using namespace Akonadi;
38using namespace Akonadi::Server;
39
40std::unique_ptr<Handler> Handler::findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi)
41{
42 // allowed are LOGIN
43 if (cmd == Protocol::Command::Login) {
44 return std::make_unique<LoginHandler>(akonadi);
45 }
46
47 return {};
48}
49
50std::unique_ptr<Handler> Handler::findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd, AkonadiServer &akonadi)
51{
52 // allowed is LOGOUT
53 if (cmd == Protocol::Command::Logout) {
54 return std::make_unique<LogoutHandler>(akonadi);
55 }
56 return nullptr;
57}
58
59std::unique_ptr<Handler> Handler::findHandlerForCommandAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi)
60{
61 switch (cmd) {
62 case Protocol::Command::Invalid:
63 Q_ASSERT_X(cmd != Protocol::Command::Invalid, __FUNCTION__, "Invalid command is not allowed");
64 return {};
65 case Protocol::Command::Hello:
66 Q_ASSERT_X(cmd != Protocol::Command::Hello, __FUNCTION__, "Hello command is not allowed in this context");
67 return {};
68 case Protocol::Command::Login:
69 case Protocol::Command::Logout:
70 return {};
71 case Protocol::Command::_ResponseBit:
72 Q_ASSERT_X(cmd != Protocol::Command::_ResponseBit, __FUNCTION__, "ResponseBit is not a valid command type");
73 return {};
74
75 case Protocol::Command::Transaction:
76 return std::make_unique<TransactionHandler>(akonadi);
77
78 case Protocol::Command::CreateItem:
79 return std::make_unique<ItemCreateHandler>(akonadi);
80 case Protocol::Command::CopyItems:
81 return std::make_unique<ItemCopyHandler>(akonadi);
82 case Protocol::Command::DeleteItems:
83 return std::make_unique<ItemDeleteHandler>(akonadi);
84 case Protocol::Command::FetchItems:
85 return std::make_unique<ItemFetchHandler>(akonadi);
86 case Protocol::Command::LinkItems:
87 return std::make_unique<ItemLinkHandler>(akonadi);
88 case Protocol::Command::ModifyItems:
89 return std::make_unique<ItemModifyHandler>(akonadi);
90 case Protocol::Command::MoveItems:
91 return std::make_unique<ItemMoveHandler>(akonadi);
92
93 case Protocol::Command::CreateCollection:
94 return std::make_unique<CollectionCreateHandler>(akonadi);
95 case Protocol::Command::CopyCollection:
96 return std::make_unique<CollectionCopyHandler>(akonadi);
97 case Protocol::Command::DeleteCollection:
98 return std::make_unique<CollectionDeleteHandler>(akonadi);
99 case Protocol::Command::FetchCollections:
100 return std::make_unique<CollectionFetchHandler>(akonadi);
101 case Protocol::Command::FetchCollectionStats:
102 return std::make_unique<CollectionStatsFetchHandler>(akonadi);
103 case Protocol::Command::ModifyCollection:
104 return std::make_unique<CollectionModifyHandler>(akonadi);
105 case Protocol::Command::MoveCollection:
106 return std::make_unique<CollectionMoveHandler>(akonadi);
107
108 case Protocol::Command::Search:
109 return std::make_unique<SearchHandler>(akonadi);
110 case Protocol::Command::SearchResult:
111 return std::make_unique<SearchResultHandler>(akonadi);
112 case Protocol::Command::StoreSearch:
113 return std::make_unique<SearchCreateHandler>(akonadi);
114
115 case Protocol::Command::CreateTag:
116 return std::make_unique<TagCreateHandler>(akonadi);
117 case Protocol::Command::DeleteTag:
118 return std::make_unique<TagDeleteHandler>(akonadi);
119 case Protocol::Command::FetchTags:
120 return std::make_unique<TagFetchHandler>(akonadi);
121 case Protocol::Command::ModifyTag:
122 return std::make_unique<TagModifyHandler>(akonadi);
123
124 case Protocol::Command::SelectResource:
125 return std::make_unique<ResourceSelectHandler>(akonadi);
126
127 case Protocol::Command::StreamPayload:
128 Q_ASSERT_X(cmd != Protocol::Command::StreamPayload, __FUNCTION__, "StreamPayload command is not allowed in this context");
129 return {};
130
131 case Protocol::Command::ItemChangeNotification:
132 Q_ASSERT_X(cmd != Protocol::Command::ItemChangeNotification, __FUNCTION__, "ItemChangeNotification command is not allowed on this connection");
133 return {};
134 case Protocol::Command::CollectionChangeNotification:
135 Q_ASSERT_X(cmd != Protocol::Command::CollectionChangeNotification,
136 __FUNCTION__,
137 "CollectionChangeNotification command is not allowed on this connection");
138 return {};
139 case Protocol::Command::TagChangeNotification:
140 Q_ASSERT_X(cmd != Protocol::Command::TagChangeNotification, __FUNCTION__, "TagChangeNotification command is not allowed on this connection");
141 return {};
142 case Protocol::Command::SubscriptionChangeNotification:
143 Q_ASSERT_X(cmd != Protocol::Command::SubscriptionChangeNotification,
144 __FUNCTION__,
145 "SubscriptionChangeNotification command is not allowed on this connection");
146 return {};
147 case Protocol::Command::DebugChangeNotification:
148 Q_ASSERT_X(cmd != Protocol::Command::DebugChangeNotification, __FUNCTION__, "DebugChangeNotification command is not allowed on this connection");
149 return {};
150 case Protocol::Command::ModifySubscription:
151 Q_ASSERT_X(cmd != Protocol::Command::ModifySubscription, __FUNCTION__, "ModifySubscription command is not allowed on this connection");
152 return {};
153 case Protocol::Command::CreateSubscription:
154 Q_ASSERT_X(cmd != Protocol::Command::CreateSubscription, __FUNCTION__, "CreateSubscription command is not allowed on this connection");
155 return {};
156 }
157
158 return {};
159}
160
161Handler::Handler(AkonadiServer &akonadi)
162 : m_akonadi(akonadi)
163{
164}
165
166void Handler::setTag(quint64 tag)
167{
168 m_tag = tag;
169}
170
171quint64 Handler::tag() const
172{
173 return m_tag;
174}
175
176void Handler::setCommand(const Protocol::CommandPtr &cmd)
177{
178 m_command = cmd;
179}
180
181Protocol::CommandPtr Handler::command() const
182{
183 return m_command;
184}
185
186void Handler::setConnection(Connection *connection)
187{
188 m_connection = connection;
189}
190
191Connection *Handler::connection() const
192{
193 return m_connection;
194}
195
196DataStore *Handler::storageBackend() const
197{
198 return m_connection->storageBackend();
199}
200
201AkonadiServer &Handler::akonadi() const
202{
203 return m_akonadi;
204}
205
206bool Handler::failureResponse(const QByteArray &failureMessage)
207{
208 return failureResponse(QString::fromUtf8(failureMessage));
209}
210
211bool Handler::failureResponse(const char *failureMessage)
212{
213 return failureResponse(QString::fromUtf8(failureMessage));
214}
215
216bool Handler::failureResponse(const QString &failureMessage)
217{
218 // Prevent sending multiple error responses from a single handler (or from
219 // a handler and then from Connection, since clients only expect a single
220 // error response
221 if (!m_sentFailureResponse) {
222 m_sentFailureResponse = true;
223 Protocol::ResponsePtr r = Protocol::Factory::response(m_command->type());
224 // FIXME: Error enums?
225 r->setError(1, failureMessage);
226
227 m_connection->sendResponse(m_tag, r);
228 }
229
230 return false;
231}
232
233bool Handler::checkScopeConstraints(const Scope &scope, const QVector<Scope::SelectionScope> &permittedScopes) const
234{
235 return std::any_of(permittedScopes.cbegin(), permittedScopes.cend(), [&scope](Scope::SelectionScope permittedScope) {
236 return scope.scope() == permittedScope;
237 });
238}
An Connection represents one connection of a client to the server.
Definition connection.h:39
This class handles all the database access.
Definition datastore.h:95
void setTag(quint64 tag)
Set the tag of the command to be processed, and thus of the response generated by this handler.
Definition handler.cpp:166
quint64 tag() const
The tag of the command associated with this handler.
Definition handler.cpp:171
static std::unique_ptr< Handler > findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi)
Find a handler for a command that is allowed when the client is not yet authenticated,...
Definition handler.cpp:40
static std::unique_ptr< Handler > findHandlerForCommandAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi)
Find a handler for a command that is allowed when the client is authenticated, like LIST,...
Definition handler.cpp:59
static std::unique_ptr< Handler > findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd, AkonadiServer &akonadi)
Find a handler for a command that is always allowed, like LOGOUT.
Definition handler.cpp:50
Helper integration between Akonadi and Qt.
const_iterator cbegin() const const
const_iterator cend() const const
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:00:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.