xref: /haiku/src/apps/diskprobe/DataEditor.h (revision a381c8a06378de22ff08adf4282b4e3f7e50d250)
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 		status_t Remove(off_t offset, off_t length);
49 		status_t Insert(off_t offset, const uint8 *data, size_t length);
50 
51 		status_t MoveBy(int32 bytes);
52 		status_t MoveTo(off_t offset);
53 
54 		status_t Undo();
55 		status_t Redo();
56 
57 		bool CanUndo() const;
58 		bool CanRedo() const;
59 
60 		status_t SetFileSize(off_t size);
61 		off_t FileSize() const { return fSize; }
62 
63 		status_t SetViewOffset(off_t offset);
64 		off_t ViewOffset() const { return fViewOffset; }
65 
66 		status_t SetViewSize(size_t size);
67 		size_t ViewSize() const { return fViewSize; }
68 
69 		status_t SetBlockSize(size_t size);
70 		size_t BlockSize() const { return fBlockSize; }
71 
72 		status_t UpdateIfNeeded(bool *_updated = NULL);
73 		status_t ForceUpdate();
74 		status_t GetViewBuffer(const uint8 **_buffer);
75 
76 		status_t StartWatching(BMessenger target);
77 		status_t StartWatching(BHandler *handler, BLooper *looper = NULL);
78 		void StopWatching(BMessenger target);
79 		void StopWatching(BHandler *handler, BLooper *looper = NULL);
80 
81 		off_t Find(off_t startPosition, const uint8 *data, size_t dataSize,
82 					bool caseInsensitive, bool cyclic,
83 					BMessenger progressMessenger, volatile bool *stop = NULL);
84 
85 		BFile &File() { return fFile; }
86 		const entry_ref &AttributeRef() const { return fAttributeRef; }
87 		const entry_ref &Ref() const { return fRef; }
88 
89 	private:
90 		friend class StateWatcher;
91 
92 		status_t SetViewOffset(off_t offset, bool sendNotices);
93 		status_t SetViewSize(size_t size, bool sendNotices);
94 		void SendNotices(uint32 what, BMessage *message = NULL);
95 		void SendNotices(DataChange *change);
96 		status_t Update();
97 		void AddChange(DataChange *change);
98 		void ApplyChanges();
99 		void RemoveRedos();
100 
101 		BObjectList<BMessenger> fObservers;
102 
103 		entry_ref	fRef, fAttributeRef;
104 		BFile		fFile;
105 		const char	*fAttribute;
106 		type_code	fType;
107 		bool		fIsDevice, fIsReadOnly;
108 		off_t		fRealSize, fSize;
109 
110 		BObjectList<DataChange>	fChanges;
111 		DataChange				*fFirstChange;
112 		DataChange				*fLastChange;
113 		int32					fChangesFromSaved;
114 
115 		uint8		*fView;
116 		off_t		fRealViewOffset, fViewOffset;
117 		size_t		fRealViewSize, fViewSize;
118 		bool		fNeedsUpdate;
119 
120 		size_t		fBlockSize;
121 };
122 
123 static const uint32 kMsgDataEditorStateChange = 'deSC';
124 static const uint32 kMsgDataEditorUpdate = 'deUp';
125 static const uint32 kMsgDataEditorParameterChange = 'dePC';
126 
127 static const uint32 kMsgDataEditorFindProgress = 'deFP';
128 
129 #endif	/* DATA_EDITOR_H */
130