The last part of typo fixes (I really hope)
[kdepim.git] / kmail / undostack.cpp
blobf20ca83a406faed1c7841874d3deb5145d29a03f
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 <akonadi/itemmovejob.h>
29 #include <kmessagebox.h>
30 #include <klocale.h>
31 #include <kdebug.h>
33 #include <QList>
35 namespace KMail {
37 UndoStack::UndoStack(int size)
38 : QObject(0), mSize(size), mLastId(0),
39 mCachedInfo(0)
41 setObjectName( "undostack" );
44 UndoStack::~UndoStack()
46 qDeleteAll( mStack );
49 void UndoStack::clear()
51 qDeleteAll( mStack );
52 mStack.clear();
55 int UndoStack::newUndoAction( const Akonadi::Collection &srcFolder, const Akonadi::Collection &destFolder )
57 UndoInfo *info = new UndoInfo;
58 info->id = ++mLastId;
59 info->srcFolder = srcFolder;
60 info->destFolder = destFolder;
61 if ((int) mStack.count() == mSize) {
62 delete mStack.last();
63 mStack.removeLast();
65 mStack.prepend( info );
66 emit undoStackChanged();
67 return info->id;
70 void UndoStack::addMsgToAction( int undoId, const Akonadi::Item &item )
72 if ( !mCachedInfo || mCachedInfo->id != undoId ) {
73 QList<UndoInfo*>::const_iterator itr = mStack.constBegin();
74 while ( itr != mStack.constEnd() ) {
75 if ( (*itr)->id == undoId ) {
76 mCachedInfo = (*itr);
77 break;
79 ++itr;
83 Q_ASSERT( mCachedInfo );
84 mCachedInfo->items.append( item );
87 void UndoStack::undo()
89 if ( mStack.count() > 0 )
91 UndoInfo *info = mStack.takeFirst();
92 emit undoStackChanged();
93 Akonadi::ItemMoveJob * job = new Akonadi::ItemMoveJob( info->items, info->srcFolder, this );
94 connect( job, SIGNAL(result(KJob*)), this, SLOT(slotMoveResult(KJob*)) );
95 delete info;
97 else
99 // Sorry.. stack is empty..
100 KMessageBox::sorry( kmkernel->mainWin(), i18n("There is nothing to undo."));
104 void UndoStack::slotMoveResult( KJob *job )
106 if ( job->error() )
107 KMessageBox::sorry( kmkernel->mainWin(), i18n("Can not move message. %1", job->errorString() ) );
110 void
111 UndoStack::pushSingleAction(const Akonadi::Item &item, const Akonadi::Collection &folder, const Akonadi::Collection &destFolder)
113 int id = newUndoAction( folder, destFolder );
114 addMsgToAction( id, item );
117 void
118 UndoStack::msgDestroyed( const Akonadi::Item & /*msg*/)
121 for(UndoInfo *info = mStack.first(); info; )
123 if (info->msgIdMD5 == msg->msgIdMD5())
125 mStack.removeRef( info );
126 info = mStack.current();
128 else
129 info = mStack.next();
134 void
135 UndoStack::folderDestroyed( const Akonadi::Collection &folder)
137 QList<UndoInfo*>::iterator it = mStack.begin();
138 while ( it != mStack.end() ) {
139 UndoInfo *info = *it;
140 if ( info &&
141 ( (info->srcFolder == folder) ||
142 (info->destFolder == folder) ) ) {
143 delete info;
144 it = mStack.erase( it );
146 else
147 it++;
149 emit undoStackChanged();
154 #include "undostack.moc"