Akonadi

relationsync.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6namespace Akonadi
7{
8class Item;
9}
10
11#include "relationsync.h"
12#include "akonadicore_debug.h"
13
14#include "jobs/relationcreatejob.h"
15#include "jobs/relationdeletejob.h"
16#include "jobs/relationfetchjob.h"
17
18using namespace Akonadi;
19
20RelationSync::RelationSync(QObject *parent)
21 : Job(parent)
22{
23}
24
25RelationSync::~RelationSync()
26{
27}
28
29void RelationSync::setRemoteRelations(const Akonadi::Relation::List &relations)
30{
31 mRemoteRelations = relations;
32 mRemoteRelationsSet = true;
33 diffRelations();
34}
35
36void RelationSync::doStart()
37{
39 connect(fetch, &KJob::result, this, &RelationSync::onLocalFetchDone);
40}
41
42void RelationSync::onLocalFetchDone(KJob *job)
43{
44 auto fetch = static_cast<Akonadi::RelationFetchJob *>(job);
45 mLocalRelations = fetch->relations();
46 mLocalRelationsFetched = true;
47 diffRelations();
48}
49
50void RelationSync::diffRelations()
51{
52 if (!mRemoteRelationsSet || !mLocalRelationsFetched) {
53 qCDebug(AKONADICORE_LOG) << "waiting for delivery: " << mRemoteRelationsSet << mLocalRelationsFetched;
54 return;
55 }
56
58 for (const Akonadi::Relation &localRelation : std::as_const(mLocalRelations)) {
59 if (!localRelation.remoteId().isEmpty()) {
60 relationByRid.insert(localRelation.remoteId(), localRelation);
61 }
62 }
63
64 for (const Akonadi::Relation &remoteRelation : std::as_const(mRemoteRelations)) {
65 if (relationByRid.contains(remoteRelation.remoteId())) {
66 relationByRid.remove(remoteRelation.remoteId());
67 } else {
68 // New relation or had its GID updated, so create one now
69 auto createJob = new RelationCreateJob(remoteRelation, this);
70 connect(createJob, &KJob::result, this, &RelationSync::checkDone);
71 }
72 }
73
75 // Removed remotely, remove locally
76 auto removeJob = new RelationDeleteJob(removedRelation, this);
77 connect(removeJob, &KJob::result, this, &RelationSync::checkDone);
78 }
79 checkDone();
80}
81
82void RelationSync::slotResult(KJob *job)
83{
84 if (job->error()) {
85 qCWarning(AKONADICORE_LOG) << "Error during CollectionSync: " << job->errorString() << job->metaObject()->className();
86 // pretend there were no errors
88 } else {
89 Akonadi::Job::slotResult(job);
90 }
91}
92
93void RelationSync::checkDone()
94{
95 if (hasSubjobs()) {
96 qCDebug(AKONADICORE_LOG) << "Still going";
97 return;
98 }
99 qCDebug(AKONADICORE_LOG) << "done";
100 emitResult();
101}
102
103#include "moc_relationsync.cpp"
Base class for all actions in the Akonadi storage.
Definition job.h:81
bool removeSubjob(KJob *job) override
Removes the given subjob of this job.
Definition job.cpp:369
Job that creates a new relation in the Akonadi storage.
Job that deletes a relation in the Akonadi storage.
Job that to fetch relations from Akonadi storage.
An Akonadi Relation.
Definition relation.h:41
static const char * GENERIC
The GENERIC type represents a generic relation between two items.
Definition relation.h:48
bool hasSubjobs() const
virtual QString errorString() const
void emitResult()
int error() const
void result(KJob *job)
Helper integration between Akonadi and Qt.
A glue between Qt and the standard library.
const char * className() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual const QMetaObject * metaObject() const const
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:07:19 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.