Akonadi

collectionmovehandler.cpp
1 /*
2  Copyright (c) 2009 Volker Krause <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "collectionmovehandler.h"
21 
22 #include "akonadi.h"
23 #include "handlerhelper.h"
24 #include "connection.h"
25 #include "cachecleaner.h"
26 #include "storage/datastore.h"
27 #include "storage/itemretriever.h"
28 #include "storage/transaction.h"
29 #include "storage/collectionqueryhelper.h"
30 
31 using namespace Akonadi;
32 using namespace Akonadi::Server;
33 
34 CollectionMoveHandler::CollectionMoveHandler(AkonadiServer &akonadi)
35  : Handler(akonadi)
36 {}
37 
38 bool CollectionMoveHandler::parseStream()
39 {
40  const auto &cmd = Protocol::cmdCast<Protocol::MoveCollectionCommand>(m_command);
41 
42  Collection source = HandlerHelper::collectionFromScope(cmd.collection(), connection()->context());
43  if (!source.isValid()) {
44  return failureResponse(QStringLiteral("Invalid collection to move"));
45  }
46 
47  Collection target;
48  if (cmd.destination().isEmpty()) {
49  target.setId(0);
50  } else {
51  target = HandlerHelper::collectionFromScope(cmd.destination(), connection()->context());
52  if (!target.isValid()) {
53  return failureResponse(QStringLiteral("Invalid destination collection"));
54  }
55  }
56 
57  if (source.parentId() == target.id()) {
58  return successResponse<Protocol::MoveCollectionResponse>();
59  }
60 
61  CacheCleanerInhibitor inhibitor(akonadi());
62 
63  // retrieve all not yet cached items of the source
64  ItemRetriever retriever(akonadi().itemRetrievalManager(), connection(), connection()->context());
65  retriever.setCollection(source, true);
66  retriever.setRetrieveFullPayload(true);
67  if (!retriever.exec()) {
68  return failureResponse(retriever.lastError());
69  }
70 
71  DataStore *store = connection()->storageBackend();
72  Transaction transaction(store, QStringLiteral("CollectionMoveHandler"));
73 
74  if (!store->moveCollection(source, target)) {
75  return failureResponse(QStringLiteral("Unable to reparent collection"));
76  }
77 
78  if (!transaction.commit()) {
79  return failureResponse(QStringLiteral("Cannot commit transaction."));
80  }
81 
82  return successResponse<Protocol::MoveCollectionResponse>();
83 }
void setCollection(const Collection &collection, bool recursive=true)
Retrieve all items in the given collection.
Helper class for retrieving missing items parts from remote resources.
Definition: itemretriever.h:53
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:48
A RAII helper class to temporarily stop the CacheCleaner.
Definition: cachecleaner.h:46
Helper class for DataStore transaction handling.
Definition: transaction.h:37
bool commit()
Commits the transaction.
Definition: transaction.cpp:42
Helper integration between Akonadi and Qt.
This class handles all the database access.
Definition: datastore.h:102
Representation of a record in the Collection table.
Definition: entities.h:451
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 30 2020 22:46:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.