Akonadi

itemlinkhandler.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "itemlinkhandler.h"
8
9#include "connection.h"
10#include "handlerhelper.h"
11#include "storage/datastore.h"
12#include "storage/itemqueryhelper.h"
13#include "storage/selectquerybuilder.h"
14#include "storage/transaction.h"
15
16#include "private/scope_p.h"
17
18using namespace Akonadi;
19using namespace Akonadi::Server;
20
21ItemLinkHandler::ItemLinkHandler(AkonadiServer &akonadi)
22 : Handler(akonadi)
23{
24}
25
27{
28 const auto &cmd = Protocol::cmdCast<Protocol::LinkItemsCommand>(m_command);
29
30 const Collection collection = HandlerHelper::collectionFromScope(cmd.destination(), connection()->context());
31 if (!collection.isVirtual()) {
32 return failureResponse(QStringLiteral("Can't link items to non-virtual collections"));
33 }
34
35 /* FIXME BIN
36 Resource originalContext;
37 Scope::SelectionScope itemSelectionScope = Scope::selectionScopeFromByteArray(m_streamParser->peekString());
38 if (itemSelectionScope != Scope::None) {
39 m_streamParser->readString();
40 // Unset Resource context if destination collection is specified using HRID/RID,
41 // because otherwise the Resource context is relative to the destination collection
42 // instead of the source collection (collection context)
43 if ((mDestinationScope.scope() == Scope::HierarchicalRid || mDestinationScope.scope() == Scope::Rid) && itemSelectionScope == Scope::Rid) {
44 originalContext = connection()->context()->resource();
45 connection()->context()->setResource(Resource());
46 }
47 }
48 Scope itemScope(itemSelectionScope);
49 itemScope.parseScope(m_streamParser);
50 */
51
53 ItemQueryHelper::scopeToQuery(cmd.items(), connection()->context(), qb);
54
55 /*
56 if (originalContext.isValid()) {
57 connection()->context()->setResource(originalContext);
58 }
59 */
60
61 if (!qb.exec()) {
62 return failureResponse(QStringLiteral("Unable to execute item query"));
63 }
64
65 const PimItem::List items = qb.result();
66 const bool createLinks = (cmd.action() == Protocol::LinkItemsCommand::Link);
67
68 DataStore *store = connection()->storageBackend();
69 Transaction transaction(store, createLinks ? QStringLiteral("LINK") : QStringLiteral("UNLINK"));
70
71 PimItem::List toLink;
72 PimItem::List toUnlink;
73 for (const PimItem &item : items) {
74 const bool alreadyLinked = collection.relatesToPimItem(item);
75 bool result = true;
76 if (createLinks && !alreadyLinked) {
77 result = collection.addPimItem(item);
78 toLink << item;
79 } else if (!createLinks && alreadyLinked) {
80 result = collection.removePimItem(item);
81 toUnlink << item;
82 }
83 if (!result) {
84 return failureResponse(QStringLiteral("Failed to modify item reference"));
85 }
86 }
87
88 if (!transaction.commit()) {
89 return failureResponse(QStringLiteral("Cannot commit transaction."));
90 }
91
92 if (!toLink.isEmpty()) {
93 store->notificationCollector()->itemsLinked(toLink, collection);
94 } else if (!toUnlink.isEmpty()) {
95 store->notificationCollector()->itemsUnlinked(toUnlink, collection);
96 }
97
98 return successResponse<Protocol::LinkItemsResponse>();
99}
Represents a collection of PIM items.
Definition collection.h:62
This class handles all the database access.
Definition datastore.h:95
NotificationCollector * notificationCollector()
Returns the notification collector of this DataStore object.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition handler.h:32
bool parseStream() override
Parse and handle the IMAP message using the streaming parser.
void itemsUnlinked(const PimItem::List &items, const Collection &collection)
Notify about unlinked items.
void itemsLinked(const PimItem::List &items, const Collection &collection)
Notify about linked items.
bool exec()
Executes the query, returns true on success.
Helper class for creating and executing database SELECT queries.
QList< T > result()
Returns the result of this SELECT query.
Helper class for DataStore transaction handling.
Definition transaction.h:23
bool commit()
Commits the transaction.
void scopeToQuery(const Scope &scope, const CommandContext &context, QueryBuilder &qb)
Add conditions to qb for the given item operation scope scope.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.