fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucbhelper / source / provider / providerhelper.cxx
blob67357d687ddd016461c7048833011a26d0db2499
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
44 OUString,
45 uno::WeakReference< ucb::XContent >,
46 OUStringHash
48 Contents;
50 struct ContentProviderImplHelper_Impl
52 uno::Reference< com::sun::star::ucb::XPropertySetRegistry >
53 m_xPropertySetRegistry;
54 Contents
55 m_aContents;
58 } // namespace ucbhelper_impl
60 namespace ucbhelper {
62 ContentProviderImplHelper::ContentProviderImplHelper(
63 const uno::Reference< uno::XComponentContext >& rxContext )
64 : m_pImpl( new ucbhelper_impl::ContentProviderImplHelper_Impl ),
65 m_xContext( rxContext )
69 // virtual
70 ContentProviderImplHelper::~ContentProviderImplHelper()
72 delete m_pImpl;
75 // XInterface
76 void SAL_CALL ContentProviderImplHelper::acquire()
77 throw()
79 OWeakObject::acquire();
82 void SAL_CALL ContentProviderImplHelper::release()
83 throw()
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,
100 lang::XTypeProvider,
101 lang::XServiceInfo,
102 com::sun::star::ucb::XContentProvider );
104 // virtual
105 sal_Bool SAL_CALL ContentProviderImplHelper::supportsService(
106 const OUString& ServiceName )
107 throw( uno::RuntimeException, std::exception )
109 return cppu::supportsService(this, ServiceName);
112 // virtual
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;
138 ++it;
139 m_pImpl->m_aContents.erase( tmp );
141 else
143 ++it;
148 void ContentProviderImplHelper::removeContent( ContentImplHelper* pContent )
150 osl::MutexGuard aGuard( m_aMutex );
152 cleanupRegisteredContents();
154 const OUString aURL(
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 >&
166 Identifier )
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 );
185 if ( xContent.is() )
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();
206 while ( it != end )
208 uno::Reference< ucb::XContent > xContent( (*it).second );
209 if ( xContent.is() )
211 rContents.push_back(
212 rtl::Reference< ContentImplHelper >(
213 static_cast< ContentImplHelper * >( xContent.get() ) ) );
215 ++it;
219 void ContentProviderImplHelper::registerNewContent(
220 const uno::Reference< ucb::XContent > & xContent )
222 if ( xContent.is() )
224 osl::MutexGuard aGuard( m_aMutex );
226 cleanupRegisteredContents();
228 const OUString aURL(
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(
273 rKey, bCreate ) );
276 return uno::Reference< com::sun::star::ucb::XPersistentPropertySet >();
279 bool ContentProviderImplHelper::renameAdditionalPropertySet(
280 const OUString& rOldKey,
281 const OUString& rNewKey,
282 bool bRecursive )
284 if ( rOldKey == rNewKey )
285 return true;
287 osl::MutexGuard aGuard( m_aMutex );
289 if ( bRecursive )
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();
303 if ( nCount > 0 )
305 OUString aOldKeyWithSlash = rOldKey;
306 OUString aOldKeyWithoutSlash;
307 if ( !aOldKeyWithSlash.endsWith("/") )
309 aOldKeyWithSlash += OUString( '/' );
310 aOldKeyWithoutSlash = rOldKey;
312 else if ( !rOldKey.isEmpty() )
313 aOldKeyWithoutSlash
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 ];
320 if ( rKey.compareTo(
321 aOldKeyWithSlash,
322 aOldKeyWithSlash.getLength() ) == 0
323 || rKey.equals( aOldKeyWithoutSlash ) )
325 OUString aNewKey
326 = rKey.replaceAt(
327 0, rOldKey.getLength(), rNewKey );
328 if ( !renameAdditionalPropertySet(
329 rKey, aNewKey, false ) )
330 return false;
335 else
336 return false;
338 else
339 return false;
341 else
343 // Get old property set, if exists.
344 uno::Reference< com::sun::star::ucb::XPersistentPropertySet > xOldSet
345 = getAdditionalPropertySet( rOldKey, false );
346 if ( xOldSet.is() )
348 // Rename property set.
349 uno::Reference< container::XNamed > xNamed(
350 xOldSet, uno::UNO_QUERY );
351 if ( xNamed.is() )
353 // ??? throws no exceptions and has no return value ???
354 xNamed->setName( rNewKey );
356 else
357 return false;
360 return true;
363 bool ContentProviderImplHelper::copyAdditionalPropertySet(
364 const OUString& rSourceKey,
365 const OUString& rTargetKey,
366 bool bRecursive )
368 if ( rSourceKey == rTargetKey )
369 return true;
371 osl::MutexGuard aGuard( m_aMutex );
373 if ( bRecursive )
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();
387 if ( nCount > 0 )
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 ];
404 if ( rKey.compareTo(
405 aSrcKeyWithSlash,
406 aSrcKeyWithSlash.getLength() ) == 0
407 || rKey.equals( aSrcKeyWithoutSlash ) )
409 OUString aNewKey
410 = rKey.replaceAt(
411 0, rSourceKey.getLength(), rTargetKey );
412 if ( !copyAdditionalPropertySet(
413 rKey, aNewKey, false ) )
414 return false;
419 else
420 return false;
422 else
423 return false;
425 else
427 // Get old property set, if exists.
428 uno::Reference< com::sun::star::ucb::XPersistentPropertySet >
429 xOldPropSet = getAdditionalPropertySet( rSourceKey, false );
430 if ( !xOldPropSet.is() )
431 return false;
433 uno::Reference< beans::XPropertySetInfo > xPropSetInfo
434 = xOldPropSet->getPropertySetInfo();
435 if ( !xPropSetInfo.is() )
436 return false;
438 uno::Reference< beans::XPropertyAccess > xOldPropAccess(
439 xOldPropSet, uno::UNO_QUERY );
440 if ( !xOldPropAccess.is() )
441 return false;
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();
451 if ( nCount )
453 // Fail, if property set with new key already exists.
454 uno::Reference< com::sun::star::ucb::XPersistentPropertySet >
455 xNewPropSet
456 = getAdditionalPropertySet( rTargetKey, false );
457 if ( xNewPropSet.is() )
458 return false;
460 // Create new, empty set.
461 xNewPropSet = getAdditionalPropertySet( rTargetKey, true );
462 if ( !xNewPropSet.is() )
463 return false;
465 uno::Reference< beans::XPropertyContainer > xNewPropContainer(
466 xNewPropSet, uno::UNO_QUERY );
467 if ( !xNewPropContainer.is() )
468 return false;
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;
480 break;
486 xNewPropContainer->addProperty(
487 rValue.Name, nAttribs, rValue.Value );
489 catch ( beans::PropertyExistException & )
492 catch ( beans::IllegalTypeException & )
495 catch ( lang::IllegalArgumentException & )
501 return true;
504 bool ContentProviderImplHelper::removeAdditionalPropertySet(
505 const OUString& rKey, bool bRecursive )
507 osl::MutexGuard aGuard( m_aMutex );
509 if ( bRecursive )
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();
523 if ( nCount > 0 )
525 OUString aKeyWithSlash = rKey;
526 OUString aKeyWithoutSlash;
527 if ( !aKeyWithSlash.endsWith("/") )
529 aKeyWithSlash += OUString( '/' );
530 aKeyWithoutSlash = rKey;
532 else if ( !rKey.isEmpty() )
533 aKeyWithoutSlash
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(
541 aKeyWithSlash,
542 aKeyWithSlash.getLength() ) == 0
543 || rCurrKey.equals( aKeyWithoutSlash ) )
545 if ( !removeAdditionalPropertySet(
546 rCurrKey, false ) )
547 return false;
552 else
553 return false;
555 else
556 return false;
558 else
560 // Get propertyset registry.
561 getAdditionalPropertySetRegistry();
563 if ( m_pImpl->m_xPropertySetRegistry.is() )
564 m_pImpl->m_xPropertySetRegistry->removePropertySet( rKey );
565 else
566 return false;
568 return true;
571 } // namespace ucbhelper
573 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */