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_dbaccess.hxx" 26 27 #ifndef _DBAUI_DATASOURCECONNECTOR_HXX_ 28 #include "datasourceconnector.hxx" 29 #endif 30 #ifndef _OSL_DIAGNOSE_H_ 31 #include <osl/diagnose.h> 32 #endif 33 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC 34 #include "dbustrings.hrc" 35 #endif 36 #ifndef _COM_SUN_STAR_SDBC_XWARNINGSSUPPLIER_HPP_ 37 #include <com/sun/star/sdbc/XWarningsSupplier.hpp> 38 #endif 39 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_ 40 #include <com/sun/star/beans/XPropertySet.hpp> 41 #endif 42 #ifndef _COM_SUN_STAR_SDB_XCOMPLETEDCONNECTION_HPP_ 43 #include <com/sun/star/sdb/XCompletedConnection.hpp> 44 #endif 45 #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_ 46 #include <com/sun/star/task/XInteractionHandler.hpp> 47 #endif 48 #ifndef _COM_SUN_STAR_FRAME_XMODEL_HPP_ 49 #include <com/sun/star/frame/XModel.hpp> 50 #endif 51 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_ 52 #include <com/sun/star/sdb/SQLContext.hpp> 53 #endif 54 #ifndef _COM_SUN_STAR_SDBC_SQLWARNING_HPP_ 55 #include <com/sun/star/sdbc/SQLWarning.hpp> 56 #endif 57 #ifndef _OSL_THREAD_H_ 58 #include <osl/thread.h> 59 #endif 60 #ifndef _COMPHELPER_EXTRACT_HXX_ 61 #include <comphelper/extract.hxx> 62 #endif 63 #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX 64 #include <comphelper/namedvaluecollection.hxx> 65 #endif 66 #ifndef _DBHELPER_DBEXCEPTION_HXX_ 67 #include <connectivity/dbexception.hxx> 68 #endif 69 #ifndef _COM_SUN_STAR_SDBC_XDATASOURCE_HPP_ 70 #include <com/sun/star/sdbc/XDataSource.hpp> 71 #endif 72 #ifndef DBAUI_TOOLS_HXX 73 #include "UITools.hxx" 74 #endif 75 #ifndef _VCL_STDTEXT_HXX 76 #include <vcl/stdtext.hxx> 77 #endif 78 #ifndef _SV_BUTTON_HXX 79 #include <vcl/button.hxx> 80 #endif 81 #ifndef SVTOOLS_FILENOTATION_HXX 82 #include <svl/filenotation.hxx> 83 #endif 84 #ifndef TOOLS_DIAGNOSE_EX_H 85 #include <tools/diagnose_ex.h> 86 #endif 87 #ifndef _CPPUHELPER_EXC_HLP_HXX_ 88 #include <cppuhelper/exc_hlp.hxx> 89 #endif 90 #ifndef _DBU_MISC_HRC_ 91 #include "dbu_misc.hrc" 92 #endif 93 #include "moduledbu.hxx" 94 95 //......................................................................... 96 namespace dbaui 97 { 98 //......................................................................... 99 100 using namespace ::com::sun::star::uno; 101 using namespace ::com::sun::star::lang; 102 using namespace ::com::sun::star::sdb; 103 using namespace ::com::sun::star::sdbc; 104 using namespace ::com::sun::star::task; 105 using namespace ::com::sun::star::beans; 106 using namespace ::com::sun::star::container; 107 using namespace ::com::sun::star::frame; 108 using namespace ::dbtools; 109 using ::svt::OFileNotation; 110 111 //===================================================================== 112 //= ODatasourceConnector 113 //===================================================================== 114 //--------------------------------------------------------------------- ODatasourceConnector(const Reference<XMultiServiceFactory> & _rxORB,Window * _pMessageParent)115 ODatasourceConnector::ODatasourceConnector(const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent) 116 :m_pErrorMessageParent(_pMessageParent) 117 ,m_xORB(_rxORB) 118 { 119 } 120 121 //--------------------------------------------------------------------- ODatasourceConnector(const Reference<XMultiServiceFactory> & _rxORB,Window * _pMessageParent,const::rtl::OUString & _rContextInformation)122 ODatasourceConnector::ODatasourceConnector( const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent, 123 const ::rtl::OUString& _rContextInformation ) 124 :m_pErrorMessageParent(_pMessageParent) 125 ,m_xORB(_rxORB) 126 ,m_sContextInformation( _rContextInformation ) 127 { 128 } 129 130 //--------------------------------------------------------------------- connect(const::rtl::OUString & _rDataSourceName,::dbtools::SQLExceptionInfo * _pErrorInfo) const131 Reference< XConnection > ODatasourceConnector::connect( const ::rtl::OUString& _rDataSourceName, 132 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const 133 { 134 Reference< XConnection > xConnection; 135 136 OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!"); 137 if (!isValid()) 138 return xConnection; 139 140 // get the data source 141 Reference< XDataSource > xDatasource( 142 getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xORB, _pErrorInfo ), 143 UNO_QUERY 144 ); 145 146 if ( xDatasource.is() ) 147 xConnection = connect( xDatasource, _pErrorInfo ); 148 return xConnection; 149 } 150 151 //--------------------------------------------------------------------- connect(const Reference<XDataSource> & _xDataSource,::dbtools::SQLExceptionInfo * _pErrorInfo) const152 Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource, 153 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const 154 { 155 Reference< XConnection > xConnection; 156 157 OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" ); 158 if ( !isValid() || !_xDataSource.is() ) 159 return xConnection; 160 161 // get user/password 162 ::rtl::OUString sPassword, sUser; 163 sal_Bool bPwdRequired = sal_False; 164 Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY); 165 try 166 { 167 xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword; 168 xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired; 169 xProp->getPropertyValue(PROPERTY_USER) >>= sUser; 170 } 171 catch(Exception&) 172 { 173 DBG_UNHANDLED_EXCEPTION(); 174 } 175 176 // try to connect 177 SQLExceptionInfo aInfo; 178 try 179 { 180 if (bPwdRequired && !sPassword.getLength()) 181 { // password required, but empty -> connect using an interaction handler 182 Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW ); 183 184 Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW ); 185 ::comphelper::NamedValueCollection aArgs( xModel->getArgs() ); 186 Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) ); 187 188 if ( !xHandler.is() ) 189 { 190 // instantiate the default SDB interaction handler 191 xHandler = Reference< XInteractionHandler >( m_xORB->createInstance( SERVICE_TASK_INTERACTION_HANDLER ), UNO_QUERY ); 192 if ( !xHandler.is() ) 193 ShowServiceNotAvailableError(m_pErrorMessageParent, (::rtl::OUString)SERVICE_TASK_INTERACTION_HANDLER, sal_True); 194 } 195 196 if ( xHandler.is() ) 197 { 198 xConnection = xConnectionCompletion->connectWithCompletion(xHandler); 199 } 200 } 201 else 202 { 203 xConnection = _xDataSource->getConnection(sUser, sPassword); 204 } 205 } 206 catch( const SQLException& ) 207 { 208 aInfo = ::cppu::getCaughtException(); 209 } 210 catch(const Exception&) 211 { 212 DBG_UNHANDLED_EXCEPTION(); 213 } 214 215 if ( !aInfo.isValid() ) 216 { 217 // there was no error during connecting, but perhaps a warning? 218 Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY ); 219 if ( xConnectionWarnings.is() ) 220 { 221 try 222 { 223 Any aWarnings( xConnectionWarnings->getWarnings() ); 224 if ( aWarnings.hasValue() ) 225 { 226 String sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) ); 227 sMessage.SearchAndReplaceAscii( "$buttontext$", Button::GetStandardText( BUTTON_MORE ) ); 228 sMessage = OutputDevice::GetNonMnemonicString( sMessage ); 229 230 SQLWarning aContext; 231 aContext.Message = sMessage; 232 aContext.NextException = aWarnings; 233 aInfo = aContext; 234 } 235 xConnectionWarnings->clearWarnings(); 236 } 237 catch( const Exception& ) 238 { 239 DBG_UNHANDLED_EXCEPTION(); 240 } 241 } 242 } 243 else 244 { 245 if ( m_sContextInformation.getLength() ) 246 { 247 SQLException aError; 248 aError.Message = m_sContextInformation; 249 aError.NextException = aInfo.get(); 250 251 aInfo = aError; 252 } 253 } 254 255 // was there an error? 256 if ( aInfo.isValid() ) 257 { 258 if ( _pErrorInfo ) 259 { 260 *_pErrorInfo = aInfo; 261 } 262 else 263 { 264 showError( aInfo, m_pErrorMessageParent, m_xORB ); 265 } 266 } 267 return xConnection; 268 } 269 270 //......................................................................... 271 } // namespace dbaui 272 //......................................................................... 273 274