Fix mem leak
[kdepim.git] / kmail / undostack.cpp
blob7f4df60fef46eac089ac537a070cef0001f3ad48
1 /*
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"
25 #include "kmkernel.h"
26 #include <KJob>
27 #include <AkonadiCore/itemmovejob.h>
29 #include <kmessagebox.h>
30 #include <KLocalizedString>
31 #include "kmail_debug.h"
33 #include <QList>
35 using namespace KMail;
37 UndoStack::UndoStack(int size)
38 : QObject(Q_NULLPTR),
39 mSize(size),
40 mLastId(0),
41 mCachedInfo(Q_NULLPTR)
45 UndoStack::~UndoStack()
47 clear();
50 void UndoStack::clear()
52 qDeleteAll(mStack);
53 mStack.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());
66 } else {
67 return QString();
71 int UndoStack::newUndoAction(const Akonadi::Collection &srcFolder, const Akonadi::Collection &destFolder)
73 UndoInfo *info = new UndoInfo;
74 info->id = ++mLastId;
75 info->srcFolder = srcFolder;
76 info->destFolder = destFolder;
77 info->moveToTrash = (destFolder == CommonKernel->trashCollectionFolder());
78 if ((int) mStack.count() == mSize) {
79 delete mStack.last();
80 mStack.removeLast();
82 mStack.prepend(info);
83 Q_EMIT undoStackChanged();
84 return info->id;
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) {
93 mCachedInfo = (*itr);
94 break;
96 ++itr;
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);
116 delete info;
117 } else {
118 // Sorry.. stack is empty..
119 KMessageBox::sorry(kmkernel->mainWin(), i18n("There is nothing to undo."));
123 void UndoStack::slotMoveResult(KJob *job)
125 if (job->error()) {
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;
141 if (info &&
142 ((info->srcFolder == folder) ||
143 (info->destFolder == folder))) {
144 delete info;
145 it = mStack.erase(it);
146 } else {
147 ++it;
150 Q_EMIT undoStackChanged();