1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _DBAUI_DBADMINIMPL_HXX_
29 #define _DBAUI_DBADMINIMPL_HXX_
30 
31 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #endif
34 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
35 #include <com/sun/star/container/XNameAccess.hpp>
36 #endif
37 #ifndef _COM_SUN_STAR_UNO_XNAMINGSERVICE_HPP_
38 #include <com/sun/star/uno/XNamingService.hpp>
39 #endif
40 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #endif
43 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #endif
46 #ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
47 #include <com/sun/star/sdbc/XConnection.hpp>
48 #endif
49 #ifndef _COM_SUN_STAR_SDBC_XDRIVER_HPP_
50 #include <com/sun/star/sdbc/XDriver.hpp>
51 #endif
52 #ifndef _COMPHELPER_STLTYPES_HXX_
53 #include <comphelper/stl_types.hxx>
54 #endif
55 #ifndef _DBAUI_DSNTYPES_HXX_
56 #include "dsntypes.hxx"
57 #endif
58 #ifndef _SFXITEMSET_HXX
59 #include <svl/itemset.hxx>
60 #endif
61 #ifndef _COM_SUN_STAR_FRAME_XMODEL_HPP_
62 #include <com/sun/star/frame/XModel.hpp>
63 #endif
64 #include <svl/poolitem.hxx>
65 
66 class Window;
67 //.........................................................................
68 namespace dbaui
69 {
70 //.........................................................................
71     class DataSourceInfoConverter
72     {
73         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory;
74     public:
75         DataSourceInfoConverter(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xFactory)
76             :m_xFactory(_xFactory)
77         {
78         }
79         void convert(const ::dbaccess::ODsnTypeCollection* _pCollection,const ::rtl::OUString& _sOldURLPrefix,const ::rtl::OUString& _sNewURLPrefix,const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _xDatasource);
80     };
81 	class IItemSetHelper;
82 	//========================================================================
83 	//= ODbDataSourceAdministrationHelper
84 	//========================================================================
85 	class ODbDataSourceAdministrationHelper
86 	{
87 	public:
88 		DECLARE_STL_MAP(sal_Int32, ::rtl::OUString, ::std::less< sal_Int32 >, MapInt2String);
89 
90 	private:
91 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
92 							m_xORB;					/// service factory
93 		::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >
94 								m_xDatabaseContext;		/// database context we're working in
95 		::com::sun::star::uno::Reference< ::com::sun::star::uno::XNamingService >
96 								m_xDynamicContext;		/// just another interface of the context ...
97 		::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >   m_xDatasource;
98         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >         m_xModel;
99 
100 		::com::sun::star::uno::Any				m_aDataSourceOrName;
101 		typedef	::std::set< ::rtl::OUString >	StringSet;
102 		typedef	StringSet::const_iterator		ConstStringSetIterator;
103 
104 
105 		MapInt2String			m_aDirectPropTranslator;	/// translating property id's into names (direct properties of a data source)
106 		MapInt2String			m_aIndirectPropTranslator;	/// translating property id's into names (indirect properties of a data source)
107 		Window*					m_pParent;
108 		IItemSetHelper*			m_pItemSetHelper;
109 	public:
110 
111 		ODbDataSourceAdministrationHelper(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xORB
112 										,Window* _pParent
113 										,IItemSetHelper* _pItemSetHelper);
114 
115 		/** translate the current dialog SfxItems into driver relevant PropertyValues
116 			@see successfullyConnected
117 		*/
118 		sal_Bool	getCurrentSettings(::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rDriverParams);
119 
120 		/** to be called if the settings got from getCurrentSettings have been used for successfully connecting
121 			@see getCurrentSettings
122 		*/
123 		void		successfullyConnected();
124 
125 		/// clear the password in the current data source's item set
126 		void		clearPassword();
127 
128 		inline ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > getORB() const { return m_xORB; }
129 
130 		::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > getDatabaseContext() const { return m_xDatabaseContext; }
131 		::com::sun::star::uno::Reference< ::com::sun::star::uno::XNamingService > getDynamicContext() const { return m_xDynamicContext; }
132 
133 		/** creates a new connection. The caller is responsible to dispose it !!!!
134 		*/
135 		::std::pair< ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >,sal_Bool>		createConnection();
136 
137 		/** return the corresponding driver for the selected URL
138 		*/
139 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver >			getDriver();
140         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDriver >			getDriver(const ::rtl::OUString& _sURL);
141 
142 		/** returns the data source the dialog is currently working with
143 		*/
144 		::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >	getCurrentDataSource();
145         // returns the Url of a database document
146         String              getDocumentUrl(SfxItemSet& _rDest);
147 
148 		void setDataSourceOrName( const ::com::sun::star::uno::Any& _rDataSourceOrName );
149 
150 		/** extracts the connection type from the given set<p/>
151 			The connection type is determined by the value of the DSN item, analyzed by the TypeCollection item.
152 		*/
153 		static ::rtl::OUString getDatasourceType( const SfxItemSet& _rSet );
154 
155 		/** returns the connection URL
156 			@return
157 				The connection URL
158 		*/
159 		String getConnectionURL() const;
160 
161 		/// fill the nescessary information from the url line
162 		void convertUrl(SfxItemSet& _rDest);
163 
164 		const MapInt2String& getIndirectProperties() const { return m_aIndirectPropTranslator; }
165 
166 		/** translates properties of an UNO data source into SfxItems
167 			@param	_rxSource
168 				The data source
169 			@param	_rDest
170 				The item set to fill.
171 		*/
172 		void translateProperties(
173 				const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxSource,
174 				SfxItemSet& _rDest);
175 
176 		/** translate SfxItems into properties of an UNO data source
177 			@param	_rSource
178 				The item set to read from.
179 			@param	_rxDest
180 				The data source to fill.
181 		*/
182 		void translateProperties(
183 				const SfxItemSet& _rSource,
184 				const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxDest);
185 
186 		sal_Bool saveChanges(const SfxItemSet& _rSource);
187 	protected:
188 		/** fill a data source info array with the settings from a given item set
189 		*/
190 		void fillDatasourceInfo(const SfxItemSet& _rSource, ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rInfo);
191 
192 		/// translate the given value into an SfxPoolItem, put this into the given set under the given id
193 		void		implTranslateProperty(SfxItemSet& _rSet, sal_Int32  _nId, const ::com::sun::star::uno::Any& _rValue);
194 
195 		/// translate the given SfxPoolItem into an <type scope="com.sun.star.Any">uno</type>
196 		::com::sun::star::uno::Any implTranslateProperty(const SfxPoolItem* _pItem);
197 
198 		/// translate the given SfxPoolItem into an <type scope="com.sun.star.Any">uno</type>, set it (under the given name) on the given property set
199 		void		implTranslateProperty(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxSet, const ::rtl::OUString& _rName, const SfxPoolItem* _pItem);
200 
201 		/** check if the data source described by the given set needs authentication<p/>
202 			The return value depends on the data source type only.
203 		*/
204 		sal_Bool			hasAuthentication(const SfxItemSet& _rSet) const;
205 
206 #ifdef DBG_UTIL
207 		::rtl::OString translatePropertyId( sal_Int32 _nId );
208 #endif
209 	};
210 
211 //.........................................................................
212 }	// namespace dbaui
213 //.........................................................................
214 
215 #endif // _DBAUI_DBADMINIMPL_HXX_
216 
217