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 :
21 : #include "datasourceconnector.hxx"
22 : #include <osl/diagnose.h>
23 : #include "dbustrings.hrc"
24 : #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
25 : #include <com/sun/star/beans/XPropertySet.hpp>
26 : #include <com/sun/star/sdb/XCompletedConnection.hpp>
27 : #include <com/sun/star/task/InteractionHandler.hpp>
28 : #include <com/sun/star/frame/XModel.hpp>
29 : #include <com/sun/star/sdb/SQLContext.hpp>
30 : #include <com/sun/star/sdbc/SQLWarning.hpp>
31 : #include <osl/thread.h>
32 : #include <comphelper/processfactory.hxx>
33 : #include <comphelper/extract.hxx>
34 : #include <comphelper/namedvaluecollection.hxx>
35 : #include <connectivity/dbexception.hxx>
36 : #include <com/sun/star/sdbc/XDataSource.hpp>
37 : #include "UITools.hxx"
38 : #include <vcl/stdtext.hxx>
39 : #include <vcl/button.hxx>
40 : #include <svl/filenotation.hxx>
41 : #include <tools/diagnose_ex.h>
42 : #include <cppuhelper/exc_hlp.hxx>
43 : #include "dbu_misc.hrc"
44 : #include "moduledbu.hxx"
45 :
46 : //.........................................................................
47 : namespace dbaui
48 : {
49 : //.........................................................................
50 :
51 : using namespace ::com::sun::star::uno;
52 : using namespace ::com::sun::star::lang;
53 : using namespace ::com::sun::star::sdb;
54 : using namespace ::com::sun::star::sdbc;
55 : using namespace ::com::sun::star::task;
56 : using namespace ::com::sun::star::beans;
57 : using namespace ::com::sun::star::container;
58 : using namespace ::com::sun::star::frame;
59 : using namespace ::dbtools;
60 : using ::svt::OFileNotation;
61 :
62 : //=====================================================================
63 : //= ODatasourceConnector
64 : //=====================================================================
65 : //---------------------------------------------------------------------
66 0 : ODatasourceConnector::ODatasourceConnector(const Reference< XComponentContext >& _rxContext, Window* _pMessageParent)
67 : :m_pErrorMessageParent(_pMessageParent)
68 0 : ,m_xContext(_rxContext)
69 : {
70 0 : }
71 :
72 : //---------------------------------------------------------------------
73 0 : ODatasourceConnector::ODatasourceConnector( const Reference< XComponentContext >& _rxContext, Window* _pMessageParent,
74 : const ::rtl::OUString& _rContextInformation )
75 : :m_pErrorMessageParent(_pMessageParent)
76 : ,m_xContext(_rxContext)
77 0 : ,m_sContextInformation( _rContextInformation )
78 : {
79 0 : }
80 :
81 : //---------------------------------------------------------------------
82 0 : Reference< XConnection > ODatasourceConnector::connect( const ::rtl::OUString& _rDataSourceName,
83 : ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
84 : {
85 0 : Reference< XConnection > xConnection;
86 :
87 : OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
88 0 : if (!isValid())
89 : return xConnection;
90 :
91 : // get the data source
92 : Reference< XDataSource > xDatasource(
93 : getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xContext, _pErrorInfo ),
94 : UNO_QUERY
95 0 : );
96 :
97 0 : if ( xDatasource.is() )
98 0 : xConnection = connect( xDatasource, _pErrorInfo );
99 0 : return xConnection;
100 : }
101 :
102 : //---------------------------------------------------------------------
103 0 : Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource,
104 : ::dbtools::SQLExceptionInfo* _pErrorInfo ) const
105 : {
106 0 : Reference< XConnection > xConnection;
107 :
108 : OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" );
109 0 : if ( !isValid() || !_xDataSource.is() )
110 : return xConnection;
111 :
112 : // get user/password
113 0 : ::rtl::OUString sPassword, sUser;
114 0 : sal_Bool bPwdRequired = sal_False;
115 0 : Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY);
116 : try
117 : {
118 0 : xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword;
119 0 : xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired;
120 0 : xProp->getPropertyValue(PROPERTY_USER) >>= sUser;
121 : }
122 0 : catch(Exception&)
123 : {
124 : DBG_UNHANDLED_EXCEPTION();
125 : }
126 :
127 : // try to connect
128 0 : SQLExceptionInfo aInfo;
129 : try
130 : {
131 0 : if (bPwdRequired && sPassword.isEmpty())
132 : { // password required, but empty -> connect using an interaction handler
133 0 : Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW );
134 :
135 0 : Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW );
136 0 : ::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
137 0 : Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) );
138 :
139 0 : if ( !xHandler.is() )
140 : {
141 : // instantiate the default SDB interaction handler
142 0 : xHandler = Reference< XInteractionHandler >( InteractionHandler::createWithParent(m_xContext, 0), UNO_QUERY );
143 : }
144 :
145 0 : xConnection = xConnectionCompletion->connectWithCompletion(xHandler);
146 : }
147 : else
148 : {
149 0 : xConnection = _xDataSource->getConnection(sUser, sPassword);
150 : }
151 : }
152 0 : catch( const SQLException& )
153 : {
154 0 : aInfo = ::cppu::getCaughtException();
155 : }
156 0 : catch(const Exception&)
157 : {
158 : DBG_UNHANDLED_EXCEPTION();
159 : }
160 :
161 0 : if ( !aInfo.isValid() )
162 : {
163 : // there was no error during connecting, but perhaps a warning?
164 0 : Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY );
165 0 : if ( xConnectionWarnings.is() )
166 : {
167 : try
168 : {
169 0 : Any aWarnings( xConnectionWarnings->getWarnings() );
170 0 : if ( aWarnings.hasValue() )
171 : {
172 0 : String sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) );
173 0 : sMessage.SearchAndReplaceAscii( "$buttontext$", Button::GetStandardText( BUTTON_MORE ) );
174 0 : sMessage = OutputDevice::GetNonMnemonicString( sMessage );
175 :
176 0 : SQLWarning aContext;
177 0 : aContext.Message = sMessage;
178 0 : aContext.NextException = aWarnings;
179 0 : aInfo = aContext;
180 : }
181 0 : xConnectionWarnings->clearWarnings();
182 : }
183 0 : catch( const Exception& )
184 : {
185 : DBG_UNHANDLED_EXCEPTION();
186 : }
187 0 : }
188 : }
189 : else
190 : {
191 0 : if ( !m_sContextInformation.isEmpty() )
192 : {
193 0 : SQLException aError;
194 0 : aError.Message = m_sContextInformation;
195 0 : aError.NextException = aInfo.get();
196 :
197 0 : aInfo = aError;
198 : }
199 : }
200 :
201 : // was there an error?
202 0 : if ( aInfo.isValid() )
203 : {
204 0 : if ( _pErrorInfo )
205 : {
206 0 : *_pErrorInfo = aInfo;
207 : }
208 : else
209 : {
210 0 : showError( aInfo, m_pErrorMessageParent, m_xContext );
211 : }
212 : }
213 0 : return xConnection;
214 : }
215 :
216 : //.........................................................................
217 : } // namespace dbaui
218 : //.........................................................................
219 :
220 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|