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

0001 /***************************************************************************
0002  *   SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>            *
0003  *                                                                         *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later                            *
0005  ***************************************************************************/
0006 
0007 #include "searchhandler.h"
0008 
0009 #include "akonadi.h"
0010 #include "akonadiserver_search_debug.h"
0011 #include "connection.h"
0012 #include "handlerhelper.h"
0013 #include "itemfetchhelper.h"
0014 #include "search/agentsearchengine.h"
0015 #include "search/searchmanager.h"
0016 #include "search/searchrequest.h"
0017 #include "searchhelper.h"
0018 
0019 using namespace Akonadi;
0020 using namespace Akonadi::Server;
0021 
0022 SearchHandler::SearchHandler(AkonadiServer &akonadi)
0023     : Handler(akonadi)
0024 {
0025 }
0026 
0027 bool SearchHandler::parseStream()
0028 {
0029     const auto &cmd = Protocol::cmdCast<Protocol::SearchCommand>(m_command);
0030 
0031     if (cmd.query().isEmpty()) {
0032         return failureResponse("No query specified");
0033     }
0034 
0035     QList<qint64> collectionIds;
0036     bool recursive = cmd.recursive();
0037 
0038     if (cmd.collections().isEmpty() || cmd.collections() == QList<qint64>{0LL}) {
0039         collectionIds << 0;
0040         recursive = true;
0041     }
0042 
0043     QList<qint64> collections = collectionIds;
0044     if (recursive) {
0045         collections += SearchHelper::matchSubcollectionsByMimeType(collectionIds, cmd.mimeTypes());
0046     }
0047 
0048     qCDebug(AKONADISERVER_SEARCH_LOG) << "SEARCH:";
0049     qCDebug(AKONADISERVER_SEARCH_LOG) << "\tQuery:" << cmd.query();
0050     qCDebug(AKONADISERVER_SEARCH_LOG) << "\tMimeTypes:" << cmd.mimeTypes();
0051     qCDebug(AKONADISERVER_SEARCH_LOG) << "\tCollections:" << collections;
0052     qCDebug(AKONADISERVER_SEARCH_LOG) << "\tRemote:" << cmd.remote();
0053     qCDebug(AKONADISERVER_SEARCH_LOG) << "\tRecursive" << recursive;
0054 
0055     if (collections.isEmpty()) {
0056         return successResponse<Protocol::SearchResponse>();
0057     }
0058 
0059     mItemFetchScope = cmd.itemFetchScope();
0060     mTagFetchScope = cmd.tagFetchScope();
0061 
0062     SearchRequest request(connection()->sessionId(), akonadi().searchManager(), akonadi().agentSearchManager());
0063     request.setCollections(collections);
0064     request.setMimeTypes(cmd.mimeTypes());
0065     request.setQuery(cmd.query());
0066     request.setRemoteSearch(cmd.remote());
0067     QObject::connect(&request, &SearchRequest::resultsAvailable, &request, [this](const QSet<qint64> &results) {
0068         processResults(results);
0069     });
0070     request.exec();
0071 
0072     // qCDebug(AKONADISERVER_SEARCH_LOG) << "\tResult:" << uids;
0073     qCDebug(AKONADISERVER_SEARCH_LOG) << "\tResult:" << mAllResults.count() << "matches";
0074 
0075     return successResponse<Protocol::SearchResponse>();
0076 }
0077 
0078 void SearchHandler::processResults(const QSet<qint64> &results)
0079 {
0080     QSet<qint64> newResults = results;
0081     newResults.subtract(mAllResults);
0082     mAllResults.unite(newResults);
0083 
0084     if (newResults.isEmpty()) {
0085         return;
0086     }
0087 
0088     ImapSet imapSet;
0089     imapSet.add(newResults);
0090 
0091     Scope scope;
0092     scope.setUidSet(imapSet);
0093 
0094     ItemFetchHelper fetchHelper(connection(), scope, mItemFetchScope, mTagFetchScope, akonadi());
0095     fetchHelper.fetchItems();
0096 }