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

0001 /*
0002     SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "job_p.h"
0008 #include "searchresultjob_p.h"
0009 
0010 #include "private/protocol_p.h"
0011 #include "protocolhelper_p.h"
0012 
0013 namespace Akonadi
0014 {
0015 class SearchResultJobPrivate : public Akonadi::JobPrivate
0016 {
0017 public:
0018     explicit SearchResultJobPrivate(SearchResultJob *parent);
0019 
0020     QList<QByteArray> rid;
0021     QByteArray searchId;
0022     Collection collection;
0023     ImapSet uid;
0024 
0025     // JobPrivate interface
0026 public:
0027     QString jobDebuggingString() const override;
0028 };
0029 
0030 QString SearchResultJobPrivate::jobDebuggingString() const
0031 {
0032     return QStringLiteral("Collection id: %1 Search id: %2").arg(collection.id()).arg(QString::fromLatin1(searchId));
0033 }
0034 
0035 SearchResultJobPrivate::SearchResultJobPrivate(SearchResultJob *parent)
0036     : JobPrivate(parent)
0037 {
0038 }
0039 
0040 } // namespace Akonadi
0041 
0042 using namespace Akonadi;
0043 
0044 SearchResultJob::SearchResultJob(const QByteArray &searchId, const Collection &collection, QObject *parent)
0045     : Job(new SearchResultJobPrivate(this), parent)
0046 {
0047     Q_D(SearchResultJob);
0048     Q_ASSERT(collection.isValid());
0049 
0050     d->searchId = searchId;
0051     d->collection = collection;
0052 }
0053 
0054 SearchResultJob::~SearchResultJob()
0055 {
0056 }
0057 
0058 void SearchResultJob::setSearchId(const QByteArray &searchId)
0059 {
0060     Q_D(SearchResultJob);
0061     d->searchId = searchId;
0062 }
0063 
0064 QByteArray SearchResultJob::searchId() const
0065 {
0066     return d_func()->searchId;
0067 }
0068 
0069 void SearchResultJob::setResult(const ImapSet &set)
0070 {
0071     Q_D(SearchResultJob);
0072     d->rid.clear();
0073     d->uid = set;
0074 }
0075 
0076 void SearchResultJob::setResult(const QList<qint64> &ids)
0077 {
0078     Q_D(SearchResultJob);
0079     d->rid.clear();
0080     d->uid = ImapSet();
0081     d->uid.add(ids);
0082 }
0083 
0084 void SearchResultJob::setResult(const QList<QByteArray> &remoteIds)
0085 {
0086     Q_D(SearchResultJob);
0087     d->uid = ImapSet();
0088     d->rid = remoteIds;
0089 }
0090 
0091 void SearchResultJob::doStart()
0092 {
0093     Q_D(SearchResultJob);
0094 
0095     Scope scope;
0096     if (!d->rid.isEmpty()) {
0097         QStringList ridSet;
0098         ridSet.reserve(d->rid.size());
0099         for (const QByteArray &rid : std::as_const(d->rid)) {
0100             ridSet << QString::fromUtf8(rid);
0101         }
0102         scope.setRidSet(ridSet);
0103     } else {
0104         scope.setUidSet(d->uid);
0105     }
0106     d->sendCommand(Protocol::SearchResultCommandPtr::create(d->searchId, d->collection.id(), scope));
0107 }
0108 
0109 bool SearchResultJob::doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
0110 {
0111     if (!response->isResponse() || response->type() != Protocol::Command::SearchResult) {
0112         return Job::doHandleResponse(tag, response);
0113     }
0114 
0115     return true;
0116 }
0117 
0118 #include "moc_searchresultjob_p.cpp"