2010-10-12 15:59:03 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-05-10 11:17:59 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-10 15:05:17 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2001-05-10 11:17:59 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2001-05-10 11:17:59 +00:00
|
|
|
*
|
2008-04-10 15:05:17 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2001-05-10 11:17:59 +00:00
|
|
|
*
|
2008-04-10 15:05:17 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2001-05-10 11:17:59 +00:00
|
|
|
*
|
2008-04-10 15:05:17 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2001-05-10 11:17:59 +00:00
|
|
|
*
|
2008-04-10 15:05:17 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2001-05-10 11:17:59 +00:00
|
|
|
*
|
2008-04-10 15:05:17 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2001-05-10 11:17:59 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2006-09-17 06:16:21 +00:00
|
|
|
|
2001-05-10 11:17:59 +00:00
|
|
|
#include "datasourceconnector.hxx"
|
|
|
|
#include <osl/diagnose.h>
|
|
|
|
#include "dbustrings.hrc"
|
2006-07-10 14:37:21 +00:00
|
|
|
#include <com/sun/star/sdbc/XWarningsSupplier.hpp>
|
2001-05-10 11:17:59 +00:00
|
|
|
#include <com/sun/star/beans/XPropertySet.hpp>
|
|
|
|
#include <com/sun/star/sdb/XCompletedConnection.hpp>
|
|
|
|
#include <com/sun/star/task/XInteractionHandler.hpp>
|
2008-03-06 17:28:54 +00:00
|
|
|
#include <com/sun/star/frame/XModel.hpp>
|
2001-05-11 07:29:30 +00:00
|
|
|
#include <com/sun/star/sdb/SQLContext.hpp>
|
|
|
|
#include <com/sun/star/sdbc/SQLWarning.hpp>
|
2001-05-10 11:17:59 +00:00
|
|
|
#include <osl/thread.h>
|
|
|
|
#include <comphelper/extract.hxx>
|
2008-03-06 17:28:54 +00:00
|
|
|
#include <comphelper/namedvaluecollection.hxx>
|
2001-05-10 11:17:59 +00:00
|
|
|
#include <connectivity/dbexception.hxx>
|
|
|
|
#include <com/sun/star/sdbc/XDataSource.hpp>
|
|
|
|
#include "UITools.hxx"
|
|
|
|
#include <vcl/stdtext.hxx>
|
2006-08-28 14:08:32 +00:00
|
|
|
#include <vcl/button.hxx>
|
2009-10-16 00:05:16 +02:00
|
|
|
#include <svl/filenotation.hxx>
|
2006-07-10 14:37:21 +00:00
|
|
|
#include <tools/diagnose_ex.h>
|
2008-12-01 12:31:27 +00:00
|
|
|
#include <cppuhelper/exc_hlp.hxx>
|
2005-01-21 16:20:22 +00:00
|
|
|
#include "dbu_misc.hrc"
|
|
|
|
#include "moduledbu.hxx"
|
2001-05-10 11:17:59 +00:00
|
|
|
|
|
|
|
//.........................................................................
|
|
|
|
namespace dbaui
|
|
|
|
{
|
|
|
|
//.........................................................................
|
|
|
|
|
|
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
using namespace ::com::sun::star::lang;
|
|
|
|
using namespace ::com::sun::star::sdb;
|
|
|
|
using namespace ::com::sun::star::sdbc;
|
|
|
|
using namespace ::com::sun::star::task;
|
|
|
|
using namespace ::com::sun::star::beans;
|
|
|
|
using namespace ::com::sun::star::container;
|
2008-03-06 17:28:54 +00:00
|
|
|
using namespace ::com::sun::star::frame;
|
2001-05-10 11:17:59 +00:00
|
|
|
using namespace ::dbtools;
|
2005-01-21 16:20:22 +00:00
|
|
|
using ::svt::OFileNotation;
|
2001-05-10 11:17:59 +00:00
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
//= ODatasourceConnector
|
|
|
|
//=====================================================================
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
ODatasourceConnector::ODatasourceConnector(const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent)
|
2006-06-20 02:23:26 +00:00
|
|
|
:m_pErrorMessageParent(_pMessageParent)
|
|
|
|
,m_xORB(_rxORB)
|
2001-05-10 11:17:59 +00:00
|
|
|
{
|
2001-08-15 05:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
ODatasourceConnector::ODatasourceConnector( const Reference< XMultiServiceFactory >& _rxORB, Window* _pMessageParent,
|
2007-07-24 11:10:26 +00:00
|
|
|
const ::rtl::OUString& _rContextInformation )
|
2006-06-20 02:23:26 +00:00
|
|
|
:m_pErrorMessageParent(_pMessageParent)
|
|
|
|
,m_xORB(_rxORB)
|
2001-08-15 05:48:59 +00:00
|
|
|
,m_sContextInformation( _rContextInformation )
|
|
|
|
{
|
2001-05-10 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2008-12-01 12:31:27 +00:00
|
|
|
Reference< XConnection > ODatasourceConnector::connect( const ::rtl::OUString& _rDataSourceName,
|
|
|
|
::dbtools::SQLExceptionInfo* _pErrorInfo ) const
|
2001-05-10 11:17:59 +00:00
|
|
|
{
|
|
|
|
Reference< XConnection > xConnection;
|
|
|
|
|
|
|
|
OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
|
|
|
|
if (!isValid())
|
|
|
|
return xConnection;
|
|
|
|
|
|
|
|
// get the data source
|
2005-01-21 16:20:22 +00:00
|
|
|
Reference< XDataSource > xDatasource(
|
2008-12-01 12:31:27 +00:00
|
|
|
getDataSourceByName( _rDataSourceName, m_pErrorMessageParent, m_xORB, _pErrorInfo ),
|
2005-01-21 16:20:22 +00:00
|
|
|
UNO_QUERY
|
|
|
|
);
|
2008-12-01 12:31:27 +00:00
|
|
|
|
2005-01-21 16:20:22 +00:00
|
|
|
if ( xDatasource.is() )
|
2008-12-01 12:31:27 +00:00
|
|
|
xConnection = connect( xDatasource, _pErrorInfo );
|
2005-01-21 16:20:22 +00:00
|
|
|
return xConnection;
|
2004-08-02 15:08:51 +00:00
|
|
|
}
|
2005-01-21 16:20:22 +00:00
|
|
|
|
2004-08-02 15:08:51 +00:00
|
|
|
//---------------------------------------------------------------------
|
2008-12-01 12:31:27 +00:00
|
|
|
Reference< XConnection > ODatasourceConnector::connect(const Reference< XDataSource>& _xDataSource,
|
|
|
|
::dbtools::SQLExceptionInfo* _pErrorInfo ) const
|
2004-08-02 15:08:51 +00:00
|
|
|
{
|
|
|
|
Reference< XConnection > xConnection;
|
|
|
|
|
2008-12-01 12:31:27 +00:00
|
|
|
OSL_ENSURE( isValid() && _xDataSource.is(), "ODatasourceConnector::connect: invalid object or argument!" );
|
|
|
|
if ( !isValid() || !_xDataSource.is() )
|
2001-05-10 11:17:59 +00:00
|
|
|
return xConnection;
|
|
|
|
|
|
|
|
// get user/password
|
|
|
|
::rtl::OUString sPassword, sUser;
|
|
|
|
sal_Bool bPwdRequired = sal_False;
|
2004-08-02 15:08:51 +00:00
|
|
|
Reference<XPropertySet> xProp(_xDataSource,UNO_QUERY);
|
2001-05-10 11:17:59 +00:00
|
|
|
try
|
|
|
|
{
|
2004-08-02 15:08:51 +00:00
|
|
|
xProp->getPropertyValue(PROPERTY_PASSWORD) >>= sPassword;
|
|
|
|
xProp->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED) >>= bPwdRequired;
|
|
|
|
xProp->getPropertyValue(PROPERTY_USER) >>= sUser;
|
2001-05-10 11:17:59 +00:00
|
|
|
}
|
|
|
|
catch(Exception&)
|
|
|
|
{
|
2008-12-01 12:31:27 +00:00
|
|
|
DBG_UNHANDLED_EXCEPTION();
|
2001-05-10 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to connect
|
|
|
|
SQLExceptionInfo aInfo;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (bPwdRequired && !sPassword.getLength())
|
|
|
|
{ // password required, but empty -> connect using an interaction handler
|
2006-07-10 14:37:21 +00:00
|
|
|
Reference< XCompletedConnection > xConnectionCompletion( _xDataSource, UNO_QUERY_THROW );
|
|
|
|
|
2008-03-06 17:28:54 +00:00
|
|
|
Reference< XModel > xModel( getDataSourceOrModel( _xDataSource ), UNO_QUERY_THROW );
|
|
|
|
::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
|
|
|
|
Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) );
|
|
|
|
|
|
|
|
if ( !xHandler.is() )
|
2001-05-10 11:17:59 +00:00
|
|
|
{
|
2008-03-06 17:28:54 +00:00
|
|
|
// instantiate the default SDB interaction handler
|
autorecovery: more sophisticated configuration data for interaction handlers
The generic css.task.InteractionHandler implementation in module uui is now able to instantiate "sub handlers", i.e.
components to delegate a request to, based on the type of the request, and some configuration data.
The "old" (and now deprecated) configuration scheme at org.openoffice.ucb.InteractionHandler did not contain type
information, so any handlers registered there were always called when no default implementation for a given request
was available.
The "new" configuration scheme at org.openoffice.Interaction contains UNO type information. That is, a given handler
implementation can declare itself responsible for an arbitrary set of UNO types, and for each of those types, whether
it is also responsible for sub types.
The generic interaction handler implementation uses this configuration data, when it encounteres an interaction
request it cannot fullfill itself, to instantiate a component to delegate the request to.
As with the "old" data, such a component is required to support the css.task.XInteractionHandler2 interface. Also,
if it supports css.lang.XInitialization, then it will be initialized with a name-value pair, the name being
"Parent", the value being the XWindow interface of the parent window for any message boxes.
As an examplary implementation for this feature, the css.sdb.InteractionHandler has been deprecated. Now the
css.sdb.DatabaseInteractionHandler is reponsible for database-related interactions, and the new configuration scheme
is pre-filled with data assigning this responsibility.
Consequently, a lot of places previously creating an css.sdb.InteractionHandler have been modified to create the
default css.task.InteractionHandler.
2010-02-01 21:32:33 +01:00
|
|
|
xHandler = Reference< XInteractionHandler >( m_xORB->createInstance( SERVICE_TASK_INTERACTION_HANDLER ), UNO_QUERY );
|
2008-03-06 17:28:54 +00:00
|
|
|
if ( !xHandler.is() )
|
autorecovery: more sophisticated configuration data for interaction handlers
The generic css.task.InteractionHandler implementation in module uui is now able to instantiate "sub handlers", i.e.
components to delegate a request to, based on the type of the request, and some configuration data.
The "old" (and now deprecated) configuration scheme at org.openoffice.ucb.InteractionHandler did not contain type
information, so any handlers registered there were always called when no default implementation for a given request
was available.
The "new" configuration scheme at org.openoffice.Interaction contains UNO type information. That is, a given handler
implementation can declare itself responsible for an arbitrary set of UNO types, and for each of those types, whether
it is also responsible for sub types.
The generic interaction handler implementation uses this configuration data, when it encounteres an interaction
request it cannot fullfill itself, to instantiate a component to delegate the request to.
As with the "old" data, such a component is required to support the css.task.XInteractionHandler2 interface. Also,
if it supports css.lang.XInitialization, then it will be initialized with a name-value pair, the name being
"Parent", the value being the XWindow interface of the parent window for any message boxes.
As an examplary implementation for this feature, the css.sdb.InteractionHandler has been deprecated. Now the
css.sdb.DatabaseInteractionHandler is reponsible for database-related interactions, and the new configuration scheme
is pre-filled with data assigning this responsibility.
Consequently, a lot of places previously creating an css.sdb.InteractionHandler have been modified to create the
default css.task.InteractionHandler.
2010-02-01 21:32:33 +01:00
|
|
|
ShowServiceNotAvailableError(m_pErrorMessageParent, (::rtl::OUString)SERVICE_TASK_INTERACTION_HANDLER, sal_True);
|
2001-05-10 11:17:59 +00:00
|
|
|
}
|
2008-03-06 17:28:54 +00:00
|
|
|
|
|
|
|
if ( xHandler.is() )
|
2006-07-10 14:37:21 +00:00
|
|
|
{
|
|
|
|
xConnection = xConnectionCompletion->connectWithCompletion(xHandler);
|
2001-05-10 11:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-02 15:08:51 +00:00
|
|
|
xConnection = _xDataSource->getConnection(sUser, sPassword);
|
2001-05-10 11:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-01 12:31:27 +00:00
|
|
|
catch( const SQLException& )
|
|
|
|
{
|
|
|
|
aInfo = ::cppu::getCaughtException();
|
|
|
|
}
|
2006-07-10 14:37:21 +00:00
|
|
|
catch(const Exception&)
|
|
|
|
{
|
|
|
|
DBG_UNHANDLED_EXCEPTION();
|
|
|
|
}
|
|
|
|
|
2008-12-01 12:31:27 +00:00
|
|
|
if ( !aInfo.isValid() )
|
|
|
|
{
|
|
|
|
// there was no error during connecting, but perhaps a warning?
|
|
|
|
Reference< XWarningsSupplier > xConnectionWarnings( xConnection, UNO_QUERY );
|
|
|
|
if ( xConnectionWarnings.is() )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Any aWarnings( xConnectionWarnings->getWarnings() );
|
|
|
|
if ( aWarnings.hasValue() )
|
|
|
|
{
|
|
|
|
String sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT ) );
|
|
|
|
sMessage.SearchAndReplaceAscii( "$buttontext$", Button::GetStandardText( BUTTON_MORE ) );
|
|
|
|
sMessage = OutputDevice::GetNonMnemonicString( sMessage );
|
2001-05-10 11:17:59 +00:00
|
|
|
|
2008-12-01 12:31:27 +00:00
|
|
|
SQLWarning aContext;
|
|
|
|
aContext.Message = sMessage;
|
|
|
|
aContext.NextException = aWarnings;
|
|
|
|
aInfo = aContext;
|
|
|
|
}
|
|
|
|
xConnectionWarnings->clearWarnings();
|
|
|
|
}
|
|
|
|
catch( const Exception& )
|
|
|
|
{
|
|
|
|
DBG_UNHANDLED_EXCEPTION();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2001-08-15 05:48:59 +00:00
|
|
|
{
|
|
|
|
if ( m_sContextInformation.getLength() )
|
|
|
|
{
|
2007-07-24 11:10:26 +00:00
|
|
|
SQLException aError;
|
|
|
|
aError.Message = m_sContextInformation;
|
|
|
|
aError.NextException = aInfo.get();
|
2001-08-15 05:48:59 +00:00
|
|
|
|
2007-07-24 11:10:26 +00:00
|
|
|
aInfo = aError;
|
2001-08-15 05:48:59 +00:00
|
|
|
}
|
2006-07-10 14:37:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 12:31:27 +00:00
|
|
|
// was there an error?
|
|
|
|
if ( aInfo.isValid() )
|
2006-07-10 14:37:21 +00:00
|
|
|
{
|
2008-12-01 12:31:27 +00:00
|
|
|
if ( _pErrorInfo )
|
2006-07-10 14:37:21 +00:00
|
|
|
{
|
2008-12-01 12:31:27 +00:00
|
|
|
*_pErrorInfo = aInfo;
|
2006-07-10 14:37:21 +00:00
|
|
|
}
|
2008-12-01 12:31:27 +00:00
|
|
|
else
|
2006-07-10 14:37:21 +00:00
|
|
|
{
|
2008-12-01 12:31:27 +00:00
|
|
|
showError( aInfo, m_pErrorMessageParent, m_xORB );
|
2006-07-10 14:37:21 +00:00
|
|
|
}
|
2001-08-15 05:48:59 +00:00
|
|
|
}
|
2001-05-10 11:17:59 +00:00
|
|
|
return xConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
//.........................................................................
|
|
|
|
} // namespace dbaui
|
|
|
|
//.........................................................................
|
|
|
|
|
2010-10-12 15:59:03 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|