xref: /haiku/src/apps/diskprobe/DataEditor.h (revision 5d9e40fe9252c8f9c5e5e41594545bfa4419fcc7)
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 		void SetBlockSize(size_t size);
65 		size_t BlockSize() const { return fBlockSize; }
66 
67 		status_t UpdateIfNeeded(bool *_updated = NULL);
68 		status_t GetViewBuffer(const uint8 **_buffer);
69 
70 		status_t StartWatching(BMessenger target);
71 		status_t StartWatching(BHandler *handler, BLooper *looper = NULL);
72 		void StopWatching(BMessenger target);
73 		void StopWatching(BHandler *handler, BLooper *looper = NULL);
74 
75 		BFile &File() { return fFile; }
76 		const entry_ref &Ref() const { return fRef; }
77 
78 	private:
79 		friend class StateWatcher;
80 
81 		status_t SetViewOffset(off_t offset, bool sendNotices);
82 		void SendNotices(uint32 what, BMessage *message = NULL);
83 		void SendNotices(DataChange *change);
84 		status_t Update();
85 		void AddChange(DataChange *change);
86 		void ApplyChanges();
87 		void RemoveRedos();
88 
89 		BObjectList<BMessenger> fObservers;
90 
91 		entry_ref	fRef;
92 		BFile		fFile;
93 		const char	*fAttribute;
94 		type_code	fType;
95 		bool		fIsDevice, fIsReadOnly;
96 		off_t		fRealSize, fSize;
97 
98 		BObjectList<DataChange>	fChanges;
99 		DataChange				*fFirstChange;
100 		DataChange				*fLastChange;
101 		int32					fChangesFromSaved;
102 
103 		uint8		*fView;
104 		off_t		fRealViewOffset, fViewOffset;
105 		size_t		fRealViewSize, fViewSize;
106 		bool		fNeedsUpdate;
107 
108 		size_t		fBlockSize;
109 };
110 
111 static const uint32 kMsgDataEditorStateChange = 'deSC';
112 static const uint32 kMsgDataEditorUpdate = 'deUp';
113 static const uint32 kMsgDataEditorOffsetChange = 'deOC';
114 
115 #endif	/* DATA_EDITOR_H */
116