6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
37 #include "FSUndoRedo.h"
46 static const int32 kUndoRedoListMaxCount
= 20;
53 virtual ~UndoItem() {}
55 virtual status_t
Undo() = 0;
56 virtual status_t
Redo() = 0;
58 virtual void UpdateEntry(BEntry
* /*entry*/, const char* /*name*/) {}
59 // updates the name of the target from the source entry "entry"
62 static BObjectList
<UndoItem
> sUndoList
, sRedoList
;
63 static BLocker
sLock("undo");
65 class UndoItemCopy
: public UndoItem
{
67 UndoItemCopy(BObjectList
<entry_ref
>* sourceList
, BDirectory
&target
,
68 BList
* pointList
, uint32 moveMode
);
69 virtual ~UndoItemCopy();
71 virtual status_t
Undo();
72 virtual status_t
Redo();
73 virtual void UpdateEntry(BEntry
* entry
, const char* name
);
76 BObjectList
<entry_ref
> fSourceList
;
77 BObjectList
<entry_ref
> fTargetList
;
78 entry_ref fSourceRef
, fTargetRef
;
83 class UndoItemMove
: public UndoItem
{
85 /** source - list of file(s) that were moved. Assumes ownership.
86 * origfolder - location it was moved from
88 UndoItemMove(BObjectList
<entry_ref
>* sourceList
, BDirectory
&target
,
90 virtual ~UndoItemMove();
92 virtual status_t
Undo();
93 virtual status_t
Redo();
96 BObjectList
<entry_ref
> fSourceList
;
97 entry_ref fSourceRef
, fTargetRef
;
101 class UndoItemFolder
: public UndoItem
{
103 UndoItemFolder(const entry_ref
&ref
);
104 // ref - entry_ref indicating the folder created
105 virtual ~UndoItemFolder();
107 virtual status_t
Undo();
108 virtual status_t
Redo();
111 // this ref has two different meanings in the different states of
113 // - Undo() - fRef indicates the folder that was created via
114 // FSCreateNewFolderIn(...)
115 // - Redo() - fRef indicates the folder in which
116 // FSCreateNewFolderIn() should be performed
121 class UndoItemRename
: public UndoItem
{
123 UndoItemRename(const entry_ref
&origRef
, const entry_ref
&ref
);
124 UndoItemRename(const BEntry
&entry
, const char* newName
);
125 virtual ~UndoItemRename();
127 virtual status_t
Undo();
128 virtual status_t
Redo();
131 entry_ref fRef
, fOrigRef
;
135 class UndoItemRenameVolume
: public UndoItem
{
137 UndoItemRenameVolume(BVolume
&volume
, const char* newName
);
138 virtual ~UndoItemRenameVolume();
140 virtual status_t
Undo();
141 virtual status_t
Redo();
145 BString fOldName
, fNewName
;
149 //--------------------------
153 ChangeListSource(BObjectList
<entry_ref
> &list
, BEntry
&entry
)
156 if (entry
.GetNodeRef(&source
) != B_OK
)
159 for (int32 index
= 0; index
< list
.CountItems(); index
++) {
160 entry_ref
* ref
= list
.ItemAt(index
);
162 ref
->device
= source
.device
;
163 ref
->directory
= source
.node
;
171 AddUndoItem(UndoItem
* item
)
173 BAutolock
locker(sLock
);
175 // we have a restricted number of possible undos
176 if (sUndoList
.CountItems() == kUndoRedoListMaxCount
)
177 sUndoList
.RemoveItem(sUndoList
.LastItem());
179 sUndoList
.AddItem(item
, 0);
180 sRedoList
.MakeEmpty();
184 // #pragma mark - Undo
195 Undo::UpdateEntry(BEntry
* entry
, const char* destName
)
198 fUndo
->UpdateEntry(entry
, destName
);
210 MoveCopyUndo::MoveCopyUndo(BObjectList
<entry_ref
>* sourceList
,
211 BDirectory
&dest
, BList
* pointList
, uint32 moveMode
)
213 if (moveMode
== kMoveSelectionTo
)
214 fUndo
= new UndoItemMove(sourceList
, dest
, pointList
);
216 fUndo
= new UndoItemCopy(sourceList
, dest
, pointList
, moveMode
);
220 NewFolderUndo::NewFolderUndo(const entry_ref
&ref
)
222 fUndo
= new UndoItemFolder(ref
);
226 RenameUndo::RenameUndo(BEntry
&entry
, const char* newName
)
228 fUndo
= new UndoItemRename(entry
, newName
);
232 RenameVolumeUndo::RenameVolumeUndo(BVolume
&volume
, const char* newName
)
234 fUndo
= new UndoItemRenameVolume(volume
, newName
);
238 // #pragma mark - UndoItemCopy
241 UndoItemCopy::UndoItemCopy(BObjectList
<entry_ref
>* sourceList
,
242 BDirectory
&target
, BList
* /*pointList*/, uint32 moveMode
)
244 fSourceList(*sourceList
),
245 fTargetList(*sourceList
),
248 BEntry
entry(sourceList
->ItemAt(0));
251 entry
.GetParent(&sourceEntry
);
252 sourceEntry
.GetRef(&fSourceRef
);
255 target
.GetEntry(&targetEntry
);
256 targetEntry
.GetRef(&fTargetRef
);
257 ChangeListSource(fTargetList
, targetEntry
);
261 UndoItemCopy::~UndoItemCopy()
269 FSDeleteRefList(new BObjectList
<entry_ref
>(fTargetList
), true, false);
277 FSMoveToFolder(new BObjectList
<entry_ref
>(fSourceList
),
278 new BEntry(&fTargetRef
), FSUndoMoveMode(fMoveMode
), NULL
);
285 UndoItemCopy::UpdateEntry(BEntry
* entry
, const char* name
)
287 entry_ref changedRef
;
288 if (entry
->GetRef(&changedRef
) != B_OK
)
291 for (int32 index
= 0; index
< fSourceList
.CountItems(); index
++) {
292 entry_ref
* ref
= fSourceList
.ItemAt(index
);
293 if (changedRef
!= *ref
)
296 ref
= fTargetList
.ItemAt(index
);
302 // #pragma mark - UndoItemMove
305 UndoItemMove::UndoItemMove(BObjectList
<entry_ref
>* sourceList
,
306 BDirectory
&target
, BList
* /*pointList*/)
308 fSourceList(*sourceList
)
310 BEntry
entry(sourceList
->ItemAt(0));
312 entry
.GetParent(&source
);
313 source
.GetRef(&fSourceRef
);
316 target
.GetEntry(&targetEntry
);
317 targetEntry
.GetRef(&fTargetRef
);
321 UndoItemMove::~UndoItemMove()
329 BObjectList
<entry_ref
>* list
= new BObjectList
<entry_ref
>(fSourceList
);
330 BEntry
entry(&fTargetRef
);
331 ChangeListSource(*list
, entry
);
333 // FSMoveToFolder() owns its arguments
334 FSMoveToFolder(list
, new BEntry(&fSourceRef
),
335 FSUndoMoveMode(kMoveSelectionTo
), NULL
);
344 // FSMoveToFolder() owns its arguments
345 FSMoveToFolder(new BObjectList
<entry_ref
>(fSourceList
),
346 new BEntry(&fTargetRef
), FSUndoMoveMode(kMoveSelectionTo
), NULL
);
355 UndoItemFolder::UndoItemFolder(const entry_ref
&ref
)
362 UndoItemFolder::~UndoItemFolder()
368 UndoItemFolder::Undo()
370 FSDelete(new entry_ref(fRef
), false, false);
376 UndoItemFolder::Redo()
378 return FSCreateNewFolder(&fRef
);
385 UndoItemRename::UndoItemRename(const entry_ref
&origRef
, const entry_ref
&ref
)
393 UndoItemRename::UndoItemRename(const BEntry
&entry
, const char* newName
)
395 entry
.GetRef(&fOrigRef
);
398 fRef
.set_name(newName
);
402 UndoItemRename::~UndoItemRename()
408 UndoItemRename::Undo()
410 BEntry
entry(&fRef
, false);
411 return entry
.Rename(fOrigRef
.name
);
416 UndoItemRename::Redo()
418 BEntry
entry(&fOrigRef
, false);
419 return entry
.Rename(fRef
.name
);
423 // #pragma mark - UndoItemRenameVolume
426 UndoItemRenameVolume::UndoItemRenameVolume(BVolume
&volume
,
432 char* buffer
= fOldName
.LockBuffer(B_FILE_NAME_LENGTH
);
433 if (buffer
!= NULL
) {
434 fVolume
.GetName(buffer
);
435 fOldName
.UnlockBuffer();
440 UndoItemRenameVolume::~UndoItemRenameVolume()
446 UndoItemRenameVolume::Undo()
448 return fVolume
.SetName(fOldName
.String());
453 UndoItemRenameVolume::Redo()
455 return fVolume
.SetName(fNewName
.String());
459 // #pragma mark - FSUndo() and FSRedo() functions
465 BAutolock
locker(sLock
);
467 UndoItem
* undoItem
= sUndoList
.FirstItem();
468 if (undoItem
== NULL
)
472 // ToDo: evaluate return code
474 sUndoList
.RemoveItem(undoItem
);
476 if (sRedoList
.CountItems() == kUndoRedoListMaxCount
)
477 sRedoList
.RemoveItem(sRedoList
.LastItem());
479 sRedoList
.AddItem(undoItem
, 0);
486 BAutolock
locker(sLock
);
488 UndoItem
* undoItem
= sRedoList
.FirstItem();
489 if (undoItem
== NULL
)
493 // ToDo: evaluate return code
495 sRedoList
.RemoveItem(undoItem
);
497 if (sUndoList
.CountItems() == kUndoRedoListMaxCount
)
498 sUndoList
.RemoveItem(sUndoList
.LastItem());
500 sUndoList
.AddItem(undoItem
, 0);
503 } // namespace BPrivate