Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / misc / datasourceconnector.cxx
blob7b87ccfd385c02b4b3df190cde4d2106a4d58e9d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "datasourceconnector.hxx"
31 #include <osl/diagnose.h>
32 #include "dbustrings.hrc"
33 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/sdb/XCompletedConnection.hpp>
36 #include <com/sun/star/task/XInteractionHandler.hpp>
37 #include <com/sun/star/frame/XModel.hpp>
38 #include <com/sun/star/sdb/SQLContext.hpp>
39 #include <com/sun/star/sdbc/SQLWarning.hpp>
40 #include <osl/thread.h>
41 #include <comphelper/extract.hxx>
42 #include <comphelper/namedvaluecollection.hxx>
43 #include <connectivity/dbexception.hxx>
44 #include <com/sun/star/sdbc/XDataSource.hpp>
45 #include "UITools.hxx"
46 #include <vcl/stdtext.hxx>
47 #include <vcl/button.hxx>
48 #include <svl/filenotation.hxx>
49 #include <tools/diagnose_ex.h>
50 #include <cppuhelper/exc_hlp.hxx>
51 #include "dbu_misc.hrc"
52 #include "moduledbu.hxx"
54 //.........................................................................
55 namespace dbaui
57 //.........................................................................
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::sdb;
62 using namespace ::com::sun::star::sdbc;
63 using namespace ::com::sun::star::task;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::container;
66 using namespace ::com::sun::star::frame;
67 using namespace ::dbtools;
68 using ::svt::OFileNotation;
70 //=====================================================================
71 //= ODatasourceConnector
72 //=====================================================================
73 //---------------------------------------------------------------------
74 ODatasourceConnector::ODatasourceConnector(const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent)
75 :m_pErrorMessageParent(_pMessageParent)
76 ,m_xORB(_rxORB)
80 //---------------------------------------------------------------------
81 ODatasourceConnector::ODatasourceConnector( const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent,
82 const ::rtl::OUString& _rContextInformation )
83 :m_pErrorMessageParent(_pMessageParent)
84 ,m_xORB(_rxORB)
85 ,m_sContextInformation( _rContextInformation )
89 //---------------------------------------------------------------------
90 Reference< XConnection > ODatasourceConnector::connect( const ::rtl::OUString& _rDataSourceName,
91 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
93 Reference< XConnection > xConnection;
95 OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
96 if (!isValid())
97 return xConnection;
99 // get the data source
100 Reference< XDataSource > xDatasource(
101 getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xORB, _pErrorInfo ),
102 UNO_QUERY
105 if ( xDatasource.is() )
106 xConnection = connect( xDatasource, _pErrorInfo );
107 return xConnection;
110 //---------------------------------------------------------------------
111 Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource,
112 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
114 Reference< XConnection > xConnection;
116 OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" );
117 if ( !isValid() || !_xDataSource.is() )
118 return xConnection;
120 // get user/password
121 ::rtl::OUString sPassword, sUser;
122 sal_Bool bPwdRequired = sal_False;
123 Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY);
126 xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword;
127 xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired;
128 xProp->getPropertyValue(PROPERTY_USER) >>= sUser;
130 catch(Exception&)
132 DBG_UNHANDLED_EXCEPTION();
135 // try to connect
136 SQLExceptionInfo aInfo;
139 if (bPwdRequired && sPassword.isEmpty())
140 { // password required, but empty -> connect using an interaction handler
141 Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW );
143 Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW );
144 ::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
145 Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) );
147 if ( !xHandler.is() )
149 // instantiate the default SDB interaction handler
150 xHandler = Reference< XInteractionHandler >( m_xORB->createInstance( SERVICE_TASK_INTERACTION_HANDLER ), UNO_QUERY );
151 if ( !xHandler.is() )
152 ShowServiceNotAvailableError(m_pErrorMessageParent, (::rtl::OUString)SERVICE_TASK_INTERACTION_HANDLER, sal_True);
155 if ( xHandler.is() )
157 xConnection = xConnectionCompletion->connectWithCompletion(xHandler);
160 else
162 xConnection = _xDataSource->getConnection(sUser, sPassword);
165 catch( const SQLException& )
167 aInfo = ::cppu::getCaughtException();
169 catch(const Exception&)
171 DBG_UNHANDLED_EXCEPTION();
174 if ( !aInfo.isValid() )
176 // there was no error during connecting, but perhaps a warning?
177 Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY );
178 if ( xConnectionWarnings.is() )
182 Any aWarnings( xConnectionWarnings->getWarnings() );
183 if ( aWarnings.hasValue() )
185 String sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) );
186 sMessage.SearchAndReplaceAscii( "$buttontext$", Button::GetStandardText( BUTTON_MORE ) );
187 sMessage = OutputDevice::GetNonMnemonicString( sMessage );
189 SQLWarning aContext;
190 aContext.Message = sMessage;
191 aContext.NextException = aWarnings;
192 aInfo = aContext;
194 xConnectionWarnings->clearWarnings();
196 catch( const Exception& )
198 DBG_UNHANDLED_EXCEPTION();
202 else
204 if ( !m_sContextInformation.isEmpty() )
206 SQLException aError;
207 aError.Message = m_sContextInformation;
208 aError.NextException = aInfo.get();
210 aInfo = aError;
214 // was there an error?
215 if ( aInfo.isValid() )
217 if ( _pErrorInfo )
219 *_pErrorInfo = aInfo;
221 else
223 showError( aInfo, m_pErrorMessageParent, m_xORB );
226 return xConnection;
229 //.........................................................................
230 } // namespace dbaui
231 //.........................................................................
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */