1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "connectivity/DriversConfig.hxx"
24 #include <tools/wldcrd.hxx>
25 
26 using namespace connectivity;
27 using namespace utl;
28 using namespace ::com::sun::star;
29 
30 namespace
31 {
lcl_convert(const uno::Sequence<::rtl::OUString> & _aSource,uno::Any & _rDest)32     void lcl_convert(const uno::Sequence< ::rtl::OUString >& _aSource,uno::Any& _rDest)
33     {
34         uno::Sequence<uno::Any> aRet(_aSource.getLength());
35         uno::Any* pAny = aRet.getArray();
36         const ::rtl::OUString* pIter = _aSource.getConstArray();
37         const ::rtl::OUString* pEnd  = pIter + _aSource.getLength();
38         for (;pIter != pEnd ; ++pIter,++pAny)
39         {
40             *pAny <<= *pIter;
41         }
42         _rDest <<= aRet;
43     }
lcl_fillValues(const::utl::OConfigurationNode & _aURLPatternNode,const::rtl::OUString & _sNode,::comphelper::NamedValueCollection & _rValues)44     void lcl_fillValues(const ::utl::OConfigurationNode& _aURLPatternNode,const ::rtl::OUString& _sNode,::comphelper::NamedValueCollection& _rValues)
45     {
46         const ::utl::OConfigurationNode aPropertiesNode = _aURLPatternNode.openNode(_sNode);
47         if ( aPropertiesNode.isValid() )
48 	    {
49             uno::Sequence< ::rtl::OUString > aStringSeq;
50             static const ::rtl::OUString s_sValue(RTL_CONSTASCII_USTRINGPARAM("/Value"));
51             const uno::Sequence< ::rtl::OUString > aProperties = aPropertiesNode.getNodeNames();
52             const ::rtl::OUString* pPropertiesIter = aProperties.getConstArray();
53             const ::rtl::OUString* pPropertiesEnd  = pPropertiesIter + aProperties.getLength();
54             for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter)
55             {
56                 uno::Any aValue = aPropertiesNode.getNodeValue(*pPropertiesIter + s_sValue);
57                 if ( aValue >>= aStringSeq )
58                 {
59                     lcl_convert(aStringSeq,aValue);
60                 }
61                 _rValues.put(*pPropertiesIter,aValue);
62             } // for (;pPropertiesIter != pPropertiesEnd ; ++pPropertiesIter,++pNamedIter)
63         } // if ( aPropertiesNode.isValid() )
64     }
lcl_readURLPatternNode(const::utl::OConfigurationTreeRoot & _aInstalled,const::rtl::OUString & _sEntry,TInstalledDriver & _rInstalledDriver)65     void lcl_readURLPatternNode(const ::utl::OConfigurationTreeRoot& _aInstalled,const ::rtl::OUString& _sEntry,TInstalledDriver& _rInstalledDriver)
66     {
67         const ::utl::OConfigurationNode aURLPatternNode = _aInstalled.openNode(_sEntry);
68 		if ( aURLPatternNode.isValid() )
69 		{
70             static const ::rtl::OUString s_sParentURLPattern(RTL_CONSTASCII_USTRINGPARAM("ParentURLPattern"));
71             static const ::rtl::OUString s_sDriver(RTL_CONSTASCII_USTRINGPARAM("Driver"));
72             static const ::rtl::OUString s_sDriverTypeDisplayName(RTL_CONSTASCII_USTRINGPARAM("DriverTypeDisplayName"));
73             static const ::rtl::OUString s_sProperties(RTL_CONSTASCII_USTRINGPARAM("Properties"));
74             static const ::rtl::OUString s_sFeatures(RTL_CONSTASCII_USTRINGPARAM("Features"));
75             static const ::rtl::OUString s_sMetaData(RTL_CONSTASCII_USTRINGPARAM("MetaData"));
76             ::rtl::OUString sParentURLPattern;
77             aURLPatternNode.getNodeValue(s_sParentURLPattern) >>= sParentURLPattern;
78             if ( sParentURLPattern.getLength() )
79                 lcl_readURLPatternNode(_aInstalled,sParentURLPattern,_rInstalledDriver);
80 
81             ::rtl::OUString sDriverFactory;
82             aURLPatternNode.getNodeValue(s_sDriver) >>= sDriverFactory;
83             if ( sDriverFactory.getLength() )
84                 _rInstalledDriver.sDriverFactory = sDriverFactory;
85 
86             ::rtl::OUString sDriverTypeDisplayName;
87             aURLPatternNode.getNodeValue(s_sDriverTypeDisplayName) >>= sDriverTypeDisplayName;
88             OSL_ENSURE(sDriverTypeDisplayName.getLength(),"No valid DriverTypeDisplayName property!");
89             if ( sDriverTypeDisplayName.getLength() )
90                 _rInstalledDriver.sDriverTypeDisplayName = sDriverTypeDisplayName;
91 
92             lcl_fillValues(aURLPatternNode,s_sProperties,_rInstalledDriver.aProperties);
93             lcl_fillValues(aURLPatternNode,s_sFeatures,_rInstalledDriver.aFeatures);
94             lcl_fillValues(aURLPatternNode,s_sMetaData,_rInstalledDriver.aMetaData);
95         }
96     }
97 }
98 // -----------------------------------------------------------------------------
DriversConfigImpl()99 DriversConfigImpl::DriversConfigImpl()
100 {
101 }
102 // -----------------------------------------------------------------------------
Load(const uno::Reference<lang::XMultiServiceFactory> & _rxORB) const103 void DriversConfigImpl::Load(const uno::Reference< lang::XMultiServiceFactory >& _rxORB) const
104 {
105     if ( m_aDrivers.empty() )
106     {
107         if ( !m_aInstalled.isValid() )
108         {
109             static const ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.Office.DataAccess.Drivers/Installed")); ///Installed
110             m_aInstalled = ::utl::OConfigurationTreeRoot::createWithServiceFactory(_rxORB, s_sNodeName, -1, ::utl::OConfigurationTreeRoot::CM_READONLY);
111         }
112 
113 	    if ( m_aInstalled.isValid() )
114 	    {
115             const uno::Sequence< ::rtl::OUString > aURLPatterns = m_aInstalled.getNodeNames();
116 		    const ::rtl::OUString* pPatternIter = aURLPatterns.getConstArray();
117 		    const ::rtl::OUString* pPatternEnd  = pPatternIter + aURLPatterns.getLength();
118 		    for (;pPatternIter != pPatternEnd ; ++pPatternIter)
119 		    {
120                 TInstalledDriver aInstalledDriver;
121                 lcl_readURLPatternNode(m_aInstalled,*pPatternIter,aInstalledDriver);
122                 if ( aInstalledDriver.sDriverFactory.getLength() )
123                     m_aDrivers.insert(TInstalledDrivers::value_type(*pPatternIter,aInstalledDriver));
124 		    }
125 	    } // if ( m_aInstalled.isValid() )
126     }
127 }
128 // -----------------------------------------------------------------------------
DriversConfig(const uno::Reference<lang::XMultiServiceFactory> & _rxORB)129 DriversConfig::DriversConfig(const uno::Reference< lang::XMultiServiceFactory >& _rxORB)
130 :m_xORB(_rxORB)
131 {
132 }
133 
134 // -----------------------------------------------------------------------------
~DriversConfig()135 DriversConfig::~DriversConfig()
136 {
137 }
138 
139 // -----------------------------------------------------------------------------
DriversConfig(const DriversConfig & _rhs)140 DriversConfig::DriversConfig( const DriversConfig& _rhs )
141 {
142     *this = _rhs;
143 }
144 
145 // -----------------------------------------------------------------------------
operator =(const DriversConfig & _rhs)146 DriversConfig& DriversConfig::operator=( const DriversConfig& _rhs )
147 {
148     if ( this != &_rhs )
149     {
150         m_aNode = _rhs.m_aNode;
151     }
152     return *this;
153 }
154 
155 // -----------------------------------------------------------------------------
getDriverFactoryName(const::rtl::OUString & _sURL) const156 ::rtl::OUString DriversConfig::getDriverFactoryName(const ::rtl::OUString& _sURL) const
157 {
158     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
159     ::rtl::OUString sRet;
160     ::rtl::OUString sOldPattern;
161     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
162     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
163     for(;aIter != aEnd;++aIter)
164     {
165         WildCard aWildCard(aIter->first);
166         if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
167         {
168             sRet = aIter->second.sDriverFactory;
169             sOldPattern = aIter->first;
170         }
171     }
172 
173     return sRet;
174 }
175 // -----------------------------------------------------------------------------
getDriverTypeDisplayName(const::rtl::OUString & _sURL) const176 ::rtl::OUString DriversConfig::getDriverTypeDisplayName(const ::rtl::OUString& _sURL) const
177 {
178     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
179     ::rtl::OUString sRet;
180     ::rtl::OUString sOldPattern;
181     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
182     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
183     for(;aIter != aEnd;++aIter)
184     {
185         WildCard aWildCard(aIter->first);
186         if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
187         {
188             sRet = aIter->second.sDriverTypeDisplayName;
189             sOldPattern = aIter->first;
190         }
191     }
192 
193     return sRet;
194 }
195 // -----------------------------------------------------------------------------
getProperties(const::rtl::OUString & _sURL) const196 const ::comphelper::NamedValueCollection& DriversConfig::getProperties(const ::rtl::OUString& _sURL) const
197 {
198     return impl_get(_sURL,1);
199 }
200 // -----------------------------------------------------------------------------
getFeatures(const::rtl::OUString & _sURL) const201 const ::comphelper::NamedValueCollection& DriversConfig::getFeatures(const ::rtl::OUString& _sURL) const
202 {
203     return impl_get(_sURL,0);
204 }
205 // -----------------------------------------------------------------------------
getMetaData(const::rtl::OUString & _sURL) const206 const ::comphelper::NamedValueCollection& DriversConfig::getMetaData(const ::rtl::OUString& _sURL) const
207 {
208     return impl_get(_sURL,2);
209 }
210 // -----------------------------------------------------------------------------
impl_get(const::rtl::OUString & _sURL,sal_Int32 _nProps) const211 const ::comphelper::NamedValueCollection& DriversConfig::impl_get(const ::rtl::OUString& _sURL,sal_Int32 _nProps) const
212 {
213     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
214     const ::comphelper::NamedValueCollection* pRet = NULL;
215     ::rtl::OUString sOldPattern;
216     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
217     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
218     for(;aIter != aEnd;++aIter)
219     {
220         WildCard aWildCard(aIter->first);
221         if ( sOldPattern.getLength() < aIter->first.getLength() && aWildCard.Matches(_sURL) )
222         {
223             switch(_nProps)
224             {
225                 case 0:
226                     pRet = &aIter->second.aFeatures;
227                     break;
228                 case 1:
229                     pRet = &aIter->second.aProperties;
230                     break;
231                 case 2:
232                     pRet = &aIter->second.aMetaData;
233                     break;
234             }
235             sOldPattern = aIter->first;
236         }
237     } // for(;aIter != aEnd;++aIter)
238     if ( pRet == NULL )
239     {
240         static const ::comphelper::NamedValueCollection s_sEmpty;
241         pRet = &s_sEmpty;
242     }
243     return *pRet;
244 }
245 // -----------------------------------------------------------------------------
getURLs() const246 uno::Sequence< ::rtl::OUString > DriversConfig::getURLs() const
247 {
248     const TInstalledDrivers& rDrivers = m_aNode->getInstalledDrivers(m_xORB);
249     uno::Sequence< ::rtl::OUString > aRet(rDrivers.size());
250     ::rtl::OUString* pIter = aRet.getArray();
251     TInstalledDrivers::const_iterator aIter = rDrivers.begin();
252     TInstalledDrivers::const_iterator aEnd = rDrivers.end();
253     for(;aIter != aEnd;++aIter,++pIter)
254     {
255         *pIter = aIter->first;
256     }
257     return aRet;
258 }
259