1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucbhelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /**************************************************************************
32*cdf0e10cSrcweir 								TODO
33*cdf0e10cSrcweir  **************************************************************************
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir  *************************************************************************/
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <hash_map>
38*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertyAccess.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/ucb/XPropertySetRegistryFactory.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/ucb/XPropertySetRegistry.hpp>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include "osl/diagnose.h"
45*cdf0e10cSrcweir #include "osl/mutex.hxx"
46*cdf0e10cSrcweir #include "cppuhelper/weakref.hxx"
47*cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx>
48*cdf0e10cSrcweir #include <ucbhelper/providerhelper.hxx>
49*cdf0e10cSrcweir #include <ucbhelper/contenthelper.hxx>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir using namespace com::sun::star;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir namespace ucbhelper_impl
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir //=========================================================================
57*cdf0e10cSrcweir //
58*cdf0e10cSrcweir // Contents.
59*cdf0e10cSrcweir //
60*cdf0e10cSrcweir //=========================================================================
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir struct equalString
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	bool operator()(
65*cdf0e10cSrcweir         const rtl::OUString& rKey11, const rtl::OUString& rKey22 ) const
66*cdf0e10cSrcweir   	{
67*cdf0e10cSrcweir   		return !!( rKey11 == rKey22 );
68*cdf0e10cSrcweir   	}
69*cdf0e10cSrcweir };
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir struct hashString
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	size_t operator()( const rtl::OUString & rName ) const
74*cdf0e10cSrcweir 	{
75*cdf0e10cSrcweir 		return rName.hashCode();
76*cdf0e10cSrcweir 	}
77*cdf0e10cSrcweir };
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir typedef std::hash_map
80*cdf0e10cSrcweir <
81*cdf0e10cSrcweir 	rtl::OUString,
82*cdf0e10cSrcweir     uno::WeakReference< ucb::XContent >,
83*cdf0e10cSrcweir 	hashString,
84*cdf0e10cSrcweir 	equalString
85*cdf0e10cSrcweir >
86*cdf0e10cSrcweir Contents;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir //=========================================================================
89*cdf0e10cSrcweir //
90*cdf0e10cSrcweir // struct ContentProviderImplHelper_Impl.
91*cdf0e10cSrcweir //
92*cdf0e10cSrcweir //=========================================================================
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir struct ContentProviderImplHelper_Impl
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	uno::Reference< com::sun::star::ucb::XPropertySetRegistry >
97*cdf0e10cSrcweir         m_xPropertySetRegistry;
98*cdf0e10cSrcweir 	Contents
99*cdf0e10cSrcweir         m_aContents;
100*cdf0e10cSrcweir };
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir } // namespace ucbhelper_impl
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir //=========================================================================
105*cdf0e10cSrcweir //=========================================================================
106*cdf0e10cSrcweir //
107*cdf0e10cSrcweir // ContentProviderImplHelper Implementation.
108*cdf0e10cSrcweir //
109*cdf0e10cSrcweir //=========================================================================
110*cdf0e10cSrcweir //=========================================================================
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir namespace ucbhelper {
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir ContentProviderImplHelper::ContentProviderImplHelper(
115*cdf0e10cSrcweir     const uno::Reference< lang::XMultiServiceFactory >& rXSMgr )
116*cdf0e10cSrcweir : m_pImpl( new ucbhelper_impl::ContentProviderImplHelper_Impl ),
117*cdf0e10cSrcweir   m_xSMgr( rXSMgr )
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir //=========================================================================
122*cdf0e10cSrcweir // virtual
123*cdf0e10cSrcweir ContentProviderImplHelper::~ContentProviderImplHelper()
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir 	delete m_pImpl;
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir //=========================================================================
129*cdf0e10cSrcweir //
130*cdf0e10cSrcweir // XInterface methods.
131*cdf0e10cSrcweir //
132*cdf0e10cSrcweir //=========================================================================
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir XINTERFACE_IMPL_3( ContentProviderImplHelper,
135*cdf0e10cSrcweir 				   lang::XTypeProvider,
136*cdf0e10cSrcweir 				   lang::XServiceInfo,
137*cdf0e10cSrcweir 				   com::sun::star::ucb::XContentProvider );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir //=========================================================================
140*cdf0e10cSrcweir //
141*cdf0e10cSrcweir // XTypeProvider methods.
142*cdf0e10cSrcweir //
143*cdf0e10cSrcweir //=========================================================================
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( ContentProviderImplHelper,
146*cdf0e10cSrcweir 				   	  lang::XTypeProvider,
147*cdf0e10cSrcweir 				   	  lang::XServiceInfo,
148*cdf0e10cSrcweir 				   	  com::sun::star::ucb::XContentProvider );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir //=========================================================================
151*cdf0e10cSrcweir //
152*cdf0e10cSrcweir // XServiceInfo methods.
153*cdf0e10cSrcweir //
154*cdf0e10cSrcweir //=========================================================================
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir // virtual
157*cdf0e10cSrcweir sal_Bool SAL_CALL ContentProviderImplHelper::supportsService(
158*cdf0e10cSrcweir 											const rtl::OUString& ServiceName )
159*cdf0e10cSrcweir 	throw( uno::RuntimeException )
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	uno::Sequence< rtl::OUString > aSNL = getSupportedServiceNames();
162*cdf0e10cSrcweir 	const rtl::OUString* pArray = aSNL.getConstArray();
163*cdf0e10cSrcweir 	for ( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
164*cdf0e10cSrcweir 	{
165*cdf0e10cSrcweir 		if ( pArray[ i ] == ServiceName )
166*cdf0e10cSrcweir 			return sal_True;
167*cdf0e10cSrcweir 	}
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 	return sal_False;
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir //=========================================================================
173*cdf0e10cSrcweir //
174*cdf0e10cSrcweir // XContentProvider methods.
175*cdf0e10cSrcweir //
176*cdf0e10cSrcweir //=========================================================================
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir // virtual
179*cdf0e10cSrcweir sal_Int32 SAL_CALL ContentProviderImplHelper::compareContentIds(
180*cdf0e10cSrcweir         const uno::Reference< com::sun::star::ucb::XContentIdentifier >& Id1,
181*cdf0e10cSrcweir         const uno::Reference< com::sun::star::ucb::XContentIdentifier >& Id2 )
182*cdf0e10cSrcweir 	throw( uno::RuntimeException )
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	// Simply do a string compare.
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	rtl::OUString aURL1( Id1->getContentIdentifier() );
187*cdf0e10cSrcweir 	rtl::OUString aURL2( Id2->getContentIdentifier() );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 	return aURL1.compareTo( aURL2 );;
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir //=========================================================================
193*cdf0e10cSrcweir //
194*cdf0e10cSrcweir // Non-interface methods
195*cdf0e10cSrcweir //
196*cdf0e10cSrcweir //=========================================================================
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir void ContentProviderImplHelper::cleanupRegisteredContents()
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     ucbhelper_impl::Contents::iterator it
203*cdf0e10cSrcweir         = m_pImpl->m_aContents.begin();
204*cdf0e10cSrcweir     while( it != m_pImpl->m_aContents.end() )
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         uno::Reference< ucb::XContent > xContent( (*it).second );
207*cdf0e10cSrcweir         if ( !xContent.is() )
208*cdf0e10cSrcweir         {
209*cdf0e10cSrcweir             ucbhelper_impl::Contents::iterator tmp = it;
210*cdf0e10cSrcweir             ++it;
211*cdf0e10cSrcweir             m_pImpl->m_aContents.erase( tmp );
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir         else
214*cdf0e10cSrcweir         {
215*cdf0e10cSrcweir             ++it;
216*cdf0e10cSrcweir         }
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir //=========================================================================
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir void ContentProviderImplHelper::removeContent( ContentImplHelper* pContent )
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     cleanupRegisteredContents();
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 	const rtl::OUString aURL(
229*cdf0e10cSrcweir         pContent->getIdentifier()->getContentIdentifier() );
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 	ucbhelper_impl::Contents::iterator it = m_pImpl->m_aContents.find( aURL );
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 	if ( it != m_pImpl->m_aContents.end() )
234*cdf0e10cSrcweir 		m_pImpl->m_aContents.erase( it );
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir //=========================================================================
238*cdf0e10cSrcweir rtl::Reference< ContentImplHelper >
239*cdf0e10cSrcweir ContentProviderImplHelper::queryExistingContent(
240*cdf0e10cSrcweir     const uno::Reference< com::sun::star::ucb::XContentIdentifier >&
241*cdf0e10cSrcweir         Identifier )
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir 	return queryExistingContent( Identifier->getContentIdentifier() );
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir //=========================================================================
247*cdf0e10cSrcweir rtl::Reference< ContentImplHelper >
248*cdf0e10cSrcweir ContentProviderImplHelper::queryExistingContent( const rtl::OUString& rURL )
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir     cleanupRegisteredContents();
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 	// Check, if a content with given id already exists...
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 	ucbhelper_impl::Contents::const_iterator it
257*cdf0e10cSrcweir         = m_pImpl->m_aContents.find( rURL );
258*cdf0e10cSrcweir 	if ( it != m_pImpl->m_aContents.end() )
259*cdf0e10cSrcweir 	{
260*cdf0e10cSrcweir         uno::Reference< ucb::XContent > xContent( (*it).second );
261*cdf0e10cSrcweir         if ( xContent.is() )
262*cdf0e10cSrcweir         {
263*cdf0e10cSrcweir             return rtl::Reference< ContentImplHelper >(
264*cdf0e10cSrcweir                 static_cast< ContentImplHelper * >( xContent.get() ) );
265*cdf0e10cSrcweir         }
266*cdf0e10cSrcweir 	}
267*cdf0e10cSrcweir 	return rtl::Reference< ContentImplHelper >();
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir //=========================================================================
271*cdf0e10cSrcweir void ContentProviderImplHelper::queryExistingContents(
272*cdf0e10cSrcweir 		ContentRefList& rContents )
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir     cleanupRegisteredContents();
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	ucbhelper_impl::Contents::const_iterator it
279*cdf0e10cSrcweir         = m_pImpl->m_aContents.begin();
280*cdf0e10cSrcweir 	ucbhelper_impl::Contents::const_iterator end
281*cdf0e10cSrcweir         = m_pImpl->m_aContents.end();
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 	while ( it != end )
284*cdf0e10cSrcweir 	{
285*cdf0e10cSrcweir         uno::Reference< ucb::XContent > xContent( (*it).second );
286*cdf0e10cSrcweir         if ( xContent.is() )
287*cdf0e10cSrcweir         {
288*cdf0e10cSrcweir             rContents.push_back(
289*cdf0e10cSrcweir                 rtl::Reference< ContentImplHelper >(
290*cdf0e10cSrcweir                     static_cast< ContentImplHelper * >( xContent.get() ) ) );
291*cdf0e10cSrcweir         }
292*cdf0e10cSrcweir 		++it;
293*cdf0e10cSrcweir 	}
294*cdf0e10cSrcweir }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir //=========================================================================
297*cdf0e10cSrcweir void ContentProviderImplHelper::registerNewContent(
298*cdf0e10cSrcweir     const uno::Reference< ucb::XContent > & xContent )
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir     if ( xContent.is() )
301*cdf0e10cSrcweir     {
302*cdf0e10cSrcweir         osl::MutexGuard aGuard( m_aMutex );
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir         cleanupRegisteredContents();
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir         const rtl::OUString aURL(
307*cdf0e10cSrcweir             xContent->getIdentifier()->getContentIdentifier() );
308*cdf0e10cSrcweir         ucbhelper_impl::Contents::const_iterator it
309*cdf0e10cSrcweir             = m_pImpl->m_aContents.find( aURL );
310*cdf0e10cSrcweir         if ( it == m_pImpl->m_aContents.end() )
311*cdf0e10cSrcweir             m_pImpl->m_aContents[ aURL ] = xContent;
312*cdf0e10cSrcweir     }
313*cdf0e10cSrcweir }
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir //=========================================================================
316*cdf0e10cSrcweir uno::Reference< com::sun::star::ucb::XPropertySetRegistry >
317*cdf0e10cSrcweir ContentProviderImplHelper::getAdditionalPropertySetRegistry()
318*cdf0e10cSrcweir {
319*cdf0e10cSrcweir 	// Get propertyset registry.
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 	if ( !m_pImpl->m_xPropertySetRegistry.is() )
324*cdf0e10cSrcweir 	{
325*cdf0e10cSrcweir 		uno::Reference< com::sun::star::ucb::XPropertySetRegistryFactory >
326*cdf0e10cSrcweir             xRegFac(
327*cdf0e10cSrcweir 				m_xSMgr->createInstance(
328*cdf0e10cSrcweir 					rtl::OUString::createFromAscii(
329*cdf0e10cSrcweir                         "com.sun.star.ucb.Store" ) ),
330*cdf0e10cSrcweir 				uno::UNO_QUERY );
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir         OSL_ENSURE( xRegFac.is(),
333*cdf0e10cSrcweir 					"ContentProviderImplHelper::getAdditionalPropertySet - "
334*cdf0e10cSrcweir 					"No UCB-Store service!" );
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 		if ( xRegFac.is() )
337*cdf0e10cSrcweir 		{
338*cdf0e10cSrcweir 			// Open/create a registry.
339*cdf0e10cSrcweir 			m_pImpl->m_xPropertySetRegistry
340*cdf0e10cSrcweir                 = xRegFac->createPropertySetRegistry( rtl::OUString() );
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir             OSL_ENSURE( m_pImpl->m_xPropertySetRegistry.is(),
343*cdf0e10cSrcweir 					"ContentProviderImplHelper::getAdditionalPropertySet - "
344*cdf0e10cSrcweir 					"Error opening registry!" );
345*cdf0e10cSrcweir 		}
346*cdf0e10cSrcweir 	}
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 	return m_pImpl->m_xPropertySetRegistry;
349*cdf0e10cSrcweir }
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir //=========================================================================
353*cdf0e10cSrcweir uno::Reference< com::sun::star::ucb::XPersistentPropertySet >
354*cdf0e10cSrcweir ContentProviderImplHelper::getAdditionalPropertySet(
355*cdf0e10cSrcweir     const rtl::OUString& rKey, sal_Bool bCreate )
356*cdf0e10cSrcweir {
357*cdf0e10cSrcweir 	// Get propertyset registry.
358*cdf0e10cSrcweir 	getAdditionalPropertySetRegistry();
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir 	if ( m_pImpl->m_xPropertySetRegistry.is() )
361*cdf0e10cSrcweir 	{
362*cdf0e10cSrcweir 		// Open/create persistent property set.
363*cdf0e10cSrcweir 	    return uno::Reference< com::sun::star::ucb::XPersistentPropertySet >(
364*cdf0e10cSrcweir             m_pImpl->m_xPropertySetRegistry->openPropertySet(
365*cdf0e10cSrcweir                 rKey, bCreate ) );
366*cdf0e10cSrcweir 	}
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir 	return uno::Reference< com::sun::star::ucb::XPersistentPropertySet >();
369*cdf0e10cSrcweir }
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir //=========================================================================
372*cdf0e10cSrcweir sal_Bool ContentProviderImplHelper::renameAdditionalPropertySet(
373*cdf0e10cSrcweir     const rtl::OUString& rOldKey,
374*cdf0e10cSrcweir     const rtl::OUString& rNewKey,
375*cdf0e10cSrcweir     sal_Bool bRecursive )
376*cdf0e10cSrcweir {
377*cdf0e10cSrcweir 	if ( rOldKey == rNewKey )
378*cdf0e10cSrcweir 		return sal_True;
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir 	if ( bRecursive )
383*cdf0e10cSrcweir 	{
384*cdf0e10cSrcweir 		// Get propertyset registry.
385*cdf0e10cSrcweir 		getAdditionalPropertySetRegistry();
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir 		if ( m_pImpl->m_xPropertySetRegistry.is() )
388*cdf0e10cSrcweir 		{
389*cdf0e10cSrcweir 			uno::Reference< container::XNameAccess > xNameAccess(
390*cdf0e10cSrcweir                 m_pImpl->m_xPropertySetRegistry, uno::UNO_QUERY );
391*cdf0e10cSrcweir 			if ( xNameAccess.is() )
392*cdf0e10cSrcweir 			{
393*cdf0e10cSrcweir 				uno::Sequence< rtl::OUString > aKeys
394*cdf0e10cSrcweir                     = xNameAccess->getElementNames();
395*cdf0e10cSrcweir 				sal_Int32 nCount = aKeys.getLength();
396*cdf0e10cSrcweir 				if ( nCount > 0 )
397*cdf0e10cSrcweir 				{
398*cdf0e10cSrcweir 					rtl::OUString aOldKeyWithSlash = rOldKey;
399*cdf0e10cSrcweir 					rtl::OUString aOldKeyWithoutSlash;
400*cdf0e10cSrcweir 					if ( aOldKeyWithSlash.lastIndexOf(
401*cdf0e10cSrcweir                              sal_Unicode('/')
402*cdf0e10cSrcweir                                  != aOldKeyWithSlash.getLength() - 1 ) )
403*cdf0e10cSrcweir 					{
404*cdf0e10cSrcweir 						aOldKeyWithSlash += rtl::OUString( sal_Unicode('/') );
405*cdf0e10cSrcweir 						aOldKeyWithoutSlash = rOldKey;
406*cdf0e10cSrcweir 					}
407*cdf0e10cSrcweir 					else if ( rOldKey.getLength() )
408*cdf0e10cSrcweir 						aOldKeyWithoutSlash
409*cdf0e10cSrcweir                             = rOldKey.copy( 0, rOldKey.getLength() - 1 );
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir 					const rtl::OUString* pKeys = aKeys.getConstArray();
412*cdf0e10cSrcweir 					for ( sal_Int32 n = 0; n < nCount; ++n )
413*cdf0e10cSrcweir 					{
414*cdf0e10cSrcweir 						const rtl::OUString& rKey = pKeys[ n ];
415*cdf0e10cSrcweir 						if ( rKey.compareTo(
416*cdf0e10cSrcweir                                  aOldKeyWithSlash,
417*cdf0e10cSrcweir                                  aOldKeyWithSlash.getLength() ) == 0
418*cdf0e10cSrcweir                              || rKey.equals( aOldKeyWithoutSlash ) )
419*cdf0e10cSrcweir 						{
420*cdf0e10cSrcweir 							rtl::OUString aNewKey
421*cdf0e10cSrcweir 								= rKey.replaceAt(
422*cdf0e10cSrcweir 									0, rOldKey.getLength(), rNewKey );
423*cdf0e10cSrcweir 							if ( !renameAdditionalPropertySet(
424*cdf0e10cSrcweir 									rKey, aNewKey, sal_False ) )
425*cdf0e10cSrcweir 								return sal_False;
426*cdf0e10cSrcweir 						}
427*cdf0e10cSrcweir 					}
428*cdf0e10cSrcweir 				}
429*cdf0e10cSrcweir 			}
430*cdf0e10cSrcweir 			else
431*cdf0e10cSrcweir 				return sal_False;
432*cdf0e10cSrcweir 		}
433*cdf0e10cSrcweir 		else
434*cdf0e10cSrcweir 			return sal_False;
435*cdf0e10cSrcweir 	}
436*cdf0e10cSrcweir 	else
437*cdf0e10cSrcweir 	{
438*cdf0e10cSrcweir 		// Get old property set, if exists.
439*cdf0e10cSrcweir 		uno::Reference< com::sun::star::ucb::XPersistentPropertySet > xOldSet
440*cdf0e10cSrcweir             = getAdditionalPropertySet( rOldKey, sal_False );
441*cdf0e10cSrcweir 		if ( xOldSet.is() )
442*cdf0e10cSrcweir 		{
443*cdf0e10cSrcweir 			// Rename property set.
444*cdf0e10cSrcweir 			uno::Reference< container::XNamed > xNamed(
445*cdf0e10cSrcweir                 xOldSet, uno::UNO_QUERY );
446*cdf0e10cSrcweir 			if ( xNamed.is() )
447*cdf0e10cSrcweir 			{
448*cdf0e10cSrcweir 				// ??? throws no exceptions and has no return value ???
449*cdf0e10cSrcweir 				xNamed->setName( rNewKey );
450*cdf0e10cSrcweir 			}
451*cdf0e10cSrcweir 			else
452*cdf0e10cSrcweir 				return sal_False;
453*cdf0e10cSrcweir 		}
454*cdf0e10cSrcweir 	}
455*cdf0e10cSrcweir 	return sal_True;
456*cdf0e10cSrcweir }
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir //=========================================================================
459*cdf0e10cSrcweir sal_Bool ContentProviderImplHelper::copyAdditionalPropertySet(
460*cdf0e10cSrcweir     const rtl::OUString& rSourceKey,
461*cdf0e10cSrcweir     const rtl::OUString& rTargetKey,
462*cdf0e10cSrcweir     sal_Bool bRecursive )
463*cdf0e10cSrcweir {
464*cdf0e10cSrcweir 	if ( rSourceKey == rTargetKey )
465*cdf0e10cSrcweir 		return sal_True;
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir 	if ( bRecursive )
470*cdf0e10cSrcweir 	{
471*cdf0e10cSrcweir 		// Get propertyset registry.
472*cdf0e10cSrcweir 		getAdditionalPropertySetRegistry();
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir 		if ( m_pImpl->m_xPropertySetRegistry.is() )
475*cdf0e10cSrcweir 		{
476*cdf0e10cSrcweir 			uno::Reference< container::XNameAccess > xNameAccess(
477*cdf0e10cSrcweir                 m_pImpl->m_xPropertySetRegistry, uno::UNO_QUERY );
478*cdf0e10cSrcweir 			if ( xNameAccess.is() )
479*cdf0e10cSrcweir 			{
480*cdf0e10cSrcweir 				uno::Sequence< rtl::OUString > aKeys
481*cdf0e10cSrcweir                     = xNameAccess->getElementNames();
482*cdf0e10cSrcweir 				sal_Int32 nCount = aKeys.getLength();
483*cdf0e10cSrcweir 				if ( nCount > 0 )
484*cdf0e10cSrcweir 				{
485*cdf0e10cSrcweir 					rtl::OUString aSrcKeyWithSlash = rSourceKey;
486*cdf0e10cSrcweir 					rtl::OUString aSrcKeyWithoutSlash;
487*cdf0e10cSrcweir 					if ( aSrcKeyWithSlash.lastIndexOf(
488*cdf0e10cSrcweir                              sal_Unicode('/')
489*cdf0e10cSrcweir                              != aSrcKeyWithSlash.getLength() - 1 ) )
490*cdf0e10cSrcweir 					{
491*cdf0e10cSrcweir 						aSrcKeyWithSlash += rtl::OUString( sal_Unicode('/') );
492*cdf0e10cSrcweir 						aSrcKeyWithoutSlash = rSourceKey;
493*cdf0e10cSrcweir 					}
494*cdf0e10cSrcweir 					else if ( rSourceKey.getLength() )
495*cdf0e10cSrcweir 						aSrcKeyWithoutSlash = rSourceKey.copy(
496*cdf0e10cSrcweir                             0, rSourceKey.getLength() - 1 );
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir 					const rtl::OUString* pKeys = aKeys.getConstArray();
499*cdf0e10cSrcweir 					for ( sal_Int32 n = 0; n < nCount; ++n )
500*cdf0e10cSrcweir 					{
501*cdf0e10cSrcweir 						const rtl::OUString& rKey = pKeys[ n ];
502*cdf0e10cSrcweir 						if ( rKey.compareTo(
503*cdf0e10cSrcweir                                  aSrcKeyWithSlash,
504*cdf0e10cSrcweir                                  aSrcKeyWithSlash.getLength() ) == 0
505*cdf0e10cSrcweir                              || rKey.equals( aSrcKeyWithoutSlash ) )
506*cdf0e10cSrcweir 						{
507*cdf0e10cSrcweir 							rtl::OUString aNewKey
508*cdf0e10cSrcweir 								= rKey.replaceAt(
509*cdf0e10cSrcweir 									0, rSourceKey.getLength(), rTargetKey );
510*cdf0e10cSrcweir 							if ( !copyAdditionalPropertySet(
511*cdf0e10cSrcweir 									rKey, aNewKey, sal_False ) )
512*cdf0e10cSrcweir 								return sal_False;
513*cdf0e10cSrcweir 						}
514*cdf0e10cSrcweir 					}
515*cdf0e10cSrcweir 				}
516*cdf0e10cSrcweir 			}
517*cdf0e10cSrcweir 			else
518*cdf0e10cSrcweir 				return sal_False;
519*cdf0e10cSrcweir 		}
520*cdf0e10cSrcweir 		else
521*cdf0e10cSrcweir 			return sal_False;
522*cdf0e10cSrcweir 	}
523*cdf0e10cSrcweir 	else
524*cdf0e10cSrcweir 	{
525*cdf0e10cSrcweir 		// Get old property set, if exists.
526*cdf0e10cSrcweir 		uno::Reference< com::sun::star::ucb::XPersistentPropertySet >
527*cdf0e10cSrcweir             xOldPropSet = getAdditionalPropertySet( rSourceKey, sal_False );
528*cdf0e10cSrcweir 		if ( !xOldPropSet.is() )
529*cdf0e10cSrcweir 			return sal_False;
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir 		uno::Reference< beans::XPropertySetInfo > xPropSetInfo
532*cdf0e10cSrcweir             = xOldPropSet->getPropertySetInfo();
533*cdf0e10cSrcweir 		if ( !xPropSetInfo.is() )
534*cdf0e10cSrcweir 			return sal_False;
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir 		uno::Reference< beans::XPropertyAccess > xOldPropAccess(
537*cdf0e10cSrcweir             xOldPropSet, uno::UNO_QUERY );
538*cdf0e10cSrcweir 		if ( !xOldPropAccess.is() )
539*cdf0e10cSrcweir 			return sal_False;
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir 		// Obtain all values from old set.
542*cdf0e10cSrcweir 	    uno::Sequence< beans::PropertyValue > aValues
543*cdf0e10cSrcweir             = xOldPropAccess->getPropertyValues();
544*cdf0e10cSrcweir 		sal_Int32 nCount = aValues.getLength();
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir 		uno::Sequence< beans::Property > aProps
547*cdf0e10cSrcweir             = xPropSetInfo->getProperties();
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir 		if ( nCount )
550*cdf0e10cSrcweir 		{
551*cdf0e10cSrcweir 			// Fail, if property set with new key already exists.
552*cdf0e10cSrcweir 			uno::Reference< com::sun::star::ucb::XPersistentPropertySet >
553*cdf0e10cSrcweir                 xNewPropSet
554*cdf0e10cSrcweir                     = getAdditionalPropertySet( rTargetKey, sal_False );
555*cdf0e10cSrcweir 			if ( xNewPropSet.is() )
556*cdf0e10cSrcweir 				return sal_False;
557*cdf0e10cSrcweir 
558*cdf0e10cSrcweir 			// Create new, empty set.
559*cdf0e10cSrcweir 			xNewPropSet = getAdditionalPropertySet( rTargetKey, sal_True );
560*cdf0e10cSrcweir 			if ( !xNewPropSet.is() )
561*cdf0e10cSrcweir 				return sal_False;
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir 			uno::Reference< beans::XPropertyContainer > xNewPropContainer(
564*cdf0e10cSrcweir                 xNewPropSet, uno::UNO_QUERY );
565*cdf0e10cSrcweir 			if ( !xNewPropContainer.is() )
566*cdf0e10cSrcweir 				return sal_False;
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir 			for ( sal_Int32 n = 0; n < nCount; ++n )
569*cdf0e10cSrcweir 			{
570*cdf0e10cSrcweir 				const beans::PropertyValue& rValue = aValues[ n ];
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir 				sal_Int16 nAttribs = 0;
573*cdf0e10cSrcweir 				for ( sal_Int32 m = 0; m < aProps.getLength(); ++m )
574*cdf0e10cSrcweir 				{
575*cdf0e10cSrcweir 					if ( aProps[ m ].Name == rValue.Name )
576*cdf0e10cSrcweir 					{
577*cdf0e10cSrcweir 						nAttribs = aProps[ m ].Attributes;
578*cdf0e10cSrcweir 						break;
579*cdf0e10cSrcweir 					}
580*cdf0e10cSrcweir 				}
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir 				try
583*cdf0e10cSrcweir 				{
584*cdf0e10cSrcweir 					xNewPropContainer->addProperty(
585*cdf0e10cSrcweir                         rValue.Name, nAttribs, rValue.Value );
586*cdf0e10cSrcweir 				}
587*cdf0e10cSrcweir 				catch ( beans::PropertyExistException & )
588*cdf0e10cSrcweir 				{
589*cdf0e10cSrcweir 				}
590*cdf0e10cSrcweir 			   	catch ( beans::IllegalTypeException & )
591*cdf0e10cSrcweir 				{
592*cdf0e10cSrcweir 				}
593*cdf0e10cSrcweir 				catch ( lang::IllegalArgumentException & )
594*cdf0e10cSrcweir 				{
595*cdf0e10cSrcweir 				}
596*cdf0e10cSrcweir 			}
597*cdf0e10cSrcweir         }
598*cdf0e10cSrcweir 	}
599*cdf0e10cSrcweir 	return sal_True;
600*cdf0e10cSrcweir }
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir //=========================================================================
603*cdf0e10cSrcweir sal_Bool ContentProviderImplHelper::removeAdditionalPropertySet(
604*cdf0e10cSrcweir     const rtl::OUString& rKey, sal_Bool bRecursive )
605*cdf0e10cSrcweir {
606*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
607*cdf0e10cSrcweir 
608*cdf0e10cSrcweir 	if ( bRecursive )
609*cdf0e10cSrcweir 	{
610*cdf0e10cSrcweir 		// Get propertyset registry.
611*cdf0e10cSrcweir 		getAdditionalPropertySetRegistry();
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir 		if ( m_pImpl->m_xPropertySetRegistry.is() )
614*cdf0e10cSrcweir 		{
615*cdf0e10cSrcweir 			uno::Reference< container::XNameAccess > xNameAccess(
616*cdf0e10cSrcweir                 m_pImpl->m_xPropertySetRegistry, uno::UNO_QUERY );
617*cdf0e10cSrcweir 			if ( xNameAccess.is() )
618*cdf0e10cSrcweir 			{
619*cdf0e10cSrcweir 				uno::Sequence< rtl::OUString > aKeys
620*cdf0e10cSrcweir                     = xNameAccess->getElementNames();
621*cdf0e10cSrcweir 				sal_Int32 nCount = aKeys.getLength();
622*cdf0e10cSrcweir 				if ( nCount > 0 )
623*cdf0e10cSrcweir 				{
624*cdf0e10cSrcweir 					rtl::OUString aKeyWithSlash = rKey;
625*cdf0e10cSrcweir 					rtl::OUString aKeyWithoutSlash;
626*cdf0e10cSrcweir 					if ( aKeyWithSlash.lastIndexOf(
627*cdf0e10cSrcweir                              sal_Unicode('/')
628*cdf0e10cSrcweir                              != aKeyWithSlash.getLength() - 1 ) )
629*cdf0e10cSrcweir 					{
630*cdf0e10cSrcweir 						aKeyWithSlash += rtl::OUString( (sal_Unicode)'/' );
631*cdf0e10cSrcweir 						aKeyWithoutSlash = rKey;
632*cdf0e10cSrcweir 					}
633*cdf0e10cSrcweir 					else if ( rKey.getLength() )
634*cdf0e10cSrcweir 						aKeyWithoutSlash
635*cdf0e10cSrcweir                             = rKey.copy( 0, rKey.getLength() - 1 );
636*cdf0e10cSrcweir 
637*cdf0e10cSrcweir 					const rtl::OUString* pKeys = aKeys.getConstArray();
638*cdf0e10cSrcweir 					for ( sal_Int32 n = 0; n < nCount; ++n )
639*cdf0e10cSrcweir 					{
640*cdf0e10cSrcweir 						const rtl::OUString& rCurrKey = pKeys[ n ];
641*cdf0e10cSrcweir 						if ( rCurrKey.compareTo(
642*cdf0e10cSrcweir                                  aKeyWithSlash,
643*cdf0e10cSrcweir                                  aKeyWithSlash.getLength() ) == 0
644*cdf0e10cSrcweir                              || rCurrKey.equals( aKeyWithoutSlash ) )
645*cdf0e10cSrcweir 						{
646*cdf0e10cSrcweir 							if ( !removeAdditionalPropertySet(
647*cdf0e10cSrcweir                                      rCurrKey, sal_False ) )
648*cdf0e10cSrcweir 								return sal_False;
649*cdf0e10cSrcweir 						}
650*cdf0e10cSrcweir 					}
651*cdf0e10cSrcweir 				}
652*cdf0e10cSrcweir 			}
653*cdf0e10cSrcweir 			else
654*cdf0e10cSrcweir 				return sal_False;
655*cdf0e10cSrcweir 		}
656*cdf0e10cSrcweir 		else
657*cdf0e10cSrcweir 			return sal_False;
658*cdf0e10cSrcweir 	}
659*cdf0e10cSrcweir 	else
660*cdf0e10cSrcweir 	{
661*cdf0e10cSrcweir 		// Get propertyset registry.
662*cdf0e10cSrcweir 		getAdditionalPropertySetRegistry();
663*cdf0e10cSrcweir 
664*cdf0e10cSrcweir 		if ( m_pImpl->m_xPropertySetRegistry.is() )
665*cdf0e10cSrcweir 			m_pImpl->m_xPropertySetRegistry->removePropertySet( rKey );
666*cdf0e10cSrcweir 		else
667*cdf0e10cSrcweir 			return sal_False;
668*cdf0e10cSrcweir 	}
669*cdf0e10cSrcweir 	return sal_True;
670*cdf0e10cSrcweir }
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir } // namespace ucbhelper
673