bump product version to 7.2.5.1
[LibreOffice.git] / ucb / source / ucp / hierarchy / hierarchydata.cxx
blobc0548cbe361b6a48dfa7edd0699994f57c3464f7
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 .
21 /**************************************************************************
22 TODO
23 **************************************************************************
25 - HierarchyEntry::move
26 --> Rewrite to use XNamed ( once this is supported by config db api ).
28 *************************************************************************/
29 #include "hierarchydata.hxx"
31 #include <tools/diagnose_ex.h>
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 "hierarchyprovider.hxx"
41 #include "hierarchyuri.hxx"
43 using namespace com::sun::star;
45 namespace hierarchy_ucp
49 struct HierarchyEntry::iterator_Impl
51 HierarchyEntryData entry;
52 uno::Reference< container::XHierarchicalNameAccess > dir;
53 uno::Reference< util::XOfficeInstallationDirectories > officeDirs;
54 uno::Sequence< OUString> names;
55 sal_Int32 pos;
56 iterator_Impl()
57 : pos( -1 /* before first */ ) {};
61 static void makeXMLName( const OUString & rIn, OUStringBuffer & rBuffer )
63 sal_Int32 nCount = rIn.getLength();
64 for ( sal_Int32 n = 0; n < nCount; ++n )
66 const sal_Unicode c = rIn[ n ];
67 switch ( c )
69 case '&':
70 rBuffer.append( "&amp;" );
71 break;
73 case '"':
74 rBuffer.append( "&quot;" );
75 break;
77 case '\'':
78 rBuffer.append( "&apos;" );
79 break;
81 case '<':
82 rBuffer.append( "&lt;" );
83 break;
85 case '>':
86 rBuffer.append( "&gt;" );
87 break;
89 default:
90 rBuffer.append( c );
91 break;
97 // HierarchyEntry Implementation.
100 #define READ_SERVICE_NAME "com.sun.star.ucb.HierarchyDataReadAccess"
101 #define READWRITE_SERVICE_NAME "com.sun.star.ucb.HierarchyDataReadWriteAccess"
103 // describe path of cfg entry
104 #define CFGPROPERTY_NODEPATH "nodepath"
107 HierarchyEntry::HierarchyEntry(
108 const uno::Reference< uno::XComponentContext >& rxContext,
109 HierarchyContentProvider* pProvider,
110 const OUString& rURL )
111 : m_xContext( rxContext ),
112 m_xOfficeInstDirs( pProvider->getOfficeInstallationDirectories() ),
113 m_bTriedToGetRootReadAccess( false )
115 HierarchyUri aUri( rURL );
116 m_aServiceSpecifier = aUri.getService();
118 m_xConfigProvider
119 = pProvider->getConfigProvider( m_aServiceSpecifier );
120 m_xRootReadAccess
121 = pProvider->getRootConfigReadNameAccess( m_aServiceSpecifier );
123 // Note: do not init m_aPath in init list. createPathFromHierarchyURL
124 // needs m_xContext and m_aMutex.
125 m_aPath = createPathFromHierarchyURL( aUri );
127 // Extract language independent name from URL.
128 sal_Int32 nPos = rURL.lastIndexOf( '/' );
129 if ( nPos > HIERARCHY_URL_SCHEME_LENGTH )
130 m_aName = rURL.copy( nPos + 1 );
131 else
132 OSL_FAIL( "HierarchyEntry - Invalid URL!" );
136 bool HierarchyEntry::hasData()
138 osl::Guard< osl::Mutex > aGuard( m_aMutex );
139 uno::Reference< container::XHierarchicalNameAccess > xRootReadAccess
140 = getRootReadAccess();
142 OSL_ENSURE( xRootReadAccess.is(), "HierarchyEntry::hasData - No root!" );
144 if ( xRootReadAccess.is() )
145 return xRootReadAccess->hasByHierarchicalName( m_aPath );
147 return false;
151 bool HierarchyEntry::getData( HierarchyEntryData& rData )
155 osl::Guard< osl::Mutex > aGuard( m_aMutex );
157 uno::Reference< container::XHierarchicalNameAccess > xRootReadAccess
158 = getRootReadAccess();
160 OSL_ENSURE( xRootReadAccess.is(),
161 "HierarchyEntry::getData - No root!" );
163 if ( xRootReadAccess.is() )
165 OUString aTitlePath = m_aPath + "/Title";
167 // Note: Avoid NoSuchElementExceptions, because exceptions are
168 // relatively 'expensive'. Checking for availability of
169 // title value is sufficient here, because if it is
170 // there, the other values will be available too.
171 if ( !xRootReadAccess->hasByHierarchicalName( aTitlePath ) )
172 return false;
174 OUString aValue;
176 // Get Title value.
177 if ( !( xRootReadAccess->getByHierarchicalName( aTitlePath )
178 >>= aValue ) )
180 OSL_FAIL( "HierarchyEntry::getData - "
181 "Got no Title value!" );
182 return false;
185 rData.setTitle( aValue );
187 // Get TargetURL value.
188 OUString aTargetURLPath = m_aPath + "/TargetURL";
189 if ( !( xRootReadAccess->getByHierarchicalName( aTargetURLPath )
190 >>= aValue ) )
192 OSL_FAIL( "HierarchyEntry::getData - "
193 "Got no TargetURL value!" );
194 return false;
197 // TargetURL property may contain a reference to the Office
198 // installation directory. To ensure a reloctable office
199 // installation, the path to the office installation directory must
200 // never be stored directly. A placeholder is used instead. Replace
201 // it by actual installation directory.
202 if ( m_xOfficeInstDirs.is() && !aValue.isEmpty() )
203 aValue = m_xOfficeInstDirs->makeAbsoluteURL( aValue );
204 rData.setTargetURL( aValue );
206 OUString aTypePath = m_aPath + "/Type";
207 if ( xRootReadAccess->hasByHierarchicalName( aTypePath ) )
209 // Might not be present since it was introduced long after
210 // Title and TargetURL (#82433#)... So not getting it is
211 // not an error.
213 // Get Type value.
214 sal_Int32 nType = 0;
215 if ( xRootReadAccess->getByHierarchicalName( aTypePath )
216 >>= nType )
218 if ( nType == 0 )
220 rData.setType( HierarchyEntryData::LINK );
222 else if ( nType == 1 )
224 rData.setType( HierarchyEntryData::FOLDER );
226 else
228 OSL_FAIL( "HierarchyEntry::getData - "
229 "Unknown Type value!" );
230 return false;
235 rData.setName( m_aName );
236 return true;
239 catch ( uno::RuntimeException const & )
241 throw;
243 catch ( container::NoSuchElementException const & )
245 // getByHierarchicalName
247 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
249 return false;
253 bool HierarchyEntry::setData( const HierarchyEntryData& rData )
257 osl::Guard< osl::Mutex > aGuard( m_aMutex );
259 if ( !m_xConfigProvider.is() )
260 m_xConfigProvider.set(
261 m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext),
262 uno::UNO_QUERY );
264 if ( m_xConfigProvider.is() )
266 // Create parent's key. It must exist!
268 OUString aParentPath;
269 bool bRoot = true;
271 sal_Int32 nPos = m_aPath.lastIndexOf( '/' );
272 if ( nPos != -1 )
274 // Skip "/Children" segment of the path, too.
275 nPos = m_aPath.lastIndexOf( '/', nPos - 1 );
277 OSL_ENSURE( nPos != -1,
278 "HierarchyEntry::setData - Wrong path!" );
280 aParentPath += m_aPath.subView( 0, nPos );
281 bRoot = false;
284 uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence(
286 {CFGPROPERTY_NODEPATH, uno::Any(aParentPath)}
287 }));
289 uno::Reference< util::XChangesBatch > xBatch(
290 m_xConfigProvider->createInstanceWithArguments(
291 READWRITE_SERVICE_NAME,
292 aArguments ),
293 uno::UNO_QUERY );
295 OSL_ENSURE( xBatch.is(),
296 "HierarchyEntry::setData - No batch!" );
298 uno::Reference< container::XNameAccess > xParentNameAccess(
299 xBatch, uno::UNO_QUERY );
301 OSL_ENSURE( xParentNameAccess.is(),
302 "HierarchyEntry::setData - No name access!" );
304 if ( xBatch.is() && xParentNameAccess.is() )
306 // Try to create own key. It must not exist!
308 bool bExists = true;
309 uno::Any aMyKey;
313 uno::Reference< container::XNameAccess > xNameAccess;
315 if ( bRoot )
317 xNameAccess = xParentNameAccess;
319 else
321 xParentNameAccess->getByName("Children") >>= xNameAccess;
324 if ( xNameAccess->hasByName( m_aName ) )
325 aMyKey = xNameAccess->getByName( m_aName );
326 else
327 bExists = false;
329 catch ( container::NoSuchElementException const & )
331 bExists = false;
334 uno::Reference< container::XNameReplace > xNameReplace;
335 uno::Reference< container::XNameContainer > xContainer;
337 if ( bExists )
339 // Key exists. Replace values.
341 aMyKey >>= xNameReplace;
343 OSL_ENSURE( xNameReplace.is(),
344 "HierarchyEntry::setData - No name replace!" );
346 else
348 // Key does not exist. Create / fill / insert it.
350 uno::Reference< lang::XSingleServiceFactory > xFac;
352 if ( bRoot )
354 // Special handling for children of root,
355 // which is not an entry. It's only a set
356 // of entries.
357 xFac.set( xParentNameAccess, uno::UNO_QUERY );
359 else
361 // Append new entry to parents child list,
362 // which is a set of entries.
363 xParentNameAccess->getByName("Children") >>= xFac;
366 OSL_ENSURE( xFac.is(),
367 "HierarchyEntry::setData - No factory!" );
369 if ( xFac.is() )
371 xNameReplace.set( xFac->createInstance(), uno::UNO_QUERY );
373 OSL_ENSURE( xNameReplace.is(),
374 "HierarchyEntry::setData - No name replace!" );
376 if ( xNameReplace.is() )
378 xContainer.set( xFac, uno::UNO_QUERY );
380 OSL_ENSURE( xContainer.is(),
381 "HierarchyEntry::setData - No container!" );
386 if ( xNameReplace.is() )
388 // Set Title value.
389 xNameReplace->replaceByName(
390 "Title",
391 uno::makeAny( rData.getTitle() ) );
393 // Set TargetURL value.
395 // TargetURL property may contain a reference to the Office
396 // installation directory. To ensure a reloctable office
397 // installation, the path to the office installation
398 // directory must never be stored directly. Use a
399 // placeholder instead.
400 OUString aValue( rData.getTargetURL() );
401 if ( m_xOfficeInstDirs.is() && !aValue.isEmpty() )
402 aValue
403 = m_xOfficeInstDirs->makeRelocatableURL( aValue );
405 xNameReplace->replaceByName(
406 "TargetURL",
407 uno::makeAny( aValue ) );
409 // Set Type value.
410 sal_Int32 nType
411 = rData.getType() == HierarchyEntryData::LINK ? 0 : 1;
412 xNameReplace->replaceByName(
413 "Type",
414 uno::makeAny( nType ) );
416 if ( xContainer.is() )
417 xContainer->insertByName(
418 m_aName, uno::makeAny( xNameReplace ) );
420 // Commit changes.
421 xBatch->commitChanges();
422 return true;
427 catch ( lang::IllegalArgumentException const & )
429 // replaceByName, insertByName
431 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
433 catch ( uno::RuntimeException const & )
435 throw;
437 catch ( container::NoSuchElementException const & )
439 // replaceByName, getByName
441 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
443 catch ( container::ElementExistException const & )
445 // insertByName
447 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
449 catch ( lang::WrappedTargetException const & )
451 // replaceByName, insertByName, getByName, commitChanges
453 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
455 catch ( uno::Exception const & )
457 // createInstance, createInstanceWithArguments
459 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
462 return false;
466 bool HierarchyEntry::move(
467 const OUString& rNewURL, const HierarchyEntryData& rData )
469 osl::Guard< osl::Mutex > aGuard( m_aMutex );
471 OUString aNewPath = createPathFromHierarchyURL( HierarchyUri(rNewURL) );
473 if ( aNewPath == m_aPath )
474 return true;
476 bool bOldRoot = true;
477 uno::Reference< util::XChangesBatch > xOldParentBatch;
479 OUString aNewKey;
480 sal_Int32 nURLPos = rNewURL.lastIndexOf( '/' );
481 if ( nURLPos > HIERARCHY_URL_SCHEME_LENGTH )
482 aNewKey = rNewURL.copy( nURLPos + 1 );
483 else
485 OSL_FAIL( "HierarchyEntry::move - Invalid URL!" );
486 return false;
489 bool bNewRoot = true;
490 uno::Reference< util::XChangesBatch > xNewParentBatch;
492 bool bDifferentParents = true;
496 if ( !m_xConfigProvider.is() )
497 m_xConfigProvider.set(
498 m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext),
499 uno::UNO_QUERY );
501 if ( !m_xConfigProvider.is() )
502 return false;
504 OUString aOldParentPath;
505 sal_Int32 nPos = m_aPath.lastIndexOf( '/' );
506 if ( nPos != -1 )
508 // Skip "/Children" segment of the path, too.
509 nPos = m_aPath.lastIndexOf( '/', nPos - 1 );
511 OSL_ENSURE( nPos != -1, "HierarchyEntry::move - Wrong path!" );
513 aOldParentPath += m_aPath.subView( 0, nPos );
514 bOldRoot = false;
517 OUString aNewParentPath;
518 nPos = aNewPath.lastIndexOf( '/' );
519 if ( nPos != -1 )
521 // Skip "/Children" segment of the path, too.
522 nPos = aNewPath.lastIndexOf( '/', nPos - 1 );
524 OSL_ENSURE( nPos != -1, "HierarchyEntry::move - Wrong path!" );
526 aNewParentPath += aNewPath.subView( 0, nPos );
527 bNewRoot = false;
530 uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence(
532 {CFGPROPERTY_NODEPATH, uno::Any(aOldParentPath)}
533 }));
535 xOldParentBatch.set(
536 m_xConfigProvider->createInstanceWithArguments(
537 READWRITE_SERVICE_NAME,
538 aArguments ),
539 uno::UNO_QUERY );
541 OSL_ENSURE( xOldParentBatch.is(), "HierarchyEntry::move - No batch!" );
543 if ( !xOldParentBatch.is() )
544 return false;
546 if ( aOldParentPath == aNewParentPath )
548 bDifferentParents = false;
549 xNewParentBatch = xOldParentBatch;
551 else
553 bDifferentParents = true;
555 uno::Sequence<uno::Any> aArguments2(comphelper::InitAnyPropertySequence(
557 {CFGPROPERTY_NODEPATH, uno::Any(aNewParentPath)}
558 }));
560 xNewParentBatch.set(
561 m_xConfigProvider->createInstanceWithArguments(
562 READWRITE_SERVICE_NAME,
563 aArguments2 ),
564 uno::UNO_QUERY );
566 OSL_ENSURE(
567 xNewParentBatch.is(), "HierarchyEntry::move - No batch!" );
569 if ( !xNewParentBatch.is() )
570 return false;
573 catch ( uno::RuntimeException const & )
575 throw;
577 catch ( uno::Exception const & )
579 // createInstance, createInstanceWithArguments
581 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
582 return false;
586 // (1) Get entry...
589 uno::Any aEntry;
590 uno::Reference< container::XNameAccess > xOldParentNameAccess;
591 uno::Reference< container::XNameContainer > xOldNameContainer;
595 xOldParentNameAccess.set( xOldParentBatch, uno::UNO_QUERY );
597 OSL_ENSURE( xOldParentNameAccess.is(),
598 "HierarchyEntry::move - No name access!" );
600 if ( !xOldParentNameAccess.is() )
601 return false;
603 if ( bOldRoot )
605 xOldNameContainer.set( xOldParentNameAccess, uno::UNO_QUERY );
607 else
609 xOldParentNameAccess->getByName("Children") >>= xOldNameContainer;
612 aEntry = xOldNameContainer->getByName( m_aName );
614 catch ( container::NoSuchElementException const & )
616 // getByName
618 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
619 return false;
621 catch ( lang::WrappedTargetException const & )
623 // getByName
625 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
626 return false;
630 // (2) Remove entry... Note: Insert BEFORE remove does not work!
635 xOldNameContainer->removeByName( m_aName );
636 xOldParentBatch->commitChanges();
638 catch ( container::NoSuchElementException const & )
640 // getByName, removeByName
642 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
643 return false;
647 // (3) Insert entry at new parent...
652 uno::Reference< container::XNameReplace > xNewNameReplace;
653 aEntry >>= xNewNameReplace;
655 OSL_ENSURE( xNewNameReplace.is(),
656 "HierarchyEntry::move - No name replace!" );
658 if ( !xNewNameReplace.is() )
659 return false;
661 uno::Reference< container::XNameAccess > xNewParentNameAccess;
662 if ( bDifferentParents )
663 xNewParentNameAccess.set( xNewParentBatch, uno::UNO_QUERY );
664 else
665 xNewParentNameAccess = xOldParentNameAccess;
667 OSL_ENSURE( xNewParentNameAccess.is(),
668 "HierarchyEntry::move - No name access!" );
670 if ( !xNewParentNameAccess.is() )
671 return false;
673 uno::Reference< container::XNameContainer > xNewNameContainer;
674 if ( bDifferentParents )
676 if ( bNewRoot )
678 xNewNameContainer.set( xNewParentNameAccess, uno::UNO_QUERY );
680 else
682 xNewParentNameAccess->getByName("Children") >>= xNewNameContainer;
685 else
686 xNewNameContainer = xOldNameContainer;
688 if ( !xNewNameContainer.is() )
689 return false;
691 xNewNameReplace->replaceByName(
692 "Title",
693 uno::makeAny( rData.getTitle() ) );
695 // TargetURL property may contain a reference to the Office
696 // installation directory. To ensure a reloctable office
697 // installation, the path to the office installation
698 // directory must never be stored directly. Use a placeholder
699 // instead.
700 OUString aValue( rData.getTargetURL() );
701 if ( m_xOfficeInstDirs.is() && !aValue.isEmpty() )
702 aValue = m_xOfficeInstDirs->makeRelocatableURL( aValue );
703 xNewNameReplace->replaceByName(
704 "TargetURL",
705 uno::makeAny( aValue ) );
706 sal_Int32 nType = rData.getType() == HierarchyEntryData::LINK ? 0 : 1;
707 xNewNameReplace->replaceByName(
708 "Type",
709 uno::makeAny( nType ) );
711 xNewNameContainer->insertByName( aNewKey, aEntry );
712 xNewParentBatch->commitChanges();
714 catch ( container::NoSuchElementException const & )
716 // replaceByName, insertByName, getByName
718 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
719 return false;
721 catch ( lang::IllegalArgumentException const & )
723 // replaceByName, insertByName
725 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
726 return false;
728 catch ( container::ElementExistException const & )
730 // insertByName
732 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
733 return false;
735 catch ( lang::WrappedTargetException const & )
737 // replaceByName, insertByName, getByName
739 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
740 return false;
743 return true;
747 bool HierarchyEntry::remove()
751 osl::Guard< osl::Mutex > aGuard( m_aMutex );
753 if ( !m_xConfigProvider.is() )
754 m_xConfigProvider.set(
755 m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext),
756 uno::UNO_QUERY );
758 if ( m_xConfigProvider.is() )
760 // Create parent's key. It must exist!
762 OUString aParentPath;
763 bool bRoot = true;
765 sal_Int32 nPos = m_aPath.lastIndexOf( '/' );
766 if ( nPos != -1 )
768 // Skip "/Children" segment of the path, too.
769 nPos = m_aPath.lastIndexOf( '/', nPos - 1 );
771 OSL_ENSURE( nPos != -1,
772 "HierarchyEntry::remove - Wrong path!" );
774 aParentPath += m_aPath.subView( 0, nPos );
775 bRoot = false;
778 uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence(
780 {CFGPROPERTY_NODEPATH, uno::Any(aParentPath)}
781 }));
783 uno::Reference< util::XChangesBatch > xBatch(
784 m_xConfigProvider->createInstanceWithArguments(
785 READWRITE_SERVICE_NAME,
786 aArguments ),
787 uno::UNO_QUERY );
789 OSL_ENSURE( xBatch.is(),
790 "HierarchyEntry::remove - No batch!" );
792 uno::Reference< container::XNameAccess > xParentNameAccess(
793 xBatch, uno::UNO_QUERY );
795 OSL_ENSURE( xParentNameAccess.is(),
796 "HierarchyEntry::remove - No name access!" );
798 if ( xBatch.is() && xParentNameAccess.is() )
800 uno::Reference< container::XNameContainer > xContainer;
802 if ( bRoot )
804 // Special handling for children of root,
805 // which is not an entry. It's only a set
806 // of entries.
807 xContainer.set( xParentNameAccess, uno::UNO_QUERY );
809 else
811 // Append new entry to parents child list,
812 // which is a set of entries.
813 xParentNameAccess->getByName("Children") >>= xContainer;
816 OSL_ENSURE( xContainer.is(),
817 "HierarchyEntry::remove - No container!" );
819 if ( xContainer.is() )
821 xContainer->removeByName( m_aName );
822 xBatch->commitChanges();
823 return true;
828 catch ( uno::RuntimeException const & )
830 throw;
832 catch ( container::NoSuchElementException const & )
834 // getByName, removeByName
836 OSL_FAIL(
837 "HierarchyEntry::remove - caught NoSuchElementException!" );
839 catch ( lang::WrappedTargetException const & )
841 // getByName, commitChanges
843 OSL_FAIL(
844 "HierarchyEntry::remove - caught WrappedTargetException!" );
846 catch ( uno::Exception const & )
848 // createInstance, createInstanceWithArguments
850 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
853 return false;
857 bool HierarchyEntry::first( iterator const & it )
859 osl::Guard< osl::Mutex > aGuard( m_aMutex );
861 if ( it.m_pImpl->pos == -1 )
863 // Init...
867 uno::Reference< container::XHierarchicalNameAccess >
868 xRootHierNameAccess = getRootReadAccess();
870 if ( xRootHierNameAccess.is() )
872 uno::Reference< container::XNameAccess > xNameAccess;
874 if ( !m_aPath.isEmpty() )
876 OUString aPath = m_aPath + "/Children";
878 xRootHierNameAccess->getByHierarchicalName( aPath )
879 >>= xNameAccess;
881 else
882 xNameAccess.set( xRootHierNameAccess, uno::UNO_QUERY );
884 OSL_ENSURE( xNameAccess.is(),
885 "HierarchyEntry::first - No name access!" );
887 if ( xNameAccess.is() )
888 it.m_pImpl->names = xNameAccess->getElementNames();
890 uno::Reference< container::XHierarchicalNameAccess >
891 xHierNameAccess( xNameAccess, uno::UNO_QUERY );
893 OSL_ENSURE( xHierNameAccess.is(),
894 "HierarchyEntry::first - No hier. name access!" );
896 it.m_pImpl->dir = xHierNameAccess;
898 it.m_pImpl->officeDirs = m_xOfficeInstDirs;
901 catch ( uno::RuntimeException const & )
903 throw;
905 catch ( container::NoSuchElementException const& )
907 // getByHierarchicalName
909 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
911 catch ( uno::Exception const & )
913 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
917 if ( !it.m_pImpl->names.hasElements() )
918 return false;
920 it.m_pImpl->pos = 0;
921 return true;
925 bool HierarchyEntry::next( iterator const & it )
927 osl::Guard< osl::Mutex > aGuard( m_aMutex );
929 if ( it.m_pImpl->pos == -1 )
930 return first( it );
932 ++(it.m_pImpl->pos);
934 return ( it.m_pImpl->pos < it.m_pImpl->names.getLength() );
938 OUString HierarchyEntry::createPathFromHierarchyURL(
939 const HierarchyUri& rURI )
941 // Transform path...
942 // folder/subfolder/subsubfolder
943 // --> ['folder']/Children/['subfolder']/Children/['subsubfolder']
945 const OUString aPath = rURI.getPath().copy( 1 ); // skip leading slash.
946 sal_Int32 nLen = aPath.getLength();
948 if ( nLen )
950 OUStringBuffer aNewPath;
951 aNewPath.append( "['" );
953 sal_Int32 nStart = 0;
954 sal_Int32 nEnd = aPath.indexOf( '/' );
958 if ( nEnd == -1 )
959 nEnd = nLen;
961 OUString aToken = aPath.copy( nStart, nEnd - nStart );
962 makeXMLName( aToken, aNewPath );
964 if ( nEnd != nLen )
966 aNewPath.append( "']/Children/['" );
967 nStart = nEnd + 1;
968 nEnd = aPath.indexOf( '/', nStart );
970 else
971 aNewPath.append( "']" );
973 while ( nEnd != nLen );
975 return aNewPath.makeStringAndClear();
978 return aPath;
982 uno::Reference< container::XHierarchicalNameAccess >
983 HierarchyEntry::getRootReadAccess()
985 if ( !m_xRootReadAccess.is() )
987 osl::Guard< osl::Mutex > aGuard( m_aMutex );
988 if ( !m_xRootReadAccess.is() )
990 if ( m_bTriedToGetRootReadAccess )
992 OSL_FAIL( "HierarchyEntry::getRootReadAccess - "
993 "Unable to read any config data! -> #82494#" );
994 return uno::Reference< container::XHierarchicalNameAccess >();
999 if ( !m_xConfigProvider.is() )
1000 m_xConfigProvider.set(
1001 m_xContext->getServiceManager()->createInstanceWithContext(m_aServiceSpecifier, m_xContext),
1002 uno::UNO_QUERY );
1004 if ( m_xConfigProvider.is() )
1006 // Create Root object.
1008 uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence(
1010 {CFGPROPERTY_NODEPATH, uno::Any(OUString())} // root path
1011 }));
1013 m_bTriedToGetRootReadAccess = true;
1015 m_xRootReadAccess.set(
1016 m_xConfigProvider->createInstanceWithArguments(
1017 READ_SERVICE_NAME,
1018 aArguments ),
1019 uno::UNO_QUERY );
1022 catch ( uno::RuntimeException const & )
1024 throw;
1026 catch ( uno::Exception const & )
1028 // createInstance, createInstanceWithArguments
1030 TOOLS_WARN_EXCEPTION("ucb.ucp", "");
1034 return m_xRootReadAccess;
1038 // HierarchyEntry::iterator Implementation.
1041 HierarchyEntry::iterator::iterator()
1042 : m_pImpl( new iterator_Impl )
1047 HierarchyEntry::iterator::~iterator()
1052 const HierarchyEntryData& HierarchyEntry::iterator::operator*() const
1054 if ( ( m_pImpl->pos != -1 )
1055 && ( m_pImpl->dir.is() )
1056 && ( m_pImpl->pos < m_pImpl->names.getLength() ) )
1060 OUStringBuffer aKey;
1061 aKey.append( "['" );
1062 makeXMLName( m_pImpl->names.getConstArray()[ m_pImpl->pos ], aKey );
1063 aKey.append( "']" );
1065 OUString aTitle = aKey.makeStringAndClear();
1066 OUString aTargetURL = aTitle;
1067 OUString aType = aTitle;
1069 aTitle += "/Title";
1070 aTargetURL += "/TargetURL";
1071 aType += "/Type";
1073 OUString aValue;
1074 m_pImpl->dir->getByHierarchicalName( aTitle ) >>= aValue;
1075 m_pImpl->entry.setTitle( aValue );
1077 m_pImpl->dir->getByHierarchicalName( aTargetURL ) >>= aValue;
1079 // TargetURL property may contain a reference to the Office
1080 // installation directory. To ensure a reloctable office
1081 // installation, the path to the office installation directory must
1082 // never be stored directly. A placeholder is used instead. Replace
1083 // it by actual installation directory.
1084 if ( m_pImpl->officeDirs.is() && !aValue.isEmpty() )
1085 aValue = m_pImpl->officeDirs->makeAbsoluteURL( aValue );
1086 m_pImpl->entry.setTargetURL( aValue );
1088 if ( m_pImpl->dir->hasByHierarchicalName( aType ) )
1090 // Might not be present since it was introduced long
1091 // after Title and TargetURL (#82433#)... So not getting
1092 // it is not an error.
1094 // Get Type value.
1095 sal_Int32 nType = 0;
1096 if ( m_pImpl->dir->getByHierarchicalName( aType ) >>= nType )
1098 if ( nType == 0 )
1100 m_pImpl->entry.setType( HierarchyEntryData::LINK );
1102 else if ( nType == 1 )
1104 m_pImpl->entry.setType( HierarchyEntryData::FOLDER );
1106 else
1108 OSL_FAIL( "HierarchyEntry::getData - "
1109 "Unknown Type value!" );
1114 m_pImpl->entry.setName(
1115 m_pImpl->names.getConstArray()[ m_pImpl->pos ] );
1117 catch ( container::NoSuchElementException const & )
1119 m_pImpl->entry = HierarchyEntryData();
1123 return m_pImpl->entry;
1126 } // namespace hierarchy_ucp
1128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */