RemoteDrawingEngine: Reduce RP_READ_BITMAP result timeout.
[haiku.git] / src / kits / tracker / FSUndoRedo.cpp
blobad6c798cf38cd147e9380182b67fa11f24dce6fc
1 /*
2 Open Tracker License
4 Terms and Conditions
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.
32 All rights reserved.
36 #include "Commands.h"
37 #include "FSUndoRedo.h"
38 #include "FSUtils.h"
40 #include <Autolock.h>
41 #include <Volume.h>
42 #include <Node.h>
43 #include <Path.h>
46 static const int32 kUndoRedoListMaxCount = 20;
49 namespace BPrivate {
51 class UndoItem {
52 public:
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 {
66 public:
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);
75 private:
76 BObjectList<entry_ref> fSourceList;
77 BObjectList<entry_ref> fTargetList;
78 entry_ref fSourceRef, fTargetRef;
79 uint32 fMoveMode;
83 class UndoItemMove : public UndoItem {
84 public:
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,
89 BList* pointList);
90 virtual ~UndoItemMove();
92 virtual status_t Undo();
93 virtual status_t Redo();
95 private:
96 BObjectList<entry_ref> fSourceList;
97 entry_ref fSourceRef, fTargetRef;
101 class UndoItemFolder : public UndoItem {
102 public:
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();
110 private:
111 // this ref has two different meanings in the different states of
112 // this object:
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
117 entry_ref fRef;
121 class UndoItemRename : public UndoItem {
122 public:
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();
130 private:
131 entry_ref fRef, fOrigRef;
135 class UndoItemRenameVolume : public UndoItem {
136 public:
137 UndoItemRenameVolume(BVolume &volume, const char* newName);
138 virtual ~UndoItemRenameVolume();
140 virtual status_t Undo();
141 virtual status_t Redo();
143 private:
144 BVolume fVolume;
145 BString fOldName, fNewName;
149 //--------------------------
152 static status_t
153 ChangeListSource(BObjectList<entry_ref> &list, BEntry &entry)
155 node_ref source;
156 if (entry.GetNodeRef(&source) != B_OK)
157 return B_ERROR;
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;
166 return B_OK;
170 static void
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
187 Undo::~Undo()
189 if (fUndo != NULL)
190 AddUndoItem(fUndo);
194 void
195 Undo::UpdateEntry(BEntry* entry, const char* destName)
197 if (fUndo != NULL)
198 fUndo->UpdateEntry(entry, destName);
202 void
203 Undo::Remove()
205 delete fUndo;
206 fUndo = NULL;
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);
215 else
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),
246 fMoveMode(moveMode)
248 BEntry entry(sourceList->ItemAt(0));
250 BEntry sourceEntry;
251 entry.GetParent(&sourceEntry);
252 sourceEntry.GetRef(&fSourceRef);
254 BEntry targetEntry;
255 target.GetEntry(&targetEntry);
256 targetEntry.GetRef(&fTargetRef);
257 ChangeListSource(fTargetList, targetEntry);
261 UndoItemCopy::~UndoItemCopy()
266 status_t
267 UndoItemCopy::Undo()
269 FSDeleteRefList(new BObjectList<entry_ref>(fTargetList), true, false);
270 return B_OK;
274 status_t
275 UndoItemCopy::Redo()
277 FSMoveToFolder(new BObjectList<entry_ref>(fSourceList),
278 new BEntry(&fTargetRef), FSUndoMoveMode(fMoveMode), NULL);
280 return B_OK;
284 void
285 UndoItemCopy::UpdateEntry(BEntry* entry, const char* name)
287 entry_ref changedRef;
288 if (entry->GetRef(&changedRef) != B_OK)
289 return;
291 for (int32 index = 0; index < fSourceList.CountItems(); index++) {
292 entry_ref* ref = fSourceList.ItemAt(index);
293 if (changedRef != *ref)
294 continue;
296 ref = fTargetList.ItemAt(index);
297 ref->set_name(name);
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));
311 BEntry source;
312 entry.GetParent(&source);
313 source.GetRef(&fSourceRef);
315 BEntry targetEntry;
316 target.GetEntry(&targetEntry);
317 targetEntry.GetRef(&fTargetRef);
321 UndoItemMove::~UndoItemMove()
326 status_t
327 UndoItemMove::Undo()
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);
337 return B_OK;
341 status_t
342 UndoItemMove::Redo()
344 // FSMoveToFolder() owns its arguments
345 FSMoveToFolder(new BObjectList<entry_ref>(fSourceList),
346 new BEntry(&fTargetRef), FSUndoMoveMode(kMoveSelectionTo), NULL);
348 return B_OK;
352 // #pragma mark -
355 UndoItemFolder::UndoItemFolder(const entry_ref &ref)
357 fRef(ref)
362 UndoItemFolder::~UndoItemFolder()
367 status_t
368 UndoItemFolder::Undo()
370 FSDelete(new entry_ref(fRef), false, false);
371 return B_OK;
375 status_t
376 UndoItemFolder::Redo()
378 return FSCreateNewFolder(&fRef);
382 // #pragma mark -
385 UndoItemRename::UndoItemRename(const entry_ref &origRef, const entry_ref &ref)
387 fRef(ref),
388 fOrigRef(origRef)
393 UndoItemRename::UndoItemRename(const BEntry &entry, const char* newName)
395 entry.GetRef(&fOrigRef);
397 fRef = fOrigRef;
398 fRef.set_name(newName);
402 UndoItemRename::~UndoItemRename()
407 status_t
408 UndoItemRename::Undo()
410 BEntry entry(&fRef, false);
411 return entry.Rename(fOrigRef.name);
415 status_t
416 UndoItemRename::Redo()
418 BEntry entry(&fOrigRef, false);
419 return entry.Rename(fRef.name);
423 // #pragma mark - UndoItemRenameVolume
426 UndoItemRenameVolume::UndoItemRenameVolume(BVolume &volume,
427 const char* newName)
429 fVolume(volume),
430 fNewName(newName)
432 char* buffer = fOldName.LockBuffer(B_FILE_NAME_LENGTH);
433 if (buffer != NULL) {
434 fVolume.GetName(buffer);
435 fOldName.UnlockBuffer();
440 UndoItemRenameVolume::~UndoItemRenameVolume()
445 status_t
446 UndoItemRenameVolume::Undo()
448 return fVolume.SetName(fOldName.String());
452 status_t
453 UndoItemRenameVolume::Redo()
455 return fVolume.SetName(fNewName.String());
459 // #pragma mark - FSUndo() and FSRedo() functions
462 void
463 FSUndo()
465 BAutolock locker(sLock);
467 UndoItem* undoItem = sUndoList.FirstItem();
468 if (undoItem == NULL)
469 return;
471 undoItem->Undo();
472 // ToDo: evaluate return code
474 sUndoList.RemoveItem(undoItem);
476 if (sRedoList.CountItems() == kUndoRedoListMaxCount)
477 sRedoList.RemoveItem(sRedoList.LastItem());
479 sRedoList.AddItem(undoItem, 0);
483 void
484 FSRedo()
486 BAutolock locker(sLock);
488 UndoItem* undoItem = sRedoList.FirstItem();
489 if (undoItem == NULL)
490 return;
492 undoItem->Redo();
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