Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / datasourceconnector.cxx
blob174e92c72d860abbcbd39217d982f296ad03a029
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;
58 // ODatasourceConnector
59 ODatasourceConnector::ODatasourceConnector(const Reference< XComponentContext >& _rxContext, vcl::Window* _pMessageParent)
60 :m_pErrorMessageParent(_pMessageParent)
61 ,m_xContext(_rxContext)
65 ODatasourceConnector::ODatasourceConnector( const Reference< XComponentContext >& _rxContext, vcl::Window* _pMessageParent,
66 const OUString& _rContextInformation )
67 :m_pErrorMessageParent(_pMessageParent)
68 ,m_xContext(_rxContext)
69 ,m_sContextInformation( _rContextInformation )
73 Reference< XConnection > ODatasourceConnector::connect( const OUString& _rDataSourceName,
74 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
76 Reference< XConnection > xConnection;
78 OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
79 if (!isValid())
80 return xConnection;
82 // get the data source
83 Reference< XDataSource > xDatasource(
84 getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xContext, _pErrorInfo ),
85 UNO_QUERY
88 if ( xDatasource.is() )
89 xConnection = connect( xDatasource, _pErrorInfo );
90 return xConnection;
93 Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource,
94 ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
96 Reference< XConnection > xConnection;
98 OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" );
99 if ( !isValid() || !_xDataSource.is() )
100 return xConnection;
102 // get user/password
103 OUString sPassword, sUser;
104 bool bPwdRequired = false;
105 Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY);
108 xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword;
109 xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired;
110 xProp->getPropertyValue(PROPERTY_USER) >>= sUser;
112 catch(Exception&)
114 DBG_UNHANDLED_EXCEPTION();
117 // try to connect
118 SQLExceptionInfo aInfo;
121 if (bPwdRequired && sPassword.isEmpty())
122 { // password required, but empty -> connect using an interaction handler
123 Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW );
125 Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW );
126 ::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
127 Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) );
129 if ( !xHandler.is() )
131 // instantiate the default SDB interaction handler
132 xHandler.set( InteractionHandler::createWithParent(m_xContext, nullptr), UNO_QUERY );
135 xConnection = xConnectionCompletion->connectWithCompletion(xHandler);
137 else
139 xConnection = _xDataSource->getConnection(sUser, sPassword);
142 catch( const SQLException& )
144 aInfo = ::cppu::getCaughtException();
146 catch(const Exception&)
148 DBG_UNHANDLED_EXCEPTION();
151 if ( !aInfo.isValid() )
153 // there was no error during connecting, but perhaps a warning?
154 Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY );
155 if ( xConnectionWarnings.is() )
159 Any aWarnings( xConnectionWarnings->getWarnings() );
160 if ( aWarnings.hasValue() )
162 OUString sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) );
163 sMessage = sMessage.replaceFirst( "$buttontext$", Button::GetStandardText( StandardButtonType::More ) );
164 sMessage = OutputDevice::GetNonMnemonicString( sMessage );
166 SQLWarning aContext;
167 aContext.Message = sMessage;
168 aContext.NextException = aWarnings;
169 aInfo = aContext;
171 xConnectionWarnings->clearWarnings();
173 catch( const Exception& )
175 DBG_UNHANDLED_EXCEPTION();
179 else
181 if ( !m_sContextInformation.isEmpty() )
183 SQLException aError;
184 aError.Message = m_sContextInformation;
185 aError.NextException = aInfo.get();
187 aInfo = aError;
191 // was there an error?
192 if ( aInfo.isValid() )
194 if ( _pErrorInfo )
196 *_pErrorInfo = aInfo;
198 else
200 showError( aInfo, m_pErrorMessageParent, m_xContext );
203 return xConnection;
206 } // namespace dbaui
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */