1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/beans/XPropertyAccess.hpp>
21 #include <com/sun/star/container/XNameAccess.hpp>
22 #include <com/sun/star/container/XNamed.hpp>
23 #include <com/sun/star/ucb/Store.hpp>
24 #include <com/sun/star/ucb/XPropertySetRegistry.hpp>
25 #include <com/sun/star/ucb/XPropertySetRegistryFactory.hpp>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <ucbhelper/contenthelper.hxx>
28 #include <ucbhelper/contentidentifier.hxx>
29 #include <ucbhelper/providerhelper.hxx>
31 #include "osl/diagnose.h"
32 #include "osl/mutex.hxx"
33 #include "cppuhelper/weakref.hxx"
35 #include <unordered_map>
37 using namespace com::sun::star
;
39 namespace ucbhelper_impl
42 typedef std::unordered_map
45 uno::WeakReference
< ucb::XContent
>,
50 struct ContentProviderImplHelper_Impl
52 uno::Reference
< com::sun::star::ucb::XPropertySetRegistry
>
53 m_xPropertySetRegistry
;
58 } // namespace ucbhelper_impl
62 ContentProviderImplHelper::ContentProviderImplHelper(
63 const uno::Reference
< uno::XComponentContext
>& rxContext
)
64 : m_pImpl( new ucbhelper_impl::ContentProviderImplHelper_Impl
),
65 m_xContext( rxContext
)
70 ContentProviderImplHelper::~ContentProviderImplHelper()
76 void SAL_CALL
ContentProviderImplHelper::acquire()
79 OWeakObject::acquire();
82 void SAL_CALL
ContentProviderImplHelper::release()
85 OWeakObject::release();
88 css::uno::Any SAL_CALL
ContentProviderImplHelper::queryInterface( const css::uno::Type
& rType
)
89 throw( css::uno::RuntimeException
, std::exception
)
91 css::uno::Any aRet
= cppu::queryInterface( rType
,
92 (static_cast< lang::XTypeProvider
* >(this)),
93 (static_cast< lang::XServiceInfo
* >(this)),
94 (static_cast< css::ucb::XContentProvider
* >(this))
96 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
99 XTYPEPROVIDER_IMPL_3( ContentProviderImplHelper
,
102 com::sun::star::ucb::XContentProvider
);
105 sal_Bool SAL_CALL
ContentProviderImplHelper::supportsService(
106 const OUString
& ServiceName
)
107 throw( uno::RuntimeException
, std::exception
)
109 return cppu::supportsService(this, ServiceName
);
113 sal_Int32 SAL_CALL
ContentProviderImplHelper::compareContentIds(
114 const uno::Reference
< com::sun::star::ucb::XContentIdentifier
>& Id1
,
115 const uno::Reference
< com::sun::star::ucb::XContentIdentifier
>& Id2
)
116 throw( uno::RuntimeException
, std::exception
)
118 // Simply do a string compare.
120 OUString
aURL1( Id1
->getContentIdentifier() );
121 OUString
aURL2( Id2
->getContentIdentifier() );
123 return aURL1
.compareTo( aURL2
);
126 void ContentProviderImplHelper::cleanupRegisteredContents()
128 osl::MutexGuard
aGuard( m_aMutex
);
130 ucbhelper_impl::Contents::iterator it
131 = m_pImpl
->m_aContents
.begin();
132 while( it
!= m_pImpl
->m_aContents
.end() )
134 uno::Reference
< ucb::XContent
> xContent( (*it
).second
);
135 if ( !xContent
.is() )
137 ucbhelper_impl::Contents::iterator tmp
= it
;
139 m_pImpl
->m_aContents
.erase( tmp
);
148 void ContentProviderImplHelper::removeContent( ContentImplHelper
* pContent
)
150 osl::MutexGuard
aGuard( m_aMutex
);
152 cleanupRegisteredContents();
155 pContent
->getIdentifier()->getContentIdentifier() );
157 ucbhelper_impl::Contents::iterator it
= m_pImpl
->m_aContents
.find( aURL
);
159 if ( it
!= m_pImpl
->m_aContents
.end() )
160 m_pImpl
->m_aContents
.erase( it
);
163 rtl::Reference
< ContentImplHelper
>
164 ContentProviderImplHelper::queryExistingContent(
165 const uno::Reference
< com::sun::star::ucb::XContentIdentifier
>&
168 return queryExistingContent( Identifier
->getContentIdentifier() );
171 rtl::Reference
< ContentImplHelper
>
172 ContentProviderImplHelper::queryExistingContent( const OUString
& rURL
)
174 osl::MutexGuard
aGuard( m_aMutex
);
176 cleanupRegisteredContents();
178 // Check, if a content with given id already exists...
180 ucbhelper_impl::Contents::const_iterator it
181 = m_pImpl
->m_aContents
.find( rURL
);
182 if ( it
!= m_pImpl
->m_aContents
.end() )
184 uno::Reference
< ucb::XContent
> xContent( (*it
).second
);
187 return rtl::Reference
< ContentImplHelper
>(
188 static_cast< ContentImplHelper
* >( xContent
.get() ) );
191 return rtl::Reference
< ContentImplHelper
>();
194 void ContentProviderImplHelper::queryExistingContents(
195 ContentRefList
& rContents
)
197 osl::MutexGuard
aGuard( m_aMutex
);
199 cleanupRegisteredContents();
201 ucbhelper_impl::Contents::const_iterator it
202 = m_pImpl
->m_aContents
.begin();
203 ucbhelper_impl::Contents::const_iterator end
204 = m_pImpl
->m_aContents
.end();
208 uno::Reference
< ucb::XContent
> xContent( (*it
).second
);
212 rtl::Reference
< ContentImplHelper
>(
213 static_cast< ContentImplHelper
* >( xContent
.get() ) ) );
219 void ContentProviderImplHelper::registerNewContent(
220 const uno::Reference
< ucb::XContent
> & xContent
)
224 osl::MutexGuard
aGuard( m_aMutex
);
226 cleanupRegisteredContents();
229 xContent
->getIdentifier()->getContentIdentifier() );
230 ucbhelper_impl::Contents::const_iterator it
231 = m_pImpl
->m_aContents
.find( aURL
);
232 if ( it
== m_pImpl
->m_aContents
.end() )
233 m_pImpl
->m_aContents
[ aURL
] = xContent
;
237 uno::Reference
< com::sun::star::ucb::XPropertySetRegistry
>
238 ContentProviderImplHelper::getAdditionalPropertySetRegistry()
240 // Get propertyset registry.
242 osl::MutexGuard
aGuard( m_aMutex
);
244 if ( !m_pImpl
->m_xPropertySetRegistry
.is() )
246 uno::Reference
< com::sun::star::ucb::XPropertySetRegistryFactory
>
247 xRegFac
= com::sun::star::ucb::Store::create( m_xContext
);
249 // Open/create a registry.
250 m_pImpl
->m_xPropertySetRegistry
251 = xRegFac
->createPropertySetRegistry( OUString() );
253 OSL_ENSURE( m_pImpl
->m_xPropertySetRegistry
.is(),
254 "ContentProviderImplHelper::getAdditionalPropertySet - "
255 "Error opening registry!" );
258 return m_pImpl
->m_xPropertySetRegistry
;
261 uno::Reference
< com::sun::star::ucb::XPersistentPropertySet
>
262 ContentProviderImplHelper::getAdditionalPropertySet(
263 const OUString
& rKey
, bool bCreate
)
265 // Get propertyset registry.
266 getAdditionalPropertySetRegistry();
268 if ( m_pImpl
->m_xPropertySetRegistry
.is() )
270 // Open/create persistent property set.
271 return uno::Reference
< com::sun::star::ucb::XPersistentPropertySet
>(
272 m_pImpl
->m_xPropertySetRegistry
->openPropertySet(
276 return uno::Reference
< com::sun::star::ucb::XPersistentPropertySet
>();
279 bool ContentProviderImplHelper::renameAdditionalPropertySet(
280 const OUString
& rOldKey
,
281 const OUString
& rNewKey
,
284 if ( rOldKey
== rNewKey
)
287 osl::MutexGuard
aGuard( m_aMutex
);
291 // Get propertyset registry.
292 getAdditionalPropertySetRegistry();
294 if ( m_pImpl
->m_xPropertySetRegistry
.is() )
296 uno::Reference
< container::XNameAccess
> xNameAccess(
297 m_pImpl
->m_xPropertySetRegistry
, uno::UNO_QUERY
);
298 if ( xNameAccess
.is() )
300 uno::Sequence
< OUString
> aKeys
301 = xNameAccess
->getElementNames();
302 sal_Int32 nCount
= aKeys
.getLength();
305 OUString aOldKeyWithSlash
= rOldKey
;
306 OUString aOldKeyWithoutSlash
;
307 if ( !aOldKeyWithSlash
.endsWith("/") )
309 aOldKeyWithSlash
+= OUString( '/' );
310 aOldKeyWithoutSlash
= rOldKey
;
312 else if ( !rOldKey
.isEmpty() )
314 = rOldKey
.copy( 0, rOldKey
.getLength() - 1 );
316 const OUString
* pKeys
= aKeys
.getConstArray();
317 for ( sal_Int32 n
= 0; n
< nCount
; ++n
)
319 const OUString
& rKey
= pKeys
[ n
];
322 aOldKeyWithSlash
.getLength() ) == 0
323 || rKey
.equals( aOldKeyWithoutSlash
) )
327 0, rOldKey
.getLength(), rNewKey
);
328 if ( !renameAdditionalPropertySet(
329 rKey
, aNewKey
, false ) )
343 // Get old property set, if exists.
344 uno::Reference
< com::sun::star::ucb::XPersistentPropertySet
> xOldSet
345 = getAdditionalPropertySet( rOldKey
, false );
348 // Rename property set.
349 uno::Reference
< container::XNamed
> xNamed(
350 xOldSet
, uno::UNO_QUERY
);
353 // ??? throws no exceptions and has no return value ???
354 xNamed
->setName( rNewKey
);
363 bool ContentProviderImplHelper::copyAdditionalPropertySet(
364 const OUString
& rSourceKey
,
365 const OUString
& rTargetKey
,
368 if ( rSourceKey
== rTargetKey
)
371 osl::MutexGuard
aGuard( m_aMutex
);
375 // Get propertyset registry.
376 getAdditionalPropertySetRegistry();
378 if ( m_pImpl
->m_xPropertySetRegistry
.is() )
380 uno::Reference
< container::XNameAccess
> xNameAccess(
381 m_pImpl
->m_xPropertySetRegistry
, uno::UNO_QUERY
);
382 if ( xNameAccess
.is() )
384 uno::Sequence
< OUString
> aKeys
385 = xNameAccess
->getElementNames();
386 sal_Int32 nCount
= aKeys
.getLength();
389 OUString aSrcKeyWithSlash
= rSourceKey
;
390 OUString aSrcKeyWithoutSlash
;
391 if ( !aSrcKeyWithSlash
.endsWith("/") )
393 aSrcKeyWithSlash
+= OUString( '/' );
394 aSrcKeyWithoutSlash
= rSourceKey
;
396 else if ( !rSourceKey
.isEmpty() )
397 aSrcKeyWithoutSlash
= rSourceKey
.copy(
398 0, rSourceKey
.getLength() - 1 );
400 const OUString
* pKeys
= aKeys
.getConstArray();
401 for ( sal_Int32 n
= 0; n
< nCount
; ++n
)
403 const OUString
& rKey
= pKeys
[ n
];
406 aSrcKeyWithSlash
.getLength() ) == 0
407 || rKey
.equals( aSrcKeyWithoutSlash
) )
411 0, rSourceKey
.getLength(), rTargetKey
);
412 if ( !copyAdditionalPropertySet(
413 rKey
, aNewKey
, false ) )
427 // Get old property set, if exists.
428 uno::Reference
< com::sun::star::ucb::XPersistentPropertySet
>
429 xOldPropSet
= getAdditionalPropertySet( rSourceKey
, false );
430 if ( !xOldPropSet
.is() )
433 uno::Reference
< beans::XPropertySetInfo
> xPropSetInfo
434 = xOldPropSet
->getPropertySetInfo();
435 if ( !xPropSetInfo
.is() )
438 uno::Reference
< beans::XPropertyAccess
> xOldPropAccess(
439 xOldPropSet
, uno::UNO_QUERY
);
440 if ( !xOldPropAccess
.is() )
443 // Obtain all values from old set.
444 uno::Sequence
< beans::PropertyValue
> aValues
445 = xOldPropAccess
->getPropertyValues();
446 sal_Int32 nCount
= aValues
.getLength();
448 uno::Sequence
< beans::Property
> aProps
449 = xPropSetInfo
->getProperties();
453 // Fail, if property set with new key already exists.
454 uno::Reference
< com::sun::star::ucb::XPersistentPropertySet
>
456 = getAdditionalPropertySet( rTargetKey
, false );
457 if ( xNewPropSet
.is() )
460 // Create new, empty set.
461 xNewPropSet
= getAdditionalPropertySet( rTargetKey
, true );
462 if ( !xNewPropSet
.is() )
465 uno::Reference
< beans::XPropertyContainer
> xNewPropContainer(
466 xNewPropSet
, uno::UNO_QUERY
);
467 if ( !xNewPropContainer
.is() )
470 for ( sal_Int32 n
= 0; n
< nCount
; ++n
)
472 const beans::PropertyValue
& rValue
= aValues
[ n
];
474 sal_Int16 nAttribs
= 0;
475 for ( sal_Int32 m
= 0; m
< aProps
.getLength(); ++m
)
477 if ( aProps
[ m
].Name
== rValue
.Name
)
479 nAttribs
= aProps
[ m
].Attributes
;
486 xNewPropContainer
->addProperty(
487 rValue
.Name
, nAttribs
, rValue
.Value
);
489 catch ( beans::PropertyExistException
& )
492 catch ( beans::IllegalTypeException
& )
495 catch ( lang::IllegalArgumentException
& )
504 bool ContentProviderImplHelper::removeAdditionalPropertySet(
505 const OUString
& rKey
, bool bRecursive
)
507 osl::MutexGuard
aGuard( m_aMutex
);
511 // Get propertyset registry.
512 getAdditionalPropertySetRegistry();
514 if ( m_pImpl
->m_xPropertySetRegistry
.is() )
516 uno::Reference
< container::XNameAccess
> xNameAccess(
517 m_pImpl
->m_xPropertySetRegistry
, uno::UNO_QUERY
);
518 if ( xNameAccess
.is() )
520 uno::Sequence
< OUString
> aKeys
521 = xNameAccess
->getElementNames();
522 sal_Int32 nCount
= aKeys
.getLength();
525 OUString aKeyWithSlash
= rKey
;
526 OUString aKeyWithoutSlash
;
527 if ( !aKeyWithSlash
.endsWith("/") )
529 aKeyWithSlash
+= OUString( '/' );
530 aKeyWithoutSlash
= rKey
;
532 else if ( !rKey
.isEmpty() )
534 = rKey
.copy( 0, rKey
.getLength() - 1 );
536 const OUString
* pKeys
= aKeys
.getConstArray();
537 for ( sal_Int32 n
= 0; n
< nCount
; ++n
)
539 const OUString
& rCurrKey
= pKeys
[ n
];
540 if ( rCurrKey
.compareTo(
542 aKeyWithSlash
.getLength() ) == 0
543 || rCurrKey
.equals( aKeyWithoutSlash
) )
545 if ( !removeAdditionalPropertySet(
560 // Get propertyset registry.
561 getAdditionalPropertySetRegistry();
563 if ( m_pImpl
->m_xPropertySetRegistry
.is() )
564 m_pImpl
->m_xPropertySetRegistry
->removePropertySet( rKey
);
571 } // namespace ucbhelper
573 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */