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 TQt, and distribute the resulting executable, 00023 without including the source code for TQt in the source distribution. 00024 */ 00025 00026 #include "history.h" 00027 00028 #include <libkcal/calendar.h> 00029 #include <libkcal/incidence.h> 00030 00031 #include <tdelocale.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( TQString() ); 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( TQString() ); 00080 } 00081 00082 void History::truncate() 00083 { 00084 while ( mUndoEntry.current() != mEntries.last() ) { 00085 mEntries.removeLast(); 00086 } 00087 mRedoEntry = TQPtrList<Entry>( mEntries ); 00088 emit redoAvailable( TQString() ); 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 = TQPtrList<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 = TQPtrList<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 = TQPtrList<Entry>( mEntries ); 00129 emit undoAvailable( entry->text() ); 00130 } 00131 } 00132 00133 void History::startMultiModify( const TQString &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 = TQPtrList<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 // TODO: Use the proper resource instead of asking! 00174 mCalendar->addIncidence( mIncidence->clone() ); 00175 } 00176 00177 void History::EntryDelete::redo() 00178 { 00179 Incidence *incidence = mCalendar->incidence( mIncidence->uid() ); 00180 mCalendar->deleteIncidence( incidence ); 00181 } 00182 00183 TQString History::EntryDelete::text() 00184 { 00185 return i18n("Delete %1").arg(mIncidence->type().data()); 00186 } 00187 00188 00189 History::EntryAdd::EntryAdd( Calendar *calendar, Incidence *incidence ) 00190 : Entry( calendar ), mIncidence( incidence->clone() ) 00191 { 00192 } 00193 00194 History::EntryAdd::~EntryAdd() 00195 { 00196 delete mIncidence; 00197 } 00198 00199 void History::EntryAdd::undo() 00200 { 00201 Incidence *incidence = mCalendar->incidence( mIncidence->uid() ); 00202 if ( incidence ) 00203 mCalendar->deleteIncidence( incidence ); 00204 } 00205 00206 void History::EntryAdd::redo() 00207 { 00208 // TODO: User the proper resource instead of asking again 00209 mCalendar->addIncidence( mIncidence->clone() ); 00210 } 00211 00212 TQString History::EntryAdd::text() 00213 { 00214 return i18n("Add %1").arg(mIncidence->type().data()); 00215 } 00216 00217 00218 History::EntryEdit::EntryEdit( Calendar *calendar, Incidence *oldIncidence, 00219 Incidence *newIncidence ) 00220 : Entry( calendar ), mOldIncidence( oldIncidence->clone() ), 00221 mNewIncidence( newIncidence->clone() ) 00222 { 00223 } 00224 00225 History::EntryEdit::~EntryEdit() 00226 { 00227 delete mOldIncidence; 00228 delete mNewIncidence; 00229 } 00230 00231 void History::EntryEdit::undo() 00232 { 00233 Incidence *incidence = mCalendar->incidence( mNewIncidence->uid() ); 00234 if ( incidence ) 00235 mCalendar->deleteIncidence( incidence ); 00236 // TODO: Use the proper resource instead of asking again 00237 mCalendar->addIncidence( mOldIncidence->clone() ); 00238 } 00239 00240 void History::EntryEdit::redo() 00241 { 00242 Incidence *incidence = mCalendar->incidence( mOldIncidence->uid() ); 00243 if ( incidence ) 00244 mCalendar->deleteIncidence( incidence ); 00245 // TODO: Use the proper resource instead of asking again 00246 mCalendar->addIncidence( mNewIncidence->clone() ); 00247 } 00248 00249 TQString History::EntryEdit::text() 00250 { 00251 return i18n("Edit %1").arg(mNewIncidence->type().data()); 00252 } 00253 00254 History::MultiEntry::MultiEntry( Calendar *calendar, const TQString &text ) 00255 : Entry( calendar ), mText( text ) 00256 { 00257 mEntries.setAutoDelete( true ); 00258 } 00259 00260 History::MultiEntry::~MultiEntry() 00261 { 00262 } 00263 00264 void History::MultiEntry::appendEntry( Entry* entry ) 00265 { 00266 mEntries.append( entry ); 00267 } 00268 00269 void History::MultiEntry::undo() 00270 { 00271 TQPtrListIterator<Entry> it( mEntries ); 00272 it.toLast(); 00273 Entry *entry; 00274 while ( (entry = it.current()) != 0 ) { 00275 --it; 00276 entry->undo(); 00277 } 00278 } 00279 00280 void History::MultiEntry::redo() 00281 { 00282 TQPtrListIterator<Entry> it( mEntries ); 00283 Entry *entry; 00284 while ( (entry = it.current()) != 0 ) { 00285 ++it; 00286 entry->redo(); 00287 } 00288 } 00289 00290 TQString History::MultiEntry::text() 00291 { 00292 return mText; 00293 } 00294 00295 #include "history.moc"