Akonadi

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

KDE's Doxygen guidelines are available online.