xref: /AOO41X/main/ucbhelper/source/provider/contentinfo.cxx (revision ac9096f48ddc8269a54878c5b102c19157b971bd) !
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_ucbhelper.hxx"
26 /**************************************************************************
27                                 TODO
28  **************************************************************************
29 
30  *************************************************************************/
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 #include <com/sun/star/ucb/XPropertySetRegistry.hpp>
33 
34 #include "osl/diagnose.h"
35 #include "osl/mutex.hxx"
36 #include <ucbhelper/contenthelper.hxx>
37 #include <ucbhelper/contentinfo.hxx>
38 
39 using namespace com::sun::star;
40 
41 //=========================================================================
42 //=========================================================================
43 //
44 // PropertySetInfo Implementation.
45 //
46 //=========================================================================
47 //=========================================================================
48 
49 namespace ucbhelper {
50 
PropertySetInfo(const uno::Reference<lang::XMultiServiceFactory> & rxSMgr,const uno::Reference<com::sun::star::ucb::XCommandEnvironment> & rxEnv,ContentImplHelper * pContent)51 PropertySetInfo::PropertySetInfo(
52     const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
53     const uno::Reference< com::sun::star::ucb::XCommandEnvironment >& rxEnv,
54     ContentImplHelper* pContent )
55 : m_xSMgr( rxSMgr ),
56   m_xEnv( rxEnv ),
57   m_pProps( 0 ),
58   m_pContent( pContent )
59 {
60 }
61 
62 //=========================================================================
63 // virtual
~PropertySetInfo()64 PropertySetInfo::~PropertySetInfo()
65 {
66     delete m_pProps;
67 }
68 
69 //=========================================================================
70 //
71 // XInterface methods.
72 //
73 //=========================================================================
74 
75 XINTERFACE_IMPL_2( PropertySetInfo,
76                    lang::XTypeProvider,
77                    beans::XPropertySetInfo );
78 
79 //=========================================================================
80 //
81 // XTypeProvider methods.
82 //
83 //=========================================================================
84 
85 XTYPEPROVIDER_IMPL_2( PropertySetInfo,
86                       lang::XTypeProvider,
87                       beans::XPropertySetInfo );
88 
89 //=========================================================================
90 //
91 // XPropertySetInfo methods.
92 //
93 //=========================================================================
94 
95 // virtual
getProperties()96 uno::Sequence< beans::Property > SAL_CALL PropertySetInfo::getProperties()
97     throw( uno::RuntimeException )
98 {
99     if ( !m_pProps )
100     {
101         osl::MutexGuard aGuard( m_aMutex );
102         if ( !m_pProps )
103         {
104             //////////////////////////////////////////////////////////////
105             // Get info for core ( native) properties.
106             //////////////////////////////////////////////////////////////
107 
108             try
109             {
110                 uno::Sequence< beans::Property > aProps
111                     = m_pContent->getProperties( m_xEnv );
112                 m_pProps = new uno::Sequence< beans::Property >( aProps );
113             }
114             catch ( uno::RuntimeException const & )
115             {
116                 throw;
117             }
118             catch ( uno::Exception const & )
119             {
120                 m_pProps = new uno::Sequence< beans::Property >( 0 );
121             }
122 
123             //////////////////////////////////////////////////////////////
124             // Get info for additional properties.
125             //////////////////////////////////////////////////////////////
126 
127             uno::Reference< com::sun::star::ucb::XPersistentPropertySet >
128                 xSet ( m_pContent->getAdditionalPropertySet( sal_False ) );
129 
130             if ( xSet.is() )
131             {
132                 // Get property set info.
133                 uno::Reference< beans::XPropertySetInfo > xInfo(
134                     xSet->getPropertySetInfo() );
135                 if ( xInfo.is() )
136                 {
137                     const uno::Sequence< beans::Property >& rAddProps
138                         = xInfo->getProperties();
139                     sal_Int32 nAddProps = rAddProps.getLength();
140                     if ( nAddProps > 0 )
141                     {
142                         sal_Int32 nPos = m_pProps->getLength();
143                         m_pProps->realloc( nPos + nAddProps );
144 
145                         beans::Property* pProps = m_pProps->getArray();
146                         const beans::Property* pAddProps
147                             = rAddProps.getConstArray();
148 
149                         for ( sal_Int32 n = 0; n < nAddProps; ++n, ++nPos )
150                             pProps[ nPos ] = pAddProps[ n ];
151                     }
152                 }
153             }
154         }
155     }
156     return *m_pProps;
157 }
158 
159 //=========================================================================
160 // virtual
getPropertyByName(const rtl::OUString & aName)161 beans::Property SAL_CALL PropertySetInfo::getPropertyByName(
162         const rtl::OUString& aName )
163     throw( beans::UnknownPropertyException, uno::RuntimeException )
164 {
165     beans::Property aProp;
166     if ( queryProperty( aName, aProp ) )
167         return aProp;
168 
169     throw beans::UnknownPropertyException();
170 }
171 
172 //=========================================================================
173 // virtual
hasPropertyByName(const rtl::OUString & Name)174 sal_Bool SAL_CALL PropertySetInfo::hasPropertyByName(
175         const rtl::OUString& Name )
176     throw( uno::RuntimeException )
177 {
178     beans::Property aProp;
179     return queryProperty( Name, aProp );
180 }
181 
182 //=========================================================================
183 //
184 // Non-Interface methods.
185 //
186 //=========================================================================
187 
reset()188 void PropertySetInfo::reset()
189 {
190     osl::MutexGuard aGuard( m_aMutex );
191     delete m_pProps;
192     m_pProps = 0;
193 }
194 
195 //=========================================================================
queryProperty(const rtl::OUString & rName,beans::Property & rProp)196 sal_Bool PropertySetInfo::queryProperty(
197     const rtl::OUString& rName, beans::Property& rProp )
198 {
199     osl::MutexGuard aGuard( m_aMutex );
200 
201     getProperties();
202 
203     const beans::Property* pProps = m_pProps->getConstArray();
204     sal_Int32 nCount = m_pProps->getLength();
205     for ( sal_Int32 n = 0; n < nCount; ++n )
206     {
207         const beans::Property& rCurrProp = pProps[ n ];
208         if ( rCurrProp.Name == rName )
209         {
210             rProp = rCurrProp;
211             return sal_True;
212         }
213     }
214 
215     return sal_False;
216 }
217 
218 //=========================================================================
219 //=========================================================================
220 //
221 // CommandProcessorInfo Implementation.
222 //
223 //=========================================================================
224 //=========================================================================
225 
CommandProcessorInfo(const uno::Reference<lang::XMultiServiceFactory> & rxSMgr,const uno::Reference<com::sun::star::ucb::XCommandEnvironment> & rxEnv,ContentImplHelper * pContent)226 CommandProcessorInfo::CommandProcessorInfo(
227     const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
228     const uno::Reference< com::sun::star::ucb::XCommandEnvironment >& rxEnv,
229     ContentImplHelper* pContent )
230 : m_xSMgr( rxSMgr ),
231   m_xEnv( rxEnv ),
232   m_pCommands( 0 ),
233   m_pContent( pContent )
234 {
235 }
236 
237 //=========================================================================
238 // virtual
~CommandProcessorInfo()239 CommandProcessorInfo::~CommandProcessorInfo()
240 {
241     delete m_pCommands;
242 }
243 
244 //=========================================================================
245 //
246 // XInterface methods.
247 //
248 //=========================================================================
249 
250 XINTERFACE_IMPL_2( CommandProcessorInfo,
251                    lang::XTypeProvider,
252                    com::sun::star::ucb::XCommandInfo );
253 
254 //=========================================================================
255 //
256 // XTypeProvider methods.
257 //
258 //=========================================================================
259 
260 XTYPEPROVIDER_IMPL_2( CommandProcessorInfo,
261                       lang::XTypeProvider,
262                       com::sun::star::ucb::XCommandInfo );
263 
264 //=========================================================================
265 //
266 // XCommandInfo methods.
267 //
268 //=========================================================================
269 
270 // virtual
271 uno::Sequence< com::sun::star::ucb::CommandInfo > SAL_CALL
getCommands()272 CommandProcessorInfo::getCommands()
273     throw( uno::RuntimeException )
274 {
275     if ( !m_pCommands )
276     {
277         osl::MutexGuard aGuard( m_aMutex );
278         if ( !m_pCommands )
279         {
280             //////////////////////////////////////////////////////////////
281             // Get info for commands.
282             //////////////////////////////////////////////////////////////
283 
284             try
285             {
286                 uno::Sequence< com::sun::star::ucb::CommandInfo > aCmds
287                     = m_pContent->getCommands( m_xEnv );
288                 m_pCommands
289                     = new uno::Sequence< com::sun::star::ucb::CommandInfo >(
290                         aCmds );
291             }
292             catch ( uno::RuntimeException const & )
293             {
294                 throw;
295             }
296             catch ( uno::Exception const & )
297             {
298                 m_pCommands
299                     = new uno::Sequence< com::sun::star::ucb::CommandInfo >(
300                         0 );
301             }
302         }
303     }
304     return *m_pCommands;
305 }
306 
307 //=========================================================================
308 // virtual
309 com::sun::star::ucb::CommandInfo SAL_CALL
getCommandInfoByName(const rtl::OUString & Name)310 CommandProcessorInfo::getCommandInfoByName(
311         const rtl::OUString& Name )
312     throw( com::sun::star::ucb::UnsupportedCommandException,
313            uno::RuntimeException )
314 {
315     com::sun::star::ucb::CommandInfo aInfo;
316     if ( queryCommand( Name, aInfo ) )
317         return aInfo;
318 
319     throw com::sun::star::ucb::UnsupportedCommandException();
320 }
321 
322 //=========================================================================
323 // virtual
324 com::sun::star::ucb::CommandInfo SAL_CALL
getCommandInfoByHandle(sal_Int32 Handle)325 CommandProcessorInfo::getCommandInfoByHandle( sal_Int32 Handle )
326     throw( com::sun::star::ucb::UnsupportedCommandException,
327            uno::RuntimeException )
328 {
329     com::sun::star::ucb::CommandInfo aInfo;
330     if ( queryCommand( Handle, aInfo ) )
331         return aInfo;
332 
333     throw com::sun::star::ucb::UnsupportedCommandException();
334 }
335 
336 //=========================================================================
337 // virtual
hasCommandByName(const rtl::OUString & Name)338 sal_Bool SAL_CALL CommandProcessorInfo::hasCommandByName(
339        const rtl::OUString& Name )
340     throw( uno::RuntimeException )
341 {
342     com::sun::star::ucb::CommandInfo aInfo;
343     return queryCommand( Name, aInfo );
344 }
345 
346 //=========================================================================
347 // virtual
hasCommandByHandle(sal_Int32 Handle)348 sal_Bool SAL_CALL CommandProcessorInfo::hasCommandByHandle( sal_Int32 Handle )
349     throw( uno::RuntimeException )
350 {
351     com::sun::star::ucb::CommandInfo aInfo;
352     return queryCommand( Handle, aInfo );
353 }
354 
355 //=========================================================================
356 //
357 // Non-Interface methods.
358 //
359 //=========================================================================
360 
reset()361 void CommandProcessorInfo::reset()
362 {
363     osl::MutexGuard aGuard( m_aMutex );
364     delete m_pCommands;
365     m_pCommands = 0;
366 }
367 
368 
369 //=========================================================================
queryCommand(const rtl::OUString & rName,com::sun::star::ucb::CommandInfo & rCommand)370 sal_Bool CommandProcessorInfo::queryCommand(
371     const rtl::OUString& rName,
372     com::sun::star::ucb::CommandInfo& rCommand )
373 {
374     osl::MutexGuard aGuard( m_aMutex );
375 
376     getCommands();
377 
378     const com::sun::star::ucb::CommandInfo* pCommands
379         = m_pCommands->getConstArray();
380     sal_Int32 nCount = m_pCommands->getLength();
381     for ( sal_Int32 n = 0; n < nCount; ++n )
382     {
383         const com::sun::star::ucb::CommandInfo& rCurrCommand = pCommands[ n ];
384         if ( rCurrCommand.Name == rName )
385         {
386             rCommand = rCurrCommand;
387             return sal_True;
388         }
389     }
390 
391     return sal_False;
392 }
393 
394 //=========================================================================
queryCommand(sal_Int32 nHandle,com::sun::star::ucb::CommandInfo & rCommand)395 sal_Bool CommandProcessorInfo::queryCommand(
396     sal_Int32 nHandle,
397     com::sun::star::ucb::CommandInfo& rCommand )
398 {
399     osl::MutexGuard aGuard( m_aMutex );
400 
401     getCommands();
402 
403     const com::sun::star::ucb::CommandInfo* pCommands
404         = m_pCommands->getConstArray();
405     sal_Int32 nCount = m_pCommands->getLength();
406     for ( sal_Int32 n = 0; n < nCount; ++n )
407     {
408         const com::sun::star::ucb::CommandInfo& rCurrCommand = pCommands[ n ];
409         if ( rCurrCommand.Handle == nHandle )
410         {
411             rCommand = rCurrCommand;
412             return sal_True;
413         }
414     }
415 
416     return sal_False;
417 }
418 
419 } // namespace ucbhelper
420