1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
|
/*
* undo.cpp - undo/redo facility
* Program: kalarm
* Copyright © 2005,2006 by David Jarvie <software@astrojar.org.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kalarm.h"
#include <tqobject.h>
#include <tqstringlist.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kdebug.h>
#include "alarmcalendar.h"
#include "alarmevent.h"
#include "alarmtext.h"
#include "functions.h"
#include "undo.moc"
static int maxCount = 12;
class UndoItem
{
public:
enum Operation { ADD, EDIT, DELETE, REACTIVATE, DEACTIVATE, MULTI };
UndoItem(); // needed by TQValueList
virtual ~UndoItem();
virtual Operation operation() const = 0;
virtual TQString actionText() const = 0;
virtual TQString description() const { return TQString(); }
virtual TQString eventID() const { return TQString(); }
virtual TQString oldEventID() const { return TQString(); }
virtual TQString newEventID() const { return TQString(); }
int id() const { return mId; }
Undo::Type type() const { return mType; }
void setType(Undo::Type t) { mType = t; }
KAEvent::Status calendar() const { return mCalendar; }
virtual void setCalendar(KAEvent::Status s) { mCalendar = s; }
virtual UndoItem* restore() = 0;
virtual bool deleteID(const TQString& /*id*/) { return false; }
enum Error { ERR_NONE, ERR_PROG, ERR_NOT_FOUND, ERR_CREATE, ERR_TEMPLATE, ERR_EXPIRED };
enum Warning { WARN_NONE, WARN_KORG_ADD, WARN_KORG_MODIFY, WARN_KORG_DELETE };
static int mLastId;
static Error mRestoreError; // error code valid only if restore() returns 0
static Warning mRestoreWarning; // warning code set by restore()
static int mRestoreWarningCount; // item count for mRestoreWarning (to allow i18n messages to work correctly)
protected:
UndoItem(Undo::Type);
static TQString addDeleteActionText(KAEvent::Status, bool add);
TQString description(const KAEvent&) const;
void replaceWith(UndoItem* item) { Undo::replace(this, item); }
int mId; // unique identifier (only for mType = UNDO, REDO)
Undo::Type mType; // which list (if any) the object is in
KAEvent::Status mCalendar;
};
class UndoMultiBase : public UndoItem
{
public:
UndoMultiBase(Undo::Type t) : UndoItem(t) { }
UndoMultiBase(Undo::Type t, Undo::List& undos) : UndoItem(t), mUndos(undos) { }
~UndoMultiBase();
const Undo::List& undos() const { return mUndos; }
protected:
Undo::List mUndos; // this list must always have >= 2 entries
};
template <class T> class UndoMulti : public UndoMultiBase
{
public:
UndoMulti(Undo::Type, const TQValueList<KAEvent>&);
UndoMulti(Undo::Type t, Undo::List& undos) : UndoMultiBase(t, undos) { }
virtual Operation operation() const { return MULTI; }
virtual UndoItem* restore();
virtual bool deleteID(const TQString& id);
virtual UndoItem* createRedo(Undo::List&) = 0;
};
class UndoAdd : public UndoItem
{
public:
UndoAdd(Undo::Type, const KAEvent&);
UndoAdd(Undo::Type, const KAEvent&, KAEvent::Status);
virtual Operation operation() const { return ADD; }
virtual TQString actionText() const;
virtual TQString description() const { return mDescription; }
virtual TQString eventID() const { return mEventID; }
virtual TQString newEventID() const { return mEventID; }
virtual UndoItem* restore() { return doRestore(); }
protected:
UndoItem* doRestore(bool setArchive = false);
virtual UndoItem* createRedo(const KAEvent&);
private:
TQString mEventID;
TQString mDescription;
};
class UndoEdit : public UndoItem
{
public:
UndoEdit(Undo::Type, const KAEvent& oldEvent, const TQString& newEventID, const TQString& description);
~UndoEdit();
virtual Operation operation() const { return EDIT; }
virtual TQString actionText() const;
virtual TQString description() const { return mDescription; }
virtual TQString eventID() const { return mNewEventID; }
virtual TQString oldEventID() const { return mOldEvent->id(); }
virtual TQString newEventID() const { return mNewEventID; }
virtual UndoItem* restore();
private:
KAEvent* mOldEvent;
TQString mNewEventID;
TQString mDescription;
};
class UndoDelete : public UndoItem
{
public:
UndoDelete(Undo::Type, const KAEvent&);
~UndoDelete();
virtual Operation operation() const { return DELETE; }
virtual TQString actionText() const;
virtual TQString description() const { return UndoItem::description(*mEvent); }
virtual TQString eventID() const { return mEvent->id(); }
virtual TQString oldEventID() const { return mEvent->id(); }
virtual UndoItem* restore();
KAEvent* event() const { return mEvent; }
protected:
virtual UndoItem* createRedo(const KAEvent&);
private:
KAEvent* mEvent;
};
class UndoReactivate : public UndoAdd
{
public:
UndoReactivate(Undo::Type t, const KAEvent& e) : UndoAdd(t, e, KAEvent::ACTIVE) { }
virtual Operation operation() const { return REACTIVATE; }
virtual TQString actionText() const;
virtual UndoItem* restore();
protected:
virtual UndoItem* createRedo(const KAEvent&);
};
class UndoDeactivate : public UndoDelete
{
public:
UndoDeactivate(Undo::Type t, const KAEvent& e) : UndoDelete(t, e) { }
virtual Operation operation() const { return DEACTIVATE; }
virtual TQString actionText() const;
virtual UndoItem* restore();
protected:
virtual UndoItem* createRedo(const KAEvent&);
};
class UndoDeletes : public UndoMulti<UndoDelete>
{
public:
UndoDeletes(Undo::Type t, const TQValueList<KAEvent>& events)
: UndoMulti<UndoDelete>(t, events) { } // UNDO only
UndoDeletes(Undo::Type t, Undo::List& undos)
: UndoMulti<UndoDelete>(t, undos) { }
virtual TQString actionText() const;
virtual UndoItem* createRedo(Undo::List&);
};
class UndoReactivates : public UndoMulti<UndoReactivate>
{
public:
UndoReactivates(Undo::Type t, const TQValueList<KAEvent>& events)
: UndoMulti<UndoReactivate>(t, events) { } // UNDO only
UndoReactivates(Undo::Type t, Undo::List& undos)
: UndoMulti<UndoReactivate>(t, undos) { }
virtual TQString actionText() const;
virtual UndoItem* createRedo(Undo::List&);
};
Undo* Undo::mInstance = 0;
Undo::List Undo::mUndoList;
Undo::List Undo::mRedoList;
/******************************************************************************
* Create the one and only instance of the Undo class.
*/
Undo* Undo::instance()
{
if (!mInstance)
mInstance = new Undo(kapp);
return mInstance;
}
/******************************************************************************
* Clear the lists of undo and redo items.
*/
void Undo::clear()
{
if (!mUndoList.isEmpty() || !mRedoList.isEmpty())
{
mInstance->blockSignals(true);
while (mUndoList.count())
delete mUndoList.first(); // N.B. 'delete' removes the object from the list
while (mRedoList.count())
delete mRedoList.first(); // N.B. 'delete' removes the object from the list
mInstance->blockSignals(false);
emitChanged();
}
}
/******************************************************************************
* Create an undo item and add it to the list of undos.
* N.B. The base class constructor adds the object to the undo list.
*/
void Undo::saveAdd(const KAEvent& event)
{
new UndoAdd(UNDO, event);
emitChanged();
}
void Undo::saveEdit(const KAEvent& oldEvent, const KAEvent& newEvent)
{
new UndoEdit(UNDO, oldEvent, newEvent.id(), AlarmText::summary(newEvent));
removeRedos(oldEvent.id()); // remove any redos which are made invalid by this edit
emitChanged();
}
void Undo::saveDelete(const KAEvent& event)
{
new UndoDelete(UNDO, event);
removeRedos(event.id()); // remove any redos which are made invalid by this deletion
emitChanged();
}
void Undo::saveDeletes(const TQValueList<KAEvent>& events)
{
int count = events.count();
if (count == 1)
saveDelete(events.first());
else if (count > 1)
{
new UndoDeletes(UNDO, events);
for (TQValueList<KAEvent>::ConstIterator it = events.begin(); it != events.end(); ++it)
removeRedos((*it).id()); // remove any redos which are made invalid by these deletions
emitChanged();
}
}
void Undo::saveReactivate(const KAEvent& event)
{
new UndoReactivate(UNDO, event);
emitChanged();
}
void Undo::saveReactivates(const TQValueList<KAEvent>& events)
{
int count = events.count();
if (count == 1)
saveReactivate(events.first());
else if (count > 1)
{
new UndoReactivates(UNDO, events);
emitChanged();
}
}
/******************************************************************************
* Remove any redos which are made invalid by a new undo.
*/
void Undo::removeRedos(const TQString& eventID)
{
TQString id = eventID;
for (Iterator it = mRedoList.begin(); it != mRedoList.end(); )
{
UndoItem* item = *it;
//kdDebug(5950)<<"removeRedos(): "<<item->eventID()<<" (looking for "<<id<<")"<<endl;
if (item->operation() == UndoItem::MULTI)
{
if (item->deleteID(id))
{
// The old multi-redo was replaced with a new single redo
delete item;
}
++it;
}
else if (item->eventID() == id)
{
if (item->operation() == UndoItem::EDIT)
id = item->oldEventID(); // continue looking for its post-edit ID
item->setType(NONE); // prevent the destructor removing it from the list
delete item;
it = mRedoList.remove(it);
}
else
++it;
}
}
/******************************************************************************
* Undo or redo a specified item.
* Reply = true if success, or if the item no longer exists.
*/
bool Undo::undo(Undo::Iterator it, Undo::Type type, TQWidget* parent, const TQString& action)
{
UndoItem::mRestoreError = UndoItem::ERR_NONE;
UndoItem::mRestoreWarning = UndoItem::WARN_NONE;
UndoItem::mRestoreWarningCount = 0;
if (it != mUndoList.end() && it != mRedoList.end() && (*it)->type() == type)
{
(*it)->restore();
delete *it; // N.B. 'delete' removes the object from its list
emitChanged();
}
TQString err;
switch (UndoItem::mRestoreError)
{
case UndoItem::ERR_NONE:
{
KAlarm::KOrgUpdateError errcode;
switch (UndoItem::mRestoreWarning)
{
case UndoItem::WARN_KORG_ADD: errcode = KAlarm::KORG_ERR_ADD; break;
case UndoItem::WARN_KORG_MODIFY: errcode = KAlarm::KORG_ERR_MODIFY; break;
case UndoItem::WARN_KORG_DELETE: errcode = KAlarm::KORG_ERR_DELETE; break;
case UndoItem::WARN_NONE:
default:
return true;
}
KAlarm::displayKOrgUpdateError(parent, errcode, UndoItem::mRestoreWarningCount);
return true;
}
case UndoItem::ERR_NOT_FOUND: err = i18n("Alarm not found"); break;
case UndoItem::ERR_CREATE: err = i18n("Error recreating alarm"); break;
case UndoItem::ERR_TEMPLATE: err = i18n("Error recreating alarm template"); break;
case UndoItem::ERR_EXPIRED: err = i18n("Cannot reactivate expired alarm"); break;
case UndoItem::ERR_PROG: err = i18n("Program error"); break;
default: err = i18n("Unknown error"); break;
}
KMessageBox::sorry(parent, i18n("Undo-action: message", "%1: %2").arg(action).arg(err));
return false;
}
/******************************************************************************
* Add an undo item to the start of one of the lists.
*/
void Undo::add(UndoItem* item, bool undo)
{
if (item)
{
// Limit the number of items stored
int undoCount = mUndoList.count();
int redoCount = mRedoList.count();
if (undoCount + redoCount >= maxCount - 1)
{
if (undoCount)
mUndoList.pop_back();
else
mRedoList.pop_back();
}
// Append the new item
List* list = undo ? &mUndoList : &mRedoList;
list->prepend(item);
}
}
/******************************************************************************
* Remove an undo item from one of the lists.
*/
void Undo::remove(UndoItem* item, bool undo)
{
List* list = undo ? &mUndoList : &mRedoList;
if (!list->isEmpty())
list->remove(item);
}
/******************************************************************************
* Replace an undo item in one of the lists.
*/
void Undo::replace(UndoItem* old, UndoItem* New)
{
Type type = old->type();
List* list = (type == UNDO) ? &mUndoList : (type == REDO) ? &mRedoList : 0;
if (!list)
return;
Iterator it = list->find(old);
if (it != list->end())
{
New->setType(type); // ensure the item points to the correct list
*it = New;
old->setType(NONE); // mark the old item as no longer being in a list
}
}
/******************************************************************************
* Return the action description of the latest undo/redo item.
*/
TQString Undo::actionText(Undo::Type type)
{
List* list = (type == UNDO) ? &mUndoList : (type == REDO) ? &mRedoList : 0;
return (list && !list->isEmpty()) ? list->first()->actionText() : TQString();
}
/******************************************************************************
* Return the action description of the undo/redo item with the specified ID.
*/
TQString Undo::actionText(Undo::Type type, int id)
{
UndoItem* undo = getItem(id, type);
return undo ? undo->actionText() : TQString();
}
/******************************************************************************
* Return the alarm description of the undo/redo item with the specified ID.
*/
TQString Undo::description(Undo::Type type, int id)
{
UndoItem* undo = getItem(id, type);
return undo ? undo->description() : TQString();
}
/******************************************************************************
* Return the descriptions of all undo or redo items, in order latest first.
* For alarms which have undergone more than one change, only the first one is
* listed, to force dependent undos to be executed in their correct order.
* If 'ids' is non-null, also returns a list of their corresponding IDs.
*/
TQValueList<int> Undo::ids(Undo::Type type)
{
TQValueList<int> ids;
TQStringList ignoreIDs;
//int n=0;
List* list = (type == UNDO) ? &mUndoList : (type == REDO) ? &mRedoList : 0;
if (!list)
return ids;
for (Iterator it = list->begin(); it != list->end(); ++it)
{
// Check whether this item should be ignored because it is a
// deendent undo. If not, add this item's ID to the ignore list.
UndoItem* item = *it;
bool omit = false;
if (item->operation() == UndoItem::MULTI)
{
// If any item in a multi-undo is disqualified, omit the whole multi-undo
TQStringList newIDs;
const Undo::List& undos = ((UndoMultiBase*)item)->undos();
for (Undo::List::ConstIterator u = undos.begin(); u != undos.end(); ++u)
{
TQString evid = (*u)->eventID();
if (ignoreIDs.find(evid) != ignoreIDs.end())
omit = true;
else if (omit)
ignoreIDs.append(evid);
else
newIDs.append(evid);
}
if (omit)
{
for (TQStringList::ConstIterator i = newIDs.begin(); i != newIDs.end(); ++i)
ignoreIDs.append(*i);
}
}
else
{
omit = (ignoreIDs.find(item->eventID()) != ignoreIDs.end());
if (!omit)
ignoreIDs.append(item->eventID());
if (item->operation() == UndoItem::EDIT)
ignoreIDs.append(item->oldEventID()); // continue looking for its post-edit ID
}
if (!omit)
ids.append(item->id());
//else kdDebug(5950)<<"Undo::ids(): omit "<<item->actionText()<<": "<<item->description()<<endl;
}
//kdDebug(5950)<<"Undo::ids(): "<<n<<" -> "<<ids.count()<<endl;
return ids;
}
/******************************************************************************
* Emit the appropriate 'changed' signal.
*/
void Undo::emitChanged()
{
if (mInstance)
mInstance->emitChanged(actionText(UNDO), actionText(REDO));
}
/******************************************************************************
* Return the item with the specified ID.
*/
UndoItem* Undo::getItem(int id, Undo::Type type)
{
List* list = (type == UNDO) ? &mUndoList : (type == REDO) ? &mRedoList : 0;
if (list)
{
for (Iterator it = list->begin(); it != list->end(); ++it)
{
if ((*it)->id() == id)
return *it;
}
}
return 0;
}
/******************************************************************************
* Find an item with the specified ID.
*/
Undo::Iterator Undo::findItem(int id, Undo::Type type)
{
List* list = (type == UNDO) ? &mUndoList : &mRedoList;
Iterator it;
for (it = list->begin(); it != list->end(); ++it)
{
if ((*it)->id() == id)
break;
}
return it;
}
/*=============================================================================
= Class: UndoItem
= A single undo action.
=============================================================================*/
int UndoItem::mLastId = 0;
UndoItem::Error UndoItem::mRestoreError;
UndoItem::Warning UndoItem::mRestoreWarning;
int UndoItem::mRestoreWarningCount;
/******************************************************************************
* Constructor.
* Optionally appends the undo to the list of undos.
*/
UndoItem::UndoItem(Undo::Type type)
: mId(0),
mType(type)
{
if (type != Undo::NONE)
{
mId = ++mLastId;
if (mId < 0)
mId = mLastId = 1; // wrap round if we reach a negative number
Undo::add(this, (mType == Undo::UNDO));
}
}
/******************************************************************************
* Destructor.
* Removes the undo from the list (if it's in the list).
*/
UndoItem::~UndoItem()
{
if (mType != Undo::NONE)
Undo::remove(this, (mType == Undo::UNDO));
}
/******************************************************************************
* Return the description of an event.
*/
TQString UndoItem::description(const KAEvent& event) const
{
return (mCalendar == KAEvent::TEMPLATE) ? event.templateName() : AlarmText::summary(event);
}
/******************************************************************************
* Return the action description of an add or delete Undo/Redo item for displaying.
*/
TQString UndoItem::addDeleteActionText(KAEvent::Status calendar, bool add)
{
switch (calendar)
{
case KAEvent::ACTIVE:
if (add)
return i18n("Action to create a new alarm", "New alarm");
else
return i18n("Action to delete an alarm", "Delete alarm");
case KAEvent::TEMPLATE:
if (add)
return i18n("Action to create a new alarm template", "New template");
else
return i18n("Action to delete an alarm template", "Delete template");
case KAEvent::EXPIRED:
return i18n("Delete expired alarm");
default:
break;
}
return TQString();
}
/*=============================================================================
= Class: UndoMultiBase
= Undo item for multiple alarms.
=============================================================================*/
template <class T>
UndoMulti<T>::UndoMulti(Undo::Type type, const TQValueList<KAEvent>& events)
: UndoMultiBase(type) // UNDO only
{
for (TQValueList<KAEvent>::ConstIterator it = events.begin(); it != events.end(); ++it)
mUndos.append(new T(Undo::NONE, *it));
}
UndoMultiBase::~UndoMultiBase()
{
for (Undo::List::Iterator it = mUndos.begin(); it != mUndos.end(); ++it)
delete *it;
}
/******************************************************************************
* Undo the item, i.e. restore multiple alarms which were deleted (or delete
* alarms which were restored).
* Create a redo item to delete (or restore) the alarms again.
* Reply = redo item.
*/
template <class T>
UndoItem* UndoMulti<T>::restore()
{
Undo::List newUndos;
for (Undo::List::Iterator it = mUndos.begin(); it != mUndos.end(); ++it)
{
UndoItem* undo = (*it)->restore();
if (undo)
newUndos.append(undo);
}
if (newUndos.isEmpty())
return 0;
// Create a redo item to delete the alarm again
return createRedo(newUndos);
}
/******************************************************************************
* If one of the multiple items has the specified ID, delete it.
* If an item is deleted and there is only one item left, the UndoMulti
* instance is removed from its list and replaced by the remaining UndoItem instead.
* Reply = true if this instance was replaced. The caller must delete it.
* = false otherwise.
*/
template <class T>
bool UndoMulti<T>::deleteID(const TQString& id)
{
for (Undo::List::Iterator it = mUndos.begin(); it != mUndos.end(); ++it)
{
UndoItem* item = *it;
if (item->eventID() == id)
{
// Found a matching entry - remove it
mUndos.remove(it);
if (mUndos.count() == 1)
{
// There is only one entry left after removal.
// Replace 'this' multi instance with the remaining single entry.
replaceWith(item);
return true;
}
else
{
delete item;
return false;
}
}
}
return false;
}
/*=============================================================================
= Class: UndoAdd
= Undo item for alarm creation.
=============================================================================*/
UndoAdd::UndoAdd(Undo::Type type, const KAEvent& event)
: UndoItem(type),
mEventID(event.id())
{
setCalendar(KAEvent::uidStatus(mEventID));
mDescription = UndoItem::description(event); // calendar must be set before calling this
}
UndoAdd::UndoAdd(Undo::Type type, const KAEvent& event, KAEvent::Status cal)
: UndoItem(type),
mEventID(KAEvent::uid(event.id(), cal))
{
setCalendar(cal);
mDescription = UndoItem::description(event); // calendar must be set before calling this
}
/******************************************************************************
* Undo the item, i.e. delete the alarm which was added.
* Create a redo item to add the alarm back again.
* Reply = redo item.
*/
UndoItem* UndoAdd::doRestore(bool setArchive)
{
// Retrieve the current state of the alarm
kdDebug(5950) << "UndoAdd::doRestore(" << mEventID << ")\n";
const KCal::Event* kcalEvent = AlarmCalendar::getEvent(mEventID);
if (!kcalEvent)
{
mRestoreError = ERR_NOT_FOUND; // alarm is no longer in calendar
return 0;
}
KAEvent event(*kcalEvent);
// Create a redo item to recreate the alarm.
// Do it now, since 'event' gets modified by KAlarm::deleteEvent()
UndoItem* undo = createRedo(event);
switch (calendar())
{
case KAEvent::ACTIVE:
if (setArchive)
event.setArchive();
// Archive it if it has already triggered
switch (KAlarm::deleteEvent(event, true))
{
case KAlarm::UPDATE_ERROR:
case KAlarm::UPDATE_FAILED:
case KAlarm::SAVE_FAILED:
mRestoreError = ERR_CREATE;
break;
case KAlarm::UPDATE_KORG_ERR:
mRestoreWarning = WARN_KORG_DELETE;
++mRestoreWarningCount;
break;
default:
break;
}
break;
case KAEvent::TEMPLATE:
if (KAlarm::deleteTemplate(event) != KAlarm::UPDATE_OK)
mRestoreError = ERR_TEMPLATE;
break;
case KAEvent::EXPIRED: // redoing the deletion of an expired alarm
KAlarm::deleteEvent(event);
break;
default:
delete undo;
mRestoreError = ERR_PROG;
return 0;
}
return undo;
}
/******************************************************************************
* Create a redo item to add the alarm back again.
*/
UndoItem* UndoAdd::createRedo(const KAEvent& event)
{
Undo::Type t = (type() == Undo::UNDO) ? Undo::REDO : (type() == Undo::REDO) ? Undo::UNDO : Undo::NONE;
return new UndoDelete(t, event);
}
/******************************************************************************
* Return the action description of the Undo item for displaying.
*/
TQString UndoAdd::actionText() const
{
return addDeleteActionText(calendar(), (type() == Undo::UNDO));
}
/*=============================================================================
= Class: UndoEdit
= Undo item for alarm edit.
=============================================================================*/
UndoEdit::UndoEdit(Undo::Type type, const KAEvent& oldEvent, const TQString& newEventID, const TQString& description)
: UndoItem(type),
mOldEvent(new KAEvent(oldEvent)),
mNewEventID(newEventID),
mDescription(description)
{
setCalendar(KAEvent::uidStatus(mNewEventID));
}
UndoEdit::~UndoEdit()
{
delete mOldEvent;
}
/******************************************************************************
* Undo the item, i.e. undo an edit to a previously existing alarm.
* Create a redo item to reapply the edit.
* Reply = redo item.
*/
UndoItem* UndoEdit::restore()
{
kdDebug(5950) << "UndoEdit::restore(" << mNewEventID << ")\n";
// Retrieve the current state of the alarm
const KCal::Event* kcalEvent = AlarmCalendar::getEvent(mNewEventID);
if (!kcalEvent)
{
mRestoreError = ERR_NOT_FOUND; // alarm is no longer in calendar
return 0;
}
KAEvent newEvent(*kcalEvent);
// Create a redo item to restore the edit
Undo::Type t = (type() == Undo::UNDO) ? Undo::REDO : (type() == Undo::REDO) ? Undo::UNDO : Undo::NONE;
UndoItem* undo = new UndoEdit(t, newEvent, mOldEvent->id(), mDescription);
switch (calendar())
{
case KAEvent::ACTIVE:
switch (KAlarm::modifyEvent(newEvent, *mOldEvent, 0))
{
case KAlarm::UPDATE_ERROR:
case KAlarm::UPDATE_FAILED:
case KAlarm::SAVE_FAILED:
mRestoreError = ERR_CREATE;
break;
case KAlarm::UPDATE_KORG_ERR:
mRestoreWarning = WARN_KORG_MODIFY;
++mRestoreWarningCount;
break;
default:
break;
}
break;
case KAEvent::TEMPLATE:
if (KAlarm::updateTemplate(*mOldEvent, 0) != KAlarm::UPDATE_OK)
mRestoreError = ERR_TEMPLATE;
break;
case KAEvent::EXPIRED: // editing of expired events is not allowed
default:
delete undo;
mRestoreError = ERR_PROG;
return 0;
}
return undo;
}
/******************************************************************************
* Return the action description of the Undo item for displaying.
*/
TQString UndoEdit::actionText() const
{
switch (calendar())
{
case KAEvent::ACTIVE:
return i18n("Action to edit an alarm", "Edit alarm");
case KAEvent::TEMPLATE:
return i18n("Action to edit an alarm template", "Edit template");
default:
break;
}
return TQString();
}
/*=============================================================================
= Class: UndoDelete
= Undo item for alarm deletion.
=============================================================================*/
UndoDelete::UndoDelete(Undo::Type type, const KAEvent& event)
: UndoItem(type),
mEvent(new KAEvent(event))
{
setCalendar(KAEvent::uidStatus(mEvent->id()));
}
UndoDelete::~UndoDelete()
{
delete mEvent;
}
/******************************************************************************
* Undo the item, i.e. restore an alarm which was deleted.
* Create a redo item to delete the alarm again.
* Reply = redo item.
*/
UndoItem* UndoDelete::restore()
{
kdDebug(5950) << "UndoDelete::restore(" << mEvent->id() << ")\n";
// Restore the original event
switch (calendar())
{
case KAEvent::ACTIVE:
if (mEvent->toBeArchived())
{
// It was archived when it was deleted
mEvent->setUid(KAEvent::EXPIRED);
switch (KAlarm::reactivateEvent(*mEvent, 0, true))
{
case KAlarm::UPDATE_KORG_ERR:
mRestoreWarning = WARN_KORG_ADD;
++mRestoreWarningCount;
break;
case KAlarm::UPDATE_ERROR:
case KAlarm::UPDATE_FAILED:
case KAlarm::SAVE_FAILED:
mRestoreError = ERR_EXPIRED;
return 0;
case KAlarm::UPDATE_OK:
break;
}
}
else
{
switch (KAlarm::addEvent(*mEvent, 0, 0, true))
{
case KAlarm::UPDATE_KORG_ERR:
mRestoreWarning = WARN_KORG_ADD;
++mRestoreWarningCount;
break;
case KAlarm::UPDATE_ERROR:
case KAlarm::UPDATE_FAILED:
case KAlarm::SAVE_FAILED:
mRestoreError = ERR_CREATE;
return 0;
case KAlarm::UPDATE_OK:
break;
}
}
break;
case KAEvent::TEMPLATE:
if (KAlarm::addTemplate(*mEvent, 0) != KAlarm::UPDATE_OK)
{
mRestoreError = ERR_CREATE;
return 0;
}
break;
case KAEvent::EXPIRED:
if (!KAlarm::addExpiredEvent(*mEvent))
{
mRestoreError = ERR_CREATE;
return 0;
}
break;
default:
mRestoreError = ERR_PROG;
return 0;
}
// Create a redo item to delete the alarm again
return createRedo(*mEvent);
}
/******************************************************************************
* Create a redo item to archive the alarm again.
*/
UndoItem* UndoDelete::createRedo(const KAEvent& event)
{
Undo::Type t = (type() == Undo::UNDO) ? Undo::REDO : (type() == Undo::REDO) ? Undo::UNDO : Undo::NONE;
return new UndoAdd(t, event);
}
/******************************************************************************
* Return the action description of the Undo item for displaying.
*/
TQString UndoDelete::actionText() const
{
return addDeleteActionText(calendar(), (type() == Undo::REDO));
}
/*=============================================================================
= Class: UndoDeletes
= Undo item for multiple alarm deletion.
=============================================================================*/
/******************************************************************************
* Create a redo item to delete the alarms again.
*/
UndoItem* UndoDeletes::createRedo(Undo::List& undos)
{
Undo::Type t = (type() == Undo::UNDO) ? Undo::REDO : (type() == Undo::REDO) ? Undo::UNDO : Undo::NONE;
return new UndoDeletes(t, undos);
}
/******************************************************************************
* Return the action description of the Undo item for displaying.
*/
TQString UndoDeletes::actionText() const
{
if (mUndos.isEmpty())
return TQString();
for (Undo::List::ConstIterator it = mUndos.begin(); it != mUndos.end(); ++it)
{
switch ((*it)->calendar())
{
case KAEvent::ACTIVE:
return i18n("Delete multiple alarms");
case KAEvent::TEMPLATE:
return i18n("Delete multiple templates");
case KAEvent::EXPIRED:
break; // check if they are ALL expired
default:
return TQString();
}
}
return i18n("Delete multiple expired alarms");
}
/*=============================================================================
= Class: UndoReactivate
= Undo item for alarm reactivation.
=============================================================================*/
/******************************************************************************
* Undo the item, i.e. re-archive the alarm which was reactivated.
* Create a redo item to reactivate the alarm back again.
* Reply = redo item.
*/
UndoItem* UndoReactivate::restore()
{
kdDebug(5950) << "UndoReactivate::restore()\n";
// Validate the alarm's calendar
switch (calendar())
{
case KAEvent::ACTIVE:
break;
default:
mRestoreError = ERR_PROG;
return 0;
}
return UndoAdd::doRestore(true); // restore alarm, ensuring that it is re-archived
}
/******************************************************************************
* Create a redo item to add the alarm back again.
*/
UndoItem* UndoReactivate::createRedo(const KAEvent& event)
{
Undo::Type t = (type() == Undo::UNDO) ? Undo::REDO : (type() == Undo::REDO) ? Undo::UNDO : Undo::NONE;
return new UndoDeactivate(t, event);
}
/******************************************************************************
* Return the action description of the Undo item for displaying.
*/
TQString UndoReactivate::actionText() const
{
return i18n("Reactivate alarm");
}
/*=============================================================================
= Class: UndoDeactivate
= Redo item for alarm reactivation.
=============================================================================*/
/******************************************************************************
* Undo the item, i.e. reactivate an alarm which was archived.
* Create a redo item to archive the alarm again.
* Reply = redo item.
*/
UndoItem* UndoDeactivate::restore()
{
kdDebug(5950) << "UndoDeactivate::restore()\n";
// Validate the alarm's calendar
switch (calendar())
{
case KAEvent::ACTIVE:
break;
default:
mRestoreError = ERR_PROG;
return 0;
}
return UndoDelete::restore();
}
/******************************************************************************
* Create a redo item to archive the alarm again.
*/
UndoItem* UndoDeactivate::createRedo(const KAEvent& event)
{
Undo::Type t = (type() == Undo::UNDO) ? Undo::REDO : (type() == Undo::REDO) ? Undo::UNDO : Undo::NONE;
return new UndoReactivate(t, event);
}
/******************************************************************************
* Return the action description of the Undo item for displaying.
*/
TQString UndoDeactivate::actionText() const
{
return i18n("Reactivate alarm");
}
/*=============================================================================
= Class: UndoReactivates
= Undo item for multiple alarm reactivation.
=============================================================================*/
/******************************************************************************
* Create a redo item to reactivate the alarms again.
*/
UndoItem* UndoReactivates::createRedo(Undo::List& undos)
{
Undo::Type t = (type() == Undo::UNDO) ? Undo::REDO : (type() == Undo::REDO) ? Undo::UNDO : Undo::NONE;
return new UndoReactivates(t, undos);
}
/******************************************************************************
* Return the action description of the Undo item for displaying.
*/
TQString UndoReactivates::actionText() const
{
return i18n("Reactivate multiple alarms");
}
|