| 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 |
|
| 21 |
/************************************************************************** |
| 22 |
TODO |
| 23 |
************************************************************************** |
| 24 |
|
| 25 |
- HierarchyEntry::move |
| 26 |
--> Rewrite to use XNamed ( once this is supported by config db api ). |
| 27 |
|
| 28 |
*************************************************************************/ |
| 29 |
#include "hierarchydata.hxx" |
| 30 |
|
| 31 |
#include <comphelper/diagnose_ex.hxx> |
| 32 |
#include <rtl/ustrbuf.hxx> |
| 33 |
#include <com/sun/star/container/XHierarchicalNameAccess.hpp> |
| 34 |
#include <com/sun/star/container/XNameContainer.hpp> |
| 35 |
#include <com/sun/star/container/XNameReplace.hpp> |
| 36 |
#include <com/sun/star/util/XChangesBatch.hpp> |
| 37 |
#include <com/sun/star/util/XOfficeInstallationDirectories.hpp> |
| 38 |
#include <com/sun/star/lang/XSingleServiceFactory.hpp> |
| 39 |
#include <comphelper/propertysequence.hxx> |
| 40 |
#include <utility> |
| 41 |
#include "hierarchyprovider.hxx" |
| 42 |
#include "hierarchyuri.hxx" |
| 43 |
|
| 44 |
using namespace com::sun::star; |
| 45 |
|
| 46 |
namespace hierarchy_ucp |
| 47 |
{ |
| 48 |
|
| 49 |
|
| 50 |
static void makeXMLName( std::u16string_view rIn, OUStringBuffer & rBuffer ) |
| 51 |
{ |
| 52 |
size_t nCount = rIn.size(); |
| 53 |
for ( size_t n = 0; n < nCount; ++n ) |
| 54 |
{ |
| 55 |
const sal_Unicode c = rIn[ n ]; |
| 56 |
switch ( c ) |
| 57 |
{ |
| 58 |
case '&': |
| 59 |
rBuffer.append( "&" ); |
| 60 |
break; |
| 61 |
|
| 62 |
case '"': |
| 63 |
rBuffer.append( """ ); |
| 64 |
break; |
| 65 |
|
| 66 |
case '\'': |
| 67 |
rBuffer.append( "'" ); |
| 68 |
break; |
| 69 |
|
| 70 |
case '<': |
| 71 |
rBuffer.append( "<" ); |
| 72 |
break; |
| 73 |
|
| 74 |
case '>': |
| 75 |
rBuffer.append( ">" ); |
| 76 |
break; |
| 77 |
|
| 78 |
default: |
| 79 |
rBuffer.append( c ); |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
// HierarchyEntry Implementation. |
| 87 |
|
| 88 |
|
| 89 |
constexpr OUStringLiteral READ_SERVICE_NAME = u"com.sun.star.ucb.HierarchyDataReadAccess"; |
| 90 |
constexpr OUString READWRITE_SERVICE_NAME = u"com.sun.star.ucb.HierarchyDataReadWriteAccess"_ustr; |
| 91 |
|
| 92 |
// describe path of cfg entry |
| 93 |
constexpr OUString CFGPROPERTY_NODEPATH = u"nodepath"_ustr; |
| 94 |
|
| 95 |
|
| 96 |
HierarchyEntry::HierarchyEntry( |
| 97 |
uno::Reference< uno::XComponentContext > xContext, |
| 98 |
HierarchyContentProvider* pProvider, |
| 99 |
const OUString& rURL ) |
| 100 |
: m_xContext(std::move( xContext )), |
| 101 |
m_xOfficeInstDirs( pProvider->getOfficeInstallationDirectories() ), |
| 102 |
m_bTriedToGetRootReadAccess( false ) |
| 103 |
{ |
| 104 |
HierarchyUri aUri( rURL ); |
| 105 |
m_aServiceSpecifier = aUri.getService(); |
| 106 |
|
| 107 |
m_xConfigProvider |
| 108 |
= pProvider->getConfigProvider( m_aServiceSpecifier ); |
| 109 |
m_xRootReadAccess |
| 110 |
= pProvider->getRootConfigReadNameAccess( m_aServiceSpecifier ); |
| 111 |
|
| 112 |
// Note: do not init m_aPath in init list. createPathFromHierarchyURL |
| 113 |
// needs m_xContext and m_aMutex. |
| 114 |
m_aPath = createPathFromHierarchyURL( aUri ); |
| 115 |
|
| 116 |
// Extract language independent name from URL. |
| 117 |
sal_Int32 nPos = rURL.lastIndexOf( '/' ); |
| 118 |
if ( nPos > HIERARCHY_URL_SCHEME_LENGTH ) |
| 119 |
m_aName = rURL.copy( nPos + 1 ); |
| 120 |
else |
| 121 |
OSL_FAIL( "HierarchyEntry - Invalid URL!" ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
bool HierarchyEntry::hasData() |
| 126 |
{ |
| 127 |
uno::Reference< container::XHierarchicalNameAccess > xRootReadAccess |
| 128 |
= getRootReadAccess(); |
| 129 |
|
| 130 |
OSL_ENSURE( xRootReadAccess.is(), "HierarchyEntry::hasData - No root!" ); |
| 131 |
|
| 132 |
if ( xRootReadAccess.is() ) |
| 133 |
return xRootReadAccess->hasByHierarchicalName( m_aPath ); |
| 134 |
|
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
bool HierarchyEntry::getData( HierarchyEntryData& rData ) |
| 140 |
{ |
| 141 |
try |
| 142 |
{ |
| 143 |
uno::Reference< container::XHierarchicalNameAccess > xRootReadAccess |
| 144 |
= getRootReadAccess(); |
| 145 |
|
| 146 |
OSL_ENSURE( xRootReadAccess.is(), |
| 147 |
"HierarchyEntry::getData - No root!" ); |
| 148 |
|
| 149 |
if ( xRootReadAccess.is() ) |
| 150 |
{ |
| 151 |
OUString aTitlePath = m_aPath + "/Title"; |
| 152 |
|
| 153 |
// Note: Avoid NoSuchElementExceptions, because exceptions are |
| 154 |
// relatively 'expensive'. Checking for availability of |
| 155 |
// title value is sufficient here, because if it is |
| 156 |
// there, the other values will be available too. |
| 157 |
if ( !xRootReadAccess->hasByHierarchicalName( aTitlePath ) ) |
| 158 |
return false; |
| 159 |
|
| 160 |
OUString aValue; |
| 161 |
|
| 162 |
// Get Title value. |
| 163 |
if ( !( xRootReadAccess->getByHierarchicalName( aTitlePath ) |
| 164 |
>>= aValue ) ) |
| 165 |
{ |
| 166 |
OSL_FAIL( "HierarchyEntry::getData - " |
| 167 |
"Got no Title value!" ); |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
rData.setTitle( aValue ); |
| 172 |
|
| 173 |
// Get TargetURL value. |
| 174 |
OUString aTargetURLPath = m_aPath + "/TargetURL"; |
| 175 |
if ( !( xRootReadAccess->getByHierarchicalName( aTargetURLPath ) |
| 176 |
>>= aValue ) ) |
| 177 |
{ |
| 178 |
OSL_FAIL( "HierarchyEntry::getData - " |
| 179 |
"Got no TargetURL value!" ); |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
// TargetURL property may contain a reference to the Office |
| 184 |
// installation directory. To ensure a reloctable office |
| 185 |
// installation, the path to the office installation directory must |
| 186 |
// never be stored directly. A placeholder is used instead. Replace |
| 187 |
// it by actual installation directory. |
| 188 |
if ( m_xOfficeInstDirs.is() && !aValue.isEmpty() ) |
| 189 |
aValue = m_xOfficeInstDirs->makeAbsoluteURL( aValue ); |
| 190 |
rData.setTargetURL( aValue ); |
| 191 |
|
| 192 |
OUString aTypePath = m_aPath + "/Type"; |
| 193 |
if ( xRootReadAccess->hasByHierarchicalName( aTypePath ) ) |
| 194 |
{ |
| 195 |
// Might not be present since it was introduced long after |
| 196 |
// Title and TargetURL (#82433#)... So not getting it is |
| 197 |
// not an error. |
| 198 |
|
| 199 |
// Get Type value. |
| 200 |
sal_Int32 nType = 0; |
| 201 |
if ( xRootReadAccess->getByHierarchicalName( aTypePath ) |
| 202 |
>>= nType ) |
| 203 |
{ |
| 204 |
if ( nType == 0 ) |
| 205 |
{ |
| 206 |
rData.setType( HierarchyEntryData::LINK ); |
| 207 |
} |
| 208 |
else if ( nType == 1 ) |
| 209 |
{ |
| 210 |
rData.setType( HierarchyEntryData::FOLDER ); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
OSL_FAIL( "HierarchyEntry::getData - " |
| 215 |
"Unknown Type value!" ); |
| 216 |
return false; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
rData.setName( m_aName ); |
| 222 |
return true; |
| 223 |
} |
| 224 |
} |
| 225 |
catch ( uno::RuntimeException const & ) |
| 226 |
{ |
| 227 |
throw; |
| 228 |
} |
| 229 |
catch ( container::NoSuchElementException const & ) |
| 230 |
{ |
| 231 |
// getByHierarchicalName |
| 232 |
|
| 233 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 234 |
} |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
bool HierarchyEntry::setData( const HierarchyEntryData& rData ) |
| 240 |
{ |
| 241 |
try |
| 242 |
{ |
| 243 |
std::unique_lock aGuard( m_aMutex ); |
| 244 |
|
| 245 |
if ( !m_xConfigProvider.is() ) |
| 246 |
m_xConfigProvider.set( |
| 247 |
m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext), |
| 248 |
uno::UNO_QUERY ); |
| 249 |
|
| 250 |
if ( m_xConfigProvider.is() ) |
| 251 |
{ |
| 252 |
// Create parent's key. It must exist! |
| 253 |
|
| 254 |
OUString aParentPath; |
| 255 |
bool bRoot = true; |
| 256 |
|
| 257 |
sal_Int32 nPos = m_aPath.lastIndexOf( '/' ); |
| 258 |
if ( nPos != -1 ) |
| 259 |
{ |
| 260 |
// Skip "/Children" segment of the path, too. |
| 261 |
nPos = m_aPath.lastIndexOf( '/', nPos - 1 ); |
| 262 |
|
| 263 |
OSL_ENSURE( nPos != -1, |
| 264 |
"HierarchyEntry::setData - Wrong path!" ); |
| 265 |
|
| 266 |
aParentPath += m_aPath.subView( 0, nPos ); |
| 267 |
bRoot = false; |
| 268 |
} |
| 269 |
|
| 270 |
uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence( |
| 271 |
{ |
| 272 |
{CFGPROPERTY_NODEPATH, uno::Any(aParentPath)} |
| 273 |
})); |
| 274 |
|
| 275 |
uno::Reference< util::XChangesBatch > xBatch( |
| 276 |
m_xConfigProvider->createInstanceWithArguments( |
| 277 |
READWRITE_SERVICE_NAME, |
| 278 |
aArguments ), |
| 279 |
uno::UNO_QUERY ); |
| 280 |
|
| 281 |
OSL_ENSURE( xBatch.is(), |
| 282 |
"HierarchyEntry::setData - No batch!" ); |
| 283 |
|
| 284 |
uno::Reference< container::XNameAccess > xParentNameAccess( |
| 285 |
xBatch, uno::UNO_QUERY ); |
| 286 |
|
| 287 |
OSL_ENSURE( xParentNameAccess.is(), |
| 288 |
"HierarchyEntry::setData - No name access!" ); |
| 289 |
|
| 290 |
if ( xBatch.is() && xParentNameAccess.is() ) |
| 291 |
{ |
| 292 |
// Try to create own key. It must not exist! |
| 293 |
|
| 294 |
bool bExists = true; |
| 295 |
uno::Any aMyKey; |
| 296 |
|
| 297 |
try |
| 298 |
{ |
| 299 |
uno::Reference< container::XNameAccess > xNameAccess; |
| 300 |
|
| 301 |
if ( bRoot ) |
| 302 |
{ |
| 303 |
xNameAccess = xParentNameAccess; |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
xParentNameAccess->getByName(u"Children"_ustr) >>= xNameAccess; |
| 308 |
} |
| 309 |
|
| 310 |
if ( xNameAccess->hasByName( m_aName ) ) |
| 311 |
aMyKey = xNameAccess->getByName( m_aName ); |
| 312 |
else |
| 313 |
bExists = false; |
| 314 |
} |
| 315 |
catch ( container::NoSuchElementException const & ) |
| 316 |
{ |
| 317 |
bExists = false; |
| 318 |
} |
| 319 |
|
| 320 |
uno::Reference< container::XNameReplace > xNameReplace; |
| 321 |
uno::Reference< container::XNameContainer > xContainer; |
| 322 |
|
| 323 |
if ( bExists ) |
| 324 |
{ |
| 325 |
// Key exists. Replace values. |
| 326 |
|
| 327 |
aMyKey >>= xNameReplace; |
| 328 |
|
| 329 |
OSL_ENSURE( xNameReplace.is(), |
| 330 |
"HierarchyEntry::setData - No name replace!" ); |
| 331 |
} |
| 332 |
else |
| 333 |
{ |
| 334 |
// Key does not exist. Create / fill / insert it. |
| 335 |
|
| 336 |
uno::Reference< lang::XSingleServiceFactory > xFac; |
| 337 |
|
| 338 |
if ( bRoot ) |
| 339 |
{ |
| 340 |
// Special handling for children of root, |
| 341 |
// which is not an entry. It's only a set |
| 342 |
// of entries. |
| 343 |
xFac.set( xParentNameAccess, uno::UNO_QUERY ); |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
// Append new entry to parents child list, |
| 348 |
// which is a set of entries. |
| 349 |
xParentNameAccess->getByName(u"Children"_ustr) >>= xFac; |
| 350 |
} |
| 351 |
|
| 352 |
OSL_ENSURE( xFac.is(), |
| 353 |
"HierarchyEntry::setData - No factory!" ); |
| 354 |
|
| 355 |
if ( xFac.is() ) |
| 356 |
{ |
| 357 |
xNameReplace.set( xFac->createInstance(), uno::UNO_QUERY ); |
| 358 |
|
| 359 |
OSL_ENSURE( xNameReplace.is(), |
| 360 |
"HierarchyEntry::setData - No name replace!" ); |
| 361 |
|
| 362 |
if ( xNameReplace.is() ) |
| 363 |
{ |
| 364 |
xContainer.set( xFac, uno::UNO_QUERY ); |
| 365 |
|
| 366 |
OSL_ENSURE( xContainer.is(), |
| 367 |
"HierarchyEntry::setData - No container!" ); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if ( xNameReplace.is() ) |
| 373 |
{ |
| 374 |
// Set Title value. |
| 375 |
xNameReplace->replaceByName( |
| 376 |
u"Title"_ustr, |
| 377 |
uno::Any( rData.getTitle() ) ); |
| 378 |
|
| 379 |
// Set TargetURL value. |
| 380 |
|
| 381 |
// TargetURL property may contain a reference to the Office |
| 382 |
// installation directory. To ensure a reloctable office |
| 383 |
// installation, the path to the office installation |
| 384 |
// directory must never be stored directly. Use a |
| 385 |
// placeholder instead. |
| 386 |
OUString aValue( rData.getTargetURL() ); |
| 387 |
if ( m_xOfficeInstDirs.is() && !aValue.isEmpty() ) |
| 388 |
aValue |
| 389 |
= m_xOfficeInstDirs->makeRelocatableURL( aValue ); |
| 390 |
|
| 391 |
xNameReplace->replaceByName( |
| 392 |
u"TargetURL"_ustr, |
| 393 |
uno::Any( aValue ) ); |
| 394 |
|
| 395 |
// Set Type value. |
| 396 |
sal_Int32 nType |
| 397 |
= rData.getType() == HierarchyEntryData::LINK ? 0 : 1; |
| 398 |
xNameReplace->replaceByName( |
| 399 |
u"Type"_ustr, |
| 400 |
uno::Any( nType ) ); |
| 401 |
|
| 402 |
if ( xContainer.is() ) |
| 403 |
xContainer->insertByName( |
| 404 |
m_aName, uno::Any( xNameReplace ) ); |
| 405 |
|
| 406 |
// Commit changes. |
| 407 |
xBatch->commitChanges(); |
| 408 |
return true; |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
catch ( lang::IllegalArgumentException const & ) |
| 414 |
{ |
| 415 |
// replaceByName, insertByName |
| 416 |
|
| 417 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 418 |
} |
| 419 |
catch ( uno::RuntimeException const & ) |
| 420 |
{ |
| 421 |
throw; |
| 422 |
} |
| 423 |
catch ( container::NoSuchElementException const & ) |
| 424 |
{ |
| 425 |
// replaceByName, getByName |
| 426 |
|
| 427 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 428 |
} |
| 429 |
catch ( container::ElementExistException const & ) |
| 430 |
{ |
| 431 |
// insertByName |
| 432 |
|
| 433 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 434 |
} |
| 435 |
catch ( lang::WrappedTargetException const & ) |
| 436 |
{ |
| 437 |
// replaceByName, insertByName, getByName, commitChanges |
| 438 |
|
| 439 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 440 |
} |
| 441 |
catch ( uno::Exception const & ) |
| 442 |
{ |
| 443 |
// createInstance, createInstanceWithArguments |
| 444 |
|
| 445 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 446 |
} |
| 447 |
|
| 448 |
return false; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
bool HierarchyEntry::move( |
| 453 |
const OUString& rNewURL, const HierarchyEntryData& rData ) |
| 454 |
{ |
| 455 |
OUString aNewPath = createPathFromHierarchyURL( HierarchyUri(rNewURL) ); |
| 456 |
|
| 457 |
std::unique_lock aGuard( m_aMutex ); |
| 458 |
|
| 459 |
if ( aNewPath == m_aPath ) |
| 460 |
return true; |
| 461 |
|
| 462 |
bool bOldRoot = true; |
| 463 |
uno::Reference< util::XChangesBatch > xOldParentBatch; |
| 464 |
|
| 465 |
OUString aNewKey; |
| 466 |
sal_Int32 nURLPos = rNewURL.lastIndexOf( '/' ); |
| 467 |
if ( nURLPos > HIERARCHY_URL_SCHEME_LENGTH ) |
| 468 |
aNewKey = rNewURL.copy( nURLPos + 1 ); |
| 469 |
else |
| 470 |
{ |
| 471 |
OSL_FAIL( "HierarchyEntry::move - Invalid URL!" ); |
| 472 |
return false; |
| 473 |
} |
| 474 |
|
| 475 |
bool bNewRoot = true; |
| 476 |
uno::Reference< util::XChangesBatch > xNewParentBatch; |
| 477 |
|
| 478 |
bool bDifferentParents = true; |
| 479 |
|
| 480 |
try |
| 481 |
{ |
| 482 |
if ( !m_xConfigProvider.is() ) |
| 483 |
m_xConfigProvider.set( |
| 484 |
m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext), |
| 485 |
uno::UNO_QUERY ); |
| 486 |
|
| 487 |
if ( !m_xConfigProvider.is() ) |
| 488 |
return false; |
| 489 |
|
| 490 |
OUString aOldParentPath; |
| 491 |
sal_Int32 nPos = m_aPath.lastIndexOf( '/' ); |
| 492 |
if ( nPos != -1 ) |
| 493 |
{ |
| 494 |
// Skip "/Children" segment of the path, too. |
| 495 |
nPos = m_aPath.lastIndexOf( '/', nPos - 1 ); |
| 496 |
|
| 497 |
OSL_ENSURE( nPos != -1, "HierarchyEntry::move - Wrong path!" ); |
| 498 |
|
| 499 |
aOldParentPath += m_aPath.subView( 0, nPos ); |
| 500 |
bOldRoot = false; |
| 501 |
} |
| 502 |
|
| 503 |
OUString aNewParentPath; |
| 504 |
nPos = aNewPath.lastIndexOf( '/' ); |
| 505 |
if ( nPos != -1 ) |
| 506 |
{ |
| 507 |
// Skip "/Children" segment of the path, too. |
| 508 |
nPos = aNewPath.lastIndexOf( '/', nPos - 1 ); |
| 509 |
|
| 510 |
OSL_ENSURE( nPos != -1, "HierarchyEntry::move - Wrong path!" ); |
| 511 |
|
| 512 |
aNewParentPath += aNewPath.subView( 0, nPos ); |
| 513 |
bNewRoot = false; |
| 514 |
} |
| 515 |
|
| 516 |
uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence( |
| 517 |
{ |
| 518 |
{CFGPROPERTY_NODEPATH, uno::Any(aOldParentPath)} |
| 519 |
})); |
| 520 |
|
| 521 |
xOldParentBatch.set( |
| 522 |
m_xConfigProvider->createInstanceWithArguments( |
| 523 |
READWRITE_SERVICE_NAME, |
| 524 |
aArguments ), |
| 525 |
uno::UNO_QUERY ); |
| 526 |
|
| 527 |
OSL_ENSURE( xOldParentBatch.is(), "HierarchyEntry::move - No batch!" ); |
| 528 |
|
| 529 |
if ( !xOldParentBatch.is() ) |
| 530 |
return false; |
| 531 |
|
| 532 |
if ( aOldParentPath == aNewParentPath ) |
| 533 |
{ |
| 534 |
bDifferentParents = false; |
| 535 |
xNewParentBatch = xOldParentBatch; |
| 536 |
} |
| 537 |
else |
| 538 |
{ |
| 539 |
uno::Sequence<uno::Any> aArguments2(comphelper::InitAnyPropertySequence( |
| 540 |
{ |
| 541 |
{CFGPROPERTY_NODEPATH, uno::Any(aNewParentPath)} |
| 542 |
})); |
| 543 |
|
| 544 |
xNewParentBatch.set( |
| 545 |
m_xConfigProvider->createInstanceWithArguments( |
| 546 |
READWRITE_SERVICE_NAME, |
| 547 |
aArguments2 ), |
| 548 |
uno::UNO_QUERY ); |
| 549 |
|
| 550 |
OSL_ENSURE( |
| 551 |
xNewParentBatch.is(), "HierarchyEntry::move - No batch!" ); |
| 552 |
|
| 553 |
if ( !xNewParentBatch.is() ) |
| 554 |
return false; |
| 555 |
} |
| 556 |
} |
| 557 |
catch ( uno::RuntimeException const & ) |
| 558 |
{ |
| 559 |
throw; |
| 560 |
} |
| 561 |
catch ( uno::Exception const & ) |
| 562 |
{ |
| 563 |
// createInstance, createInstanceWithArguments |
| 564 |
|
| 565 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 566 |
return false; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
// (1) Get entry... |
| 571 |
|
| 572 |
|
| 573 |
uno::Any aEntry; |
| 574 |
uno::Reference< container::XNameAccess > xOldParentNameAccess; |
| 575 |
uno::Reference< container::XNameContainer > xOldNameContainer; |
| 576 |
|
| 577 |
try |
| 578 |
{ |
| 579 |
xOldParentNameAccess.set( xOldParentBatch, uno::UNO_QUERY ); |
| 580 |
|
| 581 |
OSL_ENSURE( xOldParentNameAccess.is(), |
| 582 |
"HierarchyEntry::move - No name access!" ); |
| 583 |
|
| 584 |
if ( !xOldParentNameAccess.is() ) |
| 585 |
return false; |
| 586 |
|
| 587 |
if ( bOldRoot ) |
| 588 |
{ |
| 589 |
xOldNameContainer.set( xOldParentNameAccess, uno::UNO_QUERY ); |
| 590 |
} |
| 591 |
else |
| 592 |
{ |
| 593 |
xOldParentNameAccess->getByName(u"Children"_ustr) >>= xOldNameContainer; |
| 594 |
} |
| 595 |
|
| 596 |
aEntry = xOldNameContainer->getByName( m_aName ); |
| 597 |
} |
| 598 |
catch ( container::NoSuchElementException const & ) |
| 599 |
{ |
| 600 |
// getByName |
| 601 |
|
| 602 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 603 |
return false; |
| 604 |
} |
| 605 |
catch ( lang::WrappedTargetException const & ) |
| 606 |
{ |
| 607 |
// getByName |
| 608 |
|
| 609 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 610 |
return false; |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
// (2) Remove entry... Note: Insert BEFORE remove does not work! |
| 615 |
|
| 616 |
|
| 617 |
try |
| 618 |
{ |
| 619 |
xOldNameContainer->removeByName( m_aName ); |
| 620 |
xOldParentBatch->commitChanges(); |
| 621 |
} |
| 622 |
catch ( container::NoSuchElementException const & ) |
| 623 |
{ |
| 624 |
// getByName, removeByName |
| 625 |
|
| 626 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 627 |
return false; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
// (3) Insert entry at new parent... |
| 632 |
|
| 633 |
|
| 634 |
try |
| 635 |
{ |
| 636 |
uno::Reference< container::XNameReplace > xNewNameReplace; |
| 637 |
aEntry >>= xNewNameReplace; |
| 638 |
|
| 639 |
OSL_ENSURE( xNewNameReplace.is(), |
| 640 |
"HierarchyEntry::move - No name replace!" ); |
| 641 |
|
| 642 |
if ( !xNewNameReplace.is() ) |
| 643 |
return false; |
| 644 |
|
| 645 |
uno::Reference< container::XNameAccess > xNewParentNameAccess; |
| 646 |
if ( bDifferentParents ) |
| 647 |
xNewParentNameAccess.set( xNewParentBatch, uno::UNO_QUERY ); |
| 648 |
else |
| 649 |
xNewParentNameAccess = std::move(xOldParentNameAccess); |
| 650 |
|
| 651 |
OSL_ENSURE( xNewParentNameAccess.is(), |
| 652 |
"HierarchyEntry::move - No name access!" ); |
| 653 |
|
| 654 |
if ( !xNewParentNameAccess.is() ) |
| 655 |
return false; |
| 656 |
|
| 657 |
uno::Reference< container::XNameContainer > xNewNameContainer; |
| 658 |
if ( bDifferentParents ) |
| 659 |
{ |
| 660 |
if ( bNewRoot ) |
| 661 |
{ |
| 662 |
xNewNameContainer.set( xNewParentNameAccess, uno::UNO_QUERY ); |
| 663 |
} |
| 664 |
else |
| 665 |
{ |
| 666 |
xNewParentNameAccess->getByName(u"Children"_ustr) >>= xNewNameContainer; |
| 667 |
} |
| 668 |
} |
| 669 |
else |
| 670 |
xNewNameContainer = std::move(xOldNameContainer); |
| 671 |
|
| 672 |
if ( !xNewNameContainer.is() ) |
| 673 |
return false; |
| 674 |
|
| 675 |
xNewNameReplace->replaceByName( |
| 676 |
u"Title"_ustr, |
| 677 |
uno::Any( rData.getTitle() ) ); |
| 678 |
|
| 679 |
// TargetURL property may contain a reference to the Office |
| 680 |
// installation directory. To ensure a reloctable office |
| 681 |
// installation, the path to the office installation |
| 682 |
// directory must never be stored directly. Use a placeholder |
| 683 |
// instead. |
| 684 |
OUString aValue( rData.getTargetURL() ); |
| 685 |
if ( m_xOfficeInstDirs.is() && !aValue.isEmpty() ) |
| 686 |
aValue = m_xOfficeInstDirs->makeRelocatableURL( aValue ); |
| 687 |
xNewNameReplace->replaceByName( |
| 688 |
u"TargetURL"_ustr, |
| 689 |
uno::Any( aValue ) ); |
| 690 |
sal_Int32 nType = rData.getType() == HierarchyEntryData::LINK ? 0 : 1; |
| 691 |
xNewNameReplace->replaceByName( |
| 692 |
u"Type"_ustr, |
| 693 |
uno::Any( nType ) ); |
| 694 |
|
| 695 |
xNewNameContainer->insertByName( aNewKey, aEntry ); |
| 696 |
xNewParentBatch->commitChanges(); |
| 697 |
} |
| 698 |
catch ( container::NoSuchElementException const & ) |
| 699 |
{ |
| 700 |
// replaceByName, insertByName, getByName |
| 701 |
|
| 702 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 703 |
return false; |
| 704 |
} |
| 705 |
catch ( lang::IllegalArgumentException const & ) |
| 706 |
{ |
| 707 |
// replaceByName, insertByName |
| 708 |
|
| 709 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 710 |
return false; |
| 711 |
} |
| 712 |
catch ( container::ElementExistException const & ) |
| 713 |
{ |
| 714 |
// insertByName |
| 715 |
|
| 716 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 717 |
return false; |
| 718 |
} |
| 719 |
catch ( lang::WrappedTargetException const & ) |
| 720 |
{ |
| 721 |
// replaceByName, insertByName, getByName |
| 722 |
|
| 723 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 724 |
return false; |
| 725 |
} |
| 726 |
|
| 727 |
return true; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
bool HierarchyEntry::remove() |
| 732 |
{ |
| 733 |
try |
| 734 |
{ |
| 735 |
std::unique_lock aGuard( m_aMutex ); |
| 736 |
|
| 737 |
if ( !m_xConfigProvider.is() ) |
| 738 |
m_xConfigProvider.set( |
| 739 |
m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext), |
| 740 |
uno::UNO_QUERY ); |
| 741 |
|
| 742 |
if ( m_xConfigProvider.is() ) |
| 743 |
{ |
| 744 |
// Create parent's key. It must exist! |
| 745 |
|
| 746 |
OUString aParentPath; |
| 747 |
bool bRoot = true; |
| 748 |
|
| 749 |
sal_Int32 nPos = m_aPath.lastIndexOf( '/' ); |
| 750 |
if ( nPos != -1 ) |
| 751 |
{ |
| 752 |
// Skip "/Children" segment of the path, too. |
| 753 |
nPos = m_aPath.lastIndexOf( '/', nPos - 1 ); |
| 754 |
|
| 755 |
OSL_ENSURE( nPos != -1, |
| 756 |
"HierarchyEntry::remove - Wrong path!" ); |
| 757 |
|
| 758 |
aParentPath += m_aPath.subView( 0, nPos ); |
| 759 |
bRoot = false; |
| 760 |
} |
| 761 |
|
| 762 |
uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence( |
| 763 |
{ |
| 764 |
{CFGPROPERTY_NODEPATH, uno::Any(aParentPath)} |
| 765 |
})); |
| 766 |
|
| 767 |
uno::Reference< util::XChangesBatch > xBatch( |
| 768 |
m_xConfigProvider->createInstanceWithArguments( |
| 769 |
READWRITE_SERVICE_NAME, |
| 770 |
aArguments ), |
| 771 |
uno::UNO_QUERY ); |
| 772 |
|
| 773 |
OSL_ENSURE( xBatch.is(), |
| 774 |
"HierarchyEntry::remove - No batch!" ); |
| 775 |
|
| 776 |
uno::Reference< container::XNameAccess > xParentNameAccess( |
| 777 |
xBatch, uno::UNO_QUERY ); |
| 778 |
|
| 779 |
OSL_ENSURE( xParentNameAccess.is(), |
| 780 |
"HierarchyEntry::remove - No name access!" ); |
| 781 |
|
| 782 |
if ( xBatch.is() && xParentNameAccess.is() ) |
| 783 |
{ |
| 784 |
uno::Reference< container::XNameContainer > xContainer; |
| 785 |
|
| 786 |
if ( bRoot ) |
| 787 |
{ |
| 788 |
// Special handling for children of root, |
| 789 |
// which is not an entry. It's only a set |
| 790 |
// of entries. |
| 791 |
xContainer.set( xParentNameAccess, uno::UNO_QUERY ); |
| 792 |
} |
| 793 |
else |
| 794 |
{ |
| 795 |
// Append new entry to parents child list, |
| 796 |
// which is a set of entries. |
| 797 |
xParentNameAccess->getByName(u"Children"_ustr) >>= xContainer; |
| 798 |
} |
| 799 |
|
| 800 |
OSL_ENSURE( xContainer.is(), |
| 801 |
"HierarchyEntry::remove - No container!" ); |
| 802 |
|
| 803 |
if ( xContainer.is() ) |
| 804 |
{ |
| 805 |
xContainer->removeByName( m_aName ); |
| 806 |
xBatch->commitChanges(); |
| 807 |
return true; |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
catch ( uno::RuntimeException const & ) |
| 813 |
{ |
| 814 |
throw; |
| 815 |
} |
| 816 |
catch ( container::NoSuchElementException const & ) |
| 817 |
{ |
| 818 |
// getByName, removeByName |
| 819 |
|
| 820 |
OSL_FAIL( |
| 821 |
"HierarchyEntry::remove - caught NoSuchElementException!" ); |
| 822 |
} |
| 823 |
catch ( lang::WrappedTargetException const & ) |
| 824 |
{ |
| 825 |
// getByName, commitChanges |
| 826 |
|
| 827 |
OSL_FAIL( |
| 828 |
"HierarchyEntry::remove - caught WrappedTargetException!" ); |
| 829 |
} |
| 830 |
catch ( uno::Exception const & ) |
| 831 |
{ |
| 832 |
// createInstance, createInstanceWithArguments |
| 833 |
|
| 834 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 835 |
} |
| 836 |
|
| 837 |
return false; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
bool HierarchyEntry::first( iterator & it ) |
| 842 |
{ |
| 843 |
if ( it.pos == -1 ) |
| 844 |
{ |
| 845 |
// Init... |
| 846 |
|
| 847 |
try |
| 848 |
{ |
| 849 |
uno::Reference< container::XHierarchicalNameAccess > |
| 850 |
xRootHierNameAccess = getRootReadAccess(); |
| 851 |
|
| 852 |
if ( xRootHierNameAccess.is() ) |
| 853 |
{ |
| 854 |
uno::Reference< container::XNameAccess > xNameAccess; |
| 855 |
|
| 856 |
if ( !m_aPath.isEmpty() ) |
| 857 |
{ |
| 858 |
OUString aPath = m_aPath + "/Children"; |
| 859 |
|
| 860 |
xRootHierNameAccess->getByHierarchicalName( aPath ) |
| 861 |
>>= xNameAccess; |
| 862 |
} |
| 863 |
else |
| 864 |
xNameAccess.set( xRootHierNameAccess, uno::UNO_QUERY ); |
| 865 |
|
| 866 |
OSL_ENSURE( xNameAccess.is(), |
| 867 |
"HierarchyEntry::first - No name access!" ); |
| 868 |
|
| 869 |
if ( xNameAccess.is() ) |
| 870 |
it.names = xNameAccess->getElementNames(); |
| 871 |
|
| 872 |
uno::Reference< container::XHierarchicalNameAccess > |
| 873 |
xHierNameAccess( xNameAccess, uno::UNO_QUERY ); |
| 874 |
|
| 875 |
OSL_ENSURE( xHierNameAccess.is(), |
| 876 |
"HierarchyEntry::first - No hier. name access!" ); |
| 877 |
|
| 878 |
it.dir = std::move(xHierNameAccess); |
| 879 |
|
| 880 |
it.officeDirs = m_xOfficeInstDirs; |
| 881 |
} |
| 882 |
} |
| 883 |
catch ( uno::RuntimeException const & ) |
| 884 |
{ |
| 885 |
throw; |
| 886 |
} |
| 887 |
catch ( container::NoSuchElementException const& ) |
| 888 |
{ |
| 889 |
// getByHierarchicalName |
| 890 |
|
| 891 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 892 |
} |
| 893 |
catch ( uno::Exception const & ) |
| 894 |
{ |
| 895 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
if ( !it.names.hasElements() ) |
| 900 |
return false; |
| 901 |
|
| 902 |
it.pos = 0; |
| 903 |
return true; |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
bool HierarchyEntry::next( iterator& it ) |
| 908 |
{ |
| 909 |
if ( it.pos == -1 ) |
| 910 |
return first( it ); |
| 911 |
|
| 912 |
++it.pos; |
| 913 |
|
| 914 |
return ( it.pos < it.names.getLength() ); |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
OUString HierarchyEntry::createPathFromHierarchyURL( |
| 919 |
const HierarchyUri& rURI ) |
| 920 |
{ |
| 921 |
// Transform path... |
| 922 |
// folder/subfolder/subsubfolder |
| 923 |
// --> ['folder']/Children/['subfolder']/Children/['subsubfolder'] |
| 924 |
|
| 925 |
const OUString aPath = rURI.getPath().copy( 1 ); // skip leading slash. |
| 926 |
sal_Int32 nLen = aPath.getLength(); |
| 927 |
|
| 928 |
if ( nLen ) |
| 929 |
{ |
| 930 |
OUStringBuffer aNewPath( "['" ); |
| 931 |
|
| 932 |
sal_Int32 nStart = 0; |
| 933 |
sal_Int32 nEnd = aPath.indexOf( '/' ); |
| 934 |
|
| 935 |
do |
| 936 |
{ |
| 937 |
if ( nEnd == -1 ) |
| 938 |
nEnd = nLen; |
| 939 |
|
| 940 |
OUString aToken = aPath.copy( nStart, nEnd - nStart ); |
| 941 |
makeXMLName( aToken, aNewPath ); |
| 942 |
|
| 943 |
if ( nEnd != nLen ) |
| 944 |
{ |
| 945 |
aNewPath.append( "']/Children/['" ); |
| 946 |
nStart = nEnd + 1; |
| 947 |
nEnd = aPath.indexOf( '/', nStart ); |
| 948 |
} |
| 949 |
else |
| 950 |
aNewPath.append( "']" ); |
| 951 |
} |
| 952 |
while ( nEnd != nLen ); |
| 953 |
|
| 954 |
return aNewPath.makeStringAndClear(); |
| 955 |
} |
| 956 |
|
| 957 |
return aPath; |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
uno::Reference< container::XHierarchicalNameAccess > |
| 962 |
HierarchyEntry::getRootReadAccess() |
| 963 |
{ |
| 964 |
if ( !m_xRootReadAccess.is() ) |
| 965 |
{ |
| 966 |
std::unique_lock aGuard( m_aMutex ); |
| 967 |
if ( !m_xRootReadAccess.is() ) |
| 968 |
{ |
| 969 |
if ( m_bTriedToGetRootReadAccess ) |
| 970 |
{ |
| 971 |
OSL_FAIL( "HierarchyEntry::getRootReadAccess - " |
| 972 |
"Unable to read any config data! -> #82494#" ); |
| 973 |
return uno::Reference< container::XHierarchicalNameAccess >(); |
| 974 |
} |
| 975 |
|
| 976 |
try |
| 977 |
{ |
| 978 |
if ( !m_xConfigProvider.is() ) |
| 979 |
m_xConfigProvider.set( |
| 980 |
m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext), |
| 981 |
uno::UNO_QUERY ); |
| 982 |
|
| 983 |
if ( m_xConfigProvider.is() ) |
| 984 |
{ |
| 985 |
// Create Root object. |
| 986 |
|
| 987 |
uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence( |
| 988 |
{ |
| 989 |
{CFGPROPERTY_NODEPATH, uno::Any(OUString())} // root path |
| 990 |
})); |
| 991 |
|
| 992 |
m_bTriedToGetRootReadAccess = true; |
| 993 |
|
| 994 |
m_xRootReadAccess.set( |
| 995 |
m_xConfigProvider->createInstanceWithArguments( |
| 996 |
READ_SERVICE_NAME, |
| 997 |
aArguments ), |
| 998 |
uno::UNO_QUERY ); |
| 999 |
} |
| 1000 |
} |
| 1001 |
catch ( uno::RuntimeException const & ) |
| 1002 |
{ |
| 1003 |
throw; |
| 1004 |
} |
| 1005 |
catch ( uno::Exception const & ) |
| 1006 |
{ |
| 1007 |
// createInstance, createInstanceWithArguments |
| 1008 |
|
| 1009 |
TOOLS_WARN_EXCEPTION("ucb.ucp", ""); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
} |
| 1013 |
return m_xRootReadAccess; |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
// HierarchyEntry::iterator Implementation. |
| 1018 |
|
| 1019 |
|
| 1020 |
const HierarchyEntryData& HierarchyEntry::iterator::operator*() |
| 1021 |
{ |
| 1022 |
if ( ( pos != -1 ) |
| 1023 |
&& ( dir.is() ) |
| 1024 |
&& ( pos < names.getLength() ) ) |
| 1025 |
{ |
| 1026 |
try |
| 1027 |
{ |
| 1028 |
OUStringBuffer aKey( "['" ); |
| 1029 |
makeXMLName( names.getConstArray()[ pos ], aKey ); |
| 1030 |
aKey.append( "']" ); |
| 1031 |
|
| 1032 |
OUString aTitle = aKey.makeStringAndClear(); |
| 1033 |
OUString aTargetURL = aTitle; |
| 1034 |
OUString aType = aTitle; |
| 1035 |
|
| 1036 |
aTitle += "/Title"; |
| 1037 |
aTargetURL += "/TargetURL"; |
| 1038 |
aType += "/Type"; |
| 1039 |
|
| 1040 |
OUString aValue; |
| 1041 |
dir->getByHierarchicalName( aTitle ) >>= aValue; |
| 1042 |
entry.setTitle( aValue ); |
| 1043 |
|
| 1044 |
dir->getByHierarchicalName( aTargetURL ) >>= aValue; |
| 1045 |
|
| 1046 |
// TargetURL property may contain a reference to the Office |
| 1047 |
// installation directory. To ensure a reloctable office |
| 1048 |
// installation, the path to the office installation directory must |
| 1049 |
// never be stored directly. A placeholder is used instead. Replace |
| 1050 |
// it by actual installation directory. |
| 1051 |
if ( officeDirs.is() && !aValue.isEmpty() ) |
| 1052 |
aValue = officeDirs->makeAbsoluteURL( aValue ); |
| 1053 |
entry.setTargetURL( aValue ); |
| 1054 |
|
| 1055 |
if ( dir->hasByHierarchicalName( aType ) ) |
| 1056 |
{ |
| 1057 |
// Might not be present since it was introduced long |
| 1058 |
// after Title and TargetURL (#82433#)... So not getting |
| 1059 |
// it is not an error. |
| 1060 |
|
| 1061 |
// Get Type value. |
| 1062 |
sal_Int32 nType = 0; |
| 1063 |
if ( dir->getByHierarchicalName( aType ) >>= nType ) |
| 1064 |
{ |
| 1065 |
if ( nType == 0 ) |
| 1066 |
{ |
| 1067 |
entry.setType( HierarchyEntryData::LINK ); |
| 1068 |
} |
| 1069 |
else if ( nType == 1 ) |
| 1070 |
{ |
| 1071 |
entry.setType( HierarchyEntryData::FOLDER ); |
| 1072 |
} |
| 1073 |
else |
| 1074 |
{ |
| 1075 |
OSL_FAIL( "HierarchyEntry::getData - " |
| 1076 |
"Unknown Type value!" ); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
entry.setName( |
| 1082 |
names.getConstArray()[ pos ] ); |
| 1083 |
} |
| 1084 |
catch ( container::NoSuchElementException const & ) |
| 1085 |
{ |
| 1086 |
entry = HierarchyEntryData(); |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
return entry; |
| 1091 |
} |
| 1092 |
|
| 1093 |
} // namespace hierarchy_ucp |
| 1094 |
|
| 1095 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |