kmail

undostack.cpp
1/*
2 This file is part of KMail
3
4 Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
5 Copyright (c) 2003 Zack Rusin <zack@kde.org>
6
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.
10
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.
15
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.
20*/
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#include "undostack.h"
27
28#include "kmmainwin.h"
29#include "kmfolder.h"
30#include "kmmsgdict.h"
31
32#include <tdemessagebox.h>
33#include <tdelocale.h>
34#include <kdebug.h>
35
36namespace KMail {
37
38UndoStack::UndoStack(int size)
39 : TQObject(0, "undostack"), mSize(size), mLastId(0),
40 mCachedInfo(0)
41{
42 mStack.setAutoDelete(true);
43}
44
45void UndoStack::clear()
46{
47 mStack.clear();
48}
49
50int UndoStack::newUndoAction( KMFolder *srcFolder, KMFolder *destFolder )
51{
52 UndoInfo *info = new UndoInfo;
53 info->id = ++mLastId;
54 info->srcFolder = srcFolder;
55 info->destFolder = destFolder;
56 if ((int) mStack.count() == mSize)
57 mStack.removeLast();
58 mStack.prepend( info );
59 emit undoStackChanged();
60 return info->id;
61}
62
63void UndoStack::addMsgToAction( int undoId, ulong serNum )
64{
65 if ( !mCachedInfo || mCachedInfo->id != undoId ) {
66 TQPtrListIterator<UndoInfo> itr( mStack );
67 while ( itr.current() ) {
68 if ( itr.current()->id == undoId ) {
69 mCachedInfo = itr.current();
70 break;
71 }
72 ++itr;
73 }
74 }
75
76 Q_ASSERT( mCachedInfo );
77 mCachedInfo->serNums.append( serNum );
78}
79
80void UndoStack::undo()
81{
82 KMMessage *msg;
83 ulong serNum;
84 int idx = -1;
85 KMFolder *curFolder;
86 if ( mStack.count() > 0 )
87 {
88 UndoInfo *info = mStack.take(0);
89 emit undoStackChanged();
90 TQValueList<ulong>::iterator itr;
91 KMFolderOpener openDestFolder(info->destFolder, "undodest");
92 for( itr = info->serNums.begin(); itr != info->serNums.end(); ++itr ) {
93 serNum = *itr;
94 KMMsgDict::instance()->getLocation(serNum, &curFolder, &idx);
95 if ( idx == -1 || curFolder != info->destFolder ) {
96 kdDebug(5006)<<"Serious undo error!"<<endl;
97 delete info;
98 return;
99 }
100 msg = curFolder->getMsg( idx );
101 info->srcFolder->moveMsg( msg );
102 if ( info->srcFolder->count() > 1 )
103 info->srcFolder->unGetMsg( info->srcFolder->count() - 1 );
104 }
105 delete info;
106 }
107 else
108 {
109 // Sorry.. stack is empty..
110 KMessageBox::sorry( kmkernel->mainWin(), i18n("There is nothing to undo."));
111 }
112}
113
114void
115UndoStack::pushSingleAction(ulong serNum, KMFolder *folder, KMFolder *destFolder)
116{
117 int id = newUndoAction( folder, destFolder );
118 addMsgToAction( id, serNum );
119}
120
121void
122UndoStack::msgDestroyed( KMMsgBase* /*msg*/)
123{
124 /*
125 for(UndoInfo *info = mStack.first(); info; )
126 {
127 if (info->msgIdMD5 == msg->msgIdMD5())
128 {
129 mStack.removeRef( info );
130 info = mStack.current();
131 }
132 else
133 info = mStack.next();
134 }
135 */
136}
137
138void
139UndoStack::folderDestroyed( KMFolder *folder)
140{
141 for( UndoInfo *info = mStack.first(); info; )
142 {
143 if ( (info->srcFolder == folder) ||
144 (info->destFolder == folder) )
145 {
146 mStack.removeRef( info );
147 info = mStack.current();
148 }
149 else
150 info = mStack.next();
151 }
152 emit undoStackChanged();
153}
154
155}
156
157#include "undostack.moc"
RAII for KMFolder::open() / close().
Definition: kmfolder.h:688
Mail folder.
Definition: kmfolder.h:69
KMMessage * take(int idx)
Detach message from this folder.
Definition: kmfolder.cpp:380
KMMessage * getMsg(int idx)
Read message at given index.
Definition: kmfolder.cpp:321
This is a Mime Message.
Definition: kmmessage.h:68
void getLocation(unsigned long key, KMFolder **retFolder, int *retIndex) const
Returns the folder the message represented by the serial number key is in and the index in that folde...
Definition: kmmsgdict.cpp:319
static const KMMsgDict * instance()
Access the globally unique MessageDict.
Definition: kmmsgdict.cpp:167
folderdiaquotatab.h
Definition: aboutdata.cpp:40