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 type_code Type() const { return fType; } 37 38 status_t InitCheck(); 39 40 status_t Replace(off_t offset, const uint8 *data, size_t length); 41 status_t Remove(off_t offset, off_t length); 42 status_t Insert(off_t offset, const uint8 *data, size_t length); 43 44 status_t MoveBy(int32 bytes); 45 status_t MoveTo(off_t offset); 46 47 status_t Undo(); 48 status_t Redo(); 49 50 bool CanUndo() const; 51 bool CanRedo() const; 52 53 status_t SetFileSize(off_t size); 54 off_t FileSize() const { return fSize; } 55 56 status_t SetViewOffset(off_t offset); 57 off_t ViewOffset() const { return fViewOffset; } 58 59 status_t SetViewSize(size_t size); 60 size_t ViewSize() const { return fViewSize; } 61 62 void SetBlockSize(size_t size); 63 size_t BlockSize() const { return fBlockSize; } 64 65 status_t UpdateIfNeeded(bool *_updated = NULL); 66 status_t GetViewBuffer(const uint8 **_buffer); 67 68 status_t StartWatching(BMessenger target); 69 status_t StartWatching(BHandler *handler, BLooper *looper = NULL); 70 void StopWatching(BMessenger target); 71 void StopWatching(BHandler *handler, BLooper *looper = NULL); 72 73 BFile &File() { return fFile; } 74 const entry_ref &Ref() const { return fRef; } 75 76 private: 77 void SendNotices(uint32 what, BMessage *message = NULL); 78 status_t Update(); 79 void AddChange(DataChange *change); 80 void ApplyChanges(); 81 void RemoveRedos(); 82 83 BObjectList<BMessenger> fObservers; 84 85 entry_ref fRef; 86 BFile fFile; 87 const char *fAttribute; 88 type_code fType; 89 bool fIsDevice, fIsReadOnly; 90 off_t fRealSize, fSize; 91 92 BObjectList<DataChange> fChanges; 93 DataChange *fFirstChange; 94 DataChange *fLastChange; 95 96 uint8 *fView; 97 off_t fRealViewOffset, fViewOffset; 98 size_t fRealViewSize, fViewSize; 99 bool fNeedsUpdate; 100 101 size_t fBlockSize; 102 }; 103 104 static const uint32 kMsgDataEditorStateChange = 'deSC'; 105 static const uint32 kMsgDataEditorUpdate = 'deUp'; 106 static const uint32 kMsgDataEditorOffsetChange = 'deOC'; 107 108 #endif /* DATA_EDITOR_H */ 109