Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/text/XMLTextMasterPageContext.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 <sal/config.h>
21
22
#include <com/sun/star/frame/XModel.hpp>
23
#include <com/sun/star/style/XStyle.hpp>
24
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
25
#include <com/sun/star/beans/XMultiPropertyStates.hpp>
26
#include <com/sun/star/beans/XPropertySet.hpp>
27
#include <com/sun/star/container/XNameContainer.hpp>
28
#include <o3tl/any.hxx>
29
#include <sal/log.hxx>
30
#include <osl/diagnose.h>
31
#include <xmloff/xmlnamespace.hxx>
32
#include <xmloff/prstylei.hxx>
33
#include <xmloff/xmltoken.hxx>
34
#include <xmloff/XMLTextMasterPageContext.hxx>
35
#include <XMLTextHeaderFooterContext.hxx>
36
#include <PageMasterImportContext.hxx>
37
#include <xmloff/xmlimp.hxx>
38
39
40
using namespace ::com::sun::star;
41
using namespace ::com::sun::star::uno;
42
using namespace ::com::sun::star::xml::sax;
43
using namespace ::com::sun::star::style;
44
using namespace ::com::sun::star::text;
45
using namespace ::com::sun::star::beans;
46
using namespace ::com::sun::star::container;
47
using namespace ::com::sun::star::lang;
48
using namespace ::xmloff::token;
49
50
Reference < XStyle > XMLTextMasterPageContext::Create()
51
6.03k
{
52
6.03k
    Reference < XStyle > xNewStyle;
53
54
6.03k
    Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
55
6.03k
                                                    UNO_QUERY );
56
6.03k
    if( xFactory.is() )
57
6.03k
    {
58
6.03k
        Reference < XInterface > xIfc =
59
6.03k
            xFactory->createInstance(u"com.sun.star.style.PageStyle"_ustr);
60
6.03k
        if( xIfc.is() )
61
6.03k
            xNewStyle.set( xIfc, UNO_QUERY );
62
6.03k
    }
63
64
6.03k
    return xNewStyle;
65
6.03k
}
66
67
constexpr OUString gsFollowStyle( u"FollowStyle"_ustr );
68
69
XMLTextMasterPageContext::XMLTextMasterPageContext( SvXMLImport& rImport,
70
        sal_Int32 /*nElement*/,
71
        const Reference< XFastAttributeList > & xAttrList,
72
        bool bOverwrite )
73
46.3k
:   SvXMLStyleContext( rImport, XmlStyleFamily::MASTER_PAGE )
74
46.3k
,   m_bInsertHeader( false )
75
46.3k
,   m_bInsertFooter( false )
76
46.3k
,   m_bInsertHeaderLeft( false )
77
46.3k
,   m_bInsertFooterLeft( false )
78
46.3k
,   m_bInsertHeaderFirst( false )
79
46.3k
,   m_bInsertFooterFirst( false )
80
46.3k
,   m_bHeaderInserted( false )
81
46.3k
,   m_bFooterInserted( false )
82
46.3k
{
83
46.3k
    OUString sName, sDisplayName;
84
46.3k
    for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
85
87.7k
    {
86
87.7k
        const OUString aValue = aIter.toString();
87
87.7k
        switch (aIter.getToken())
88
87.7k
        {
89
41.9k
            case XML_ELEMENT(STYLE, XML_NAME):
90
41.9k
                sName = aValue;
91
41.9k
                break;
92
779
            case XML_ELEMENT(STYLE, XML_DISPLAY_NAME):
93
779
                sDisplayName = aValue;
94
779
                break;
95
412
            case XML_ELEMENT(STYLE, XML_NEXT_STYLE_NAME):
96
412
                m_sFollow = aValue;
97
412
                break;
98
32.2k
            case XML_ELEMENT(STYLE, XML_PAGE_LAYOUT_NAME):
99
32.2k
                m_sPageMasterName = aValue;
100
32.2k
                break;
101
12.2k
            case XML_ELEMENT(DRAW, XML_STYLE_NAME):
102
12.2k
                m_sDrawingPageStyle = aValue;
103
12.2k
                break;
104
162
            default:
105
162
                XMLOFF_WARN_UNKNOWN("xmloff", aIter);
106
87.7k
        }
107
87.7k
    }
108
109
46.3k
    if( !sDisplayName.isEmpty() )
110
779
    {
111
779
        rImport.AddStyleDisplayName( XmlStyleFamily::MASTER_PAGE, sName,
112
779
                                     sDisplayName );
113
779
    }
114
45.6k
    else
115
45.6k
    {
116
45.6k
        sDisplayName = sName;
117
45.6k
    }
118
119
46.3k
    if( sDisplayName.isEmpty() )
120
4.42k
        return;
121
122
41.9k
    Reference < XNameContainer > xPageStyles =
123
41.9k
            GetImport().GetTextImport()->GetPageStyles();
124
41.9k
    if( !xPageStyles.is() )
125
0
        return;
126
127
41.9k
    Any aAny;
128
41.9k
    bool bNew = false;
129
41.9k
    if( xPageStyles->hasByName( sDisplayName ) )
130
35.9k
    {
131
35.9k
        aAny = xPageStyles->getByName( sDisplayName );
132
35.9k
        aAny >>= m_xStyle;
133
35.9k
    }
134
6.03k
    else
135
6.03k
    {
136
6.03k
        m_xStyle = Create();
137
6.03k
        if( !m_xStyle.is() )
138
0
            return;
139
140
6.03k
        xPageStyles->insertByName( sDisplayName, Any(m_xStyle) );
141
6.03k
        bNew = true;
142
6.03k
    }
143
144
41.9k
    Reference < XPropertySet > xPropSet( m_xStyle, UNO_QUERY );
145
41.9k
    Reference< XPropertySetInfo > xPropSetInfo =
146
41.9k
                xPropSet->getPropertySetInfo();
147
41.9k
    OUString sIsPhysical( u"IsPhysical"_ustr );
148
41.9k
    if( !bNew && xPropSetInfo->hasPropertyByName( sIsPhysical ) )
149
16.3k
    {
150
16.3k
        aAny = xPropSet->getPropertyValue( sIsPhysical );
151
16.3k
        bNew = !*o3tl::doAccess<bool>(aAny);
152
16.3k
    }
153
41.9k
    SetNew( bNew );
154
155
41.9k
    if( !(bOverwrite || bNew) )
156
0
        return;
157
158
41.9k
    Reference < XMultiPropertyStates > xMultiStates( xPropSet,
159
41.9k
                                                     UNO_QUERY );
160
41.9k
    OSL_ENSURE( xMultiStates.is(),
161
41.9k
                "text page style does not support multi property set" );
162
41.9k
    if( xMultiStates.is() )
163
41.9k
        xMultiStates->setAllPropertiesToDefault();
164
165
41.9k
    if ( xPropSetInfo->hasPropertyByName( u"GridDisplay"_ustr ) )
166
18.7k
        xPropSet->setPropertyValue( u"GridDisplay"_ustr, Any(false) );
167
168
41.9k
    if ( xPropSetInfo->hasPropertyByName( u"GridPrint"_ustr ) )
169
18.7k
        xPropSet->setPropertyValue( u"GridPrint"_ustr, Any(false) );
170
171
41.9k
    m_bInsertHeader = m_bInsertFooter = true;
172
41.9k
    m_bInsertHeaderLeft = m_bInsertFooterLeft = true;
173
41.9k
    m_bInsertHeaderFirst = m_bInsertFooterFirst = true;
174
41.9k
}
175
176
XMLTextMasterPageContext::~XMLTextMasterPageContext()
177
46.3k
{
178
46.3k
}
179
180
css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextMasterPageContext::createFastChildContext(
181
    sal_Int32 nElement,
182
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
183
64.8k
{
184
64.8k
    SvXMLImportContextRef xContext;
185
186
64.8k
    bool bInsert = false, bFooter = false, bLeft = false, bFirst = false;
187
64.8k
    switch( nElement )
188
64.8k
    {
189
11.2k
    case XML_ELEMENT(STYLE, XML_HEADER):
190
11.2k
        if( m_bInsertHeader && !m_bHeaderInserted )
191
9.63k
        {
192
9.63k
            bInsert = true;
193
9.63k
            m_bHeaderInserted = true;
194
9.63k
        }
195
11.2k
        break;
196
7.68k
    case XML_ELEMENT(STYLE, XML_FOOTER):
197
7.68k
        if( m_bInsertFooter && !m_bFooterInserted )
198
5.65k
        {
199
5.65k
            bInsert = bFooter = true;
200
5.65k
            m_bFooterInserted = true;
201
5.65k
        }
202
7.68k
        break;
203
22.7k
    case XML_ELEMENT(STYLE, XML_HEADER_LEFT):
204
22.7k
        if( m_bInsertHeaderLeft && m_bHeaderInserted )
205
21.3k
            bInsert = bLeft = true;
206
22.7k
        break;
207
22.3k
    case XML_ELEMENT(STYLE, XML_FOOTER_LEFT):
208
22.3k
        if( m_bInsertFooterLeft && m_bFooterInserted )
209
21.0k
            bInsert = bFooter = bLeft = true;
210
22.3k
        break;
211
0
    case XML_ELEMENT(LO_EXT, XML_HEADER_FIRST):
212
2
    case XML_ELEMENT(STYLE, XML_HEADER_FIRST):
213
2
        if( m_bInsertHeaderFirst && m_bHeaderInserted )
214
1
            bInsert = bFirst = true;
215
2
        break;
216
0
    case XML_ELEMENT(LO_EXT, XML_FOOTER_FIRST):
217
0
    case XML_ELEMENT(STYLE, XML_FOOTER_FIRST):
218
0
        if( m_bInsertFooterFirst && m_bFooterInserted )
219
0
            bInsert = bFooter = bFirst = true;
220
0
        break;
221
64.8k
    }
222
223
64.8k
    if( bInsert && m_xStyle.is() )
224
57.6k
    {
225
57.6k
        xContext = CreateHeaderFooterContext( nElement, xAttrList,
226
57.6k
                                              bFooter, bLeft, bFirst );
227
57.6k
    }
228
229
64.8k
    return xContext;
230
64.8k
}
231
232
SvXMLImportContext *XMLTextMasterPageContext::CreateHeaderFooterContext(
233
            sal_Int32 /*nElement*/,
234
            const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/,
235
            const bool bFooter,
236
            const bool bLeft,
237
            const bool bFirst )
238
4.64k
{
239
4.64k
    Reference < XPropertySet > xPropSet( m_xStyle, UNO_QUERY );
240
4.64k
    return new XMLTextHeaderFooterContext( GetImport(), xPropSet, bFooter, bLeft, bFirst );
241
4.64k
}
242
243
void XMLTextMasterPageContext::Finish( bool bOverwrite )
244
31.6k
{
245
31.6k
    if( !(m_xStyle.is() && (IsNew() || bOverwrite)) )
246
2.75k
        return;
247
248
28.9k
    Reference < XPropertySet > xPropSet( m_xStyle, UNO_QUERY );
249
28.9k
    XMLPropStyleContext * pDrawingPageStyle(nullptr);
250
28.9k
    if (!m_sDrawingPageStyle.isEmpty())
251
7.80k
    {
252
7.80k
        pDrawingPageStyle = GetImport().GetTextImport()->FindDrawingPage(m_sDrawingPageStyle);
253
7.80k
    }
254
28.9k
    PageStyleContext * pPageLayout(nullptr);
255
28.9k
    if( !m_sPageMasterName.isEmpty() )
256
21.6k
    {
257
21.6k
        pPageLayout = static_cast<PageStyleContext *>(GetImport().GetTextImport()->FindPageMaster(m_sPageMasterName));
258
21.6k
    }
259
28.9k
    if (pPageLayout)
260
15.1k
    {
261
15.1k
        pPageLayout->FillPropertySet_PageStyle(xPropSet, pDrawingPageStyle);
262
15.1k
    }
263
13.7k
    else if (pDrawingPageStyle)
264
1.04k
    {
265
        // don't need to care about old background attributes in this case
266
1.04k
        pDrawingPageStyle->FillPropertySet(xPropSet);
267
1.04k
    }
268
269
28.9k
    Reference < XNameContainer > xPageStyles =
270
28.9k
        GetImport().GetTextImport()->GetPageStyles();
271
28.9k
    if( !xPageStyles.is() )
272
0
        return;
273
274
28.9k
    Reference< XPropertySetInfo > xPropSetInfo =
275
28.9k
        xPropSet->getPropertySetInfo();
276
28.9k
    if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
277
12.4k
    {
278
12.4k
        OUString sDisplayFollow(
279
12.4k
            GetImport().GetStyleDisplayName(
280
12.4k
                    XmlStyleFamily::MASTER_PAGE, m_sFollow ) );
281
12.4k
        if( sDisplayFollow.isEmpty() ||
282
335
            !xPageStyles->hasByName( sDisplayFollow ) )
283
12.1k
            sDisplayFollow = m_xStyle->getName();
284
285
12.4k
        Any aAny = xPropSet->getPropertyValue( gsFollowStyle );
286
12.4k
        OUString sCurrFollow;
287
12.4k
        aAny >>= sCurrFollow;
288
12.4k
        if( sCurrFollow != sDisplayFollow )
289
235
        {
290
235
            xPropSet->setPropertyValue( gsFollowStyle, Any(sDisplayFollow) );
291
235
        }
292
12.4k
    }
293
294
28.9k
    if ( xPropSetInfo->hasPropertyByName( u"Hidden"_ustr ) )
295
28.9k
    {
296
28.9k
        xPropSet->setPropertyValue( u"Hidden"_ustr, uno::Any( IsHidden( ) ) );
297
28.9k
    }
298
28.9k
}
299
300
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */