Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / toolkit / source / hatchwindow / documentcloser.cxx
blob6ef68a6a8845d693d0c7be62b57f3d184e5f378e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <com/sun/star/uno/XComponentContext.hpp>
21 #include <com/sun/star/util/XCloseable.hpp>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/lang/XComponent.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <com/sun/star/frame/XFrame.hpp>
27 #include <com/sun/star/awt/XVclWindowPeer.hpp>
28 #include <cppuhelper/implbase.hxx>
29 #include <comphelper/interfacecontainer4.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <utility>
32 #include <vcl/svapp.hxx>
33 #include <vcl/dialoghelper.hxx>
34 #include <vcl/window.hxx>
35 #include <tools/link.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
38 using namespace ::com::sun::star;
40 namespace {
42 // the service is implemented as a wrapper to be able to die by refcount
43 // the disposing mechanics is required for java related scenarios
44 class ODocumentCloser : public ::cppu::WeakImplHelper< css::lang::XComponent,
45 css::lang::XServiceInfo >
47 std::mutex m_aMutex;
48 css::uno::Reference< css::frame::XFrame > m_xFrame;
49 ::comphelper::OInterfaceContainerHelper4<lang::XEventListener> m_aListenersContainer; // list of listeners
51 bool m_bDisposed;
53 public:
54 explicit ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments);
56 // XComponent
57 virtual void SAL_CALL dispose() override;
58 virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) override;
59 virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override;
61 // XServiceInfo
62 virtual OUString SAL_CALL getImplementationName( ) override;
63 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
64 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
67 class MainThreadFrameCloserRequest
69 uno::Reference< frame::XFrame > m_xFrame;
71 public:
72 explicit MainThreadFrameCloserRequest( uno::Reference< frame::XFrame > xFrame )
73 : m_xFrame(std::move( xFrame ))
76 DECL_STATIC_LINK( MainThreadFrameCloserRequest, worker, void*, void );
78 static void Start( MainThreadFrameCloserRequest* pRequest );
82 void MainThreadFrameCloserRequest::Start( MainThreadFrameCloserRequest* pMTRequest )
84 if ( pMTRequest )
86 if ( Application::IsMainThread() )
88 // this is the main thread
89 worker( nullptr, pMTRequest );
91 else
92 Application::PostUserEvent( LINK( nullptr, MainThreadFrameCloserRequest, worker ), pMTRequest );
97 IMPL_STATIC_LINK( MainThreadFrameCloserRequest, worker, void*, p, void )
99 MainThreadFrameCloserRequest* pMTRequest = static_cast<MainThreadFrameCloserRequest*>(p);
100 if ( !pMTRequest )
101 return;
103 if ( pMTRequest->m_xFrame.is() )
105 // this is the main thread, the solar mutex must be locked
106 SolarMutexGuard aGuard;
110 uno::Reference< awt::XWindow > xWindow = pMTRequest->m_xFrame->getContainerWindow();
111 uno::Reference< awt::XVclWindowPeer > xWinPeer( xWindow, uno::UNO_QUERY_THROW );
113 xWindow->setVisible( false );
115 // reparent the window
116 xWinPeer->setProperty( "PluginParent", uno::Any( sal_Int64(0) ) );
118 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
119 if (pWindow)
120 vcl::EndAllDialogs(pWindow);
122 catch( uno::Exception& )
124 // ignore all the errors
129 uno::Reference< util::XCloseable > xCloseable( pMTRequest->m_xFrame, uno::UNO_QUERY_THROW );
130 xCloseable->close( true );
132 catch( uno::Exception& )
134 // ignore all the errors
138 delete pMTRequest;
141 ODocumentCloser::ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments)
142 : m_bDisposed( false )
144 std::unique_lock aGuard( m_aMutex );
145 if ( !m_refCount )
146 throw uno::RuntimeException(); // the object must be refcounted already!
148 sal_Int32 nLen = aArguments.getLength();
149 if ( nLen != 1 )
150 throw lang::IllegalArgumentException(
151 "Wrong count of parameters!",
152 uno::Reference< uno::XInterface >(),
153 0 );
155 if ( !( aArguments[0] >>= m_xFrame ) || !m_xFrame.is() )
156 throw lang::IllegalArgumentException(
157 "Nonempty reference is expected as the first argument!",
158 uno::Reference< uno::XInterface >(),
159 0 );
163 // XComponent
165 void SAL_CALL ODocumentCloser::dispose()
167 std::unique_lock aGuard( m_aMutex );
169 if ( m_bDisposed )
170 return;
172 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
173 m_aListenersContainer.disposeAndClear( aGuard, aSource );
175 // TODO: trigger a main thread execution to close the frame
176 if ( m_xFrame.is() )
178 // the created object will be deleted after thread execution
179 MainThreadFrameCloserRequest* pCloser = new MainThreadFrameCloserRequest( m_xFrame );
180 MainThreadFrameCloserRequest::Start( pCloser );
183 m_bDisposed = true;
187 void SAL_CALL ODocumentCloser::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
189 std::unique_lock aGuard( m_aMutex );
190 if ( m_bDisposed )
191 throw lang::DisposedException(); // TODO
193 m_aListenersContainer.addInterface( aGuard, xListener );
197 void SAL_CALL ODocumentCloser::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
199 std::unique_lock aGuard( m_aMutex );
200 m_aListenersContainer.removeInterface( aGuard, xListener );
203 // XServiceInfo
204 OUString SAL_CALL ODocumentCloser::getImplementationName( )
206 return "com.sun.star.comp.embed.DocumentCloser";
209 sal_Bool SAL_CALL ODocumentCloser::supportsService( const OUString& ServiceName )
211 return cppu::supportsService(this, ServiceName);
214 uno::Sequence< OUString > SAL_CALL ODocumentCloser::getSupportedServiceNames()
216 return { "com.sun.star.embed.DocumentCloser" };
221 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
222 com_sun_star_comp_embed_DocumentCloser_get_implementation(
223 SAL_UNUSED_PARAMETER css::uno::XComponentContext *,
224 css::uno::Sequence<css::uno::Any> const &arguments)
226 return cppu::acquire(new ODocumentCloser(arguments));
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */