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