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