1 /* 2 ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 ** Distributed under the terms of the OpenBeOS License. 4 */ 5 #ifndef DATA_EDITOR_H 6 #define DATA_EDITOR_H 7 8 9 #include <File.h> 10 #include <Entry.h> 11 #include <Locker.h> 12 #include <ObjectList.h> 13 14 15 class DataChange; 16 17 class DataEditor : public BLocker { 18 public: 19 DataEditor(); 20 DataEditor(entry_ref &ref, const char *attribute = NULL); 21 DataEditor(BEntry &entry, const char *attribute = NULL); 22 DataEditor(const DataEditor &editor); 23 ~DataEditor(); 24 25 status_t SetTo(const char *path, const char *attribute = NULL); 26 status_t SetTo(entry_ref &ref, const char *attribute = NULL); 27 status_t SetTo(BEntry &entry, const char *attribute = NULL); 28 status_t SetToAttribute(const char *attribute); 29 30 bool IsReadOnly() const { return fIsReadOnly; } 31 bool IsDevice() const { return fIsDevice; } 32 bool IsAttribute() const { return fAttribute != NULL; } 33 //bool IsModified() const { return fIsModified; } 34 35 const char *Attribute() const { return fAttribute; } 36 37 status_t InitCheck(); 38 39 status_t Replace(off_t offset, const uint8 *data, size_t length); 40 status_t Remove(off_t offset, off_t length); 41 status_t Insert(off_t offset, const uint8 *data, size_t length); 42 43 status_t MoveBy(int32 bytes); 44 status_t MoveTo(off_t offset); 45 46 status_t Undo(); 47 status_t Redo(); 48 49 bool CanUndo() const; 50 bool CanRedo() const; 51 52 status_t SetFileSize(off_t size); 53 off_t FileSize() const { return fSize; } 54 55 status_t SetViewOffset(off_t offset); 56 off_t ViewOffset() const { return fViewOffset; } 57 58 status_t SetViewSize(size_t size); 59 size_t ViewSize() const { return fViewSize; } 60 61 void SetBlockSize(size_t size); 62 size_t BlockSize() const { return fBlockSize; } 63 64 status_t UpdateIfNeeded(bool *_updated = NULL); 65 status_t GetViewBuffer(const uint8 **_buffer); 66 67 status_t StartWatching(BMessenger target); 68 status_t StartWatching(BHandler *handler, BLooper *looper = NULL); 69 void StopWatching(BMessenger target); 70 void StopWatching(BHandler *handler, BLooper *looper = NULL); 71 72 BFile &File() { return fFile; } 73 const entry_ref &Ref() const { return fRef; } 74 75 private: 76 void SendNotices(uint32 what, BMessage *message = NULL); 77 status_t Update(); 78 void AddChange(DataChange *change); 79 void ApplyChanges(); 80 void RemoveRedos(); 81 82 BObjectList<BMessenger> fObservers; 83 84 entry_ref fRef; 85 BFile fFile; 86 const char *fAttribute; 87 bool fIsDevice, fIsReadOnly; 88 off_t fRealSize, fSize; 89 90 BObjectList<DataChange> fChanges; 91 DataChange *fFirstChange; 92 DataChange *fLastChange; 93 94 uint8 *fView; 95 off_t fRealViewOffset, fViewOffset; 96 size_t fRealViewSize, fViewSize; 97 bool fNeedsUpdate; 98 99 size_t fBlockSize; 100 }; 101 102 static const uint32 kMsgDataEditorStateChange = 'deSC'; 103 static const uint32 kMsgDataEditorUpdate = 'deUp'; 104 static const uint32 kMsgDataEditorOffsetChange = 'deOC'; 105 106 #endif /* DATA_EDITOR_H */ 107