kmail Library API Documentation

undostack.cpp

00001 /*
00002     This file is part of KMail
00003 
00004     Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
00005     Copyright (c) 2003 Zack Rusin <zack@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License
00009     version 2 as published by the Free Software Foundation.
00010 
00011     This software is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this library; see the file COPYING. If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include <config.h>
00024 #endif
00025 
00026 #include "undostack.h"
00027 
00028 #include "kmmainwin.h"
00029 #include "kmkernel.h"
00030 #include "kmfolder.h"
00031 #include "kmmessage.h"
00032 #include "kmmsgdict.h"
00033 
00034 #include <kmessagebox.h>
00035 #include <klocale.h>
00036 #include <kdebug.h>
00037 
00038 namespace KMail {
00039 
00040 UndoStack::UndoStack(int size)
00041   : QObject(0, "undostack"), mSize(size), mLastId(0),
00042     mCachedInfo(0)
00043 {
00044    mStack.setAutoDelete(true);
00045 }
00046 
00047 void UndoStack::clear()
00048 {
00049    mStack.clear();
00050 }
00051 
00052 int UndoStack::newUndoAction( KMFolder *srcFolder, KMFolder *destFolder )
00053 {
00054   UndoInfo *info = new UndoInfo;
00055   info->id         = ++mLastId;
00056   info->srcFolder  = srcFolder;
00057   info->destFolder = destFolder;
00058   if ((int) mStack.count() == mSize)
00059     mStack.removeLast();
00060   mStack.prepend( info );
00061   emit undoStackChanged();
00062   return info->id;
00063 }
00064 
00065 void UndoStack::addMsgToAction( int undoId, ulong serNum )
00066 {
00067   if ( !mCachedInfo || mCachedInfo->id != undoId ) {
00068     QPtrListIterator<UndoInfo> itr( mStack );
00069     while ( itr.current() ) {
00070       if ( itr.current()->id == undoId ) {
00071         mCachedInfo = itr.current();
00072         break;
00073       }
00074       ++itr;
00075     }
00076   }
00077 
00078   Q_ASSERT( mCachedInfo );
00079   mCachedInfo->serNums.append( serNum );
00080 }
00081 
00082 void UndoStack::undo()
00083 {
00084   KMMessage *msg;
00085   ulong serNum;
00086   int idx = -1;
00087   KMFolder *curFolder;
00088   if ( mStack.count() > 0 )
00089   {
00090     UndoInfo *info = mStack.take(0);
00091     emit undoStackChanged();
00092     QValueList<ulong>::iterator itr;
00093     info->destFolder->open();
00094     for( itr = info->serNums.begin(); itr != info->serNums.end(); ++itr ) {
00095       serNum = *itr;
00096       kmkernel->msgDict()->getLocation(serNum, &curFolder, &idx);
00097       if ( idx == -1 || curFolder != info->destFolder ) {
00098         kdDebug(5006)<<"Serious undo error!"<<endl;
00099         delete info;
00100         return;
00101       }
00102       msg = curFolder->getMsg( idx );
00103       info->srcFolder->moveMsg( msg );
00104       if ( info->srcFolder->count() > 1 )
00105         info->srcFolder->unGetMsg( info->srcFolder->count() - 1 );
00106     }
00107     info->destFolder->close();
00108     delete info;
00109   }
00110   else
00111   {
00112     // Sorry.. stack is empty..
00113     KMessageBox::sorry( kmkernel->mainWin(), i18n("There is nothing to undo!"));
00114   }
00115 }
00116 
00117 void
00118 UndoStack::pushSingleAction(ulong serNum, KMFolder *folder, KMFolder *destFolder)
00119 {
00120   int id = newUndoAction( folder, destFolder );
00121   addMsgToAction( id, serNum );
00122 }
00123 
00124 void
00125 UndoStack::msgDestroyed( KMMsgBase* /*msg*/)
00126 {
00127   /*
00128    for(UndoInfo *info = mStack.first(); info; )
00129    {
00130       if (info->msgIdMD5 == msg->msgIdMD5())
00131       {
00132          mStack.removeRef( info );
00133          info = mStack.current();
00134       }
00135       else
00136          info = mStack.next();
00137    }
00138   */
00139 }
00140 
00141 void
00142 UndoStack::folderDestroyed( KMFolder *folder)
00143 {
00144    for( UndoInfo *info = mStack.first(); info; )
00145    {
00146       if ( (info->srcFolder == folder) ||
00147        (info->destFolder == folder) )
00148       {
00149          mStack.removeRef( info );
00150          info = mStack.current();
00151       }
00152       else
00153          info = mStack.next();
00154    }
00155    emit undoStackChanged();
00156 }
00157 
00158 }
00159 
00160 #include "undostack.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:35 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003