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>
47, m_bInitialized( false )
60 catch ( uno::RuntimeException& )
72 throw lang::DisposedException();
92 throw lang::DisposedException();
100 std::unique_lock aGuard(
m_aMutex );
108 std::unique_lock aGuard(
m_aMutex );
110 throw frame::DoubleInitializationException();
113 throw lang::DisposedException();
116 throw uno::RuntimeException();
118 uno::Reference< uno::XInterface > xInstance;
119 uno::Reference< embed::XActionsApproval > xApproval;
124 if ( nLen < 2 || nLen > 3 )
125 throw lang::IllegalArgumentException(
126 "Wrong count of parameters!",
127 uno::Reference< uno::XInterface >(),
130 if ( !(
aArguments[0] >>= xInstance ) || !xInstance.is() )
131 throw lang::IllegalArgumentException(
132 "Nonempty reference is expected as the first argument!",
133 uno::Reference< uno::XInterface >(),
136 sal_Int32 nModes = 0;
140 !( nModes & embed::Actions::PREVENT_CLOSE ) &&
141 !( nModes & embed::Actions::PREVENT_TERMINATION )
145 throw lang::IllegalArgumentException(
146 "The correct modes set is expected as the second argument!",
147 uno::Reference< uno::XInterface >(),
151 if ( nLen == 3 && !(
aArguments[2] >>= xApproval ) )
152 throw lang::IllegalArgumentException(
153 "If the third argument is provided, it must be XActionsApproval implementation!",
154 uno::Reference< uno::XInterface >(),
163 catch( uno::Exception& )
176 return "com.sun.star.comp.embed.InstanceLocker";
186 return {
"com.sun.star.embed.InstanceLocker" };
193 uno::Reference< uno::XInterface > xInstance,
195 uno::Reference< embed::XActionsApproval > xApproval )
196: m_xInstance(
std::move( xInstance ))
197, m_xApproval(
std::move( xApproval ))
198, m_xWrapper(
std::move( xWrapper ))
200, m_bInitialized( false )
213 std::unique_lock aGuard(
m_aMutex );
223 if ( nMode & embed::Actions::PREVENT_CLOSE )
227 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( xInstance, uno::UNO_QUERY );
228 if ( xCloseBroadcaster.is() )
229 xCloseBroadcaster->removeCloseListener(
static_cast< util::XCloseListener*
>(
this ) );
231 uno::Reference< util::XCloseable > xCloseable( xInstance, uno::UNO_QUERY );
232 if ( xCloseable.is() )
233 xCloseable->close(
true );
235 catch( uno::Exception& )
239 if ( nMode & embed::Actions::PREVENT_TERMINATION )
243 uno::Reference< frame::XDesktop > xDesktop( xInstance, uno::UNO_QUERY_THROW );
244 xDesktop->removeTerminateListener(
static_cast< frame::XTerminateListener*
>(
this ) );
246 catch( uno::Exception& )
255 std::unique_lock aGuard(
m_aMutex );
265 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
267 if ( xComponent.is() )
269 try { xComponent->dispose(); }
270 catch( uno::Exception& ){}
280 std::unique_lock aGuard(
m_aMutex );
286 uno::Reference< embed::XActionsApproval > xApprove =
m_xApproval;
291 if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_CLOSE ) )
292 throw util::CloseVetoException();
294 catch( util::CloseVetoException& )
299 catch( uno::Exception& )
308 std::unique_lock aGuard(
m_aMutex );
314 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster(
aEvent.Source, uno::UNO_QUERY );
315 if ( !xCloseBroadcaster.is() )
318 xCloseBroadcaster->removeCloseListener(
static_cast< util::XCloseListener*
>(
this ) );
319 m_nMode &= ~embed::Actions::PREVENT_CLOSE;
323 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
325 if ( xComponent.is() )
327 try { xComponent->dispose(); }
328 catch( uno::Exception& ){}
338 std::unique_lock aGuard(
m_aMutex );
344 uno::Reference< embed::XActionsApproval > xApprove =
m_xApproval;
349 if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_TERMINATION ) )
350 throw frame::TerminationVetoException();
352 catch( frame::TerminationVetoException& )
357 catch( uno::Exception& )
366 std::unique_lock aGuard(
m_aMutex );
372 uno::Reference< frame::XDesktop > xDesktop(
aEvent.Source, uno::UNO_QUERY );
373 if ( !xDesktop.is() )
378 xDesktop->removeTerminateListener(
static_cast< frame::XTerminateListener*
>(
this ) );
379 m_nMode &= ~embed::Actions::PREVENT_TERMINATION;
383 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
385 if ( xComponent.is() )
387 try { xComponent->dispose(); }
388 catch( uno::Exception& ){}
392 catch( uno::Exception& )
401 std::unique_lock aGuard(
m_aMutex );
408 if (
m_nMode & embed::Actions::PREVENT_CLOSE )
410 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster(
m_xInstance, uno::UNO_QUERY_THROW );
411 xCloseBroadcaster->addCloseListener(
static_cast< util::XCloseListener*
>(
this ) );
414 if (
m_nMode & embed::Actions::PREVENT_TERMINATION )
416 uno::Reference< frame::XDesktop > xDesktop(
m_xInstance, uno::UNO_QUERY_THROW );
417 xDesktop->addTerminateListener(
static_cast< frame::XTerminateListener*
>(
this ) );
420 catch( uno::Exception& )
423 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
425 if ( xComponent.is() )
427 try { xComponent->dispose(); }
428 catch( uno::Exception& ){}
437extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
439 css::uno::XComponentContext *,
440 css::uno::Sequence<css::uno::Any>
const &)
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > m_aListenersContainer
virtual ~OInstanceLocker() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
rtl::Reference< OLockListener > m_xLockListener
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
virtual void SAL_CALL dispose() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::WeakReference< css::lang::XComponent > m_xWrapper
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
css::uno::Reference< css::uno::XInterface > m_xInstance
virtual void SAL_CALL queryClosing(const css::lang::EventObject &Source, sal_Bool GetsOwnership) override
virtual void SAL_CALL notifyClosing(const css::lang::EventObject &Source) override
css::uno::Reference< css::embed::XActionsApproval > m_xApproval
virtual void SAL_CALL queryTermination(const css::lang::EventObject &Event) override
virtual ~OLockListener() override
virtual void SAL_CALL notifyTermination(const css::lang::EventObject &Event) override
OLockListener(css::uno::WeakReference< css::lang::XComponent > xWrapper, css::uno::Reference< css::uno::XInterface > xInstance, sal_Int32 nMode, css::uno::Reference< css::embed::XActionsApproval > xApproval)
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
Inserts an element into the container.
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
Removes an element from the container.
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_embed_InstanceLocker(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Sequence< PropertyValue > aArguments
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)