24 #include <com/sun/star/util/CloseVetoException.hpp>
25 #include <com/sun/star/util/XCloseBroadcaster.hpp>
26 #include <com/sun/star/util/XCloseable.hpp>
27 #include <com/sun/star/lang/DisposedException.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/frame/XDesktop.hpp>
30 #include <com/sun/star/frame/TerminationVetoException.hpp>
31 #include <com/sun/star/frame/DoubleInitializationException.hpp>
32 #include <com/sun/star/embed/Actions.hpp>
33 #include <com/sun/star/embed/XActionsApproval.hpp>
47 , m_bInitialized( false )
60 catch ( uno::RuntimeException& )
69 ::osl::MutexGuard aGuard(
m_aMutex );
72 throw lang::DisposedException();
74 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(
this) );
90 ::osl::MutexGuard aGuard(
m_aMutex );
92 throw lang::DisposedException();
103 ::osl::MutexGuard aGuard(
m_aMutex );
112 ::osl::MutexGuard aGuard(
m_aMutex );
114 throw frame::DoubleInitializationException();
117 throw lang::DisposedException();
120 throw uno::RuntimeException();
122 uno::Reference< uno::XInterface > xInstance;
123 uno::Reference< embed::XActionsApproval > xApproval;
127 sal_Int32 nLen = aArguments.getLength();
128 if ( nLen < 2 || nLen > 3 )
129 throw lang::IllegalArgumentException(
130 "Wrong count of parameters!",
131 uno::Reference< uno::XInterface >(),
134 if ( !( aArguments[0] >>= xInstance ) || !xInstance.is() )
135 throw lang::IllegalArgumentException(
136 "Nonempty reference is expected as the first argument!",
137 uno::Reference< uno::XInterface >(),
140 sal_Int32 nModes = 0;
142 !( aArguments[1] >>= nModes ) ||
144 !( nModes & embed::Actions::PREVENT_CLOSE ) &&
145 !( nModes & embed::Actions::PREVENT_TERMINATION )
149 throw lang::IllegalArgumentException(
150 "The correct modes set is expected as the second argument!",
151 uno::Reference< uno::XInterface >(),
155 if ( nLen == 3 && !( aArguments[2] >>= xApproval ) )
156 throw lang::IllegalArgumentException(
157 "If the third argument is provided, it must be XActionsApproval implementation!",
158 uno::Reference< uno::XInterface >(),
167 catch( uno::Exception& )
179 return "com.sun.star.comp.embed.InstanceLocker";
189 return {
"com.sun.star.embed.InstanceLocker" };
196 const uno::Reference< uno::XInterface >& xInstance,
198 const uno::Reference< embed::XActionsApproval >& rApproval )
199 : m_xInstance( xInstance )
200 , m_xApproval( rApproval )
201 , m_xWrapper( xWrapper )
203 , m_bInitialized( false )
221 if (
m_nMode & embed::Actions::PREVENT_CLOSE )
225 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster(
m_xInstance, uno::UNO_QUERY );
226 if ( xCloseBroadcaster.is() )
227 xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >(
this ) );
229 uno::Reference< util::XCloseable > xCloseable(
m_xInstance, uno::UNO_QUERY );
230 if ( xCloseable.is() )
231 xCloseable->close(
true );
233 catch( uno::Exception& )
237 if (
m_nMode & embed::Actions::PREVENT_TERMINATION )
241 uno::Reference< frame::XDesktop > xDesktop(
m_xInstance, uno::UNO_QUERY_THROW );
242 xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >(
this ) );
244 catch( uno::Exception& )
256 osl::ClearableMutexGuard aGuard(
m_aMutex );
266 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
268 if ( xComponent.is() )
270 try { xComponent->dispose(); }
271 catch( uno::Exception& ){}
281 osl::ClearableMutexGuard aGuard(
m_aMutex );
287 uno::Reference< embed::XActionsApproval > xApprove =
m_xApproval;
292 if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_CLOSE ) )
293 throw util::CloseVetoException();
295 catch( util::CloseVetoException& )
300 catch( uno::Exception& )
309 osl::ClearableMutexGuard aGuard(
m_aMutex );
315 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( aEvent.Source, uno::UNO_QUERY );
316 if ( !xCloseBroadcaster.is() )
319 xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >(
this ) );
324 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
326 if ( xComponent.is() )
328 try { xComponent->dispose(); }
329 catch( uno::Exception& ){}
339 osl::ClearableMutexGuard aGuard(
m_aMutex );
340 if ( !(aEvent.Source ==
m_xInstance && (
m_nMode & embed::Actions::PREVENT_TERMINATION )) )
345 uno::Reference< embed::XActionsApproval > xApprove =
m_xApproval;
350 if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_TERMINATION ) )
351 throw frame::TerminationVetoException();
353 catch( frame::TerminationVetoException& )
358 catch( uno::Exception& )
367 osl::ClearableMutexGuard aGuard(
m_aMutex );
373 uno::Reference< frame::XDesktop > xDesktop( aEvent.Source, uno::UNO_QUERY );
374 if ( !xDesktop.is() )
379 xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >(
this ) );
384 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
386 if ( xComponent.is() )
388 try { xComponent->dispose(); }
389 catch( uno::Exception& ){}
393 catch( uno::Exception& )
402 osl::ClearableMutexGuard aGuard(
m_aMutex );
409 if (
m_nMode & embed::Actions::PREVENT_CLOSE )
411 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster(
m_xInstance, uno::UNO_QUERY_THROW );
412 xCloseBroadcaster->addCloseListener( static_cast< util::XCloseListener* >(
this ) );
415 if (
m_nMode & embed::Actions::PREVENT_TERMINATION )
417 uno::Reference< frame::XDesktop > xDesktop(
m_xInstance, uno::UNO_QUERY_THROW );
418 xDesktop->addTerminateListener( static_cast< frame::XTerminateListener* >(
this ) );
421 catch( uno::Exception& )
424 uno::Reference< lang::XComponent > xComponent(
m_xWrapper.get(), uno::UNO_QUERY );
426 if ( xComponent.is() )
428 try { xComponent->dispose(); }
429 catch( uno::Exception& ){}
438 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
440 css::uno::XComponentContext *,
441 css::uno::Sequence<css::uno::Any>
const &)
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
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
std::unique_ptr<::comphelper::OInterfaceContainerHelper2 > m_pListenersContainer
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
virtual ~OLockListener() override
virtual void SAL_CALL queryClosing(const css::lang::EventObject &Source, sal_Bool GetsOwnership) override