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>
50static void makeXMLName( std::u16string_view rIn, OUStringBuffer & rBuffer )
52 size_t nCount = rIn.size();
59 rBuffer.append(
"&" );
63 rBuffer.append(
""" );
67 rBuffer.append(
"'" );
71 rBuffer.append(
"<" );
75 rBuffer.append(
">" );
97 uno::Reference< uno::XComponentContext > xContext,
99 const OUString& rURL )
102 m_bTriedToGetRootReadAccess( false )
117 sal_Int32
nPos = rURL.lastIndexOf(
'/' );
121 OSL_FAIL(
"HierarchyEntry - Invalid URL!" );
127 uno::Reference< container::XHierarchicalNameAccess > xRootReadAccess
130 OSL_ENSURE( xRootReadAccess.is(),
"HierarchyEntry::hasData - No root!" );
132 if ( xRootReadAccess.is() )
133 return xRootReadAccess->hasByHierarchicalName(
m_aPath );
143 uno::Reference< container::XHierarchicalNameAccess > xRootReadAccess
146 OSL_ENSURE( xRootReadAccess.is(),
147 "HierarchyEntry::getData - No root!" );
149 if ( xRootReadAccess.is() )
151 OUString aTitlePath =
m_aPath +
"/Title";
157 if ( !xRootReadAccess->hasByHierarchicalName( aTitlePath ) )
163 if ( !( xRootReadAccess->getByHierarchicalName( aTitlePath )
166 OSL_FAIL(
"HierarchyEntry::getData - "
167 "Got no Title value!" );
174 OUString aTargetURLPath =
m_aPath +
"/TargetURL";
175 if ( !( xRootReadAccess->getByHierarchicalName( aTargetURLPath )
178 OSL_FAIL(
"HierarchyEntry::getData - "
179 "Got no TargetURL value!" );
192 OUString aTypePath =
m_aPath +
"/Type";
193 if ( xRootReadAccess->hasByHierarchicalName( aTypePath ) )
201 if ( xRootReadAccess->getByHierarchicalName( aTypePath )
208 else if (
nType == 1 )
214 OSL_FAIL(
"HierarchyEntry::getData - "
215 "Unknown Type value!" );
225 catch ( uno::RuntimeException
const & )
229 catch ( container::NoSuchElementException
const & )
243 std::unique_lock aGuard(
m_aMutex );
254 OUString aParentPath;
263 OSL_ENSURE(
nPos != -1,
264 "HierarchyEntry::setData - Wrong path!" );
275 uno::Reference< util::XChangesBatch > xBatch(
281 OSL_ENSURE( xBatch.is(),
282 "HierarchyEntry::setData - No batch!" );
284 uno::Reference< container::XNameAccess > xParentNameAccess(
285 xBatch, uno::UNO_QUERY );
287 OSL_ENSURE( xParentNameAccess.is(),
288 "HierarchyEntry::setData - No name access!" );
290 if ( xBatch.is() && xParentNameAccess.is() )
299 uno::Reference< container::XNameAccess > xNameAccess;
303 xNameAccess = xParentNameAccess;
307 xParentNameAccess->getByName(
"Children") >>= xNameAccess;
310 if ( xNameAccess->hasByName(
m_aName ) )
311 aMyKey = xNameAccess->getByName(
m_aName );
315 catch ( container::NoSuchElementException
const & )
320 uno::Reference< container::XNameReplace > xNameReplace;
321 uno::Reference< container::XNameContainer > xContainer;
327 aMyKey >>= xNameReplace;
329 OSL_ENSURE( xNameReplace.is(),
330 "HierarchyEntry::setData - No name replace!" );
336 uno::Reference< lang::XSingleServiceFactory > xFac;
343 xFac.set( xParentNameAccess, uno::UNO_QUERY );
349 xParentNameAccess->getByName(
"Children") >>= xFac;
352 OSL_ENSURE( xFac.is(),
353 "HierarchyEntry::setData - No factory!" );
357 xNameReplace.set( xFac->createInstance(), uno::UNO_QUERY );
359 OSL_ENSURE( xNameReplace.is(),
360 "HierarchyEntry::setData - No name replace!" );
362 if ( xNameReplace.is() )
364 xContainer.set( xFac, uno::UNO_QUERY );
366 OSL_ENSURE( xContainer.is(),
367 "HierarchyEntry::setData - No container!" );
372 if ( xNameReplace.is() )
375 xNameReplace->replaceByName(
391 xNameReplace->replaceByName(
398 xNameReplace->replaceByName(
402 if ( xContainer.is() )
403 xContainer->insertByName(
407 xBatch->commitChanges();
413 catch ( lang::IllegalArgumentException
const & )
419 catch ( uno::RuntimeException
const & )
423 catch ( container::NoSuchElementException
const & )
429 catch ( container::ElementExistException
const & )
435 catch ( lang::WrappedTargetException
const & )
441 catch ( uno::Exception
const & )
457 std::unique_lock aGuard(
m_aMutex );
462 bool bOldRoot =
true;
463 uno::Reference< util::XChangesBatch > xOldParentBatch;
466 sal_Int32 nURLPos = rNewURL.lastIndexOf(
'/' );
468 aNewKey = rNewURL.copy( nURLPos + 1 );
471 OSL_FAIL(
"HierarchyEntry::move - Invalid URL!" );
475 bool bNewRoot =
true;
476 uno::Reference< util::XChangesBatch > xNewParentBatch;
478 bool bDifferentParents =
true;
490 OUString aOldParentPath;
497 OSL_ENSURE(
nPos != -1,
"HierarchyEntry::move - Wrong path!" );
503 OUString aNewParentPath;
504 nPos = aNewPath.lastIndexOf(
'/' );
508 nPos = aNewPath.lastIndexOf(
'/',
nPos - 1 );
510 OSL_ENSURE(
nPos != -1,
"HierarchyEntry::move - Wrong path!" );
512 aNewParentPath += aNewPath.subView( 0,
nPos );
527 OSL_ENSURE( xOldParentBatch.is(),
"HierarchyEntry::move - No batch!" );
529 if ( !xOldParentBatch.is() )
532 if ( aOldParentPath == aNewParentPath )
534 bDifferentParents =
false;
535 xNewParentBatch = xOldParentBatch;
539 bDifferentParents =
true;
553 xNewParentBatch.is(),
"HierarchyEntry::move - No batch!" );
555 if ( !xNewParentBatch.is() )
559 catch ( uno::RuntimeException
const & )
563 catch ( uno::Exception
const & )
576 uno::Reference< container::XNameAccess > xOldParentNameAccess;
577 uno::Reference< container::XNameContainer > xOldNameContainer;
581 xOldParentNameAccess.set( xOldParentBatch, uno::UNO_QUERY );
583 OSL_ENSURE( xOldParentNameAccess.is(),
584 "HierarchyEntry::move - No name access!" );
586 if ( !xOldParentNameAccess.is() )
591 xOldNameContainer.set( xOldParentNameAccess, uno::UNO_QUERY );
595 xOldParentNameAccess->getByName(
"Children") >>= xOldNameContainer;
598 aEntry = xOldNameContainer->getByName(
m_aName );
600 catch ( container::NoSuchElementException
const & )
607 catch ( lang::WrappedTargetException
const & )
621 xOldNameContainer->removeByName(
m_aName );
622 xOldParentBatch->commitChanges();
624 catch ( container::NoSuchElementException
const & )
638 uno::Reference< container::XNameReplace > xNewNameReplace;
639 aEntry >>= xNewNameReplace;
641 OSL_ENSURE( xNewNameReplace.is(),
642 "HierarchyEntry::move - No name replace!" );
644 if ( !xNewNameReplace.is() )
647 uno::Reference< container::XNameAccess > xNewParentNameAccess;
648 if ( bDifferentParents )
649 xNewParentNameAccess.set( xNewParentBatch, uno::UNO_QUERY );
651 xNewParentNameAccess = xOldParentNameAccess;
653 OSL_ENSURE( xNewParentNameAccess.is(),
654 "HierarchyEntry::move - No name access!" );
656 if ( !xNewParentNameAccess.is() )
659 uno::Reference< container::XNameContainer > xNewNameContainer;
660 if ( bDifferentParents )
664 xNewNameContainer.set( xNewParentNameAccess, uno::UNO_QUERY );
668 xNewParentNameAccess->getByName(
"Children") >>= xNewNameContainer;
672 xNewNameContainer = xOldNameContainer;
674 if ( !xNewNameContainer.is() )
677 xNewNameReplace->replaceByName(
689 xNewNameReplace->replaceByName(
693 xNewNameReplace->replaceByName(
697 xNewNameContainer->insertByName( aNewKey, aEntry );
698 xNewParentBatch->commitChanges();
700 catch ( container::NoSuchElementException
const & )
707 catch ( lang::IllegalArgumentException
const & )
714 catch ( container::ElementExistException
const & )
721 catch ( lang::WrappedTargetException
const & )
737 std::unique_lock aGuard(
m_aMutex );
748 OUString aParentPath;
757 OSL_ENSURE(
nPos != -1,
758 "HierarchyEntry::remove - Wrong path!" );
769 uno::Reference< util::XChangesBatch > xBatch(
775 OSL_ENSURE( xBatch.is(),
776 "HierarchyEntry::remove - No batch!" );
778 uno::Reference< container::XNameAccess > xParentNameAccess(
779 xBatch, uno::UNO_QUERY );
781 OSL_ENSURE( xParentNameAccess.is(),
782 "HierarchyEntry::remove - No name access!" );
784 if ( xBatch.is() && xParentNameAccess.is() )
786 uno::Reference< container::XNameContainer > xContainer;
793 xContainer.set( xParentNameAccess, uno::UNO_QUERY );
799 xParentNameAccess->getByName(
"Children") >>= xContainer;
802 OSL_ENSURE( xContainer.is(),
803 "HierarchyEntry::remove - No container!" );
805 if ( xContainer.is() )
807 xContainer->removeByName(
m_aName );
808 xBatch->commitChanges();
814 catch ( uno::RuntimeException
const & )
818 catch ( container::NoSuchElementException
const & )
823 "HierarchyEntry::remove - caught NoSuchElementException!" );
825 catch ( lang::WrappedTargetException
const & )
830 "HierarchyEntry::remove - caught WrappedTargetException!" );
832 catch ( uno::Exception
const & )
851 uno::Reference< container::XHierarchicalNameAccess >
854 if ( xRootHierNameAccess.is() )
856 uno::Reference< container::XNameAccess > xNameAccess;
860 OUString aPath =
m_aPath +
"/Children";
862 xRootHierNameAccess->getByHierarchicalName( aPath )
866 xNameAccess.set( xRootHierNameAccess, uno::UNO_QUERY );
868 OSL_ENSURE( xNameAccess.is(),
869 "HierarchyEntry::first - No name access!" );
871 if ( xNameAccess.is() )
872 it.
names = xNameAccess->getElementNames();
874 uno::Reference< container::XHierarchicalNameAccess >
875 xHierNameAccess( xNameAccess, uno::UNO_QUERY );
877 OSL_ENSURE( xHierNameAccess.is(),
878 "HierarchyEntry::first - No hier. name access!" );
880 it.
dir = xHierNameAccess;
885 catch ( uno::RuntimeException
const & )
889 catch ( container::NoSuchElementException
const& )
895 catch ( uno::Exception
const & )
901 if ( !it.
names.hasElements() )
916 return ( it.
pos < it.
names.getLength() );
927 const OUString aPath = rURI.
getPath().copy( 1 );
928 sal_Int32 nLen = aPath.getLength();
932 OUStringBuffer aNewPath(
"['" );
934 sal_Int32 nStart = 0;
935 sal_Int32 nEnd = aPath.indexOf(
'/' );
942 OUString aToken = aPath.copy( nStart, nEnd - nStart );
947 aNewPath.append(
"']/Children/['" );
949 nEnd = aPath.indexOf(
'/', nStart );
952 aNewPath.append(
"']" );
954 while ( nEnd != nLen );
956 return aNewPath.makeStringAndClear();
963uno::Reference< container::XHierarchicalNameAccess >
968 std::unique_lock aGuard(
m_aMutex );
973 OSL_FAIL(
"HierarchyEntry::getRootReadAccess - "
974 "Unable to read any config data! -> #82494#" );
975 return uno::Reference< container::XHierarchicalNameAccess >();
1003 catch ( uno::RuntimeException
const & )
1007 catch ( uno::Exception
const & )
1030 OUStringBuffer aKey(
"['" );
1032 aKey.append(
"']" );
1034 OUString aTitle = aKey.makeStringAndClear();
1036 OUString aType = aTitle;
1043 dir->getByHierarchicalName( aTitle ) >>= aValue;
1054 aValue =
officeDirs->makeAbsoluteURL( aValue );
1057 if (
dir->hasByHierarchicalName( aType ) )
1064 sal_Int32
nType = 0;
1065 if (
dir->getByHierarchicalName( aType ) >>=
nType )
1071 else if (
nType == 1 )
1077 OSL_FAIL(
"HierarchyEntry::getData - "
1078 "Unknown Type value!" );
1086 catch ( container::NoSuchElementException
const & )
Reference< XComponentContext > m_xContext
css::uno::Reference< css::lang::XMultiServiceFactory > getConfigProvider(const OUString &rServiceSpecifier)
css::uno::Reference< css::container::XHierarchicalNameAccess > getRootConfigReadNameAccess(const OUString &rServiceSpecifier)
void setName(const OUString &rName)
void setTargetURL(const OUString &rURL)
const OUString & getTitle() const
void setTitle(const OUString &rTitle)
void setType(const Type &rType)
const OUString & getTargetURL() const
css::uno::Sequence< OUString > names
css::uno::Reference< css::container::XHierarchicalNameAccess > dir
const HierarchyEntryData & operator*()
css::uno::Reference< css::util::XOfficeInstallationDirectories > officeDirs
css::uno::Reference< css::container::XHierarchicalNameAccess > m_xRootReadAccess
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool move(const OUString &rNewURL, const HierarchyEntryData &rData)
HierarchyEntry(css::uno::Reference< css::uno::XComponentContext > xContext, HierarchyContentProvider *pProvider, const OUString &rURL)
css::uno::Reference< css::lang::XMultiServiceFactory > m_xConfigProvider
css::uno::Reference< css::container::XHierarchicalNameAccess > getRootReadAccess()
bool setData(const HierarchyEntryData &rData)
bool getData(HierarchyEntryData &rData)
OUString m_aServiceSpecifier
bool m_bTriedToGetRootReadAccess
static OUString createPathFromHierarchyURL(const HierarchyUri &rURI)
css::uno::Reference< css::util::XOfficeInstallationDirectories > m_xOfficeInstDirs
const OUString & getService() const
const OUString & getPath() const
#define TOOLS_WARN_EXCEPTION(area, stream)
#define HIERARCHY_URL_SCHEME_LENGTH
Sequence< PropertyValue > aArguments
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
static void makeXMLName(std::u16string_view rIn, OUStringBuffer &rBuffer)
constexpr OUStringLiteral READWRITE_SERVICE_NAME
constexpr OUStringLiteral CFGPROPERTY_NODEPATH
constexpr OUStringLiteral READ_SERVICE_NAME
uno::Reference< util::XOfficeInstallationDirectories > m_xOfficeInstDirs