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/beans/XPropertySet.hpp>
21 #include <com/sun/star/text/XText.hpp>
22 #include <com/sun/star/sheet/XHeaderFooterContent.hpp>
23 #include <xmloff/nmspmap.hxx>
24 #include <xmloff/xmlnmspe.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include "XMLTableHeaderFooterContext.hxx"
27 #include <xmloff/xmltoken.hxx>
28 #include <comphelper/extract.hxx>
29 
30 #include <unonames.hxx>
31 
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::xml::sax;
35 using namespace ::com::sun::star::text;
36 using namespace ::com::sun::star::beans;
37 using namespace xmloff::token;
38 
39 
XMLTableHeaderFooterContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList,const Reference<XPropertySet> & rPageStylePropSet,bool bFooter,bool bLeft)40 XMLTableHeaderFooterContext::XMLTableHeaderFooterContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
41                        const OUString& rLName,
42                        const uno::Reference<
43                             xml::sax::XAttributeList > & xAttrList,
44                        const Reference < XPropertySet > & rPageStylePropSet,
45                        bool bFooter, bool bLeft ) :
46     SvXMLImportContext( rImport, nPrfx, rLName ),
47     xPropSet( rPageStylePropSet ),
48     sOn( bFooter ? OUString(SC_UNO_PAGE_FTRON) : OUString(SC_UNO_PAGE_HDRON) ),
49     bContainsLeft(false),
50     bContainsRight(false),
51     bContainsCenter(false)
52 {
53     OUString sContent( bFooter ? OUString(SC_UNO_PAGE_RIGHTFTRCON) : OUString(SC_UNO_PAGE_RIGHTHDRCON) );
54     OUString sContentLeft( bFooter ? OUString(SC_UNO_PAGE_LEFTFTRCONT) : OUString(SC_UNO_PAGE_LEFTHDRCONT) );
55     OUString sShareContent( bFooter ? OUString(SC_UNO_PAGE_FTRSHARED) : OUString(SC_UNO_PAGE_HDRSHARED) );
56     bool bDisplay( true );
57     sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
58     for( sal_Int16 i=0; i < nAttrCount; ++i )
59     {
60         const OUString& rAttrName(xAttrList->getNameByIndex( i ));
61         OUString aLName;
62         sal_uInt16 nPrefix(GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLName ));
63         const OUString& rValue(xAttrList->getValueByIndex( i ));
64 
65         // TODO: use a map here
66         if( XML_NAMESPACE_STYLE == nPrefix )
67         {
68             if( IsXMLToken(aLName, XML_DISPLAY ) )
69                 bDisplay = IsXMLToken(rValue, XML_TRUE);
70         }
71     }
72     if( bLeft )
73     {
74         bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
75 
76         if( bOn && bDisplay )
77         {
78             if( ::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
79                 // Don't share headers any longer
80                 xPropSet->setPropertyValue( sShareContent, uno::makeAny(false) );
81         }
82         else
83         {
84             if( !::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
85                 // share headers
86                 xPropSet->setPropertyValue( sShareContent, uno::makeAny(true) );
87         }
88     }
89     else
90     {
91         bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
92         if ( bOn != bDisplay )
93             xPropSet->setPropertyValue( sOn, uno::makeAny(bDisplay) );
94     }
95     if (bLeft)
96         sCont = sContentLeft;
97     else
98         sCont = sContent;
99     xPropSet->getPropertyValue( sCont ) >>= xHeaderFooterContent;
100 }
101 
~XMLTableHeaderFooterContext()102 XMLTableHeaderFooterContext::~XMLTableHeaderFooterContext()
103 {
104 }
105 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)106 SvXMLImportContextRef XMLTableHeaderFooterContext::CreateChildContext(
107     sal_uInt16 nPrefix,
108     const OUString& rLocalName,
109     const uno::Reference< xml::sax::XAttributeList > & xAttrList )
110 {
111     SvXMLImportContext *pContext(nullptr);
112 
113     if ((nPrefix == XML_NAMESPACE_TEXT) &&
114         IsXMLToken(rLocalName, XML_P))
115     {
116         if (!xTextCursor.is())
117         {
118             if( xHeaderFooterContent.is() )
119             {
120                 uno::Reference < text::XText > xText(xHeaderFooterContent->getCenterText());
121                 xText->setString("");
122                 xTextCursor.set(xText->createTextCursor());
123                 xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
124                 GetImport().GetTextImport()->SetCursor( xTextCursor );
125                 bContainsCenter = true;
126             }
127         }
128         pContext =
129             GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
130                                                                     nPrefix,
131                                                                     rLocalName,
132                                                                     xAttrList);
133     }
134     else
135     {
136         if (nPrefix == XML_NAMESPACE_STYLE)
137         {
138             if (xHeaderFooterContent.is())
139             {
140                 uno::Reference < text::XText > xText;
141                 if (IsXMLToken(rLocalName, XML_REGION_LEFT ))
142                 {
143                     xText.set(xHeaderFooterContent->getLeftText());
144                     bContainsLeft = true;
145                 }
146                 else if (IsXMLToken(rLocalName, XML_REGION_CENTER ))
147                 {
148                     xText.set(xHeaderFooterContent->getCenterText());
149                     bContainsCenter = true;
150                 }
151                 else if (IsXMLToken(rLocalName, XML_REGION_RIGHT ))
152                 {
153                     xText.set(xHeaderFooterContent->getRightText());
154                     bContainsRight = true;
155                 }
156                 if (xText.is())
157                 {
158                     xText->setString("");
159                     //SvXMLImport aSvXMLImport( GetImport() );
160                     uno::Reference < text::XTextCursor > xTempTextCursor(xText->createTextCursor());
161                     pContext = new XMLHeaderFooterRegionContext( GetImport(), nPrefix, rLocalName, xTempTextCursor);
162                 }
163             }
164         }
165     }
166     if( !pContext )
167         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
168 
169     return pContext;
170 }
171 
EndElement()172 void XMLTableHeaderFooterContext::EndElement()
173 {
174     if( GetImport().GetTextImport()->GetCursor().is() )
175     {
176         //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
177         if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
178         {
179             GetImport().GetTextImport()->GetText()->insertString(
180                 GetImport().GetTextImport()->GetCursorAsRange(), "",
181                 true );
182         }
183         GetImport().GetTextImport()->ResetCursor();
184     }
185     if (xOldTextCursor.is())
186         GetImport().GetTextImport()->SetCursor(xOldTextCursor);
187     if (xHeaderFooterContent.is())
188     {
189         if (!bContainsLeft)
190             xHeaderFooterContent->getLeftText()->setString("");
191         if (!bContainsCenter)
192             xHeaderFooterContent->getCenterText()->setString("");
193         if (!bContainsRight)
194             xHeaderFooterContent->getRightText()->setString("");
195 
196         xPropSet->setPropertyValue( sCont, uno::makeAny(xHeaderFooterContent) );
197     }
198 }
199 
200 
XMLHeaderFooterRegionContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,uno::Reference<text::XTextCursor> & xCursor)201 XMLHeaderFooterRegionContext::XMLHeaderFooterRegionContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
202                        const OUString& rLName,
203                        uno::Reference< text::XTextCursor >& xCursor ) :
204     SvXMLImportContext( rImport, nPrfx, rLName ),
205     xTextCursor ( xCursor )
206 {
207     xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
208     GetImport().GetTextImport()->SetCursor( xTextCursor );
209 }
210 
~XMLHeaderFooterRegionContext()211 XMLHeaderFooterRegionContext::~XMLHeaderFooterRegionContext()
212 {
213 }
214 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)215 SvXMLImportContextRef XMLHeaderFooterRegionContext::CreateChildContext(
216     sal_uInt16 nPrefix,
217     const OUString& rLocalName,
218     const uno::Reference< xml::sax::XAttributeList > & xAttrList )
219 {
220     SvXMLImportContext *pContext(nullptr);
221 
222     if ((nPrefix == XML_NAMESPACE_TEXT) &&
223         IsXMLToken(rLocalName, XML_P))
224     {
225         pContext =
226             GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
227                                                                     nPrefix,
228                                                                     rLocalName,
229                                                                     xAttrList);
230     }
231     if( !pContext )
232         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
233 
234     return pContext;
235 }
236 
EndElement()237 void XMLHeaderFooterRegionContext::EndElement()
238 {
239     if( GetImport().GetTextImport()->GetCursor().is() )
240     {
241         //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
242         if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
243         {
244             GetImport().GetTextImport()->GetText()->insertString(
245                 GetImport().GetTextImport()->GetCursorAsRange(), "",
246                 true );
247         }
248         GetImport().GetTextImport()->ResetCursor();
249     }
250     if (xOldTextCursor.is())
251         GetImport().GetTextImport()->SetCursor(xOldTextCursor);
252 }
253 
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
255