1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "connectivity/DriversConfig.hxx"
28*cdf0e10cSrcweir #include <tools/wldcrd.hxx>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir using namespace connectivity;
31*cdf0e10cSrcweir using namespace utl;
32*cdf0e10cSrcweir using namespace ::com::sun::star;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir namespace
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir     void lcl_convert(const uno::Sequence< ::rtl::OUString >& _aSource,uno::Any& _rDest)
37*cdf0e10cSrcweir     {
38*cdf0e10cSrcweir         uno::Sequence<uno::Any> aRet(_aSource.getLength());
39*cdf0e10cSrcweir         uno::Any* pAny = aRet.getArray();
40*cdf0e10cSrcweir         const ::rtl::OUString* pIter = _aSource.getConstArray();
41*cdf0e10cSrcweir         const ::rtl::OUString* pEnd  = pIter + _aSource.getLength();
42*cdf0e10cSrcweir         for (;pIter != pEnd ; ++pIter,++pAny)
43*cdf0e10cSrcweir         {
44*cdf0e10cSrcweir             *pAny <<= *pIter;
45*cdf0e10cSrcweir         }
46*cdf0e10cSrcweir         _rDest <<= aRet;
47*cdf0e10cSrcweir     }
48*cdf0e10cSrcweir     void lcl_fillValues(const ::utl::OConfigurationNode& _aURLPatternNode,const ::rtl::OUString& _sNode,::comphelper::NamedValueCollection& _rValues)
49*cdf0e10cSrcweir     {
50*cdf0e10cSrcweir         const ::utl::OConfigurationNode aPropertiesNode = _aURLPatternNode.openNode(_sNode);
51*cdf0e10cSrcweir         if ( aPropertiesNode.isValid() )
52*cdf0e10cSrcweir 	    {
53*cdf0e10cSrcweir             uno::Sequence< ::rtl::OUString > aStringSeq;
54*cdf0e10cSrcweir             static const ::rtl::OUString s_sValue(RTL_CONSTASCII_USTRINGPARAM("/Value"));
55*cdf0e10cSrcweir             const uno::Sequence< ::rtl::OUString > aProperties = aPropertiesNode.getNodeNames();
56*cdf0e10cSrcweir             const ::rtl::OUString* pPropertiesIter = aProperties.getConstArray();
57*cdf0e10cSrcweir             const ::rtl::OUString* pPropertiesEnd  = pPropertiesIter + aProperties.getLength();
58*cdf0e10cSrcweir             for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter)
59*cdf0e10cSrcweir             {
60*cdf0e10cSrcweir                 uno::Any aValue = aPropertiesNode.getNodeValue(*pPropertiesIter + s_sValue);
61*cdf0e10cSrcweir                 if ( aValue >>= aStringSeq )
62*cdf0e10cSrcweir                 {
63*cdf0e10cSrcweir                     lcl_convert(aStringSeq,aValue);
64*cdf0e10cSrcweir                 }
65*cdf0e10cSrcweir                 _rValues.put(*pPropertiesIter,aValue);
66*cdf0e10cSrcweir             } // for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter,++pNamedIter)
67*cdf0e10cSrcweir         } // if ( aPropertiesNode.isValid() )
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir     void lcl_readURLPatternNode(const ::utl::OConfigurationTreeRoot& _aInstalled,const ::rtl::OUString& _sEntry,TInstalledDriver& _rInstalledDriver)
70*cdf0e10cSrcweir     {
71*cdf0e10cSrcweir         const ::utl::OConfigurationNode aURLPatternNode = _aInstalled.openNode(_sEntry);
72*cdf0e10cSrcweir 		if ( aURLPatternNode.isValid() )
73*cdf0e10cSrcweir 		{
74*cdf0e10cSrcweir             static const ::rtl::OUString s_sParentURLPattern(RTL_CONSTASCII_USTRINGPARAM("ParentURLPattern"));
75*cdf0e10cSrcweir             static const ::rtl::OUString s_sDriver(RTL_CONSTASCII_USTRINGPARAM("Driver"));
76*cdf0e10cSrcweir             static const ::rtl::OUString s_sDriverTypeDisplayName(RTL_CONSTASCII_USTRINGPARAM("DriverTypeDisplayName"));
77*cdf0e10cSrcweir             static const ::rtl::OUString s_sProperties(RTL_CONSTASCII_USTRINGPARAM("Properties"));
78*cdf0e10cSrcweir             static const ::rtl::OUString s_sFeatures(RTL_CONSTASCII_USTRINGPARAM("Features"));
79*cdf0e10cSrcweir             static const ::rtl::OUString s_sMetaData(RTL_CONSTASCII_USTRINGPARAM("MetaData"));
80*cdf0e10cSrcweir             ::rtl::OUString sParentURLPattern;
81*cdf0e10cSrcweir             aURLPatternNode.getNodeValue(s_sParentURLPattern) >>= sParentURLPattern;
82*cdf0e10cSrcweir             if ( sParentURLPattern.getLength() )
83*cdf0e10cSrcweir                 lcl_readURLPatternNode(_aInstalled,sParentURLPattern,_rInstalledDriver);
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir             ::rtl::OUString sDriverFactory;
86*cdf0e10cSrcweir             aURLPatternNode.getNodeValue(s_sDriver) >>= sDriverFactory;
87*cdf0e10cSrcweir             if ( sDriverFactory.getLength() )
88*cdf0e10cSrcweir                 _rInstalledDriver.sDriverFactory = sDriverFactory;
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir             ::rtl::OUString sDriverTypeDisplayName;
91*cdf0e10cSrcweir             aURLPatternNode.getNodeValue(s_sDriverTypeDisplayName) >>= sDriverTypeDisplayName;
92*cdf0e10cSrcweir             OSL_ENSURE(sDriverTypeDisplayName.getLength(),"No valid DriverTypeDisplayName property!");
93*cdf0e10cSrcweir             if ( sDriverTypeDisplayName.getLength() )
94*cdf0e10cSrcweir                 _rInstalledDriver.sDriverTypeDisplayName = sDriverTypeDisplayName;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir             lcl_fillValues(aURLPatternNode,s_sProperties,_rInstalledDriver.aProperties);
97*cdf0e10cSrcweir             lcl_fillValues(aURLPatternNode,s_sFeatures,_rInstalledDriver.aFeatures);
98*cdf0e10cSrcweir             lcl_fillValues(aURLPatternNode,s_sMetaData,_rInstalledDriver.aMetaData);
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir // -----------------------------------------------------------------------------
103*cdf0e10cSrcweir DriversConfigImpl::DriversConfigImpl()
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir // -----------------------------------------------------------------------------
107*cdf0e10cSrcweir void DriversConfigImpl::Load(const uno::Reference< lang::XMultiServiceFactory >& _rxORB) const
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir     if ( m_aDrivers.empty() )
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         if ( !m_aInstalled.isValid() )
112*cdf0e10cSrcweir         {
113*cdf0e10cSrcweir             static const ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.Office.DataAccess.Drivers/Installed")); ///Installed
114*cdf0e10cSrcweir             m_aInstalled = ::utl::OConfigurationTreeRoot::createWithServiceFactory(_rxORB, s_sNodeName, -1, ::utl::OConfigurationTreeRoot::CM_READONLY);
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	    if ( m_aInstalled.isValid() )
118*cdf0e10cSrcweir 	    {
119*cdf0e10cSrcweir             const uno::Sequence< ::rtl::OUString > aURLPatterns = m_aInstalled.getNodeNames();
120*cdf0e10cSrcweir 		    const ::rtl::OUString* pPatternIter = aURLPatterns.getConstArray();
121*cdf0e10cSrcweir 		    const ::rtl::OUString* pPatternEnd  = pPatternIter + aURLPatterns.getLength();
122*cdf0e10cSrcweir 		    for (;pPatternIter != pPatternEnd ; ++pPatternIter)
123*cdf0e10cSrcweir 		    {
124*cdf0e10cSrcweir                 TInstalledDriver aInstalledDriver;
125*cdf0e10cSrcweir                 lcl_readURLPatternNode(m_aInstalled,*pPatternIter,aInstalledDriver);
126*cdf0e10cSrcweir                 if ( aInstalledDriver.sDriverFactory.getLength() )
127*cdf0e10cSrcweir                     m_aDrivers.insert(TInstalledDrivers::value_type(*pPatternIter,aInstalledDriver));
128*cdf0e10cSrcweir 		    }
129*cdf0e10cSrcweir 	    } // if ( m_aInstalled.isValid() )
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir // -----------------------------------------------------------------------------
133*cdf0e10cSrcweir DriversConfig::DriversConfig(const uno::Reference< lang::XMultiServiceFactory >& _rxORB)
134*cdf0e10cSrcweir :m_xORB(_rxORB)
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir // -----------------------------------------------------------------------------
139*cdf0e10cSrcweir DriversConfig::~DriversConfig()
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir // -----------------------------------------------------------------------------
144*cdf0e10cSrcweir DriversConfig::DriversConfig( const DriversConfig& _rhs )
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir     *this = _rhs;
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir // -----------------------------------------------------------------------------
150*cdf0e10cSrcweir DriversConfig& DriversConfig::operator=( const DriversConfig& _rhs )
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir     if ( this != &_rhs )
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         m_aNode = _rhs.m_aNode;
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir     return *this;
157*cdf0e10cSrcweir }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir // -----------------------------------------------------------------------------
160*cdf0e10cSrcweir ::rtl::OUString DriversConfig::getDriverFactoryName(const ::rtl::OUString& _sURL) const
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
163*cdf0e10cSrcweir     ::rtl::OUString sRet;
164*cdf0e10cSrcweir     ::rtl::OUString sOldPattern;
165*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
166*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
167*cdf0e10cSrcweir     for(;aIter != aEnd;++aIter)
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         WildCard aWildCard(aIter->first);
170*cdf0e10cSrcweir         if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir             sRet = aIter->second.sDriverFactory;
173*cdf0e10cSrcweir             sOldPattern = aIter->first;
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     return sRet;
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir // -----------------------------------------------------------------------------
180*cdf0e10cSrcweir ::rtl::OUString DriversConfig::getDriverTypeDisplayName(const ::rtl::OUString& _sURL) const
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
183*cdf0e10cSrcweir     ::rtl::OUString sRet;
184*cdf0e10cSrcweir     ::rtl::OUString sOldPattern;
185*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
186*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
187*cdf0e10cSrcweir     for(;aIter != aEnd;++aIter)
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         WildCard aWildCard(aIter->first);
190*cdf0e10cSrcweir         if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             sRet = aIter->second.sDriverTypeDisplayName;
193*cdf0e10cSrcweir             sOldPattern = aIter->first;
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     return sRet;
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir // -----------------------------------------------------------------------------
200*cdf0e10cSrcweir const ::comphelper::NamedValueCollection& DriversConfig::getProperties(const ::rtl::OUString& _sURL) const
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir     return impl_get(_sURL,1);
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir // -----------------------------------------------------------------------------
205*cdf0e10cSrcweir const ::comphelper::NamedValueCollection& DriversConfig::getFeatures(const ::rtl::OUString& _sURL) const
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir     return impl_get(_sURL,0);
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir // -----------------------------------------------------------------------------
210*cdf0e10cSrcweir const ::comphelper::NamedValueCollection& DriversConfig::getMetaData(const ::rtl::OUString& _sURL) const
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     return impl_get(_sURL,2);
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir // -----------------------------------------------------------------------------
215*cdf0e10cSrcweir const ::comphelper::NamedValueCollection& DriversConfig::impl_get(const ::rtl::OUString& _sURL,sal_Int32 _nProps) const
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
218*cdf0e10cSrcweir     const ::comphelper::NamedValueCollection* pRet = NULL;
219*cdf0e10cSrcweir     ::rtl::OUString sOldPattern;
220*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
221*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
222*cdf0e10cSrcweir     for(;aIter != aEnd;++aIter)
223*cdf0e10cSrcweir     {
224*cdf0e10cSrcweir         WildCard aWildCard(aIter->first);
225*cdf0e10cSrcweir         if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
226*cdf0e10cSrcweir         {
227*cdf0e10cSrcweir             switch(_nProps)
228*cdf0e10cSrcweir             {
229*cdf0e10cSrcweir                 case 0:
230*cdf0e10cSrcweir                     pRet = &aIter->second.aFeatures;
231*cdf0e10cSrcweir                     break;
232*cdf0e10cSrcweir                 case 1:
233*cdf0e10cSrcweir                     pRet = &aIter->second.aProperties;
234*cdf0e10cSrcweir                     break;
235*cdf0e10cSrcweir                 case 2:
236*cdf0e10cSrcweir                     pRet = &aIter->second.aMetaData;
237*cdf0e10cSrcweir                     break;
238*cdf0e10cSrcweir             }
239*cdf0e10cSrcweir             sOldPattern = aIter->first;
240*cdf0e10cSrcweir         }
241*cdf0e10cSrcweir     } // for(;aIter != aEnd;++aIter)
242*cdf0e10cSrcweir     if ( pRet == NULL )
243*cdf0e10cSrcweir     {
244*cdf0e10cSrcweir         static const ::comphelper::NamedValueCollection s_sEmpty;
245*cdf0e10cSrcweir         pRet = &s_sEmpty;
246*cdf0e10cSrcweir     }
247*cdf0e10cSrcweir     return *pRet;
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir // -----------------------------------------------------------------------------
250*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > DriversConfig::getURLs() const
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
253*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aRet(rDrivers.size());
254*cdf0e10cSrcweir     ::rtl::OUString* pIter = aRet.getArray();
255*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
256*cdf0e10cSrcweir     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
257*cdf0e10cSrcweir     for(;aIter != aEnd;++aIter,++pIter)
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         *pIter = aIter->first;
260*cdf0e10cSrcweir     }
261*cdf0e10cSrcweir     return aRet;
262*cdf0e10cSrcweir }
263