Akonadi

relationfetchjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Christian Mollekopf <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "relationfetchjob.h"
8 #include "job_p.h"
9 #include "private/protocol_p.h"
10 #include "protocolhelper_p.h"
11 #include <QTimer>
12 
13 using namespace Akonadi;
14 
15 class Akonadi::RelationFetchJobPrivate : public JobPrivate
16 {
17 public:
18  explicit RelationFetchJobPrivate(RelationFetchJob *parent)
19  : JobPrivate(parent)
20  {
21  mEmitTimer.setSingleShot(true);
22  mEmitTimer.setInterval(std::chrono::milliseconds{100});
23  }
24 
25  void init()
26  {
27  QObject::connect(&mEmitTimer, &QTimer::timeout, q_ptr, [this]() {
28  timeout();
29  });
30  }
31 
32  void aboutToFinish() override
33  {
34  timeout();
35  }
36 
37  void timeout()
38  {
39  Q_Q(RelationFetchJob);
40  mEmitTimer.stop(); // in case we are called by result()
41  if (!mPendingRelations.isEmpty()) {
42  if (!q->error()) {
43  Q_EMIT q->relationsReceived(mPendingRelations);
44  }
45  mPendingRelations.clear();
46  }
47  }
48 
49  Q_DECLARE_PUBLIC(RelationFetchJob)
50 
51  Relation::List mResultRelations;
52  Relation::List mPendingRelations; // relation pending for emitting itemsReceived()
53  QTimer mEmitTimer;
54  QVector<QByteArray> mTypes;
55  QString mResource;
56  Relation mRequestedRelation;
57 };
58 
60  : Job(new RelationFetchJobPrivate(this), parent)
61 {
63  d->init();
64  d->mRequestedRelation = relation;
65 }
66 
68  : Job(new RelationFetchJobPrivate(this), parent)
69 {
71  d->init();
72  d->mTypes = types;
73 }
74 
76 {
78 
79  d->sendCommand(Protocol::FetchRelationsCommandPtr::create(
80  d->mRequestedRelation.left().id(),
81  d->mRequestedRelation.right().id(),
82  (d->mTypes.isEmpty() && !d->mRequestedRelation.type().isEmpty()) ? QVector<QByteArray>() << d->mRequestedRelation.type() : d->mTypes,
83  d->mResource));
84 }
85 
87 {
89 
90  if (!response->isResponse() || response->type() != Protocol::Command::FetchRelations) {
91  return Job::doHandleResponse(tag, response);
92  }
93 
94  const Relation rel = ProtocolHelper::parseRelationFetchResult(Protocol::cmdCast<Protocol::FetchRelationsResponse>(response));
95  // Invalid response means there will be no more responses
96  if (!rel.isValid()) {
97  return true;
98  }
99 
100  d->mResultRelations.append(rel);
101  d->mPendingRelations.append(rel);
102  if (!d->mEmitTimer.isActive()) {
103  d->mEmitTimer.start();
104  }
105  return false;
106 }
107 
109 {
110  Q_D(const RelationFetchJob);
111  return d->mResultRelations;
112 }
113 
114 void RelationFetchJob::setResource(const QString &identifier)
115 {
117  d->mResource = identifier;
118 }
119 
120 #include "moc_relationfetchjob.cpp"
void doStart() override
This method must be reimplemented in the concrete jobs.
QCA_EXPORT void init()
An Akonadi Relation.
Definition: relation.h:39
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Job that to fetch relations from Akonadi storage.
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 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
Relation::List relations() const
Returns the relations.
RelationFetchJob(const Relation &relation, QObject *parent=nullptr)
Creates a new relation fetch job.
Q_D(Todo)
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.