LibreOffice Module ucbhelper (master)  1
providerhelper.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <com/sun/star/beans/IllegalTypeException.hpp>
23 #include <com/sun/star/beans/PropertyExistException.hpp>
24 #include <com/sun/star/beans/XPropertyAccess.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/container/XNamed.hpp>
27 #include <com/sun/star/ucb/Store.hpp>
28 #include <com/sun/star/ucb/XPropertySetRegistry.hpp>
29 #include <com/sun/star/ucb/XPropertySetRegistryFactory.hpp>
34 #include <ucbhelper/macros.hxx>
35 
36 #include <osl/diagnose.h>
37 #include <osl/mutex.hxx>
38 #include <cppuhelper/weakref.hxx>
39 
40 #include <unordered_map>
41 
42 using namespace com::sun::star;
43 
44 namespace ucbhelper_impl
45 {
46 
47 typedef std::unordered_map
48 <
49  OUString,
50  uno::WeakReference< ucb::XContent >
51 >
53 
55 {
56  uno::Reference< css::ucb::XPropertySetRegistry > m_xPropertySetRegistry;
58 };
59 
60 } // namespace ucbhelper_impl
61 
62 namespace ucbhelper {
63 
64 ContentProviderImplHelper::ContentProviderImplHelper(
65  const uno::Reference< uno::XComponentContext >& rxContext )
66 : m_pImpl( new ucbhelper_impl::ContentProviderImplHelper_Impl ),
67  m_xContext( rxContext )
68 {
69 }
70 
71 // virtual
73 {
74 }
75 
76 // virtual
78  const OUString& ServiceName )
79 {
80  return cppu::supportsService(this, ServiceName);
81 }
82 
83 // virtual
85  const uno::Reference< css::ucb::XContentIdentifier >& Id1,
86  const uno::Reference< css::ucb::XContentIdentifier >& Id2 )
87 {
88  // Simply do a string compare.
89 
90  OUString aURL1( Id1->getContentIdentifier() );
91  OUString aURL2( Id2->getContentIdentifier() );
92 
93  return aURL1.compareTo( aURL2 );
94 }
95 
97 {
98  osl::MutexGuard aGuard( m_aMutex );
99 
100  ucbhelper_impl::Contents::iterator it
101  = m_pImpl->m_aContents.begin();
102  while( it != m_pImpl->m_aContents.end() )
103  {
104  uno::Reference< ucb::XContent > xContent( (*it).second );
105  if ( !xContent.is() )
106  {
107  ucbhelper_impl::Contents::iterator tmp = it;
108  ++it;
109  m_pImpl->m_aContents.erase( tmp );
110  }
111  else
112  {
113  ++it;
114  }
115  }
116 }
117 
119 {
120  osl::MutexGuard aGuard( m_aMutex );
121 
123 
124  const OUString aURL(
125  pContent->getIdentifier()->getContentIdentifier() );
126 
127  ucbhelper_impl::Contents::iterator it = m_pImpl->m_aContents.find( aURL );
128 
129  if ( it != m_pImpl->m_aContents.end() )
130  m_pImpl->m_aContents.erase( it );
131 }
132 
135  const uno::Reference< css::ucb::XContentIdentifier >& Identifier )
136 {
137  return queryExistingContent( Identifier->getContentIdentifier() );
138 }
139 
142 {
143  osl::MutexGuard aGuard( m_aMutex );
144 
146 
147  // Check, if a content with given id already exists...
148 
149  ucbhelper_impl::Contents::const_iterator it
150  = m_pImpl->m_aContents.find( rURL );
151  if ( it != m_pImpl->m_aContents.end() )
152  {
153  uno::Reference< ucb::XContent > xContent( (*it).second );
154  if ( xContent.is() )
155  {
157  static_cast< ContentImplHelper * >( xContent.get() ) );
158  }
159  }
161 }
162 
164  ContentRefList& rContents )
165 {
166  osl::MutexGuard aGuard( m_aMutex );
167 
169 
170  for ( const auto& rContent : m_pImpl->m_aContents )
171  {
172  uno::Reference< ucb::XContent > xContent( rContent.second );
173  if ( xContent.is() )
174  {
175  rContents.emplace_back(
176  static_cast< ContentImplHelper * >( xContent.get() ) );
177  }
178  }
179 }
180 
182  const uno::Reference< ucb::XContent > & xContent )
183 {
184  if ( !xContent.is() )
185  return;
186 
187  osl::MutexGuard aGuard( m_aMutex );
188 
190 
191  const OUString aURL(
192  xContent->getIdentifier()->getContentIdentifier() );
193  ucbhelper_impl::Contents::const_iterator it
194  = m_pImpl->m_aContents.find( aURL );
195  if ( it == m_pImpl->m_aContents.end() )
196  m_pImpl->m_aContents[ aURL ] = xContent;
197 }
198 
199 uno::Reference< css::ucb::XPropertySetRegistry >
201 {
202  // Get propertyset registry.
203 
204  osl::MutexGuard aGuard( m_aMutex );
205 
206  if ( !m_pImpl->m_xPropertySetRegistry.is() )
207  {
208  uno::Reference< css::ucb::XPropertySetRegistryFactory >
209  xRegFac = css::ucb::Store::create( m_xContext );
210 
211  // Open/create a registry.
212  m_pImpl->m_xPropertySetRegistry
213  = xRegFac->createPropertySetRegistry( OUString() );
214 
215  OSL_ENSURE( m_pImpl->m_xPropertySetRegistry.is(),
216  "ContentProviderImplHelper::getAdditionalPropertySet - "
217  "Error opening registry!" );
218  }
219 
220  return m_pImpl->m_xPropertySetRegistry;
221 }
222 
223 uno::Reference< css::ucb::XPersistentPropertySet >
225  const OUString& rKey, bool bCreate )
226 {
227  // Get propertyset registry.
229 
230  if ( m_pImpl->m_xPropertySetRegistry.is() )
231  {
232  // Open/create persistent property set.
233  return m_pImpl->m_xPropertySetRegistry->openPropertySet(
234  rKey, bCreate );
235  }
236 
237  return uno::Reference< css::ucb::XPersistentPropertySet >();
238 }
239 
241  const OUString& rOldKey,
242  const OUString& rNewKey,
243  bool bRecursive )
244 {
245  if ( rOldKey == rNewKey )
246  return true;
247 
248  osl::MutexGuard aGuard( m_aMutex );
249 
250  if ( bRecursive )
251  {
252  // Get propertyset registry.
254 
255  if ( !m_pImpl->m_xPropertySetRegistry.is() )
256  return false;
257 
258  uno::Reference< container::XNameAccess > xNameAccess(
259  m_pImpl->m_xPropertySetRegistry, uno::UNO_QUERY );
260  if ( !xNameAccess.is() )
261  return false;
262 
263  const uno::Sequence< OUString > aKeys
264  = xNameAccess->getElementNames();
265  if ( aKeys.hasElements() )
266  {
267  OUString aOldKeyWithSlash = rOldKey;
268  OUString aOldKeyWithoutSlash;
269  if ( !aOldKeyWithSlash.endsWith("/") )
270  {
271  aOldKeyWithSlash += "/";
272  aOldKeyWithoutSlash = rOldKey;
273  }
274  else if ( !rOldKey.isEmpty() )
275  aOldKeyWithoutSlash
276  = rOldKey.copy( 0, rOldKey.getLength() - 1 );
277 
278  for ( const OUString& rKey : aKeys )
279  {
280  if ( rKey.startsWith( aOldKeyWithSlash )
281  || rKey == aOldKeyWithoutSlash )
282  {
283  OUString aNewKey
284  = rKey.replaceAt(
285  0, rOldKey.getLength(), rNewKey );
287  rKey, aNewKey, false ) )
288  return false;
289  }
290  }
291  }
292  }
293  else
294  {
295  // Get old property set, if exists.
296  uno::Reference< css::ucb::XPersistentPropertySet > xOldSet
297  = getAdditionalPropertySet( rOldKey, false );
298  if ( xOldSet.is() )
299  {
300  // Rename property set.
301  uno::Reference< container::XNamed > xNamed(
302  xOldSet, uno::UNO_QUERY );
303  if ( !xNamed.is() )
304  return false;
305 
306  // ??? throws no exceptions and has no return value ???
307  xNamed->setName( rNewKey );
308  }
309  }
310  return true;
311 }
312 
314  const OUString& rSourceKey,
315  const OUString& rTargetKey,
316  bool bRecursive )
317 {
318  if ( rSourceKey == rTargetKey )
319  return true;
320 
321  osl::MutexGuard aGuard( m_aMutex );
322 
323  if ( bRecursive )
324  {
325  // Get propertyset registry.
327 
328  if ( !m_pImpl->m_xPropertySetRegistry.is() )
329  return false;
330 
331  uno::Reference< container::XNameAccess > xNameAccess(
332  m_pImpl->m_xPropertySetRegistry, uno::UNO_QUERY );
333  if ( !xNameAccess.is() )
334  return false;
335 
336  const uno::Sequence< OUString > aKeys
337  = xNameAccess->getElementNames();
338  if ( aKeys.hasElements() )
339  {
340  OUString aSrcKeyWithSlash = rSourceKey;
341  OUString aSrcKeyWithoutSlash;
342  if ( !aSrcKeyWithSlash.endsWith("/") )
343  {
344  aSrcKeyWithSlash += "/";
345  aSrcKeyWithoutSlash = rSourceKey;
346  }
347  else if ( !rSourceKey.isEmpty() )
348  aSrcKeyWithoutSlash = rSourceKey.copy(
349  0, rSourceKey.getLength() - 1 );
350 
351  for ( const OUString& rKey : aKeys )
352  {
353  if ( rKey.startsWith(aSrcKeyWithSlash )
354  || rKey == aSrcKeyWithoutSlash )
355  {
356  OUString aNewKey
357  = rKey.replaceAt(
358  0, rSourceKey.getLength(), rTargetKey );
360  rKey, aNewKey, false ) )
361  return false;
362  }
363  }
364  }
365  }
366  else
367  {
368  // Get old property set, if exists.
369  uno::Reference< css::ucb::XPersistentPropertySet >
370  xOldPropSet = getAdditionalPropertySet( rSourceKey, false );
371  if ( !xOldPropSet.is() )
372  return false;
373 
374  uno::Reference< beans::XPropertySetInfo > xPropSetInfo
375  = xOldPropSet->getPropertySetInfo();
376  if ( !xPropSetInfo.is() )
377  return false;
378 
379  uno::Reference< beans::XPropertyAccess > xOldPropAccess(
380  xOldPropSet, uno::UNO_QUERY );
381  if ( !xOldPropAccess.is() )
382  return false;
383 
384  // Obtain all values from old set.
385  const uno::Sequence< beans::PropertyValue > aValues
386  = xOldPropAccess->getPropertyValues();
387 
388  uno::Sequence< beans::Property > aProps
389  = xPropSetInfo->getProperties();
390 
391  if ( aValues.hasElements() )
392  {
393  // Fail, if property set with new key already exists.
394  uno::Reference< css::ucb::XPersistentPropertySet >
395  xNewPropSet
396  = getAdditionalPropertySet( rTargetKey, false );
397  if ( xNewPropSet.is() )
398  return false;
399 
400  // Create new, empty set.
401  xNewPropSet = getAdditionalPropertySet( rTargetKey, true );
402  if ( !xNewPropSet.is() )
403  return false;
404 
405  uno::Reference< beans::XPropertyContainer > xNewPropContainer(
406  xNewPropSet, uno::UNO_QUERY );
407  if ( !xNewPropContainer.is() )
408  return false;
409 
410  for ( const beans::PropertyValue& rValue : aValues )
411  {
412  sal_Int16 nAttribs = 0;
413  auto pProp = std::find_if(aProps.begin(), aProps.end(),
414  [&rValue](const beans::Property& rProp) { return rProp.Name == rValue.Name; });
415  if (pProp != aProps.end())
416  nAttribs = pProp->Attributes;
417 
418  try
419  {
420  xNewPropContainer->addProperty(
421  rValue.Name, nAttribs, rValue.Value );
422  }
423  catch ( beans::PropertyExistException & )
424  {
425  }
426  catch ( beans::IllegalTypeException & )
427  {
428  }
429  catch ( lang::IllegalArgumentException & )
430  {
431  }
432  }
433  }
434  }
435  return true;
436 }
437 
438 bool ContentProviderImplHelper::removeAdditionalPropertySet(
439  const OUString& rKey, bool bRecursive )
440 {
441  osl::MutexGuard aGuard( m_aMutex );
442 
443  if ( bRecursive )
444  {
445  // Get propertyset registry.
446  getAdditionalPropertySetRegistry();
447 
448  if ( !m_pImpl->m_xPropertySetRegistry.is() )
449  return false;
450 
451  uno::Reference< container::XNameAccess > xNameAccess(
452  m_pImpl->m_xPropertySetRegistry, uno::UNO_QUERY );
453  if ( !xNameAccess.is() )
454  return false;
455 
456  const uno::Sequence< OUString > aKeys
457  = xNameAccess->getElementNames();
458  if ( aKeys.hasElements() )
459  {
460  OUString aKeyWithSlash = rKey;
461  OUString aKeyWithoutSlash;
462  if ( !aKeyWithSlash.endsWith("/") )
463  {
464  aKeyWithSlash += "/";
465  aKeyWithoutSlash = rKey;
466  }
467  else if ( !rKey.isEmpty() )
468  aKeyWithoutSlash
469  = rKey.copy( 0, rKey.getLength() - 1 );
470 
471  for ( const OUString& rCurrKey : aKeys )
472  {
473  if ( rCurrKey.startsWith(aKeyWithSlash )
474  || rCurrKey == aKeyWithoutSlash )
475  {
476  if ( !removeAdditionalPropertySet(
477  rCurrKey, false ) )
478  return false;
479  }
480  }
481  }
482  }
483  else
484  {
485  // Get propertyset registry.
486  getAdditionalPropertySetRegistry();
487 
488  if ( !m_pImpl->m_xPropertySetRegistry.is() )
489  return false;
490 
491  m_pImpl->m_xPropertySetRegistry->removePropertySet( rKey );
492  }
493  return true;
494 }
495 
496 } // namespace ucbhelper
497 
498 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
std::vector< ContentImplHelperRef > ContentRefList
uno::Reference< css::ucb::XPropertySetRegistry > m_xPropertySetRegistry
virtual sal_Int32 SAL_CALL compareContentIds(const css::uno::Reference< css::ucb::XContentIdentifier > &Id1, const css::uno::Reference< css::ucb::XContentIdentifier > &Id2) override
void queryExistingContents(ContentRefList &rContents)
This method fills a list with all contents existing at calling time.
UCBHELPER_DLLPRIVATE void removeContent(ContentImplHelper *pContent)
std::unordered_map< OUString, uno::WeakReference< ucb::XContent >> Contents
This is an abstract base class for implementations of the service com.sun.star.ucb.Content.
UCBHELPER_DLLPRIVATE void cleanupRegisteredContents()
css::uno::Any const & rValue
css::uno::Reference< css::ucb::XPersistentPropertySet > getAdditionalPropertySet(const OUString &rKey, bool bCreate)
This method returns the propertyset containing the Additional Core Properties of a content...
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
std::unique_ptr< ucbhelper_impl::ContentProviderImplHelper_Impl > m_pImpl
unsigned char sal_Bool
rtl::Reference< ContentImplHelper > queryExistingContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
This method returns a content with the given id, if it already exists.
css::uno::Reference< css::uno::XComponentContext > m_xContext
void registerNewContent(const css::uno::Reference< css::ucb::XContent > &xContent)
This method registers a newly created content instance with the content provider. ...
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual ~ContentProviderImplHelper() override
virtual css::uno::Reference< css::ucb::XContentIdentifier > SAL_CALL getIdentifier() override
UCBHELPER_DLLPRIVATE css::uno::Reference< css::ucb::XPropertySetRegistry > getAdditionalPropertySetRegistry()
uno::Reference< ucb::XContent > xContent
osl::Mutex m_aMutex
Reference< XComponentContext > m_xContext
bool renameAdditionalPropertySet(const OUString &rOldKey, const OUString &rNewKey, bool bRecursive)
This method renames the propertyset containing the Additional Core Properties of a content...
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
bool copyAdditionalPropertySet(const OUString &rSourceKey, const OUString &rTargetKey, bool bRecursive)
This method copies the propertyset containing the Additional Core Properties of a content...