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 void SendNotices(DataChange *change); 79 status_t Update(); 80 void AddChange(DataChange *change); 81 void ApplyChanges(); 82 bool RemoveRedos(); 83 84 BObjectList<BMessenger> fObservers; 85 86 entry_ref fRef; 87 BFile fFile; 88 const char *fAttribute; 89 type_code fType; 90 bool fIsDevice, fIsReadOnly; 91 off_t fRealSize, fSize; 92 93 BObjectList<DataChange> fChanges; 94 DataChange *fFirstChange; 95 DataChange *fLastChange; 96 97 uint8 *fView; 98 off_t fRealViewOffset, fViewOffset; 99 size_t fRealViewSize, fViewSize; 100 bool fNeedsUpdate; 101 102 size_t fBlockSize; 103 }; 104 105 static const uint32 kMsgDataEditorStateChange = 'deSC'; 106 static const uint32 kMsgDataEditorUpdate = 'deUp'; 107 static const uint32 kMsgDataEditorOffsetChange = 'deOC'; 108 109 #endif /* DATA_EDITOR_H */ 110