1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _XMLOFF_FORMS_LAYEREXPORT_HXX_
29 #define _XMLOFF_FORMS_LAYEREXPORT_HXX_
30 
31 #include <com/sun/star/container/XIndexAccess.hpp>
32 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
33 #include <com/sun/star/util/XNumberFormats.hpp>
34 #include <com/sun/star/awt/XControlModel.hpp>
35 #include "callbacks.hxx"
36 #include "ifacecompare.hxx"
37 #include <vos/ref.hxx>
38 
39 class SvXMLExport;
40 class SvXMLNumFmtExport;
41 class XMLPropertyHandlerFactory;
42 class SvXMLExportPropertyMapper;
43 
44 //.........................................................................
45 namespace xmloff
46 {
47 //.........................................................................
48 
49 	typedef ::std::set  <   ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
50                         ,   OPropertySetCompare
51                         >   PropertySetBag;
52 
53     // maps objects (property sets) to strings, e.g. control ids.
54     typedef ::std::map  <   ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
55                         ,   ::rtl::OUString
56                         ,   OPropertySetCompare
57                         >   MapPropertySet2String;
58 
59     // map pages to maps (of property sets to strings)
60     typedef ::std::map  <   ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >
61                         ,   MapPropertySet2String
62                         ,   ODrawPageCompare
63                         >   MapPropertySet2Map;
64 
65 	//=====================================================================
66 	//= OFormLayerXMLExport_Impl
67 	//=====================================================================
68 	/** the implementation class for OFormLayerXMLExport
69 	*/
70 	class OFormLayerXMLExport_Impl
71 				:public IFormsExportContext
72 	{
73 		friend class OFormLayerXMLExport;
74 
75 	protected:
76 		SvXMLExport&		m_rContext;
77 		SvXMLNumFmtExport*	m_pControlNumberStyles;
78 
79 		// ignore list for control models
80 		PropertySetBag		m_aIgnoreList;
81 
82 		// style handling
83 		::vos::ORef< XMLPropertyHandlerFactory >	m_xPropertyHandlerFactory;
84 		::vos::ORef< SvXMLExportPropertyMapper >	m_xStyleExportMapper;
85 
86 		// we need our own number formats supplier:
87 		// Controls which have a number formats do not work with the formats supplier of the document they reside
88 		// in, instead they use the formats of the data source their form is associated with. If there is no
89 		// such form or no such data source, they work with an own formatter.
90 		// Even more, time and date fields do not work with a central formatter at all, they have their own one
91 		// (which is shared internally, but this is a (hidden) implementation detail.)
92 		//
93 		// To not contaminate the global (document) number formats supplier (which could be obtained from the context),
94 		// we have an own one.
95 		// (Contaminate means: If a user adds a user-defined format to a formatted field, this format is stored in
96 		// in the data source's formats supplier. To export this _and_ reuse existing structures, we would need to
97 		// add this format to the global (document) formats supplier.
98 		// In case of an export we could do some cleanup afterwards, but in case of an import, there is no such
99 		// chance, as (if other user-defined formats exist in the document as well) we can't distinguish
100 		// between user-defined formats really needed for the doc (i.e. in a calc cell) and formats only added
101 		// to the supplier because the controls needed it.
102 		::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormats >
103 													m_xControlNumberFormats;
104 
105 		MapPropertySet2Map	m_aControlIds;
106 			// the control ids of all controls on all pages we ever examined
107 
108 		MapPropertySet2Map	m_aReferringControls;
109 			// for a given page (iter->first), and a given control (iter->second->first), this is the comma-separated
110 			// lists of ids of the controls refering to the control given.
111 
112         MapPropertySet2Map::iterator
113 							m_aCurrentPageIds;
114 			// the iterator for the control id map for the page beeing handled
115 		MapPropertySet2Map::iterator
116 							m_aCurrentPageReferring;
117 			// the same for the map of referring controls
118 
119 		// TODO: To avoid this construct above, and to have a cleaner implementation, an class encapsulating the
120 		// export of a single page should be introduced.
121 
122 		DECLARE_STL_MAP( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >, sal_Int32, OPropertySetCompare, MapPropertySet2Int);
123 		MapPropertySet2Int	m_aControlNumberFormats;
124 			// maps controls to format keys, which are relative to our own formats supplier
125 
126 		MapPropertySet2String	m_aGridColumnStyles;
127 			// style names of grid columns
128 
129 	public:
130 		OFormLayerXMLExport_Impl(SvXMLExport& _rContext);
131         virtual ~OFormLayerXMLExport_Impl();
132 
133 	protected:
134 		/** exports one single grid column
135 		*/
136 		void	exportGridColumn(
137 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxColumn,
138 			const ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _rEvents
139 			);
140 
141 		/** exports one single control
142 		*/
143 		void	exportControl(
144 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl,
145 			const ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _rEvents
146 			);
147 
148 		/** exports one single form
149 		*/
150 		void	exportForm(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxProps,
151 			const ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _rEvents
152 			);
153 
154 		/** seek to the page given.
155 
156 			<p>This must be called before you can retrieve any ids for controls on the page.</p>
157 
158 			@see
159 				getControlId
160 		*/
161 		sal_Bool	seekPage(
162 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage);
163 
164 		/** get the id of the given control.
165 
166 			<p>You must have sought to the page of the control before calling this.</p>
167 		*/
168 		::rtl::OUString
169 				getControlId(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl);
170 
171 		/** retrieves the style name for the control's number style.
172 
173 			<p>For performance reasons, this method is allowed to be called for any controls, even those which
174 			do not have a number style. In this case, an empty string is returned.</p>
175 		*/
176 		::rtl::OUString
177 				getControlNumberStyle( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl );
178 
179 		// IFormsExportContext
180 		virtual void										exportCollectionElements(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& _rxCollection);
181 		virtual SvXMLExport&								getGlobalContext();
182 		virtual ::rtl::OUString								getObjectStyleName(
183 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject );
184 		virtual ::vos::ORef< SvXMLExportPropertyMapper >	getStylePropertyMapper();
185 
186 		/** clear any structures which have been build in the recent <method>examine</method> calls.
187 		*/
188 		void clear();
189 
190 		/** examine a forms collection.
191 
192 			<p>The method will collect control ids and add styles to the export context as necessary.</p>
193 
194 			<p>Every control in the object hierarchy given will be assigned to a unique id, which is stored for later
195 			use.</p>
196 
197 			<p>In addition, any references the controls may have between each other, are collected and stored for
198 			later use.</p>
199 
200 			<p>Upon calling this method, the id map will be cleared before collecting the new ids, so any ids
201 			you collected previously will be lost</p>
202 
203 			@param _rxDrawPage
204 				the draw page which's forms collection should be examined
205 
206 			@see getControlId
207 			@see exportControl
208 			@see exportForms
209 		*/
210 		void examineForms(
211 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage);
212 
213 		/** export a forms collection of a draw page
214 
215 			<p>The method will obtain the forms collection of the page and call
216 			<method>exportCollectionElements</method>.</p>
217 		*/
218 		void exportForms(
219 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage);
220 
221         /** exports the XForms model data
222         */
223         void exportXForms() const;
224 
225         /** determines whether the given page contains logical forms
226         */
227 		bool pageContainsForms( const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage ) const;
228 
229         /** determines whether the given page contains XForm instances
230         */
231 		bool documentContainsXForms() const;
232 
233         /** exports the controls number styles
234 		*/
235 		void	exportControlNumberStyles();
236 
237 		/** exports the automatic control number styles
238 		*/
239 		void	exportAutoControlNumberStyles();
240 
241 		/** exports the auto-styles collected during the examineForms calls
242 		*/
243 		void exportAutoStyles();
244 
245 	protected:
246 		sal_Bool impl_isFormPageContainingForms(
247 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage,
248 			::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& _rxForms);
249 
250 		/** moves the m_aCurrentPage* members to the positions specifying the given page.
251 
252 			@return <TRUE/> if there already were structures for the given page
253 		*/
254 		sal_Bool implMoveIterators(
255 			const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& _rxDrawPage,
256 			sal_Bool _bClear);
257 
258 		/** check the object given if it's a control, if so, examine it.
259 			@return <TRUE/> if the object has been handled
260 		*/
261 		sal_Bool checkExamineControl(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject);
262 
263 		/** examines the control's number format, so later the format style can be referred
264 
265 			<p>remembers the format key for the control, so it can later be asked for in getControlNumberStyle</p>
266 		*/
267 		void examineControlNumberFormat(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl);
268 
269 		/** examines the control's number format, so later the format style can be referred
270 
271 			<p>does not remember the information returned in any way</p>
272 		*/
273 		sal_Int32 implExamineControlNumberFormat( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject );
274 
275 		/** collects AutoStyles for grid columns
276 		*/
277 		void collectGridColumnStylesAndIds( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxControl );
278 
279 		/** ensures that the number format of the given control exist in our own formats supplier.
280 
281 			<p>The given control is examined for it's format (i.e. it's FormatKey/FormatsSupplier properties),
282 			and the format is added (if necessary) to m_xControlNumberFormats</p>.
283 
284 			@return
285 				the format key of the control's format relative to our own formats supplier
286 
287 		*/
288 		sal_Int32	ensureTranslateFormat(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxFormattedControl);
289 
290 		/// returns the instance exporting our control's number styles
291 		SvXMLNumFmtExport*	getControlNumberStyleExport();
292 
293 		/// ensures that the instance exporting our control's number styles exists
294 		void				ensureControlNumberStyleExport();
295 
296 		/** determines the number format style for the given object without remembering it
297 		*/
298 		::rtl::OUString
299 				getImmediateNumberStyle( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject );
300 
301 		/** returns the prefix to be used for control number styles
302 		*/
303 		static const ::rtl::OUString& getControlNumberStyleNamePrefix();
304 
305 		/** exclude the given control (model) from export.
306 
307 			<p>If your document contains form controls which are not to be exported for whatever reason,
308 			you need to announce the models of these controls (can be retrieved from XControlShape::getControl)
309 			to the form layer exporter.<br/>
310 			Of course you have to do this before calling <member>exportForms</member></p>
311 		*/
312 		void excludeFromExport( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > _rxControl );
313 	};
314 
315 //.........................................................................
316 }	// namespace xmloff
317 //.........................................................................
318 
319 #endif // _XMLOFF_FORMS_LAYEREXPORT_HXX_
320 
321