Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/text/XMLTextHeaderFooterContext.cxx
Line
Count
Source
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/text/XText.hpp>
21
#include <com/sun/star/text/XParagraphAppend.hpp>
22
#include <com/sun/star/beans/XPropertySet.hpp>
23
#include <o3tl/any.hxx>
24
#include <XMLTextHeaderFooterContext.hxx>
25
#include <xmloff/xmlimp.hxx>
26
27
28
using namespace ::com::sun::star;
29
using namespace ::com::sun::star::uno;
30
using namespace ::com::sun::star::xml::sax;
31
using namespace ::com::sun::star::text;
32
using namespace ::com::sun::star::beans;
33
34
35
XMLTextHeaderFooterContext::XMLTextHeaderFooterContext( SvXMLImport& rImport,
36
                        const Reference < XPropertySet > & rPageStylePropSet,
37
                       bool bFooter, bool bLft, bool bFrst ) :
38
5.01k
    SvXMLImportContext( rImport ),
39
5.01k
    xPropSet( rPageStylePropSet ),
40
5.01k
    sOn( bFooter ? u"FooterIsOn"_ustr : u"HeaderIsOn"_ustr ),
41
5.01k
    sShareContent( bFooter ? u"FooterIsShared"_ustr : u"HeaderIsShared"_ustr ),
42
5.01k
    sText( bFooter ? u"FooterText"_ustr : u"HeaderText"_ustr ),
43
5.01k
    sTextFirst(bFooter ? u"FooterTextFirst"_ustr : u"HeaderTextFirst"_ustr),
44
5.01k
    sTextLeft( bFooter ?  u"FooterTextLeft"_ustr : u"HeaderTextLeft"_ustr ),
45
5.01k
    bInsertContent( true ),
46
5.01k
    bLeft( bLft ),
47
5.01k
    bFirst( bFrst )
48
5.01k
{
49
    // NOTE: if this ever handles XML_DISPLAY attr then beware of fdo#72850 !
50
5.01k
    if( !(bLeft || bFirst) )
51
4.25k
        return;
52
53
755
    Any aAny = xPropSet->getPropertyValue( sOn );
54
755
    bool bOn = *o3tl::doAccess<bool>(aAny);
55
56
755
    if( bOn )
57
474
    {
58
474
        if (bLeft)
59
474
        {
60
474
            aAny = xPropSet->getPropertyValue( sShareContent );
61
474
            bool bShared = bool();
62
474
            if (!(aAny >>= bShared))
63
474
                assert(false); // should return a value!
64
474
            if( bShared )
65
474
            {
66
                // Don't share headers any longer
67
474
                xPropSet->setPropertyValue( sShareContent, Any(false) );
68
474
            }
69
474
        }
70
474
        if (bFirst)
71
0
        {
72
0
            static constexpr OUString sShareContentFirst( u"FirstIsShared"_ustr );
73
0
            aAny = xPropSet->getPropertyValue( sShareContentFirst );
74
0
            bool bSharedFirst = bool();
75
0
            if (!(aAny >>= bSharedFirst))
76
0
                assert(false); // should return a value!
77
0
            if( bSharedFirst )
78
0
            {
79
                // Don't share first/right headers any longer
80
0
                xPropSet->setPropertyValue( sShareContentFirst, Any(false) );
81
0
            }
82
0
        }
83
474
    }
84
281
    else
85
281
    {
86
        // If headers or footers are switched off, no content must be
87
        // inserted.
88
281
        bInsertContent = false;
89
281
    }
90
755
}
91
92
XMLTextHeaderFooterContext::~XMLTextHeaderFooterContext()
93
5.01k
{
94
5.01k
}
95
96
css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextHeaderFooterContext::createFastChildContext(
97
    sal_Int32 nElement,
98
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
99
42.2k
{
100
42.2k
    SvXMLImportContext *pContext = nullptr;
101
42.2k
    if( bInsertContent )
102
42.2k
    {
103
42.2k
        if( !xOldTextCursor.is() )
104
4.12k
        {
105
4.12k
            bool bRemoveContent = true;
106
4.12k
            Any aAny;
107
4.12k
            if( bLeft || bFirst )
108
173
            {
109
                // Headers and footers are switched on already,
110
                // and they aren't shared.
111
173
                if (bLeft)
112
173
                    aAny = xPropSet->getPropertyValue( sTextLeft );
113
0
                else
114
0
                    aAny = xPropSet->getPropertyValue( sTextFirst );
115
173
            }
116
3.95k
            else
117
3.95k
            {
118
3.95k
                aAny = xPropSet->getPropertyValue( sOn );
119
3.95k
                bool bOn = *o3tl::doAccess<bool>(aAny);
120
121
3.95k
                if( !bOn )
122
3.95k
                {
123
                    // Switch header on
124
3.95k
                    xPropSet->setPropertyValue( sOn, Any(true) );
125
126
                    // The content has not to be removed, because the header
127
                    // or footer is empty already.
128
3.95k
                    bRemoveContent = false;
129
3.95k
                }
130
131
                // If a header or footer is not shared, share it now.
132
3.95k
                aAny = xPropSet->getPropertyValue( sShareContent );
133
3.95k
                bool bShared = *o3tl::doAccess<bool>(aAny);
134
3.95k
                if( !bShared )
135
0
                {
136
0
                    xPropSet->setPropertyValue( sShareContent, Any(true) );
137
0
                }
138
139
3.95k
                aAny = xPropSet->getPropertyValue( sText );
140
3.95k
            }
141
142
4.12k
            Reference < XText > xText;
143
4.12k
            aAny >>= xText;
144
145
4.12k
            if( bRemoveContent )
146
173
            {
147
173
                xText->setString(OUString());
148
                // fdo#82165 shapes anchored at the beginning or end survive
149
                // setString("") - kill them the hard way: SwDoc::DelFullPara()
150
173
                uno::Reference<text::XParagraphAppend> const xAppend(
151
173
                        xText, uno::UNO_QUERY_THROW);
152
173
                uno::Reference<lang::XComponent> const xPara(
153
173
                    xAppend->finishParagraph(
154
173
                        uno::Sequence<beans::PropertyValue>()),
155
173
                    uno::UNO_QUERY_THROW);
156
173
                xPara->dispose();
157
173
            }
158
159
4.12k
            rtl::Reference < XMLTextImportHelper > xTxtImport =
160
4.12k
                GetImport().GetTextImport();
161
162
4.12k
            xOldTextCursor = xTxtImport->GetCursor();
163
4.12k
            xTxtImport->SetCursor( xText->createTextCursor() );
164
4.12k
        }
165
166
42.2k
        pContext =
167
42.2k
            GetImport().GetTextImport()->CreateTextChildContext(
168
42.2k
                GetImport(), nElement, xAttrList,
169
42.2k
                XMLTextType::HeaderFooter );
170
42.2k
    }
171
172
42.2k
    return pContext;
173
42.2k
}
174
175
void XMLTextHeaderFooterContext::endFastElement(sal_Int32 )
176
2.07k
{
177
2.07k
    if( xOldTextCursor.is() )
178
1.20k
    {
179
1.20k
        GetImport().GetTextImport()->DeleteParagraph();
180
1.20k
        GetImport().GetTextImport()->SetCursor( xOldTextCursor );
181
1.20k
    }
182
864
    else if( !bLeft )
183
289
    {
184
        // If no content has been inserted into the header or footer,
185
        // switch it off.
186
289
        xPropSet->setPropertyValue( sOn, Any(false) );
187
289
    }
188
2.07k
}
189
190
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */