korganizer
history.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "history.h"
00027
00028 #include <kcal/calendar.h>
00029 #include <kcal/incidence.h>
00030
00031 #include <klocale.h>
00032
00033 using namespace KCal;
00034 using namespace KOrg;
00035
00036 History::History( Calendar *calendar )
00037 : mCalendar( calendar ), mCurrentMultiEntry( 0 )
00038 {
00039 }
00040
00041 void History::undo()
00042 {
00043 if ( mUndoEntries.isEmpty() ) {
00044 return;
00045 }
00046
00047 Entry *entry = mUndoEntries.pop();
00048 if ( !entry ) {
00049 return;
00050 }
00051
00052 entry->undo();
00053 mRedoEntries.push( entry );
00054 emit undone();
00055
00056 emit redoAvailable( entry->text() );
00057
00058 if ( !mUndoEntries.isEmpty() ) {
00059
00060
00061 entry = mUndoEntries.pop();
00062 mUndoEntries.push( entry );
00063 emit undoAvailable( entry ? entry->text() : QString() );
00064 } else {
00065 emit undoAvailable( QString() );
00066 }
00067 }
00068
00069 void History::redo()
00070 {
00071 if ( mRedoEntries.isEmpty() ) {
00072 return;
00073 }
00074 if ( mCurrentMultiEntry ) {
00075 mCurrentMultiEntry = 0;
00076 }
00077 Entry *entry = mRedoEntries.pop();
00078 if ( !entry ) {
00079 return;
00080 }
00081
00082 emit undoAvailable( entry->text() );
00083
00084 entry->redo();
00085 mUndoEntries.push( entry );
00086 emit redone();
00087
00088 if ( !mRedoEntries.isEmpty() ) {
00089
00090
00091 entry = mRedoEntries.pop();
00092 mRedoEntries.push( entry );
00093 emit redoAvailable( entry ? entry->text() : QString() );
00094 } else {
00095 emit redoAvailable( QString() );
00096 }
00097 }
00098
00099 void History::truncate()
00100 {
00101 qDeleteAll( mUndoEntries );
00102 mUndoEntries.clear();
00103 emit redoAvailable( QString() );
00104 }
00105
00106 void History::addEntry( Entry *entry )
00107 {
00108 if ( mCurrentMultiEntry ) {
00109 mCurrentMultiEntry->appendEntry( entry );
00110 } else {
00111 truncate();
00112 mUndoEntries.push( entry );
00113 emit undoAvailable( entry->text() );
00114 }
00115 }
00116
00117 void History::recordDelete( Incidence *incidence )
00118 {
00119 addEntry( new EntryDelete( mCalendar, incidence ) );
00120 }
00121
00122 void History::recordAdd( Incidence *incidence )
00123 {
00124 addEntry( new EntryAdd( mCalendar, incidence ) );
00125 }
00126
00127 void History::recordEdit( Incidence *oldIncidence, Incidence *newIncidence )
00128 {
00129 addEntry( new EntryEdit( mCalendar, oldIncidence, newIncidence ) );
00130 }
00131
00132 void History::startMultiModify( const QString &description )
00133 {
00134 if ( mCurrentMultiEntry ) {
00135 endMultiModify();
00136 }
00137 mCurrentMultiEntry = new MultiEntry( mCalendar, description );
00138 addEntry( mCurrentMultiEntry );
00139 }
00140
00141 void History::endMultiModify()
00142 {
00143 mCurrentMultiEntry = 0;
00144 }
00145
00146 History::Entry::Entry( Calendar *calendar )
00147 : mCalendar( calendar )
00148 {
00149 }
00150
00151 History::Entry::~Entry()
00152 {
00153 }
00154
00155 History::EntryDelete::EntryDelete( Calendar *calendar, Incidence *incidence )
00156 : Entry( calendar ), mIncidence( incidence->clone() )
00157 {
00158 }
00159
00160 History::EntryDelete::~EntryDelete()
00161 {
00162 delete mIncidence;
00163 }
00164
00165 void History::EntryDelete::undo()
00166 {
00167
00168 mCalendar->addIncidence( mIncidence->clone() );
00169 }
00170
00171 void History::EntryDelete::redo()
00172 {
00173 Incidence *incidence = mCalendar->incidence( mIncidence->uid() );
00174 mCalendar->deleteIncidence( incidence );
00175 }
00176
00177 QString History::EntryDelete::text()
00178 {
00179 return i18n( "Delete %1", QString::fromLatin1( mIncidence->type() ) );
00180 }
00181
00182 History::EntryAdd::EntryAdd( Calendar *calendar, Incidence *incidence )
00183 : Entry( calendar ), mIncidence( incidence->clone() )
00184 {
00185 }
00186
00187 History::EntryAdd::~EntryAdd()
00188 {
00189 delete mIncidence;
00190 }
00191
00192 void History::EntryAdd::undo()
00193 {
00194 Incidence *incidence = mCalendar->incidence( mIncidence->uid() );
00195 if ( incidence ) {
00196 mCalendar->deleteIncidence( incidence );
00197 }
00198 }
00199
00200 void History::EntryAdd::redo()
00201 {
00202
00203 mCalendar->addIncidence( mIncidence->clone() );
00204 }
00205
00206 QString History::EntryAdd::text()
00207 {
00208 return i18n( "Add %1", QString::fromLatin1( mIncidence->type() ) );
00209 }
00210
00211 History::EntryEdit::EntryEdit( Calendar *calendar, Incidence *oldIncidence,
00212 Incidence *newIncidence )
00213 : Entry( calendar ), mOldIncidence( oldIncidence->clone() ),
00214 mNewIncidence( newIncidence->clone() )
00215 {
00216 }
00217
00218 History::EntryEdit::~EntryEdit()
00219 {
00220 delete mOldIncidence;
00221 delete mNewIncidence;
00222 }
00223
00224 void History::EntryEdit::undo()
00225 {
00226 Incidence *incidence = mCalendar->incidence( mNewIncidence->uid() );
00227 if ( incidence ) {
00228 mCalendar->deleteIncidence( incidence );
00229 }
00230
00231 mCalendar->addIncidence( mOldIncidence->clone() );
00232 }
00233
00234 void History::EntryEdit::redo()
00235 {
00236 Incidence *incidence = mCalendar->incidence( mOldIncidence->uid() );
00237 if ( incidence ) {
00238 mCalendar->deleteIncidence( incidence );
00239 }
00240
00241 mCalendar->addIncidence( mNewIncidence->clone() );
00242 }
00243
00244 QString History::EntryEdit::text()
00245 {
00246 return i18n( "Edit %1", QString::fromLatin1( mNewIncidence->type() ) );
00247 }
00248
00249 History::MultiEntry::MultiEntry( Calendar *calendar, const QString &text )
00250 : Entry( calendar ), mText( text )
00251 {
00252 }
00253
00254 History::MultiEntry::~MultiEntry()
00255 {
00256 qDeleteAll( mEntries );
00257 }
00258
00259 void History::MultiEntry::appendEntry( Entry *entry )
00260 {
00261 mEntries.append( entry );
00262 }
00263
00264 void History::MultiEntry::undo()
00265 {
00266 for ( int i = mEntries.size()-1; i>=0; --i ) {
00267 Entry *entry = mEntries.at( i );
00268 if ( entry ) {
00269 entry->undo();
00270 }
00271 }
00272 }
00273
00274 void History::MultiEntry::redo()
00275 {
00276 foreach ( Entry *entry, mEntries ) {
00277 entry->redo();
00278 }
00279 }
00280
00281 QString History::MultiEntry::text()
00282 {
00283 return mText;
00284 }
00285
00286 #include "history.moc"