Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / datasourceconnector.cxx
blob8ed65da4591864976c6e7112a66512b0f2928409
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "datasourceconnector.hxx"
21 #include <osl/diagnose.h>
22 #include "dbustrings.hrc"
23 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/sdb/XCompletedConnection.hpp>
26 #include <com/sun/star/task/InteractionHandler.hpp>
27 #include <com/sun/star/frame/XModel.hpp>
28 #include <com/sun/star/sdb/SQLContext.hpp>
29 #include <com/sun/star/sdbc/SQLWarning.hpp>
30 #include <osl/thread.h>
31 #include <comphelper/processfactory.hxx>
32 #include <comphelper/extract.hxx>
33 #include <comphelper/namedvaluecollection.hxx>
34 #include <connectivity/dbexception.hxx>
35 #include <com/sun/star/sdbc/XDataSource.hpp>
36 #include "UITools.hxx"
37 #include <vcl/stdtext.hxx>
38 #include <vcl/button.hxx>
39 #include <svl/filenotation.hxx>
40 #include <tools/diagnose_ex.h>
41 #include <cppuhelper/exc_hlp.hxx>
42 #include "dbu_misc.hrc"
43 #include "moduledbu.hxx"
45 namespace dbaui
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::sdb;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::task;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::container;
55 using namespace ::com::sun::star::frame;
56 using namespace ::dbtools;
57 using ::svt::OFileNotation;
59 // ODatasourceConnector
60 ODatasourceConnector::ODatasourceConnector(const Reference< XComponentContext >& _rxContext, vcl::Window* _pMessageParent)
61 :m_pErrorMessageParent(_pMessageParent)
62 ,m_xContext(_rxContext)
66 ODatasourceConnector::ODatasourceConnector( const Reference< XComponentContext >& _rxContext, vcl::Window* _pMessageParent,
67 const OUString& _rContextInformation )
68 :m_pErrorMessageParent(_pMessageParent)
69 ,m_xContext(_rxContext)
70 ,m_sContextInformation( _rContextInformation )
74 Reference< XConnection > ODatasourceConnector::connect( const OUString& _rDataSourceName,
75 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
77 Reference< XConnection > xConnection;
79 OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
80 if (!isValid())
81 return xConnection;
83 // get the data source
84 Reference< XDataSource > xDatasource(
85 getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xContext, _pErrorInfo ),
86 UNO_QUERY
89 if ( xDatasource.is() )
90 xConnection = connect( xDatasource, _pErrorInfo );
91 return xConnection;
94 Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource,
95 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
97 Reference< XConnection > xConnection;
99 OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" );
100 if ( !isValid() || !_xDataSource.is() )
101 return xConnection;
103 // get user/password
104 OUString sPassword, sUser;
105 bool bPwdRequired = false;
106 Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY);
109 xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword;
110 xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired;
111 xProp->getPropertyValue(PROPERTY_USER) >>= sUser;
113 catch(Exception&)
115 DBG_UNHANDLED_EXCEPTION();
118 // try to connect
119 SQLExceptionInfo aInfo;
122 if (bPwdRequired && sPassword.isEmpty())
123 { // password required, but empty -> connect using an interaction handler
124 Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW );
126 Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW );
127 ::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
128 Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) );
130 if ( !xHandler.is() )
132 // instantiate the default SDB interaction handler
133 xHandler = Reference< XInteractionHandler >( InteractionHandler::createWithParent(m_xContext, 0), UNO_QUERY );
136 xConnection = xConnectionCompletion->connectWithCompletion(xHandler);
138 else
140 xConnection = _xDataSource->getConnection(sUser, sPassword);
143 catch( const SQLException& )
145 aInfo = ::cppu::getCaughtException();
147 catch(const Exception&)
149 DBG_UNHANDLED_EXCEPTION();
152 if ( !aInfo.isValid() )
154 // there was no error during connecting, but perhaps a warning?
155 Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY );
156 if ( xConnectionWarnings.is() )
160 Any aWarnings( xConnectionWarnings->getWarnings() );
161 if ( aWarnings.hasValue() )
163 OUString sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) );
164 sMessage = sMessage.replaceFirst( "$buttontext$", Button::GetStandardText( StandardButtonType::More ) );
165 sMessage = OutputDevice::GetNonMnemonicString( sMessage );
167 SQLWarning aContext;
168 aContext.Message = sMessage;
169 aContext.NextException = aWarnings;
170 aInfo = aContext;
172 xConnectionWarnings->clearWarnings();
174 catch( const Exception& )
176 DBG_UNHANDLED_EXCEPTION();
180 else
182 if ( !m_sContextInformation.isEmpty() )
184 SQLException aError;
185 aError.Message = m_sContextInformation;
186 aError.NextException = aInfo.get();
188 aInfo = aError;
192 // was there an error?
193 if ( aInfo.isValid() )
195 if ( _pErrorInfo )
197 *_pErrorInfo = aInfo;
199 else
201 showError( aInfo, m_pErrorMessageParent, m_xContext );
204 return xConnection;
207 } // namespace dbaui
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */