LCOV - code coverage report
Current view: top level - dbaccess/source/ui/browser - dbloader.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 73 104 70.2 %
Date: 2015-06-13 12:38:46 Functions: 11 13 84.6 %
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             : #include "dbu_reghelper.hxx"
      21             : #include "dbustrings.hrc"
      22             : #include "uiservices.hxx"
      23             : #include "UITools.hxx"
      24             : 
      25             : #include <com/sun/star/container/XChild.hpp>
      26             : #include <com/sun/star/container/XNameAccess.hpp>
      27             : #include <com/sun/star/container/XSet.hpp>
      28             : #include <com/sun/star/document/XEventListener.hpp>
      29             : #include <com/sun/star/frame/XController2.hpp>
      30             : #include <com/sun/star/frame/XFrame.hpp>
      31             : #include <com/sun/star/frame/XFrameLoader.hpp>
      32             : #include <com/sun/star/frame/XLoadEventListener.hpp>
      33             : #include <com/sun/star/lang/XInitialization.hpp>
      34             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      35             : #include <com/sun/star/lang/XServiceInfo.hpp>
      36             : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      37             : #include <com/sun/star/registry/XRegistryKey.hpp>
      38             : #include <com/sun/star/sdb/ReportDesign.hpp>
      39             : #include <com/sun/star/sdbc/XConnection.hpp>
      40             : #include <com/sun/star/frame/XModule.hpp>
      41             : 
      42             : #include <com/sun/star/sdbc/XDataSource.hpp>
      43             : #include <comphelper/namedvaluecollection.hxx>
      44             : #include <comphelper/processfactory.hxx>
      45             : #include <cppuhelper/implbase2.hxx>
      46             : #include <cppuhelper/supportsservice.hxx>
      47             : #include <toolkit/awt/vclxwindow.hxx>
      48             : #include <toolkit/helper/vclunohelper.hxx>
      49             : #include <tools/diagnose_ex.h>
      50             : #include <tools/urlobj.hxx>
      51             : #include <vcl/svapp.hxx>
      52             : 
      53             : using namespace ::com::sun::star;
      54             : using namespace ::com::sun::star::uno;
      55             : using namespace ::com::sun::star::frame;
      56             : using namespace ::com::sun::star::beans;
      57             : using namespace ::com::sun::star::sdb;
      58             : using namespace ::com::sun::star::sdbc;
      59             : using namespace ::com::sun::star::container;
      60             : using namespace ::com::sun::star::lang;
      61             : using namespace ::com::sun::star::registry;
      62             : using namespace dbaui;
      63             : 
      64             : class DBContentLoader : public ::cppu::WeakImplHelper2< XFrameLoader, XServiceInfo>
      65             : {
      66             : private:
      67             :     OUString                     m_aURL;
      68             :     Sequence< PropertyValue>            m_aArgs;
      69             :     Reference< XLoadEventListener >     m_xListener;
      70             :     Reference< XFrame >                 m_xFrame;
      71             :     Reference< XComponentContext >      m_xContext;
      72             : public:
      73             :     DBContentLoader(const Reference< XComponentContext >&);
      74             :     virtual ~DBContentLoader();
      75             : 
      76             :     // XServiceInfo
      77             :     OUString                 SAL_CALL getImplementationName() throw(std::exception  ) SAL_OVERRIDE;
      78             :     sal_Bool                        SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception  ) SAL_OVERRIDE;
      79             :     Sequence< OUString >     SAL_CALL getSupportedServiceNames() throw(std::exception  ) SAL_OVERRIDE;
      80             : 
      81             :     // static methods
      82          26 :     static OUString          getImplementationName_Static() throw(  )
      83             :     {
      84          26 :         return OUString("org.openoffice.comp.dbu.DBContentLoader");
      85             :     }
      86             :     static Sequence< OUString> getSupportedServiceNames_Static() throw(  );
      87             :     static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
      88             :             SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&);
      89             : 
      90             :     // XLoader
      91             :     virtual void SAL_CALL load( const Reference< XFrame > & _rFrame, const OUString& _rURL,
      92             :                                 const Sequence< PropertyValue >& _rArgs,
      93             :                                 const Reference< XLoadEventListener > & _rListener) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      94             :     virtual void SAL_CALL cancel() throw(std::exception) SAL_OVERRIDE;
      95             : };
      96             : 
      97             : 
      98           4 : DBContentLoader::DBContentLoader(const Reference< XComponentContext >& _rxContext)
      99           4 :     :m_xContext(_rxContext)
     100             : {
     101             : 
     102           4 : }
     103             : 
     104           8 : DBContentLoader::~DBContentLoader()
     105             : {
     106             : 
     107           8 : }
     108             : 
     109          12 : extern "C" void SAL_CALL createRegistryInfo_DBContentLoader()
     110             : {
     111          12 :     static ::dbaui::OMultiInstanceAutoRegistration< DBContentLoader > aAutoRegistration;
     112          12 : }
     113             : 
     114           4 : Reference< XInterface > SAL_CALL DBContentLoader::Create( const Reference< XMultiServiceFactory >  & rSMgr )
     115             : {
     116           4 :     return *(new DBContentLoader(comphelper::getComponentContext(rSMgr)));
     117             : }
     118             : 
     119             : // XServiceInfo
     120           2 : OUString SAL_CALL DBContentLoader::getImplementationName() throw(std::exception  )
     121             : {
     122           2 :     return getImplementationName_Static();
     123             : }
     124             : 
     125             : // XServiceInfo
     126           0 : sal_Bool SAL_CALL DBContentLoader::supportsService(const OUString& ServiceName) throw(std::exception  )
     127             : {
     128           0 :     return cppu::supportsService(this, ServiceName);
     129             : }
     130             : 
     131             : // XServiceInfo
     132           1 : Sequence< OUString > SAL_CALL DBContentLoader::getSupportedServiceNames() throw(std::exception  )
     133             : {
     134           1 :     return getSupportedServiceNames_Static();
     135             : }
     136             : 
     137             : // ORegistryServiceManager_Static
     138          13 : Sequence< OUString > DBContentLoader::getSupportedServiceNames_Static() throw(  )
     139             : {
     140          13 :     Sequence< OUString > aSNS( 2 );
     141          13 :     aSNS[0] = "com.sun.star.frame.FrameLoader";
     142          13 :     aSNS[1] = "com.sun.star.sdb.ContentLoader";
     143          13 :     return aSNS;
     144             : }
     145             : 
     146           1 : void SAL_CALL DBContentLoader::load(const Reference< XFrame > & rFrame, const OUString& rURL,
     147             :         const Sequence< PropertyValue >& rArgs,
     148             :         const Reference< XLoadEventListener > & rListener) throw(::com::sun::star::uno::RuntimeException, std::exception)
     149             : {
     150           1 :     m_xFrame    = rFrame;
     151           1 :     m_xListener = rListener;
     152           1 :     m_aURL      = rURL;
     153           1 :     m_aArgs     = rArgs;
     154             : 
     155             :     struct ServiceNameToImplName
     156             :     {
     157             :         const sal_Char*     pAsciiServiceName;
     158             :         const sal_Char*     pAsciiImplementationName;
     159           6 :         ServiceNameToImplName( const sal_Char* _pService, const sal_Char* _pImpl )
     160             :             :pAsciiServiceName( _pService )
     161           6 :             ,pAsciiImplementationName( _pImpl )
     162             :         {
     163           6 :         }
     164             :     } aImplementations[] = {
     165             :         ServiceNameToImplName( URL_COMPONENT_FORMGRIDVIEW,      "org.openoffice.comp.dbu.OFormGridView"        ),
     166             :         ServiceNameToImplName( URL_COMPONENT_DATASOURCEBROWSER, "org.openoffice.comp.dbu.ODatasourceBrowser"   ),
     167             :         ServiceNameToImplName( URL_COMPONENT_QUERYDESIGN,       "org.openoffice.comp.dbu.OQueryDesign"         ),
     168             :         ServiceNameToImplName( URL_COMPONENT_TABLEDESIGN,       "org.openoffice.comp.dbu.OTableDesign"         ),
     169             :         ServiceNameToImplName( URL_COMPONENT_RELATIONDESIGN,    "org.openoffice.comp.dbu.ORelationDesign"      ),
     170             :         ServiceNameToImplName( URL_COMPONENT_VIEWDESIGN,        "org.openoffice.comp.dbu.OViewDesign"          )
     171           1 :     };
     172             : 
     173           1 :     INetURLObject aParser( rURL );
     174           2 :     Reference< XController2 > xController;
     175             : 
     176           2 :     const OUString sComponentURL( aParser.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
     177           2 :     for ( size_t i=0; i < sizeof( aImplementations ) / sizeof( aImplementations[0] ); ++i )
     178             :     {
     179           2 :         if ( sComponentURL.equalsAscii( aImplementations[i].pAsciiServiceName ) )
     180             :         {
     181           2 :             xController.set( m_xContext->getServiceManager()->
     182           1 :                createInstanceWithContext( OUString::createFromAscii( aImplementations[i].pAsciiImplementationName ), m_xContext), UNO_QUERY_THROW );
     183           1 :             break;
     184             :         }
     185             :     }
     186             : 
     187             :     // if a data source browser is loaded without its tree pane, then we assume it to be a
     188             :     // table data view, effectively. In this case, we need to adjust the module identifier.
     189             :     // #i85879#
     190           2 :     ::comphelper::NamedValueCollection aLoadArgs( rArgs );
     191             : 
     192           1 :     if  ( sComponentURL == URL_COMPONENT_DATASOURCEBROWSER )
     193             :     {
     194           4 :         bool bDisableBrowser =  !aLoadArgs.getOrDefault( "ShowTreeViewButton", sal_True )   // compatibility name
     195           3 :                                 ||  !aLoadArgs.getOrDefault( OUString(PROPERTY_ENABLE_BROWSER), sal_True );
     196             : 
     197           1 :         if ( bDisableBrowser )
     198             :         {
     199             :             try
     200             :             {
     201           0 :                 Reference< XModule > xModule( xController, UNO_QUERY_THROW );
     202           0 :                 xModule->setIdentifier( OUString( "com.sun.star.sdb.TableDataView" ) );
     203             :             }
     204           0 :             catch( const Exception& )
     205             :             {
     206             :                 DBG_UNHANDLED_EXCEPTION();
     207             :             }
     208             :         }
     209             :     }
     210             : 
     211           1 :     if ( sComponentURL == URL_COMPONENT_REPORTDESIGN )
     212             :     {
     213           0 :         bool bPreview = aLoadArgs.getOrDefault( "Preview", sal_False );
     214           0 :         if ( bPreview )
     215             :         {   // report designs cannot be previewed
     216           0 :             if ( rListener.is() )
     217           0 :                 rListener->loadCancelled( this );
     218           1 :             return;
     219             :         }
     220           0 :         Reference< XModel > xReportModel( aLoadArgs.getOrDefault( "Model", Reference< XModel >() ) );
     221           0 :         if ( xReportModel.is() )
     222             :         {
     223           0 :             xController.set( ReportDesign::create( m_xContext ) );
     224           0 :             xController->attachModel( xReportModel );
     225           0 :             xReportModel->connectController( xController.get() );
     226           0 :             xReportModel->setCurrentController( xController.get() );
     227           0 :         }
     228             :     }
     229             : 
     230           1 :     bool bSuccess = xController.is();
     231           2 :     Reference< XModel > xDatabaseDocument;
     232           1 :     if ( bSuccess )
     233             :     {
     234           1 :         Reference< XDataSource > xDataSource    ( aLoadArgs.getOrDefault( "DataSource",       Reference< XDataSource >() ) );
     235           2 :         OUString          sDataSourceName( aLoadArgs.getOrDefault( "DataSourceName",   OUString()          ) );
     236           2 :         Reference< XConnection > xConnection    ( aLoadArgs.getOrDefault( "ActiveConnection", Reference< XConnection >() ) );
     237           1 :         if ( xDataSource.is() )
     238             :         {
     239           0 :             xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
     240             :         }
     241           1 :         else if ( !sDataSourceName.isEmpty() )
     242             :         {
     243           0 :             ::dbtools::SQLExceptionInfo aError;
     244           0 :             xDataSource.set( getDataSourceByName( sDataSourceName, NULL, m_xContext, &aError ) );
     245           0 :             xDatabaseDocument.set( getDataSourceOrModel( xDataSource ), UNO_QUERY );
     246             :         }
     247           1 :         else if ( xConnection.is() )
     248             :         {
     249           0 :             Reference< XChild > xAsChild( xConnection, UNO_QUERY );
     250           0 :             if ( xAsChild.is() )
     251             :             {
     252             :                 OSL_ENSURE( Reference< XDataSource >( xAsChild->getParent(), UNO_QUERY ).is(),
     253             :                     "DBContentLoader::load: a connection whose parent is no data source?" );
     254           0 :                 xDatabaseDocument.set( getDataSourceOrModel( xAsChild->getParent() ), UNO_QUERY );
     255           0 :             }
     256             :         }
     257             : 
     258             :         // init controller
     259           2 :         SolarMutexGuard aGuard;
     260             :         try
     261             :         {
     262           1 :             Reference<XInitialization > xIni(xController,UNO_QUERY);
     263           2 :             PropertyValue aFrame(OUString("Frame"),0,makeAny(rFrame),PropertyState_DIRECT_VALUE);
     264           2 :             Sequence< Any > aInitArgs(m_aArgs.getLength()+1);
     265             : 
     266           1 :             Any* pBegin = aInitArgs.getArray();
     267           1 :             Any* pEnd   = pBegin + aInitArgs.getLength();
     268           1 :             *pBegin <<= aFrame;
     269           1 :             const PropertyValue* pIter      = m_aArgs.getConstArray();
     270           8 :             for(++pBegin;pBegin != pEnd;++pBegin,++pIter)
     271             :             {
     272           7 :                 *pBegin <<= *pIter;
     273             :             }
     274             : 
     275           2 :             xIni->initialize(aInitArgs);
     276             :         }
     277           0 :         catch(const Exception&)
     278             :         {
     279             :             // Does this need to be shown to the user?
     280           0 :             bSuccess = false;
     281             :             try
     282             :             {
     283           0 :                 ::comphelper::disposeComponent( xController );
     284             :             }
     285           0 :             catch( const Exception& )
     286             :             {
     287             :                 DBG_UNHANDLED_EXCEPTION();
     288             :             }
     289           1 :         }
     290             :     }
     291             : 
     292             :     // assign controller and frame
     293           1 :     if ( bSuccess )
     294             :     {
     295           1 :         if ( xController.is() && rFrame.is() )
     296             :         {
     297           1 :             rFrame->setComponent( xController->getComponentWindow(), xController.get() );
     298           1 :             xController->attachFrame(rFrame);
     299             :         }
     300             : 
     301           1 :         if ( rListener.is() )
     302           1 :             rListener->loadFinished( this );
     303             :     }
     304             :     else
     305           0 :         if ( rListener.is() )
     306           1 :             rListener->loadCancelled( this );
     307             : }
     308             : 
     309           0 : void DBContentLoader::cancel() throw(std::exception)
     310             : {
     311           0 : }
     312             : 
     313             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11