File: history_p.cpp

package info (click to toggle)
akonadi-calendar 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,808 kB
  • sloc: cpp: 14,079; xml: 36; makefile: 8; sh: 4
file content (375 lines) | stat: -rw-r--r-- 12,005 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
  Copyright (C) 2010-2012 Sérgio Martins <iamsergio@gmail.com>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "history_p.h"
#include "akonadicalendar_debug.h"
#include <kcalutils/stringify.h>
#include <KLocalizedString>

Entry::Entry(const Akonadi::Item &item,
             const QString &description,
             History *qq)
    : QObject()
{
    mItems << item;
    init(description, qq);
}

Entry::Entry(const Akonadi::Item::List &items,
             const QString &description,
             History *qq)
    : QObject(), mItems(items)
{
    init(description, qq);
}

void Entry::init(const QString &description, History *qq)
{
    mDescription = description;
    q = qq;
    mChanger = qq->d->mChanger;
}

QWidget *Entry::currentParent() const
{
    return q->d->mCurrentParent;
}

void Entry::updateIds(Item::Id oldId, Item::Id newId)
{
    Q_ASSERT(newId != -1);
    Q_ASSERT(oldId != newId);

    Akonadi::Item::List::iterator it = mItems.begin();
    while (it != mItems.end()) {
        if ((*it).id() == oldId) {
            (*it).setId(newId);
            (*it).setRevision(0);
        }
        ++it;
    }
}

void Entry::updateIdsGlobaly(Item::Id oldId, Item::Id newId)
{
    q->d->updateIds(oldId, newId);
}

void Entry::doIt(OperationType type)
{
    bool result = false;
    mChangeIds.clear();
    if (type == TypeRedo) {
        result = redo();
    } else if (type == TypeUndo) {
        result = undo();
    } else {
        Q_ASSERT(false);
    }

    if (!result) {
        emit finished(IncidenceChanger::ResultCodeJobError, i18n("General error"));
    }
}

CreationEntry::CreationEntry(const Akonadi::Item &item, const QString &description,
                             History *q)
    : Entry(item, description, q)
{
    mLatestRevisionByItemId.insert(item.id(), item.revision());
    Q_ASSERT(mItems.count() == 1);
    const Incidence::Ptr incidence = mItems.first().payload<KCalCore::Incidence::Ptr>();
    if (mDescription.isEmpty()) {
        mDescription = i18nc("%1 is event, todo or journal", "%1 creation",
                             KCalUtils::Stringify::incidenceType(incidence->type()));
    }
    connect(mChanger, &IncidenceChanger::createFinished, this, &CreationEntry::onCreateFinished);
    connect(mChanger, &IncidenceChanger::deleteFinished, this, &CreationEntry::onDeleteFinished);
}

bool CreationEntry::undo()
{
    const int changeId = mChanger->deleteIncidence(mItems.first(), currentParent());
    mChangeIds << changeId;

    if (changeId == -1) {
        qCritical() << "Undo failed";
    }

    return changeId != -1;
}

bool CreationEntry::redo()
{
    Akonadi::Item item = mItems.first();
    Q_ASSERT(item.hasPayload<KCalCore::Incidence::Ptr>());
    const int changeId = mChanger->createIncidence(item.payload<KCalCore::Incidence::Ptr>(),
                         Collection(item.storageCollectionId()),
                         currentParent());
    mChangeIds << changeId;

    if (changeId == -1) {
        qCritical() << "Redo failed";
    }

    return changeId != -1;
}

void CreationEntry::onDeleteFinished(int changeId, const QVector<Akonadi::Item::Id> &deletedIds,
                                     Akonadi::IncidenceChanger::ResultCode resultCode,
                                     const QString &errorString)
{
    if (mChangeIds.contains(changeId)) {
        if (resultCode == IncidenceChanger::ResultCodeSuccess) {
            Q_ASSERT(deletedIds.count() == 1);
            mLatestRevisionByItemId.remove(deletedIds.first());   // TODO
        }
        emit finished(resultCode, errorString);
    }
}

void CreationEntry::onCreateFinished(int changeId, const Akonadi::Item &item,
                                     Akonadi::IncidenceChanger::ResultCode resultCode,
                                     const QString &errorString)
{
    if (mChangeIds.contains(changeId)) {
        if (resultCode == IncidenceChanger::ResultCodeSuccess) {
            mLatestRevisionByItemId.insert(item.id(), item.revision());
            Q_ASSERT(mItems.count() == 1);

            if (mItems.first().id() == item.id()) {
                qCWarning(AKONADICALENDAR_LOG) << "Duplicate id. Old= " << mItems.first().id() << item.id();
                Q_ASSERT(false);
            }
            updateIdsGlobaly(mItems.first().id(), item.id());
        }
        emit finished(resultCode, errorString);
    }
}

DeletionEntry::DeletionEntry(const Akonadi::Item::List &items, const QString &description,
                             History *q)
    : Entry(items, description, q)
{
    const Incidence::Ptr incidence = items.first().payload<KCalCore::Incidence::Ptr>();
    if (mDescription.isEmpty()) {
        mDescription = i18nc("%1 is event, todo or journal", "%1 deletion",
                             KCalUtils::Stringify::incidenceType(incidence->type()));
    }
    connect(mChanger, &IncidenceChanger::createFinished, this, &DeletionEntry::onCreateFinished);
    connect(mChanger, &IncidenceChanger::deleteFinished, this, &DeletionEntry::onDeleteFinished);
}

bool DeletionEntry::undo()
{
    mResultCode = IncidenceChanger::ResultCodeSuccess;
    mErrorString.clear();
    const bool useAtomicOperation = mItems.count() > 1 ;
    bool success = true;
    for (const Akonadi::Item &item : qAsConst(mItems)) {
        if (useAtomicOperation) {
            mChanger->startAtomicOperation();
        }

        Q_ASSERT(item.hasPayload<KCalCore::Incidence::Ptr>());
        const int changeId = mChanger->createIncidence(item.payload<KCalCore::Incidence::Ptr>(),
                             Collection(item.storageCollectionId()),
                             currentParent());
        success = (changeId != -1) && success;
        mChangeIds << changeId;
        if (useAtomicOperation) {
            mChanger->endAtomicOperation();
        }

        mOldIdByChangeId.insert(changeId, item.id());
    }
    mNumPendingCreations = mItems.count();
    return success;
}

bool DeletionEntry::redo()
{
    const int changeId = mChanger->deleteIncidences(mItems, currentParent());
    mChangeIds << changeId;

    if (changeId == -1) {
        qCritical() << "Redo failed";
    }

    return changeId != -1;
}

void DeletionEntry::onDeleteFinished(int changeId, const QVector<Akonadi::Item::Id> &deletedIds,
                                     Akonadi::IncidenceChanger::ResultCode resultCode,
                                     const QString &errorString)
{
    if (mChangeIds.contains(changeId)) {
        if (resultCode == IncidenceChanger::ResultCodeSuccess) {
            for (const Akonadi::Item::Id id : deletedIds) {
                mLatestRevisionByItemId.remove(id);    // TODO
            }
        }
        emit finished(resultCode, errorString);
    }
}

void DeletionEntry::onCreateFinished(int changeId, const Akonadi::Item &item,
                                     Akonadi::IncidenceChanger::ResultCode resultCode,
                                     const QString &errorString)
{
    if (mChangeIds.contains(changeId)) {
        if (resultCode == IncidenceChanger::ResultCodeSuccess) {
            updateIdsGlobaly(mOldIdByChangeId.value(changeId), item.id());
            mLatestRevisionByItemId.insert(item.id(), item.revision());
        } else {
            mResultCode = resultCode;
            mErrorString = errorString;
        }
        --mNumPendingCreations;
        mOldIdByChangeId.remove(changeId);
        if (mNumPendingCreations == 0) {
            emit finished(mResultCode, mErrorString);
        }
    }
}

ModificationEntry::ModificationEntry(const Akonadi::Item &item,
                                     const Incidence::Ptr &originalPayload,
                                     const QString &description,
                                     History *q)
    : Entry(item, description, q)
    , mOriginalPayload(originalPayload->clone())
{
    const Incidence::Ptr incidence = mItems.first().payload<KCalCore::Incidence::Ptr>();
    if (mDescription.isEmpty()) {
        mDescription =  i18nc("%1 is event, todo or journal", "%1 modification",
                              KCalUtils::Stringify::incidenceType(incidence->type()));
    }

    connect(mChanger, &IncidenceChanger::modifyFinished, this, &ModificationEntry::onModifyFinished);
}

bool ModificationEntry::undo()
{
    Item oldItem = mItems.first();
    oldItem.setPayload<KCalCore::Incidence::Ptr>(mOriginalPayload);
    const int changeId = mChanger->modifyIncidence(oldItem, Incidence::Ptr(), currentParent());
    mChangeIds << changeId;

    if (changeId == -1) {
        qCritical() << "Undo failed";
    }

    return changeId != -1;
}

bool ModificationEntry::redo()
{
    const int changeId = mChanger->modifyIncidence(mItems.first(), mOriginalPayload,
                         currentParent());
    mChangeIds << changeId;

    if (changeId == -1) {
        qCritical() << "Redo failed";
    }

    return changeId != -1;
}

void ModificationEntry::onModifyFinished(int changeId, const Akonadi::Item &item,
        Akonadi::IncidenceChanger::ResultCode resultCode,
        const QString &errorString)
{
    if (mChangeIds.contains(changeId)) {
        if (resultCode == IncidenceChanger::ResultCodeSuccess) {
            mLatestRevisionByItemId.insert(item.id(), item.revision());
        }
        emit finished(resultCode, errorString);
    }
}

MultiEntry::MultiEntry(int id, const QString &description,
                       History *q)
    : Entry(Item(), description, q)
    , mAtomicOperationId(id)
    , mFinishedEntries(0)
    , mOperationInProgress(TypeNone)

{
}

void MultiEntry::addEntry(const Entry::Ptr &entry)
{
    Q_ASSERT(mOperationInProgress == TypeNone);
    mEntries.append(entry);
    connect(entry.data(), &Entry::finished, this, &MultiEntry::onEntryFinished,
            Qt::UniqueConnection);
}

void MultiEntry::updateIds(Item::Id oldId, Item::Id newId)
{
    const int numberOfEntries(mEntries.count());
    for (int i = 0; i < numberOfEntries; ++i) {
        mEntries.at(i)->updateIds(oldId, newId);
    }
}

bool MultiEntry::undo()
{
    mChanger->startAtomicOperation();
    mOperationInProgress = TypeUndo;
    Q_ASSERT(!mEntries.isEmpty());
    mFinishedEntries = 0;

    const int count = mEntries.count();
    // To undo a batch of changes we iterate in reverse order so we don't violate
    // causality.
    for (int i = count - 1; i >= 0; --i) {
        mEntries[i]->doIt(TypeUndo);
    }

    mChanger->endAtomicOperation();
    return true;
}

bool MultiEntry::redo()
{
    mChanger->startAtomicOperation();
    mOperationInProgress = TypeRedo;
    Q_ASSERT(!mEntries.isEmpty());
    mFinishedEntries = 0;
    for (const Entry::Ptr &entry : qAsConst(mEntries)) {
        entry->doIt(TypeRedo);
    }
    mChanger->endAtomicOperation();
    return true;
}

void MultiEntry::onEntryFinished(Akonadi::IncidenceChanger::ResultCode resultCode,
                                 const QString &errorString)
{
    ++mFinishedEntries;
    if (mFinishedEntries == mEntries.count() ||
            (mFinishedEntries < mEntries.count() &&
             resultCode != IncidenceChanger::ResultCodeSuccess)) {
        mFinishedEntries = mEntries.count(); // we're done
        mOperationInProgress = TypeNone;
        emit finished(resultCode, errorString);
    }
}