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

KDE's Doxygen guidelines are available online.