1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_embeddedobj.hxx"
30 
31 #include "mainthreadexecutor.hxx"
32 
33 #include <vcl/svapp.hxx>
34 
35 using namespace ::com::sun::star;
36 
37 //-------------------------------------------------------------------------
38 uno::Sequence< ::rtl::OUString > SAL_CALL MainThreadExecutor::impl_staticGetSupportedServiceNames()
39 {
40     uno::Sequence< ::rtl::OUString > aRet(2);
41     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.thread.MainThreadExecutor");
42     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.thread.MainThreadExecutor");
43     return aRet;
44 }
45 
46 //-------------------------------------------------------------------------
47 ::rtl::OUString SAL_CALL MainThreadExecutor::impl_staticGetImplementationName()
48 {
49     return ::rtl::OUString::createFromAscii("com.sun.star.comp.thread.MainThreadExecutor");
50 }
51 
52 //-------------------------------------------------------------------------
53 uno::Reference< uno::XInterface > SAL_CALL MainThreadExecutor::impl_staticCreateSelfInstance(
54 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
55 {
56 	return uno::Reference< uno::XInterface >( *new MainThreadExecutor( xServiceManager ) );
57 }
58 
59 //-------------------------------------------------------------------------
60 uno::Any SAL_CALL MainThreadExecutor::execute( const uno::Sequence< beans::NamedValue >& aArguments )
61 	throw ( lang::IllegalArgumentException,
62 			uno::Exception,
63 			uno::RuntimeException )
64 {
65 	uno::Reference< task::XJob > xJob;
66 	uno::Sequence< beans::NamedValue > aValues;
67 	sal_Int32 nValuesSize = 0;
68 
69 	for ( sal_Int32 nInd = 0; nInd < aArguments.getLength(); nInd++ )
70 		if ( aArguments[nInd].Name.equalsAscii( "JobToExecute" ) )
71 			aArguments[nInd].Value >>= xJob;
72 		else
73 		{
74 			aValues.realloc( ++nValuesSize );
75 			aValues[nValuesSize-1].Name = aArguments[nInd].Name;
76 			aValues[nValuesSize-1].Value = aArguments[nInd].Value;
77 		}
78 
79 	if ( xJob.is() )
80 	{
81 		MainThreadExecutorRequest* pMainThreadExecutorRequest = new MainThreadExecutorRequest( xJob, aValues );
82 		Application::PostUserEvent( STATIC_LINK( NULL, MainThreadExecutor, worker ), pMainThreadExecutorRequest );
83 	}
84 
85 	// TODO: wait for result
86 	return uno::Any();
87 }
88 
89 //-------------------------------------------------------------------------
90 IMPL_STATIC_LINK( MainThreadExecutor, worker, MainThreadExecutorRequest*, pThreadExecutorRequest )
91 {
92 	pThreadExecutorRequest->doIt();
93 
94 	delete pThreadExecutorRequest;
95 	return 0;
96 }
97 
98 //-------------------------------------------------------------------------
99 ::rtl::OUString SAL_CALL MainThreadExecutor::getImplementationName()
100 		throw ( uno::RuntimeException )
101 {
102 	return impl_staticGetImplementationName();
103 }
104 
105 //-------------------------------------------------------------------------
106 sal_Bool SAL_CALL MainThreadExecutor::supportsService( const ::rtl::OUString& ServiceName )
107 		throw ( uno::RuntimeException )
108 {
109 	uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
110 
111 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
112     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
113         	return sal_True;
114 
115 	return sal_False;
116 }
117 
118 //-------------------------------------------------------------------------
119 uno::Sequence< ::rtl::OUString > SAL_CALL MainThreadExecutor::getSupportedServiceNames()
120 		throw ( uno::RuntimeException )
121 {
122 	return impl_staticGetSupportedServiceNames();
123 }
124 
125 //-------------------------------------------------------------------------
126 MainThreadExecutorRequest::MainThreadExecutorRequest( const uno::Reference< task::XJob >& xJob,
127 													const uno::Sequence< beans::NamedValue >& aValues )
128 : m_xJob( xJob )
129 , m_aValues( aValues )
130 {
131 }
132 
133 //-------------------------------------------------------------------------
134 void MainThreadExecutorRequest::doIt()
135 {
136 	if ( m_xJob.is() )
137 		m_xJob->execute( m_aValues );
138 }
139 
140