Commit 8316db61 authored by Krzysztof Nowicki's avatar Krzysztof Nowicki Committed by Laurent Montel
Browse files

Implement chunked item tag update


Signed-off-by: Krzysztof Nowicki's avatarKrzysztof Nowicki <krissn@op.pl>
parent aaf6ef50
......@@ -1165,13 +1165,18 @@ void EwsResource::itemsTagsChanged(const Item::List &items, const QSet<Tag> &add
Q_UNUSED(addedTags)
Q_UNUSED(removedTags)
Q_EMIT status(Running, i18nc("@info:status", "Updating item tags"));
auto job = new EwsUpdateItemsTagsJob(items, mTagStore, mEwsClient, this);
connect(job, &EwsUpdateItemsTagsJob::result, this, &EwsResource::itemsTagChangeFinished);
connectStatusSignals(job);
job->start();
}
void EwsResource::itemsTagChangeFinished(KJob *job)
{
emitReadyStatus();
if (job->error()) {
cancelTask(i18nc("@info:status", "Failed to process item tags update request"));
return;
......
......@@ -22,11 +22,14 @@
using namespace Akonadi;
constexpr unsigned ChunkSize = 10;
EwsUpdateItemsTagsJob::EwsUpdateItemsTagsJob(const Akonadi::Item::List &items, EwsTagStore *tagStore, EwsClient &client, EwsResource *parent)
: EwsJob(parent)
, mItems(items)
, mTagStore(tagStore)
, mClient(client)
, mChunkedJob(ChunkSize)
{
}
......@@ -99,7 +102,9 @@ void EwsUpdateItemsTagsJob::globalTagsWriteFinished(KJob *job)
void EwsUpdateItemsTagsJob::doUpdateItemsTags()
{
auto req = new EwsUpdateItemRequest(mClient, this);
QVector<EwsUpdateItemRequest::ItemChange> itemChanges;
itemChanges.reserve(mItems.size());
Q_FOREACH (const Item &item, mItems) {
EwsUpdateItemRequest::ItemChange ic(EwsId(item.remoteId(), item.remoteRevision()), EwsItemHandler::mimeToItemType(item.mimeType()));
if (!item.tags().isEmpty()) {
......@@ -124,34 +129,46 @@ void EwsUpdateItemsTagsJob::doUpdateItemsTags()
upd = new EwsUpdateItemRequest::DeleteUpdate(EwsPropertyField(QStringLiteral("item:Categories")));
ic.addUpdate(upd);
}
req->addItemChange(ic);
itemChanges.append(ic);
}
connect(req, &EwsUpdateItemRequest::result, this, &EwsUpdateItemsTagsJob::updateItemsTagsRequestFinished);
req->start();
mChunkedJob.setItems(itemChanges);
mChunkedJob.start(
[this](EwsUpdateItemRequest::ItemChange::List::const_iterator firstChange, EwsUpdateItemRequest::ItemChange::List::const_iterator lastChange) {
auto req = new EwsUpdateItemRequest(mClient, this);
for (auto it = firstChange; it != lastChange; ++it) {
req->addItemChange(*it);
}
return req;
},
[](EwsUpdateItemRequest *req) {
return req->responses();
},
[this](unsigned int progress) {
Q_EMIT percent(progress);
},
[this](bool success, const QString &error) {
updateItemsTagsRequestFinished(success, error);
});
}
void EwsUpdateItemsTagsJob::updateItemsTagsRequestFinished(KJob *job)
void EwsUpdateItemsTagsJob::updateItemsTagsRequestFinished(bool success, const QString &error)
{
if (job->error()) {
setErrorMsg(job->errorString());
if (!success) {
setErrorMsg(error);
emitResult();
return;
}
auto req = qobject_cast<EwsUpdateItemRequest *>(job);
if (!req) {
setErrorMsg(QStringLiteral("Invalid EwsUpdateItemRequest job object"));
return;
}
Q_ASSERT(mItems.count() == req->responses().count());
auto responses = mChunkedJob.responses();
Q_ASSERT(mItems.count() == responses.count());
auto itemIt = mItems.begin();
Q_FOREACH (const EwsUpdateItemRequest::Response &resp, req->responses()) {
for (const auto &resp : responses) {
if (resp.isSuccess()) {
itemIt->setRemoteRevision(resp.itemId().changeKey());
}
++itemIt;
}
emitResult();
......
......@@ -6,7 +6,10 @@
#pragma once
#include "ewsabstractchunkedjob.h"
#include "ewsjob.h"
#include "ewsupdateitemrequest.h"
#include <AkonadiCore/Item>
class EwsTagStore;
......@@ -40,14 +43,20 @@ public:
private Q_SLOTS:
void itemsTagsChangedTagsFetched(KJob *job);
void updateItemsTagsRequestFinished(KJob *job);
void globalTagsWriteFinished(KJob *job);
Q_SIGNALS:
void status(int status, const QString &message = QString());
void percent(int progress);
private:
void updateItemsTagsRequestFinished(bool success, const QString &error);
void doUpdateItemsTags();
Akonadi::Item::List mItems;
EwsTagStore *mTagStore = nullptr;
EwsClient &mClient;
EwsAbstractChunkedJob<EwsUpdateItemRequest, EwsUpdateItemRequest::ItemChange, EwsUpdateItemRequest::Response> mChunkedJob;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment