1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <core_resource.hxx>
21 #include <datasourceconnector.hxx>
22 #include <osl/diagnose.h>
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/sdbc/SQLWarning.hpp>
29 #include <comphelper/namedvaluecollection.hxx>
30 #include <connectivity/dbexception.hxx>
31 #include <com/sun/star/sdbc/XDataSource.hpp>
32 #include <UITools.hxx>
34 #include <vcl/mnemonic.hxx>
35 #include <vcl/outdev.hxx>
36 #include <vcl/stdtext.hxx>
37 #include <vcl/weld.hxx>
38 #include <comphelper/diagnose_ex.hxx>
39 #include <cppuhelper/exc_hlp.hxx>
40 #include <strings.hrc>
41 #include <strings.hxx>
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::lang
;
48 using namespace ::com::sun::star::sdb
;
49 using namespace ::com::sun::star::sdbc
;
50 using namespace ::com::sun::star::task
;
51 using namespace ::com::sun::star::beans
;
52 using namespace ::com::sun::star::container
;
53 using namespace ::com::sun::star::frame
;
54 using namespace ::dbtools
;
56 // ODatasourceConnector
57 ODatasourceConnector::ODatasourceConnector(const Reference
< XComponentContext
>& _rxContext
, weld::Window
* _pMessageParent
)
58 :m_pErrorMessageParent(_pMessageParent
)
59 ,m_xContext(_rxContext
)
63 ODatasourceConnector::ODatasourceConnector( const Reference
< XComponentContext
>& _rxContext
, weld::Window
* _pMessageParent
,
64 OUString _sContextInformation
)
65 :m_pErrorMessageParent(_pMessageParent
)
66 ,m_xContext(_rxContext
)
67 ,m_sContextInformation(std::move( _sContextInformation
))
71 Reference
< XConnection
> ODatasourceConnector::connect( const OUString
& _rDataSourceName
,
72 ::dbtools::SQLExceptionInfo
* _pErrorInfo
) const
74 Reference
< XConnection
> xConnection
;
76 OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
80 // get the data source
81 Reference
< XDataSource
> xDatasource
=
82 getDataSourceByName( _rDataSourceName
, m_pErrorMessageParent
, m_xContext
, _pErrorInfo
);
84 if ( xDatasource
.is() )
85 xConnection
= connect( xDatasource
, _pErrorInfo
);
89 Reference
< XConnection
> ODatasourceConnector::connect(const Reference
< XDataSource
>& _xDataSource
,
90 ::dbtools::SQLExceptionInfo
* _pErrorInfo
) const
92 Reference
< XConnection
> xConnection
;
94 OSL_ENSURE( isValid() && _xDataSource
.is(), "ODatasourceConnector::connect: invalid object or argument!" );
95 if ( !isValid() || !_xDataSource
.is() )
99 OUString sPassword
, sUser
;
100 bool bPwdRequired
= false;
101 Reference
<XPropertySet
> xProp(_xDataSource
,UNO_QUERY
);
104 xProp
->getPropertyValue(PROPERTY_PASSWORD
) >>= sPassword
;
105 xProp
->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED
) >>= bPwdRequired
;
106 xProp
->getPropertyValue(PROPERTY_USER
) >>= sUser
;
110 DBG_UNHANDLED_EXCEPTION("dbaccess");
114 SQLExceptionInfo aInfo
;
117 if (bPwdRequired
&& sPassword
.isEmpty())
118 { // password required, but empty -> connect using an interaction handler
119 Reference
< XCompletedConnection
> xConnectionCompletion( _xDataSource
, UNO_QUERY_THROW
);
121 Reference
< XModel
> xModel( getDataSourceOrModel( _xDataSource
), UNO_QUERY_THROW
);
122 ::comphelper::NamedValueCollection
aArgs( xModel
->getArgs() );
123 Reference
< XInteractionHandler
> xHandler( aArgs
.getOrDefault( "InteractionHandler", Reference
< XInteractionHandler
>() ) );
125 if ( !xHandler
.is() )
127 // instantiate the default SDB interaction handler
128 xHandler
= InteractionHandler::createWithParent(m_xContext
, m_pErrorMessageParent
? m_pErrorMessageParent
->GetXWindow() : nullptr);
131 xConnection
= xConnectionCompletion
->connectWithCompletion(xHandler
);
135 xConnection
= _xDataSource
->getConnection(sUser
, sPassword
);
138 catch( const SQLException
& )
140 aInfo
= ::cppu::getCaughtException();
142 catch(const Exception
&)
144 DBG_UNHANDLED_EXCEPTION("dbaccess");
147 if ( !aInfo
.isValid() )
149 // there was no error during connecting, but perhaps a warning?
150 Reference
< XWarningsSupplier
> xConnectionWarnings( xConnection
, UNO_QUERY
);
151 if ( xConnectionWarnings
.is() )
155 Any
aWarnings( xConnectionWarnings
->getWarnings() );
156 if ( aWarnings
.hasValue() )
158 OUString
sMessage( DBA_RES( STR_WARNINGS_DURING_CONNECT
) );
159 sMessage
= sMessage
.replaceFirst( "$buttontext$", GetStandardText( StandardButtonType::More
) );
160 sMessage
= removeMnemonicFromString( sMessage
);
163 aContext
.Message
= sMessage
;
164 aContext
.NextException
= aWarnings
;
167 xConnectionWarnings
->clearWarnings();
169 catch( const Exception
& )
171 DBG_UNHANDLED_EXCEPTION("dbaccess");
177 if ( !m_sContextInformation
.isEmpty() )
180 aError
.Message
= m_sContextInformation
;
181 aError
.NextException
= aInfo
.get();
187 // was there an error?
188 if ( aInfo
.isValid() )
192 *_pErrorInfo
= aInfo
;
196 showError(aInfo
, m_pErrorMessageParent
? m_pErrorMessageParent
->GetXWindow() : nullptr, m_xContext
);
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */