Akonadi

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

KDE's Doxygen guidelines are available online.