Akonadi

collectionmovehandler.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "collectionmovehandler.h"
8#include "akonadiserver_debug.h"
9
10#include "akonadi.h"
11#include "cachecleaner.h"
12#include "connection.h"
13#include "handlerhelper.h"
14#include "storage/datastore.h"
15#include "storage/itemretriever.h"
16#include "storage/transaction.h"
17
18using namespace Akonadi;
19using namespace Akonadi::Server;
20
21namespace
22{
23
24bool isRootCollection(const Scope &scope)
25{
26 return scope.scope() == Scope::Uid && scope.uid() == 0;
27}
28
29}
30
31CollectionMoveHandler::CollectionMoveHandler(AkonadiServer &akonadi)
32 : Handler(akonadi)
33{
34}
35
37{
38 const auto &cmd = Protocol::cmdCast<Protocol::MoveCollectionCommand>(m_command);
39
40 Collection source = HandlerHelper::collectionFromScope(cmd.collection(), connection()->context());
41 if (!source.isValid()) {
42 return failureResponse(QStringLiteral("Invalid collection to move"));
43 }
44
45 Collection target;
46 if (!checkScopeConstraints(cmd.destination(), {Scope::Uid, Scope::HierarchicalRid})) {
47 qCWarning(AKONADISERVER_LOG) << "Only UID and HRID-based move destination are supported.";
48 return failureResponse("Only UID and HRID-based move destination are supported");
49 }
50 if (isRootCollection(cmd.destination())) {
51 target.setId(0);
52 } else {
53 target = HandlerHelper::collectionFromScope(cmd.destination(), connection()->context());
54 if (!target.isValid()) {
55 qCWarning(AKONADISERVER_LOG) << "Invalid destination collection" << cmd.destination();
56 return failureResponse("Invalid destination collection");
57 }
58 }
59
60 if (source.parentId() == target.id()) {
61 return successResponse<Protocol::MoveCollectionResponse>();
62 }
63
64 CacheCleanerInhibitor inhibitor(akonadi());
65
66 // retrieve all not yet cached items of the source
67 ItemRetriever retriever(akonadi().itemRetrievalManager(), connection(), connection()->context());
68 retriever.setCollection(source, true);
69 retriever.setRetrieveFullPayload(true);
70 if (!retriever.exec()) {
71 return failureResponse(retriever.lastError());
72 }
73
74 DataStore *store = connection()->storageBackend();
75 Transaction transaction(store, QStringLiteral("CollectionMoveHandler"));
76
77 if (!store->moveCollection(source, target)) {
78 return failureResponse(QStringLiteral("Unable to reparent collection"));
79 }
80
81 if (!transaction.commit()) {
82 return failureResponse(QStringLiteral("Cannot commit transaction."));
83 }
84
85 return successResponse<Protocol::MoveCollectionResponse>();
86}
Represents a collection of PIM items.
Definition collection.h:62
void setId(Id identifier)
Sets the unique identifier of the collection.
A RAII helper class to temporarily stop the CacheCleaner.
bool parseStream() override
Parse and handle the IMAP message using the streaming parser.
This class handles all the database access.
Definition datastore.h:95
virtual bool moveCollection(Collection &collection, const Collection &newParent)
moves the collection collection to newParent.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition handler.h:32
Helper class for retrieving missing items parts from remote resources.
void setCollection(const Collection &collection, bool recursive=true)
Retrieve all items in the given collection.
Helper class for DataStore transaction handling.
Definition transaction.h:23
bool commit()
Commits the transaction.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 11:56:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.