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 <akonadi/itemmovejob.h>
29 #include <kmessagebox.h>
37 UndoStack::UndoStack(int size
)
38 : QObject(0), mSize(size
), mLastId(0),
41 setObjectName( "undostack" );
44 UndoStack::~UndoStack()
49 void UndoStack::clear()
55 int UndoStack::newUndoAction( const Akonadi::Collection
&srcFolder
, const Akonadi::Collection
&destFolder
)
57 UndoInfo
*info
= new UndoInfo
;
59 info
->srcFolder
= srcFolder
;
60 info
->destFolder
= destFolder
;
61 if ((int) mStack
.count() == mSize
) {
65 mStack
.prepend( info
);
66 emit
undoStackChanged();
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
) {
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
*)) );
99 // Sorry.. stack is empty..
100 KMessageBox::sorry( kmkernel
->mainWin(), i18n("There is nothing to undo."));
104 void UndoStack::slotMoveResult( KJob
*job
)
107 KMessageBox::sorry( kmkernel
->mainWin(), i18n("Can not move message. %1", job
->errorString() ) );
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
);
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();
129 info = mStack.next();
135 UndoStack::folderDestroyed( const Akonadi::Collection
&folder
)
137 QList
<UndoInfo
*>::iterator it
= mStack
.begin();
138 while ( it
!= mStack
.end() ) {
139 UndoInfo
*info
= *it
;
141 ( (info
->srcFolder
== folder
) ||
142 (info
->destFolder
== folder
) ) ) {
144 it
= mStack
.erase( it
);
149 emit
undoStackChanged();
154 #include "undostack.moc"