Akonadi

relationsync.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 namespace Akonadi
20 {
21 class Item;
22 }
23 
24 #include "relationsync.h"
25 #include "akonadicore_debug.h"
26 #include "itemfetchscope.h"
27 
28 
29 #include "jobs/itemfetchjob.h"
30 #include "jobs/relationfetchjob.h"
31 #include "jobs/relationcreatejob.h"
32 #include "jobs/relationdeletejob.h"
33 
34 
35 using namespace Akonadi;
36 
37 RelationSync::RelationSync(QObject *parent)
38  : Job(parent)
39 {
40 
41 }
42 
43 RelationSync::~RelationSync()
44 {
45 
46 }
47 
48 void RelationSync::setRemoteRelations(const Akonadi::Relation::List &relations)
49 {
50  mRemoteRelations = relations;
51  mRemoteRelationsSet = true;
52  diffRelations();
53 }
54 
55 void RelationSync::doStart()
56 {
58  connect(fetch, &KJob::result, this, &RelationSync::onLocalFetchDone);
59 }
60 
61 void RelationSync::onLocalFetchDone(KJob *job)
62 {
63  Akonadi::RelationFetchJob *fetch = static_cast<Akonadi::RelationFetchJob *>(job);
64  mLocalRelations = fetch->relations();
65  mLocalRelationsFetched = true;
66  diffRelations();
67 }
68 
69 void RelationSync::diffRelations()
70 {
71  if (!mRemoteRelationsSet || !mLocalRelationsFetched) {
72  qCDebug(AKONADICORE_LOG) << "waiting for delivery: " << mRemoteRelationsSet << mLocalRelationsFetched;
73  return;
74  }
75 
77  for (const Akonadi::Relation &localRelation : qAsConst(mLocalRelations)) {
78  if (!localRelation.remoteId().isEmpty()) {
79  relationByRid.insert(localRelation.remoteId(), localRelation);
80  }
81  }
82 
83  for (const Akonadi::Relation &remoteRelation : qAsConst(mRemoteRelations)) {
84  if (relationByRid.contains(remoteRelation.remoteId())) {
85  relationByRid.remove(remoteRelation.remoteId());
86  } else {
87  //New relation or had its GID updated, so create one now
88  RelationCreateJob *createJob = new RelationCreateJob(remoteRelation, this);
89  connect(createJob, &KJob::result, this, &RelationSync::checkDone);
90  }
91  }
92 
93  for (const Akonadi::Relation &removedRelation : qAsConst(relationByRid)) {
94  //Removed remotely, remove locally
95  RelationDeleteJob *removeJob = new RelationDeleteJob(removedRelation, this);
96  connect(removeJob, &KJob::result, this, &RelationSync::checkDone);
97  }
98  checkDone();
99 }
100 
101 void RelationSync::slotResult(KJob *job)
102 {
103  if (job->error()) {
104  qCWarning(AKONADICORE_LOG) << "Error during CollectionSync: " << job->errorString() << job->metaObject()->className();
105  // pretend there were no errors
107  } else {
108  Akonadi::Job::slotResult(job);
109  }
110 }
111 
112 void RelationSync::checkDone()
113 {
114  if (hasSubjobs()) {
115  qCDebug(AKONADICORE_LOG) << "Still going";
116  return;
117  }
118  qCDebug(AKONADICORE_LOG) << "done";
119  emitResult();
120 }
121 
QHash::iterator insert(const Key &key, const T &value)
Relation::List relations() const
Returns the relations.
An Akonadi Relation.
Definition: relation.h:52
void emitResult()
virtual QString errorString() const
virtual const QMetaObject * metaObject() const const
Base class for all actions in the Akonadi storage.
Definition: job.h:93
bool hasSubjobs() const
bool removeSubjob(KJob *job) override
Removes the given subjob of this job.
Definition: job.cpp:375
Job that to fetch relations from Akonadi storage.
Job that deletes a relation in the Akonadi storage.
int remove(const Key &key)
const char * className() const const
Helper integration between Akonadi and Qt.
static const char * GENERIC
The GENERIC type represents a generic relation between two items.
Definition: relation.h:60
Job that creates a new relation in the Akonadi storage.
bool contains(const Key &key) const const
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.