1*b0724fc6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b0724fc6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b0724fc6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b0724fc6SAndrew Rist  * distributed with this work for additional information
6*b0724fc6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b0724fc6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b0724fc6SAndrew Rist  * "License"); you may not use this file except in compliance
9*b0724fc6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b0724fc6SAndrew Rist  *
11*b0724fc6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b0724fc6SAndrew Rist  *
13*b0724fc6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b0724fc6SAndrew Rist  * software distributed under the License is distributed on an
15*b0724fc6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b0724fc6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b0724fc6SAndrew Rist  * specific language governing permissions and limitations
18*b0724fc6SAndrew Rist  * under the License.
19*b0724fc6SAndrew Rist  *
20*b0724fc6SAndrew Rist  *************************************************************/
21*b0724fc6SAndrew Rist 
22*b0724fc6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_toolkit.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "vcl/svapp.hxx"
28cdf0e10cSrcweir #include "vos/mutex.hxx"
29cdf0e10cSrcweir #include "sal/config.h"
30cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
31cdf0e10cSrcweir #include "cppuhelper/implementationentry.hxx"
32cdf0e10cSrcweir #include "cppuhelper/implbase2.hxx"
33cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
34cdf0e10cSrcweir #include "com/sun/star/awt/XRequestCallback.hpp"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // component helper namespace
38cdf0e10cSrcweir namespace comp_AsyncCallback {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace css = ::com::sun::star;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir // component and service helper functions:
43cdf0e10cSrcweir ::rtl::OUString SAL_CALL _getImplementationName();
44cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames();
45cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > SAL_CALL _create( css::uno::Reference< css::uno::XComponentContext > const & context );
46cdf0e10cSrcweir 
47cdf0e10cSrcweir } // closing component helper namespace
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir /// anonymous implementation namespace
52cdf0e10cSrcweir namespace {
53cdf0e10cSrcweir 
54cdf0e10cSrcweir namespace css = ::com::sun::star;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir class AsyncCallback:
57cdf0e10cSrcweir     public ::cppu::WeakImplHelper2<
58cdf0e10cSrcweir         css::lang::XServiceInfo,
59cdf0e10cSrcweir         css::awt::XRequestCallback>
60cdf0e10cSrcweir {
61cdf0e10cSrcweir public:
62cdf0e10cSrcweir     explicit AsyncCallback(css::uno::Reference< css::uno::XComponentContext > const & context);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     // ::com::sun::star::lang::XServiceInfo:
65cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException);
66cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException);
67cdf0e10cSrcweir     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     // ::com::sun::star::awt::XRequestCallback:
70cdf0e10cSrcweir     virtual void SAL_CALL addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const ::com::sun::star::uno::Any & aData) throw (css::uno::RuntimeException);
71cdf0e10cSrcweir 
72cdf0e10cSrcweir private:
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     struct CallbackData
75cdf0e10cSrcweir     {
CallbackData__anon1c97f6920111::AsyncCallback::CallbackData76cdf0e10cSrcweir         CallbackData( const css::uno::Reference< css::awt::XCallback >& rCallback, const css::uno::Any& rAny ) :
77cdf0e10cSrcweir             xCallback( rCallback ), aData( rAny ) {}
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         css::uno::Reference< css::awt::XCallback > xCallback;
80cdf0e10cSrcweir         css::uno::Any                              aData;
81cdf0e10cSrcweir     };
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     DECL_STATIC_LINK( AsyncCallback, Notify_Impl, CallbackData* );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     AsyncCallback(AsyncCallback &); // not defined
86cdf0e10cSrcweir     void operator =(AsyncCallback &); // not defined
87cdf0e10cSrcweir 
~AsyncCallback()88cdf0e10cSrcweir     virtual ~AsyncCallback() {}
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > m_xContext;
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
AsyncCallback(css::uno::Reference<css::uno::XComponentContext> const & context)93cdf0e10cSrcweir AsyncCallback::AsyncCallback(css::uno::Reference< css::uno::XComponentContext > const & context) :
94cdf0e10cSrcweir     m_xContext(context)
95cdf0e10cSrcweir {}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir // com.sun.star.uno.XServiceInfo:
getImplementationName()98cdf0e10cSrcweir ::rtl::OUString SAL_CALL AsyncCallback::getImplementationName() throw (css::uno::RuntimeException)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     return comp_AsyncCallback::_getImplementationName();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
supportsService(::rtl::OUString const & serviceName)103cdf0e10cSrcweir ::sal_Bool SAL_CALL AsyncCallback::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     const css::uno::Sequence< ::rtl::OUString > serviceNames = comp_AsyncCallback::_getSupportedServiceNames();
106cdf0e10cSrcweir     for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
107cdf0e10cSrcweir         if (serviceNames[i] == serviceName)
108cdf0e10cSrcweir             return sal_True;
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir     return sal_False;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
getSupportedServiceNames()113cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > SAL_CALL AsyncCallback::getSupportedServiceNames() throw (css::uno::RuntimeException)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     return comp_AsyncCallback::_getSupportedServiceNames();
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir // ::com::sun::star::awt::XRequestCallback:
addCallback(const css::uno::Reference<css::awt::XCallback> & xCallback,const::com::sun::star::uno::Any & aData)119cdf0e10cSrcweir void SAL_CALL AsyncCallback::addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const ::com::sun::star::uno::Any & aData) throw (css::uno::RuntimeException)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     if ( Application::IsInMain() )
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
124cdf0e10cSrcweir         CallbackData* pCallbackData = new CallbackData( xCallback, aData );
125cdf0e10cSrcweir         Application::PostUserEvent( STATIC_LINK( this, AsyncCallback, Notify_Impl ), pCallbackData );
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir // private asynchronous link to call reference to the callback object
IMPL_STATIC_LINK_NOINSTANCE(AsyncCallback,Notify_Impl,CallbackData *,pCallbackData)130cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( AsyncCallback, Notify_Impl, CallbackData*, pCallbackData )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     try
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         // Asynchronous execution
135cdf0e10cSrcweir         // Check pointer and reference before!
136cdf0e10cSrcweir         if ( pCallbackData && pCallbackData->xCallback.is() )
137cdf0e10cSrcweir             pCallbackData->xCallback->notify( pCallbackData->aData );
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir     catch ( css::uno::Exception& )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     delete pCallbackData;
144cdf0e10cSrcweir     return 0;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir } // closing anonymous implementation namespace
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir // component helper namespace
152cdf0e10cSrcweir namespace comp_AsyncCallback {
153cdf0e10cSrcweir 
_getImplementationName()154cdf0e10cSrcweir ::rtl::OUString SAL_CALL _getImplementationName() {
155cdf0e10cSrcweir     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
156cdf0e10cSrcweir         "com.sun.star.awt.comp.AsyncCallback"));
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
_getSupportedServiceNames()159cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
160cdf0e10cSrcweir {
161cdf0e10cSrcweir     css::uno::Sequence< ::rtl::OUString > s(1);
162cdf0e10cSrcweir     s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
163cdf0e10cSrcweir         "com.sun.star.awt.AsyncCallback"));
164cdf0e10cSrcweir     return s;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
_create(const css::uno::Reference<css::uno::XComponentContext> & context)167cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > SAL_CALL _create(
168cdf0e10cSrcweir     const css::uno::Reference< css::uno::XComponentContext > & context)
169cdf0e10cSrcweir         SAL_THROW((css::uno::Exception))
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >(new AsyncCallback(context));
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir } // closing component helper namespace
175cdf0e10cSrcweir 
176cdf0e10cSrcweir static ::cppu::ImplementationEntry const entries[] = {
177cdf0e10cSrcweir     { &comp_AsyncCallback::_create,
178cdf0e10cSrcweir       &comp_AsyncCallback::_getImplementationName,
179cdf0e10cSrcweir       &comp_AsyncCallback::_getSupportedServiceNames,
180cdf0e10cSrcweir       &::cppu::createSingleComponentFactory, 0, 0 },
181cdf0e10cSrcweir     { 0, 0, 0, 0, 0, 0 }
182cdf0e10cSrcweir };
183cdf0e10cSrcweir 
comp_AsyncCallback_component_getFactory(const char * implName,void * serviceManager,void * registryKey)184cdf0e10cSrcweir void * SAL_CALL comp_AsyncCallback_component_getFactory(
185cdf0e10cSrcweir     const char * implName, void * serviceManager, void * registryKey)
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     return ::cppu::component_getFactoryHelper(
188cdf0e10cSrcweir         implName, serviceManager, registryKey, entries);
189cdf0e10cSrcweir }
190