Akonadi

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

KDE's Doxygen guidelines are available online.