Akonadi

itemdeletejob.cpp
1/*
2 SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "itemdeletejob.h"
8
9#include "collection.h"
10#include "job_p.h"
11#include "private/protocol_p.h"
12#include "protocolhelper_p.h"
13
14#include <span>
15
16using namespace Akonadi;
17
18class Akonadi::ItemDeleteJobPrivate : public JobPrivate
19{
20 static constexpr std::size_t MaxBatchSize = 10'000UL;
21
22public:
23 explicit ItemDeleteJobPrivate(ItemDeleteJob *parent)
24 : JobPrivate(parent)
25 {
26 }
27
28 bool requestBatch()
29 {
30 if (!mItems.empty()) { // item-based removal
31 const auto batchSize = qMin(MaxBatchSize, mRemainingItems.size());
32 if (batchSize == 0) {
33 return true;
34 }
35
36 const auto batch = mRemainingItems.subspan(0, batchSize);
37 mRemainingItems = mRemainingItems.subspan(batch.size());
38 const auto batchItems = QList(batch.begin(), batch.end());
39
40 sendCommand(Protocol::DeleteItemsCommandPtr::create(ProtocolHelper::entitySetToScope(batchItems),
41 ProtocolHelper::commandContextToProtocol(mCollection, mCurrentTag, batchItems)));
42 } else { // collection- or tag-based removal
43 sendCommand(Protocol::DeleteItemsCommandPtr::create(Scope(), ProtocolHelper::commandContextToProtocol(mCollection, mCurrentTag, {})));
44 }
45
46 return false;
47 }
48
49 Q_DECLARE_PUBLIC(ItemDeleteJob)
50 QString jobDebuggingString() const override;
51
52 Item::List mItems;
53 Collection mCollection;
54 Tag mCurrentTag;
55 std::span<const Item> mRemainingItems;
56};
57
58QString Akonadi::ItemDeleteJobPrivate::jobDebuggingString() const
59{
60 QString itemStr = QStringLiteral("items id: ");
61 bool firstItem = true;
62 for (const Akonadi::Item &item : std::as_const(mItems)) {
63 if (firstItem) {
64 firstItem = false;
65 } else {
66 itemStr += QStringLiteral(", ");
67 }
68 itemStr += QString::number(item.id());
69 }
70
71 return QStringLiteral("Remove %1 from collection id %2").arg(itemStr).arg(mCollection.id());
72}
73
75 : Job(new ItemDeleteJobPrivate(this), parent)
76{
78
79 d->mItems << item;
80 d->mRemainingItems = std::span(d->mItems);
81}
82
84 : Job(new ItemDeleteJobPrivate(this), parent)
85{
87
88 d->mItems = items;
89 d->mRemainingItems = std::span(d->mItems);
90}
91
93 : Job(new ItemDeleteJobPrivate(this), parent)
94{
96
97 d->mCollection = collection;
98}
99
101 : Job(new ItemDeleteJobPrivate(this), parent)
102{
104
105 d->mCurrentTag = tag;
106}
107
109
111{
112 Q_D(const ItemDeleteJob);
113
114 return d->mItems;
115}
116
118{
120
121 try {
122 d->requestBatch();
123 } catch (const Akonadi::Exception &e) {
126 emitResult();
127 return;
128 }
129}
130
131bool ItemDeleteJob::doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
132{
134
135 if (!response->isResponse() || response->type() != Protocol::Command::DeleteItems) {
136 return Job::doHandleResponse(tag, response);
137 }
138
139 if (!d->mRemainingItems.empty()) {
140 return d->requestBatch();
141 }
142
143 return true;
144}
145
146#include "moc_itemdeletejob.cpp"
Represents a collection of PIM items.
Definition collection.h:62
Base class for exceptions used by the Akonadi library.
const char * what() const noexcept override
Returns the error message associated with this exception.
Definition exception.cpp:65
Item::List deletedItems() const
Returns the items passed on in the constructor.
void doStart() override
This method must be reimplemented in the concrete jobs.
bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response) override
This method should be reimplemented in the concrete jobs in case you want to handle incoming data.
ItemDeleteJob(const Item &item, QObject *parent=nullptr)
Creates a new item delete job that deletes item.
~ItemDeleteJob() override
Destroys the item delete job.
Represents a PIM item stored in Akonadi storage.
Definition item.h:100
QList< Item > List
Describes a list of items.
Definition item.h:110
virtual bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
This method should be reimplemented in the concrete jobs in case you want to handle incoming data.
Definition job.cpp:381
Job(QObject *parent=nullptr)
Creates a new job.
Definition job.cpp:290
@ Unknown
Unknown error.
Definition job.h:102
An Akonadi Tag.
Definition tag.h:26
void setErrorText(const QString &errorText)
void emitResult()
void setError(int errorCode)
Helper integration between Akonadi and Qt.
KLEO_EXPORT std::unique_ptr< GpgME::DefaultAssuanTransaction > sendCommand(std::shared_ptr< GpgME::Context > &assuanContext, const std::string &command, GpgME::Error &err)
QObject * parent() const const
QString fromUtf8(QByteArrayView str)
QString number(double n, char format, int precision)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:52:13 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.