LCOV - code coverage report
Current view: top level - extensions/source/abpilot - datasourcehandling.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 233 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 42 0.0 %
Legend: Lines: hit not hit

          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 "abpresid.hrc"
      22             : #include "abptypes.hxx"
      23             : #include "componentmodule.hxx"
      24             : #include "datasourcehandling.hxx"
      25             : #include "addresssettings.hxx"
      26             : 
      27             : #include <boost/noncopyable.hpp>
      28             : #include <com/sun/star/beans/XPropertySet.hpp>
      29             : #include <com/sun/star/container/XNameAccess.hpp>
      30             : #include <com/sun/star/frame/XStorable.hpp>
      31             : #include <com/sun/star/lang/XComponent.hpp>
      32             : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      33             : #include <com/sun/star/sdb/DatabaseContext.hpp>
      34             : #include <com/sun/star/sdb/SQLContext.hpp>
      35             : #include <com/sun/star/sdb/XCompletedConnection.hpp>
      36             : #include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
      37             : #include <com/sun/star/sdb/XDocumentDataSource.hpp>
      38             : #include <com/sun/star/sdbc/XConnection.hpp>
      39             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      40             : #include <com/sun/star/task/InteractionHandler.hpp>
      41             : #include <com/sun/star/uno/XNamingService.hpp>
      42             : 
      43             : #include <comphelper/interaction.hxx>
      44             : #include <comphelper/processfactory.hxx>
      45             : #include <tools/debug.hxx>
      46             : #include <tools/diagnose_ex.h>
      47             : #include <unotools/confignode.hxx>
      48             : #include <unotools/sharedunocomponent.hxx>
      49             : #include <vcl/stdtext.hxx>
      50             : #include <sfx2/objsh.hxx>
      51             : #include <sfx2/docfile.hxx>
      52             : #include <sfx2/viewfrm.hxx>
      53             : #include <comphelper/propertysequence.hxx>
      54             : 
      55             : namespace
      56             : {
      57             : 
      58             : /// Returns the URL of this object shell.
      59           0 : OUString lcl_getOwnURL(SfxObjectShell* pObjectShell)
      60             : {
      61           0 :     OUString aRet;
      62             : 
      63           0 :     if (!pObjectShell)
      64           0 :         return aRet;
      65             : 
      66           0 :     const INetURLObject& rURLObject = pObjectShell->GetMedium()->GetURLObject();
      67           0 :     aRet = rURLObject.GetMainURL(INetURLObject::DECODE_WITH_CHARSET);
      68           0 :     return aRet;
      69             : }
      70             : 
      71             : }
      72             : 
      73             : namespace abp
      74             : {
      75             : 
      76             : 
      77             :     using namespace ::utl;
      78             :     using namespace ::comphelper;
      79             :     using namespace ::com::sun::star;
      80             :     using namespace ::com::sun::star::uno;
      81             :     using namespace ::com::sun::star::lang;
      82             :     using namespace ::com::sun::star::sdb;
      83             :     using namespace ::com::sun::star::sdbc;
      84             :     using namespace ::com::sun::star::task;
      85             :     using namespace ::com::sun::star::beans;
      86             :     using namespace ::com::sun::star::sdbcx;
      87             :     using namespace ::com::sun::star::container;
      88             :     using namespace ::com::sun::star::frame;
      89             : 
      90             : 
      91             :     struct PackageAccessControl { };
      92             : 
      93             : 
      94             : 
      95           0 :     static Reference< XDatabaseContext > lcl_getDataSourceContext( const Reference< XComponentContext >& _rxContext )
      96             :     {
      97           0 :         Reference<XDatabaseContext> xContext = DatabaseContext::create(_rxContext);
      98           0 :         return xContext;
      99             :     }
     100             : 
     101             : 
     102             :     /// creates a new data source and inserts it into the context
     103           0 :     static void lcl_implCreateAndInsert(
     104             :         const Reference< XComponentContext >& _rxContext, const OUString& _rName,
     105             :         Reference< XPropertySet >& /* [out] */ _rxNewDataSource )
     106             :     {
     107             : 
     108             :         // get the data source context
     109           0 :         Reference< XDatabaseContext > xContext = lcl_getDataSourceContext( _rxContext );
     110             : 
     111             :         DBG_ASSERT( !xContext->hasByName( _rName ), "lcl_implCreateAndInsert: name already used!" );
     112             :         (void)_rName;
     113             : 
     114             : 
     115             :         // create a new data source
     116           0 :         Reference< XPropertySet > xNewDataSource;
     117           0 :         if (xContext.is())
     118           0 :             xNewDataSource = Reference< XPropertySet >( xContext->createInstance(), UNO_QUERY );
     119             :         DBG_ASSERT( xNewDataSource.is(), "lcl_implCreateAndInsert: could not create a new data source!" );
     120             : 
     121             : 
     122             :         // insert the data source into the context
     123             :         DBG_ASSERT( xContext.is(), "lcl_implCreateAndInsert: missing an interface on the context (XNamingService)!" );
     124           0 :         if (xContext.is())
     125             :         {
     126             :             //  xDynamicContext->registerObject( _rName, xNewDataSource );
     127           0 :             _rxNewDataSource = xNewDataSource;
     128           0 :         }
     129           0 :     }
     130             : 
     131             : 
     132             :     /// creates and inserts a data source, and sets its URL property to the string given
     133           0 :     static ODataSource lcl_implCreateAndSetURL(
     134             :         const Reference< XComponentContext >& _rxORB, const OUString& _rName,
     135             :         const sal_Char* _pInitialAsciiURL )
     136             :     {
     137           0 :         ODataSource aReturn( _rxORB );
     138             :         try
     139             :         {
     140             :             // create the new data source
     141           0 :             Reference< XPropertySet > xNewDataSource;
     142           0 :             lcl_implCreateAndInsert( _rxORB, _rName, xNewDataSource );
     143             : 
     144             : 
     145             :             // set the URL property
     146           0 :             if (xNewDataSource.is())
     147             :             {
     148           0 :                 xNewDataSource->setPropertyValue(
     149             :                     OUString( "URL" ),
     150             :                     makeAny( OUString::createFromAscii( _pInitialAsciiURL ) )
     151           0 :                 );
     152             :             }
     153             : 
     154           0 :             aReturn.setDataSource( xNewDataSource, _rName,PackageAccessControl() );
     155             :         }
     156           0 :         catch(const Exception&)
     157             :         {
     158             :             OSL_FAIL( "lcl_implCreateAndSetURL: caught an exception while creating the data source!" );
     159             :         }
     160             : 
     161           0 :         return aReturn;
     162             :     }
     163             : 
     164           0 :     void lcl_registerDataSource(
     165             :         const Reference< XComponentContext >& _rxORB, const OUString& _sName,
     166             :         const OUString& _sURL )
     167             :     {
     168             :         OSL_ENSURE( !_sName.isEmpty(), "lcl_registerDataSource: invalid name!" );
     169             :         OSL_ENSURE( !_sURL.isEmpty(), "lcl_registerDataSource: invalid URL!" );
     170             :         try
     171             :         {
     172           0 :             Reference< XDatabaseContext > xRegistrations( DatabaseContext::create(_rxORB) );
     173           0 :             if ( xRegistrations->hasRegisteredDatabase( _sName ) )
     174           0 :                 xRegistrations->changeDatabaseLocation( _sName, _sURL );
     175             :             else
     176           0 :                 xRegistrations->registerDatabaseLocation( _sName, _sURL );
     177             :         }
     178           0 :         catch( const Exception& )
     179             :         {
     180             :             DBG_UNHANDLED_EXCEPTION();
     181             :         }
     182           0 :     }
     183             : 
     184           0 :     struct ODataSourceContextImpl: private boost::noncopyable
     185             :     {
     186             :         Reference< XComponentContext >      xORB;
     187             :         Reference< XNameAccess >            xContext;           /// the UNO data source context
     188             :         StringBag                           aDataSourceNames;   /// for quicker name checks (without the UNO overhead)
     189             : 
     190           0 :         ODataSourceContextImpl( const Reference< XComponentContext >& _rxORB ) : xORB( _rxORB ) { }
     191             :     };
     192             : 
     193           0 :     ODataSourceContext::ODataSourceContext(const Reference< XComponentContext >& _rxORB)
     194           0 :         :m_pImpl( new ODataSourceContextImpl( _rxORB ) )
     195             :     {
     196             :         try
     197             :         {
     198             :             // create the UNO context
     199           0 :             m_pImpl->xContext = Reference<XNameAccess>(
     200             :                       lcl_getDataSourceContext( _rxORB ),
     201           0 :                       UNO_QUERY_THROW);
     202             : 
     203           0 :             if (m_pImpl->xContext.is())
     204             :             {
     205             :                 // collect the data source names
     206           0 :                 Sequence< OUString > aDSNames = m_pImpl->xContext->getElementNames();
     207           0 :                 const OUString* pDSNames = aDSNames.getConstArray();
     208           0 :                 const OUString* pDSNamesEnd = pDSNames + aDSNames.getLength();
     209             : 
     210           0 :                 for ( ;pDSNames != pDSNamesEnd; ++pDSNames )
     211           0 :                     m_pImpl->aDataSourceNames.insert( *pDSNames );
     212             :             }
     213             :         }
     214           0 :         catch( const Exception& )
     215             :         {
     216             :             OSL_FAIL( "ODataSourceContext::ODataSourceContext: caught an exception!" );
     217             :         }
     218           0 :     }
     219           0 :     ODataSourceContext::~ODataSourceContext()
     220             :     {
     221           0 :         delete(m_pImpl);
     222           0 :     }
     223             : 
     224             : 
     225           0 :     OUString& ODataSourceContext::disambiguate(OUString& _rDataSourceName)
     226             :     {
     227           0 :         OUString sCheck( _rDataSourceName );
     228           0 :         StringBag::const_iterator aPos = m_pImpl->aDataSourceNames.find( sCheck );
     229             : 
     230           0 :         sal_Int32 nPostfix = 1;
     231           0 :         while ( ( m_pImpl->aDataSourceNames.end() != aPos ) && ( nPostfix < 65535 ) )
     232             :         {   // there already is a data source with this name
     233           0 :             sCheck = _rDataSourceName;
     234           0 :             sCheck += OUString::number( nPostfix++ );
     235             : 
     236           0 :             aPos = m_pImpl->aDataSourceNames.find( sCheck );
     237             :         }
     238             : 
     239           0 :         _rDataSourceName = sCheck;
     240           0 :         return _rDataSourceName;
     241             :     }
     242             : 
     243             : 
     244           0 :     void ODataSourceContext::getDataSourceNames( StringBag& _rNames ) const
     245             :     {
     246           0 :         _rNames = m_pImpl->aDataSourceNames;
     247           0 :     }
     248             : 
     249             : 
     250           0 :     ODataSource ODataSourceContext::createNewLDAP( const OUString& _rName)
     251             :     {
     252           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:ldap:" );
     253             :     }
     254             : 
     255             : 
     256           0 :     ODataSource ODataSourceContext::createNewMORK( const OUString& _rName)
     257             :     {
     258           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:mozilla" );
     259             :     }
     260             : 
     261             : 
     262           0 :     ODataSource ODataSourceContext::createNewThunderbird( const OUString& _rName )
     263             :     {
     264           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:thunderbird" );
     265             :     }
     266             : 
     267             : 
     268           0 :     ODataSource ODataSourceContext::createNewEvolutionLdap( const OUString& _rName)
     269             :     {
     270           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:evolution:ldap" );
     271             :     }
     272             : 
     273           0 :     ODataSource ODataSourceContext::createNewEvolutionGroupwise( const OUString& _rName)
     274             :     {
     275           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:evolution:groupwise" );
     276             :     }
     277             : 
     278           0 :     ODataSource ODataSourceContext::createNewEvolution( const OUString& _rName)
     279             :     {
     280           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:evolution:local" );
     281             :     }
     282             : 
     283             : 
     284           0 :     ODataSource ODataSourceContext::createNewKab( const OUString& _rName)
     285             :     {
     286           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:kab" );
     287             :     }
     288             : 
     289             : 
     290           0 :     ODataSource ODataSourceContext::createNewMacab( const OUString& _rName)
     291             :     {
     292           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:macab" );
     293             :     }
     294             : 
     295             : 
     296           0 :     ODataSource ODataSourceContext::createNewOutlook( const OUString& _rName)
     297             :     {
     298           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:outlook" );
     299             :     }
     300             : 
     301             : 
     302           0 :     ODataSource ODataSourceContext::createNewOE( const OUString& _rName)
     303             :     {
     304           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:outlookexp" );
     305             :     }
     306             : 
     307             : 
     308           0 :     ODataSource ODataSourceContext::createNewDBase( const OUString& _rName)
     309             :     {
     310           0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:dbase:" );
     311             :     }
     312             : 
     313           0 :     struct ODataSourceImpl
     314             :     {
     315             :     public:
     316             :         Reference< XComponentContext >          xORB;               /// the service factory
     317             :         Reference< XPropertySet >               xDataSource;        /// the UNO data source
     318             :         ::utl::SharedUNOComponent< XConnection >
     319             :                                                 xConnection;
     320             :         StringBag                               aTables;            // the cached table names
     321             :         OUString                         sName;
     322             :         bool                                bTablesUpToDate;    // table name cache up-to-date?
     323             : 
     324           0 :         ODataSourceImpl( const Reference< XComponentContext >& _rxORB )
     325             :             :xORB( _rxORB )
     326           0 :             ,bTablesUpToDate( false )
     327             :         {
     328           0 :         }
     329             : 
     330             :         ODataSourceImpl( const ODataSourceImpl& _rSource );
     331             :     };
     332             : 
     333             : 
     334           0 :     ODataSourceImpl::ODataSourceImpl( const ODataSourceImpl& _rSource )
     335             :         :xORB( _rSource.xORB )
     336             :         ,xDataSource( _rSource.xDataSource )
     337             :         ,xConnection( _rSource.xConnection )
     338             :         ,aTables( _rSource.aTables )
     339             :         ,sName( _rSource.sName )
     340           0 :         ,bTablesUpToDate( _rSource.bTablesUpToDate )
     341             :     {
     342           0 :     }
     343             : 
     344           0 :     ODataSource::ODataSource( const ODataSource& _rSource )
     345           0 :         :m_pImpl( NULL )
     346             :     {
     347           0 :         *this = _rSource;
     348           0 :     }
     349             : 
     350             : 
     351           0 :     ODataSource& ODataSource::operator=( const ODataSource& _rSource )
     352             :     {
     353           0 :         if( this != &_rSource )
     354             :         {
     355           0 :             delete m_pImpl;
     356           0 :             m_pImpl = new ODataSourceImpl( *_rSource.m_pImpl );
     357             :         }
     358           0 :         return *this;
     359             :     }
     360             : 
     361             : 
     362           0 :     ODataSource::ODataSource( const Reference< XComponentContext >& _rxORB )
     363           0 :         :m_pImpl(new ODataSourceImpl(_rxORB))
     364             :     {
     365           0 :     }
     366             : 
     367             : 
     368           0 :     ODataSource::~ODataSource( )
     369             :     {
     370           0 :         delete m_pImpl;
     371           0 :     }
     372             : 
     373           0 :     void ODataSource::store(const AddressSettings& rSettings)
     374             :     {
     375           0 :         if (!isValid())
     376             :             // nothing to do
     377           0 :             return;
     378             :         try
     379             :         {
     380           0 :             Reference< XDocumentDataSource > xDocAccess( m_pImpl->xDataSource, UNO_QUERY );
     381           0 :             Reference< XStorable > xStorable;
     382           0 :             if ( xDocAccess.is() )
     383           0 :                 xStorable.set(xDocAccess->getDatabaseDocument(), css::uno::UNO_QUERY);
     384             :             OSL_ENSURE( xStorable.is(),"DataSource is no XStorable!" );
     385           0 :             if ( xStorable.is() )
     386             :             {
     387           0 :                 SfxObjectShell* pObjectShell = SfxViewFrame::Current()->GetObjectShell();
     388           0 :                 OUString aOwnURL = lcl_getOwnURL(pObjectShell);
     389           0 :                 if (aOwnURL.isEmpty() || !rSettings.bEmbedDataSource)
     390             :                 {
     391             :                     // Cannot or should not embed.
     392           0 :                     xStorable->storeAsURL(m_pImpl->sName,Sequence<PropertyValue>());
     393             :                 }
     394             :                 else
     395             :                 {
     396             :                     // Embed.
     397           0 :                     OUString aStreamRelPath = "EmbeddedDatabase";
     398           0 :                     OUString sTmpName = "vnd.sun.star.pkg://";
     399           0 :                     sTmpName += INetURLObject::encode(aOwnURL, INetURLObject::PART_AUTHORITY, INetURLObject::ENCODE_ALL);
     400           0 :                     sTmpName += "/" + aStreamRelPath;
     401           0 :                     uno::Reference<embed::XStorage> xStorage = pObjectShell->GetStorage();
     402             :                     uno::Sequence<beans::PropertyValue> aSequence = comphelper::InitPropertySequence(
     403             :                     {
     404             :                         {"TargetStorage", uno::makeAny(xStorage)},
     405             :                         {"StreamRelPath", uno::makeAny(aStreamRelPath)},
     406             :                         {"BaseURI", uno::makeAny(aOwnURL)}
     407           0 :                     });
     408           0 :                     xStorable->storeAsURL(sTmpName, aSequence);
     409           0 :                     m_pImpl->sName = sTmpName;
     410             : 
     411             :                     // Refer to the sub-storage name in the document settings, so
     412             :                     // we can load it again next time the file is imported.
     413           0 :                     uno::Reference<lang::XMultiServiceFactory> xFactory(pObjectShell->GetModel(), uno::UNO_QUERY);
     414           0 :                     uno::Reference<beans::XPropertySet> xPropertySet(xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY);
     415           0 :                     xPropertySet->setPropertyValue("EmbeddedDatabaseName", uno::makeAny(aStreamRelPath));
     416           0 :                 }
     417           0 :             }
     418             :         }
     419           0 :         catch(const Exception&)
     420             :         {
     421             :             OSL_FAIL( "ODataSource::registerDataSource: caught an exception while creating the data source!" );
     422             :         }
     423             :     }
     424             : 
     425           0 :     void ODataSource::registerDataSource( const OUString& _sRegisteredDataSourceName)
     426             :     {
     427           0 :         if (!isValid())
     428             :             // nothing to do
     429           0 :             return;
     430             : 
     431             :         try
     432             :         {
     433             :             // invalidate ourself
     434           0 :             lcl_registerDataSource(m_pImpl->xORB,_sRegisteredDataSourceName,m_pImpl->sName);
     435             :         }
     436           0 :         catch(const Exception&)
     437             :         {
     438             :             OSL_FAIL( "ODataSource::registerDataSource: caught an exception while creating the data source!" );
     439             :         }
     440             :     }
     441             : 
     442             : 
     443           0 :     void ODataSource::setDataSource( const Reference< XPropertySet >& _rxDS,const OUString& _sName, PackageAccessControl )
     444             :     {
     445           0 :         if (m_pImpl->xDataSource.get() == _rxDS.get())
     446             :             // nothing to do
     447           0 :             return;
     448             : 
     449           0 :         if ( isConnected() )
     450           0 :             disconnect();
     451             : 
     452           0 :         m_pImpl->sName = _sName;
     453           0 :         m_pImpl->xDataSource = _rxDS;
     454             :     }
     455             : 
     456             : 
     457           0 :     void ODataSource::remove()
     458             :     {
     459           0 :         if (!isValid())
     460             :             // nothing to do
     461           0 :             return;
     462             : 
     463             :         try
     464             :         {
     465             :             // invalidate ourself
     466           0 :             m_pImpl->xDataSource.clear();
     467             :         }
     468           0 :         catch(const Exception&)
     469             :         {
     470             :             OSL_FAIL( "ODataSource::remove: caught an exception while creating the data source!" );
     471             :         }
     472             :     }
     473             : 
     474             : 
     475           0 :     bool ODataSource::rename( const OUString& _rName )
     476             :     {
     477           0 :         if (!isValid())
     478             :             // nothing to do
     479           0 :             return false;
     480             : 
     481           0 :         m_pImpl->sName = _rName;
     482           0 :         return true;
     483             :     }
     484             : 
     485             : 
     486           0 :     OUString ODataSource::getName() const
     487             :     {
     488           0 :         if ( !isValid() )
     489           0 :             return OUString();
     490           0 :         return m_pImpl->sName;
     491             :     }
     492             : 
     493             : 
     494           0 :     bool ODataSource::hasTable( const OUString& _rTableName ) const
     495             :     {
     496           0 :         if ( !isConnected() )
     497           0 :             return false;
     498             : 
     499           0 :         const StringBag& aTables( getTableNames() );
     500           0 :         return aTables.find( _rTableName ) != aTables.end();
     501             :     }
     502             : 
     503             : 
     504           0 :     const StringBag& ODataSource::getTableNames() const
     505             :     {
     506           0 :         m_pImpl->aTables.clear();
     507           0 :         if ( !isConnected() )
     508             :         {
     509             :             OSL_FAIL( "ODataSource::getTableNames: not connected!" );
     510             :         }
     511             :         else
     512             :         {
     513             :             try
     514             :             {
     515             :                 // get the tables container from the connection
     516           0 :                 Reference< XTablesSupplier > xSuppTables( m_pImpl->xConnection.getTyped(), UNO_QUERY );
     517           0 :                 Reference< XNameAccess > xTables;
     518           0 :                 if ( xSuppTables.is( ) )
     519           0 :                     xTables = xSuppTables->getTables();
     520             :                 DBG_ASSERT( xTables.is(), "ODataSource::getTableNames: could not retrieve the tables container!" );
     521             : 
     522             :                 // get the names
     523           0 :                 Sequence< OUString > aTableNames;
     524           0 :                 if ( xTables.is( ) )
     525           0 :                     aTableNames = xTables->getElementNames( );
     526             : 
     527             :                 // copy the names
     528           0 :                 const OUString* pTableNames = aTableNames.getConstArray();
     529           0 :                 const OUString* pTableNamesEnd = pTableNames + aTableNames.getLength();
     530           0 :                 for (;pTableNames < pTableNamesEnd; ++pTableNames)
     531           0 :                     m_pImpl->aTables.insert( *pTableNames );
     532             :             }
     533           0 :             catch(const Exception&)
     534             :             {
     535             :             }
     536             :         }
     537             : 
     538             :         // now the table cache is up-to-date
     539           0 :         m_pImpl->bTablesUpToDate = true;
     540           0 :         return m_pImpl->aTables;
     541             :     }
     542             : 
     543             : 
     544           0 :     bool ODataSource::connect( vcl::Window* _pMessageParent )
     545             :     {
     546           0 :         if ( isConnected( ) )
     547             :             // nothing to do
     548           0 :             return true;
     549             : 
     550             : 
     551             :         // create the interaction handler (needed for authentication and error handling)
     552           0 :         Reference< XInteractionHandler > xInteractions;
     553             :         try
     554             :         {
     555             :             xInteractions.set(
     556             :                 InteractionHandler::createWithParent(m_pImpl->xORB, 0),
     557           0 :                 UNO_QUERY);
     558             :         }
     559           0 :         catch(const Exception&)
     560             :         {
     561             :         }
     562             : 
     563             : 
     564             :         // failure to create the interaction handler is a serious issue ...
     565           0 :         if (!xInteractions.is())
     566             :         {
     567           0 :             OUString s_sInteractionHandlerServiceName("com.sun.star.task.InteractionHandler");
     568           0 :             if ( _pMessageParent )
     569           0 :                 ShowServiceNotAvailableError( _pMessageParent, s_sInteractionHandlerServiceName, true );
     570           0 :             return false;
     571             :         }
     572             : 
     573             : 
     574             :         // open the connection
     575           0 :         Any aError;
     576           0 :         Reference< XConnection > xConnection;
     577             :         try
     578             :         {
     579           0 :             Reference< XCompletedConnection > xComplConn( m_pImpl->xDataSource, UNO_QUERY );
     580             :             DBG_ASSERT( xComplConn.is(), "ODataSource::connect: missing the XCompletedConnection interface on the data source!" );
     581           0 :             if ( xComplConn.is() )
     582           0 :                 xConnection = xComplConn->connectWithCompletion( xInteractions );
     583             :         }
     584           0 :         catch( const SQLContext& e ) { aError <<= e; }
     585           0 :         catch( const SQLWarning& e ) { aError <<= e; }
     586           0 :         catch( const SQLException& e ) { aError <<= e; }
     587           0 :         catch( const Exception& )
     588             :         {
     589             :             OSL_FAIL( "ODataSource::connect: caught a generic exception!" );
     590             :         }
     591             : 
     592             : 
     593             :         // handle errors
     594           0 :         if ( aError.hasValue() && _pMessageParent )
     595             :         {
     596             :             try
     597             :             {
     598           0 :                 SQLException aException;
     599           0 :                   aError >>= aException;
     600           0 :                   if ( aException.Message.isEmpty() )
     601             :                   {
     602             :                     // prepend some context info
     603           0 :                     SQLContext aDetailedError;
     604           0 :                     aDetailedError.Message = ModuleRes(RID_STR_NOCONNECTION).toString();
     605           0 :                     aDetailedError.Details = ModuleRes(RID_STR_PLEASECHECKSETTINGS).toString();
     606           0 :                     aDetailedError.NextException = aError;
     607             :                     // handle (aka display) the new context info
     608           0 :                     xInteractions->handle( new OInteractionRequest( makeAny( aDetailedError ) ) );
     609             :                   }
     610             :                   else
     611             :                   {
     612             :                       // handle (aka display) the original error
     613           0 :                     xInteractions->handle( new OInteractionRequest( makeAny( aException ) ) );
     614           0 :                 }
     615             :             }
     616           0 :             catch( const Exception& )
     617             :             {
     618             :                 OSL_FAIL( "ODataSource::connect: caught an exception while trying to display the error!" );
     619             :             }
     620             :         }
     621             : 
     622           0 :         if ( !xConnection.is() )
     623           0 :             return false;
     624             : 
     625             : 
     626             :         // success
     627           0 :         m_pImpl->xConnection.reset( xConnection );
     628           0 :         m_pImpl->aTables.clear();
     629           0 :         m_pImpl->bTablesUpToDate = false;
     630             : 
     631           0 :         return true;
     632             :     }
     633             : 
     634             : 
     635           0 :     void ODataSource::disconnect( )
     636             :     {
     637           0 :         m_pImpl->xConnection.clear();
     638           0 :         m_pImpl->aTables.clear();
     639           0 :         m_pImpl->bTablesUpToDate = false;
     640           0 :     }
     641             : 
     642             : 
     643           0 :     bool ODataSource::isConnected( ) const
     644             :     {
     645           0 :         return m_pImpl->xConnection.is();
     646             :     }
     647             : 
     648             : 
     649           0 :     bool ODataSource::isValid() const
     650             :     {
     651           0 :         return m_pImpl && m_pImpl->xDataSource.is();
     652             :     }
     653             : 
     654           0 :     Reference< XPropertySet > ODataSource::getDataSource() const
     655             :     {
     656           0 :         return m_pImpl ? m_pImpl->xDataSource : Reference< XPropertySet >();
     657             :     }
     658             : 
     659             : 
     660             : }   // namespace abp
     661             : 
     662             : 
     663             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11