Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 <xmloff/nmspmap.hxx>
25 : #include <xmloff/xmlnmspe.hxx>
26 : #include <xmloff/xmltoken.hxx>
27 : #include <xmloff/XMLTextMasterPageContext.hxx>
28 : #include "XMLTextHeaderFooterContext.hxx"
29 : #include <xmloff/xmlimp.hxx>
30 : #include "PageMasterImportContext.hxx"
31 :
32 :
33 :
34 : using namespace ::com::sun::star;
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::xml::sax;
37 : using namespace ::com::sun::star::style;
38 : using namespace ::com::sun::star::text;
39 : using namespace ::com::sun::star::beans;
40 : using namespace ::com::sun::star::container;
41 : using namespace ::com::sun::star::lang;
42 : using namespace ::xmloff::token;
43 :
44 374 : Reference < XStyle > XMLTextMasterPageContext::Create()
45 : {
46 374 : Reference < XStyle > xNewStyle;
47 :
48 374 : Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
49 748 : UNO_QUERY );
50 374 : if( xFactory.is() )
51 : {
52 : Reference < XInterface > xIfc =
53 374 : xFactory->createInstance("com.sun.star.style.PageStyle");
54 374 : if( xIfc.is() )
55 374 : xNewStyle = Reference < XStyle >( xIfc, UNO_QUERY );
56 : }
57 :
58 748 : return xNewStyle;
59 : }
60 0 : TYPEINIT1( XMLTextMasterPageContext, SvXMLStyleContext );
61 :
62 1504 : XMLTextMasterPageContext::XMLTextMasterPageContext( SvXMLImport& rImport,
63 : sal_uInt16 nPrfx, const OUString& rLName,
64 : const Reference< XAttributeList > & xAttrList,
65 : bool bOverwrite )
66 : : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, XML_STYLE_FAMILY_MASTER_PAGE )
67 : , sIsPhysical( "IsPhysical" )
68 : , sFollowStyle( "FollowStyle" )
69 : , bInsertHeader( false )
70 : , bInsertFooter( false )
71 : , bInsertHeaderLeft( false )
72 : , bInsertFooterLeft( false )
73 : , bInsertHeaderFirst( false )
74 : , bInsertFooterFirst( false )
75 : , bHeaderInserted( false )
76 : , bFooterInserted( false )
77 : , bHeaderLeftInserted( false )
78 : , bFooterLeftInserted( false )
79 : , bHeaderFirstInserted( false )
80 1504 : , bFooterFirstInserted( false )
81 : {
82 3008 : OUString sName, sDisplayName;
83 1504 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
84 4822 : for( sal_Int16 i=0; i < nAttrCount; i++ )
85 : {
86 3318 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
87 6636 : OUString aLocalName;
88 3318 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
89 3318 : if( XML_NAMESPACE_STYLE == nPrefix )
90 : {
91 3318 : if( IsXMLToken( aLocalName, XML_NAME ) )
92 : {
93 1504 : sName = xAttrList->getValueByIndex( i );
94 : }
95 1814 : else if( IsXMLToken( aLocalName, XML_DISPLAY_NAME ) )
96 : {
97 258 : sDisplayName = xAttrList->getValueByIndex( i );
98 : }
99 1556 : else if( IsXMLToken( aLocalName, XML_NEXT_STYLE_NAME ) )
100 : {
101 52 : sFollow = xAttrList->getValueByIndex( i );
102 : }
103 1504 : else if( IsXMLToken( aLocalName, XML_PAGE_LAYOUT_NAME ) )
104 : {
105 1504 : sPageMasterName = xAttrList->getValueByIndex( i );
106 : }
107 : }
108 3318 : }
109 :
110 1504 : if( !sDisplayName.isEmpty() )
111 : {
112 : rImport.AddStyleDisplayName( XML_STYLE_FAMILY_MASTER_PAGE, sName,
113 258 : sDisplayName );
114 : }
115 : else
116 : {
117 1246 : sDisplayName = sName;
118 : }
119 :
120 1504 : if( sDisplayName.isEmpty() )
121 0 : return;
122 :
123 : Reference < XNameContainer > xPageStyles =
124 3008 : GetImport().GetTextImport()->GetPageStyles();
125 1504 : if( !xPageStyles.is() )
126 0 : return;
127 :
128 3008 : Any aAny;
129 1504 : bool bNew = false;
130 1504 : if( xPageStyles->hasByName( sDisplayName ) )
131 : {
132 1130 : aAny = xPageStyles->getByName( sDisplayName );
133 1130 : aAny >>= xStyle;
134 : }
135 : else
136 : {
137 374 : xStyle = Create();
138 374 : if( !xStyle.is() )
139 0 : return;
140 :
141 374 : aAny <<= xStyle;
142 374 : xPageStyles->insertByName( sDisplayName, aAny );
143 374 : bNew = true;
144 : }
145 :
146 3008 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
147 : Reference< XPropertySetInfo > xPropSetInfo =
148 3008 : xPropSet->getPropertySetInfo();
149 1504 : if( !bNew && xPropSetInfo->hasPropertyByName( sIsPhysical ) )
150 : {
151 598 : aAny = xPropSet->getPropertyValue( sIsPhysical );
152 598 : bNew = !*(sal_Bool *)aAny.getValue();
153 : }
154 1504 : SetNew( bNew );
155 :
156 1504 : if( bOverwrite || bNew )
157 : {
158 : Reference < XMultiPropertyStates > xMultiStates( xPropSet,
159 1402 : UNO_QUERY );
160 : OSL_ENSURE( xMultiStates.is(),
161 : "text page style does not support multi property set" );
162 1402 : if( xMultiStates.is() )
163 1402 : xMultiStates->setAllPropertiesToDefault();
164 :
165 1402 : bInsertHeader = bInsertFooter = true;
166 1402 : bInsertHeaderLeft = bInsertFooterLeft = true;
167 1402 : bInsertHeaderFirst = bInsertFooterFirst = true;
168 1504 : }
169 : }
170 :
171 2274 : XMLTextMasterPageContext::~XMLTextMasterPageContext()
172 : {
173 2274 : }
174 :
175 2464 : SvXMLImportContext *XMLTextMasterPageContext::CreateChildContext(
176 : sal_uInt16 nPrefix,
177 : const OUString& rLocalName,
178 : const Reference< XAttributeList > & xAttrList )
179 : {
180 2464 : SvXMLImportContext *pContext = 0;
181 :
182 : const SvXMLTokenMap& rTokenMap =
183 2464 : GetImport().GetTextImport()->GetTextMasterPageElemTokenMap();
184 :
185 2464 : bool bInsert = false, bFooter = false, bLeft = false, bFirst = false;
186 2464 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
187 : {
188 : case XML_TOK_TEXT_MP_HEADER:
189 658 : if( bInsertHeader && !bHeaderInserted )
190 : {
191 658 : bInsert = true;
192 658 : bHeaderInserted = true;
193 : }
194 658 : break;
195 : case XML_TOK_TEXT_MP_FOOTER:
196 656 : if( bInsertFooter && !bFooterInserted )
197 : {
198 656 : bInsert = bFooter = true;
199 656 : bFooterInserted = true;
200 : }
201 656 : break;
202 : case XML_TOK_TEXT_MP_HEADER_LEFT:
203 560 : if( bInsertHeaderLeft && bHeaderInserted && !bHeaderLeftInserted )
204 560 : bInsert = bLeft = true;
205 560 : break;
206 : case XML_TOK_TEXT_MP_FOOTER_LEFT:
207 562 : if( bInsertFooterLeft && bFooterInserted && !bFooterLeftInserted )
208 562 : bInsert = bFooter = bLeft = true;
209 562 : break;
210 : case XML_TOK_TEXT_MP_HEADER_FIRST:
211 14 : if( bInsertHeaderFirst && bHeaderInserted && !bHeaderFirstInserted )
212 14 : bInsert = bFirst = true;
213 14 : break;
214 : case XML_TOK_TEXT_MP_FOOTER_FIRST:
215 14 : if( bInsertFooterFirst && bFooterInserted && !bFooterFirstInserted )
216 14 : bInsert = bFooter = bFirst = true;
217 14 : break;
218 : }
219 :
220 2464 : if( bInsert && xStyle.is() )
221 : {
222 : pContext = CreateHeaderFooterContext( nPrefix, rLocalName,
223 : xAttrList,
224 2464 : bFooter, bLeft, bFirst );
225 : }
226 : else
227 : {
228 : pContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
229 0 : xAttrList );
230 : }
231 :
232 2464 : return pContext;
233 : }
234 :
235 220 : SvXMLImportContext *XMLTextMasterPageContext::CreateHeaderFooterContext(
236 : sal_uInt16 nPrefix,
237 : const OUString& rLocalName,
238 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList > & xAttrList,
239 : const bool bFooter,
240 : const bool bLeft,
241 : const bool bFirst )
242 : {
243 220 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
244 220 : return new XMLTextHeaderFooterContext( GetImport(),
245 : nPrefix, rLocalName,
246 : xAttrList,
247 : xPropSet,
248 220 : bFooter, bLeft, bFirst );
249 : }
250 :
251 1504 : void XMLTextMasterPageContext::Finish( bool bOverwrite )
252 : {
253 1504 : if( xStyle.is() && (IsNew() || bOverwrite) )
254 : {
255 1402 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
256 1402 : if( !sPageMasterName.isEmpty() )
257 : {
258 : XMLPropStyleContext* pStyle =
259 1402 : GetImport().GetTextImport()->FindPageMaster( sPageMasterName );
260 1402 : if (pStyle)
261 : {
262 1402 : pStyle->FillPropertySet(xPropSet);
263 : }
264 : }
265 :
266 : Reference < XNameContainer > xPageStyles =
267 2804 : GetImport().GetTextImport()->GetPageStyles();
268 1402 : if( !xPageStyles.is() )
269 1504 : return;
270 :
271 : Reference< XPropertySetInfo > xPropSetInfo =
272 2804 : xPropSet->getPropertySetInfo();
273 1402 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
274 : {
275 : OUString sDisplayFollow(
276 668 : GetImport().GetStyleDisplayName(
277 1336 : XML_STYLE_FAMILY_MASTER_PAGE, sFollow ) );
278 720 : if( sDisplayFollow.isEmpty() ||
279 52 : !xPageStyles->hasByName( sDisplayFollow ) )
280 616 : sDisplayFollow = xStyle->getName();
281 :
282 1336 : Any aAny = xPropSet->getPropertyValue( sFollowStyle );
283 1336 : OUString sCurrFollow;
284 668 : aAny >>= sCurrFollow;
285 668 : if( sCurrFollow != sDisplayFollow )
286 : {
287 30 : aAny <<= sDisplayFollow;
288 30 : xPropSet->setPropertyValue( sFollowStyle, aAny );
289 668 : }
290 : }
291 :
292 1402 : if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
293 : {
294 1402 : xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
295 1402 : }
296 : }
297 : }
298 :
299 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|