196de5490SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 396de5490SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 496de5490SAndrew Rist * or more contributor license agreements. See the NOTICE file 596de5490SAndrew Rist * distributed with this work for additional information 696de5490SAndrew Rist * regarding copyright ownership. The ASF licenses this file 796de5490SAndrew Rist * to you under the Apache License, Version 2.0 (the 896de5490SAndrew Rist * "License"); you may not use this file except in compliance 996de5490SAndrew Rist * with the License. You may obtain a copy of the License at 1096de5490SAndrew Rist * 1196de5490SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 1296de5490SAndrew Rist * 1396de5490SAndrew Rist * Unless required by applicable law or agreed to in writing, 1496de5490SAndrew Rist * software distributed under the License is distributed on an 1596de5490SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1696de5490SAndrew Rist * KIND, either express or implied. See the License for the 1796de5490SAndrew Rist * specific language governing permissions and limitations 1896de5490SAndrew Rist * under the License. 1996de5490SAndrew Rist * 2096de5490SAndrew Rist *************************************************************/ 2196de5490SAndrew Rist 2296de5490SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25*b63233d8Sdamjan #include "precompiled_dbui.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #ifndef _DBAUI_DATASOURCECONNECTOR_HXX_ 28cdf0e10cSrcweir #include "datasourceconnector.hxx" 29cdf0e10cSrcweir #endif 30cdf0e10cSrcweir #ifndef _OSL_DIAGNOSE_H_ 31cdf0e10cSrcweir #include <osl/diagnose.h> 32cdf0e10cSrcweir #endif 33cdf0e10cSrcweir #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC 34cdf0e10cSrcweir #include "dbustrings.hrc" 35cdf0e10cSrcweir #endif 36cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XWARNINGSSUPPLIER_HPP_ 37cdf0e10cSrcweir #include <com/sun/star/sdbc/XWarningsSupplier.hpp> 38cdf0e10cSrcweir #endif 39cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_ 40cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 41cdf0e10cSrcweir #endif 42cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XCOMPLETEDCONNECTION_HPP_ 43cdf0e10cSrcweir #include <com/sun/star/sdb/XCompletedConnection.hpp> 44cdf0e10cSrcweir #endif 45cdf0e10cSrcweir #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_ 46cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp> 47cdf0e10cSrcweir #endif 48cdf0e10cSrcweir #ifndef _COM_SUN_STAR_FRAME_XMODEL_HPP_ 49cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp> 50cdf0e10cSrcweir #endif 51cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_ 52cdf0e10cSrcweir #include <com/sun/star/sdb/SQLContext.hpp> 53cdf0e10cSrcweir #endif 54cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_SQLWARNING_HPP_ 55cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLWarning.hpp> 56cdf0e10cSrcweir #endif 57cdf0e10cSrcweir #ifndef _OSL_THREAD_H_ 58cdf0e10cSrcweir #include <osl/thread.h> 59cdf0e10cSrcweir #endif 60cdf0e10cSrcweir #ifndef _COMPHELPER_EXTRACT_HXX_ 61cdf0e10cSrcweir #include <comphelper/extract.hxx> 62cdf0e10cSrcweir #endif 63cdf0e10cSrcweir #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX 64cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx> 65cdf0e10cSrcweir #endif 66cdf0e10cSrcweir #ifndef _DBHELPER_DBEXCEPTION_HXX_ 67cdf0e10cSrcweir #include <connectivity/dbexception.hxx> 68cdf0e10cSrcweir #endif 69cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XDATASOURCE_HPP_ 70cdf0e10cSrcweir #include <com/sun/star/sdbc/XDataSource.hpp> 71cdf0e10cSrcweir #endif 72cdf0e10cSrcweir #ifndef DBAUI_TOOLS_HXX 73cdf0e10cSrcweir #include "UITools.hxx" 74cdf0e10cSrcweir #endif 75cdf0e10cSrcweir #ifndef _VCL_STDTEXT_HXX 76cdf0e10cSrcweir #include <vcl/stdtext.hxx> 77cdf0e10cSrcweir #endif 78cdf0e10cSrcweir #ifndef _SV_BUTTON_HXX 79cdf0e10cSrcweir #include <vcl/button.hxx> 80cdf0e10cSrcweir #endif 81cdf0e10cSrcweir #ifndef SVTOOLS_FILENOTATION_HXX 82cdf0e10cSrcweir #include <svl/filenotation.hxx> 83cdf0e10cSrcweir #endif 84cdf0e10cSrcweir #ifndef TOOLS_DIAGNOSE_EX_H 85cdf0e10cSrcweir #include <tools/diagnose_ex.h> 86cdf0e10cSrcweir #endif 87cdf0e10cSrcweir #ifndef _CPPUHELPER_EXC_HLP_HXX_ 88cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx> 89cdf0e10cSrcweir #endif 90cdf0e10cSrcweir #ifndef _DBU_MISC_HRC_ 91cdf0e10cSrcweir #include "dbu_misc.hrc" 92cdf0e10cSrcweir #endif 93cdf0e10cSrcweir #include "moduledbu.hxx" 94cdf0e10cSrcweir 95cdf0e10cSrcweir //......................................................................... 96cdf0e10cSrcweir namespace dbaui 97cdf0e10cSrcweir { 98cdf0e10cSrcweir //......................................................................... 99cdf0e10cSrcweir 100cdf0e10cSrcweir using namespace ::com::sun::star::uno; 101cdf0e10cSrcweir using namespace ::com::sun::star::lang; 102cdf0e10cSrcweir using namespace ::com::sun::star::sdb; 103cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 104cdf0e10cSrcweir using namespace ::com::sun::star::task; 105cdf0e10cSrcweir using namespace ::com::sun::star::beans; 106cdf0e10cSrcweir using namespace ::com::sun::star::container; 107cdf0e10cSrcweir using namespace ::com::sun::star::frame; 108cdf0e10cSrcweir using namespace ::dbtools; 109cdf0e10cSrcweir using ::svt::OFileNotation; 110cdf0e10cSrcweir 111cdf0e10cSrcweir //===================================================================== 112cdf0e10cSrcweir //= ODatasourceConnector 113cdf0e10cSrcweir //===================================================================== 114cdf0e10cSrcweir //--------------------------------------------------------------------- ODatasourceConnector(const Reference<XMultiServiceFactory> & _rxORB,Window * _pMessageParent)115cdf0e10cSrcweir ODatasourceConnector::ODatasourceConnector(const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent) 116cdf0e10cSrcweir :m_pErrorMessageParent(_pMessageParent) 117cdf0e10cSrcweir ,m_xORB(_rxORB) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir //--------------------------------------------------------------------- ODatasourceConnector(const Reference<XMultiServiceFactory> & _rxORB,Window * _pMessageParent,const::rtl::OUString & _rContextInformation)122cdf0e10cSrcweir ODatasourceConnector::ODatasourceConnector( const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent, 123cdf0e10cSrcweir const ::rtl::OUString& _rContextInformation ) 124cdf0e10cSrcweir :m_pErrorMessageParent(_pMessageParent) 125cdf0e10cSrcweir ,m_xORB(_rxORB) 126cdf0e10cSrcweir ,m_sContextInformation( _rContextInformation ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir //--------------------------------------------------------------------- connect(const::rtl::OUString & _rDataSourceName,::dbtools::SQLExceptionInfo * _pErrorInfo) const131cdf0e10cSrcweir Reference< XConnection > ODatasourceConnector::connect( const ::rtl::OUString& _rDataSourceName, 132cdf0e10cSrcweir ::dbtools::SQLExceptionInfo* _pErrorInfo ) const 133cdf0e10cSrcweir { 134cdf0e10cSrcweir Reference< XConnection > xConnection; 135cdf0e10cSrcweir 136cdf0e10cSrcweir OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!"); 137cdf0e10cSrcweir if (!isValid()) 138cdf0e10cSrcweir return xConnection; 139cdf0e10cSrcweir 140cdf0e10cSrcweir // get the data source 141cdf0e10cSrcweir Reference< XDataSource > xDatasource( 142cdf0e10cSrcweir getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xORB, _pErrorInfo ), 143cdf0e10cSrcweir UNO_QUERY 144cdf0e10cSrcweir ); 145cdf0e10cSrcweir 146cdf0e10cSrcweir if ( xDatasource.is() ) 147cdf0e10cSrcweir xConnection = connect( xDatasource, _pErrorInfo ); 148cdf0e10cSrcweir return xConnection; 149cdf0e10cSrcweir } 150cdf0e10cSrcweir 151cdf0e10cSrcweir //--------------------------------------------------------------------- connect(const Reference<XDataSource> & _xDataSource,::dbtools::SQLExceptionInfo * _pErrorInfo) const152cdf0e10cSrcweir Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource, 153cdf0e10cSrcweir ::dbtools::SQLExceptionInfo* _pErrorInfo ) const 154cdf0e10cSrcweir { 155cdf0e10cSrcweir Reference< XConnection > xConnection; 156cdf0e10cSrcweir 157cdf0e10cSrcweir OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" ); 158cdf0e10cSrcweir if ( !isValid() || !_xDataSource.is() ) 159cdf0e10cSrcweir return xConnection; 160cdf0e10cSrcweir 161cdf0e10cSrcweir // get user/password 162cdf0e10cSrcweir ::rtl::OUString sPassword, sUser; 163cdf0e10cSrcweir sal_Bool bPwdRequired = sal_False; 164cdf0e10cSrcweir Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY); 165cdf0e10cSrcweir try 166cdf0e10cSrcweir { 167cdf0e10cSrcweir xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword; 168cdf0e10cSrcweir xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired; 169cdf0e10cSrcweir xProp->getPropertyValue(PROPERTY_USER) >>= sUser; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir catch(Exception&) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir // try to connect 177cdf0e10cSrcweir SQLExceptionInfo aInfo; 178cdf0e10cSrcweir try 179cdf0e10cSrcweir { 180cdf0e10cSrcweir if (bPwdRequired && !sPassword.getLength()) 181cdf0e10cSrcweir { // password required, but empty -> connect using an interaction handler 182cdf0e10cSrcweir Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW ); 183cdf0e10cSrcweir 184cdf0e10cSrcweir Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW ); 185cdf0e10cSrcweir ::comphelper::NamedValueCollection aArgs( xModel->getArgs() ); 186cdf0e10cSrcweir Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) ); 187cdf0e10cSrcweir 188cdf0e10cSrcweir if ( !xHandler.is() ) 189cdf0e10cSrcweir { 190cdf0e10cSrcweir // instantiate the default SDB interaction handler 191cdf0e10cSrcweir xHandler = Reference< XInteractionHandler >( m_xORB->createInstance( SERVICE_TASK_INTERACTION_HANDLER ), UNO_QUERY ); 192cdf0e10cSrcweir if ( !xHandler.is() ) 193cdf0e10cSrcweir ShowServiceNotAvailableError(m_pErrorMessageParent, (::rtl::OUString)SERVICE_TASK_INTERACTION_HANDLER, sal_True); 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir if ( xHandler.is() ) 197cdf0e10cSrcweir { 198cdf0e10cSrcweir xConnection = xConnectionCompletion->connectWithCompletion(xHandler); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir } 201cdf0e10cSrcweir else 202cdf0e10cSrcweir { 203cdf0e10cSrcweir xConnection = _xDataSource->getConnection(sUser, sPassword); 204cdf0e10cSrcweir } 205cdf0e10cSrcweir } 206cdf0e10cSrcweir catch( const SQLException& ) 207cdf0e10cSrcweir { 208cdf0e10cSrcweir aInfo = ::cppu::getCaughtException(); 209cdf0e10cSrcweir } 210cdf0e10cSrcweir catch(const Exception&) 211cdf0e10cSrcweir { 212cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir 215cdf0e10cSrcweir if ( !aInfo.isValid() ) 216cdf0e10cSrcweir { 217cdf0e10cSrcweir // there was no error during connecting, but perhaps a warning? 218cdf0e10cSrcweir Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY ); 219cdf0e10cSrcweir if ( xConnectionWarnings.is() ) 220cdf0e10cSrcweir { 221cdf0e10cSrcweir try 222cdf0e10cSrcweir { 223cdf0e10cSrcweir Any aWarnings( xConnectionWarnings->getWarnings() ); 224cdf0e10cSrcweir if ( aWarnings.hasValue() ) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir String sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) ); 227cdf0e10cSrcweir sMessage.SearchAndReplaceAscii( "$buttontext$", Button::GetStandardText( BUTTON_MORE ) ); 228cdf0e10cSrcweir sMessage = OutputDevice::GetNonMnemonicString( sMessage ); 229cdf0e10cSrcweir 230cdf0e10cSrcweir SQLWarning aContext; 231cdf0e10cSrcweir aContext.Message = sMessage; 232cdf0e10cSrcweir aContext.NextException = aWarnings; 233cdf0e10cSrcweir aInfo = aContext; 234cdf0e10cSrcweir } 235cdf0e10cSrcweir xConnectionWarnings->clearWarnings(); 236cdf0e10cSrcweir } 237cdf0e10cSrcweir catch( const Exception& ) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 240cdf0e10cSrcweir } 241cdf0e10cSrcweir } 242cdf0e10cSrcweir } 243cdf0e10cSrcweir else 244cdf0e10cSrcweir { 245cdf0e10cSrcweir if ( m_sContextInformation.getLength() ) 246cdf0e10cSrcweir { 247cdf0e10cSrcweir SQLException aError; 248cdf0e10cSrcweir aError.Message = m_sContextInformation; 249cdf0e10cSrcweir aError.NextException = aInfo.get(); 250cdf0e10cSrcweir 251cdf0e10cSrcweir aInfo = aError; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir // was there an error? 256cdf0e10cSrcweir if ( aInfo.isValid() ) 257cdf0e10cSrcweir { 258cdf0e10cSrcweir if ( _pErrorInfo ) 259cdf0e10cSrcweir { 260cdf0e10cSrcweir *_pErrorInfo = aInfo; 261cdf0e10cSrcweir } 262cdf0e10cSrcweir else 263cdf0e10cSrcweir { 264cdf0e10cSrcweir showError( aInfo, m_pErrorMessageParent, m_xORB ); 265cdf0e10cSrcweir } 266cdf0e10cSrcweir } 267cdf0e10cSrcweir return xConnection; 268cdf0e10cSrcweir } 269cdf0e10cSrcweir 270cdf0e10cSrcweir //......................................................................... 271cdf0e10cSrcweir } // namespace dbaui 272cdf0e10cSrcweir //......................................................................... 273cdf0e10cSrcweir 274