bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLTextMasterPageContext.cxx
blob21eafe49df49e3c69bc92dcfee32fe48525cbf6a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/style/XStyle.hpp>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/style/PageStyleLayout.hpp>
23 #include <com/sun/star/beans/XMultiPropertyStates.hpp>
24 #include <osl/diagnose.h>
25 #include <xmloff/nmspmap.hxx>
26 #include <xmloff/xmlnmspe.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/XMLTextMasterPageContext.hxx>
29 #include "XMLTextHeaderFooterContext.hxx"
30 #include <xmloff/xmlimp.hxx>
31 #include "PageMasterImportContext.hxx"
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::xml::sax;
38 using namespace ::com::sun::star::style;
39 using namespace ::com::sun::star::text;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::lang;
43 using namespace ::xmloff::token;
45 Reference < XStyle > XMLTextMasterPageContext::Create()
47 Reference < XStyle > xNewStyle;
49 Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
50 UNO_QUERY );
51 if( xFactory.is() )
53 Reference < XInterface > xIfc =
54 xFactory->createInstance("com.sun.star.style.PageStyle");
55 if( xIfc.is() )
56 xNewStyle = Reference < XStyle >( xIfc, UNO_QUERY );
59 return xNewStyle;
61 TYPEINIT1( XMLTextMasterPageContext, SvXMLStyleContext );
63 XMLTextMasterPageContext::XMLTextMasterPageContext( SvXMLImport& rImport,
64 sal_uInt16 nPrfx, const OUString& rLName,
65 const Reference< XAttributeList > & xAttrList,
66 bool bOverwrite )
67 : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, XML_STYLE_FAMILY_MASTER_PAGE )
68 , sIsPhysical( "IsPhysical" )
69 , sFollowStyle( "FollowStyle" )
70 , bInsertHeader( false )
71 , bInsertFooter( false )
72 , bInsertHeaderLeft( false )
73 , bInsertFooterLeft( false )
74 , bInsertHeaderFirst( false )
75 , bInsertFooterFirst( false )
76 , bHeaderInserted( false )
77 , bFooterInserted( false )
78 , bHeaderLeftInserted( false )
79 , bFooterLeftInserted( false )
80 , bHeaderFirstInserted( false )
81 , bFooterFirstInserted( false )
83 OUString sName, sDisplayName;
84 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
85 for( sal_Int16 i=0; i < nAttrCount; i++ )
87 const OUString& rAttrName = xAttrList->getNameByIndex( i );
88 OUString aLocalName;
89 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
90 if( XML_NAMESPACE_STYLE == nPrefix )
92 if( IsXMLToken( aLocalName, XML_NAME ) )
94 sName = xAttrList->getValueByIndex( i );
96 else if( IsXMLToken( aLocalName, XML_DISPLAY_NAME ) )
98 sDisplayName = xAttrList->getValueByIndex( i );
100 else if( IsXMLToken( aLocalName, XML_NEXT_STYLE_NAME ) )
102 sFollow = xAttrList->getValueByIndex( i );
104 else if( IsXMLToken( aLocalName, XML_PAGE_LAYOUT_NAME ) )
106 sPageMasterName = xAttrList->getValueByIndex( i );
111 if( !sDisplayName.isEmpty() )
113 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_MASTER_PAGE, sName,
114 sDisplayName );
116 else
118 sDisplayName = sName;
121 if( sDisplayName.isEmpty() )
122 return;
124 Reference < XNameContainer > xPageStyles =
125 GetImport().GetTextImport()->GetPageStyles();
126 if( !xPageStyles.is() )
127 return;
129 Any aAny;
130 bool bNew = false;
131 if( xPageStyles->hasByName( sDisplayName ) )
133 aAny = xPageStyles->getByName( sDisplayName );
134 aAny >>= xStyle;
136 else
138 xStyle = Create();
139 if( !xStyle.is() )
140 return;
142 aAny <<= xStyle;
143 xPageStyles->insertByName( sDisplayName, aAny );
144 bNew = true;
147 Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
148 Reference< XPropertySetInfo > xPropSetInfo =
149 xPropSet->getPropertySetInfo();
150 if( !bNew && xPropSetInfo->hasPropertyByName( sIsPhysical ) )
152 aAny = xPropSet->getPropertyValue( sIsPhysical );
153 bNew = !*static_cast<sal_Bool const *>(aAny.getValue());
155 SetNew( bNew );
157 if( bOverwrite || bNew )
159 Reference < XMultiPropertyStates > xMultiStates( xPropSet,
160 UNO_QUERY );
161 OSL_ENSURE( xMultiStates.is(),
162 "text page style does not support multi property set" );
163 if( xMultiStates.is() )
164 xMultiStates->setAllPropertiesToDefault();
166 bInsertHeader = bInsertFooter = true;
167 bInsertHeaderLeft = bInsertFooterLeft = true;
168 bInsertHeaderFirst = bInsertFooterFirst = true;
172 XMLTextMasterPageContext::~XMLTextMasterPageContext()
176 SvXMLImportContext *XMLTextMasterPageContext::CreateChildContext(
177 sal_uInt16 nPrefix,
178 const OUString& rLocalName,
179 const Reference< XAttributeList > & xAttrList )
181 SvXMLImportContext *pContext = 0;
183 const SvXMLTokenMap& rTokenMap =
184 GetImport().GetTextImport()->GetTextMasterPageElemTokenMap();
186 bool bInsert = false, bFooter = false, bLeft = false, bFirst = false;
187 switch( rTokenMap.Get( nPrefix, rLocalName ) )
189 case XML_TOK_TEXT_MP_HEADER:
190 if( bInsertHeader && !bHeaderInserted )
192 bInsert = true;
193 bHeaderInserted = true;
195 break;
196 case XML_TOK_TEXT_MP_FOOTER:
197 if( bInsertFooter && !bFooterInserted )
199 bInsert = bFooter = true;
200 bFooterInserted = true;
202 break;
203 case XML_TOK_TEXT_MP_HEADER_LEFT:
204 if( bInsertHeaderLeft && bHeaderInserted && !bHeaderLeftInserted )
205 bInsert = bLeft = true;
206 break;
207 case XML_TOK_TEXT_MP_FOOTER_LEFT:
208 if( bInsertFooterLeft && bFooterInserted && !bFooterLeftInserted )
209 bInsert = bFooter = bLeft = true;
210 break;
211 case XML_TOK_TEXT_MP_HEADER_FIRST:
212 if( bInsertHeaderFirst && bHeaderInserted && !bHeaderFirstInserted )
213 bInsert = bFirst = true;
214 break;
215 case XML_TOK_TEXT_MP_FOOTER_FIRST:
216 if( bInsertFooterFirst && bFooterInserted && !bFooterFirstInserted )
217 bInsert = bFooter = bFirst = true;
218 break;
221 if( bInsert && xStyle.is() )
223 pContext = CreateHeaderFooterContext( nPrefix, rLocalName,
224 xAttrList,
225 bFooter, bLeft, bFirst );
227 else
229 pContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
230 xAttrList );
233 return pContext;
236 SvXMLImportContext *XMLTextMasterPageContext::CreateHeaderFooterContext(
237 sal_uInt16 nPrefix,
238 const OUString& rLocalName,
239 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList > & xAttrList,
240 const bool bFooter,
241 const bool bLeft,
242 const bool bFirst )
244 Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
245 return new XMLTextHeaderFooterContext( GetImport(),
246 nPrefix, rLocalName,
247 xAttrList,
248 xPropSet,
249 bFooter, bLeft, bFirst );
252 void XMLTextMasterPageContext::Finish( bool bOverwrite )
254 if( xStyle.is() && (IsNew() || bOverwrite) )
256 Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
257 if( !sPageMasterName.isEmpty() )
259 XMLPropStyleContext* pStyle =
260 GetImport().GetTextImport()->FindPageMaster( sPageMasterName );
261 if (pStyle)
263 pStyle->FillPropertySet(xPropSet);
267 Reference < XNameContainer > xPageStyles =
268 GetImport().GetTextImport()->GetPageStyles();
269 if( !xPageStyles.is() )
270 return;
272 Reference< XPropertySetInfo > xPropSetInfo =
273 xPropSet->getPropertySetInfo();
274 if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
276 OUString sDisplayFollow(
277 GetImport().GetStyleDisplayName(
278 XML_STYLE_FAMILY_MASTER_PAGE, sFollow ) );
279 if( sDisplayFollow.isEmpty() ||
280 !xPageStyles->hasByName( sDisplayFollow ) )
281 sDisplayFollow = xStyle->getName();
283 Any aAny = xPropSet->getPropertyValue( sFollowStyle );
284 OUString sCurrFollow;
285 aAny >>= sCurrFollow;
286 if( sCurrFollow != sDisplayFollow )
288 aAny <<= sDisplayFollow;
289 xPropSet->setPropertyValue( sFollowStyle, aAny );
293 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
295 xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */