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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include <xmloff/XMLPageExport.hxx>
31 #include <tools/debug.hxx>
32 #include "xmloff/xmlnmspe.hxx"
33 #include <xmloff/xmltoken.hxx>
34 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
35 #include <com/sun/star/style/XStyle.hpp>
36 #include <com/sun/star/container/XNameContainer.hpp>
37 #include <com/sun/star/container/XIndexReplace.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <xmloff/families.hxx>
40 #include <xmloff/xmlexp.hxx>
41 #include "PageMasterPropHdlFactory.hxx"
42 #ifndef _XMLOFF_PAGEMASTERSTYLEMAP_HXX
43 #include <xmloff/PageMasterStyleMap.hxx>
44 #endif
45 #ifndef _XMLOFF_PAGEMASTERPROPMAPPER_HXX
46 #include "PageMasterPropMapper.hxx"
47 #endif
48 #include "PageMasterExportPropMapper.hxx"
49 #include "PageMasterExportPropMapper.hxx"
50 
51 using ::rtl::OUString;
52 using ::rtl::OUStringBuffer;
53 
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::style;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::beans;
59 using namespace ::xmloff::token;
60 
61 
62 //______________________________________________________________________________
63 
64 sal_Bool XMLPageExport::findPageMasterName( const OUString& rStyleName, OUString& rPMName ) const
65 {
66 	for( ::std::vector< XMLPageExportNameEntry >::const_iterator pEntry = aNameVector.begin();
67 			pEntry != aNameVector.end(); pEntry++ )
68 	{
69 		if( pEntry->sStyleName == rStyleName )
70 		{
71 			rPMName = pEntry->sPageMasterName;
72 			return sal_True;
73 		}
74 	}
75 	return sal_False;
76 }
77 
78 void XMLPageExport::collectPageMasterAutoStyle(
79 		const Reference < XPropertySet > & rPropSet,
80 		OUString& rPageMasterName )
81 {
82 	DBG_ASSERT( xPageMasterPropSetMapper.is(), "page master family/XMLPageMasterPropSetMapper not found" );
83 	if( xPageMasterPropSetMapper.is() )
84 	{
85 		::std::vector<XMLPropertyState> xPropStates = xPageMasterExportPropMapper->Filter( rPropSet );
86 		if( !xPropStates.empty())
87 		{
88 			OUString sParent;
89 			rPageMasterName = rExport.GetAutoStylePool()->Find( XML_STYLE_FAMILY_PAGE_MASTER, sParent, xPropStates );
90 			if (!rPageMasterName.getLength())
91 				rPageMasterName = rExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_PAGE_MASTER, sParent, xPropStates);
92 		}
93 	}
94 }
95 
96 void XMLPageExport::exportMasterPageContent(
97 				const Reference < XPropertySet > &,
98 				sal_Bool /*bAutoStyles*/ )
99 {
100 
101 }
102 
103 sal_Bool XMLPageExport::exportStyle(
104 			const Reference< XStyle >& rStyle,
105 			sal_Bool bAutoStyles )
106 {
107 	Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
108 	Reference< XPropertySetInfo > xPropSetInfo = xPropSet->getPropertySetInfo();
109 
110 	// Don't export styles that aren't existing really. This may be the
111 	// case for StarOffice Writer's pool styles.
112 	if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
113 	{
114 		Any aAny = xPropSet->getPropertyValue( sIsPhysical );
115 		if( !*(sal_Bool *)aAny.getValue() )
116 			return sal_False;
117 	}
118 
119 	if( bAutoStyles )
120 	{
121 		XMLPageExportNameEntry aEntry;
122 		collectPageMasterAutoStyle( xPropSet, aEntry.sPageMasterName );
123 		aEntry.sStyleName = rStyle->getName();
124 		aNameVector.push_back( aEntry );
125 
126 		exportMasterPageContent( xPropSet, sal_True );
127 	}
128 	else
129 	{
130 		OUString sName( rStyle->getName() );
131 		sal_Bool bEncoded = sal_False;
132 		GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME,
133 						  GetExport().EncodeStyleName( sName, &bEncoded ) );
134 
135 		if( bEncoded )
136 			GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
137 							  	 sName);
138 
139 		OUString sPMName;
140 		if( findPageMasterName( sName, sPMName ) )
141 			GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_PAGE_LAYOUT_NAME, GetExport().EncodeStyleName( sPMName ) );
142 
143         Reference<XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
144         if ( xInfo.is() && xInfo->hasPropertyByName(sFollowStyle) )
145         {
146             OUString sNextName;
147 		    xPropSet->getPropertyValue( sFollowStyle ) >>= sNextName;
148 
149 		    if( sName != sNextName && sNextName.getLength() )
150 		    {
151 			    GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
152 				    GetExport().EncodeStyleName( sNextName ) );
153 		    }
154         }
155 //		OUString sPageMaster = GetExport().GetAutoStylePool()->Find(
156 //											XML_STYLE_FAMILY_PAGE_MASTER,
157 //											xPropSet );
158 //		if( sPageMaster.getLength() )
159 //			GetExport().AddAttribute( XML_NAMESPACE_STYLE,
160 //									  XML_PAGE_MASTER_NAME,
161 //									  sPageMaster );
162 
163 		SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
164 								  XML_MASTER_PAGE, sal_True, sal_True );
165 
166 		exportMasterPageContent( xPropSet, sal_False );
167 	}
168 
169 	return sal_True;
170 }
171 
172 XMLPageExport::XMLPageExport( SvXMLExport& rExp ) :
173 	rExport( rExp ),
174 	sIsPhysical( RTL_CONSTASCII_USTRINGPARAM( "IsPhysical" ) ),
175 	sFollowStyle( RTL_CONSTASCII_USTRINGPARAM( "FollowStyle" ) )
176 {
177 	xPageMasterPropHdlFactory = new XMLPageMasterPropHdlFactory;
178 	xPageMasterPropSetMapper = new XMLPageMasterPropSetMapper(
179 								(XMLPropertyMapEntry*) aXMLPageMasterStyleMap,
180 								xPageMasterPropHdlFactory  );
181 	xPageMasterExportPropMapper = new XMLPageMasterExportPropMapper(
182 									xPageMasterPropSetMapper, rExp);
183 
184 	rExport.GetAutoStylePool()->AddFamily( XML_STYLE_FAMILY_PAGE_MASTER, OUString( RTL_CONSTASCII_USTRINGPARAM( XML_STYLE_FAMILY_PAGE_MASTER_NAME ) ),
185 		xPageMasterExportPropMapper, OUString( RTL_CONSTASCII_USTRINGPARAM( XML_STYLE_FAMILY_PAGE_MASTER_PREFIX ) ), sal_False );
186 
187 	Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(),
188 													   UNO_QUERY );
189 	DBG_ASSERT( xFamiliesSupp.is(),
190 				"No XStyleFamiliesSupplier from XModel for export!" );
191 	if( xFamiliesSupp.is() )
192 	{
193 		Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
194 		DBG_ASSERT( xFamiliesSupp.is(),
195 					"getStyleFamilies() from XModel failed for export!" );
196 		if( xFamilies.is() )
197 		{
198 			const OUString aPageStyleName(
199 						RTL_CONSTASCII_USTRINGPARAM( "PageStyles" ));
200 
201 			if( xFamilies->hasByName( aPageStyleName ) )
202 			{
203                 xPageStyles.set(xFamilies->getByName( aPageStyleName ),uno::UNO_QUERY);
204 
205 				DBG_ASSERT( xPageStyles.is(),
206 							"Page Styles not found for export!" );
207 			}
208 		}
209 	}
210 }
211 
212 XMLPageExport::~XMLPageExport()
213 {
214 }
215 
216 void XMLPageExport::exportStyles( sal_Bool bUsed, sal_Bool bAutoStyles )
217 {
218 	if( xPageStyles.is() )
219 	{
220         uno::Sequence< ::rtl::OUString> aSeq = xPageStyles->getElementNames();
221         const ::rtl::OUString* pIter = aSeq.getConstArray();
222         const ::rtl::OUString* pEnd	  = pIter + aSeq.getLength();
223         for(;pIter != pEnd;++pIter)
224 		{
225             Reference< XStyle > xStyle(xPageStyles->getByName( *pIter ),uno::UNO_QUERY);
226 			if( !bUsed || xStyle->isInUse() )
227 				exportStyle( xStyle, bAutoStyles );
228 		}
229 	}
230 }
231 
232 void XMLPageExport::exportAutoStyles()
233 {
234 	rExport.GetAutoStylePool()->exportXML(XML_STYLE_FAMILY_PAGE_MASTER
235         , rExport.GetDocHandler(), rExport.GetMM100UnitConverter(),
236 		rExport.GetNamespaceMap()
237         );
238 }
239 
240 void XMLPageExport::exportDefaultStyle()
241 {
242 	Reference < lang::XMultiServiceFactory > xFactory (GetExport().GetModel(), UNO_QUERY);
243 	if (xFactory.is())
244 	{
245 		OUString sTextDefaults ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.text.Defaults" ) );
246 		Reference < XPropertySet > xPropSet (xFactory->createInstance ( sTextDefaults ), UNO_QUERY);
247 		if (xPropSet.is())
248 		{
249 			// <style:default-style ...>
250 			GetExport().CheckAttrList();
251 
252 			::std::vector< XMLPropertyState > xPropStates =
253 				xPageMasterExportPropMapper->FilterDefaults( xPropSet );
254 
255 			sal_Bool bExport = sal_False;
256 			UniReference < XMLPropertySetMapper > aPropMapper(xPageMasterExportPropMapper->getPropertySetMapper());
257 			for( ::std::vector< XMLPropertyState >::iterator aIter = xPropStates.begin(); aIter != xPropStates.end(); ++aIter )
258 			{
259         		XMLPropertyState *pProp = &(*aIter);
260 				sal_Int16 nContextId	= aPropMapper->GetEntryContextId( pProp->mnIndex );
261 				if( nContextId == CTF_PM_STANDARD_MODE )
262 				{
263 					bExport = sal_True;
264 					break;
265 				}
266 			}
267 
268 		//	if ( xPropStates.size() != 0 &&
269           //          ( xPropStates.size() != 1 || xPropStates[0].mnIndex != -1 ) )
270 			if( bExport )
271 			{
272 				//<style:default-page-layout>
273 				SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
274 										  XML_DEFAULT_PAGE_LAYOUT,
275 										  sal_True, sal_True );
276 
277 				xPageMasterExportPropMapper->exportXML( GetExport(), xPropStates,
278 									   	  XML_EXPORT_FLAG_IGN_WS );
279 			}
280 		}
281 	}
282 }
283