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

KDE's Doxygen guidelines are available online.