Akonadi

collectionmovehandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "collectionmovehandler.h"
8 
9 #include "akonadi.h"
10 #include "cachecleaner.h"
11 #include "connection.h"
12 #include "handlerhelper.h"
13 #include "storage/collectionqueryhelper.h"
14 #include "storage/datastore.h"
15 #include "storage/itemretriever.h"
16 #include "storage/transaction.h"
17 
18 using namespace Akonadi;
19 using namespace Akonadi::Server;
20 
21 CollectionMoveHandler::CollectionMoveHandler(AkonadiServer &akonadi)
22  : Handler(akonadi)
23 {
24 }
25 
26 bool CollectionMoveHandler::parseStream()
27 {
28  const auto &cmd = Protocol::cmdCast<Protocol::MoveCollectionCommand>(m_command);
29 
30  Collection source = HandlerHelper::collectionFromScope(cmd.collection(), connection()->context());
31  if (!source.isValid()) {
32  return failureResponse(QStringLiteral("Invalid collection to move"));
33  }
34 
35  Collection target;
36  if (cmd.destination().isEmpty()) {
37  target.setId(0);
38  } else {
39  target = HandlerHelper::collectionFromScope(cmd.destination(), connection()->context());
40  if (!target.isValid()) {
41  return failureResponse(QStringLiteral("Invalid destination collection"));
42  }
43  }
44 
45  if (source.parentId() == target.id()) {
46  return successResponse<Protocol::MoveCollectionResponse>();
47  }
48 
49  CacheCleanerInhibitor inhibitor(akonadi());
50 
51  // retrieve all not yet cached items of the source
52  ItemRetriever retriever(akonadi().itemRetrievalManager(), connection(), connection()->context());
53  retriever.setCollection(source, true);
54  retriever.setRetrieveFullPayload(true);
55  if (!retriever.exec()) {
56  return failureResponse(retriever.lastError());
57  }
58 
59  DataStore *store = connection()->storageBackend();
60  Transaction transaction(store, QStringLiteral("CollectionMoveHandler"));
61 
62  if (!store->moveCollection(source, target)) {
63  return failureResponse(QStringLiteral("Unable to reparent collection"));
64  }
65 
66  if (!transaction.commit()) {
67  return failureResponse(QStringLiteral("Cannot commit transaction."));
68  }
69 
70  return successResponse<Protocol::MoveCollectionResponse>();
71 }
This class handles all the database access.
Definition: datastore.h:94
Helper class for DataStore transaction handling.
Definition: transaction.h:22
void setId(Id identifier)
Sets the unique identifier of the collection.
Definition: collection.cpp:91
Represents a collection of PIM items.
Definition: collection.h:61
A RAII helper class to temporarily stop the CacheCleaner.
Definition: cachecleaner.h:31
void setCollection(const Collection &collection, bool recursive=true)
Retrieve all items in the given collection.
virtual bool moveCollection(Collection &collection, const Collection &newParent)
moves the collection collection to newParent.
Definition: datastore.cpp:942
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:39
Helper class for retrieving missing items parts from remote resources.
Definition: itemretriever.h:38
bool commit()
Commits the transaction.
Definition: transaction.cpp:29
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.