Akonadi

conflicthandler.cpp
1/*
2 SPDX-FileCopyrightText: 2010 KDAB
3 SPDX-FileContributor: Tobias Koenig <tokoe@kde.org>
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
17using namespace Akonadi;
18
19ConflictHandler::ConflictHandler(ConflictType type, QObject *parent)
20 : QObject(parent)
21 , mConflictType(type)
22 , mSession(new Session("conflict handling session", this))
23{
24}
25
26void ConflictHandler::setConflictingItems(const Akonadi::Item &changedItem, const Akonadi::Item &conflictingItem)
27{
28 mChangedItem = changedItem;
29 mConflictingItem = conflictingItem;
30}
31
32void ConflictHandler::start()
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
44void 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
61void ConflictHandler::resolve()
62{
63#pragma message("warning KF6 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
84void 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
97void 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
106void ConflictHandler::useOtherItem()
107{
108 // We can just ignore the local item here and leave everything as it is.
109 Q_EMIT conflictResolved();
110}
111
112void 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
120void 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"
Job that creates a new item in the Akonadi storage.
Job that fetches items from the Akonadi storage.
Job that modifies an existing item in the Akonadi storage.
Represents a PIM item stored in Akonadi storage.
Definition item.h:100
int error() const
QString errorText() const
ASAP CLI session.
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QueuedConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.