1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2008 by Sun Microsystems, Inc.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * $RCSfile: urltransformer.cxx,v $
10  * $Revision: 1.17 $
11  *
12  * This file is part of OpenOffice.org.
13  *
14  * OpenOffice.org is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License version 3
16  * only, as published by the Free Software Foundation.
17  *
18  * OpenOffice.org is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU Lesser General Public License version 3 for more details
22  * (a copy is included in the LICENSE file that accompanied this code).
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * version 3 along with OpenOffice.org.  If not, see
26  * <http://www.openoffice.org/license.html>
27  * for a copy of the LGPLv3 License.
28  *
29  ************************************************************************/
30 
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 
34 //_________________________________________________________________________________________________________________
35 //	my own includes
36 //_________________________________________________________________________________________________________________
37 
38 #include "services.h"
39 #include "services/modelwinservice.hxx"
40 
41 //_________________________________________________________________________________________________________________
42 //	interface includes
43 //_________________________________________________________________________________________________________________
44 
45 #include <com/sun/star/awt/XControlModel.hpp>
46 
47 using namespace ::com::sun::star;
48 
49 //_________________________________________________________________________________________________________________
50 //	namespace
51 //_________________________________________________________________________________________________________________
52 
53 namespace framework{
54 
55 //_________________________________________________________________________________________________________________
56 //	non exported definitions
57 //_________________________________________________________________________________________________________________
58 
59 //_________________________________________________________________________________________________________________
60 //	declarations
61 //_________________________________________________________________________________________________________________
62 
63 class Impl_ModelWinService
64 {
65     public:
66         ~Impl_ModelWinService();
67 
68         static Impl_ModelWinService* getSingleInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager );
69 
70         uno::Any getByName( const ::rtl::OUString& sName )
71             throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException );
72 
73         uno::Sequence< ::rtl::OUString > getElementNames()
74             throw( uno::RuntimeException );
75 
76         sal_Bool hasByName( const ::rtl::OUString& sName )
77             throw( uno::RuntimeException );
78 
79         uno::Type getElementType()
80             throw( css::uno::RuntimeException );
81 
82         sal_Bool hasElements()
83             throw( css::uno::RuntimeException );
84 
85         void registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel );
86 
87         void deregisterModelForXWindow( const uno::Reference< awt::XWindow >& rWindow );
88 
89     private:
90         typedef BaseHash< uno::WeakReference< awt::XControlModel > > ModelWinMap;
91 
92         Impl_ModelWinService();
93         Impl_ModelWinService( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager );
94 
95         static Impl_ModelWinService* m_pModelWinService;
96 
97         ::com::sun::star::uno::WeakReference< ::com::sun::star::lang::XMultiServiceFactory > m_xServiceManager;
98         ModelWinMap m_aModelMap;
99 };
100 
101 Impl_ModelWinService* Impl_ModelWinService::m_pModelWinService = 0;
102 
103 Impl_ModelWinService* Impl_ModelWinService::getSingleInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager )
104 {
105     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
106     if ( !m_pModelWinService )
107         m_pModelWinService = new Impl_ModelWinService( rServiceManager );
108     return m_pModelWinService;
109 }
110 
111 Impl_ModelWinService::Impl_ModelWinService( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager ) :
112     m_xServiceManager( rServiceManager )
113 {
114 }
115 
116 Impl_ModelWinService::Impl_ModelWinService()
117 {
118 }
119 
120 Impl_ModelWinService::~Impl_ModelWinService()
121 {
122 }
123 
124 void Impl_ModelWinService::registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel )
125 {
126     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
127 
128     ::rtl::OUString sName = rtl::OUString::valueOf( reinterpret_cast< sal_Int64 >((void*)rWindow.get()));
129     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
130     if ( pIter != m_aModelMap.end() )
131         pIter->second = rModel;
132     else
133         m_aModelMap[sName] = rModel;
134 }
135 
136 void Impl_ModelWinService::deregisterModelForXWindow( const uno::Reference< awt::XWindow >& /*rWindow*/ )
137 {
138 }
139 
140 uno::Any Impl_ModelWinService::getByName( const ::rtl::OUString& sName )
141 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
142 {
143     uno::Any aAny;
144 
145     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
146     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
147     if ( pIter != m_aModelMap.end())
148     {
149         uno::Reference< awt::XControlModel > xModel( pIter->second );
150         aAny = uno::makeAny(xModel);
151     }
152 
153     return aAny;
154 }
155 
156 uno::Sequence< ::rtl::OUString > Impl_ModelWinService::getElementNames()
157 throw( uno::RuntimeException )
158 {
159     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
160     uno::Sequence< ::rtl::OUString > aResult( m_aModelMap.size() );
161 
162     sal_Int32 i = 0;
163     ModelWinMap::const_iterator pIter = m_aModelMap.begin();
164     while ( pIter != m_aModelMap.end())
165         aResult[i++] = pIter->first;
166 
167     return aResult;
168 }
169 
170 sal_Bool Impl_ModelWinService::hasByName( const ::rtl::OUString& sName )
171 throw( uno::RuntimeException )
172 {
173     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
174     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
175     if ( pIter != m_aModelMap.end())
176         return true;
177     else
178         return false;
179 }
180 
181 uno::Type Impl_ModelWinService::getElementType()
182 throw( css::uno::RuntimeException )
183 {
184     return ::getCppuType(( const uno::Reference< awt::XControlModel >*)NULL );
185 }
186 
187 sal_Bool Impl_ModelWinService::hasElements()
188 throw( css::uno::RuntimeException )
189 {
190     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
191     return (m_aModelMap.size() > 0);
192 }
193 
194 //*****************************************************************************************************************
195 //	css::uno::XInterface, XTypeProvider, XServiceInfo
196 //*****************************************************************************************************************
197 
198 DEFINE_XINTERFACE_4                 (   ModelWinService                                 ,
199                                         OWeakObject                                     ,
200                                         DIRECT_INTERFACE(css::lang::XTypeProvider       ),
201                                         DIRECT_INTERFACE(css::lang::XServiceInfo        ),
202                                         DIRECT_INTERFACE(css::container::XNameAccess    ),
203                                         DIRECT_INTERFACE(css::container::XElementAccess )
204 									)
205 
206 DEFINE_XTYPEPROVIDER_4              (   ModelWinService                ,
207                                         css::lang::XTypeProvider       ,
208                                         css::lang::XServiceInfo        ,
209                                         css::container::XNameAccess    ,
210                                         css::container::XElementAccess
211 									)
212 
213 DEFINE_XSERVICEINFO_MULTISERVICE    (   ModelWinService                    ,
214                                         OWeakObject                        ,
215                                         SERVICENAME_MODELWINSERVICE        ,
216                                         IMPLEMENTATIONNAME_MODELWINSERVICE
217 									)
218 
219 DEFINE_INIT_SERVICE                 (   ModelWinService,
220                                         {
221                                         }
222                                     )
223 
224 //*****************************************************************************************************************
225 //	constructor
226 //*****************************************************************************************************************
227 ModelWinService::ModelWinService(const uno::Reference< lang::XMultiServiceFactory >& rServiceManager ) :
228     m_xServiceManager( rServiceManager )
229 {
230 }
231 
232 ModelWinService::~ModelWinService()
233 {
234 }
235 
236 void ModelWinService::registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel )
237 {
238     Impl_ModelWinService::getSingleInstance(m_xServiceManager)->registerModelForXWindow( rWindow, rModel );
239 }
240 
241 void ModelWinService::deregisterModelForXWindow( const uno::Reference< awt::XWindow >& rWindow )
242 {
243     Impl_ModelWinService::getSingleInstance(m_xServiceManager)->deregisterModelForXWindow( rWindow );
244 }
245 
246 uno::Any SAL_CALL ModelWinService::getByName( const ::rtl::OUString& sName )
247 throw(	container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
248 {
249     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->getByName( sName );
250 }
251 
252 uno::Sequence< ::rtl::OUString > SAL_CALL ModelWinService::getElementNames()
253 throw( uno::RuntimeException )
254 {
255     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->getElementNames( );
256 }
257 
258 sal_Bool SAL_CALL ModelWinService::hasByName( const ::rtl::OUString& sName )
259 throw( uno::RuntimeException )
260 {
261     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->hasByName( sName );
262 }
263 
264 //---------------------------------------------------------------------------------------------------------
265 //	XElementAccess
266 //---------------------------------------------------------------------------------------------------------
267 uno::Type SAL_CALL ModelWinService::getElementType()
268 throw( uno::RuntimeException )
269 {
270     return ::getCppuType( (const uno::Reference< awt::XControlModel > *)NULL );
271 }
272 
273 sal_Bool SAL_CALL ModelWinService::hasElements()
274 throw( uno::RuntimeException )
275 {
276     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->hasElements();
277 }
278 
279 }
280