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