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_toolkit.hxx"
30 
31 #include "vcl/svapp.hxx"
32 #include "vos/mutex.hxx"
33 #include "sal/config.h"
34 #include "cppuhelper/factory.hxx"
35 #include "cppuhelper/implementationentry.hxx"
36 #include "cppuhelper/implbase2.hxx"
37 #include "com/sun/star/lang/XServiceInfo.hpp"
38 #include "com/sun/star/awt/XRequestCallback.hpp"
39 
40 
41 // component helper namespace
42 namespace comp_AsyncCallback {
43 
44 namespace css = ::com::sun::star;
45 
46 // component and service helper functions:
47 ::rtl::OUString SAL_CALL _getImplementationName();
48 css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames();
49 css::uno::Reference< css::uno::XInterface > SAL_CALL _create( css::uno::Reference< css::uno::XComponentContext > const & context );
50 
51 } // closing component helper namespace
52 
53 
54 
55 /// anonymous implementation namespace
56 namespace {
57 
58 namespace css = ::com::sun::star;
59 
60 class AsyncCallback:
61     public ::cppu::WeakImplHelper2<
62         css::lang::XServiceInfo,
63         css::awt::XRequestCallback>
64 {
65 public:
66     explicit AsyncCallback(css::uno::Reference< css::uno::XComponentContext > const & context);
67 
68     // ::com::sun::star::lang::XServiceInfo:
69     virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException);
70     virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException);
71     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException);
72 
73     // ::com::sun::star::awt::XRequestCallback:
74     virtual void SAL_CALL addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const ::com::sun::star::uno::Any & aData) throw (css::uno::RuntimeException);
75 
76 private:
77 
78     struct CallbackData
79     {
80         CallbackData( const css::uno::Reference< css::awt::XCallback >& rCallback, const css::uno::Any& rAny ) :
81             xCallback( rCallback ), aData( rAny ) {}
82 
83         css::uno::Reference< css::awt::XCallback > xCallback;
84         css::uno::Any                              aData;
85     };
86 
87     DECL_STATIC_LINK( AsyncCallback, Notify_Impl, CallbackData* );
88 
89     AsyncCallback(AsyncCallback &); // not defined
90     void operator =(AsyncCallback &); // not defined
91 
92     virtual ~AsyncCallback() {}
93 
94     css::uno::Reference< css::uno::XComponentContext > m_xContext;
95 };
96 
97 AsyncCallback::AsyncCallback(css::uno::Reference< css::uno::XComponentContext > const & context) :
98     m_xContext(context)
99 {}
100 
101 // com.sun.star.uno.XServiceInfo:
102 ::rtl::OUString SAL_CALL AsyncCallback::getImplementationName() throw (css::uno::RuntimeException)
103 {
104     return comp_AsyncCallback::_getImplementationName();
105 }
106 
107 ::sal_Bool SAL_CALL AsyncCallback::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException)
108 {
109     const css::uno::Sequence< ::rtl::OUString > serviceNames = comp_AsyncCallback::_getSupportedServiceNames();
110     for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
111         if (serviceNames[i] == serviceName)
112             return sal_True;
113     }
114     return sal_False;
115 }
116 
117 css::uno::Sequence< ::rtl::OUString > SAL_CALL AsyncCallback::getSupportedServiceNames() throw (css::uno::RuntimeException)
118 {
119     return comp_AsyncCallback::_getSupportedServiceNames();
120 }
121 
122 // ::com::sun::star::awt::XRequestCallback:
123 void SAL_CALL AsyncCallback::addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const ::com::sun::star::uno::Any & aData) throw (css::uno::RuntimeException)
124 {
125     if ( Application::IsInMain() )
126     {
127         osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
128         CallbackData* pCallbackData = new CallbackData( xCallback, aData );
129         Application::PostUserEvent( STATIC_LINK( this, AsyncCallback, Notify_Impl ), pCallbackData );
130     }
131 }
132 
133 // private asynchronous link to call reference to the callback object
134 IMPL_STATIC_LINK_NOINSTANCE( AsyncCallback, Notify_Impl, CallbackData*, pCallbackData )
135 {
136     try
137     {
138         // Asynchronous execution
139         // Check pointer and reference before!
140         if ( pCallbackData && pCallbackData->xCallback.is() )
141             pCallbackData->xCallback->notify( pCallbackData->aData );
142     }
143     catch ( css::uno::Exception& )
144     {
145     }
146 
147     delete pCallbackData;
148     return 0;
149 }
150 
151 } // closing anonymous implementation namespace
152 
153 
154 
155 // component helper namespace
156 namespace comp_AsyncCallback {
157 
158 ::rtl::OUString SAL_CALL _getImplementationName() {
159     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
160         "com.sun.star.awt.comp.AsyncCallback"));
161 }
162 
163 css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
164 {
165     css::uno::Sequence< ::rtl::OUString > s(1);
166     s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
167         "com.sun.star.awt.AsyncCallback"));
168     return s;
169 }
170 
171 css::uno::Reference< css::uno::XInterface > SAL_CALL _create(
172     const css::uno::Reference< css::uno::XComponentContext > & context)
173         SAL_THROW((css::uno::Exception))
174 {
175     return static_cast< ::cppu::OWeakObject * >(new AsyncCallback(context));
176 }
177 
178 } // closing component helper namespace
179 
180 static ::cppu::ImplementationEntry const entries[] = {
181     { &comp_AsyncCallback::_create,
182       &comp_AsyncCallback::_getImplementationName,
183       &comp_AsyncCallback::_getSupportedServiceNames,
184       &::cppu::createSingleComponentFactory, 0, 0 },
185     { 0, 0, 0, 0, 0, 0 }
186 };
187 
188 void * SAL_CALL comp_AsyncCallback_component_getFactory(
189     const char * implName, void * serviceManager, void * registryKey)
190 {
191     return ::cppu::component_getFactoryHelper(
192         implName, serviceManager, registryKey, entries);
193 }
194