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_framework.hxx"
26 
27 //_________________________________________________________________________________________________________________
28 //	my own includes
29 //_________________________________________________________________________________________________________________
30 
31 #include "services.h"
32 #include "services/modelwinservice.hxx"
33 
34 //_________________________________________________________________________________________________________________
35 //	interface includes
36 //_________________________________________________________________________________________________________________
37 
38 #include <com/sun/star/awt/XControlModel.hpp>
39 
40 using namespace ::com::sun::star;
41 
42 //_________________________________________________________________________________________________________________
43 //	namespace
44 //_________________________________________________________________________________________________________________
45 
46 namespace framework{
47 
48 //_________________________________________________________________________________________________________________
49 //	non exported definitions
50 //_________________________________________________________________________________________________________________
51 
52 //_________________________________________________________________________________________________________________
53 //	declarations
54 //_________________________________________________________________________________________________________________
55 
56 class Impl_ModelWinService
57 {
58     public:
59         ~Impl_ModelWinService();
60 
61         static Impl_ModelWinService* getSingleInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager );
62 
63         uno::Any getByName( const ::rtl::OUString& sName )
64             throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException );
65 
66         uno::Sequence< ::rtl::OUString > getElementNames()
67             throw( uno::RuntimeException );
68 
69         sal_Bool hasByName( const ::rtl::OUString& sName )
70             throw( uno::RuntimeException );
71 
72         uno::Type getElementType()
73             throw( css::uno::RuntimeException );
74 
75         sal_Bool hasElements()
76             throw( css::uno::RuntimeException );
77 
78         void registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel );
79 
80         void deregisterModelForXWindow( const uno::Reference< awt::XWindow >& rWindow );
81 
82     private:
83         typedef BaseHash< uno::WeakReference< awt::XControlModel > > ModelWinMap;
84 
85         Impl_ModelWinService();
86         Impl_ModelWinService( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager );
87 
88         static Impl_ModelWinService* m_pModelWinService;
89 
90         ::com::sun::star::uno::WeakReference< ::com::sun::star::lang::XMultiServiceFactory > m_xServiceManager;
91         ModelWinMap m_aModelMap;
92 };
93 
94 Impl_ModelWinService* Impl_ModelWinService::m_pModelWinService = 0;
95 
getSingleInstance(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & rServiceManager)96 Impl_ModelWinService* Impl_ModelWinService::getSingleInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager )
97 {
98     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
99     if ( !m_pModelWinService )
100         m_pModelWinService = new Impl_ModelWinService( rServiceManager );
101     return m_pModelWinService;
102 }
103 
Impl_ModelWinService(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & rServiceManager)104 Impl_ModelWinService::Impl_ModelWinService( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager ) :
105     m_xServiceManager( rServiceManager )
106 {
107 }
108 
Impl_ModelWinService()109 Impl_ModelWinService::Impl_ModelWinService()
110 {
111 }
112 
~Impl_ModelWinService()113 Impl_ModelWinService::~Impl_ModelWinService()
114 {
115 }
116 
registerModelForXWindow(const uno::Reference<awt::XWindow> & rWindow,const uno::Reference<awt::XControlModel> & rModel)117 void Impl_ModelWinService::registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel )
118 {
119     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
120 
121     ::rtl::OUString sName = rtl::OUString::valueOf( reinterpret_cast< sal_Int64 >((void*)rWindow.get()));
122     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
123     if ( pIter != m_aModelMap.end() )
124         pIter->second = rModel;
125     else
126         m_aModelMap[sName] = rModel;
127 }
128 
deregisterModelForXWindow(const uno::Reference<awt::XWindow> &)129 void Impl_ModelWinService::deregisterModelForXWindow( const uno::Reference< awt::XWindow >& /*rWindow*/ )
130 {
131 }
132 
getByName(const::rtl::OUString & sName)133 uno::Any Impl_ModelWinService::getByName( const ::rtl::OUString& sName )
134 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
135 {
136     uno::Any aAny;
137 
138     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
139     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
140     if ( pIter != m_aModelMap.end())
141     {
142         uno::Reference< awt::XControlModel > xModel( pIter->second );
143         aAny = uno::makeAny(xModel);
144     }
145 
146     return aAny;
147 }
148 
getElementNames()149 uno::Sequence< ::rtl::OUString > Impl_ModelWinService::getElementNames()
150 throw( uno::RuntimeException )
151 {
152     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
153     uno::Sequence< ::rtl::OUString > aResult( m_aModelMap.size() );
154 
155     sal_Int32 i = 0;
156     ModelWinMap::const_iterator pIter = m_aModelMap.begin();
157     while ( pIter != m_aModelMap.end())
158         aResult[i++] = pIter->first;
159 
160     return aResult;
161 }
162 
hasByName(const::rtl::OUString & sName)163 sal_Bool Impl_ModelWinService::hasByName( const ::rtl::OUString& sName )
164 throw( uno::RuntimeException )
165 {
166     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
167     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
168     if ( pIter != m_aModelMap.end())
169         return true;
170     else
171         return false;
172 }
173 
getElementType()174 uno::Type Impl_ModelWinService::getElementType()
175 throw( css::uno::RuntimeException )
176 {
177     return ::getCppuType(( const uno::Reference< awt::XControlModel >*)NULL );
178 }
179 
hasElements()180 sal_Bool Impl_ModelWinService::hasElements()
181 throw( css::uno::RuntimeException )
182 {
183     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
184     return (m_aModelMap.size() > 0);
185 }
186 
187 //*****************************************************************************************************************
188 //	css::uno::XInterface, XTypeProvider, XServiceInfo
189 //*****************************************************************************************************************
190 
DEFINE_XINTERFACE_4(ModelWinService,OWeakObject,DIRECT_INTERFACE (css::lang::XTypeProvider),DIRECT_INTERFACE (css::lang::XServiceInfo),DIRECT_INTERFACE (css::container::XNameAccess),DIRECT_INTERFACE (css::container::XElementAccess))191 DEFINE_XINTERFACE_4                 (   ModelWinService                                 ,
192                                         OWeakObject                                     ,
193                                         DIRECT_INTERFACE(css::lang::XTypeProvider       ),
194                                         DIRECT_INTERFACE(css::lang::XServiceInfo        ),
195                                         DIRECT_INTERFACE(css::container::XNameAccess    ),
196                                         DIRECT_INTERFACE(css::container::XElementAccess )
197 									)
198 
199 DEFINE_XTYPEPROVIDER_4              (   ModelWinService                ,
200                                         css::lang::XTypeProvider       ,
201                                         css::lang::XServiceInfo        ,
202                                         css::container::XNameAccess    ,
203                                         css::container::XElementAccess
204 									)
205 
206 DEFINE_XSERVICEINFO_MULTISERVICE    (   ModelWinService                    ,
207                                         OWeakObject                        ,
208                                         SERVICENAME_MODELWINSERVICE        ,
209                                         IMPLEMENTATIONNAME_MODELWINSERVICE
210 									)
211 
212 DEFINE_INIT_SERVICE                 (   ModelWinService,
213                                         {
214                                         }
215                                     )
216 
217 //*****************************************************************************************************************
218 //	constructor
219 //*****************************************************************************************************************
220 ModelWinService::ModelWinService(const uno::Reference< lang::XMultiServiceFactory >& rServiceManager ) :
221     m_xServiceManager( rServiceManager )
222 {
223 }
224 
~ModelWinService()225 ModelWinService::~ModelWinService()
226 {
227 }
228 
registerModelForXWindow(const uno::Reference<awt::XWindow> & rWindow,const uno::Reference<awt::XControlModel> & rModel)229 void ModelWinService::registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel )
230 {
231     Impl_ModelWinService::getSingleInstance(m_xServiceManager)->registerModelForXWindow( rWindow, rModel );
232 }
233 
deregisterModelForXWindow(const uno::Reference<awt::XWindow> & rWindow)234 void ModelWinService::deregisterModelForXWindow( const uno::Reference< awt::XWindow >& rWindow )
235 {
236     Impl_ModelWinService::getSingleInstance(m_xServiceManager)->deregisterModelForXWindow( rWindow );
237 }
238 
getByName(const::rtl::OUString & sName)239 uno::Any SAL_CALL ModelWinService::getByName( const ::rtl::OUString& sName )
240 throw(	container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
241 {
242     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->getByName( sName );
243 }
244 
getElementNames()245 uno::Sequence< ::rtl::OUString > SAL_CALL ModelWinService::getElementNames()
246 throw( uno::RuntimeException )
247 {
248     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->getElementNames( );
249 }
250 
hasByName(const::rtl::OUString & sName)251 sal_Bool SAL_CALL ModelWinService::hasByName( const ::rtl::OUString& sName )
252 throw( uno::RuntimeException )
253 {
254     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->hasByName( sName );
255 }
256 
257 //---------------------------------------------------------------------------------------------------------
258 //	XElementAccess
259 //---------------------------------------------------------------------------------------------------------
getElementType()260 uno::Type SAL_CALL ModelWinService::getElementType()
261 throw( uno::RuntimeException )
262 {
263     return ::getCppuType( (const uno::Reference< awt::XControlModel > *)NULL );
264 }
265 
hasElements()266 sal_Bool SAL_CALL ModelWinService::hasElements()
267 throw( uno::RuntimeException )
268 {
269     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->hasElements();
270 }
271 
272 }
273