1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <com/sun/star/uno/Sequence.hxx>
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/embed/XEmbeddedObject.hpp>
25 #include <com/sun/star/embed/XEmbeddedOleObject.hpp>
26 #include <com/sun/star/embed/XInplaceObject.hpp>
27 #include <com/sun/star/embed/XEmbedPersist.hpp>
28 #include <com/sun/star/embed/XLinkageSupport.hpp>
29 #include <com/sun/star/embed/VerbDescriptor.hpp>
30 #include <com/sun/star/container/XChild.hpp>
31 #include <com/sun/star/util/XCloseable.hpp>
32 #include <com/sun/star/util/XCloseListener.hpp>
33 #include <com/sun/star/io/XActiveDataStreamer.hpp>
34 #include <com/sun/star/lang/XInitialization.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
37 #include <cppuhelper/implbase.hxx>
38 #include <rtl/ref.hxx>
40 #include <osl/thread.h>
43 namespace comphelper
{
44 class OMultiTypeInterfaceContainerHelper2
;
47 class VerbExecutionController
49 // the following mutex is allowed to be locked only for variables initialization, so no deadlock can be caused
50 ::osl::Mutex m_aVerbExecutionMutex
;
52 sal_Int32 m_nNotificationLock
;
55 bool m_bWasEverActive
;
56 bool m_bVerbExecutionInProgress
;
57 oslThreadIdentifier m_nVerbExecutionThreadIdentifier
;
58 bool m_bChangedOnVerbExecution
;
63 VerbExecutionController()
64 : m_nNotificationLock( 0 )
66 , m_bWasEverActive( false )
67 , m_bVerbExecutionInProgress( false )
68 , m_nVerbExecutionThreadIdentifier( 0 )
69 , m_bChangedOnVerbExecution( false )
74 void StartControlExecution();
75 bool EndControlExecution_WasModified();
76 void ModificationNotificationIsDone();
77 // no need to lock anything to check the value of the numeric members
78 bool CanDoNotification() { return ( !m_bVerbExecutionInProgress
&& !m_bWasEverActive
&& !m_nNotificationLock
); }
79 // ... or to change it
80 void ObjectIsActive() { m_bWasEverActive
= true; }
82 void LockNotification();
83 void UnlockNotification();
86 class VerbExecutionControllerGuard
88 VerbExecutionController
& m_rController
;
91 VerbExecutionControllerGuard( VerbExecutionController
& rController
)
92 : m_rController( rController
)
94 m_rController
.LockNotification();
97 ~VerbExecutionControllerGuard()
99 m_rController
.UnlockNotification();
107 * Represents an OLE object that has native data and we try to let an external
108 * application handle that data.
110 class OleEmbeddedObject
: public ::cppu::WeakImplHelper
111 < css::embed::XEmbeddedObject
112 , css::embed::XEmbeddedOleObject
113 , css::embed::XEmbedPersist
114 , css::embed::XLinkageSupport
115 , css::embed::XInplaceObject
116 , css::container::XChild
117 , css::io::XActiveDataStreamer
118 , css::lang::XInitialization
119 , css::lang::XServiceInfo
>
121 friend class OleComponent
;
123 ::osl::Mutex m_aMutex
;
125 rtl::Reference
<OleComponent
> m_pOleComponent
;
127 std::unique_ptr
<::comphelper::OMultiTypeInterfaceContainerHelper2
> m_pInterfaceContainer
;
132 sal_Int32 m_nObjectState
;
133 sal_Int32 m_nTargetState
;
134 sal_Int32 m_nUpdateMode
;
136 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
138 css::uno::Sequence
< sal_Int8
> m_aClassID
;
139 OUString m_aClassName
;
141 css::uno::Reference
< css::embed::XEmbeddedClient
> m_xClientSite
;
143 OUString m_aContainerName
;
145 css::uno::Reference
< css::util::XCloseListener
> m_xClosePreventer
;
147 bool m_bWaitSaveCompleted
;
148 bool m_bNewVisReplInStream
;
149 css::uno::Reference
< css::io::XStream
> m_xNewCachedVisRepl
;
150 OUString m_aNewEntryName
;
151 css::uno::Reference
< css::embed::XStorage
> m_xNewParentStorage
;
152 css::uno::Reference
< css::io::XStream
> m_xNewObjectStream
;
155 css::uno::Reference
< css::io::XStream
> m_xCachedVisualRepresentation
;
156 bool m_bVisReplInitialized
;
157 bool m_bVisReplInStream
;
158 bool m_bStoreVisRepl
;
162 // TODO/LATER: may need to cache more than one aspect in future
163 bool m_bHasCachedSize
; // the object has cached size
164 css::awt::Size m_aCachedSize
;
165 sal_Int64 m_nCachedAspect
;
167 bool m_bHasSizeToSet
; // the object has cached size that should be set to OLE component
168 css::awt::Size m_aSizeToSet
; // this size might be different from the cached one ( scaling is applied )
169 sal_Int64 m_nAspectToSet
;
172 // cache the status of the object
173 // TODO/LATER: may need to cache more than one aspect in future
176 sal_Int64 m_nStatusAspect
;
178 // embedded object related stuff
179 OUString m_aEntryName
;
180 css::uno::Reference
< css::embed::XStorage
> m_xParentStorage
;
181 css::uno::Reference
< css::io::XStream
> m_xObjectStream
;
183 // link related stuff
184 OUString m_aLinkURL
; // ???
186 // points to own view provider if the object has no server
187 rtl::Reference
<OwnView_Impl
> m_xOwnView
;
189 // whether the object should be initialized from clipboard in case of default initialization
190 bool m_bFromClipboard
;
194 OUString m_aTempDumpURL
;
197 // the following member is used during verb execution to detect whether the verb execution modifies the object
198 VerbExecutionController m_aVerbExecutionController
;
200 // if the following member is set, the object works in wrapper mode
201 css::uno::Reference
< css::embed::XEmbeddedObject
> m_xWrappedObject
;
202 bool m_bTriedConversion
;
203 OUString m_aFilterName
; // if m_bTriedConversion, then the filter detected by that
205 css::uno::Reference
< css::uno::XInterface
> m_xParent
;
207 /// If it is allowed to modify entries in the stream of the OLE storage.
208 bool m_bStreamReadOnly
= false;
211 /// @throws css::uno::Exception
212 css::uno::Reference
< css::io::XStream
> TryToGetAcceptableFormat_Impl(
213 const css::uno::Reference
< css::io::XStream
>& xStream
);
215 /// @throws css::io::IOException
216 /// @throws css::uno::RuntimeException
217 css::uno::Reference
< css::io::XStream
> GetNewFilledTempStream_Impl(
218 const css::uno::Reference
< css::io::XInputStream
>& xInStream
);
220 void SwitchComponentToRunningState_Impl();
222 void MakeEventListenerNotification_Impl( const OUString
& aEventName
);
224 void StateChangeNotification_Impl( bool bBeforeChange
, sal_Int32 nOldState
, sal_Int32 nNewState
);
225 css::uno::Reference
< css::io::XOutputStream
> GetStreamForSaving();
228 css::uno::Sequence
< sal_Int32
> GetIntermediateVerbsSequence_Impl( sal_Int32 nNewState
);
230 static css::uno::Sequence
< sal_Int32
> GetReachableStatesList_Impl(
231 const css::uno::Sequence
< css::embed::VerbDescriptor
>& aVerbList
);
236 void SwitchOwnPersistence(
237 const css::uno::Reference
< css::embed::XStorage
>& xNewParentStorage
,
238 const css::uno::Reference
< css::io::XStream
>& xNewObjectStream
,
239 const OUString
& aNewName
);
241 void SwitchOwnPersistence(
242 const css::uno::Reference
< css::embed::XStorage
>& xNewParentStorage
,
243 const OUString
& aNewName
);
245 void GetRidOfComponent();
247 /// @throws css::uno::Exception
248 void StoreToLocation_Impl(
249 const css::uno::Reference
< css::embed::XStorage
>& xStorage
,
250 const OUString
& sEntName
,
251 const css::uno::Sequence
< css::beans::PropertyValue
>& lObjArgs
,
254 /// @throws css::uno::Exception
255 void StoreObjectToStream( css::uno::Reference
< css::io::XOutputStream
> const & xOutStream
);
257 /// @throws css::uno::Exception
258 void InsertVisualCache_Impl(
259 const css::uno::Reference
< css::io::XStream
>& xTargetStream
,
260 const css::uno::Reference
< css::io::XStream
>& xCachedVisualRepresentation
);
262 /// @throws css::uno::Exception
263 void RemoveVisualCache_Impl( const css::uno::Reference
< css::io::XStream
>& xTargetStream
);
265 void SetVisReplInStream( bool bExists
);
266 bool HasVisReplInStream();
268 /// @throws css::uno::Exception
269 css::embed::VisualRepresentation
GetVisualRepresentationInNativeFormat_Impl(
270 const css::uno::Reference
< css::io::XStream
>& xCachedVisRepr
);
272 css::uno::Reference
< css::io::XStream
> TryToRetrieveCachedVisualRepresentation_Impl(
273 const css::uno::Reference
< css::io::XStream
>& xStream
,
274 bool bAllowRepair50
= false )
277 bool SaveObject_Impl();
278 bool OnShowWindow_Impl( bool bShow
);
279 void CreateOleComponent_Impl( rtl::Reference
<OleComponent
> const & pOleComponent
= {} );
280 void CreateOleComponentAndLoad_Impl( rtl::Reference
<OleComponent
> const & pOleComponent
= {} );
281 void CreateOleComponentFromClipboard_Impl( OleComponent
* pOleComponent
= nullptr );
282 OUString
CreateTempURLEmpty_Impl();
283 OUString
GetTempURL_Impl();
284 void SetObjectIsLink_Impl( bool bIsLink
) { m_bIsLink
= bIsLink
; }
287 // the following 4 methods are related to switch to wrapping mode
288 void MoveListeners();
289 css::uno::Reference
< css::embed::XStorage
> CreateTemporarySubstorage( OUString
& o_aStorageName
);
290 OUString
MoveToTemporarySubstream();
291 bool TryToConvertToOOo( const css::uno::Reference
< css::io::XStream
>& xStream
);
294 // in case a new object must be created the class ID must be specified
295 OleEmbeddedObject( css::uno::Reference
< css::uno::XComponentContext
> xContext
,
296 const css::uno::Sequence
< sal_Int8
>& aClassID
,
297 OUString aClassName
);
299 // in case object will be loaded from a persistent entry or from a file the class ID will be detected on loading
300 // factory can do it for OOo objects, but for OLE objects OS dependent code is required
301 OleEmbeddedObject( css::uno::Reference
< css::uno::XComponentContext
> xContext
,
304 // this constructor let object be initialized from clipboard
305 OleEmbeddedObject( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
308 virtual ~OleEmbeddedObject() override
;
311 static void OnIconChanged_Impl();
312 void OnViewChanged_Impl();
313 void OnClosed_Impl();
318 virtual void SAL_CALL
changeState( sal_Int32 nNewState
) override
;
320 virtual css::uno::Sequence
< sal_Int32
> SAL_CALL
getReachableStates() override
;
322 virtual sal_Int32 SAL_CALL
getCurrentState() override
;
324 virtual void SAL_CALL
doVerb( sal_Int32 nVerbID
) override
;
326 virtual css::uno::Sequence
< css::embed::VerbDescriptor
> SAL_CALL
getSupportedVerbs() override
;
328 virtual void SAL_CALL
setClientSite(
329 const css::uno::Reference
< css::embed::XEmbeddedClient
>& xClient
) override
;
331 virtual css::uno::Reference
< css::embed::XEmbeddedClient
> SAL_CALL
getClientSite() override
;
333 virtual void SAL_CALL
update() override
;
335 virtual void SAL_CALL
setUpdateMode( sal_Int32 nMode
) override
;
337 virtual sal_Int64 SAL_CALL
getStatus( sal_Int64 nAspect
) override
;
339 virtual void SAL_CALL
setContainerName( const OUString
& sName
) override
;
344 virtual void SAL_CALL
setVisualAreaSize( sal_Int64 nAspect
, const css::awt::Size
& aSize
) override
;
346 virtual css::awt::Size SAL_CALL
getVisualAreaSize( sal_Int64 nAspect
) override
;
348 virtual css::embed::VisualRepresentation SAL_CALL
getPreferredVisualRepresentation( ::sal_Int64 nAspect
) override
;
350 virtual sal_Int32 SAL_CALL
getMapUnit( sal_Int64 nAspect
) override
;
355 virtual void SAL_CALL
setPersistentEntry(
356 const css::uno::Reference
< css::embed::XStorage
>& xStorage
,
357 const OUString
& sEntName
,
358 sal_Int32 nEntryConnectionMode
,
359 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
,
360 const css::uno::Sequence
< css::beans::PropertyValue
>& lObjArgs
) override
;
362 virtual void SAL_CALL
storeToEntry( const css::uno::Reference
< css::embed::XStorage
>& xStorage
, const OUString
& sEntName
, const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
, const css::uno::Sequence
< css::beans::PropertyValue
>& lObjArgs
) override
;
364 virtual void SAL_CALL
storeAsEntry(
365 const css::uno::Reference
< css::embed::XStorage
>& xStorage
,
366 const OUString
& sEntName
,
367 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
,
368 const css::uno::Sequence
< css::beans::PropertyValue
>& lObjArgs
) override
;
370 virtual void SAL_CALL
saveCompleted( sal_Bool bUseNew
) override
;
372 virtual sal_Bool SAL_CALL
hasEntry() override
;
374 virtual OUString SAL_CALL
getEntryName() override
;
378 virtual void SAL_CALL
breakLink( const css::uno::Reference
< css::embed::XStorage
>& xStorage
,
379 const OUString
& sEntName
) override
;
381 virtual sal_Bool SAL_CALL
isLink() override
;
383 virtual OUString SAL_CALL
getLinkURL() override
;
385 // XCommonEmbedPersist
386 virtual void SAL_CALL
storeOwn() override
;
388 virtual sal_Bool SAL_CALL
isReadonly() override
;
390 virtual void SAL_CALL
reload(
391 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
,
392 const css::uno::Sequence
< css::beans::PropertyValue
>& lObjArgs
) override
;
396 virtual css::uno::Sequence
< sal_Int8
> SAL_CALL
getClassID() override
;
398 virtual OUString SAL_CALL
getClassName() override
;
400 virtual void SAL_CALL
setClassInfo(
401 const css::uno::Sequence
< sal_Int8
>& aClassID
, const OUString
& aClassName
) override
;
403 // XStateChangeBroadcaster
404 virtual void SAL_CALL
addStateChangeListener( const css::uno::Reference
< css::embed::XStateChangeListener
>& xListener
) override
;
405 virtual void SAL_CALL
removeStateChangeListener( const css::uno::Reference
< css::embed::XStateChangeListener
>& xListener
) override
;
408 // XComponentSupplier
410 virtual css::uno::Reference
< css::util::XCloseable
> SAL_CALL
getComponent() override
;
414 virtual void SAL_CALL
close( sal_Bool DeliverOwnership
) override
;
416 virtual void SAL_CALL
addCloseListener(
417 const css::uno::Reference
< css::util::XCloseListener
>& Listener
) override
;
419 virtual void SAL_CALL
removeCloseListener(
420 const css::uno::Reference
< css::util::XCloseListener
>& Listener
) override
;
423 virtual void SAL_CALL
addEventListener(
424 const css::uno::Reference
< css::document::XEventListener
>& Listener
) override
;
426 virtual void SAL_CALL
removeEventListener(
427 const css::uno::Reference
< css::document::XEventListener
>& Listener
) override
;
429 // XInplaceObject ( only for wrapping scenario here )
431 virtual void SAL_CALL
setObjectRectangles( const css::awt::Rectangle
& aPosRect
,
432 const css::awt::Rectangle
& aClipRect
) override
;
434 virtual void SAL_CALL
enableModeless( sal_Bool bEnable
) override
;
436 virtual void SAL_CALL
translateAccelerators(
437 const css::uno::Sequence
< css::awt::KeyEvent
>& aKeys
) override
;
439 // XChild ( only for wrapping scenario here )
440 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
getParent( ) override
;
441 virtual void SAL_CALL
setParent( const css::uno::Reference
< css::uno::XInterface
>& Parent
) override
;
443 // XActiveDataStreamer
444 void SAL_CALL
setStream(const css::uno::Reference
<css::io::XStream
>& xStream
) override
;
445 css::uno::Reference
<css::io::XStream
> SAL_CALL
getStream() override
;
448 void SAL_CALL
initialize(const css::uno::Sequence
<css::uno::Any
>& rArguments
) override
;
451 OUString SAL_CALL
getImplementationName() override
;
452 sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
453 css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */