Akonadi

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

KDE's Doxygen guidelines are available online.