23 #include <com/sun/star/util/CloseVetoException.hpp>
24 #include <com/sun/star/util/XCloseBroadcaster.hpp>
25 #include <com/sun/star/util/XCloseable.hpp>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/frame/XDesktop.hpp>
29 #include <com/sun/star/frame/TerminationVetoException.hpp>
30 #include <com/sun/star/frame/DoubleInitializationException.hpp>
31 #include <com/sun/star/embed/Actions.hpp>
32 #include <com/sun/star/embed/XActionsApproval.hpp>
46 , m_bInitialized(
false )
59 catch ( uno::RuntimeException& )
71 throw lang::DisposedException();
73 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(
this) );
91 throw lang::DisposedException();
107 std::unique_lock aGuard(
m_aMutex );
109 throw frame::DoubleInitializationException();
112 throw lang::DisposedException();
115 throw uno::RuntimeException();
117 uno::Reference< uno::XInterface > xInstance;
118 uno::Reference< embed::XActionsApproval > xApproval;
122 sal_Int32 nLen = aArguments.getLength();
123 if ( nLen < 2 || nLen > 3 )
124 throw lang::IllegalArgumentException(
125 "Wrong count of parameters!",
126 uno::Reference< uno::XInterface >(),
129 if ( !( aArguments[0] >>= xInstance ) || !xInstance.is() )
130 throw lang::IllegalArgumentException(
131 "Nonempty reference is expected as the first argument!",
132 uno::Reference< uno::XInterface >(),
135 sal_Int32 nModes = 0;
137 !( aArguments[1] >>= nModes ) ||
139 !( nModes & embed::Actions::PREVENT_CLOSE ) &&
140 !( nModes & embed::Actions::PREVENT_TERMINATION )
144 throw lang::IllegalArgumentException(
145 "The correct modes set is expected as the second argument!",
146 uno::Reference< uno::XInterface >(),
150 if ( nLen == 3 && !( aArguments[2] >>= xApproval ) )
151 throw lang::IllegalArgumentException(
152 "If the third argument is provided, it must be XActionsApproval implementation!",
153 uno::Reference< uno::XInterface >(),
162 catch( uno::Exception& )
175 return "com.sun.star.comp.embed.InstanceLocker";
185 return {
"com.sun.star.embed.InstanceLocker" };
192 const uno::Reference< uno::XInterface >& xInstance,
194 const uno::Reference< embed::XActionsApproval >& rApproval )
195 : m_xInstance( xInstance )
196 , m_xApproval( rApproval )
197 , m_xWrapper( xWrapper )
199 , m_bInitialized(
false )
212 std::unique_lock aGuard(
m_aMutex );
222 if ( nMode & embed::Actions::PREVENT_CLOSE )
226 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( xInstance, uno::UNO_QUERY );
227 if ( xCloseBroadcaster.is() )
228 xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >(
this ) );
230 uno::Reference< util::XCloseable > xCloseable( xInstance, uno::UNO_QUERY );
231 if ( xCloseable.is() )
232 xCloseable->close(
true );
234 catch( uno::Exception& )
238 if ( nMode & embed::Actions::PREVENT_TERMINATION )
242 uno::Reference< frame::XDesktop > xDesktop( xInstance, uno::UNO_QUERY_THROW );
243 xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >(
this ) );
245 catch( uno::Exception& )
254 std::unique_lock aGuard(
m_aMutex );
264 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
266 if ( xComponent.is() )
268 try { xComponent->dispose(); }
269 catch( uno::Exception& ){}
279 std::unique_lock aGuard(
m_aMutex );
285 uno::Reference< embed::XActionsApproval > xApprove =
m_xApproval;
290 if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_CLOSE ) )
291 throw util::CloseVetoException();
293 catch( util::CloseVetoException& )
298 catch( uno::Exception& )
307 std::unique_lock aGuard(
m_aMutex );
313 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( aEvent.Source, uno::UNO_QUERY );
314 if ( !xCloseBroadcaster.is() )
317 xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >(
this ) );
322 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
324 if ( xComponent.is() )
326 try { xComponent->dispose(); }
327 catch( uno::Exception& ){}
337 std::unique_lock aGuard(
m_aMutex );
338 if ( !(aEvent.Source ==
m_xInstance && (
m_nMode & embed::Actions::PREVENT_TERMINATION )) )
343 uno::Reference< embed::XActionsApproval > xApprove =
m_xApproval;
348 if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_TERMINATION ) )
349 throw frame::TerminationVetoException();
351 catch( frame::TerminationVetoException& )
356 catch( uno::Exception& )
365 std::unique_lock aGuard(
m_aMutex );
371 uno::Reference< frame::XDesktop > xDesktop( aEvent.Source, uno::UNO_QUERY );
372 if ( !xDesktop.is() )
377 xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >(
this ) );
382 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
384 if ( xComponent.is() )
386 try { xComponent->dispose(); }
387 catch( uno::Exception& ){}
391 catch( uno::Exception& )
400 std::unique_lock aGuard(
m_aMutex );
407 if (
m_nMode & embed::Actions::PREVENT_CLOSE )
409 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster(
m_xInstance, uno::UNO_QUERY_THROW );
410 xCloseBroadcaster->addCloseListener( static_cast< util::XCloseListener* >(
this ) );
413 if (
m_nMode & embed::Actions::PREVENT_TERMINATION )
415 uno::Reference< frame::XDesktop > xDesktop(
m_xInstance, uno::UNO_QUERY_THROW );
416 xDesktop->addTerminateListener( static_cast< frame::XTerminateListener* >(
this ) );
419 catch( uno::Exception& )
422 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
424 if ( xComponent.is() )
426 try { xComponent->dispose(); }
427 catch( uno::Exception& ){}
436 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
438 css::uno::XComponentContext *,
439 css::uno::Sequence<css::uno::Any>
const &)
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
virtual void SAL_CALL dispose() override
css::uno::Reference< css::embed::XActionsApproval > m_xApproval
virtual void SAL_CALL notifyTermination(const css::lang::EventObject &Event) override
exports com.sun.star. embed
virtual void SAL_CALL queryTermination(const css::lang::EventObject &Event) override
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
Inserts an element into the container.
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_embed_InstanceLocker(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
rtl::Reference< OLockListener > m_xLockListener
virtual OUString SAL_CALL getImplementationName() override
virtual ~OInstanceLocker() override
virtual void SAL_CALL notifyClosing(const css::lang::EventObject &Source) override
OLockListener(const css::uno::WeakReference< css::lang::XComponent > &xWrapper, const css::uno::Reference< css::uno::XInterface > &xInstance, sal_Int32 nMode, const css::uno::Reference< css::embed::XActionsApproval > &rApproval)
css::uno::Reference< css::uno::XInterface > m_xInstance
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::WeakReference< css::lang::XComponent > m_xWrapper
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
Removes an element from the container.
virtual ~OLockListener() override
comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > m_aListenersContainer
virtual void SAL_CALL queryClosing(const css::lang::EventObject &Source, sal_Bool GetsOwnership) override
bool m_bDetectedRangeSegmentation false