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 <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"
32 :
33 :
34 :
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;
44 :
45 281 : Reference < XStyle > XMLTextMasterPageContext::Create()
46 : {
47 281 : Reference < XStyle > xNewStyle;
48 :
49 281 : Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
50 562 : UNO_QUERY );
51 281 : if( xFactory.is() )
52 : {
53 : Reference < XInterface > xIfc =
54 281 : xFactory->createInstance("com.sun.star.style.PageStyle");
55 281 : if( xIfc.is() )
56 281 : xNewStyle = Reference < XStyle >( xIfc, UNO_QUERY );
57 : }
58 :
59 562 : return xNewStyle;
60 : }
61 0 : TYPEINIT1( XMLTextMasterPageContext, SvXMLStyleContext );
62 :
63 1033 : 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 1033 : , bFooterFirstInserted( false )
82 : {
83 2066 : OUString sName, sDisplayName;
84 1033 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
85 3372 : for( sal_Int16 i=0; i < nAttrCount; i++ )
86 : {
87 2339 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
88 4678 : OUString aLocalName;
89 2339 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
90 2339 : if( XML_NAMESPACE_STYLE == nPrefix )
91 : {
92 2339 : if( IsXMLToken( aLocalName, XML_NAME ) )
93 : {
94 1033 : sName = xAttrList->getValueByIndex( i );
95 : }
96 1306 : else if( IsXMLToken( aLocalName, XML_DISPLAY_NAME ) )
97 : {
98 230 : sDisplayName = xAttrList->getValueByIndex( i );
99 : }
100 1076 : else if( IsXMLToken( aLocalName, XML_NEXT_STYLE_NAME ) )
101 : {
102 43 : sFollow = xAttrList->getValueByIndex( i );
103 : }
104 1033 : else if( IsXMLToken( aLocalName, XML_PAGE_LAYOUT_NAME ) )
105 : {
106 1033 : sPageMasterName = xAttrList->getValueByIndex( i );
107 : }
108 : }
109 2339 : }
110 :
111 1033 : if( !sDisplayName.isEmpty() )
112 : {
113 : rImport.AddStyleDisplayName( XML_STYLE_FAMILY_MASTER_PAGE, sName,
114 230 : sDisplayName );
115 : }
116 : else
117 : {
118 803 : sDisplayName = sName;
119 : }
120 :
121 1033 : if( sDisplayName.isEmpty() )
122 0 : return;
123 :
124 : Reference < XNameContainer > xPageStyles =
125 2066 : GetImport().GetTextImport()->GetPageStyles();
126 1033 : if( !xPageStyles.is() )
127 0 : return;
128 :
129 2066 : Any aAny;
130 1033 : bool bNew = false;
131 1033 : if( xPageStyles->hasByName( sDisplayName ) )
132 : {
133 752 : aAny = xPageStyles->getByName( sDisplayName );
134 752 : aAny >>= xStyle;
135 : }
136 : else
137 : {
138 281 : xStyle = Create();
139 281 : if( !xStyle.is() )
140 0 : return;
141 :
142 281 : aAny <<= xStyle;
143 281 : xPageStyles->insertByName( sDisplayName, aAny );
144 281 : bNew = true;
145 : }
146 :
147 2066 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
148 : Reference< XPropertySetInfo > xPropSetInfo =
149 2066 : xPropSet->getPropertySetInfo();
150 1033 : if( !bNew && xPropSetInfo->hasPropertyByName( sIsPhysical ) )
151 : {
152 416 : aAny = xPropSet->getPropertyValue( sIsPhysical );
153 416 : bNew = !*static_cast<sal_Bool const *>(aAny.getValue());
154 : }
155 1033 : SetNew( bNew );
156 :
157 1033 : if( bOverwrite || bNew )
158 : {
159 : Reference < XMultiPropertyStates > xMultiStates( xPropSet,
160 980 : UNO_QUERY );
161 : OSL_ENSURE( xMultiStates.is(),
162 : "text page style does not support multi property set" );
163 980 : if( xMultiStates.is() )
164 980 : xMultiStates->setAllPropertiesToDefault();
165 :
166 980 : bInsertHeader = bInsertFooter = true;
167 980 : bInsertHeaderLeft = bInsertFooterLeft = true;
168 980 : bInsertHeaderFirst = bInsertFooterFirst = true;
169 1033 : }
170 : }
171 :
172 1625 : XMLTextMasterPageContext::~XMLTextMasterPageContext()
173 : {
174 1625 : }
175 :
176 1570 : SvXMLImportContext *XMLTextMasterPageContext::CreateChildContext(
177 : sal_uInt16 nPrefix,
178 : const OUString& rLocalName,
179 : const Reference< XAttributeList > & xAttrList )
180 : {
181 1570 : SvXMLImportContext *pContext = 0;
182 :
183 : const SvXMLTokenMap& rTokenMap =
184 1570 : GetImport().GetTextImport()->GetTextMasterPageElemTokenMap();
185 :
186 1570 : bool bInsert = false, bFooter = false, bLeft = false, bFirst = false;
187 1570 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
188 : {
189 : case XML_TOK_TEXT_MP_HEADER:
190 412 : if( bInsertHeader && !bHeaderInserted )
191 : {
192 412 : bInsert = true;
193 412 : bHeaderInserted = true;
194 : }
195 412 : break;
196 : case XML_TOK_TEXT_MP_FOOTER:
197 427 : if( bInsertFooter && !bFooterInserted )
198 : {
199 427 : bInsert = bFooter = true;
200 427 : bFooterInserted = true;
201 : }
202 427 : break;
203 : case XML_TOK_TEXT_MP_HEADER_LEFT:
204 355 : if( bInsertHeaderLeft && bHeaderInserted && !bHeaderLeftInserted )
205 355 : bInsert = bLeft = true;
206 355 : break;
207 : case XML_TOK_TEXT_MP_FOOTER_LEFT:
208 362 : if( bInsertFooterLeft && bFooterInserted && !bFooterLeftInserted )
209 362 : bInsert = bFooter = bLeft = true;
210 362 : break;
211 : case XML_TOK_TEXT_MP_HEADER_FIRST:
212 7 : if( bInsertHeaderFirst && bHeaderInserted && !bHeaderFirstInserted )
213 7 : bInsert = bFirst = true;
214 7 : break;
215 : case XML_TOK_TEXT_MP_FOOTER_FIRST:
216 7 : if( bInsertFooterFirst && bFooterInserted && !bFooterFirstInserted )
217 7 : bInsert = bFooter = bFirst = true;
218 7 : break;
219 : }
220 :
221 1570 : if( bInsert && xStyle.is() )
222 : {
223 : pContext = CreateHeaderFooterContext( nPrefix, rLocalName,
224 : xAttrList,
225 1570 : bFooter, bLeft, bFirst );
226 : }
227 : else
228 : {
229 : pContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
230 0 : xAttrList );
231 : }
232 :
233 1570 : return pContext;
234 : }
235 :
236 160 : 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 )
243 : {
244 160 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
245 160 : return new XMLTextHeaderFooterContext( GetImport(),
246 : nPrefix, rLocalName,
247 : xAttrList,
248 : xPropSet,
249 160 : bFooter, bLeft, bFirst );
250 : }
251 :
252 1033 : void XMLTextMasterPageContext::Finish( bool bOverwrite )
253 : {
254 1033 : if( xStyle.is() && (IsNew() || bOverwrite) )
255 : {
256 980 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
257 980 : if( !sPageMasterName.isEmpty() )
258 : {
259 : XMLPropStyleContext* pStyle =
260 980 : GetImport().GetTextImport()->FindPageMaster( sPageMasterName );
261 980 : if (pStyle)
262 : {
263 980 : pStyle->FillPropertySet(xPropSet);
264 : }
265 : }
266 :
267 : Reference < XNameContainer > xPageStyles =
268 1960 : GetImport().GetTextImport()->GetPageStyles();
269 980 : if( !xPageStyles.is() )
270 1033 : return;
271 :
272 : Reference< XPropertySetInfo > xPropSetInfo =
273 1960 : xPropSet->getPropertySetInfo();
274 980 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
275 : {
276 : OUString sDisplayFollow(
277 539 : GetImport().GetStyleDisplayName(
278 1078 : XML_STYLE_FAMILY_MASTER_PAGE, sFollow ) );
279 582 : if( sDisplayFollow.isEmpty() ||
280 43 : !xPageStyles->hasByName( sDisplayFollow ) )
281 496 : sDisplayFollow = xStyle->getName();
282 :
283 1078 : Any aAny = xPropSet->getPropertyValue( sFollowStyle );
284 1078 : OUString sCurrFollow;
285 539 : aAny >>= sCurrFollow;
286 539 : if( sCurrFollow != sDisplayFollow )
287 : {
288 23 : aAny <<= sDisplayFollow;
289 23 : xPropSet->setPropertyValue( sFollowStyle, aAny );
290 539 : }
291 : }
292 :
293 980 : if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
294 : {
295 980 : xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
296 980 : }
297 : }
298 : }
299 :
300 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|