LCOV - code coverage report
Current view: top level - ucb/source/ucp/package - pkgprovider.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 82 0.0 %
Date: 2014-04-14 Functions: 0 30 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 <boost/unordered_map.hpp>
      28             : #include <osl/diagnose.h>
      29             : #include <comphelper/processfactory.hxx>
      30             : #include <cppuhelper/weak.hxx>
      31             : #include <ucbhelper/contentidentifier.hxx>
      32             : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
      33             : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
      34             : #include "pkgprovider.hxx"
      35             : #include "pkgcontent.hxx"
      36             : #include "pkguri.hxx"
      37             : 
      38             : using namespace com::sun::star;
      39             : 
      40             : namespace package_ucp
      41             : {
      42             : 
      43             : 
      44             : 
      45             : // class Package.
      46             : 
      47             : 
      48             : 
      49             : class Package : public cppu::OWeakObject,
      50             :                 public container::XHierarchicalNameAccess
      51             : {
      52             :     friend class ContentProvider;
      53             : 
      54             :     OUString                                        m_aName;
      55             :     uno::Reference< container::XHierarchicalNameAccess > m_xNA;
      56             :     ContentProvider*                                     m_pOwner;
      57             : 
      58             : public:
      59           0 :     Package( const OUString& rName,
      60             :              const uno::Reference< container::XHierarchicalNameAccess > & xNA,
      61             :              ContentProvider* pOwner )
      62           0 :     : m_aName( rName ), m_xNA( xNA ), m_pOwner( pOwner ) {}
      63           0 :     virtual ~Package() { m_pOwner->removePackage( m_aName ); }
      64             : 
      65             :     // XInterface
      66             :     virtual uno::Any SAL_CALL
      67           0 :     queryInterface( const uno::Type& aType )
      68             :         throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE
      69           0 :     { return m_xNA->queryInterface( aType ); }
      70             :     virtual void SAL_CALL
      71           0 :     acquire() throw() SAL_OVERRIDE
      72           0 :     { OWeakObject::acquire(); }
      73             :     virtual void SAL_CALL
      74           0 :     release() throw() SAL_OVERRIDE
      75           0 :     { OWeakObject::release(); }
      76             : 
      77             :     // XHierarchicalNameAccess
      78             :     virtual uno::Any SAL_CALL
      79           0 :     getByHierarchicalName( const OUString& aName )
      80             :         throw( container::NoSuchElementException, uno::RuntimeException, std::exception ) SAL_OVERRIDE
      81           0 :     { return m_xNA->getByHierarchicalName( aName ); }
      82             :     virtual sal_Bool SAL_CALL
      83           0 :     hasByHierarchicalName( const OUString& aName )
      84             :         throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE
      85           0 :     { return m_xNA->hasByHierarchicalName( aName ); }
      86             : };
      87             : 
      88             : 
      89             : 
      90             : // Packages.
      91             : 
      92             : 
      93             : 
      94             : struct equalString
      95             : {
      96           0 :     bool operator()(
      97             :         const OUString& rKey1, const OUString& rKey2 ) const
      98             :     {
      99           0 :         return !!( rKey1 == rKey2 );
     100             :     }
     101             : };
     102             : 
     103             : struct hashString
     104             : {
     105           0 :     size_t operator()( const OUString & rName ) const
     106             :     {
     107           0 :         return rName.hashCode();
     108             :     }
     109             : };
     110             : 
     111             : typedef boost::unordered_map
     112             : <
     113             :     OUString,
     114             :     Package*,
     115             :     hashString,
     116             :     equalString
     117             : >
     118             : PackageMap;
     119             : 
     120           0 : class Packages : public PackageMap {};
     121             : 
     122             : }
     123             : 
     124             : using namespace package_ucp;
     125             : 
     126             : 
     127             : 
     128             : 
     129             : // ContentProvider Implementation.
     130             : 
     131             : 
     132             : 
     133             : 
     134           0 : ContentProvider::ContentProvider(
     135             :             const uno::Reference< uno::XComponentContext >& rxContext )
     136             : : ::ucbhelper::ContentProviderImplHelper( rxContext ),
     137           0 :   m_pPackages( 0 )
     138             : {
     139           0 : }
     140             : 
     141             : 
     142             : // virtual
     143           0 : ContentProvider::~ContentProvider()
     144             : {
     145           0 :     delete m_pPackages;
     146           0 : }
     147             : 
     148             : // XInterface methods.
     149           0 : void SAL_CALL ContentProvider::acquire()
     150             :     throw()
     151             : {
     152           0 :     OWeakObject::acquire();
     153           0 : }
     154             : 
     155           0 : void SAL_CALL ContentProvider::release()
     156             :     throw()
     157             : {
     158           0 :     OWeakObject::release();
     159           0 : }
     160             : 
     161           0 : css::uno::Any SAL_CALL ContentProvider::queryInterface( const css::uno::Type & rType )
     162             :     throw( css::uno::RuntimeException, std::exception )
     163             : {
     164             :     css::uno::Any aRet = cppu::queryInterface( rType,
     165             :                                                (static_cast< lang::XTypeProvider* >(this)),
     166             :                                                (static_cast< lang::XServiceInfo* >(this)),
     167             :                                                (static_cast< ucb::XContentProvider* >(this))
     168           0 :                                                );
     169           0 :     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
     170             : }
     171             : 
     172             : // XTypeProvider methods.
     173             : 
     174             : 
     175             : 
     176           0 : XTYPEPROVIDER_IMPL_3( ContentProvider,
     177             :                       lang::XTypeProvider,
     178             :                       lang::XServiceInfo,
     179             :                       ucb::XContentProvider );
     180             : 
     181             : 
     182             : 
     183             : // XServiceInfo methods.
     184             : 
     185             : 
     186             : 
     187           0 : XSERVICEINFO_IMPL_1_CTX( ContentProvider,
     188             :                      OUString( "com.sun.star.comp.ucb.PackageContentProvider" ),
     189             :                      OUString( PACKAGE_CONTENT_PROVIDER_SERVICE_NAME ) );
     190             : 
     191             : 
     192             : 
     193             : // Service factory implementation.
     194             : 
     195             : 
     196             : 
     197           0 : ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
     198             : 
     199             : 
     200             : 
     201             : // XContentProvider methods.
     202             : 
     203             : 
     204             : 
     205             : // virtual
     206           0 : uno::Reference< ucb::XContent > SAL_CALL ContentProvider::queryContent(
     207             :             const uno::Reference< ucb::XContentIdentifier >& Identifier )
     208             :     throw( ucb::IllegalIdentifierException, uno::RuntimeException, std::exception )
     209             : {
     210           0 :     if ( !Identifier.is() )
     211           0 :         return uno::Reference< ucb::XContent >();
     212             : 
     213           0 :     PackageUri aUri( Identifier->getContentIdentifier() );
     214           0 :     if ( !aUri.isValid() )
     215           0 :         throw ucb::IllegalIdentifierException();
     216             : 
     217             :     // Create a new identifier for the mormalized URL returned by
     218             :     // PackageUri::getUri().
     219           0 :     uno::Reference< ucb::XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aUri.getUri() );
     220             : 
     221           0 :     osl::MutexGuard aGuard( m_aMutex );
     222             : 
     223             :     // Check, if a content with given id already exists...
     224             :     uno::Reference< ucb::XContent > xContent
     225           0 :         = queryExistingContent( xId ).get();
     226           0 :     if ( xContent.is() )
     227           0 :         return xContent;
     228             : 
     229             :     // Create a new content.
     230             : 
     231           0 :     xContent = Content::create( m_xContext, this, Identifier ); // not xId!!!
     232           0 :     registerNewContent( xContent );
     233             : 
     234           0 :     if ( xContent.is() && !xContent->getIdentifier().is() )
     235           0 :         throw ucb::IllegalIdentifierException();
     236             : 
     237           0 :     return xContent;
     238             : }
     239             : 
     240             : 
     241             : 
     242             : // Other methods.
     243             : 
     244             : 
     245             : 
     246             : uno::Reference< container::XHierarchicalNameAccess >
     247           0 : ContentProvider::createPackage( const PackageUri & rURI )
     248             : {
     249           0 :     osl::MutexGuard aGuard( m_aMutex );
     250             : 
     251           0 :     OUString rURL = rURI.getPackage() + rURI.getParam();
     252             : 
     253           0 :     if ( m_pPackages )
     254             :     {
     255           0 :         Packages::const_iterator it = m_pPackages->find( rURL );
     256           0 :         if ( it != m_pPackages->end() )
     257             :         {
     258             :             // Already instanciated. Return package.
     259           0 :             return (*it).second->m_xNA;
     260             :         }
     261             :     }
     262             :     else
     263           0 :         m_pPackages = new Packages;
     264             : 
     265             :     // Create new package...
     266           0 :     uno::Sequence< uno::Any > aArguments( 1 );
     267           0 :     aArguments[ 0 ] <<= rURL;
     268           0 :     uno::Reference< container::XHierarchicalNameAccess > xNameAccess;
     269             :     try
     270             :     {
     271           0 :         xNameAccess = uno::Reference< container::XHierarchicalNameAccess >(
     272           0 :             m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
     273             :                 "com.sun.star.packages.comp.ZipPackage",
     274           0 :                 aArguments, m_xContext ),
     275           0 :             css::uno::UNO_QUERY_THROW );
     276             :     }
     277           0 :     catch ( uno::RuntimeException const & )
     278             :     {
     279           0 :         throw;
     280             :     }
     281           0 :     catch ( uno::Exception const & e )
     282             :     {
     283             :         throw css::lang::WrappedTargetRuntimeException(
     284           0 :             e.Message, e.Context, css::uno::makeAny(e));
     285             :     }
     286             : 
     287           0 :     rtl::Reference< Package> xPackage = new Package( rURL, xNameAccess, this );
     288           0 :     (*m_pPackages)[ rURL ] = xPackage.get();
     289           0 :     return xPackage.get();
     290             : }
     291             : 
     292             : 
     293           0 : sal_Bool ContentProvider::removePackage( const OUString & rName )
     294             : {
     295           0 :     osl::MutexGuard aGuard( m_aMutex );
     296             : 
     297           0 :     if ( m_pPackages )
     298             :     {
     299           0 :         Packages::iterator it = m_pPackages->find( rName );
     300           0 :         if ( it != m_pPackages->end() )
     301             :         {
     302           0 :             m_pPackages->erase( it );
     303           0 :             return sal_True;
     304             :         }
     305             :     }
     306           0 :     return sal_False;
     307             : }
     308             : 
     309             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10