1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_embeddedobj.hxx"
26 
27 #include "mainthreadexecutor.hxx"
28 
29 #include <vcl/svapp.hxx>
30 
31 using namespace ::com::sun::star;
32 
33 //-------------------------------------------------------------------------
impl_staticGetSupportedServiceNames()34 uno::Sequence< ::rtl::OUString > SAL_CALL MainThreadExecutor::impl_staticGetSupportedServiceNames()
35 {
36     uno::Sequence< ::rtl::OUString > aRet(2);
37     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.thread.MainThreadExecutor");
38     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.thread.MainThreadExecutor");
39     return aRet;
40 }
41 
42 //-------------------------------------------------------------------------
impl_staticGetImplementationName()43 ::rtl::OUString SAL_CALL MainThreadExecutor::impl_staticGetImplementationName()
44 {
45     return ::rtl::OUString::createFromAscii("com.sun.star.comp.thread.MainThreadExecutor");
46 }
47 
48 //-------------------------------------------------------------------------
impl_staticCreateSelfInstance(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)49 uno::Reference< uno::XInterface > SAL_CALL MainThreadExecutor::impl_staticCreateSelfInstance(
50 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
51 {
52 	return uno::Reference< uno::XInterface >( *new MainThreadExecutor( xServiceManager ) );
53 }
54 
55 //-------------------------------------------------------------------------
execute(const uno::Sequence<beans::NamedValue> & aArguments)56 uno::Any SAL_CALL MainThreadExecutor::execute( const uno::Sequence< beans::NamedValue >& aArguments )
57 	throw ( lang::IllegalArgumentException,
58 			uno::Exception,
59 			uno::RuntimeException )
60 {
61 	uno::Reference< task::XJob > xJob;
62 	uno::Sequence< beans::NamedValue > aValues;
63 	sal_Int32 nValuesSize = 0;
64 
65 	for ( sal_Int32 nInd = 0; nInd < aArguments.getLength(); nInd++ )
66 		if ( aArguments[nInd].Name.equalsAscii( "JobToExecute" ) )
67 			aArguments[nInd].Value >>= xJob;
68 		else
69 		{
70 			aValues.realloc( ++nValuesSize );
71 			aValues[nValuesSize-1].Name = aArguments[nInd].Name;
72 			aValues[nValuesSize-1].Value = aArguments[nInd].Value;
73 		}
74 
75 	if ( xJob.is() )
76 	{
77 		MainThreadExecutorRequest* pMainThreadExecutorRequest = new MainThreadExecutorRequest( xJob, aValues );
78 		Application::PostUserEvent( STATIC_LINK( NULL, MainThreadExecutor, worker ), pMainThreadExecutorRequest );
79 	}
80 
81 	// TODO: wait for result
82 	return uno::Any();
83 }
84 
85 //-------------------------------------------------------------------------
IMPL_STATIC_LINK(MainThreadExecutor,worker,MainThreadExecutorRequest *,pThreadExecutorRequest)86 IMPL_STATIC_LINK( MainThreadExecutor, worker, MainThreadExecutorRequest*, pThreadExecutorRequest )
87 {
88 	pThreadExecutorRequest->doIt();
89 
90 	delete pThreadExecutorRequest;
91 	return 0;
92 }
93 
94 //-------------------------------------------------------------------------
getImplementationName()95 ::rtl::OUString SAL_CALL MainThreadExecutor::getImplementationName()
96 		throw ( uno::RuntimeException )
97 {
98 	return impl_staticGetImplementationName();
99 }
100 
101 //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)102 sal_Bool SAL_CALL MainThreadExecutor::supportsService( const ::rtl::OUString& ServiceName )
103 		throw ( uno::RuntimeException )
104 {
105 	uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
106 
107 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
108     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
109         	return sal_True;
110 
111 	return sal_False;
112 }
113 
114 //-------------------------------------------------------------------------
getSupportedServiceNames()115 uno::Sequence< ::rtl::OUString > SAL_CALL MainThreadExecutor::getSupportedServiceNames()
116 		throw ( uno::RuntimeException )
117 {
118 	return impl_staticGetSupportedServiceNames();
119 }
120 
121 //-------------------------------------------------------------------------
MainThreadExecutorRequest(const uno::Reference<task::XJob> & xJob,const uno::Sequence<beans::NamedValue> & aValues)122 MainThreadExecutorRequest::MainThreadExecutorRequest( const uno::Reference< task::XJob >& xJob,
123 													const uno::Sequence< beans::NamedValue >& aValues )
124 : m_xJob( xJob )
125 , m_aValues( aValues )
126 {
127 }
128 
129 //-------------------------------------------------------------------------
doIt()130 void MainThreadExecutorRequest::doIt()
131 {
132 	if ( m_xJob.is() )
133 		m_xJob->execute( m_aValues );
134 }
135 
136