korganizer Library API Documentation

history.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program 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
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "history.h"
00026 
00027 #include <libkcal/calendar.h>
00028 #include <libkcal/incidence.h>
00029 
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 
00033 using namespace KCal;
00034 using namespace KOrg;
00035 
00036 History::History( KCal::Calendar *calendar )
00037   : mCalendar( calendar ), mCurrentMultiEntry( 0 ), 
00038     mUndoEntry( mEntries ), mRedoEntry( mEntries )
00039 {
00040   mEntries.setAutoDelete( true );
00041 }
00042 
00043 void History::undo()
00044 {
00045   if ( mCurrentMultiEntry ) mCurrentMultiEntry = 0;
00046   Entry *entry = mUndoEntry.current();
00047   if ( !entry ) return;
00048 
00049   entry->undo();
00050   emit undone();
00051 
00052   emit redoAvailable( entry->text() );
00053 
00054   mRedoEntry = mUndoEntry;
00055   --mUndoEntry;
00056 
00057   entry = mUndoEntry.current();
00058   if ( entry ) emit undoAvailable( entry->text() );
00059   else emit undoAvailable( QString::null );
00060 }
00061 
00062 void History::redo()
00063 {
00064   if ( mCurrentMultiEntry ) mCurrentMultiEntry = 0;
00065   Entry *entry = mRedoEntry.current();
00066   if ( !entry ) return;
00067 
00068   emit undoAvailable( entry->text() );
00069 
00070   entry->redo();
00071   emit redone();
00072 
00073   mUndoEntry = mRedoEntry;
00074   ++mRedoEntry;
00075 
00076   entry = mRedoEntry.current();
00077   if ( entry ) emit redoAvailable( entry->text() );
00078   else emit redoAvailable( QString::null );
00079 }
00080 
00081 void History::truncate()
00082 {
00083   while ( mUndoEntry.current() != mEntries.last() ) {
00084     mEntries.removeLast();
00085   }
00086   mRedoEntry = QPtrList<Entry>( mEntries );
00087   emit redoAvailable( QString::null );
00088 }
00089 
00090 void History::recordDelete( Incidence *incidence )
00091 {
00092   Entry *entry = new EntryDelete( mCalendar, incidence );
00093   if (mCurrentMultiEntry) {
00094     mCurrentMultiEntry->appendEntry( entry );
00095   } else {
00096     truncate();
00097     mEntries.append( entry );
00098     mUndoEntry.toLast();
00099     mRedoEntry = QPtrList<Entry>( mEntries );
00100     emit undoAvailable( entry->text() );
00101   }
00102 }
00103 
00104 void History::recordAdd( Incidence *incidence )
00105 {
00106   Entry *entry = new EntryAdd( mCalendar, incidence );
00107   if (mCurrentMultiEntry) {
00108     mCurrentMultiEntry->appendEntry( entry );
00109   } else {
00110     truncate();
00111     mEntries.append( entry );
00112     mUndoEntry.toLast();
00113     mRedoEntry = QPtrList<Entry>( mEntries );
00114     emit undoAvailable( entry->text() );
00115   }
00116 }
00117 
00118 void History::recordEdit( Incidence *oldIncidence, Incidence *newIncidence )
00119 {
00120   Entry *entry = new EntryEdit( mCalendar, oldIncidence, newIncidence );
00121   if (mCurrentMultiEntry) {
00122     mCurrentMultiEntry->appendEntry( entry );
00123   } else {
00124     truncate();
00125     mEntries.append( entry );
00126     mUndoEntry.toLast();
00127     mRedoEntry = QPtrList<Entry>( mEntries );
00128     emit undoAvailable( entry->text() );
00129   }
00130 }
00131 
00132 void History::startMultiModify( const QString &description )
00133 {
00134   if ( mCurrentMultiEntry ) {
00135     endMultiModify();
00136   }
00137   mCurrentMultiEntry = new MultiEntry( mCalendar, description );
00138   truncate();
00139   mEntries.append( mCurrentMultiEntry );
00140   mUndoEntry.toLast();
00141   mRedoEntry = QPtrList<Entry>( mEntries );
00142   emit undoAvailable( mCurrentMultiEntry->text() );
00143 }
00144 
00145 void History::endMultiModify()
00146 {
00147   mCurrentMultiEntry = 0;
00148 }
00149 
00150 
00151 History::Entry::Entry( KCal::Calendar *calendar )
00152   : mCalendar( calendar )
00153 {
00154 }
00155 
00156 History::Entry::~Entry()
00157 {
00158 }
00159 
00160 History::EntryDelete::EntryDelete( Calendar *calendar, Incidence *incidence )
00161   : Entry( calendar ), mIncidence( incidence->clone() )
00162 {
00163 }
00164 
00165 History::EntryDelete::~EntryDelete()
00166 {
00167   delete mIncidence;
00168 }
00169 
00170 void History::EntryDelete::undo()
00171 {
00172   mCalendar->addIncidence( mIncidence->clone() );
00173 }
00174 
00175 void History::EntryDelete::redo()
00176 {
00177   Incidence *incidence = mCalendar->incidence( mIncidence->uid() );
00178   mCalendar->deleteIncidence( incidence );
00179 }
00180 
00181 QString History::EntryDelete::text()
00182 {
00183   return i18n("Delete %1").arg(mIncidence->type());
00184 }
00185 
00186 
00187 History::EntryAdd::EntryAdd( Calendar *calendar, Incidence *incidence )
00188   : Entry( calendar ), mIncidence( incidence->clone() )
00189 {
00190 }
00191 
00192 History::EntryAdd::~EntryAdd()
00193 {
00194   delete mIncidence;
00195 }
00196 
00197 void History::EntryAdd::undo()
00198 {
00199   Incidence *incidence = mCalendar->incidence( mIncidence->uid() );
00200   if ( incidence )
00201     mCalendar->deleteIncidence( incidence );
00202 }
00203 
00204 void History::EntryAdd::redo()
00205 {
00206   mCalendar->addIncidence( mIncidence->clone() );
00207 }
00208 
00209 QString History::EntryAdd::text()
00210 {
00211   return i18n("Add %1").arg(mIncidence->type());
00212 }
00213 
00214 
00215 History::EntryEdit::EntryEdit( Calendar *calendar, Incidence *oldIncidence,
00216                                Incidence *newIncidence )
00217   : Entry( calendar ), mOldIncidence( oldIncidence->clone() ),
00218     mNewIncidence( newIncidence->clone() )
00219 {
00220 }
00221 
00222 History::EntryEdit::~EntryEdit()
00223 {
00224   delete mOldIncidence;
00225   delete mNewIncidence;
00226 }
00227 
00228 void History::EntryEdit::undo()
00229 {
00230   Incidence *incidence = mCalendar->incidence( mNewIncidence->uid() );
00231   if ( incidence )
00232       mCalendar->deleteIncidence( incidence );
00233   mCalendar->addIncidence( mOldIncidence->clone() );
00234 }
00235 
00236 void History::EntryEdit::redo()
00237 {
00238   Incidence *incidence = mCalendar->incidence( mOldIncidence->uid() );
00239   if ( incidence )
00240       mCalendar->deleteIncidence( incidence );
00241   mCalendar->addIncidence( mNewIncidence->clone() );
00242 }
00243 
00244 QString History::EntryEdit::text()
00245 {
00246   return i18n("Edit %1").arg(mNewIncidence->type());
00247 }
00248 
00249 History::MultiEntry::MultiEntry( Calendar *calendar, QString text )
00250   : Entry( calendar ), mText( text )
00251 {
00252   mEntries.setAutoDelete( true );
00253 }
00254 
00255 History::MultiEntry::~MultiEntry()
00256 {
00257 }
00258 
00259 void History::MultiEntry::appendEntry( Entry* entry )
00260 {
00261   mEntries.append( entry );
00262 }
00263 
00264 void History::MultiEntry::undo()
00265 {
00266   QPtrListIterator<Entry> it( mEntries );
00267   it.toLast();
00268   Entry *entry;
00269   while ( (entry = it.current()) != 0 ) {
00270     --it;
00271     entry->undo();
00272   }
00273 }
00274 
00275 void History::MultiEntry::redo()
00276 {
00277   QPtrListIterator<Entry> it( mEntries );
00278   Entry *entry;
00279   while ( (entry = it.current()) != 0 ) {
00280     ++it;
00281     entry->redo();
00282   }
00283 }
00284 
00285 QString History::MultiEntry::text()
00286 {
00287   return mText;
00288 }
00289 
00290 #include "history.moc"
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu May 3 20:24:52 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003