korganizer

history.cpp

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