Akonadi

collectiondeletehandler.cpp
1 /*
2  Copyright (c) 2006 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 "collectiondeletehandler.h"
21 
22 #include "connection.h"
23 #include "handlerhelper.h"
24 #include "storage/datastore.h"
25 #include "storage/transaction.h"
26 #include "storage/selectquerybuilder.h"
27 #include "storage/collectionqueryhelper.h"
28 #include "search/searchmanager.h"
29 
30 #include <private/scope_p.h>
31 
32 using namespace Akonadi;
33 using namespace Akonadi::Server;
34 
35 CollectionDeleteHandler::CollectionDeleteHandler(AkonadiServer &akonadi)
36  : Handler(akonadi)
37 {}
38 
39 bool CollectionDeleteHandler::deleteRecursive(Collection &col)
40 {
41  Collection::List children = col.children();
42  for (Collection &child : children) {
43  if (!deleteRecursive(child)) {
44  return false;
45  }
46  }
47 
48  DataStore *db = connection()->storageBackend();
49  return db->cleanupCollection(col);
50 }
51 
52 bool CollectionDeleteHandler::parseStream()
53 {
54  const auto &cmd = Protocol::cmdCast<Protocol::DeleteCollectionCommand>(m_command);
55 
56  Collection collection = HandlerHelper::collectionFromScope(cmd.collection(), connection()->context());
57  if (!collection.isValid()) {
58  return failureResponse(QStringLiteral("No such collection."));
59  }
60 
61  // handle virtual folders
62  if (collection.resource().name() == QLatin1String(AKONADI_SEARCH_RESOURCE)) {
63  // don't delete virtual root
64  if (collection.parentId() == 0) {
65  return failureResponse(QStringLiteral("Cannot delete virtual root collection"));
66  }
67  }
68 
69  Transaction transaction(storageBackend(), QStringLiteral("DELETE"));
70 
71  if (!deleteRecursive(collection)) {
72  return failureResponse(QStringLiteral("Unable to delete collection"));
73  }
74 
75  if (!transaction.commit()) {
76  return failureResponse(QStringLiteral("Unable to commit transaction"));
77  }
78 
79  return successResponse<Protocol::DeleteCollectionResponse>();
80 }
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:48
Represents a collection of PIM items.
Definition: collection.h:76
const QObjectList & children() const const
Helper class for DataStore transaction handling.
Definition: transaction.h:37
virtual bool cleanupCollection(Collection &collection)
removes the given collection and all its content
Definition: datastore.cpp:824
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
Collection collection() const
Returns the created collection if the job was executed successfully.
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 Wed May 27 2020 22:43:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.