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