Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/draw/layerimp.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 <sal/log.hxx>
23
#include <comphelper/diagnose_ex.hxx>
24
#include <com/sun/star/drawing/XLayerManager.hpp>
25
#include <com/sun/star/beans/XPropertySet.hpp>
26
#include <com/sun/star/frame/XModel.hpp>
27
#include <com/sun/star/drawing/XLayerSupplier.hpp>
28
#include <xmloff/xmltoken.hxx>
29
#include <xmloff/xmlimp.hxx>
30
#include <xmloff/xmlnamespace.hxx>
31
#include "layerimp.hxx"
32
33
34
#include <XMLStringBufferImportContext.hxx>
35
36
using namespace ::xmloff::token;
37
using namespace ::com::sun::star;
38
using namespace ::com::sun::star::xml;
39
using namespace ::com::sun::star::xml::sax;
40
using namespace ::com::sun::star::uno;
41
using namespace ::com::sun::star::drawing;
42
using namespace ::com::sun::star::beans;
43
using namespace ::com::sun::star::container;
44
45
namespace {
46
47
class SdXMLLayerContext : public SvXMLImportContext
48
{
49
public:
50
    SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
51
52
    virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
53
        sal_Int32 nElement,
54
        const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
55
    virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
56
57
private:
58
    css::uno::Reference< css::container::XNameAccess > mxLayerManager;
59
    OUString msName;
60
    OUStringBuffer sDescriptionBuffer;
61
    OUStringBuffer sTitleBuffer;
62
    OUString msDisplay;
63
    OUString msProtected;
64
};
65
66
}
67
68
SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
69
2.13k
: SvXMLImportContext(rImport)
70
2.13k
, mxLayerManager( xLayerManager )
71
2.13k
{
72
2.13k
    for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
73
2.14k
    {
74
2.14k
        OUString sValue = aIter.toString();
75
2.14k
        switch(aIter.getToken())
76
2.14k
        {
77
2.11k
            case XML_ELEMENT(DRAW, XML_NAME):
78
2.11k
                msName = sValue;
79
2.11k
                break;
80
0
            case XML_ELEMENT(DRAW, XML_DISPLAY):
81
0
                msDisplay = sValue;
82
0
                break;
83
0
            case XML_ELEMENT(DRAW, XML_PROTECTED):
84
0
                msProtected = sValue;
85
0
                break;
86
29
            default:
87
29
                XMLOFF_WARN_UNKNOWN("xmloff", aIter);
88
2.14k
        }
89
2.14k
    }
90
91
2.13k
}
92
93
css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerContext::createFastChildContext(
94
    sal_Int32 nElement,
95
    const css::uno::Reference< css::xml::sax::XFastAttributeList >&  )
96
39
{
97
39
    if( nElement == XML_ELEMENT(SVG, XML_TITLE) || nElement == XML_ELEMENT(SVG_COMPAT, XML_TITLE) )
98
0
    {
99
0
        return new XMLStringBufferImportContext( GetImport(), sTitleBuffer);
100
0
    }
101
39
    else if( nElement == XML_ELEMENT(SVG, XML_DESC) || nElement == XML_ELEMENT(SVG_COMPAT, XML_DESC) )
102
0
    {
103
0
        return new XMLStringBufferImportContext( GetImport(), sDescriptionBuffer);
104
0
    }
105
39
    else
106
39
        XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
107
39
    return nullptr;
108
39
}
109
110
void SdXMLLayerContext::endFastElement(sal_Int32 )
111
2.12k
{
112
2.12k
    SAL_WARN_IF( msName.isEmpty(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
113
2.12k
    if( msName.isEmpty() )
114
16
        return;
115
116
2.10k
    try
117
2.10k
    {
118
2.10k
        Reference< XPropertySet > xLayer;
119
120
2.10k
        if( mxLayerManager->hasByName( msName ) )
121
1.98k
        {
122
1.98k
            mxLayerManager->getByName( msName ) >>= xLayer;
123
1.98k
            SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
124
1.98k
        }
125
125
        else
126
125
        {
127
125
            Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY );
128
125
            if( xLayerManager.is() )
129
125
                xLayer = xLayerManager->insertNewByIndex( xLayerManager->getCount() );
130
125
            SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
131
132
125
            if( xLayer.is() )
133
125
                xLayer->setPropertyValue(u"Name"_ustr, Any( msName ) );
134
125
        }
135
136
2.10k
        if( xLayer.is() )
137
2.10k
        {
138
2.10k
            xLayer->setPropertyValue(u"Title"_ustr, Any( sTitleBuffer.makeStringAndClear() ) );
139
2.10k
            xLayer->setPropertyValue(u"Description"_ustr, Any( sDescriptionBuffer.makeStringAndClear() ) );
140
2.10k
            bool bIsVisible( true );
141
2.10k
            bool bIsPrintable( true );
142
2.10k
            if ( !msDisplay.isEmpty() )
143
0
            {
144
0
                bIsVisible = (msDisplay == "always") || (msDisplay == "screen");
145
0
                bIsPrintable = (msDisplay == "always") || (msDisplay == "printer");
146
0
            }
147
2.10k
            xLayer->setPropertyValue(u"IsVisible"_ustr, Any( bIsVisible ) );
148
2.10k
            xLayer->setPropertyValue(u"IsPrintable"_ustr, Any( bIsPrintable ) );
149
2.10k
            bool bIsLocked( false );
150
2.10k
            if ( !msProtected.isEmpty() )
151
0
                bIsLocked = (msProtected == "true");
152
2.10k
            xLayer->setPropertyValue(u"IsLocked"_ustr, Any( bIsLocked ) );
153
154
            // tdf#129898 repair layer "DrawnInSlideshow", which was wrongly written
155
            // in LO 6.2 to 6.4. It should always have ODF defaults.
156
2.10k
            if (msName == "DrawnInSlideshow")
157
0
            {
158
0
                xLayer->setPropertyValue(u"IsVisible"_ustr, Any(true));
159
0
                xLayer->setPropertyValue(u"IsPrintable"_ustr, Any(true));
160
0
                xLayer->setPropertyValue(u"IsLocked"_ustr, Any(false));
161
0
            }
162
2.10k
        }
163
2.10k
    }
164
2.10k
    catch( Exception& )
165
2.10k
    {
166
0
        TOOLS_WARN_EXCEPTION("xmloff.draw", "");
167
0
    }
168
2.10k
}
169
170
171
SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport )
172
441
: SvXMLImportContext(rImport)
173
441
{
174
441
    Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
175
441
    SAL_WARN_IF( !xLayerSupplier.is(), "xmloff", "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
176
441
    if( xLayerSupplier.is() )
177
441
        mxLayerManager = xLayerSupplier->getLayerManager();
178
441
}
179
180
SdXMLLayerSetContext::~SdXMLLayerSetContext()
181
441
{
182
441
}
183
184
css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerSetContext::createFastChildContext(
185
    sal_Int32 /*nElement*/,
186
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
187
2.13k
{
188
2.13k
    return new SdXMLLayerContext( GetImport(), xAttrList, mxLayerManager );
189
2.13k
}
190
191
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */