Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/util/XCloseBroadcaster.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/frame/DoubleInitializationException.hpp>
25 : #include <com/sun/star/awt/XVclWindowPeer.hpp>
26 : #include <comphelper/processfactory.hxx>
27 : #include <osl/mutex.hxx>
28 : #include <vcl/svapp.hxx>
29 : #include <vcl/dialog.hxx>
30 : #include <tools/link.hxx>
31 : #include <toolkit/helper/vclunohelper.hxx>
32 :
33 : #include "documentcloser.hxx"
34 :
35 : using namespace ::com::sun::star;
36 :
37 :
38 : // ====================================================================
39 : // MainThreadFrameCloserRequest
40 : // ====================================================================
41 :
42 0 : class MainThreadFrameCloserRequest
43 : {
44 : uno::Reference< frame::XFrame > m_xFrame;
45 :
46 : public:
47 0 : MainThreadFrameCloserRequest( const uno::Reference< frame::XFrame >& xFrame )
48 0 : : m_xFrame( xFrame )
49 0 : {}
50 :
51 : DECL_STATIC_LINK( MainThreadFrameCloserRequest, worker, MainThreadFrameCloserRequest* );
52 :
53 : static void Start( MainThreadFrameCloserRequest* pRequest );
54 : };
55 :
56 : // --------------------------------------------------------
57 0 : void MainThreadFrameCloserRequest::Start( MainThreadFrameCloserRequest* pMTRequest )
58 : {
59 0 : if ( pMTRequest )
60 : {
61 0 : if ( Application::GetMainThreadIdentifier() == osl_getThreadIdentifier( NULL ) )
62 : {
63 : // this is the main thread
64 0 : worker( NULL, pMTRequest );
65 : }
66 : else
67 0 : Application::PostUserEvent( STATIC_LINK( NULL, MainThreadFrameCloserRequest, worker ), pMTRequest );
68 : }
69 0 : }
70 :
71 : // --------------------------------------------------------
72 0 : IMPL_STATIC_LINK( MainThreadFrameCloserRequest, worker, MainThreadFrameCloserRequest*, pMTRequest )
73 : {
74 : (void) pThis; // unused
75 0 : if ( pMTRequest )
76 : {
77 0 : if ( pMTRequest->m_xFrame.is() )
78 : {
79 : // this is the main thread, the solar mutex must be locked
80 0 : SolarMutexGuard aGuard;
81 :
82 : try
83 : {
84 0 : uno::Reference< awt::XWindow > xWindow = pMTRequest->m_xFrame->getContainerWindow();
85 0 : uno::Reference< awt::XVclWindowPeer > xWinPeer( xWindow, uno::UNO_QUERY_THROW );
86 :
87 0 : xWindow->setVisible( sal_False );
88 :
89 : // reparent the window
90 0 : xWinPeer->setProperty( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PluginParent" ) ),
91 0 : uno::makeAny( (sal_Int64) 0 ) );
92 :
93 0 : Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
94 0 : if ( pWindow )
95 0 : Dialog::EndAllDialogs( pWindow );
96 : }
97 0 : catch( uno::Exception& )
98 : {
99 : // ignore all the errors
100 : }
101 :
102 : try
103 : {
104 0 : uno::Reference< util::XCloseable > xCloseable( pMTRequest->m_xFrame, uno::UNO_QUERY_THROW );
105 0 : xCloseable->close( sal_True );
106 : }
107 0 : catch( uno::Exception& )
108 : {
109 : // ignore all the errors
110 0 : }
111 : }
112 :
113 0 : delete pMTRequest;
114 : }
115 :
116 0 : return 0;
117 : }
118 :
119 :
120 : // ====================================================================
121 : // ODocumentCloser
122 : // ====================================================================
123 :
124 : // --------------------------------------------------------
125 0 : ODocumentCloser::ODocumentCloser( const uno::Reference< uno::XComponentContext >& xContext )
126 : : m_xContext( xContext )
127 : , m_pListenersContainer( NULL )
128 : , m_bDisposed( sal_False )
129 0 : , m_bInitialized( sal_False )
130 : {
131 0 : }
132 :
133 : // --------------------------------------------------------
134 0 : ODocumentCloser::~ODocumentCloser()
135 : {
136 0 : if ( m_pListenersContainer )
137 : {
138 0 : delete m_pListenersContainer;
139 0 : m_pListenersContainer = NULL;
140 : }
141 0 : }
142 :
143 : // XComponent
144 : // --------------------------------------------------------
145 0 : void SAL_CALL ODocumentCloser::dispose()
146 : throw (uno::RuntimeException)
147 : {
148 0 : ::osl::MutexGuard aGuard( m_aMutex );
149 :
150 0 : if ( m_bDisposed )
151 0 : throw lang::DisposedException();
152 :
153 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
154 0 : if ( m_pListenersContainer )
155 0 : m_pListenersContainer->disposeAndClear( aSource );
156 :
157 : // TODO: trigger a main thread execution to close the frame
158 0 : if ( m_xFrame.is() )
159 : {
160 : // the created object will be deleted after thread execution
161 0 : MainThreadFrameCloserRequest* pCloser = new MainThreadFrameCloserRequest( m_xFrame );
162 0 : MainThreadFrameCloserRequest::Start( pCloser );
163 : }
164 :
165 0 : m_bDisposed = sal_True;
166 0 : }
167 :
168 : // --------------------------------------------------------
169 0 : void SAL_CALL ODocumentCloser::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
170 : throw (uno::RuntimeException)
171 : {
172 0 : ::osl::MutexGuard aGuard( m_aMutex );
173 0 : if ( m_bDisposed )
174 0 : throw lang::DisposedException(); // TODO
175 :
176 0 : if ( !m_pListenersContainer )
177 0 : m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
178 :
179 0 : m_pListenersContainer->addInterface( xListener );
180 0 : }
181 :
182 : // --------------------------------------------------------
183 0 : void SAL_CALL ODocumentCloser::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
184 : throw (uno::RuntimeException)
185 : {
186 0 : ::osl::MutexGuard aGuard( m_aMutex );
187 0 : if ( m_pListenersContainer )
188 0 : m_pListenersContainer->removeInterface( xListener );
189 0 : }
190 :
191 : // XInitialization
192 : // --------------------------------------------------------
193 0 : void SAL_CALL ODocumentCloser::initialize( const uno::Sequence< uno::Any >& aArguments )
194 : throw (uno::Exception, uno::RuntimeException)
195 : {
196 0 : ::osl::MutexGuard aGuard( m_aMutex );
197 0 : if ( m_bInitialized )
198 0 : throw frame::DoubleInitializationException();
199 :
200 0 : if ( m_bDisposed )
201 0 : throw lang::DisposedException(); // TODO
202 :
203 0 : if ( !m_refCount )
204 0 : throw uno::RuntimeException(); // the object must be refcounted already!
205 :
206 0 : sal_Int32 nLen = aArguments.getLength();
207 0 : if ( nLen != 1 )
208 : throw lang::IllegalArgumentException(
209 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Wrong count of parameters!" ) ),
210 : uno::Reference< uno::XInterface >(),
211 0 : 0 );
212 :
213 0 : if ( !( aArguments[0] >>= m_xFrame ) || !m_xFrame.is() )
214 : throw lang::IllegalArgumentException(
215 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Nonempty reference is expected as the first argument!" ) ),
216 : uno::Reference< uno::XInterface >(),
217 0 : 0 );
218 :
219 0 : m_bInitialized = sal_True;
220 0 : }
221 :
222 :
223 : // XServiceInfo
224 : // --------------------------------------------------------
225 0 : ::rtl::OUString SAL_CALL ODocumentCloser::getImplementationName( )
226 : throw (uno::RuntimeException)
227 : {
228 0 : return impl_staticGetImplementationName();
229 : }
230 :
231 : // --------------------------------------------------------
232 0 : ::sal_Bool SAL_CALL ODocumentCloser::supportsService( const ::rtl::OUString& ServiceName )
233 : throw (uno::RuntimeException)
234 : {
235 0 : uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
236 :
237 0 : for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
238 0 : if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
239 0 : return sal_True;
240 :
241 0 : return sal_False;
242 : }
243 :
244 : // --------------------------------------------------------
245 0 : uno::Sequence< ::rtl::OUString > SAL_CALL ODocumentCloser::getSupportedServiceNames()
246 : throw (uno::RuntimeException)
247 : {
248 0 : return impl_staticGetSupportedServiceNames();
249 : }
250 :
251 : // Static methods
252 : // --------------------------------------------------------
253 0 : uno::Sequence< ::rtl::OUString > SAL_CALL ODocumentCloser::impl_staticGetSupportedServiceNames()
254 : {
255 0 : const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.embed.DocumentCloser" ) );
256 0 : return uno::Sequence< rtl::OUString >( &aServiceName, 1 );
257 : }
258 :
259 : // --------------------------------------------------------
260 0 : ::rtl::OUString SAL_CALL ODocumentCloser::impl_staticGetImplementationName()
261 : {
262 0 : return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.embed.DocumentCloser" ) );
263 : }
264 :
265 : // --------------------------------------------------------
266 0 : uno::Reference< uno::XInterface > SAL_CALL ODocumentCloser::impl_staticCreateSelfInstance(
267 : const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
268 : {
269 : uno::Reference< uno::XComponentContext > xContext(
270 0 : comphelper::getComponentContext( xServiceManager ) );
271 0 : return static_cast< cppu::OWeakObject * >( new ODocumentCloser( xContext ) );
272 : }
273 :
274 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|