Akonadi

tagfetchjob.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "tagfetchjob.h"
8#include "job_p.h"
9#include "protocolhelper_p.h"
10#include "tagfetchscope.h"
11#include <QTimer>
12
13using namespace Akonadi;
14using namespace std::chrono_literals;
15class Akonadi::TagFetchJobPrivate : public JobPrivate
16{
17public:
18 explicit TagFetchJobPrivate(TagFetchJob *parent)
19 : JobPrivate(parent)
20 {
21 }
22
23 void init()
24 {
25 Q_Q(TagFetchJob);
26 mEmitTimer = new QTimer(q);
27 mEmitTimer->setSingleShot(true);
28 mEmitTimer->setInterval(100ms);
29 q->connect(mEmitTimer, &QTimer::timeout, q, [this]() {
30 timeout();
31 });
32 }
33
34 void aboutToFinish() override
35 {
36 timeout();
37 }
38
39 void timeout()
40 {
41 Q_Q(TagFetchJob);
42 mEmitTimer->stop(); // in case we are called by result()
43 if (!mPendingTags.isEmpty()) {
44 if (!q->error()) {
45 Q_EMIT q->tagsReceived(mPendingTags);
46 }
47 mPendingTags.clear();
48 }
49 }
50
51 Q_DECLARE_PUBLIC(TagFetchJob)
52
53 Tag::List mRequestedTags;
54 Tag::List mResultTags;
55 Tag::List mPendingTags; // items pending for emitting itemsReceived()
56 QTimer *mEmitTimer = nullptr;
57 TagFetchScope mFetchScope;
58};
59
61 : Job(new TagFetchJobPrivate(this), parent)
62{
64 d->init();
65}
66
68 : Job(new TagFetchJobPrivate(this), parent)
69{
71 d->init();
72 d->mRequestedTags << tag;
73}
74
76 : Job(new TagFetchJobPrivate(this), parent)
77{
79 d->init();
80 d->mRequestedTags = tags;
81}
82
84 : Job(new TagFetchJobPrivate(this), parent)
85{
87 d->init();
88 for (Tag::Id id : ids) {
89 d->mRequestedTags << Tag(id);
90 }
91}
92
94{
96 d->mFetchScope = fetchScope;
97}
98
100{
102 return d->mFetchScope;
103}
104
106{
108
109 Protocol::FetchTagsCommandPtr cmd;
110 if (d->mRequestedTags.isEmpty()) {
111 cmd = Protocol::FetchTagsCommandPtr::create(Scope());
112 } else {
113 try {
114 cmd = Protocol::FetchTagsCommandPtr::create(ProtocolHelper::entitySetToScope(d->mRequestedTags));
115 } catch (const Exception &e) {
118 emitResult();
119 return;
120 }
121 }
122 cmd->setFetchScope(ProtocolHelper::tagFetchScopeToProtocol(d->mFetchScope));
123
124 d->sendCommand(cmd);
125}
126
127bool TagFetchJob::doHandleResponse(qint64 _tag, const Protocol::CommandPtr &response)
128{
130
131 if (!response->isResponse() || response->type() != Protocol::Command::FetchTags) {
133 }
134
135 const auto &resp = Protocol::cmdCast<Protocol::FetchTagsResponse>(response);
136 // Invalid tag in response marks the last response
137 if (resp.id() < 0) {
138 return true;
139 }
140
141 const Tag tag = ProtocolHelper::parseTagFetchResult(resp);
142 d->mResultTags.append(tag);
143 d->mPendingTags.append(tag);
144 if (!d->mEmitTimer->isActive()) {
145 d->mEmitTimer->start();
146 }
147
148 return false;
149}
150
152{
153 Q_D(const TagFetchJob);
154 return d->mResultTags;
155}
156
157#include "moc_tagfetchjob.cpp"
158#include <chrono>
159
160using namespace std::chrono_literals;
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
Base class for all actions in the Akonadi storage.
Definition job.h:81
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
@ Unknown
Unknown error.
Definition job.h:102
Job that fetches tags from the Akonadi storage.
Definition tagfetchjob.h:29
Tag::List tags() const
Returns the fetched tags after the job has been completed.
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.
TagFetchScope & fetchScope()
Returns the tag fetch scope.
TagFetchJob(QObject *parent=nullptr)
Constructs a new tag fetch job that retrieves all tags stored in Akonadi.
void setFetchScope(const TagFetchScope &fetchScope)
Sets the tag fetch scope.
Specifies which parts of a tag should be fetched from the Akonadi storage.
An Akonadi Tag.
Definition tag.h:26
void setErrorText(const QString &errorText)
void emitResult()
void setError(int errorCode)
Helper integration between Akonadi and Qt.
void clear()
bool isEmpty() const const
T qobject_cast(QObject *object)
QString fromUtf8(QByteArrayView str)
void setInterval(int msec)
void setSingleShot(bool singleShot)
void stop()
void timeout()
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:07:19 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.