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 class StateWatcher; 17 18 class DataEditor : public BLocker { 19 public: 20 DataEditor(); 21 DataEditor(entry_ref &ref, const char *attribute = NULL); 22 DataEditor(BEntry &entry, const char *attribute = NULL); 23 DataEditor(const DataEditor &editor); 24 ~DataEditor(); 25 26 status_t SetTo(const char *path, const char *attribute = NULL); 27 status_t SetTo(entry_ref &ref, const char *attribute = NULL); 28 status_t SetTo(BEntry &entry, const char *attribute = NULL); 29 30 status_t Save(); 31 32 bool IsReadOnly() const { return fIsReadOnly; } 33 bool IsDevice() const { return fIsDevice; } 34 bool IsAttribute() const { return fAttribute != NULL; } 35 bool IsModified() const { return fLastChange != fFirstChange; } 36 37 const char *Attribute() const { return fAttribute; } 38 type_code Type() const { return fType; } 39 40 status_t InitCheck(); 41 42 status_t Replace(off_t offset, const uint8 *data, size_t length); 43 status_t Remove(off_t offset, off_t length); 44 status_t Insert(off_t offset, const uint8 *data, size_t length); 45 46 status_t MoveBy(int32 bytes); 47 status_t MoveTo(off_t offset); 48 49 status_t Undo(); 50 status_t Redo(); 51 52 bool CanUndo() const; 53 bool CanRedo() const; 54 55 status_t SetFileSize(off_t size); 56 off_t FileSize() const { return fSize; } 57 58 status_t SetViewOffset(off_t offset); 59 off_t ViewOffset() const { return fViewOffset; } 60 61 status_t SetViewSize(size_t size); 62 size_t ViewSize() const { return fViewSize; } 63 64 status_t SetBlockSize(size_t size); 65 size_t BlockSize() const { return fBlockSize; } 66 67 status_t UpdateIfNeeded(bool *_updated = NULL); 68 status_t ForceUpdate(); 69 status_t GetViewBuffer(const uint8 **_buffer); 70 71 status_t StartWatching(BMessenger target); 72 status_t StartWatching(BHandler *handler, BLooper *looper = NULL); 73 void StopWatching(BMessenger target); 74 void StopWatching(BHandler *handler, BLooper *looper = NULL); 75 76 off_t Find(off_t startPosition, const uint8 *data, size_t dataSize, 77 bool caseInsensitive, bool cyclic, 78 BMessenger progressMessenger, volatile bool *stop = NULL); 79 80 BFile &File() { return fFile; } 81 const entry_ref &Ref() const { return fRef; } 82 83 private: 84 friend class StateWatcher; 85 86 status_t SetViewOffset(off_t offset, bool sendNotices); 87 status_t SetViewSize(size_t size, bool sendNotices); 88 void SendNotices(uint32 what, BMessage *message = NULL); 89 void SendNotices(DataChange *change); 90 status_t Update(); 91 void AddChange(DataChange *change); 92 void ApplyChanges(); 93 void RemoveRedos(); 94 95 BObjectList<BMessenger> fObservers; 96 97 entry_ref fRef; 98 BFile fFile; 99 const char *fAttribute; 100 type_code fType; 101 bool fIsDevice, fIsReadOnly; 102 off_t fRealSize, fSize; 103 104 BObjectList<DataChange> fChanges; 105 DataChange *fFirstChange; 106 DataChange *fLastChange; 107 int32 fChangesFromSaved; 108 109 uint8 *fView; 110 off_t fRealViewOffset, fViewOffset; 111 size_t fRealViewSize, fViewSize; 112 bool fNeedsUpdate; 113 114 size_t fBlockSize; 115 }; 116 117 static const uint32 kMsgDataEditorStateChange = 'deSC'; 118 static const uint32 kMsgDataEditorUpdate = 'deUp'; 119 static const uint32 kMsgDataEditorParameterChange = 'dePC'; 120 121 static const uint32 kMsgDataEditorFindProgress = 'deFP'; 122 123 #endif /* DATA_EDITOR_H */ 124