Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/ole/oleobjecthelper.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 <oox/ole/oleobjecthelper.hxx>
21
22
#include <com/sun/star/awt/Rectangle.hpp>
23
#include <com/sun/star/awt/Size.hpp>
24
#include <com/sun/star/beans/PropertyValue.hpp>
25
#include <com/sun/star/beans/XPropertySet.hpp>
26
#include <com/sun/star/container/XNameAccess.hpp>
27
#include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
28
#include <com/sun/star/embed/Aspects.hpp>
29
#include <com/sun/star/frame/XModel.hpp>
30
#include <com/sun/star/io/XOutputStream.hpp>
31
#include <com/sun/star/lang/XComponent.hpp>
32
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
33
#include <osl/diagnose.h>
34
#include <comphelper/propertyvalue.hxx>
35
#include <comphelper/sequenceashashmap.hxx>
36
#include <oox/helper/propertymap.hxx>
37
#include <oox/token/properties.hxx>
38
#include <utility>
39
40
namespace oox::ole {
41
42
using namespace ::com::sun::star;
43
using namespace ::com::sun::star::container;
44
using namespace ::com::sun::star::embed;
45
using namespace ::com::sun::star::io;
46
using namespace ::com::sun::star::lang;
47
using namespace ::com::sun::star::uno;
48
49
OleObjectInfo::OleObjectInfo() :
50
12.7M
    mbLinked( false ),
51
12.7M
    mbShowAsIcon( false ),
52
12.7M
    mbAutoUpdate( false ),
53
12.7M
    mbHasPicture( false )
54
12.7M
{
55
12.7M
}
56
57
const char g_aEmbeddedObjScheme[] = "vnd.sun.star.EmbeddedObject:";
58
59
OleObjectHelper::OleObjectHelper(
60
        const Reference< XMultiServiceFactory >& rxModelFactory,
61
        uno::Reference<frame::XModel> xModel)
62
22
    : m_xModel(std::move(xModel))
63
22
    , mnObjectId( 100 )
64
22
{
65
22
    assert(m_xModel.is());
66
22
    if( rxModelFactory.is() ) try
67
22
    {
68
22
        mxResolver.set( rxModelFactory->createInstance( u"com.sun.star.document.ImportEmbeddedObjectResolver"_ustr ), UNO_QUERY );
69
22
    }
70
22
    catch(const Exception& )
71
22
    {
72
0
    }
73
22
}
74
75
OleObjectHelper::~OleObjectHelper()
76
22
{
77
22
    try
78
22
    {
79
22
        Reference< XComponent > xResolverComp( mxResolver, UNO_QUERY_THROW );
80
22
        xResolverComp->dispose();
81
22
    }
82
22
    catch(const Exception& )
83
22
    {
84
0
    }
85
22
}
86
87
// TODO: this is probably a sub-optimal approach: ideally the media type
88
// of the stream from [Content_Types].xml should be stored somewhere for this
89
// purpose, but currently the media type of all OLE streams in the storage is
90
// just "application/vnd.sun.star.oleobject"
91
void SaveInteropProperties(uno::Reference<frame::XModel> const& xModel,
92
       OUString const& rObjectName, OUString const*const pOldObjectName,
93
       OUString const& rProgId)
94
138
{
95
138
    static constexpr OUString sEmbeddingsPropName = u"EmbeddedObjects"_ustr;
96
97
    // get interop grab bag from document
98
138
    uno::Reference<beans::XPropertySet> const xDocProps(xModel, uno::UNO_QUERY);
99
138
    comphelper::SequenceAsHashMap aGrabBag(xDocProps->getPropertyValue(u"InteropGrabBag"_ustr));
100
101
    // get EmbeddedObjects property inside grab bag
102
138
    comphelper::SequenceAsHashMap objectsList;
103
138
    auto grabIt = aGrabBag.find(sEmbeddingsPropName);
104
138
    if (grabIt != aGrabBag.end())
105
68
        objectsList << grabIt->second;
106
107
138
    uno::Sequence< beans::PropertyValue > aGrabBagAttribute{ comphelper::makePropertyValue(u"ProgID"_ustr,
108
138
                                                                                           rProgId) };
109
110
    // If we got an "old name", erase that first.
111
138
    if (pOldObjectName)
112
0
    {
113
0
        comphelper::SequenceAsHashMap::iterator it = objectsList.find(*pOldObjectName);
114
0
        if (it != objectsList.end())
115
0
            objectsList.erase(it);
116
0
    }
117
118
138
    objectsList[rObjectName] <<= aGrabBagAttribute;
119
120
    // put objects list back into the grab bag
121
138
    aGrabBag[sEmbeddingsPropName] <<= objectsList.getAsConstPropertyValueList();
122
123
    // put grab bag back into the document
124
138
    xDocProps->setPropertyValue(u"InteropGrabBag"_ustr, uno::Any(aGrabBag.getAsConstPropertyValueList()));
125
138
}
126
127
bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const awt::Size& rObjSize )
128
22
{
129
22
    bool bRet = false;
130
131
22
    if( rOleObject.mbLinked )
132
7
    {
133
        // linked OLE object - set target URL
134
7
        if( !rOleObject.maTargetLink.isEmpty() )
135
4
        {
136
4
            rPropMap.setProperty( PROP_LinkURL, rOleObject.maTargetLink);
137
4
            bRet = true;
138
4
        }
139
7
    }
140
15
    else
141
15
    {
142
        // embedded OLE object - import the embedded data
143
15
        if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try
144
12
        {
145
12
            OUString aObjectId = "Obj" + OUString::number( mnObjectId++ );
146
147
12
            Reference< XNameAccess > xResolverNA( mxResolver, UNO_QUERY_THROW );
148
12
            Reference< XOutputStream > xOutStrm( xResolverNA->getByName( aObjectId ), UNO_QUERY_THROW );
149
12
            xOutStrm->writeBytes( rOleObject.maEmbeddedData );
150
12
            xOutStrm->closeOutput();
151
152
12
            SaveInteropProperties(m_xModel, aObjectId, nullptr, rOleObject.maProgId);
153
154
12
            OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId );
155
12
            OSL_ENSURE( aUrl.match( g_aEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" );
156
12
            OUString aPersistName = aUrl.copy( strlen(g_aEmbeddedObjScheme) );
157
12
            if( !aPersistName.isEmpty() )
158
12
            {
159
12
                rPropMap.setProperty( PROP_PersistName, aPersistName);
160
12
                bRet = true;
161
12
            }
162
12
        }
163
12
        catch(const Exception& )
164
12
        {
165
0
        }
166
15
    }
167
168
22
    if( bRet )
169
16
    {
170
16
        rPropMap.setProperty( PROP_Aspect, (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT));
171
16
        rPropMap.setProperty( PROP_VisualArea, awt::Rectangle( 0, 0, rObjSize.Width, rObjSize.Height ));
172
16
    }
173
22
    return bRet;
174
22
}
175
176
} // namespace oox::ole
177
178
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */