File indexing completed on 2024-06-23 05:07:00

0001 /*
0002     SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "collectiondeletehandler.h"
0008 
0009 #include "connection.h"
0010 #include "handlerhelper.h"
0011 #include "search/searchmanager.h"
0012 #include "storage/collectionqueryhelper.h"
0013 #include "storage/datastore.h"
0014 #include "storage/selectquerybuilder.h"
0015 #include "storage/transaction.h"
0016 
0017 #include "private/scope_p.h"
0018 
0019 using namespace Akonadi;
0020 using namespace Akonadi::Server;
0021 
0022 CollectionDeleteHandler::CollectionDeleteHandler(AkonadiServer &akonadi)
0023     : Handler(akonadi)
0024 {
0025 }
0026 
0027 bool CollectionDeleteHandler::deleteRecursive(Collection &col)
0028 {
0029     Collection::List children = col.children();
0030     for (Collection &child : children) {
0031         if (!deleteRecursive(child)) {
0032             return false;
0033         }
0034     }
0035 
0036     DataStore *db = connection()->storageBackend();
0037     return db->cleanupCollection(col);
0038 }
0039 
0040 bool CollectionDeleteHandler::parseStream()
0041 {
0042     const auto &cmd = Protocol::cmdCast<Protocol::DeleteCollectionCommand>(m_command);
0043 
0044     Collection collection = HandlerHelper::collectionFromScope(cmd.collection(), connection()->context());
0045     if (!collection.isValid()) {
0046         return failureResponse(QStringLiteral("No such collection."));
0047     }
0048 
0049     // handle virtual folders
0050     if (collection.resource().name() == QLatin1StringView(AKONADI_SEARCH_RESOURCE)) {
0051         // don't delete virtual root
0052         if (collection.parentId() == 0) {
0053             return failureResponse(QStringLiteral("Cannot delete virtual root collection"));
0054         }
0055     }
0056 
0057     Transaction transaction(storageBackend(), QStringLiteral("DELETE"));
0058 
0059     if (!deleteRecursive(collection)) {
0060         return failureResponse(QStringLiteral("Unable to delete collection"));
0061     }
0062 
0063     if (!transaction.commit()) {
0064         return failureResponse(QStringLiteral("Unable to commit transaction"));
0065     }
0066 
0067     return successResponse<Protocol::DeleteCollectionResponse>();
0068 }