xref: /trunk/main/framework/source/services/modelwinservice.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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 
65         uno::Sequence< ::rtl::OUString > getElementNames();
66 
67         sal_Bool hasByName( const ::rtl::OUString& sName );
68 
69         uno::Type getElementType();
70 
71         sal_Bool hasElements();
72 
73         void registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel );
74 
75         void deregisterModelForXWindow( const uno::Reference< awt::XWindow >& rWindow );
76 
77     private:
78         typedef BaseHash< uno::WeakReference< awt::XControlModel > > ModelWinMap;
79 
80         Impl_ModelWinService();
81         Impl_ModelWinService( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager );
82 
83         static Impl_ModelWinService* m_pModelWinService;
84 
85         ::com::sun::star::uno::WeakReference< ::com::sun::star::lang::XMultiServiceFactory > m_xServiceManager;
86         ModelWinMap m_aModelMap;
87 };
88 
89 Impl_ModelWinService* Impl_ModelWinService::m_pModelWinService = 0;
90 
91 Impl_ModelWinService* Impl_ModelWinService::getSingleInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager )
92 {
93     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
94     if ( !m_pModelWinService )
95         m_pModelWinService = new Impl_ModelWinService( rServiceManager );
96     return m_pModelWinService;
97 }
98 
99 Impl_ModelWinService::Impl_ModelWinService( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rServiceManager ) :
100     m_xServiceManager( rServiceManager )
101 {
102 }
103 
104 Impl_ModelWinService::Impl_ModelWinService()
105 {
106 }
107 
108 Impl_ModelWinService::~Impl_ModelWinService()
109 {
110 }
111 
112 void Impl_ModelWinService::registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel )
113 {
114     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
115 
116     ::rtl::OUString sName = rtl::OUString::valueOf( reinterpret_cast< sal_Int64 >((void*)rWindow.get()));
117     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
118     if ( pIter != m_aModelMap.end() )
119         pIter->second = rModel;
120     else
121         m_aModelMap[sName] = rModel;
122 }
123 
124 void Impl_ModelWinService::deregisterModelForXWindow( const uno::Reference< awt::XWindow >& /*rWindow*/ )
125 {
126 }
127 
128 uno::Any Impl_ModelWinService::getByName( const ::rtl::OUString& sName )
129 {
130     uno::Any aAny;
131 
132     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
133     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
134     if ( pIter != m_aModelMap.end())
135     {
136         uno::Reference< awt::XControlModel > xModel( pIter->second );
137         aAny = uno::makeAny(xModel);
138     }
139 
140     return aAny;
141 }
142 
143 uno::Sequence< ::rtl::OUString > Impl_ModelWinService::getElementNames()
144 {
145     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
146     uno::Sequence< ::rtl::OUString > aResult( m_aModelMap.size() );
147 
148     sal_Int32 i = 0;
149     ModelWinMap::const_iterator pIter = m_aModelMap.begin();
150     while ( pIter != m_aModelMap.end())
151         aResult[i++] = pIter->first;
152 
153     return aResult;
154 }
155 
156 sal_Bool Impl_ModelWinService::hasByName( const ::rtl::OUString& sName )
157 {
158     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
159     ModelWinMap::iterator pIter = m_aModelMap.find( sName );
160     if ( pIter != m_aModelMap.end())
161         return true;
162     else
163         return false;
164 }
165 
166 uno::Type Impl_ModelWinService::getElementType()
167 {
168     return ::getCppuType(( const uno::Reference< awt::XControlModel >*)NULL );
169 }
170 
171 sal_Bool Impl_ModelWinService::hasElements()
172 {
173     osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
174     return (m_aModelMap.size() > 0);
175 }
176 
177 //*****************************************************************************************************************
178 //  css::uno::XInterface, XTypeProvider, XServiceInfo
179 //*****************************************************************************************************************
180 
181 DEFINE_XINTERFACE_4                 (   ModelWinService                                 ,
182                                         OWeakObject                                     ,
183                                         DIRECT_INTERFACE(css::lang::XTypeProvider       ),
184                                         DIRECT_INTERFACE(css::lang::XServiceInfo        ),
185                                         DIRECT_INTERFACE(css::container::XNameAccess    ),
186                                         DIRECT_INTERFACE(css::container::XElementAccess )
187                                     )
188 
189 DEFINE_XTYPEPROVIDER_4              (   ModelWinService                ,
190                                         css::lang::XTypeProvider       ,
191                                         css::lang::XServiceInfo        ,
192                                         css::container::XNameAccess    ,
193                                         css::container::XElementAccess
194                                     )
195 
196 DEFINE_XSERVICEINFO_MULTISERVICE    (   ModelWinService                    ,
197                                         OWeakObject                        ,
198                                         SERVICENAME_MODELWINSERVICE        ,
199                                         IMPLEMENTATIONNAME_MODELWINSERVICE
200                                     )
201 
202 DEFINE_INIT_SERVICE                 (   ModelWinService,
203                                         {
204                                         }
205                                     )
206 
207 //*****************************************************************************************************************
208 //  constructor
209 //*****************************************************************************************************************
210 ModelWinService::ModelWinService(const uno::Reference< lang::XMultiServiceFactory >& rServiceManager ) :
211     m_xServiceManager( rServiceManager )
212 {
213 }
214 
215 ModelWinService::~ModelWinService()
216 {
217 }
218 
219 void ModelWinService::registerModelForXWindow( const uno::Reference< awt::XWindow >& rWindow, const uno::Reference< awt::XControlModel >& rModel )
220 {
221     Impl_ModelWinService::getSingleInstance(m_xServiceManager)->registerModelForXWindow( rWindow, rModel );
222 }
223 
224 void ModelWinService::deregisterModelForXWindow( const uno::Reference< awt::XWindow >& rWindow )
225 {
226     Impl_ModelWinService::getSingleInstance(m_xServiceManager)->deregisterModelForXWindow( rWindow );
227 }
228 
229 uno::Any SAL_CALL ModelWinService::getByName( const ::rtl::OUString& sName )
230 {
231     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->getByName( sName );
232 }
233 
234 uno::Sequence< ::rtl::OUString > SAL_CALL ModelWinService::getElementNames()
235 {
236     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->getElementNames( );
237 }
238 
239 sal_Bool SAL_CALL ModelWinService::hasByName( const ::rtl::OUString& sName )
240 {
241     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->hasByName( sName );
242 }
243 
244 //---------------------------------------------------------------------------------------------------------
245 //  XElementAccess
246 //---------------------------------------------------------------------------------------------------------
247 uno::Type SAL_CALL ModelWinService::getElementType()
248 {
249     return ::getCppuType( (const uno::Reference< awt::XControlModel > *)NULL );
250 }
251 
252 sal_Bool SAL_CALL ModelWinService::hasElements()
253 {
254     return Impl_ModelWinService::getSingleInstance(m_xServiceManager)->hasElements();
255 }
256 
257 }
258