Akonadi

relationfetchjob.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 "relationfetchjob.h"
21 #include "job_p.h"
22 #include "relation.h"
23 #include "protocolhelper_p.h"
24 #include "private/protocol_p.h"
25 #include <QTimer>
26 
27 using namespace Akonadi;
28 
29 class Akonadi::RelationFetchJobPrivate : public JobPrivate
30 {
31 public:
32  RelationFetchJobPrivate(RelationFetchJob *parent)
33  : JobPrivate(parent)
34  , mEmitTimer(nullptr)
35  {
36  }
37 
38  void init()
39  {
40  Q_Q(RelationFetchJob);
41  mEmitTimer = new QTimer(q);
42  mEmitTimer->setSingleShot(true);
43  mEmitTimer->setInterval(100);
44  q->connect(mEmitTimer, SIGNAL(timeout()), q, SLOT(timeout()));
45  }
46 
47  void aboutToFinish() override {
48  timeout();
49  }
50 
51  void timeout()
52  {
53  Q_Q(RelationFetchJob);
54  mEmitTimer->stop(); // in case we are called by result()
55  if (!mPendingRelations.isEmpty()) {
56  if (!q->error()) {
57  Q_EMIT q->relationsReceived(mPendingRelations);
58  }
59  mPendingRelations.clear();
60  }
61  }
62 
63  Q_DECLARE_PUBLIC(RelationFetchJob)
64 
65  Relation::List mResultRelations;
66  Relation::List mPendingRelations; // relation pending for emitting itemsReceived()
67  QTimer *mEmitTimer = nullptr;
68  QVector<QByteArray> mTypes;
69  QString mResource;
70  Relation mRequestedRelation;
71 };
72 
74  : Job(new RelationFetchJobPrivate(this), parent)
75 {
76  Q_D(RelationFetchJob);
77  d->init();
78  d->mRequestedRelation = relation;
79 }
80 
82  : Job(new RelationFetchJobPrivate(this), parent)
83 {
84  Q_D(RelationFetchJob);
85  d->init();
86  d->mTypes = types;
87 }
88 
90 {
91  Q_D(RelationFetchJob);
92 
93  d->sendCommand(Protocol::FetchRelationsCommandPtr::create(
94  d->mRequestedRelation.left().id(),
95  d->mRequestedRelation.right().id(),
96  (d->mTypes.isEmpty() && !d->mRequestedRelation.type().isEmpty()) ? QVector<QByteArray>() << d->mRequestedRelation.type() : d->mTypes,
97  d->mResource));
98 }
99 
101 {
102  Q_D(RelationFetchJob);
103 
104  if (!response->isResponse() || response->type() != Protocol::Command::FetchRelations) {
105  return Job::doHandleResponse(tag, response);
106  }
107 
108  const Relation rel = ProtocolHelper::parseRelationFetchResult(
109  Protocol::cmdCast<Protocol::FetchRelationsResponse>(response));
110  // Invalid response means there will be no more responses
111  if (!rel.isValid()) {
112  return true;
113  }
114 
115  d->mResultRelations.append(rel);
116  d->mPendingRelations.append(rel);
117  if (!d->mEmitTimer->isActive()) {
118  d->mEmitTimer->start();
119  }
120  return false;
121 }
122 
124 {
125  Q_D(const RelationFetchJob);
126  return d->mResultRelations;
127 }
128 
129 void RelationFetchJob::setResource(const QString &identifier)
130 {
131  Q_D(RelationFetchJob);
132  d->mResource = identifier;
133 }
134 
135 #include "moc_relationfetchjob.cpp"
Relation::List relations() const
Returns the relations.
An Akonadi Relation.
Definition: relation.h:52
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
RelationFetchJob(const Relation &relation, QObject *parent=nullptr)
Creates a new relation fetch job.
Base class for all actions in the Akonadi storage.
Definition: job.h:93
QUICKADDONS_EXPORT void init()
Job that to fetch relations from Akonadi storage.
void doStart() override
This method must be reimplemented in the concrete jobs.
Definition: item.h:44
Helper integration between Akonadi and Qt.
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...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.