1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "vbaheaderfooterhelper.hxx"
24 #include "wordvbahelper.hxx"
25 #include <comphelper/processfactory.hxx>
26 #include <com/sun/star/frame/XController.hpp>
27 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
28 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 
31 using namespace ::com::sun::star;
32 using namespace ::ooo::vba;
33 
34 #define FIRST_PAGE 1;
35 
36 // Class HeaderFooterHelper
37 
isHeader(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)38 sal_Bool HeaderFooterHelper::isHeader( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
39 {
40     uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
41     uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( xModel ), uno::UNO_QUERY_THROW );
42 
43     sal_Bool isOn = sal_False;
44     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isOn;
45     if( !isOn )
46         return sal_False;
47 
48     sal_Bool isShared = sal_False;
49     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsShared"))) >>= isShared;
50 
51     rtl::OUString aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderText") );
52     if( !isShared )
53     {
54         if( 0 == xPageCursor->getPage() % 2 )
55         {
56             aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderTextLeft") );
57         }
58         else
59         {
60             aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderTextRight") );
61         }
62     }
63 
64     uno::Reference< text::XText > xText( xStyleProps->getPropertyValue( aPropText ), uno::UNO_QUERY_THROW );
65     //FIXME: can not compare in this way?
66     return ( xText == xCurrentText );
67 }
68 
isFirstPageHeader(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)69 sal_Bool HeaderFooterHelper::isFirstPageHeader( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
70 {
71     if( isHeader( xModel, xCurrentText ) )
72     {
73         uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
74         // FIXME: getPage allways returns 1
75         sal_Int32 nPage = xPageCursor->getPage();
76         return nPage == FIRST_PAGE;
77     }
78     return sal_False;
79 }
80 
isEvenPagesHeader(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)81 sal_Bool HeaderFooterHelper::isEvenPagesHeader( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
82 {
83     if( isHeader( xModel, xCurrentText ) )
84     {
85         uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( xModel ), uno::UNO_QUERY_THROW );
86         sal_Bool isShared = sal_False;
87         xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsShared"))) >>= isShared;
88         if( !isShared )
89         {
90             uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
91             return ( 0 == xPageCursor->getPage() % 2 );
92         }
93     }
94     return sal_False;
95 }
96 
isFooter(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)97 sal_Bool HeaderFooterHelper::isFooter( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
98 {
99     uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
100     uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( xModel ), uno::UNO_QUERY_THROW );
101 
102     sal_Bool isOn = sal_False;
103     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn"))) >>= isOn;
104     if( !isOn )
105         return sal_False;
106 
107     sal_Bool isShared = sal_False;
108     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsShared"))) >>= isShared;
109 
110     rtl::OUString aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterText") );
111     if( !isShared )
112     {
113         if( 0 == xPageCursor->getPage() % 2 )
114         {
115             aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterTextLeft") );
116         }
117         else
118         {
119             aPropText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterTextRight") );
120         }
121     }
122 
123     uno::Reference< text::XText > xText( xStyleProps->getPropertyValue( aPropText ), uno::UNO_QUERY_THROW );
124 
125     return ( xText == xCurrentText );
126 }
127 
isFirstPageFooter(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)128 sal_Bool HeaderFooterHelper::isFirstPageFooter( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
129 {
130     if( isFooter( xModel, xCurrentText ) )
131     {
132         uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
133         sal_Int32 nPage = xPageCursor->getPage();
134         return nPage == FIRST_PAGE;
135     }
136     return sal_False;
137 }
138 
isEvenPagesFooter(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)139 sal_Bool HeaderFooterHelper::isEvenPagesFooter( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
140 {
141     if( isFooter( xModel, xCurrentText ) )
142     {
143         uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( xModel ), uno::UNO_QUERY_THROW );
144         sal_Bool isShared = sal_False;
145         xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsShared"))) >>= isShared;
146         if( !isShared )
147         {
148             uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
149             return ( 0 == xPageCursor->getPage() % 2 );
150         }
151     }
152     return sal_False;
153 }
154 #ifdef TOMORROW
isPrimaryHeader(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)155 sal_Bool HeaderFooterHelper::isPrimaryHeader( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
156 {
157     if( isHeader( xModel, xCurrentText ) )
158     {
159         return( !( isFirstPageHeader( xModel, xCurrentText ) && isEvenPagesHeader( xModel, xCurrentText ) ) );
160     }
161     return sal_False;
162 }
163 
isPrimaryFooter(const uno::Reference<frame::XModel> & xModel,const uno::Reference<text::XText> & xCurrentText)164 sal_Bool HeaderFooterHelper::isPrimaryFooter( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XText >& xCurrentText ) throw (uno::RuntimeException)
165 {
166     if( isHeader( xModel, xCurrentText ) )
167     {
168         return( !( isFirstPageFooter( xModel, xCurrentText ) && isEvenPagesFooter( xModel, xCurrentText ) ) );
169     }
170     return sal_False;
171 }
172 #endif
173