1 /* AbiWord
2  * Copyright (C) 2002 Tomas Frydrych <tomas@frydrych.uklinux.net>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 #ifndef PT_REVISION_H
21 #define PT_REVISION_H
22 
23 #include "ut_types.h"
24 #include "ut_string_class.h"
25 #include "ut_vector.h"
26 #include "pp_AttrProp.h"
27 
28 class PD_Document;
29 
30 ABI_EXPORT const char* UT_getAttribute( const PP_AttrProp* pAP, const char* name, const char* def = 0 );
31 /**
32  * Like UT_getAttribute(name,atts,def) but check for a revision attribute and
33  * if found first look for the most recent value of atts in the revision.
34  */
35 ABI_EXPORT std::string UT_getLatestAttribute( const PP_AttrProp* pAP,
36                                               const char* name,
37                                               const char* def );
38 
39 
40 typedef enum {
41 	PP_REVISION_NONE             = 0,
42 	PP_REVISION_ADDITION         = 0x01,
43 	PP_REVISION_DELETION         = 0x02,
44 	PP_REVISION_FMT_CHANGE       = 0x04,
45 	PP_REVISION_ADDITION_AND_FMT = 0x05
46 } PP_RevisionType;
47 
48 /*! PP_Revision is a class that encapsulates a single revision,
49     holding its id, type and associated properties and attributes. It
50     provides functions for retrieving information and from merging
51     properties
52 */
53 class ABI_EXPORT PP_Revision: public PP_AttrProp
54 {
55   public:
56 	PP_Revision(UT_uint32 Id,
57 				PP_RevisionType eType,
58 				const gchar *  props,
59 				const gchar * attrs);
60 
61 	PP_Revision(UT_uint32 Id,
62 				PP_RevisionType eType,
63 				const gchar ** props,
64 				const gchar ** attrs);
65 
~PP_Revision()66 	virtual ~PP_Revision(){};
67 
getId()68 	UT_uint32        getId()    const {return m_iID;}
setId(UT_uint32 iId)69 	void             setId(UT_uint32 iId) {UT_ASSERT_HARMLESS( iId >= m_iID); m_iID = iId;}
70 
getType()71 	PP_RevisionType  getType()  const {return m_eType;}
setType(PP_RevisionType t)72 	void             setType(PP_RevisionType t) {m_eType = t; m_bDirty = true;}
73 
74 	const gchar * getPropsString() const;
75 	const gchar * getAttrsString() const;
76 
77 	// this is intentionally not virtual (no need for that)
78 	bool	setAttributes(const gchar ** attributes);
79 
80 	bool operator == (const PP_Revision &op2) const;
81 
82 //    PP_Revision* clone() const;
83 
84     std::string toString() const;
85     bool onlyContainsAbiwordChangeTrackingMarkup() const;
86 
87   private:
88 	void             _refreshString() const;
89 	bool             _handleNestedRevAttr();
90 
91 	UT_uint32        m_iID;
92 	PP_RevisionType  m_eType;
93 	// these next three are a cache, therefor mutable
94 	mutable UT_String        m_sXMLProps;
95 	mutable UT_String        m_sXMLAttrs;
96 	mutable bool             m_bDirty;
97 };
98 
99 
100 
101 /*! PP_RevisionAttr is class that represent a revision attribute; it
102     is initialized by an attribute string:
103 
104       <c revision="R1[,R2,R3,...]">some text</>
105                    ^^^^^^^^^^^^^^
106 
107       R1, etc., conform to the following syntax (items in square
108       brackets are optional):
109 
110       [+]n[{props}[{atrrs}]]    -- addition with optional properties
111                                    and attributes; props and attrs
112                                    are formed as `name:value'
113       -n                        -- deletion
114       !n{props}                 -- formating change only
115 
116       where n is a numerical id of the revision and props is regular
117       property string, for instance
118 
119           font-family:Times New Roman
120 
121       revoval of property/attribute is indicated by setting to -/-, e.g.,
122 
123           font-family:-/-
124 
125       (the revision attribute parser in the class translates that into "")
126 
127 
128   The class provides methods for adding and removing individual
129   revisions and evaluating how a particular revised fragment should be
130   displayed in the document
131 */
132 
133 class ABI_EXPORT PP_RevisionAttr
134 {
135   public:
PP_RevisionAttr()136 	PP_RevisionAttr()
137 		:m_vRev(),m_sXMLstring(),m_bDirty(true),m_iSuperfluous(0),m_pLastRevision(NULL)
138 		{};
139 	PP_RevisionAttr(const gchar * r);
140 
141 
142 	PP_RevisionAttr(UT_uint32 iId, PP_RevisionType eType, const gchar ** pAttrs, const gchar ** pProps);
143 
144 	~PP_RevisionAttr();
145 
146 	void                  setRevision(const gchar * r);
147 	void                  setRevision(std::string&  r);
148 
149 	void                  addRevision(UT_uint32 iId,
150 									  PP_RevisionType eType,
151 									  const gchar ** pAttrs,
152 									  const gchar ** pProps);
153 	void                  addRevision(UT_uint32 iId, PP_RevisionType eType );
154     // No ownership of the given revision is taken.
155     void                  addRevision( const PP_Revision* r );
156 
157 	bool                  changeRevisionType(UT_uint32 iId, PP_RevisionType eType);
158 	bool                  changeRevisionId(UT_uint32 iOldId, UT_uint32 iNewId);
159 
160 	void                  removeRevisionIdWithType(UT_uint32 iId, PP_RevisionType eType);
161 	void                  removeRevisionIdTypeless(UT_uint32 iId);
162 	void                  removeAllLesserOrEqualIds(UT_uint32 id);
163 	void                  removeAllHigherOrEqualIds(UT_uint32 id);
164 	void                  removeRevision(const PP_Revision * pRev);
165 
166 	const PP_Revision *   getGreatestLesserOrEqualRevision(UT_uint32 id,
167 														   const PP_Revision ** ppR) const;
168 	const PP_Revision *   getLowestGreaterOrEqualRevision(UT_uint32 id) const;
169 
170 	const PP_Revision *   getLastRevision() const;
171 	const PP_Revision *   getRevisionWithId(UT_uint32 iId, UT_uint32 & iMinId) const;
172     UT_uint32             getHighestId() const;
173 
getRevisionsCount()174 	UT_uint32             getRevisionsCount() const {return m_vRev.getItemCount();}
empty()175     bool                  empty() const { return !getRevisionsCount(); }
getNthRevision(UT_uint32 n)176 	const PP_Revision *   getNthRevision(UT_uint32 n) const {return (const PP_Revision*)m_vRev.getNthItem(n);}
177 
178 	void                  pruneForCumulativeResult(PD_Document * pDoc);
179 
180 	/*! please note that the following are convenience functions; if
181 	    you need to make repeated enqueries, it is better to call
182 	    getGreatestLesserOrEqualRevision() or getLastRevision() and
183 	    query the returned PP_Revision object.
184     */
185 	bool                  isVisible(UT_uint32 id) const;
186 	bool                  hasProperty(UT_uint32 iId, const gchar * pName, const gchar * &pValue) const;
187 	bool                  hasProperty(const gchar * pName, const gchar * &pValue) const;
188 	PP_RevisionType       getType(UT_uint32 iId) const;
189 	PP_RevisionType       getType() const;
190     UT_uint32             getHighestRevisionNumberWithAttribute( const gchar * pName ) const;
191 #if 0
192 	const UT_Vector *     getProps(UT_uint32 iId);
193 	const UT_Vector *     getProps();
194 #endif
195 	const gchar *      getXMLstring() const;
196     std::string        getXMLstringUpTo( UT_uint32 iId ) const;
forceDirty()197 	void                  forceDirty() {m_bDirty = true;}
198 	bool                  isFragmentSuperfluous() const;
199 
200 	bool operator== (const PP_RevisionAttr &op2) const;
201 
202     // MIQ: This would be nice, but there are ownership issues I don't know about with M
203 //    PP_RevisionAttr& operator=(const PP_RevisionAttr &rhs);
204 
205     void mergeAll( const PP_RevisionAttr& ra );
206     void mergeAttr( UT_uint32 iId, PP_RevisionType t,
207                     const gchar* pzName, const gchar* pzValue );
208     void mergeAttrIfNotAlreadyThere( UT_uint32 iId, PP_RevisionType t,
209                                      const gchar* pzName, const gchar* pzValue );
210 
211 	const PP_Revision *   getLowestDeletionRevision() const;
212 
213 
214   private:
215 	void _init(const gchar *r);
216 	void _clear();
217 	void _refreshString() const;
218 
219 	UT_Vector           m_vRev;
220 	// these next 2 are a cache, hence mutable
221 	mutable UT_String           m_sXMLstring;
222 	mutable bool                m_bDirty; // indicates whether m_sXMLstring corresponds
223 						          // to current state of the instance
224 	UT_uint32           m_iSuperfluous;
225 	// also a cache
226 	mutable const PP_Revision * m_pLastRevision;
227 };
228 
229 #endif // #ifndef PT_REVISION_H
230