File indexing completed on 2024-11-10 04:40:31

0001 
0002 /*
0003     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0004     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "searchcreatejob.h"
0010 
0011 #include "job_p.h"
0012 #include "protocolhelper_p.h"
0013 #include "searchquery.h"
0014 
0015 #include "private/protocol_p.h"
0016 
0017 using namespace Akonadi;
0018 
0019 class Akonadi::SearchCreateJobPrivate : public JobPrivate
0020 {
0021 public:
0022     SearchCreateJobPrivate(const QString &name, const SearchQuery &query, SearchCreateJob *parent)
0023         : JobPrivate(parent)
0024         , mName(name)
0025         , mQuery(query)
0026     {
0027     }
0028 
0029     const QString mName;
0030     const SearchQuery mQuery;
0031     QStringList mMimeTypes;
0032     QList<Collection> mCollections;
0033     bool mRecursive = false;
0034     bool mRemote = false;
0035     Collection mCreatedCollection;
0036 
0037     // JobPrivate interface
0038 public:
0039     QString jobDebuggingString() const override;
0040 };
0041 
0042 QString Akonadi::SearchCreateJobPrivate::jobDebuggingString() const
0043 {
0044     QString str = QStringLiteral("Name :%1 ").arg(mName);
0045     if (mRecursive) {
0046         str += QStringLiteral("Recursive ");
0047     }
0048     if (mRemote) {
0049         str += QStringLiteral("Remote");
0050     }
0051     return str;
0052 }
0053 
0054 SearchCreateJob::SearchCreateJob(const QString &name, const SearchQuery &searchQuery, QObject *parent)
0055     : Job(new SearchCreateJobPrivate(name, searchQuery, this), parent)
0056 {
0057 }
0058 
0059 SearchCreateJob::~SearchCreateJob()
0060 {
0061 }
0062 
0063 void SearchCreateJob::setSearchCollections(const QList<Collection> &collections)
0064 {
0065     Q_D(SearchCreateJob);
0066 
0067     d->mCollections = collections;
0068 }
0069 
0070 QList<Collection> SearchCreateJob::searchCollections() const
0071 {
0072     return d_func()->mCollections;
0073 }
0074 
0075 void SearchCreateJob::setSearchMimeTypes(const QStringList &mimeTypes)
0076 {
0077     Q_D(SearchCreateJob);
0078 
0079     d->mMimeTypes = mimeTypes;
0080 }
0081 
0082 QStringList SearchCreateJob::searchMimeTypes() const
0083 {
0084     return d_func()->mMimeTypes;
0085 }
0086 
0087 void SearchCreateJob::setRecursive(bool recursive)
0088 {
0089     Q_D(SearchCreateJob);
0090 
0091     d->mRecursive = recursive;
0092 }
0093 
0094 bool SearchCreateJob::isRecursive() const
0095 {
0096     return d_func()->mRecursive;
0097 }
0098 
0099 void SearchCreateJob::setRemoteSearchEnabled(bool enabled)
0100 {
0101     Q_D(SearchCreateJob);
0102 
0103     d->mRemote = enabled;
0104 }
0105 
0106 bool SearchCreateJob::isRemoteSearchEnabled() const
0107 {
0108     return d_func()->mRemote;
0109 }
0110 
0111 void SearchCreateJob::doStart()
0112 {
0113     Q_D(SearchCreateJob);
0114 
0115     auto cmd = Protocol::StoreSearchCommandPtr::create();
0116     cmd->setName(d->mName);
0117     cmd->setQuery(QString::fromUtf8(d->mQuery.toJSON()));
0118     cmd->setMimeTypes(d->mMimeTypes);
0119     cmd->setRecursive(d->mRecursive);
0120     cmd->setRemote(d->mRemote);
0121     if (!d->mCollections.isEmpty()) {
0122         QList<qint64> ids;
0123         ids.reserve(d->mCollections.size());
0124         for (const Collection &col : std::as_const(d->mCollections)) {
0125             ids << col.id();
0126         }
0127         cmd->setQueryCollections(ids);
0128     }
0129 
0130     d->sendCommand(cmd);
0131 }
0132 
0133 Akonadi::Collection SearchCreateJob::createdCollection() const
0134 {
0135     Q_D(const SearchCreateJob);
0136     return d->mCreatedCollection;
0137 }
0138 
0139 bool SearchCreateJob::doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
0140 {
0141     Q_D(SearchCreateJob);
0142     if (response->isResponse() && response->type() == Protocol::Command::FetchCollections) {
0143         d->mCreatedCollection = ProtocolHelper::parseCollection(Protocol::cmdCast<Protocol::FetchCollectionsResponse>(response));
0144         return false;
0145     }
0146 
0147     if (response->isResponse() && response->type() == Protocol::Command::StoreSearch) {
0148         return true;
0149     }
0150 
0151     return Job::doHandleResponse(tag, response);
0152 }
0153 
0154 #include "moc_searchcreatejob.cpp"