update dev300-m58
[ooovba.git] / dbaccess / source / ui / misc / datasourceconnector.cxx
blob3612bb0da76067bcc2dd231ce09b8157a07fa706
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: datasourceconnector.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _DBAUI_DATASOURCECONNECTOR_HXX_
35 #include "datasourceconnector.hxx"
36 #endif
37 #ifndef _OSL_DIAGNOSE_H_
38 #include <osl/diagnose.h>
39 #endif
40 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
41 #include "dbustrings.hrc"
42 #endif
43 #ifndef _COM_SUN_STAR_SDBC_XWARNINGSSUPPLIER_HPP_
44 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
45 #endif
46 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
47 #include <com/sun/star/beans/XPropertySet.hpp>
48 #endif
49 #ifndef _COM_SUN_STAR_SDB_XCOMPLETEDCONNECTION_HPP_
50 #include <com/sun/star/sdb/XCompletedConnection.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_
53 #include <com/sun/star/task/XInteractionHandler.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_FRAME_XMODEL_HPP_
56 #include <com/sun/star/frame/XModel.hpp>
57 #endif
58 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
59 #include <com/sun/star/sdb/SQLContext.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_SDBC_SQLWARNING_HPP_
62 #include <com/sun/star/sdbc/SQLWarning.hpp>
63 #endif
64 #ifndef _OSL_THREAD_H_
65 #include <osl/thread.h>
66 #endif
67 #ifndef _COMPHELPER_EXTRACT_HXX_
68 #include <comphelper/extract.hxx>
69 #endif
70 #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX
71 #include <comphelper/namedvaluecollection.hxx>
72 #endif
73 #ifndef _DBHELPER_DBEXCEPTION_HXX_
74 #include <connectivity/dbexception.hxx>
75 #endif
76 #ifndef _COM_SUN_STAR_SDBC_XDATASOURCE_HPP_
77 #include <com/sun/star/sdbc/XDataSource.hpp>
78 #endif
79 #ifndef DBAUI_TOOLS_HXX
80 #include "UITools.hxx"
81 #endif
82 #ifndef _VCL_STDTEXT_HXX
83 #include <vcl/stdtext.hxx>
84 #endif
85 #ifndef _SV_BUTTON_HXX
86 #include <vcl/button.hxx>
87 #endif
88 #ifndef SVTOOLS_FILENOTATION_HXX
89 #include <svtools/filenotation.hxx>
90 #endif
91 #ifndef TOOLS_DIAGNOSE_EX_H
92 #include <tools/diagnose_ex.h>
93 #endif
94 #ifndef _CPPUHELPER_EXC_HLP_HXX_
95 #include <cppuhelper/exc_hlp.hxx>
96 #endif
97 #ifndef _DBU_MISC_HRC_
98 #include "dbu_misc.hrc"
99 #endif
100 #include "moduledbu.hxx"
102 //.........................................................................
103 namespace dbaui
105 //.........................................................................
107 using namespace ::com::sun::star::uno;
108 using namespace ::com::sun::star::lang;
109 using namespace ::com::sun::star::sdb;
110 using namespace ::com::sun::star::sdbc;
111 using namespace ::com::sun::star::task;
112 using namespace ::com::sun::star::beans;
113 using namespace ::com::sun::star::container;
114 using namespace ::com::sun::star::frame;
115 using namespace ::dbtools;
116 using ::svt::OFileNotation;
118 //=====================================================================
119 //= ODatasourceConnector
120 //=====================================================================
121 //---------------------------------------------------------------------
122 ODatasourceConnector::ODatasourceConnector(const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent)
123 :m_pErrorMessageParent(_pMessageParent)
124 ,m_xORB(_rxORB)
128 //---------------------------------------------------------------------
129 ODatasourceConnector::ODatasourceConnector( const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent,
130 const ::rtl::OUString& _rContextInformation )
131 :m_pErrorMessageParent(_pMessageParent)
132 ,m_xORB(_rxORB)
133 ,m_sContextInformation( _rContextInformation )
137 //---------------------------------------------------------------------
138 Reference< XConnection > ODatasourceConnector::connect( const ::rtl::OUString& _rDataSourceName,
139 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
141 Reference< XConnection > xConnection;
143 OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
144 if (!isValid())
145 return xConnection;
147 // get the data source
148 Reference< XDataSource > xDatasource(
149 getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xORB, _pErrorInfo ),
150 UNO_QUERY
153 if ( xDatasource.is() )
154 xConnection = connect( xDatasource, _pErrorInfo );
155 return xConnection;
158 //---------------------------------------------------------------------
159 Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource,
160 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
162 Reference< XConnection > xConnection;
164 OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" );
165 if ( !isValid() || !_xDataSource.is() )
166 return xConnection;
168 // get user/password
169 ::rtl::OUString sPassword, sUser;
170 sal_Bool bPwdRequired = sal_False;
171 Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY);
174 xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword;
175 xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired;
176 xProp->getPropertyValue(PROPERTY_USER) >>= sUser;
178 catch(Exception&)
180 DBG_UNHANDLED_EXCEPTION();
183 // try to connect
184 SQLExceptionInfo aInfo;
187 if (bPwdRequired && !sPassword.getLength())
188 { // password required, but empty -> connect using an interaction handler
189 Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW );
191 Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW );
192 ::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
193 Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) );
195 if ( !xHandler.is() )
197 // instantiate the default SDB interaction handler
198 xHandler = Reference< XInteractionHandler >( m_xORB->createInstance( SERVICE_SDB_INTERACTION_HANDLER ), UNO_QUERY );
199 if ( !xHandler.is() )
200 ShowServiceNotAvailableError(m_pErrorMessageParent, String(SERVICE_SDB_INTERACTION_HANDLER), sal_True);
203 if ( xHandler.is() )
205 xConnection = xConnectionCompletion->connectWithCompletion(xHandler);
208 else
210 xConnection = _xDataSource->getConnection(sUser, sPassword);
213 catch( const SQLException& )
215 aInfo = ::cppu::getCaughtException();
217 catch(const Exception&)
219 DBG_UNHANDLED_EXCEPTION();
222 if ( !aInfo.isValid() )
224 // there was no error during connecting, but perhaps a warning?
225 Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY );
226 if ( xConnectionWarnings.is() )
230 Any aWarnings( xConnectionWarnings->getWarnings() );
231 if ( aWarnings.hasValue() )
233 String sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) );
234 sMessage.SearchAndReplaceAscii( "$buttontext$", Button::GetStandardText( BUTTON_MORE ) );
235 sMessage = OutputDevice::GetNonMnemonicString( sMessage );
237 SQLWarning aContext;
238 aContext.Message = sMessage;
239 aContext.NextException = aWarnings;
240 aInfo = aContext;
242 xConnectionWarnings->clearWarnings();
244 catch( const Exception& )
246 DBG_UNHANDLED_EXCEPTION();
250 else
252 if ( m_sContextInformation.getLength() )
254 SQLException aError;
255 aError.Message = m_sContextInformation;
256 aError.NextException = aInfo.get();
258 aInfo = aError;
262 // was there an error?
263 if ( aInfo.isValid() )
265 if ( _pErrorInfo )
267 *_pErrorInfo = aInfo;
269 else
271 showError( aInfo, m_pErrorMessageParent, m_xORB );
274 return xConnection;
277 //.........................................................................
278 } // namespace dbaui
279 //.........................................................................