File: history.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 (351 lines) | stat: -rw-r--r-- 10,742 bytes parent folder | download
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
/*
  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.h"
#include "history_p.h"
#include "akonadicalendar_debug.h"
#include <kcalutils/stringify.h>

using namespace KCalCore;
using namespace Akonadi;

History::History(QObject *parent)
    : QObject(parent)
    , d(new Private(this))
{
}

History::~History()
{
    delete d;
}

History::Private::Private(History *qq)
    : mChanger(new IncidenceChanger(/*history=*/false, qq))
    , mOperationTypeInProgress(TypeNone)
    , mUndoAllInProgress(false)
    , mEnabled(true)
    , q(qq)
{
    mChanger->setObjectName(QStringLiteral("changer"));   // for auto-connects
}

void History::recordCreation(const Akonadi::Item &item,
                             const QString &description,
                             const uint atomicOperationId)
{
    Q_ASSERT_X(item.isValid(), "History::recordCreation()",
               "Item must be valid.");

    Q_ASSERT_X(item.hasPayload<KCalCore::Incidence::Ptr>(), "History::recordCreation()",
               "Item must have Incidence::Ptr payload.");

    Entry::Ptr entry(new CreationEntry(item, description, this));

    d->stackEntry(entry, atomicOperationId);
}

void History::recordModification(const Akonadi::Item &oldItem,
                                 const Akonadi::Item &newItem,
                                 const QString &description,
                                 const uint atomicOperationId)
{
    Q_ASSERT_X(oldItem.isValid(), "History::recordModification", "old item must be valid");
    Q_ASSERT_X(newItem.isValid(), "History::recordModification", "newItem item must be valid");
    Q_ASSERT_X(oldItem.hasPayload<KCalCore::Incidence::Ptr>(), "History::recordModification",
               "old item must have Incidence::Ptr payload");
    Q_ASSERT_X(newItem.hasPayload<KCalCore::Incidence::Ptr>(), "History::recordModification",
               "newItem item must have Incidence::Ptr payload");

    Entry::Ptr entry(new ModificationEntry(newItem, oldItem.payload<KCalCore::Incidence::Ptr>(),
                                           description, this));

    Q_ASSERT(newItem.revision() >= oldItem.revision());

    d->stackEntry(entry, atomicOperationId);
}

void History::recordDeletion(const Akonadi::Item &item,
                             const QString &description,
                             const uint atomicOperationId)
{
    Q_ASSERT_X(item.isValid(), "History::recordDeletion", "Item must be valid");
    Item::List list;
    list.append(item);
    recordDeletions(list, description, atomicOperationId);
}

void History::recordDeletions(const Akonadi::Item::List &items,
                              const QString &description,
                              const uint atomicOperationId)
{
    Entry::Ptr entry(new DeletionEntry(items, description, this));

    for (const Akonadi::Item &item : items) {
        Q_UNUSED(item);
        Q_ASSERT_X(item.isValid(),
                   "History::recordDeletion()", "Item must be valid.");
        Q_ASSERT_X(item.hasPayload<Incidence::Ptr>(),
                   "History::recordDeletion()", "Item must have an Incidence::Ptr payload.");
    }

    d->stackEntry(entry, atomicOperationId);
}

QString History::nextUndoDescription() const
{
    if (!d->mUndoStack.isEmpty()) {
        return d->mUndoStack.top()->mDescription;
    } else {
        return QString();
    }
}

QString History::nextRedoDescription() const
{
    if (!d->mRedoStack.isEmpty()) {
        return d->mRedoStack.top()->mDescription;
    } else {
        return QString();
    }
}

void History::undo(QWidget *parent)
{
    d->undoOrRedo(TypeUndo, parent);
}

void History::redo(QWidget *parent)
{
    d->undoOrRedo(TypeRedo, parent);
}

void History::undoAll(QWidget *parent)
{
    if (d->mOperationTypeInProgress != TypeNone) {
        qCWarning(AKONADICALENDAR_LOG) << "Don't call History::undoAll() while an undo/redo/undoAll is in progress";
    } else if (d->mEnabled) {
        d->mUndoAllInProgress = true;
        d->mCurrentParent = parent;
        d->doIt(TypeUndo);
    } else {
        qCWarning(AKONADICALENDAR_LOG) << "Don't call undo/redo when History is disabled";
    }
}

bool History::clear()
{
    bool result = true;
    if (d->mOperationTypeInProgress == TypeNone) {
        d->mRedoStack.clear();
        d->mUndoStack.clear();
        d->mLastErrorString.clear();
        d->mQueuedEntries.clear();
    } else {
        result = false;
    }
    emit changed();
    return result;
}

QString History::lastErrorString() const
{
    return d->mLastErrorString;
}

bool History::undoAvailable() const
{
    return !d->mUndoStack.isEmpty() && d->mOperationTypeInProgress == TypeNone;
}

bool History::redoAvailable() const
{
    return !d->mRedoStack.isEmpty() && d->mOperationTypeInProgress == TypeNone;
}

void History::Private::updateIds(Item::Id oldId, Item::Id newId)
{
    mEntryInProgress->updateIds(oldId, newId);

    for (const Entry::Ptr &entry : qAsConst(mUndoStack)) {
        entry->updateIds(oldId, newId);
    }

    for (const Entry::Ptr &entry : qAsConst(mRedoStack)) {
        entry->updateIds(oldId, newId);
    }
}

void History::Private::doIt(OperationType type)
{
    mOperationTypeInProgress = type;
    emit q->changed(); // Application will disable undo/redo buttons because operation is in progress
    Q_ASSERT(!stack().isEmpty());
    mEntryInProgress = stack().pop();

    connect(mEntryInProgress.data(), &Entry::finished, this, &Private::handleFinished,
            Qt::UniqueConnection);
    mEntryInProgress->doIt(type);
}

void History::Private::handleFinished(IncidenceChanger::ResultCode changerResult,
                                      const QString &errorString)
{
    Q_ASSERT(mOperationTypeInProgress != TypeNone);
    Q_ASSERT(!(mUndoAllInProgress && mOperationTypeInProgress == TypeRedo));

    const bool success = (changerResult == IncidenceChanger::ResultCodeSuccess);
    const History::ResultCode resultCode = success ? History::ResultCodeSuccess :
                                           History::ResultCodeError;

    if (success) {
        mLastErrorString.clear();
        destinationStack().push(mEntryInProgress);
    } else {
        mLastErrorString = errorString;
        stack().push(mEntryInProgress);
    }

    mCurrentParent = nullptr;

    // Process recordCreation/Modification/Deletions that came in while an operation
    // was in progress
    if (!mQueuedEntries.isEmpty()) {
        mRedoStack.clear();
        for (const Entry::Ptr &entry : qAsConst(mQueuedEntries)) {
            mUndoStack.push(entry);
        }
        mQueuedEntries.clear();
    }

    emitDone(mOperationTypeInProgress, resultCode);
    mOperationTypeInProgress = TypeNone;
    emit q->changed();
}

void History::Private::stackEntry(const Entry::Ptr &entry, uint atomicOperationId)
{
    const bool useMultiEntry = (atomicOperationId > 0);

    Entry::Ptr entryToPush;

    if (useMultiEntry) {
        Entry::Ptr topEntry = (mOperationTypeInProgress == TypeNone)                           ?
                              (mUndoStack.isEmpty() ? Entry::Ptr() : mUndoStack.top())   :
                              (mQueuedEntries.isEmpty() ? Entry::Ptr() : mQueuedEntries.last());

        const bool topIsMultiEntry = qobject_cast<MultiEntry *>(topEntry.data());

        if (topIsMultiEntry) {
            MultiEntry::Ptr multiEntry = topEntry.staticCast<MultiEntry>();
            if (multiEntry->mAtomicOperationId != atomicOperationId) {
                multiEntry = MultiEntry::Ptr(new MultiEntry(atomicOperationId, entry->mDescription, q));
                entryToPush = multiEntry;
            }
            multiEntry->addEntry(entry);
        } else {
            MultiEntry::Ptr multiEntry = MultiEntry::Ptr(new MultiEntry(atomicOperationId,
                                         entry->mDescription, q));
            multiEntry->addEntry(entry);
            entryToPush = multiEntry;
        }
    } else {
        entryToPush = entry;
    }

    if (mOperationTypeInProgress == TypeNone) {
        if (entryToPush) {
            mUndoStack.push(entryToPush);
        }
        mRedoStack.clear();
        emit q->changed();
    } else {
        if (entryToPush) {
            mQueuedEntries.append(entryToPush);
        }
    }
}

void History::Private::undoOrRedo(OperationType type, QWidget *parent)
{
    // Don't call undo() without the previous one finishing
    Q_ASSERT(mOperationTypeInProgress == TypeNone);

    if (!stack(type).isEmpty()) {
        if (mEnabled) {
            mCurrentParent = parent;
            doIt(type);
        } else {
            qCWarning(AKONADICALENDAR_LOG) << "Don't call undo/redo when History is disabled";
        }
    } else {
        qCWarning(AKONADICALENDAR_LOG) << "Don't call undo/redo when the stack is empty.";
    }
}

QStack<Entry::Ptr> &History::Private::stack(OperationType type)
{
    // Entries from the undo stack go to the redo stack, and vice-versa
    return type == TypeUndo ? mUndoStack : mRedoStack;
}

void History::Private::setEnabled(bool enabled)
{
    mEnabled = enabled;
}

int History::Private::redoCount() const
{
    return mRedoStack.count();
}

int History::Private::undoCount() const
{
    return mUndoStack.count();
}

QStack<Entry::Ptr> &History::Private::stack()
{
    return stack(mOperationTypeInProgress);
}

QStack<Entry::Ptr> &History::Private::destinationStack()
{
    // Entries from the undo stack go to the redo stack, and vice-versa
    return mOperationTypeInProgress == TypeRedo ? mUndoStack : mRedoStack;
}

void History::Private::emitDone(OperationType type, History::ResultCode resultCode)
{
    if (type == TypeUndo) {
        emit q->undone(resultCode);
    } else if (type == TypeRedo) {
        emit q->redone(resultCode);
    } else {
        Q_ASSERT(false);
    }
}

Akonadi::IncidenceChanger *History::incidenceChanger() const
{
    return d->mChanger;
}

#include "moc_history.cpp"
#include "moc_history_p.cpp"