Akonadi

conflicthandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 KDAB
3  SPDX-FileContributor: Tobias Koenig <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "conflicthandler_p.h"
9 
10 #include "itemcreatejob.h"
11 #include "itemfetchjob.h"
12 #include "itemfetchscope.h"
13 #include "itemmodifyjob.h"
14 #include "session.h"
15 #include <KLocalizedString>
16 
17 using namespace Akonadi;
18 
19 ConflictHandler::ConflictHandler(ConflictType type, QObject *parent)
20  : QObject(parent)
21  , mConflictType(type)
22  , mSession(new Session("conflict handling session", this))
23 {
24 }
25 
26 void ConflictHandler::setConflictingItems(const Akonadi::Item &changedItem, const Akonadi::Item &conflictingItem)
27 {
28  mChangedItem = changedItem;
29  mConflictingItem = conflictingItem;
30 }
31 
32 void ConflictHandler::start()
33 {
34  if (mConflictType == LocalLocalConflict || mConflictType == LocalRemoteConflict) {
35  ItemFetchJob *job = new ItemFetchJob(mConflictingItem, mSession);
38  connect(job, &ItemFetchJob::result, this, &ConflictHandler::slotOtherItemFetched);
39  } else {
40  resolve();
41  }
42 }
43 
44 void ConflictHandler::slotOtherItemFetched(KJob *job)
45 {
46  if (job->error()) {
47  Q_EMIT error(job->errorText()); //TODO: extend error message
48  return;
49  }
50 
51  ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
52  if (fetchJob->items().isEmpty()) {
53  Q_EMIT error(i18n("Did not find other item for conflict handling"));
54  return;
55  }
56 
57  mConflictingItem = fetchJob->items().at(0);
58  QMetaObject::invokeMethod(this, &ConflictHandler::resolve, Qt::QueuedConnection);
59 }
60 
61 void ConflictHandler::resolve()
62 {
63 #pragma message ("warning KF5 Port me!")
64 #if 0
65  ConflictResolveDialog dlg;
66  dlg.setConflictingItems(mChangedItem, mConflictingItem);
67  dlg.exec();
68 
69  const ResolveStrategy strategy = dlg.resolveStrategy();
70  switch (strategy) {
71  case UseLocalItem:
72  useLocalItem();
73  break;
74  case UseOtherItem:
75  useOtherItem();
76  break;
77  case UseBothItems:
78  useBothItems();
79  break;
80  }
81 #endif
82 }
83 
84 void ConflictHandler::useLocalItem()
85 {
86  // We have to overwrite the other item inside the Akonadi storage with the local
87  // item. To make this happen, we have to set the revision of the local item to
88  // the one of the other item to let the Akonadi server accept it.
89 
90  Item newItem(mChangedItem);
91  newItem.setRevision(mConflictingItem.revision());
92 
93  ItemModifyJob *job = new ItemModifyJob(newItem, mSession);
94  connect(job, &ItemModifyJob::result, this, &ConflictHandler::slotUseLocalItemFinished);
95 }
96 
97 void ConflictHandler::slotUseLocalItemFinished(KJob *job)
98 {
99  if (job->error()) {
100  Q_EMIT error(job->errorText()); //TODO: extend error message
101  } else {
102  Q_EMIT conflictResolved();
103  }
104 }
105 
106 void ConflictHandler::useOtherItem()
107 {
108  // We can just ignore the local item here and leave everything as it is.
109  Q_EMIT conflictResolved();
110 }
111 
112 void ConflictHandler::useBothItems()
113 {
114  // We have to create a new item for the local item under the collection that has
115  // been retrieved when we fetched the other item.
116  ItemCreateJob *job = new ItemCreateJob(mChangedItem, mConflictingItem.parentCollection(), mSession);
117  connect(job, &ItemCreateJob::result, this, &ConflictHandler::slotUseBothItemsFinished);
118 }
119 
120 void ConflictHandler::slotUseBothItemsFinished(KJob *job)
121 {
122  if (job->error()) {
123  Q_EMIT error(job->errorText()); //TODO: extend error message
124  } else {
125  Q_EMIT conflictResolved();
126  }
127 }
128 
129 #include "moc_conflicthandler_p.cpp"
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Item::List items() const
Returns the fetched items.
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Only retrieve the immediate parent collection.
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
A communication session with the Akonadi storage.
Definition: core/session.h:54
Job that creates a new item in the Akonadi storage.
Definition: itemcreatejob.h:62
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
Job that modifies an existing item in the Akonadi storage.
Definition: itemmodifyjob.h:83
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:71
void result(KJob *job)
QueuedConnection
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
Q_EMITQ_EMIT
QString errorText() const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 23:16:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.