Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
44 :
45 : namespace dbaui
46 : {
47 :
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;
58 :
59 : // ODatasourceConnector
60 0 : ODatasourceConnector::ODatasourceConnector(const Reference< XComponentContext >& _rxContext, Window* _pMessageParent)
61 : :m_pErrorMessageParent(_pMessageParent)
62 0 : ,m_xContext(_rxContext)
63 : {
64 0 : }
65 :
66 0 : ODatasourceConnector::ODatasourceConnector( const Reference< XComponentContext >& _rxContext, Window* _pMessageParent,
67 : const OUString& _rContextInformation )
68 : :m_pErrorMessageParent(_pMessageParent)
69 : ,m_xContext(_rxContext)
70 0 : ,m_sContextInformation( _rContextInformation )
71 : {
72 0 : }
73 :
74 0 : Reference< XConnection > ODatasourceConnector::connect( const OUString& _rDataSourceName,
75 : ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
76 : {
77 0 : Reference< XConnection > xConnection;
78 :
79 : OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
80 0 : if (!isValid())
81 0 : return xConnection;
82 :
83 : // get the data source
84 : Reference< XDataSource > xDatasource(
85 : getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xContext, _pErrorInfo ),
86 : UNO_QUERY
87 0 : );
88 :
89 0 : if ( xDatasource.is() )
90 0 : xConnection = connect( xDatasource, _pErrorInfo );
91 0 : return xConnection;
92 : }
93 :
94 0 : Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource,
95 : ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
96 : {
97 0 : Reference< XConnection > xConnection;
98 :
99 : OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" );
100 0 : if ( !isValid() || !_xDataSource.is() )
101 0 : return xConnection;
102 :
103 : // get user/password
104 0 : OUString sPassword, sUser;
105 0 : sal_Bool bPwdRequired = sal_False;
106 0 : Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY);
107 : try
108 : {
109 0 : xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword;
110 0 : xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired;
111 0 : xProp->getPropertyValue(PROPERTY_USER) >>= sUser;
112 : }
113 0 : catch(Exception&)
114 : {
115 : DBG_UNHANDLED_EXCEPTION();
116 : }
117 :
118 : // try to connect
119 0 : SQLExceptionInfo aInfo;
120 : try
121 : {
122 0 : if (bPwdRequired && sPassword.isEmpty())
123 : { // password required, but empty -> connect using an interaction handler
124 0 : Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW );
125 :
126 0 : Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW );
127 0 : ::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
128 0 : Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) );
129 :
130 0 : if ( !xHandler.is() )
131 : {
132 : // instantiate the default SDB interaction handler
133 0 : xHandler = Reference< XInteractionHandler >( InteractionHandler::createWithParent(m_xContext, 0), UNO_QUERY );
134 : }
135 :
136 0 : xConnection = xConnectionCompletion->connectWithCompletion(xHandler);
137 : }
138 : else
139 : {
140 0 : xConnection = _xDataSource->getConnection(sUser, sPassword);
141 : }
142 : }
143 0 : catch( const SQLException& )
144 : {
145 0 : aInfo = ::cppu::getCaughtException();
146 : }
147 0 : catch(const Exception&)
148 : {
149 : DBG_UNHANDLED_EXCEPTION();
150 : }
151 :
152 0 : if ( !aInfo.isValid() )
153 : {
154 : // there was no error during connecting, but perhaps a warning?
155 0 : Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY );
156 0 : if ( xConnectionWarnings.is() )
157 : {
158 : try
159 : {
160 0 : Any aWarnings( xConnectionWarnings->getWarnings() );
161 0 : if ( aWarnings.hasValue() )
162 : {
163 0 : OUString sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) );
164 0 : sMessage = sMessage.replaceFirst( "$buttontext$", Button::GetStandardText( BUTTON_MORE ) );
165 0 : sMessage = OutputDevice::GetNonMnemonicString( sMessage );
166 :
167 0 : SQLWarning aContext;
168 0 : aContext.Message = sMessage;
169 0 : aContext.NextException = aWarnings;
170 0 : aInfo = aContext;
171 : }
172 0 : xConnectionWarnings->clearWarnings();
173 : }
174 0 : catch( const Exception& )
175 : {
176 : DBG_UNHANDLED_EXCEPTION();
177 : }
178 0 : }
179 : }
180 : else
181 : {
182 0 : if ( !m_sContextInformation.isEmpty() )
183 : {
184 0 : SQLException aError;
185 0 : aError.Message = m_sContextInformation;
186 0 : aError.NextException = aInfo.get();
187 :
188 0 : aInfo = aError;
189 : }
190 : }
191 :
192 : // was there an error?
193 0 : if ( aInfo.isValid() )
194 : {
195 0 : if ( _pErrorInfo )
196 : {
197 0 : *_pErrorInfo = aInfo;
198 : }
199 : else
200 : {
201 0 : showError( aInfo, m_pErrorMessageParent, m_xContext );
202 : }
203 : }
204 0 : return xConnection;
205 : }
206 :
207 : } // namespace dbaui
208 :
209 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|