7#include "itemfetchjob.h"
9#include "attributefactory.h"
10#include "collection.h"
11#include "itemfetchscope.h"
13#include "private/protocol_p.h"
14#include "protocolhelper_p.h"
16#include "tagfetchscope.h"
22class Akonadi::ItemFetchJobPrivate :
public JobPrivate
25 explicit ItemFetchJobPrivate(ItemFetchJob *parent)
27 , mCollection(Collection::root())
29 mEmitTimer.setSingleShot(
true);
30 mEmitTimer.setInterval(std::chrono::milliseconds{100});
33 ~ItemFetchJobPrivate()
override
44 void aboutToFinish()
override
54 if (!mPendingItems.isEmpty()) {
56 Q_EMIT q->itemsReceived(mPendingItems);
58 mPendingItems.clear();
62 QString jobDebuggingString()
const override
64 if (mRequestedItems.isEmpty()) {
65 QString str = QStringLiteral(
"All items from collection %1").arg(mCollection.id());
66 if (mFetchScope.fetchChangedSince().isValid()) {
67 str += QStringLiteral(
" changed since %1").
arg(mFetchScope.fetchChangedSince().toString());
71 }
else if (mRequestedItems.size() > 10) {
72 return QStringLiteral(
"%1 items from collection %2").arg(mRequestedItems.size()).arg(mCollection.id());
76 QString itemStr = QStringLiteral(
"items id: ");
77 bool firstItem =
true;
78 for (
const Akonadi::Item &item : std::as_const(mRequestedItems)) {
82 itemStr += QStringLiteral(
", ");
85 const Akonadi::Collection parentCollection = item.parentCollection();
86 if (parentCollection.isValid()) {
87 itemStr += QStringLiteral(
" from collection %1").
arg(parentCollection.id());
92 }
catch (
const Exception &e) {
98 Q_DECLARE_PUBLIC(ItemFetchJob)
100 Collection mCollection;
104 ItemFetchScope mFetchScope;
107 ProtocolHelperValuePool *mValuePool =
nullptr;
108 ItemFetchJob::DeliveryOptions mDeliveryOptions = ItemFetchJob::Default;
111 Protocol::FetchLimit mItemsLimit;
115 :
Job(new ItemFetchJobPrivate(this),
parent)
120 d->mCollection = collection;
121 d->mValuePool =
new ProtocolHelperValuePool;
125 :
Job(new ItemFetchJobPrivate(this),
parent)
130 d->mRequestedItems.append(item);
134 :
Job(new ItemFetchJobPrivate(this),
parent)
139 d->mRequestedItems =
items;
143 :
Job(new ItemFetchJobPrivate(this),
parent)
148 d->mRequestedItems.reserve(
items.size());
149 for (
auto id :
items) {
150 d->mRequestedItems.append(
Item(
id));
154 :
Job(new ItemFetchJobPrivate(this),
parent)
159 d->mCurrentTag = tag;
160 d->mValuePool =
new ProtocolHelperValuePool;
169 constexpr int maximumParametersSize = 1000;
170 const auto items = d->mRequestedItems.mid(d->mChunkStart, maximumParametersSize);
171 d->mChunkStart += maximumParametersSize;
174 d->sendCommand(Protocol::FetchItemsCommandPtr::create(
items.isEmpty() ? Scope() : ProtocolHelper::entitySetToScope(
items),
175 ProtocolHelper::commandContextToProtocol(d->mCollection, d->mCurrentTag,
items),
176 ProtocolHelper::itemFetchScopeToProtocol(d->mFetchScope),
177 ProtocolHelper::tagFetchScopeToProtocol(d->mFetchScope.tagFetchScope()),
191 if (!response->isResponse() || response->type() != Protocol::Command::FetchItems) {
195 const auto &resp = Protocol::cmdCast<Protocol::FetchItemsResponse>(response);
199 if (d->mChunkStart < d->mRequestedItems.size()) {
208 const Item item = ProtocolHelper::parseItemFetchResult(resp,
nullptr, d->mValuePool);
216 d->mResultItems.append(item);
220 d->mPendingItems.append(item);
221 if (!d->mEmitTimer.isActive()) {
222 d->mEmitTimer.start();
235 return d->mResultItems;
242 d->mResultItems.clear();
256 return d->mFetchScope;
263 d->mCollection = collection;
270 d->mDeliveryOptions = options;
277 return d->mDeliveryOptions;
290 d->mItemsLimit.setLimit(limit);
291 d->mItemsLimit.setLimitOffset(
start);
292 d->mItemsLimit.setSortOrder(order);
294#include "moc_itemfetchjob.cpp"
Represents a collection of PIM items.
Base class for exceptions used by the Akonadi library.
const char * what() const noexcept override
Returns the error message associated with this exception.
int count() const
Returns the total number of retrieved items.
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.
void itemsReceived(const Akonadi::Item::List &items)
This signal is emitted whenever new items have been fetched completely.
void setDeliveryOption(DeliveryOptions options)
Sets the mechanisms by which the items should be fetched.
ItemFetchJob(const Collection &collection, QObject *parent=nullptr)
Creates a new item fetch job that retrieves all items inside the given collection.
void setFetchScope(const ItemFetchScope &fetchScope)
Sets the item fetch scope.
void setLimit(int limit, int start, Qt::SortOrder order=Qt::DescendingOrder)
Sets the limit of fetched items.
DeliveryOptions deliveryOptions() const
Returns the delivery options.
void setCollection(const Collection &collection)
Specifies the collection the item is in.
void doStart() override
This method must be reimplemented in the concrete jobs.
@ EmitItemsInBatches
emitted via signal in bulk (collected and emitted delayed via timer)
@ ItemGetter
items available through items()
@ EmitItemsIndividually
emitted via signal upon reception
ItemFetchScope & fetchScope()
Returns the item fetch scope.
void clearItems()
Save memory by clearing the fetched items.
~ItemFetchJob() override
Destroys the item fetch job.
Item::List items() const
Returns the fetched items.
Specifies which parts of an item should be fetched from the Akonadi storage.
Represents a PIM item stored in Akonadi storage.
bool isValid() const
Returns whether the item is valid.
QList< Item > List
Describes a list of items.
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.
Job(QObject *parent=nullptr)
Creates a new job.
void start() override
Jobs are started automatically once entering the event loop again, no need to explicitly call this.
void setErrorText(const QString &errorText)
void setError(int errorCode)
Helper integration between Akonadi and Qt.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString arg(Args &&... args) const const
QString fromUtf8(QByteArrayView str)
QString number(double n, char format, int precision)