docthemes: Save themes def. to a file when added to ColorSets
[LibreOffice.git] / toolkit / source / hatchwindow / documentcloser.cxx
blob53277e6982925e0ac0098accccf203cbe6bacb40
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 );
112 if (xWinPeer)
114 xWindow->setVisible( false );
116 // reparent the window
117 xWinPeer->setProperty( u"PluginParent"_ustr, uno::Any( sal_Int64(0) ) );
119 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
120 if (pWindow)
121 vcl::EndAllDialogs(pWindow);
124 catch( uno::Exception& )
126 // ignore all the errors
131 uno::Reference< util::XCloseable > xCloseable( pMTRequest->m_xFrame, uno::UNO_QUERY );
132 if (xCloseable)
133 xCloseable->close( true );
135 catch( uno::Exception& )
137 // ignore all the errors
141 delete pMTRequest;
144 ODocumentCloser::ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments)
145 : m_bDisposed( false )
147 std::unique_lock aGuard( m_aMutex );
148 if ( !m_refCount )
149 throw uno::RuntimeException(); // the object must be refcounted already!
151 sal_Int32 nLen = aArguments.getLength();
152 if ( nLen != 1 )
153 throw lang::IllegalArgumentException(
154 u"Wrong count of parameters!"_ustr,
155 uno::Reference< uno::XInterface >(),
156 0 );
158 if ( !( aArguments[0] >>= m_xFrame ) || !m_xFrame.is() )
159 throw lang::IllegalArgumentException(
160 u"Nonempty reference is expected as the first argument!"_ustr,
161 uno::Reference< uno::XInterface >(),
162 0 );
166 // XComponent
168 void SAL_CALL ODocumentCloser::dispose()
170 std::unique_lock aGuard( m_aMutex );
172 if ( m_bDisposed )
173 return;
175 lang::EventObject aSource( getXWeak() );
176 m_aListenersContainer.disposeAndClear( aGuard, aSource );
178 // TODO: trigger a main thread execution to close the frame
179 if ( m_xFrame.is() )
181 // the created object will be deleted after thread execution
182 MainThreadFrameCloserRequest* pCloser = new MainThreadFrameCloserRequest( m_xFrame );
183 MainThreadFrameCloserRequest::Start( pCloser );
186 m_bDisposed = true;
190 void SAL_CALL ODocumentCloser::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
192 std::unique_lock aGuard( m_aMutex );
193 if ( m_bDisposed )
194 throw lang::DisposedException(); // TODO
196 m_aListenersContainer.addInterface( aGuard, xListener );
200 void SAL_CALL ODocumentCloser::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
202 std::unique_lock aGuard( m_aMutex );
203 m_aListenersContainer.removeInterface( aGuard, xListener );
206 // XServiceInfo
207 OUString SAL_CALL ODocumentCloser::getImplementationName( )
209 return u"com.sun.star.comp.embed.DocumentCloser"_ustr;
212 sal_Bool SAL_CALL ODocumentCloser::supportsService( const OUString& ServiceName )
214 return cppu::supportsService(this, ServiceName);
217 uno::Sequence< OUString > SAL_CALL ODocumentCloser::getSupportedServiceNames()
219 return { u"com.sun.star.embed.DocumentCloser"_ustr };
224 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
225 com_sun_star_comp_embed_DocumentCloser_get_implementation(
226 SAL_UNUSED_PARAMETER css::uno::XComponentContext *,
227 css::uno::Sequence<css::uno::Any> const &arguments)
229 return cppu::acquire(new ODocumentCloser(arguments));
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */