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