Akonadi

tagfetchjob.cpp
1 /*
2  Copyright (c) 2014 Christian Mollekopf <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "tagfetchjob.h"
21 #include "job_p.h"
22 #include "tag.h"
23 #include "protocolhelper_p.h"
24 #include "tagfetchscope.h"
25 #include "attributefactory.h"
26 #include <QTimer>
27 
28 using namespace Akonadi;
29 
30 class Akonadi::TagFetchJobPrivate : public JobPrivate
31 {
32 public:
33  TagFetchJobPrivate(TagFetchJob *parent)
34  : JobPrivate(parent)
35  {
36  }
37 
38  void init()
39  {
40  Q_Q(TagFetchJob);
41  mEmitTimer = new QTimer(q);
42  mEmitTimer->setSingleShot(true);
43  mEmitTimer->setInterval(100);
44  q->connect(mEmitTimer, &QTimer::timeout, q, [this]() { timeout(); });
45  }
46 
47  void aboutToFinish() override {
48  timeout();
49  }
50 
51  void timeout()
52  {
53  Q_Q(TagFetchJob);
54  mEmitTimer->stop(); // in case we are called by result()
55  if (!mPendingTags.isEmpty()) {
56  if (!q->error()) {
57  Q_EMIT q->tagsReceived(mPendingTags);
58  }
59  mPendingTags.clear();
60  }
61  }
62 
63  Q_DECLARE_PUBLIC(TagFetchJob)
64 
65  Tag::List mRequestedTags;
66  Tag::List mResultTags;
67  Tag::List mPendingTags; // items pending for emitting itemsReceived()
68  QTimer *mEmitTimer = nullptr;
69  TagFetchScope mFetchScope;
70 };
71 
73  : Job(new TagFetchJobPrivate(this), parent)
74 {
75  Q_D(TagFetchJob);
76  d->init();
77 }
78 
79 TagFetchJob::TagFetchJob(const Tag &tag, QObject *parent)
80  : Job(new TagFetchJobPrivate(this), parent)
81 {
82  Q_D(TagFetchJob);
83  d->init();
84  d->mRequestedTags << tag;
85 }
86 
88  : Job(new TagFetchJobPrivate(this), parent)
89 {
90  Q_D(TagFetchJob);
91  d->init();
92  d->mRequestedTags = tags;
93 }
94 
96  : Job(new TagFetchJobPrivate(this), parent)
97 {
98  Q_D(TagFetchJob);
99  d->init();
100  for (Tag::Id id : ids) {
101  d->mRequestedTags << Tag(id);
102  }
103 }
104 
106 {
107  Q_D(TagFetchJob);
108  d->mFetchScope = fetchScope;
109 }
110 
112 {
113  Q_D(TagFetchJob);
114  return d->mFetchScope;
115 }
116 
118 {
119  Q_D(TagFetchJob);
120 
121  Protocol::FetchTagsCommandPtr cmd;
122  if (d->mRequestedTags.isEmpty()) {
123  cmd = Protocol::FetchTagsCommandPtr::create(Scope(ImapInterval(1, 0)));
124  } else {
125  try {
126  cmd = Protocol::FetchTagsCommandPtr::create(ProtocolHelper::entitySetToScope(d->mRequestedTags));
127  } catch (const Exception &e) {
130  emitResult();
131  return;
132  }
133  }
134  cmd->setFetchScope(ProtocolHelper::tagFetchScopeToProtocol(d->mFetchScope));
135 
136  d->sendCommand(cmd);
137 }
138 
139 bool TagFetchJob::doHandleResponse(qint64 _tag, const Protocol::CommandPtr &response)
140 {
141  Q_D(TagFetchJob);
142 
143  if (!response->isResponse() || response->type() != Protocol::Command::FetchTags) {
144  return Job::doHandleResponse(_tag, response);
145  }
146 
147  const auto &resp = Protocol::cmdCast<Protocol::FetchTagsResponse>(response);
148  // Invalid tag in response marks the last response
149  if (resp.id() < 0) {
150  return true;
151  }
152 
153  const Tag tag = ProtocolHelper::parseTagFetchResult(resp);
154  d->mResultTags.append(tag);
155  d->mPendingTags.append(tag);
156  if (!d->mEmitTimer->isActive()) {
157  d->mEmitTimer->start();
158  }
159 
160  return false;
161 }
162 
164 {
165  Q_D(const TagFetchJob);
166  return d->mResultTags;
167 }
168 
169 #include "moc_tagfetchjob.cpp"
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:385
void emitResult()
Specifies which parts of a tag should be fetched from the Akonadi storage.
Definition: tagfetchscope.h:35
Unknown error.
Definition: job.h:115
void setError(int errorCode)
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...
Base class for all actions in the Akonadi storage.
Definition: job.h:93
void timeout()
void setErrorText(const QString &errorText)
QString fromUtf8(const char *str, int size)
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.
QCA_EXPORT void init()
const char * what() const noexcept override
Returns the error message associated with this exception.
Definition: exception.cpp:75
Helper integration between Akonadi and Qt.
Base class for exceptions used by the Akonadi library.
Definition: exceptionbase.h:43
An Akonadi Tag.
Definition: tag.h:39
void setFetchScope(const TagFetchScope &fetchScope)
Sets the tag fetch scope.
TagFetchScope & fetchScope()
Returns the tag fetch scope.
QObject * parent() const const
TagFetchJob(QObject *parent=nullptr)
Constructs a new tag fetch job that retrieves all tags stored in Akonadi.
Definition: tagfetchjob.cpp:72
Job that fetches tags from the Akonadi storage.
Definition: tagfetchjob.h:43
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jun 6 2020 23:09:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.