bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / DriversConfig.cxx
blobe165763d5e5e054fa3319054fc05131b6128133b
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 .
19 #include <connectivity/DriversConfig.hxx>
20 #include <tools/wldcrd.hxx>
21 #include <comphelper/sequence.hxx>
23 using namespace connectivity;
24 using namespace utl;
25 using namespace ::com::sun::star;
27 namespace
29 void lcl_convert(const uno::Sequence< OUString >& _aSource,uno::Any& _rDest)
31 uno::Sequence<uno::Any> aRet(_aSource.getLength());
32 uno::Any* pAny = aRet.getArray();
33 const OUString* pIter = _aSource.getConstArray();
34 const OUString* pEnd = pIter + _aSource.getLength();
35 for (;pIter != pEnd ; ++pIter,++pAny)
37 *pAny <<= *pIter;
39 _rDest <<= aRet;
41 void lcl_fillValues(const ::utl::OConfigurationNode& _aURLPatternNode,const OUString& _sNode,::comphelper::NamedValueCollection& _rValues)
43 const ::utl::OConfigurationNode aPropertiesNode = _aURLPatternNode.openNode(_sNode);
44 if ( aPropertiesNode.isValid() )
46 uno::Sequence< OUString > aStringSeq;
47 const uno::Sequence< OUString > aProperties = aPropertiesNode.getNodeNames();
48 const OUString* pPropertiesIter = aProperties.getConstArray();
49 const OUString* pPropertiesEnd = pPropertiesIter + aProperties.getLength();
50 for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter)
52 uno::Any aValue = aPropertiesNode.getNodeValue(*pPropertiesIter + "/Value");
53 if ( aValue >>= aStringSeq )
55 lcl_convert(aStringSeq,aValue);
57 _rValues.put(*pPropertiesIter,aValue);
58 } // for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter,++pNamedIter)
59 } // if ( aPropertiesNode.isValid() )
61 void lcl_readURLPatternNode(const ::utl::OConfigurationTreeRoot& _aInstalled,const OUString& _sEntry,TInstalledDriver& _rInstalledDriver)
63 const ::utl::OConfigurationNode aURLPatternNode = _aInstalled.openNode(_sEntry);
64 if ( aURLPatternNode.isValid() )
66 OUString sParentURLPattern;
67 aURLPatternNode.getNodeValue("ParentURLPattern") >>= sParentURLPattern;
68 if ( !sParentURLPattern.isEmpty() )
69 lcl_readURLPatternNode(_aInstalled,sParentURLPattern,_rInstalledDriver);
71 OUString sDriverFactory;
72 aURLPatternNode.getNodeValue("Driver") >>= sDriverFactory;
73 if ( !sDriverFactory.isEmpty() )
74 _rInstalledDriver.sDriverFactory = sDriverFactory;
76 OUString sDriverTypeDisplayName;
77 aURLPatternNode.getNodeValue("DriverTypeDisplayName") >>= sDriverTypeDisplayName;
78 OSL_ENSURE(!sDriverTypeDisplayName.isEmpty(),"No valid DriverTypeDisplayName property!");
79 if ( !sDriverTypeDisplayName.isEmpty() )
80 _rInstalledDriver.sDriverTypeDisplayName = sDriverTypeDisplayName;
82 lcl_fillValues(aURLPatternNode,"Properties",_rInstalledDriver.aProperties);
83 lcl_fillValues(aURLPatternNode,"Features",_rInstalledDriver.aFeatures);
84 lcl_fillValues(aURLPatternNode,"MetaData",_rInstalledDriver.aMetaData);
89 DriversConfigImpl::DriversConfigImpl()
93 const TInstalledDrivers& DriversConfigImpl::getInstalledDrivers(const uno::Reference< uno::XComponentContext >& _rxORB) const
95 if ( m_aDrivers.empty() )
97 if ( !m_aInstalled.isValid() )
99 m_aInstalled = ::utl::OConfigurationTreeRoot::createWithComponentContext(_rxORB,
100 "org.openoffice.Office.DataAccess.Drivers/Installed", -1, ::utl::OConfigurationTreeRoot::CM_READONLY);
103 if ( m_aInstalled.isValid() )
105 const uno::Sequence< OUString > aURLPatterns = m_aInstalled.getNodeNames();
106 const OUString* pPatternIter = aURLPatterns.getConstArray();
107 const OUString* pPatternEnd = pPatternIter + aURLPatterns.getLength();
108 for (;pPatternIter != pPatternEnd ; ++pPatternIter)
110 TInstalledDriver aInstalledDriver;
111 lcl_readURLPatternNode(m_aInstalled,*pPatternIter,aInstalledDriver);
112 if ( !aInstalledDriver.sDriverFactory.isEmpty() )
113 m_aDrivers.emplace(*pPatternIter,aInstalledDriver);
115 } // if ( m_aInstalled.isValid() )
117 return m_aDrivers;
120 DriversConfig::DriversConfig(const uno::Reference< uno::XComponentContext >& _rxORB)
121 :m_xORB(_rxORB)
126 DriversConfig::~DriversConfig()
131 DriversConfig::DriversConfig( const DriversConfig& _rhs )
133 *this = _rhs;
137 DriversConfig& DriversConfig::operator=( const DriversConfig& _rhs )
139 if ( this != &_rhs )
141 m_aNode = _rhs.m_aNode;
143 return *this;
147 OUString DriversConfig::getDriverFactoryName(const OUString& _sURL) const
149 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
150 OUString sRet;
151 OUString sOldPattern;
152 for(const auto& [rPattern, rDriver] : rDrivers)
154 WildCard aWildCard(rPattern);
155 if ( sOldPattern.getLength() < rPattern.getLength() && aWildCard.Matches(_sURL) )
157 sRet = rDriver.sDriverFactory;
158 sOldPattern = rPattern;
162 return sRet;
165 OUString DriversConfig::getDriverTypeDisplayName(const OUString& _sURL) const
167 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
168 OUString sRet;
169 OUString sOldPattern;
170 for(const auto& [rPattern, rDriver] : rDrivers)
172 WildCard aWildCard(rPattern);
173 if ( sOldPattern.getLength() < rPattern.getLength() && aWildCard.Matches(_sURL) )
175 sRet = rDriver.sDriverTypeDisplayName;
176 sOldPattern = rPattern;
180 return sRet;
183 const ::comphelper::NamedValueCollection& DriversConfig::getProperties(const OUString& _sURL) const
185 return impl_get(_sURL,1);
188 const ::comphelper::NamedValueCollection& DriversConfig::getFeatures(const OUString& _sURL) const
190 return impl_get(_sURL,0);
193 const ::comphelper::NamedValueCollection& DriversConfig::getMetaData(const OUString& _sURL) const
195 return impl_get(_sURL,2);
198 const ::comphelper::NamedValueCollection& DriversConfig::impl_get(const OUString& _sURL,sal_Int32 _nProps) const
200 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
201 const ::comphelper::NamedValueCollection* pRet = nullptr;
202 OUString sOldPattern;
203 for(const auto& [rPattern, rDriver] : rDrivers)
205 WildCard aWildCard(rPattern);
206 if ( sOldPattern.getLength() < rPattern.getLength() && aWildCard.Matches(_sURL) )
208 switch(_nProps)
210 case 0:
211 pRet = &rDriver.aFeatures;
212 break;
213 case 1:
214 pRet = &rDriver.aProperties;
215 break;
216 case 2:
217 pRet = &rDriver.aMetaData;
218 break;
220 sOldPattern = rPattern;
222 } // for(;aIter != aEnd;++aIter)
223 if ( pRet == nullptr )
225 static const ::comphelper::NamedValueCollection s_sEmpty;
226 pRet = &s_sEmpty;
228 return *pRet;
231 uno::Sequence< OUString > DriversConfig::getURLs() const
233 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
234 return comphelper::mapKeysToSequence(rDrivers);
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */