File indexing completed on 2024-11-24 04:43:54

0001 /*
0002     SPDX-FileCopyrightText: 2015-2020 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ewsmodifyitemflagsjob.h"
0008 
0009 #include "ewsitemhandler.h"
0010 #include "ewsmodifyitemjob.h"
0011 
0012 using namespace Akonadi;
0013 
0014 EwsModifyItemFlagsJob::EwsModifyItemFlagsJob(EwsClient &client,
0015                                              QObject *parent,
0016                                              const Item::List &items,
0017                                              const QSet<QByteArray> &addedFlags,
0018                                              const QSet<QByteArray> &removedFlags)
0019     : EwsJob(parent)
0020     , mItems(items)
0021     , mClient(client)
0022     , mAddedFlags(addedFlags)
0023     , mRemovedFlags(removedFlags)
0024 {
0025 }
0026 
0027 EwsModifyItemFlagsJob::~EwsModifyItemFlagsJob() = default;
0028 
0029 void EwsModifyItemFlagsJob::itemModifyFinished(KJob *job)
0030 {
0031     if (job->error()) {
0032         setErrorText(job->errorString());
0033         emitResult();
0034         return;
0035     }
0036 
0037     auto req = qobject_cast<EwsModifyItemJob *>(job);
0038     if (!req) {
0039         setErrorText(QStringLiteral("Invalid EwsModifyItemJob job object"));
0040         emitResult();
0041         return;
0042     }
0043 
0044     mResultItems += req->items();
0045     removeSubjob(job);
0046 
0047     if (subjobs().isEmpty()) {
0048         Q_ASSERT(mResultItems.size() == mItems.size());
0049         emitResult();
0050     }
0051 }
0052 
0053 void EwsModifyItemFlagsJob::start()
0054 {
0055     Item::List items[EwsItemTypeUnknown];
0056 
0057     for (const Item &item : std::as_const(mItems)) {
0058         EwsItemType type = EwsItemHandler::mimeToItemType(item.mimeType());
0059         if (type == EwsItemTypeUnknown) {
0060             setErrorText(QStringLiteral("Unknown item type %1 for item %2").arg(item.mimeType(), item.remoteId()));
0061             emitResult();
0062             return;
0063         } else {
0064             items[type].append(item);
0065         }
0066     }
0067 
0068     bool started = false;
0069     for (int type = 0; type < EwsItemTypeUnknown; type++) {
0070         if (!items[static_cast<EwsItemType>(type)].isEmpty()) {
0071             EwsItemHandler *handler = EwsItemHandler::itemHandler(static_cast<EwsItemType>(type));
0072             EwsModifyItemJob *job = handler->modifyItemJob(mClient, items[type], QSet<QByteArray>() << "FLAGS", this);
0073             connect(job, &EwsModifyItemJob::result, this, &EwsModifyItemFlagsJob::itemModifyFinished);
0074             connect(job, &EwsModifyItemJob::reportStatus, this, [this](int s, const QString &message) {
0075                 Q_EMIT reportStatus(s, message);
0076             });
0077             connect(job, &EwsModifyItemJob::reportPercent, this, [this](int p) {
0078                 Q_EMIT reportPercent(p);
0079             });
0080 
0081             addSubjob(job);
0082             job->start();
0083             started = true;
0084         }
0085     }
0086 
0087     if (!started) {
0088         setErrorText(QStringLiteral("No items to process"));
0089         emitResult();
0090     }
0091 }
0092 
0093 #include "moc_ewsmodifyitemflagsjob.cpp"