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

0001 /***************************************************************************
0002  *   SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>            *
0003  *                                                                         *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later                            *
0005  ***************************************************************************/
0006 
0007 #include "searchcreatehandler.h"
0008 
0009 #include "akonadi.h"
0010 #include "akonadiserver_debug.h"
0011 #include "connection.h"
0012 #include "handlerhelper.h"
0013 #include "search/searchmanager.h"
0014 #include "shared/akranges.h"
0015 #include "storage/datastore.h"
0016 #include "storage/entity.h"
0017 #include "storage/transaction.h"
0018 
0019 using namespace Akonadi;
0020 using namespace Akonadi::Server;
0021 using namespace AkRanges;
0022 
0023 SearchCreateHandler::SearchCreateHandler(AkonadiServer &akonadi)
0024     : Handler(akonadi)
0025 {
0026 }
0027 
0028 bool SearchCreateHandler::parseStream()
0029 {
0030     const auto &cmd = Protocol::cmdCast<Protocol::StoreSearchCommand>(m_command);
0031 
0032     if (cmd.name().isEmpty()) {
0033         return failureResponse("No name specified");
0034     }
0035 
0036     if (cmd.query().isEmpty()) {
0037         return failureResponse("No query specified");
0038     }
0039 
0040     DataStore *db = connection()->storageBackend();
0041     Transaction transaction(db, QStringLiteral("SEARCH PERSISTENT"));
0042 
0043     QStringList queryAttributes;
0044 
0045     if (cmd.remote()) {
0046         queryAttributes << QStringLiteral(AKONADI_PARAM_REMOTE);
0047     }
0048     if (cmd.recursive()) {
0049         queryAttributes << QStringLiteral(AKONADI_PARAM_RECURSIVE);
0050     }
0051 
0052     QList<qint64> queryColIds = cmd.queryCollections();
0053     std::sort(queryColIds.begin(), queryColIds.end());
0054     const auto queryCollections = queryColIds | Views::transform([](const auto id) {
0055                                       return QString::number(id);
0056                                   })
0057         | Actions::toQList;
0058 
0059     Collection col;
0060     col.setQueryString(cmd.query());
0061     col.setQueryAttributes(queryAttributes.join(QLatin1Char(' ')));
0062     col.setQueryCollections(queryCollections.join(QLatin1Char(' ')));
0063     col.setParentId(1); // search root
0064     col.setResourceId(1); // search resource
0065     col.setName(cmd.name());
0066     col.setIsVirtual(true);
0067 
0068     const QStringList lstMimeTypes = cmd.mimeTypes();
0069     if (!db->appendCollection(col, lstMimeTypes, {{"AccessRights", "luD"}})) {
0070         return failureResponse("Unable to create persistent search");
0071     }
0072 
0073     if (!transaction.commit()) {
0074         return failureResponse("Unable to commit transaction");
0075     }
0076 
0077     akonadi().searchManager().updateSearch(col);
0078 
0079     sendResponse(HandlerHelper::fetchCollectionsResponse(akonadi(), col));
0080     return successResponse<Protocol::StoreSearchResponse>();
0081 }