Akonadi

relationfetchjob.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 "relationfetchjob.h"
8#include "job_p.h"
9#include "private/protocol_p.h"
10#include "protocolhelper_p.h"
11#include <QTimer>
12
13using namespace Akonadi;
14
15class Akonadi::RelationFetchJobPrivate : public JobPrivate
16{
17public:
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 {
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 QList<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()) ? QList<QByteArray>() << d->mRequestedRelation.type() : d->mTypes,
83 d->mResource));
84}
85
87{
89
90 if (!response->isResponse() || response->type() != Protocol::Command::FetchRelations) {
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
114void RelationFetchJob::setResource(const QString &identifier)
115{
117 d->mResource = identifier;
118}
119
120#include "moc_relationfetchjob.cpp"
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
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.
Relation::List relations() const
Returns the relations.
void doStart() override
This method must be reimplemented in the concrete jobs.
RelationFetchJob(const Relation &relation, QObject *parent=nullptr)
Creates a new relation fetch job.
An Akonadi Relation.
Definition relation.h:41
Helper integration between Akonadi and Qt.
void clear()
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
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 Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.