LCOV - code coverage report
Current view: top level - libreoffice/xmlhelp/source/cxxhelp/provider - provider.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 155 0.0 %
Date: 2012-12-27 Functions: 0 24 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             : /**************************************************************************
      22             :                                 TODO
      23             :  **************************************************************************
      24             : 
      25             :  *************************************************************************/
      26             : 
      27             : #include <stdio.h>
      28             : #include <osl/file.hxx>
      29             : #include <osl/diagnose.h>
      30             : #include <ucbhelper/contentidentifier.hxx>
      31             : #include <com/sun/star/frame/XConfigManager.hpp>
      32             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      33             : #include <com/sun/star/beans/PropertyValue.hpp>
      34             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      35             : #include <com/sun/star/container/XContainer.hpp>
      36             : #include <com/sun/star/container/XNameAccess.hpp>
      37             : #include <com/sun/star/container/XNameReplace.hpp>
      38             : #include <com/sun/star/uno/XComponentContext.hpp>
      39             : #include <comphelper/processfactory.hxx>
      40             : #include <unotools/configmgr.hxx>
      41             : #include <rtl/bootstrap.hxx>
      42             : 
      43             : #include "databases.hxx"
      44             : #include "provider.hxx"
      45             : #include "content.hxx"
      46             : 
      47             : using namespace com::sun::star;
      48             : using namespace chelp;
      49             : 
      50             : //=========================================================================
      51             : //=========================================================================
      52             : //
      53             : // ContentProvider Implementation.
      54             : //
      55             : //=========================================================================
      56             : //=========================================================================
      57             : 
      58           0 : ContentProvider::ContentProvider(
      59             :     const uno::Reference< uno::XComponentContext >& rxContext )
      60             :     : ::ucbhelper::ContentProviderImplHelper( rxContext ),
      61             :         isInitialized( false ),
      62             :         m_aScheme(MYUCP_URL_SCHEME),
      63           0 :         m_pDatabases( 0 )
      64             : {
      65           0 : }
      66             : 
      67             : //=========================================================================
      68             : // virtual
      69           0 : ContentProvider::~ContentProvider()
      70             : {
      71           0 :     delete m_pDatabases;
      72           0 : }
      73             : 
      74             : //=========================================================================
      75             : //
      76             : // XInterface methods.
      77             : //
      78             : //=========================================================================
      79             : 
      80           0 : XINTERFACE_IMPL_6( ContentProvider,
      81             :                    lang::XTypeProvider,
      82             :                    lang::XServiceInfo,
      83             :                    ucb::XContentProvider,
      84             :                    lang::XComponent,
      85             :                    lang::XEventListener, /* base of XContainerListener */
      86             :                    container::XContainerListener);
      87             : 
      88             : //=========================================================================
      89             : //
      90             : // XTypeProvider methods.
      91             : //
      92             : //=========================================================================
      93             : 
      94           0 : XTYPEPROVIDER_IMPL_5( ContentProvider,
      95             :                          lang::XTypeProvider,
      96             :                          lang::XServiceInfo,
      97             :                          ucb::XContentProvider,
      98             :                       lang::XComponent,
      99             :                       container::XContainerListener);
     100             : 
     101             : //=========================================================================
     102             : //
     103             : // XServiceInfo methods.
     104             : //
     105             : //=========================================================================
     106             : 
     107           0 : rtl::OUString SAL_CALL ContentProvider::getImplementationName()
     108             :     throw( uno::RuntimeException )
     109             : {
     110           0 :     return getImplementationName_Static();
     111             : }
     112             : 
     113           0 : rtl::OUString ContentProvider::getImplementationName_Static()
     114             : {
     115           0 :     return rtl::OUString("CHelpContentProvider" );
     116             : }
     117             : 
     118             : sal_Bool SAL_CALL
     119           0 : ContentProvider::supportsService(const rtl::OUString& ServiceName )
     120             :     throw( uno::RuntimeException )
     121             : {
     122           0 :     uno::Sequence< rtl::OUString > aSNL = getSupportedServiceNames();
     123           0 :     const rtl::OUString* pArray = aSNL.getArray();
     124           0 :     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
     125             :     {
     126           0 :         if( pArray[ i ] == ServiceName )
     127           0 :             return sal_True;
     128             :     }
     129             : 
     130           0 :     return sal_False;
     131             : }
     132             : 
     133             : uno::Sequence< rtl::OUString > SAL_CALL
     134           0 : ContentProvider::getSupportedServiceNames()
     135             :     throw( uno::RuntimeException )
     136             : {
     137           0 :     return getSupportedServiceNames_Static();
     138             : }
     139             : 
     140             : static uno::Reference< uno::XInterface > SAL_CALL
     141           0 : ContentProvider_CreateInstance(
     142             :          const uno::Reference< lang::XMultiServiceFactory> & rSMgr )
     143             :     throw( uno::Exception )
     144             : {
     145             :     lang::XServiceInfo * pX = static_cast< lang::XServiceInfo * >(
     146           0 :         new ContentProvider( comphelper::getComponentContext(rSMgr) ) );
     147           0 :     return uno::Reference< uno::XInterface >::query( pX );
     148             : }
     149             : 
     150             : uno::Sequence< rtl::OUString >
     151           0 : ContentProvider::getSupportedServiceNames_Static()
     152             : {
     153           0 :     uno::Sequence< rtl::OUString > aSNS( 2 );
     154             :     aSNS.getArray()[ 0 ] =
     155             :         rtl::OUString(
     156           0 :             MYUCP_CONTENT_PROVIDER_SERVICE_NAME1 );
     157           0 :     aSNS.getArray()[ 1 ] =
     158             :         rtl::OUString(
     159           0 :             MYUCP_CONTENT_PROVIDER_SERVICE_NAME2 );
     160             : 
     161           0 :     return aSNS;
     162             : }
     163             : 
     164             : //=========================================================================
     165             : //
     166             : // Service factory implementation.
     167             : //
     168             : //=========================================================================
     169             : 
     170           0 : ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
     171             : 
     172             : //=========================================================================
     173             : //
     174             : // XContentProvider methods.
     175             : //
     176             : //=========================================================================
     177             : 
     178             : // virtual
     179             : uno::Reference< ucb::XContent > SAL_CALL
     180           0 : ContentProvider::queryContent(
     181             :         const uno::Reference< ucb::XContentIdentifier >& xCanonicId )
     182             :     throw( ucb::IllegalIdentifierException, uno::RuntimeException )
     183             : {
     184           0 :     if ( !xCanonicId->getContentProviderScheme()
     185           0 :              .equalsIgnoreAsciiCase( m_aScheme ) )
     186             :     {   // Wrong URL-scheme
     187           0 :         throw ucb::IllegalIdentifierException();
     188             :     }
     189             : 
     190             :     {
     191           0 :         osl::MutexGuard aGuard( m_aMutex );
     192           0 :         if( !isInitialized )
     193           0 :             init();
     194             :     }
     195             : 
     196           0 :     if( !m_pDatabases )
     197           0 :         throw uno::RuntimeException();
     198             : 
     199           0 :     rtl::OUString aOUString( m_pDatabases->getInstallPathAsURL() );
     200             :     rtl::OString aOString( aOUString.getStr(),
     201             :                            aOUString.getLength(),
     202           0 :                            RTL_TEXTENCODING_UTF8 );
     203             : 
     204             :     // Check, if a content with given id already exists...
     205             :     uno::Reference< ucb::XContent > xContent
     206           0 :         = queryExistingContent( xCanonicId ).get();
     207           0 :     if ( xContent.is() )
     208             :         return xContent;
     209             : 
     210           0 :     xContent = new Content( m_xContext, this, xCanonicId, m_pDatabases );
     211             : 
     212             :     // register new content
     213           0 :     registerNewContent( xContent );
     214             : 
     215             :     // Further checks
     216             : 
     217           0 :     if ( !xContent->getIdentifier().is() )
     218           0 :         throw ucb::IllegalIdentifierException();
     219             : 
     220           0 :     return xContent;
     221             : }
     222             : 
     223             : void SAL_CALL
     224           0 : ContentProvider::dispose()
     225             :     throw ( uno::RuntimeException)
     226             : {
     227           0 :     if(m_xContainer.is())
     228             :     {
     229           0 :         m_xContainer->removeContainerListener(this);
     230           0 :         m_xContainer.clear();
     231             :     }
     232           0 : }
     233             : 
     234             : void SAL_CALL
     235           0 : ContentProvider::elementReplaced(const container::ContainerEvent& Event)
     236             :     throw (uno::RuntimeException)
     237             : {
     238           0 :     if(!m_pDatabases)
     239             :         return;
     240             : 
     241           0 :     rtl::OUString accessor;
     242           0 :     Event.Accessor >>= accessor;
     243           0 :     if(accessor.compareToAscii("HelpStyleSheet"))
     244             :         return;
     245             : 
     246           0 :     rtl::OUString replacedElement,element;
     247           0 :     Event.ReplacedElement >>= replacedElement;
     248           0 :     Event.Element >>= element;
     249             : 
     250           0 :     if(replacedElement == element)
     251             :         return;
     252             : 
     253           0 :     m_pDatabases->changeCSS(element);
     254             : }
     255             : 
     256           0 : void ContentProvider::init()
     257             : {
     258           0 :     osl::MutexGuard aGuard( m_aMutex );
     259             : 
     260           0 :     isInitialized = true;
     261             :     uno::Reference< lang::XMultiServiceFactory > sProvider(
     262           0 :         getConfiguration() );
     263             :     uno::Reference< container::XHierarchicalNameAccess > xHierAccess(
     264             :         getHierAccess( sProvider,
     265           0 :                        "org.openoffice.Office.Common" ) );
     266             : 
     267           0 :     rtl::OUString instPath( getKey( xHierAccess,"Path/Current/Help" ) );
     268           0 :     if( instPath.isEmpty() )
     269             :         // try to determine path from default
     270           0 :         instPath = rtl::OUString( "$(instpath)/help" );
     271             :     // replace anything like $(instpath);
     272           0 :     subst( instPath );
     273             : 
     274           0 :     rtl::OUString stylesheet( getKey( xHierAccess,"Help/HelpStyleSheet" ) );
     275             :     try
     276             :     {
     277             :         // now adding as configuration change listener for the stylesheet
     278             :         uno::Reference< container::XNameAccess> xAccess(
     279           0 :             xHierAccess, uno::UNO_QUERY );
     280           0 :         if( xAccess.is() )
     281             :         {
     282             :             uno::Any aAny =
     283           0 :                 xAccess->getByName( rtl::OUString( "Help" ) );
     284           0 :             aAny >>= m_xContainer;
     285           0 :             if( m_xContainer.is() )
     286           0 :                 m_xContainer->addContainerListener( this );
     287           0 :         }
     288             :     }
     289           0 :     catch( uno::Exception const & )
     290             :     {
     291             :     }
     292             : 
     293           0 :     xHierAccess = getHierAccess( sProvider, "org.openoffice.Setup" );
     294             : 
     295             :     rtl::OUString setupversion(
     296           0 :         getKey( xHierAccess,"Product/ooSetupVersion" ) );
     297           0 :     rtl::OUString setupextension;
     298             : 
     299             :     try
     300             :     {
     301             :         uno::Reference< lang::XMultiServiceFactory > xConfigProvider =
     302           0 :               configuration::theDefaultProvider::get( m_xContext );
     303             : 
     304           0 :         uno::Sequence < uno::Any > lParams(1);
     305           0 :         beans::PropertyValue                       aParam ;
     306           0 :         aParam.Name    = ::rtl::OUString("nodepath");
     307           0 :         aParam.Value <<= ::rtl::OUString("/org.openoffice.Setup/Product");
     308           0 :         lParams[0] = uno::makeAny(aParam);
     309             : 
     310             :         // open it
     311           0 :         uno::Reference< uno::XInterface > xCFG( xConfigProvider->createInstanceWithArguments(
     312             :                     ::rtl::OUString("com.sun.star.configuration.ConfigurationAccess"),
     313           0 :                     lParams) );
     314             : 
     315           0 :         uno::Reference< container::XNameAccess > xDirectAccess(xCFG, uno::UNO_QUERY);
     316           0 :         uno::Any aRet = xDirectAccess->getByName(::rtl::OUString("ooSetupExtension"));
     317             : 
     318           0 :         aRet >>= setupextension;
     319             :     }
     320           0 :     catch ( uno::Exception& )
     321             :     {
     322             :     }
     323             : 
     324             :     rtl::OUString productversion(
     325             :         setupversion +
     326           0 :         rtl::OUString( " " ) +
     327           0 :         setupextension );
     328             : 
     329           0 :     uno::Sequence< rtl::OUString > aImagesZipPaths( 2 );
     330           0 :     xHierAccess = getHierAccess( sProvider,  "org.openoffice.Office.Common" );
     331             : 
     332           0 :     rtl::OUString aPath( getKey( xHierAccess, "Path/Current/UserConfig" ) );
     333           0 :     subst( aPath );
     334           0 :     aImagesZipPaths[ 0 ] = aPath;
     335             : 
     336           0 :     aPath = rtl::OUString("$BRAND_BASE_DIR/share/config");
     337           0 :     rtl::Bootstrap::expandMacros(aPath);
     338           0 :     aImagesZipPaths[ 1 ] = aPath;
     339             : 
     340           0 :     sal_Bool showBasic = getBooleanKey(xHierAccess,"Help/ShowBasic");
     341             :     m_pDatabases = new Databases( showBasic,
     342             :                                   instPath,
     343             :                                   aImagesZipPaths,
     344             :                                   utl::ConfigManager::getProductName(),
     345             :                                   productversion,
     346             :                                   stylesheet,
     347           0 :                                   m_xContext );
     348           0 : }
     349             : 
     350             : uno::Reference< lang::XMultiServiceFactory >
     351           0 : ContentProvider::getConfiguration() const
     352             : {
     353           0 :     uno::Reference< lang::XMultiServiceFactory > xProvider;
     354           0 :     if( m_xContext.is() )
     355             :     {
     356             :         try
     357             :         {
     358           0 :             xProvider = configuration::theDefaultProvider::get( m_xContext );
     359             :         }
     360           0 :         catch( const uno::Exception& )
     361             :         {
     362             :             OSL_ENSURE( xProvider.is(), "cant instantiate configuration" );
     363             :         }
     364             :     }
     365             : 
     366           0 :     return xProvider;
     367             : }
     368             : 
     369             : uno::Reference< container::XHierarchicalNameAccess >
     370           0 : ContentProvider::getHierAccess(
     371             :     const uno::Reference< lang::XMultiServiceFactory >& sProvider,
     372             :     const char* file ) const
     373             : {
     374           0 :     uno::Reference< container::XHierarchicalNameAccess > xHierAccess;
     375             : 
     376           0 :     if( sProvider.is() )
     377             :     {
     378           0 :         uno::Sequence< uno::Any > seq( 1 );
     379             :         rtl::OUString sReaderService(
     380             :             rtl::OUString(
     381           0 :                 "com.sun.star.configuration.ConfigurationAccess" ) );
     382             : 
     383           0 :         seq[ 0 ] <<= rtl::OUString::createFromAscii( file );
     384             : 
     385             :         try
     386             :         {
     387             :             xHierAccess =
     388             :                 uno::Reference< container::XHierarchicalNameAccess >(
     389           0 :                     sProvider->createInstanceWithArguments(
     390           0 :                         sReaderService, seq ),
     391           0 :                     uno::UNO_QUERY );
     392             :         }
     393           0 :         catch( const uno::Exception& )
     394             :         {
     395           0 :         }
     396             :     }
     397           0 :     return xHierAccess;
     398             : }
     399             : 
     400             : 
     401             : 
     402             : rtl::OUString
     403           0 : ContentProvider::getKey(
     404             :     const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
     405             :     const char* key ) const
     406             : {
     407           0 :     rtl::OUString instPath;
     408           0 :     if( xHierAccess.is() )
     409             :     {
     410           0 :         uno::Any aAny;
     411             :         try
     412             :         {
     413             :             aAny =
     414           0 :                 xHierAccess->getByHierarchicalName(
     415           0 :                     rtl::OUString::createFromAscii( key ) );
     416             :         }
     417           0 :         catch( const container::NoSuchElementException& )
     418             :         {
     419             :         }
     420           0 :         aAny >>= instPath;
     421             :     }
     422           0 :     return instPath;
     423             : }
     424             : 
     425             : sal_Bool
     426           0 : ContentProvider::getBooleanKey(
     427             :     const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
     428             :     const char* key ) const
     429             : {
     430           0 :   sal_Bool ret = sal_False;
     431           0 :   if( xHierAccess.is() )
     432             :   {
     433           0 :       uno::Any aAny;
     434             :       try
     435             :       {
     436             :           aAny =
     437           0 :             xHierAccess->getByHierarchicalName(
     438           0 :                 rtl::OUString::createFromAscii( key ) );
     439             :       }
     440           0 :       catch( const container::NoSuchElementException& )
     441             :       {
     442             :       }
     443           0 :       aAny >>= ret;
     444             :   }
     445           0 :   return ret;
     446             : }
     447             : 
     448           0 : void ContentProvider::subst( rtl::OUString& instpath ) const
     449             : {
     450           0 :     uno::Reference< frame::XConfigManager > xCfgMgr;
     451           0 :     if( m_xContext.is() )
     452             :     {
     453             :         try
     454             :         {
     455             :             xCfgMgr =
     456             :                 uno::Reference< frame::XConfigManager >(
     457           0 :                     m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.config.SpecialConfigManager", m_xContext),
     458           0 :                     uno::UNO_QUERY );
     459             :         }
     460           0 :         catch( const uno::Exception&)
     461             :         {
     462             :             OSL_ENSURE( xCfgMgr.is(),
     463             :                         "cant instantiate the special config manager " );
     464             :         }
     465             :     }
     466             : 
     467             :     OSL_ENSURE( xCfgMgr.is(), "specialconfigmanager not found\n" );
     468             : 
     469           0 :     if( xCfgMgr.is() )
     470           0 :         instpath = xCfgMgr->substituteVariables( instpath );
     471           0 : }
     472             : 
     473             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10