Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/embeddedobj/source/commonembedding/visobj.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/embed/Aspects.hpp>
21
#include <com/sun/star/embed/EmbedStates.hpp>
22
#include <com/sun/star/embed/WrongStateException.hpp>
23
#include <com/sun/star/datatransfer/XTransferable.hpp>
24
#include <com/sun/star/uno/Sequence.hxx>
25
#include <com/sun/star/lang/DisposedException.hpp>
26
27
28
#include <commonembobj.hxx>
29
#include <sal/log.hxx>
30
31
32
using namespace ::com::sun::star;
33
34
void SAL_CALL OCommonEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
35
0
{
36
0
    ::osl::MutexGuard aGuard( m_aMutex );
37
0
    if ( m_bDisposed )
38
0
        throw lang::DisposedException(); // TODO
39
40
0
    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
41
0
    if ( nAspect == embed::Aspects::MSOLE_ICON )
42
        // no representation can be retrieved
43
0
        throw embed::WrongStateException( u"Illegal call!"_ustr,
44
0
                                    static_cast< ::cppu::OWeakObject* >(this) );
45
46
0
    if ( m_nObjectState == -1 )
47
0
        throw embed::WrongStateException( u"The own object has no persistence!"_ustr,
48
0
                                    static_cast< ::cppu::OWeakObject* >(this) );
49
50
0
    m_bHasClonedSize = false;
51
52
0
    bool bBackToLoaded = false;
53
0
    if ( m_nObjectState == embed::EmbedStates::LOADED )
54
0
    {
55
0
        changeState( embed::EmbedStates::RUNNING );
56
57
        // the links should be switched back to loaded state for now to avoid locking problems
58
0
        bBackToLoaded = m_bIsLinkURL;
59
0
    }
60
61
0
    bool bSuccess = m_xDocHolder->SetExtent( nAspect, aSize );
62
63
0
    if ( bBackToLoaded )
64
0
        changeState( embed::EmbedStates::LOADED );
65
66
0
    if ( !bSuccess )
67
0
        throw uno::Exception(u"SetExtent failed"_ustr, nullptr); // TODO:
68
0
}
69
70
awt::Size SAL_CALL OCommonEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
71
0
{
72
0
    ::osl::MutexGuard aGuard( m_aMutex );
73
0
    if ( m_bDisposed )
74
0
        throw lang::DisposedException(); // TODO
75
76
0
    if ( m_nObjectState == -1 )
77
0
        throw embed::WrongStateException( u"The own object has no persistence!"_ustr,
78
0
                                    static_cast< ::cppu::OWeakObject* >(this) );
79
80
0
    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
81
82
0
    if ( m_bHasClonedSize )
83
0
        return m_aClonedSize;
84
85
0
    bool bBackToLoaded = false;
86
0
    if ( m_nObjectState == embed::EmbedStates::LOADED )
87
0
    {
88
0
        changeState( embed::EmbedStates::RUNNING );
89
90
        // the links should be switched back to loaded state for now to avoid locking problems
91
0
        bBackToLoaded = m_bIsLinkURL;
92
0
    }
93
94
0
    awt::Size aResult;
95
0
    bool bSuccess = m_xDocHolder->GetExtent( nAspect, &aResult );
96
97
0
    if ( bBackToLoaded )
98
0
        changeState( embed::EmbedStates::LOADED );
99
100
0
    if ( !bSuccess )
101
0
        throw uno::Exception(u"GetExtent failed"_ustr, nullptr); // TODO:
102
103
0
    return aResult;
104
0
}
105
106
sal_Int32 SAL_CALL OCommonEmbeddedObject::getMapUnit( sal_Int64 nAspect )
107
0
{
108
0
    ::osl::MutexGuard aGuard( m_aMutex );
109
0
    if ( m_bDisposed )
110
0
        throw lang::DisposedException(); // TODO
111
112
0
    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
113
0
    if ( nAspect == embed::Aspects::MSOLE_ICON )
114
        // no representation can be retrieved
115
0
        throw embed::WrongStateException( u"Illegal call!"_ustr,
116
0
                                    static_cast< ::cppu::OWeakObject* >(this) );
117
118
0
    if ( m_nObjectState == -1 )
119
0
        throw embed::WrongStateException( u"The own object has no persistence!"_ustr,
120
0
                                    static_cast< ::cppu::OWeakObject* >(this) );
121
122
0
    if ( m_bHasClonedSize )
123
0
        return m_nClonedMapUnit;
124
125
0
    bool bBackToLoaded = false;
126
0
    if ( m_nObjectState == embed::EmbedStates::LOADED )
127
0
    {
128
0
        changeState( embed::EmbedStates::RUNNING );
129
130
        // the links should be switched back to loaded state for now to avoid locking problems
131
0
        bBackToLoaded = m_bIsLinkURL;
132
0
    }
133
134
0
    sal_Int32 nResult = m_xDocHolder->GetMapUnit( nAspect );
135
136
0
    if ( bBackToLoaded )
137
0
        changeState( embed::EmbedStates::LOADED );
138
139
0
    if ( nResult < 0  )
140
0
        throw uno::Exception("result " + OUString::number(nResult), nullptr); // TODO:
141
142
0
    return nResult;
143
0
}
144
145
embed::VisualRepresentation SAL_CALL OCommonEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
146
0
{
147
0
    ::osl::MutexGuard aGuard( m_aMutex );
148
0
    if ( m_bDisposed )
149
0
        throw lang::DisposedException(); // TODO
150
151
0
    if ( m_nObjectState == -1 )
152
0
        throw embed::WrongStateException( u"The own object has no persistence!"_ustr,
153
0
                                    static_cast< ::cppu::OWeakObject* >(this) );
154
155
156
0
    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
157
0
    if ( nAspect == embed::Aspects::MSOLE_ICON )
158
        // no representation can be retrieved
159
0
        throw embed::WrongStateException( u"Illegal call!"_ustr,
160
0
                                    static_cast< ::cppu::OWeakObject* >(this) );
161
162
0
    bool bBackToLoaded = false;
163
0
    if ( m_nObjectState == embed::EmbedStates::LOADED )
164
0
    {
165
        // restore original VisualAreaSize, because writer objects set
166
        // themselves to a default size OLESIZE
167
0
        awt::Size aOrigSize = getVisualAreaSize(nAspect);
168
0
        changeState(embed::EmbedStates::RUNNING);
169
0
        const bool bIsChart = GetDocumentServiceName() == "com.sun.star.chart2.ChartDocument";
170
        // tdf#108643 unless it's a chart, cause those are weird (#i103460#)
171
0
        if (!bIsChart && aOrigSize != getVisualAreaSize(nAspect))
172
0
            setVisualAreaSize(nAspect, aOrigSize);
173
174
        // the links should be switched back to loaded state for now to avoid locking problems
175
0
        bBackToLoaded = m_bIsLinkURL;
176
0
    }
177
178
0
    SAL_WARN_IF( !m_xDocHolder->GetComponent().is(), "embeddedobj.common", "Running or Active object has no component!" );
179
180
    // TODO: return for the aspect of the document
181
0
    embed::VisualRepresentation aVisualRepresentation;
182
183
0
    uno::Reference< embed::XVisualObject > xVisualObject( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
184
0
    if( xVisualObject.is())
185
0
    {
186
0
        aVisualRepresentation = xVisualObject->getPreferredVisualRepresentation( nAspect );
187
0
    }
188
0
    else
189
0
    {
190
0
        uno::Reference< datatransfer::XTransferable > xTransferable( m_xDocHolder->GetComponent(), uno::UNO_QUERY_THROW );
191
192
0
        datatransfer::DataFlavor aDataFlavor(
193
0
                u"application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\""_ustr,
194
0
                u"GDIMetaFile"_ustr,
195
0
                cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
196
197
0
        if( !xTransferable->isDataFlavorSupported( aDataFlavor ))
198
0
            throw uno::RuntimeException();
199
0
        aVisualRepresentation.Data = xTransferable->getTransferData( aDataFlavor );
200
0
        aVisualRepresentation.Flavor = std::move(aDataFlavor);
201
0
    }
202
203
0
    if ( bBackToLoaded )
204
0
        changeState( embed::EmbedStates::LOADED );
205
206
0
    return aVisualRepresentation;
207
0
}
208
209
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */