Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / DriversConfig.cxx
blob7804ad9f6fab7599e48578baf953857da63b01c6
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 <svtools/miscopt.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 static const OUString s_sValue("/Value");
48 const uno::Sequence< OUString > aProperties = aPropertiesNode.getNodeNames();
49 const OUString* pPropertiesIter = aProperties.getConstArray();
50 const OUString* pPropertiesEnd = pPropertiesIter + aProperties.getLength();
51 for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter)
53 uno::Any aValue = aPropertiesNode.getNodeValue(*pPropertiesIter + s_sValue);
54 if ( aValue >>= aStringSeq )
56 lcl_convert(aStringSeq,aValue);
58 _rValues.put(*pPropertiesIter,aValue);
59 } // for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter,++pNamedIter)
60 } // if ( aPropertiesNode.isValid() )
62 void lcl_readURLPatternNode(const ::utl::OConfigurationTreeRoot& _aInstalled,const OUString& _sEntry,TInstalledDriver& _rInstalledDriver)
64 const ::utl::OConfigurationNode aURLPatternNode = _aInstalled.openNode(_sEntry);
65 if ( aURLPatternNode.isValid() )
67 OUString sParentURLPattern;
68 aURLPatternNode.getNodeValue("ParentURLPattern") >>= sParentURLPattern;
69 if ( !sParentURLPattern.isEmpty() )
70 lcl_readURLPatternNode(_aInstalled,sParentURLPattern,_rInstalledDriver);
72 OUString sDriverFactory;
73 aURLPatternNode.getNodeValue("Driver") >>= sDriverFactory;
74 if ( !sDriverFactory.isEmpty() )
75 _rInstalledDriver.sDriverFactory = sDriverFactory;
77 OUString sDriverTypeDisplayName;
78 aURLPatternNode.getNodeValue("DriverTypeDisplayName") >>= sDriverTypeDisplayName;
79 OSL_ENSURE(!sDriverTypeDisplayName.isEmpty(),"No valid DriverTypeDisplayName property!");
80 if ( !sDriverTypeDisplayName.isEmpty() )
81 _rInstalledDriver.sDriverTypeDisplayName = sDriverTypeDisplayName;
83 lcl_fillValues(aURLPatternNode,"Properties",_rInstalledDriver.aProperties);
84 lcl_fillValues(aURLPatternNode,"Features",_rInstalledDriver.aFeatures);
85 lcl_fillValues(aURLPatternNode,"MetaData",_rInstalledDriver.aMetaData);
90 DriversConfigImpl::DriversConfigImpl()
94 void DriversConfigImpl::Load(const uno::Reference< uno::XComponentContext >& _rxORB) const
96 if ( m_aDrivers.empty() )
98 if ( !m_aInstalled.isValid() )
100 static const OUString s_sNodeName("org.openoffice.Office.DataAccess.Drivers/Installed"); ///Installed
101 m_aInstalled = ::utl::OConfigurationTreeRoot::createWithComponentContext(_rxORB, s_sNodeName, -1, ::utl::OConfigurationTreeRoot::CM_READONLY);
104 if ( m_aInstalled.isValid() )
106 SvtMiscOptions aMiscOptions;
108 const uno::Sequence< OUString > aURLPatterns = m_aInstalled.getNodeNames();
109 const OUString* pPatternIter = aURLPatterns.getConstArray();
110 const OUString* pPatternEnd = pPatternIter + aURLPatterns.getLength();
111 for (;pPatternIter != pPatternEnd ; ++pPatternIter)
113 TInstalledDriver aInstalledDriver;
114 lcl_readURLPatternNode(m_aInstalled,*pPatternIter,aInstalledDriver);
115 if ( !aInstalledDriver.sDriverFactory.isEmpty() &&
116 ( aMiscOptions.IsExperimentalMode() ||
117 !aInstalledDriver.sDriverFactory.equals("com.sun.star.comp.sdbc.firebird.Driver") ))
118 m_aDrivers.insert(TInstalledDrivers::value_type(*pPatternIter,aInstalledDriver));
120 } // if ( m_aInstalled.isValid() )
124 DriversConfig::DriversConfig(const uno::Reference< uno::XComponentContext >& _rxORB)
125 :m_xORB(_rxORB)
130 DriversConfig::~DriversConfig()
135 DriversConfig::DriversConfig( const DriversConfig& _rhs )
137 *this = _rhs;
141 DriversConfig& DriversConfig::operator=( const DriversConfig& _rhs )
143 if ( this != &_rhs )
145 m_aNode = _rhs.m_aNode;
147 return *this;
151 OUString DriversConfig::getDriverFactoryName(const OUString& _sURL) const
153 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
154 OUString sRet;
155 OUString sOldPattern;
156 TInstalledDrivers::const_iterator aIter = rDrivers.begin();
157 TInstalledDrivers::const_iterator aEnd = rDrivers.end();
158 for(;aIter != aEnd;++aIter)
160 WildCard aWildCard(aIter->first);
161 if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
163 sRet = aIter->second.sDriverFactory;
164 sOldPattern = aIter->first;
168 return sRet;
171 OUString DriversConfig::getDriverTypeDisplayName(const OUString& _sURL) const
173 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
174 OUString sRet;
175 OUString sOldPattern;
176 TInstalledDrivers::const_iterator aIter = rDrivers.begin();
177 TInstalledDrivers::const_iterator aEnd = rDrivers.end();
178 for(;aIter != aEnd;++aIter)
180 WildCard aWildCard(aIter->first);
181 if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
183 sRet = aIter->second.sDriverTypeDisplayName;
184 sOldPattern = aIter->first;
188 return sRet;
191 const ::comphelper::NamedValueCollection& DriversConfig::getProperties(const OUString& _sURL) const
193 return impl_get(_sURL,1);
196 const ::comphelper::NamedValueCollection& DriversConfig::getFeatures(const OUString& _sURL) const
198 return impl_get(_sURL,0);
201 const ::comphelper::NamedValueCollection& DriversConfig::getMetaData(const OUString& _sURL) const
203 return impl_get(_sURL,2);
206 const ::comphelper::NamedValueCollection& DriversConfig::impl_get(const OUString& _sURL,sal_Int32 _nProps) const
208 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
209 const ::comphelper::NamedValueCollection* pRet = NULL;
210 OUString sOldPattern;
211 TInstalledDrivers::const_iterator aIter = rDrivers.begin();
212 TInstalledDrivers::const_iterator aEnd = rDrivers.end();
213 for(;aIter != aEnd;++aIter)
215 WildCard aWildCard(aIter->first);
216 if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
218 switch(_nProps)
220 case 0:
221 pRet = &aIter->second.aFeatures;
222 break;
223 case 1:
224 pRet = &aIter->second.aProperties;
225 break;
226 case 2:
227 pRet = &aIter->second.aMetaData;
228 break;
230 sOldPattern = aIter->first;
232 } // for(;aIter != aEnd;++aIter)
233 if ( pRet == NULL )
235 static const ::comphelper::NamedValueCollection s_sEmpty;
236 pRet = &s_sEmpty;
238 return *pRet;
241 uno::Sequence< OUString > DriversConfig::getURLs() const
243 const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
244 uno::Sequence< OUString > aRet(rDrivers.size());
245 OUString* pIter = aRet.getArray();
246 TInstalledDrivers::const_iterator aIter = rDrivers.begin();
247 TInstalledDrivers::const_iterator aEnd = rDrivers.end();
248 for(;aIter != aEnd;++aIter,++pIter)
250 *pIter = aIter->first;
252 return aRet;
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */