2 This file is part of KMail
4 Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
5 Copyright (c) 2003 Zack Rusin <zack@kde.org>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
11 This software is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "undostack.h"
24 #include "kmmainwin.h"
27 #include <AkonadiCore/itemmovejob.h>
29 #include <kmessagebox.h>
30 #include <KLocalizedString>
31 #include "kmail_debug.h"
35 using namespace KMail
;
37 UndoStack::UndoStack(int size
)
41 mCachedInfo(Q_NULLPTR
)
45 UndoStack::~UndoStack()
50 void UndoStack::clear()
56 int UndoStack::size() const
58 return mStack
.count();
61 QString
UndoStack::undoInfo() const
63 if (!mStack
.isEmpty()) {
64 UndoInfo
*info
= mStack
.first();
65 return info
->moveToTrash
? i18n("Move To Trash") : i18np("Move Message", "Move Messages", info
->items
.count());
71 int UndoStack::newUndoAction(const Akonadi::Collection
&srcFolder
, const Akonadi::Collection
&destFolder
)
73 UndoInfo
*info
= new UndoInfo
;
75 info
->srcFolder
= srcFolder
;
76 info
->destFolder
= destFolder
;
77 info
->moveToTrash
= (destFolder
== CommonKernel
->trashCollectionFolder());
78 if ((int) mStack
.count() == mSize
) {
83 Q_EMIT
undoStackChanged();
87 void UndoStack::addMsgToAction(int undoId
, const Akonadi::Item
&item
)
89 if (!mCachedInfo
|| mCachedInfo
->id
!= undoId
) {
90 QList
<UndoInfo
*>::const_iterator itr
= mStack
.constBegin();
91 while (itr
!= mStack
.constEnd()) {
92 if ((*itr
)->id
== undoId
) {
100 Q_ASSERT(mCachedInfo
);
101 mCachedInfo
->items
.append(item
);
104 bool UndoStack::isEmpty() const
106 return mStack
.isEmpty();
109 void UndoStack::undo()
111 if (!mStack
.isEmpty()) {
112 UndoInfo
*info
= mStack
.takeFirst();
113 Q_EMIT
undoStackChanged();
114 Akonadi::ItemMoveJob
*job
= new Akonadi::ItemMoveJob(info
->items
, info
->srcFolder
, this);
115 connect(job
, &Akonadi::ItemMoveJob::result
, this, &UndoStack::slotMoveResult
);
118 // Sorry.. stack is empty..
119 KMessageBox::sorry(kmkernel
->mainWin(), i18n("There is nothing to undo."));
123 void UndoStack::slotMoveResult(KJob
*job
)
126 KMessageBox::sorry(kmkernel
->mainWin(), i18n("Cannot move message. %1", job
->errorString()));
130 void UndoStack::pushSingleAction(const Akonadi::Item
&item
, const Akonadi::Collection
&folder
, const Akonadi::Collection
&destFolder
)
132 const int id
= newUndoAction(folder
, destFolder
);
133 addMsgToAction(id
, item
);
136 void UndoStack::folderDestroyed(const Akonadi::Collection
&folder
)
138 QList
<UndoInfo
*>::iterator it
= mStack
.begin();
139 while (it
!= mStack
.end()) {
140 UndoInfo
*info
= *it
;
142 ((info
->srcFolder
== folder
) ||
143 (info
->destFolder
== folder
))) {
145 it
= mStack
.erase(it
);
150 Q_EMIT
undoStackChanged();