korganizer

history.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 #include "history.h"
27 
28 #include <libkcal/calendar.h>
29 #include <libkcal/incidence.h>
30 
31 #include <tdelocale.h>
32 #include <kdebug.h>
33 
34 using namespace KCal;
35 using namespace KOrg;
36 
37 History::History( KCal::Calendar *calendar )
38  : mCalendar( calendar ), mCurrentMultiEntry( 0 ),
39  mUndoEntry( mEntries ), mRedoEntry( mEntries )
40 {
41  mEntries.setAutoDelete( true );
42 }
43 
44 void History::undo()
45 {
46  if ( mCurrentMultiEntry ) mCurrentMultiEntry = 0;
47  Entry *entry = mUndoEntry.current();
48  if ( !entry ) return;
49 
50  entry->undo();
51  emit undone();
52 
53  emit redoAvailable( entry->text() );
54 
55  mRedoEntry = mUndoEntry;
56  --mUndoEntry;
57 
58  entry = mUndoEntry.current();
59  if ( entry ) emit undoAvailable( entry->text() );
60  else emit undoAvailable( TQString() );
61 }
62 
63 void History::redo()
64 {
65  if ( mCurrentMultiEntry ) mCurrentMultiEntry = 0;
66  Entry *entry = mRedoEntry.current();
67  if ( !entry ) return;
68 
69  emit undoAvailable( entry->text() );
70 
71  entry->redo();
72  emit redone();
73 
74  mUndoEntry = mRedoEntry;
75  ++mRedoEntry;
76 
77  entry = mRedoEntry.current();
78  if ( entry ) emit redoAvailable( entry->text() );
79  else emit redoAvailable( TQString() );
80 }
81 
82 void History::truncate()
83 {
84  while ( mUndoEntry.current() != mEntries.last() ) {
85  mEntries.removeLast();
86  }
87  mRedoEntry = TQPtrList<Entry>( mEntries );
88  emit redoAvailable( TQString() );
89 }
90 
91 void History::recordDelete( Incidence *incidence )
92 {
93  Entry *entry = new EntryDelete( mCalendar, incidence );
94  if (mCurrentMultiEntry) {
95  mCurrentMultiEntry->appendEntry( entry );
96  } else {
97  truncate();
98  mEntries.append( entry );
99  mUndoEntry.toLast();
100  mRedoEntry = TQPtrList<Entry>( mEntries );
101  emit undoAvailable( entry->text() );
102  }
103 }
104 
105 void History::recordAdd( Incidence *incidence )
106 {
107  Entry *entry = new EntryAdd( mCalendar, incidence );
108  if (mCurrentMultiEntry) {
109  mCurrentMultiEntry->appendEntry( entry );
110  } else {
111  truncate();
112  mEntries.append( entry );
113  mUndoEntry.toLast();
114  mRedoEntry = TQPtrList<Entry>( mEntries );
115  emit undoAvailable( entry->text() );
116  }
117 }
118 
119 void History::recordEdit( Incidence *oldIncidence, Incidence *newIncidence )
120 {
121  Entry *entry = new EntryEdit( mCalendar, oldIncidence, newIncidence );
122  if (mCurrentMultiEntry) {
123  mCurrentMultiEntry->appendEntry( entry );
124  } else {
125  truncate();
126  mEntries.append( entry );
127  mUndoEntry.toLast();
128  mRedoEntry = TQPtrList<Entry>( mEntries );
129  emit undoAvailable( entry->text() );
130  }
131 }
132 
133 void History::startMultiModify( const TQString &description )
134 {
135  if ( mCurrentMultiEntry ) {
136  endMultiModify();
137  }
138  mCurrentMultiEntry = new MultiEntry( mCalendar, description );
139  truncate();
140  mEntries.append( mCurrentMultiEntry );
141  mUndoEntry.toLast();
142  mRedoEntry = TQPtrList<Entry>( mEntries );
143  emit undoAvailable( mCurrentMultiEntry->text() );
144 }
145 
146 void History::endMultiModify()
147 {
148  mCurrentMultiEntry = 0;
149 }
150 
151 
152 History::Entry::Entry( KCal::Calendar *calendar )
153  : mCalendar( calendar )
154 {
155 }
156 
157 History::Entry::~Entry()
158 {
159 }
160 
161 History::EntryDelete::EntryDelete( Calendar *calendar, Incidence *incidence )
162  : Entry( calendar ), mIncidence( incidence->clone() )
163 {
164 }
165 
166 History::EntryDelete::~EntryDelete()
167 {
168  delete mIncidence;
169 }
170 
171 void History::EntryDelete::undo()
172 {
173  // TODO: Use the proper resource instead of asking!
174  mCalendar->addIncidence( mIncidence->clone() );
175 }
176 
177 void History::EntryDelete::redo()
178 {
179  Incidence *incidence = mCalendar->incidence( mIncidence->uid() );
180  mCalendar->deleteIncidence( incidence );
181 }
182 
183 TQString History::EntryDelete::text()
184 {
185  return i18n("Delete %1").arg(mIncidence->type().data());
186 }
187 
188 
189 History::EntryAdd::EntryAdd( Calendar *calendar, Incidence *incidence )
190  : Entry( calendar ), mIncidence( incidence->clone() )
191 {
192 }
193 
194 History::EntryAdd::~EntryAdd()
195 {
196  delete mIncidence;
197 }
198 
199 void History::EntryAdd::undo()
200 {
201  Incidence *incidence = mCalendar->incidence( mIncidence->uid() );
202  if ( incidence )
203  mCalendar->deleteIncidence( incidence );
204 }
205 
206 void History::EntryAdd::redo()
207 {
208  // TODO: User the proper resource instead of asking again
209  mCalendar->addIncidence( mIncidence->clone() );
210 }
211 
212 TQString History::EntryAdd::text()
213 {
214  return i18n("Add %1").arg(mIncidence->type().data());
215 }
216 
217 
218 History::EntryEdit::EntryEdit( Calendar *calendar, Incidence *oldIncidence,
219  Incidence *newIncidence )
220  : Entry( calendar ), mOldIncidence( oldIncidence->clone() ),
221  mNewIncidence( newIncidence->clone() )
222 {
223 }
224 
225 History::EntryEdit::~EntryEdit()
226 {
227  delete mOldIncidence;
228  delete mNewIncidence;
229 }
230 
231 void History::EntryEdit::undo()
232 {
233  Incidence *incidence = mCalendar->incidence( mNewIncidence->uid() );
234  if ( incidence )
235  mCalendar->deleteIncidence( incidence );
236  // TODO: Use the proper resource instead of asking again
237  mCalendar->addIncidence( mOldIncidence->clone() );
238 }
239 
240 void History::EntryEdit::redo()
241 {
242  Incidence *incidence = mCalendar->incidence( mOldIncidence->uid() );
243  if ( incidence )
244  mCalendar->deleteIncidence( incidence );
245  // TODO: Use the proper resource instead of asking again
246  mCalendar->addIncidence( mNewIncidence->clone() );
247 }
248 
249 TQString History::EntryEdit::text()
250 {
251  return i18n("Edit %1").arg(mNewIncidence->type().data());
252 }
253 
254 History::MultiEntry::MultiEntry( Calendar *calendar, const TQString &text )
255  : Entry( calendar ), mText( text )
256 {
257  mEntries.setAutoDelete( true );
258 }
259 
260 History::MultiEntry::~MultiEntry()
261 {
262 }
263 
264 void History::MultiEntry::appendEntry( Entry* entry )
265 {
266  mEntries.append( entry );
267 }
268 
269 void History::MultiEntry::undo()
270 {
271  TQPtrListIterator<Entry> it( mEntries );
272  it.toLast();
273  Entry *entry;
274  while ( (entry = it.current()) != 0 ) {
275  --it;
276  entry->undo();
277  }
278 }
279 
280 void History::MultiEntry::redo()
281 {
282  TQPtrListIterator<Entry> it( mEntries );
283  Entry *entry;
284  while ( (entry = it.current()) != 0 ) {
285  ++it;
286  entry->redo();
287  }
288 }
289 
290 TQString History::MultiEntry::text()
291 {
292  return mText;
293 }
294 
295 #include "history.moc"