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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cui.hxx"
26 #include "sdbcdriverenum.hxx"
27 #include <comphelper/stl_types.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/container/XEnumerationAccess.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 
33 //........................................................................
34 namespace offapp
35 {
36 //........................................................................
37 
38 	using namespace ::com::sun::star::uno;
39 	using namespace ::com::sun::star::lang;
40 	using namespace ::com::sun::star::container;
41 
42 	//====================================================================
43 	//= ODriverEnumerationImpl
44 	//====================================================================
45 	class ODriverEnumerationImpl
46 	{
47 	protected:
48 		::std::vector< ::rtl::OUString >	m_aImplNames;
49 
50 	public:
51 		ODriverEnumerationImpl();
52 
getDriverImplNames() const53 		const ::std::vector< ::rtl::OUString >& getDriverImplNames() const { return m_aImplNames; }
54 	};
55 
56 	//--------------------------------------------------------------------
ODriverEnumerationImpl()57 	ODriverEnumerationImpl::ODriverEnumerationImpl()
58 	{
59 		try
60 		{
61 			Reference< XMultiServiceFactory > xORB = ::comphelper::getProcessServiceFactory();
62 			Reference< XInterface > xDM = xORB->createInstance(::rtl::OUString::createFromAscii("com.sun.star.sdbc.DriverManager"));
63 			OSL_ENSURE(xDM.is(), "ODriverEnumerationImpl::ODriverEnumerationImpl: no access to the SDBC driver manager!");
64 
65 			Reference< XEnumerationAccess > xEnumAccess(xDM, UNO_QUERY);
66 			OSL_ENSURE(xEnumAccess.is() || !xDM.is(), "ODriverEnumerationImpl::ODriverEnumerationImpl: can't enumerate SDBC drivers (missing the interface)!");
67 			if (xEnumAccess.is())
68 			{
69 				Reference< XEnumeration > xEnumDrivers = xEnumAccess->createEnumeration();
70 				OSL_ENSURE(xEnumDrivers.is(), "ODriverEnumerationImpl::ODriverEnumerationImpl: invalid enumeration object!");
71 
72 				Reference< XServiceInfo > xDriverSI;
73 				while (xEnumDrivers->hasMoreElements())
74 				{
75 					xEnumDrivers->nextElement() >>= xDriverSI;
76 					OSL_ENSURE(xDriverSI.is(), "ODriverEnumerationImpl::ODriverEnumerationImpl: driver without service info!");
77 					if (xDriverSI.is())
78 						m_aImplNames.push_back(xDriverSI->getImplementationName());
79 				}
80 			}
81 		}
82 		catch(const Exception&)
83 		{
84 			OSL_ENSURE(sal_False, "ODriverEnumerationImpl::ODriverEnumerationImpl: caught an exception while enumerating the drivers!");
85 		}
86 	}
87 
88 	//====================================================================
89 	//= ODriverEnumeration
90 	//====================================================================
91 	//--------------------------------------------------------------------
ODriverEnumeration()92 	ODriverEnumeration::ODriverEnumeration() throw()
93 		:m_pImpl(new ODriverEnumerationImpl)
94 	{
95 	}
96 
97 	//--------------------------------------------------------------------
~ODriverEnumeration()98 	ODriverEnumeration::~ODriverEnumeration() throw()
99 	{
100 		delete m_pImpl;
101 	}
102 
103 	//--------------------------------------------------------------------
begin() const104 	ODriverEnumeration::const_iterator ODriverEnumeration::begin() const throw()
105 	{
106 		return m_pImpl->getDriverImplNames().begin();
107 	}
108 
109 	//--------------------------------------------------------------------
end() const110 	ODriverEnumeration::const_iterator ODriverEnumeration::end() const throw()
111 	{
112 		return m_pImpl->getDriverImplNames().end();
113 	}
114 //........................................................................
115 }	// namespace offapp
116 //........................................................................
117 
118 
119