2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-21 14:30:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2006-02-09 12:58:54 +00:00
|
|
|
|
2013-12-23 10:27:49 +01:00
|
|
|
#include <com/sun/star/uno/XComponentContext.hpp>
|
2006-02-09 12:58:54 +00:00
|
|
|
#include <com/sun/star/util/XCloseBroadcaster.hpp>
|
|
|
|
#include <com/sun/star/util/XCloseable.hpp>
|
|
|
|
#include <com/sun/star/lang/DisposedException.hpp>
|
|
|
|
#include <com/sun/star/lang/IllegalArgumentException.hpp>
|
2013-12-23 10:27:49 +01:00
|
|
|
#include <com/sun/star/lang/XComponent.hpp>
|
|
|
|
#include <com/sun/star/lang/XServiceInfo.hpp>
|
|
|
|
#include <com/sun/star/frame/XFrame.hpp>
|
2006-02-09 12:58:54 +00:00
|
|
|
#include <com/sun/star/awt/XVclWindowPeer.hpp>
|
2012-09-29 17:20:22 +02:00
|
|
|
#include <comphelper/processfactory.hxx>
|
2014-01-16 21:28:41 +01:00
|
|
|
#include <cppuhelper/implbase2.hxx>
|
2013-12-23 10:27:49 +01:00
|
|
|
#include <cppuhelper/interfacecontainer.h>
|
2013-10-16 19:25:05 -03:00
|
|
|
#include <cppuhelper/supportsservice.hxx>
|
2010-10-16 03:20:00 -05:00
|
|
|
#include <osl/mutex.hxx>
|
2013-08-21 14:18:40 +02:00
|
|
|
#include <osl/thread.hxx>
|
2013-12-23 10:27:49 +01:00
|
|
|
#include <rtl/ref.hxx>
|
2006-02-09 12:58:54 +00:00
|
|
|
#include <vcl/svapp.hxx>
|
|
|
|
#include <vcl/dialog.hxx>
|
|
|
|
#include <tools/link.hxx>
|
|
|
|
#include <toolkit/helper/vclunohelper.hxx>
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
2013-12-23 10:27:49 +01:00
|
|
|
namespace {
|
2006-02-09 12:58:54 +00:00
|
|
|
|
2013-12-23 10:27:49 +01:00
|
|
|
// the service is implemented as a wrapper to be able to die by refcount
|
|
|
|
// the disposing mechanics is required for java related scenarios
|
2014-01-16 21:28:41 +01:00
|
|
|
class ODocumentCloser : public ::cppu::WeakImplHelper2< ::com::sun::star::lang::XComponent,
|
2013-12-23 10:27:49 +01:00
|
|
|
::com::sun::star::lang::XServiceInfo >
|
|
|
|
{
|
|
|
|
::osl::Mutex m_aMutex;
|
|
|
|
::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > m_xFrame;
|
|
|
|
::cppu::OInterfaceContainerHelper* m_pListenersContainer; // list of listeners
|
|
|
|
|
2014-05-08 11:42:56 +02:00
|
|
|
bool m_bDisposed;
|
2013-12-23 10:27:49 +01:00
|
|
|
|
|
|
|
public:
|
2015-06-01 21:04:06 +01:00
|
|
|
explicit ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments);
|
2014-04-01 19:18:35 +02:00
|
|
|
virtual ~ODocumentCloser();
|
2013-12-23 10:27:49 +01:00
|
|
|
|
|
|
|
// XComponent
|
2014-03-26 16:37:00 +01:00
|
|
|
virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
|
|
|
|
virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
|
|
|
|
virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
|
2013-12-23 10:27:49 +01:00
|
|
|
|
|
|
|
// XServiceInfo
|
2014-03-26 16:37:00 +01:00
|
|
|
virtual OUString SAL_CALL getImplementationName( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
|
2014-04-03 13:52:06 +02:00
|
|
|
virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
|
2014-03-26 16:37:00 +01:00
|
|
|
virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
|
2013-12-23 10:27:49 +01:00
|
|
|
};
|
2006-02-09 12:58:54 +00:00
|
|
|
|
|
|
|
class MainThreadFrameCloserRequest
|
|
|
|
{
|
|
|
|
uno::Reference< frame::XFrame > m_xFrame;
|
|
|
|
|
|
|
|
public:
|
2015-06-01 21:04:06 +01:00
|
|
|
explicit MainThreadFrameCloserRequest( const uno::Reference< frame::XFrame >& xFrame )
|
2006-02-09 12:58:54 +00:00
|
|
|
: m_xFrame( xFrame )
|
|
|
|
{}
|
|
|
|
|
|
|
|
DECL_STATIC_LINK( MainThreadFrameCloserRequest, worker, MainThreadFrameCloserRequest* );
|
|
|
|
|
|
|
|
static void Start( MainThreadFrameCloserRequest* pRequest );
|
|
|
|
};
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2006-02-09 12:58:54 +00:00
|
|
|
void MainThreadFrameCloserRequest::Start( MainThreadFrameCloserRequest* pMTRequest )
|
|
|
|
{
|
|
|
|
if ( pMTRequest )
|
|
|
|
{
|
2013-08-21 14:18:40 +02:00
|
|
|
if ( Application::GetMainThreadIdentifier() == osl::Thread::getCurrentIdentifier() )
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
|
|
|
// this is the main thread
|
|
|
|
worker( NULL, pMTRequest );
|
|
|
|
}
|
|
|
|
else
|
2015-04-28 15:10:58 +02:00
|
|
|
Application::PostUserEvent( LINK( NULL, MainThreadFrameCloserRequest, worker ), pMTRequest );
|
2006-02-09 12:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-05-09 21:35:10 +02:00
|
|
|
IMPL_STATIC_LINK( MainThreadFrameCloserRequest, worker, MainThreadFrameCloserRequest*, pMTRequest )
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
|
|
|
if ( pMTRequest )
|
|
|
|
{
|
|
|
|
if ( pMTRequest->m_xFrame.is() )
|
|
|
|
{
|
|
|
|
// this is the main thread, the solar mutex must be locked
|
2010-10-13 01:47:23 -05:00
|
|
|
SolarMutexGuard aGuard;
|
2006-02-09 12:58:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
uno::Reference< awt::XWindow > xWindow = pMTRequest->m_xFrame->getContainerWindow();
|
|
|
|
uno::Reference< awt::XVclWindowPeer > xWinPeer( xWindow, uno::UNO_QUERY_THROW );
|
|
|
|
|
|
|
|
xWindow->setVisible( sal_False );
|
|
|
|
|
|
|
|
// reparent the window
|
2013-04-07 12:06:47 +02:00
|
|
|
xWinPeer->setProperty( OUString( "PluginParent" ),
|
2006-02-09 12:58:54 +00:00
|
|
|
uno::makeAny( (sal_Int64) 0 ) );
|
|
|
|
|
2014-09-23 11:20:40 +02:00
|
|
|
vcl::Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
|
2006-02-09 12:58:54 +00:00
|
|
|
if ( pWindow )
|
|
|
|
Dialog::EndAllDialogs( pWindow );
|
|
|
|
}
|
|
|
|
catch( uno::Exception& )
|
|
|
|
{
|
|
|
|
// ignore all the errors
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
uno::Reference< util::XCloseable > xCloseable( pMTRequest->m_xFrame, uno::UNO_QUERY_THROW );
|
|
|
|
xCloseable->close( sal_True );
|
|
|
|
}
|
|
|
|
catch( uno::Exception& )
|
|
|
|
{
|
|
|
|
// ignore all the errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete pMTRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-22 11:54:19 +01:00
|
|
|
ODocumentCloser::ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments)
|
2013-12-23 10:27:49 +01:00
|
|
|
: m_pListenersContainer( NULL )
|
2014-05-08 11:42:56 +02:00
|
|
|
, m_bDisposed( false )
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
2014-01-16 21:28:41 +01:00
|
|
|
::osl::MutexGuard aGuard( m_aMutex );
|
|
|
|
if ( !m_refCount )
|
|
|
|
throw uno::RuntimeException(); // the object must be refcounted already!
|
|
|
|
|
|
|
|
sal_Int32 nLen = aArguments.getLength();
|
|
|
|
if ( nLen != 1 )
|
|
|
|
throw lang::IllegalArgumentException(
|
|
|
|
OUString("Wrong count of parameters!" ),
|
|
|
|
uno::Reference< uno::XInterface >(),
|
|
|
|
0 );
|
|
|
|
|
|
|
|
if ( !( aArguments[0] >>= m_xFrame ) || !m_xFrame.is() )
|
|
|
|
throw lang::IllegalArgumentException(
|
|
|
|
OUString("Nonempty reference is expected as the first argument!" ),
|
|
|
|
uno::Reference< uno::XInterface >(),
|
|
|
|
0 );
|
2006-02-09 12:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2006-02-09 12:58:54 +00:00
|
|
|
ODocumentCloser::~ODocumentCloser()
|
|
|
|
{
|
|
|
|
if ( m_pListenersContainer )
|
|
|
|
{
|
|
|
|
delete m_pListenersContainer;
|
|
|
|
m_pListenersContainer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XComponent
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2006-02-09 12:58:54 +00:00
|
|
|
void SAL_CALL ODocumentCloser::dispose()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (uno::RuntimeException, std::exception)
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( m_aMutex );
|
|
|
|
|
|
|
|
if ( m_bDisposed )
|
|
|
|
throw lang::DisposedException();
|
|
|
|
|
|
|
|
lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
|
|
|
|
if ( m_pListenersContainer )
|
|
|
|
m_pListenersContainer->disposeAndClear( aSource );
|
|
|
|
|
|
|
|
// TODO: trigger a main thread execution to close the frame
|
|
|
|
if ( m_xFrame.is() )
|
|
|
|
{
|
|
|
|
// the created object will be deleted after thread execution
|
|
|
|
MainThreadFrameCloserRequest* pCloser = new MainThreadFrameCloserRequest( m_xFrame );
|
|
|
|
MainThreadFrameCloserRequest::Start( pCloser );
|
|
|
|
}
|
|
|
|
|
2014-05-08 11:42:56 +02:00
|
|
|
m_bDisposed = true;
|
2006-02-09 12:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2006-02-09 12:58:54 +00:00
|
|
|
void SAL_CALL ODocumentCloser::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (uno::RuntimeException, std::exception)
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( m_aMutex );
|
|
|
|
if ( m_bDisposed )
|
|
|
|
throw lang::DisposedException(); // TODO
|
|
|
|
|
|
|
|
if ( !m_pListenersContainer )
|
|
|
|
m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
|
|
|
|
|
|
|
|
m_pListenersContainer->addInterface( xListener );
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2006-02-09 12:58:54 +00:00
|
|
|
void SAL_CALL ODocumentCloser::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (uno::RuntimeException, std::exception)
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( m_aMutex );
|
|
|
|
if ( m_pListenersContainer )
|
|
|
|
m_pListenersContainer->removeInterface( xListener );
|
|
|
|
}
|
|
|
|
|
|
|
|
// XServiceInfo
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString SAL_CALL ODocumentCloser::getImplementationName( )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (uno::RuntimeException, std::exception)
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
2013-12-23 10:27:49 +01:00
|
|
|
return OUString( "com.sun.star.comp.embed.DocumentCloser" );
|
2006-02-09 12:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 13:52:06 +02:00
|
|
|
sal_Bool SAL_CALL ODocumentCloser::supportsService( const OUString& ServiceName )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (uno::RuntimeException, std::exception)
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
2013-10-16 19:25:05 -03:00
|
|
|
return cppu::supportsService(this, ServiceName);
|
2006-02-09 12:58:54 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
uno::Sequence< OUString > SAL_CALL ODocumentCloser::getSupportedServiceNames()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (uno::RuntimeException, std::exception)
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
2013-04-07 12:06:47 +02:00
|
|
|
const OUString aServiceName( "com.sun.star.embed.DocumentCloser" );
|
|
|
|
return uno::Sequence< OUString >( &aServiceName, 1 );
|
2006-02-09 12:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-23 10:27:49 +01:00
|
|
|
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
|
|
|
|
com_sun_star_comp_embed_DocumentCloser_get_implementation(
|
2014-01-22 11:54:19 +01:00
|
|
|
SAL_UNUSED_PARAMETER css::uno::XComponentContext *,
|
|
|
|
css::uno::Sequence<css::uno::Any> const &arguments)
|
2006-02-09 12:58:54 +00:00
|
|
|
{
|
2014-01-22 11:54:19 +01:00
|
|
|
return cppu::acquire(new ODocumentCloser(arguments));
|
2006-02-09 12:58:54 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 08:27:31 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|