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 
33 {
34  if (mConflictType == LocalLocalConflict || mConflictType == LocalRemoteConflict) {
35  auto job = new ItemFetchJob(mConflictingItem, mSession);
36  job->fetchScope().fetchFullPayload();
37  job->fetchScope().setAncestorRetrieval(ItemFetchScope::Parent);
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  auto 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  auto 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  auto 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 result(KJob *job)
Job that creates a new item in the Akonadi storage.
Definition: itemcreatejob.h:60
Q_SCRIPTABLE Q_NOREPLY void start()
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
Job that modifies an existing item in the Akonadi storage.
Definition: itemmodifyjob.h:81
QString i18n(const char *text, const TYPE &arg...)
A communication session with the Akonadi storage.
Definition: core/session.h:55
QString errorText() const
QueuedConnection
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
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)
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:69
int error() const
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
@ Parent
Only retrieve the immediate parent collection.
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:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.