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