1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiSource Application Framework
4  * Copyright (C) 1998,1999 AbiSource, Inc.
5  * Copyright (C) 2004 Tomas Frydrych <tomasfrydrych@yahoo.co.uk>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  */
22 
23 
24 #ifndef AD_DOCUMENT_H
25 #define AD_DOCUMENT_H
26 
27 #include <string>
28 
29 #ifndef UT_TYPES_H
30 #include "ut_types.h"
31 #endif
32 #include "ut_string_class.h"
33 #include "ut_vector.h"
34 #include "time.h"
35 
36 // fwd. decl.
37 class XAP_ResourceManager;
38 class UT_UUID;
39 class XAP_Frame;
40 class AV_View;
41 
42 // a helper class for history tracking
43 class ABI_EXPORT AD_VersionData
44 {
45   public:
46 
47 	// constructor for importers
48 	AD_VersionData(UT_uint32 v, UT_UTF8String &uuid, time_t start, bool autorev, UT_uint32 xid);
49 	AD_VersionData(UT_uint32 v, const char * uuid, time_t start, bool autorev, UT_uint32 xid);
50 
51 	// constructor for new entries
52 	AD_VersionData(UT_uint32 v, time_t start, bool autorev, UT_uint32 xid);
53 
54 	// copy constructor
55 	AD_VersionData(const AD_VersionData & v);
56 
57 	virtual ~AD_VersionData();
58 
59 	AD_VersionData & operator = (const AD_VersionData &v);
60 
61 	bool operator == (const AD_VersionData &v);
62 
getId()63 	UT_uint32      getId()const{return m_iId;}
64 	time_t         getTime()const;
getStartTime()65 	time_t         getStartTime()const {return m_tStart;}
getUID()66 	const UT_UUID& getUID()const {return (const UT_UUID&)*m_pUUID;}
67 	bool           newUID(); // true on success
68 
setId(UT_uint32 iid)69 	void           setId(UT_uint32 iid) {m_iId = iid;}
70 
isAutoRevisioned()71 	bool           isAutoRevisioned()const {return m_bAutoRevision;}
72 	void           setAutoRevisioned(bool autorev);
73 
getTopXID()74 	UT_uint32      getTopXID() const {return m_iTopXID;}
75 
76   private:
77 	UT_uint32   m_iId;
78 	UT_UUID *   m_pUUID;
79 	time_t      m_tStart;
80 	bool        m_bAutoRevision;
81 	UT_uint32   m_iTopXID;
82 };
83 
84 enum AD_HISTORY_STATE
85 {
86 	ADHIST_FULL_RESTORE,
87 	ADHIST_PARTIAL_RESTORE,
88 	ADHIST_NO_RESTORE
89 };
90 
91 
92 // a helper class for keeping track of revisions in the document
93 class ABI_EXPORT AD_Revision
94 {
95   public:
96 	AD_Revision(UT_uint32 iId, UT_UCS4Char * pDesc, time_t start, UT_uint32 iVer = 0)
m_iId(iId)97 		:m_iId(iId),m_pDescription(pDesc), m_tStart(start), m_iVersion(iVer){};
98 
~AD_Revision()99 	~AD_Revision(){delete [] m_pDescription;}
100 
getId()101 	UT_uint32         getId()const{return m_iId;}
getDescription()102 	UT_UCS4Char *     getDescription() const {return m_pDescription;}
103 
104 	// NB: getStartTime() == 0 should be interpreted as 'unknown'
getStartTime()105 	time_t            getStartTime() const {return m_tStart;}
setStartTime(time_t t)106 	void              setStartTime(time_t t) {m_tStart = t;}
107 
getVersion()108 	UT_uint32         getVersion()const {return m_iVersion;}
setVersion(UT_uint32 iVer)109 	void              setVersion(UT_uint32 iVer) {m_iVersion = iVer;}
110 
111   private:
112 	UT_uint32     m_iId;
113 	UT_UCS4Char * m_pDescription;
114 	time_t        m_tStart;
115 	UT_uint32     m_iVersion;
116 };
117 
118 enum AD_DOCUMENT_TYPE
119 {
120 	ADDOCUMENT_ABIWORD
121 };
122 
123 struct _dataItemPair;
124 typedef _dataItemPair* PD_DataItemHandle;
125 
126 class ABI_EXPORT AD_Document
127 {
128 public:
129 	AD_Document();
130 	void				ref(void);
131 	void				unref(void);
132 
133 	virtual AD_DOCUMENT_TYPE getType() const = 0;
134 
resourceManager()135 	XAP_ResourceManager &	resourceManager () const { return *m_pResourceManager; }
136 
137 	const char *			getFilename(void) const;
138 	const std::string     & getPrintFilename(void) const;
139 	void                    setPrintFilename(const std::string & sFile);
clearFilename(void)140 	void                    clearFilename(void) {_setFilename(NULL); forceDirty();}
141 	// TODO - this should be returning IEFileType,
142 	// but that's AP stuff, so it's not here
143 
144 	virtual UT_Error		readFromFile(const char * szFilename, int ieft, const char * props = NULL) = 0;
145 	virtual UT_Error		importFile(const char * szFilename, int ieft, bool markClean = false, bool bImportStylesFirst = true, const char * props = NULL) = 0;
146 	virtual UT_Error		newDocument() = 0;
147 	virtual bool			isDirty(void) const = 0;
forceDirty()148 	virtual void            forceDirty() {m_bForcedDirty = true;};
isForcedDirty()149 	bool                    isForcedDirty() const {return m_bForcedDirty;}
150 
151 	virtual bool			canDo(bool bUndo) const = 0;
152 	virtual bool			undoCmd(UT_uint32 repeatCount) = 0;
153 	virtual bool			redoCmd(UT_uint32 repeatCount) = 0;
154 
155 	UT_Error		        saveAs(const char * szFilename, int ieft, const char * props = NULL);
156 	UT_Error		        saveAs(const char * szFilename, int ieft, bool cpy, const char * props = NULL);
157 	UT_Error		        save(void);
158 	virtual bool			createDataItem(const char * szName,
159 										   bool bBase64,
160 										   const UT_ByteBuf * pByteBuf,
161 										   const std::string & mime_type,
162 										   PD_DataItemHandle* ppHandle) = 0;
163 	virtual bool            replaceDataItem(const char * szName,
164 											const UT_ByteBuf * pByteBuf) = 0;
165 	virtual bool			getDataItemDataByName(const char * szName,
166 												  const UT_ByteBuf ** ppByteBuf,
167 												  std::string * mime_type,
168 												  PD_DataItemHandle* ppHandle) const = 0;
169 public:
170 
171 	/**
172 	 * Returns the # of seconds since the last save of this file
173 	 */
getTimeSinceSave()174 	time_t          getTimeSinceSave () const { return (time(NULL) - m_lastSavedTime); }
getLastSavedTime()175 	time_t          getLastSavedTime() const {return m_lastSavedTime;}
setLastSavedTime(time_t t)176 	void            setLastSavedTime(time_t t) {m_lastSavedTime = t;}
177 	virtual UT_uint32 getLastSavedAsType() const = 0;
178 
getTimeSinceOpen()179 	time_t          getTimeSinceOpen () const { return (time(NULL) - m_lastOpenedTime); }
getLastOpenedTime()180 	time_t          getLastOpenedTime() const {return m_lastOpenedTime;}
setLastOpenedTime(time_t t)181 	void            setLastOpenedTime(time_t t) {m_lastOpenedTime = t;}
182 
getEditTime()183 	time_t          getEditTime()const {return (m_iEditTime + (time(NULL) - m_lastOpenedTime));}
setEditTime(UT_uint32 t)184 	void            setEditTime(UT_uint32 t) {m_iEditTime = t;}
185 
setDocVersion(UT_uint32 i)186 	void            setDocVersion(UT_uint32 i){m_iVersion = i;}
getDocVersion()187 	UT_uint32       getDocVersion() const {return m_iVersion;}
188 
189 	void			setEncodingName(const char * szEncodingName);
190 	const char *	getEncodingName() const;
191 
192 	bool			isPieceTableChanging(void) const;
193 
194 	virtual void setMetaDataProp (const std::string & key, const std::string & value) = 0;
195 	virtual bool getMetaDataProp (const std::string & key, std::string & outProp) const = 0;
196 
197 	// RIVERA TODO not working and may not be needed
198 	virtual void setAnnotationProp (const std::string & key, const std::string & value) = 0;
199 	virtual bool getAnnotationProp (const std::string & key, std::string & outProp) const = 0;
200 
201 	// history tracking
202 	void            addRecordToHistory(const AD_VersionData & v);
203 	void            purgeHistory();
getHistoryCount()204 	UT_sint32       getHistoryCount()const {return m_vHistory.getItemCount();}
205 	UT_uint32       getHistoryNthId(UT_sint32 i)const;
206 	time_t          getHistoryNthTime(UT_sint32 i)const;
207 	time_t          getHistoryNthTimeStarted(UT_sint32 i)const;
208 	time_t          getHistoryNthEditTime(UT_sint32 i)const;
209 	const UT_UUID&  getHistoryNthUID(UT_sint32 i)const;
210 	bool            getHistoryNthAutoRevisioned(UT_sint32 i)const;
211 	UT_uint32       getHistoryNthTopXID(UT_sint32 i)const;
212 
213 	AD_HISTORY_STATE       verifyHistoryState(UT_uint32 &iVersion) const;
214 	const AD_VersionData * findHistoryRecord(UT_uint32 iVersion) const;
215     bool                   showHistory(AV_View * pView);
216 
217 	bool            areDocumentsRelated (const AD_Document &d) const;
218 	bool            areDocumentHistoriesEqual(const AD_Document &d, UT_uint32 &iVer) const;
219 
220 	virtual bool    areDocumentContentsEqual(const AD_Document &d, UT_uint32 &pos) const = 0;
221 	virtual bool    areDocumentFormatsEqual(const AD_Document &d, UT_uint32 &pos) const = 0;
222 	virtual bool    areDocumentStylesheetsEqual(const AD_Document &d) const = 0;
223 
224 	void            setDocUUID(const char * u);
225 	const char *    getDocUUIDString()const;
getDocUUID()226 	const UT_UUID * getDocUUID()const {return m_pUUID;};
227 	void            setOrigUUID(const char * u);
228 	const char *    getOrigDocUUIDString()const;
getOrigDocUUID()229 	const UT_UUID * getOrigDocUUID()const {return m_pOrigUUID;};
230 	void            setMyUUID(const char * u);
231 	UT_UTF8String   getMyUUIDString()const;
getMyUUID()232 	const UT_UUID * getMyUUID()const {return m_pMyUUID;};
233 
234 	UT_UUID *       getNewUUID()   const;
235 	UT_uint32       getNewUUID32() const;
236 	UT_uint64       getNewUUID64() const;
237 
238 	bool            addRevision(UT_uint32 iId, UT_UCS4Char * pDesc,
239 								time_t tStart, UT_uint32 iVersion, bool bGenCR=true);
240 
241 	bool            addRevision(UT_uint32 iId, const UT_UCS4Char * pDesc, UT_uint32 iLen,
242 								time_t tStart, UT_uint32 iVersion, bool bGenCR=true);
243 	bool            addRevision(AD_Revision * pRev, bool bGenCR=true);
244 	virtual bool    createAndSendDocPropCR( const gchar ** pAtts, const gchar ** pProps) = 0;
245 
getRevisions()246 	const UT_GenericVector<AD_Revision*> &         getRevisions() {return m_vRevisions;}
247 	UT_uint32           getHighestRevisionId() const;
248 	const AD_Revision*  getHighestRevision() const;
249 	UT_sint32           getRevisionIndxFromId(UT_uint32 iId) const;
250     bool                usingChangeTracking() const;
251 
isMarkRevisions()252 	bool                isMarkRevisions() const{ return m_bMarkRevisions;}
isShowRevisions()253 	bool                isShowRevisions() const{ return m_bShowRevisions;}
254 
getShowRevisionId()255 	UT_uint32           getShowRevisionId() const {return m_iShowRevisionID;}
getRevisionId()256 	UT_uint32           getRevisionId() const{ return m_iRevisionID;}
257 
258 	UT_uint32           findAutoRevisionId(UT_uint32 iVersion) const;
259 	UT_uint32           findNearestAutoRevisionId(UT_uint32 iVersion, bool bLesser = true) const;
260 
261 	void                toggleMarkRevisions();
262 	void                toggleShowRevisions();
263 
264 	virtual void        setMarkRevisions(bool bMark);
265 	void                setShowRevisions(bool bShow);
266 	void                setShowRevisionId(UT_uint32 iId);
267 	void                setRevisionId(UT_uint32 iId);
268 
isAutoRevisioning()269 	bool                isAutoRevisioning()const {return m_bAutoRevisioning;}
270 	virtual void        setAutoRevisioning(bool autorev);
271 
272 	virtual void        purgeRevisionTable(bool bUnconditional = false) = 0;
273 
274 	virtual bool        acceptRejectRevision(bool bReject,
275 											 UT_uint32 iStart,
276 											 UT_uint32 iEnd,
277 											 UT_uint32 iLevel) = 0;
278 
279 	virtual bool        rejectAllHigherRevisions(UT_uint32 iLevel) = 0;
280 
281 	virtual bool        acceptAllRevisions() = 0;
282 
283 	bool                purgeAllRevisions(AV_View * pView);
284 	bool                isOrigUUID(void) const;
setFilename(char * name)285 	void                setFilename(char * name)
286 	{_setFilename(name);}
287 	virtual UT_uint32   getXID() const = 0;
288 	virtual UT_uint32   getTopXID() const = 0;
289 
290  protected:
291 	virtual UT_Error	_saveAs(const char * szFilename, int ieft, const char * props = NULL) = 0;
292 	virtual UT_Error	_saveAs(const char * szFilename, int ieft, bool cpy, const char * props = NULL) = 0;
293 	virtual UT_Error	_save(void) = 0;
294 
295 	void            _purgeRevisionTable();
296 	void            _adjustHistoryOnSave();
_setFilename(char * name)297 	void			_setFilename(char * name) {FREEP(m_szFilename); m_szFilename = name;}
_setForceDirty(bool b)298 	void            _setForceDirty(bool b) {m_bForcedDirty = b;}
_setPieceTableChanging(bool b)299 	void            _setPieceTableChanging(bool b) {m_bPieceTableChanging = b;}
_setMarkRevisions(bool bMark)300 	void            _setMarkRevisions(bool bMark) {m_bMarkRevisions = bMark;}
301 
302     bool            _restoreVersion(XAP_Frame * pFrame, UT_uint32 iVersion);
303 
304 	virtual void    _clearUndo() = 0;
305 
306 
307 	virtual ~AD_Document();		//  Use unref() instead.
308 
309 private:
310 	XAP_ResourceManager *	m_pResourceManager;
311 
312 	int				m_iRefCount;
313 	const char *	m_szFilename;
314 	UT_String		m_szEncodingName;
315 
316 	bool			m_bPieceTableChanging;
317 	time_t          m_lastSavedTime;
318 	time_t          m_lastOpenedTime;
319 	time_t          m_iEditTime;     // previous edit time (up till open)
320 	UT_uint32       m_iVersion;
321 
322 	// these are for tracking versioning
323 	bool            m_bHistoryWasSaved;
324 	UT_Vector       m_vHistory;
325 	UT_GenericVector<AD_Revision*> m_vRevisions;
326 
327 	bool            m_bMarkRevisions;
328 	bool            m_bShowRevisions;
329 	UT_uint32       m_iRevisionID;
330 	UT_uint32       m_iShowRevisionID;
331 	bool            m_bAutoRevisioning;
332 
333 	bool            m_bForcedDirty;
334 
335 	UT_UUID *       m_pUUID;
336 	UT_UUID *       m_pOrigUUID;
337 	UT_UUID *       m_pMyUUID;
338 	bool            m_bDoNotAdjustHistory;
339 	bool            m_bAfterFirstSave;
340  	UT_UTF8String   m_sMyUUIDString;
341  	UT_UTF8String   m_sOrigUUIDString;
342 	std::string     m_sPrintFilename;
343 };
344 
345 
346 #endif /* AD_DOCUMENT_H */
347