12f86921cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 32f86921cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 42f86921cSAndrew Rist * or more contributor license agreements. See the NOTICE file 52f86921cSAndrew Rist * distributed with this work for additional information 62f86921cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 72f86921cSAndrew Rist * to you under the Apache License, Version 2.0 (the 82f86921cSAndrew Rist * "License"); you may not use this file except in compliance 92f86921cSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 112f86921cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 132f86921cSAndrew Rist * Unless required by applicable law or agreed to in writing, 142f86921cSAndrew Rist * software distributed under the License is distributed on an 152f86921cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 162f86921cSAndrew Rist * KIND, either express or implied. See the License for the 172f86921cSAndrew Rist * specific language governing permissions and limitations 182f86921cSAndrew Rist * under the License. 19cdf0e10cSrcweir * 202f86921cSAndrew Rist *************************************************************/ 212f86921cSAndrew Rist 222f86921cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25421ed02eSdamjan #include "precompiled_pkg.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir /************************************************************************** 28cdf0e10cSrcweir TODO 29cdf0e10cSrcweir ************************************************************************** 30cdf0e10cSrcweir 31cdf0e10cSrcweir *************************************************************************/ 32cdf0e10cSrcweir 33cdf0e10cSrcweir #include <hash_map> 34cdf0e10cSrcweir #include <osl/diagnose.h> 35cdf0e10cSrcweir #include <cppuhelper/weak.hxx> 36cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx> 37cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp> 38cdf0e10cSrcweir #include "pkgprovider.hxx" 39cdf0e10cSrcweir #include "pkgcontent.hxx" 40cdf0e10cSrcweir #include "pkguri.hxx" 41cdf0e10cSrcweir 42cdf0e10cSrcweir using namespace com::sun::star; 43cdf0e10cSrcweir 44cdf0e10cSrcweir namespace package_ucp 45cdf0e10cSrcweir { 46cdf0e10cSrcweir 47cdf0e10cSrcweir //========================================================================= 48cdf0e10cSrcweir // 49cdf0e10cSrcweir // class Package. 50cdf0e10cSrcweir // 51cdf0e10cSrcweir //========================================================================= 52cdf0e10cSrcweir 53cdf0e10cSrcweir class Package : public cppu::OWeakObject, 54cdf0e10cSrcweir public container::XHierarchicalNameAccess 55cdf0e10cSrcweir { 56cdf0e10cSrcweir friend class ContentProvider; 57cdf0e10cSrcweir 58cdf0e10cSrcweir rtl::OUString m_aName; 59cdf0e10cSrcweir uno::Reference< container::XHierarchicalNameAccess > m_xNA; 60cdf0e10cSrcweir ContentProvider* m_pOwner; 61cdf0e10cSrcweir 62cdf0e10cSrcweir public: 63cdf0e10cSrcweir Package( const rtl::OUString& rName, 64cdf0e10cSrcweir const uno::Reference< container::XHierarchicalNameAccess > & xNA, 65cdf0e10cSrcweir ContentProvider* pOwner ) 66cdf0e10cSrcweir : m_aName( rName ), m_xNA( xNA ), m_pOwner( pOwner ) {} 67cdf0e10cSrcweir virtual ~Package() { m_pOwner->removePackage( m_aName ); } 68cdf0e10cSrcweir 69cdf0e10cSrcweir // XInterface 70cdf0e10cSrcweir virtual uno::Any SAL_CALL 71cdf0e10cSrcweir queryInterface( const uno::Type& aType ) 72cdf0e10cSrcweir throw( uno::RuntimeException ) 73cdf0e10cSrcweir { return m_xNA->queryInterface( aType ); } 74cdf0e10cSrcweir virtual void SAL_CALL 75cdf0e10cSrcweir acquire() throw() 76cdf0e10cSrcweir { OWeakObject::acquire(); } 77cdf0e10cSrcweir virtual void SAL_CALL 78cdf0e10cSrcweir release() throw() 79cdf0e10cSrcweir { OWeakObject::release(); } 80cdf0e10cSrcweir 81cdf0e10cSrcweir // XHierarchicalNameAccess 82cdf0e10cSrcweir virtual uno::Any SAL_CALL 83cdf0e10cSrcweir getByHierarchicalName( const rtl::OUString& aName ) 84cdf0e10cSrcweir throw( container::NoSuchElementException, uno::RuntimeException ) 85cdf0e10cSrcweir { return m_xNA->getByHierarchicalName( aName ); } 86cdf0e10cSrcweir virtual sal_Bool SAL_CALL 87cdf0e10cSrcweir hasByHierarchicalName( const rtl::OUString& aName ) 88cdf0e10cSrcweir throw( uno::RuntimeException ) 89cdf0e10cSrcweir { return m_xNA->hasByHierarchicalName( aName ); } 90cdf0e10cSrcweir }; 91cdf0e10cSrcweir 92cdf0e10cSrcweir //========================================================================= 93cdf0e10cSrcweir // 94cdf0e10cSrcweir // Packages. 95cdf0e10cSrcweir // 96cdf0e10cSrcweir //========================================================================= 97cdf0e10cSrcweir 98cdf0e10cSrcweir struct equalString 99cdf0e10cSrcweir { 100cdf0e10cSrcweir bool operator()( 101cdf0e10cSrcweir const rtl::OUString& rKey1, const rtl::OUString& rKey2 ) const 102cdf0e10cSrcweir { 103cdf0e10cSrcweir return !!( rKey1 == rKey2 ); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir }; 106cdf0e10cSrcweir 107cdf0e10cSrcweir struct hashString 108cdf0e10cSrcweir { 109cdf0e10cSrcweir size_t operator()( const rtl::OUString & rName ) const 110cdf0e10cSrcweir { 111cdf0e10cSrcweir return rName.hashCode(); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir }; 114cdf0e10cSrcweir 115cdf0e10cSrcweir typedef std::hash_map 116cdf0e10cSrcweir < 117cdf0e10cSrcweir rtl::OUString, 118cdf0e10cSrcweir Package*, 119cdf0e10cSrcweir hashString, 120cdf0e10cSrcweir equalString 121cdf0e10cSrcweir > 122cdf0e10cSrcweir PackageMap; 123cdf0e10cSrcweir 124cdf0e10cSrcweir class Packages : public PackageMap {}; 125cdf0e10cSrcweir 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir using namespace package_ucp; 129cdf0e10cSrcweir 130cdf0e10cSrcweir //========================================================================= 131cdf0e10cSrcweir //========================================================================= 132cdf0e10cSrcweir // 133cdf0e10cSrcweir // ContentProvider Implementation. 134cdf0e10cSrcweir // 135cdf0e10cSrcweir //========================================================================= 136cdf0e10cSrcweir //========================================================================= 137cdf0e10cSrcweir 138cdf0e10cSrcweir ContentProvider::ContentProvider( 139cdf0e10cSrcweir const uno::Reference< lang::XMultiServiceFactory >& rSMgr ) 140cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper( rSMgr ), 141cdf0e10cSrcweir m_pPackages( 0 ) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir } 144cdf0e10cSrcweir 145cdf0e10cSrcweir //========================================================================= 146cdf0e10cSrcweir // virtual 147cdf0e10cSrcweir ContentProvider::~ContentProvider() 148cdf0e10cSrcweir { 149cdf0e10cSrcweir delete m_pPackages; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir 152cdf0e10cSrcweir //========================================================================= 153cdf0e10cSrcweir // 154cdf0e10cSrcweir // XInterface methods. 155cdf0e10cSrcweir // 156cdf0e10cSrcweir //========================================================================= 157cdf0e10cSrcweir 158cdf0e10cSrcweir XINTERFACE_IMPL_3( ContentProvider, 159cdf0e10cSrcweir lang::XTypeProvider, 160cdf0e10cSrcweir lang::XServiceInfo, 161cdf0e10cSrcweir ucb::XContentProvider ); 162cdf0e10cSrcweir 163cdf0e10cSrcweir //========================================================================= 164cdf0e10cSrcweir // 165cdf0e10cSrcweir // XTypeProvider methods. 166cdf0e10cSrcweir // 167cdf0e10cSrcweir //========================================================================= 168cdf0e10cSrcweir 169cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( ContentProvider, 170cdf0e10cSrcweir lang::XTypeProvider, 171cdf0e10cSrcweir lang::XServiceInfo, 172cdf0e10cSrcweir ucb::XContentProvider ); 173cdf0e10cSrcweir 174cdf0e10cSrcweir //========================================================================= 175cdf0e10cSrcweir // 176cdf0e10cSrcweir // XServiceInfo methods. 177cdf0e10cSrcweir // 178cdf0e10cSrcweir //========================================================================= 179cdf0e10cSrcweir 180cdf0e10cSrcweir XSERVICEINFO_IMPL_1( ContentProvider, 181cdf0e10cSrcweir rtl::OUString::createFromAscii( 182cdf0e10cSrcweir "com.sun.star.comp.ucb.PackageContentProvider" ), 183cdf0e10cSrcweir rtl::OUString::createFromAscii( 184cdf0e10cSrcweir PACKAGE_CONTENT_PROVIDER_SERVICE_NAME ) ); 185cdf0e10cSrcweir 186cdf0e10cSrcweir //========================================================================= 187cdf0e10cSrcweir // 188cdf0e10cSrcweir // Service factory implementation. 189cdf0e10cSrcweir // 190cdf0e10cSrcweir //========================================================================= 191cdf0e10cSrcweir 192cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider ); 193cdf0e10cSrcweir 194cdf0e10cSrcweir //========================================================================= 195cdf0e10cSrcweir // 196cdf0e10cSrcweir // XContentProvider methods. 197cdf0e10cSrcweir // 198cdf0e10cSrcweir //========================================================================= 199cdf0e10cSrcweir 200cdf0e10cSrcweir // virtual 201cdf0e10cSrcweir uno::Reference< ucb::XContent > SAL_CALL ContentProvider::queryContent( 202cdf0e10cSrcweir const uno::Reference< ucb::XContentIdentifier >& Identifier ) 203cdf0e10cSrcweir throw( ucb::IllegalIdentifierException, uno::RuntimeException ) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir if ( !Identifier.is() ) 206cdf0e10cSrcweir return uno::Reference< ucb::XContent >(); 207cdf0e10cSrcweir 208cdf0e10cSrcweir PackageUri aUri( Identifier->getContentIdentifier() ); 209cdf0e10cSrcweir if ( !aUri.isValid() ) 210cdf0e10cSrcweir throw ucb::IllegalIdentifierException(); 211cdf0e10cSrcweir 212cdf0e10cSrcweir // Create a new identifier for the mormalized URL returned by 213cdf0e10cSrcweir // PackageUri::getUri(). 214cdf0e10cSrcweir uno::Reference< ucb::XContentIdentifier > xId 215cdf0e10cSrcweir = new ::ucbhelper::ContentIdentifier( m_xSMgr, aUri.getUri() ); 216cdf0e10cSrcweir 217cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 218cdf0e10cSrcweir 219cdf0e10cSrcweir // Check, if a content with given id already exists... 220cdf0e10cSrcweir uno::Reference< ucb::XContent > xContent 221cdf0e10cSrcweir = queryExistingContent( xId ).get(); 222cdf0e10cSrcweir if ( xContent.is() ) 223cdf0e10cSrcweir return xContent; 224cdf0e10cSrcweir 225cdf0e10cSrcweir // Create a new content. 226cdf0e10cSrcweir 227cdf0e10cSrcweir xContent = Content::create( m_xSMgr, this, Identifier ); // not xId!!! 228cdf0e10cSrcweir registerNewContent( xContent ); 229cdf0e10cSrcweir 230cdf0e10cSrcweir if ( xContent.is() && !xContent->getIdentifier().is() ) 231cdf0e10cSrcweir throw ucb::IllegalIdentifierException(); 232cdf0e10cSrcweir 233cdf0e10cSrcweir return xContent; 234cdf0e10cSrcweir } 235cdf0e10cSrcweir 236cdf0e10cSrcweir //========================================================================= 237cdf0e10cSrcweir // 238cdf0e10cSrcweir // Other methods. 239cdf0e10cSrcweir // 240cdf0e10cSrcweir //========================================================================= 241cdf0e10cSrcweir 242cdf0e10cSrcweir uno::Reference< container::XHierarchicalNameAccess > 243cdf0e10cSrcweir ContentProvider::createPackage( const rtl::OUString & rName, const rtl::OUString & rParam ) 244cdf0e10cSrcweir { 245cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 246cdf0e10cSrcweir 247cdf0e10cSrcweir if ( !rName.getLength() ) 248cdf0e10cSrcweir { 249cdf0e10cSrcweir OSL_ENSURE( sal_False, 250cdf0e10cSrcweir "ContentProvider::createPackage - Invalid URL!" ); 251cdf0e10cSrcweir return uno::Reference< container::XHierarchicalNameAccess >(); 252cdf0e10cSrcweir } 253cdf0e10cSrcweir 254cdf0e10cSrcweir rtl::OUString rURL = rName + rParam; 255cdf0e10cSrcweir 256cdf0e10cSrcweir if ( m_pPackages ) 257cdf0e10cSrcweir { 258cdf0e10cSrcweir Packages::const_iterator it = m_pPackages->find( rURL ); 259cdf0e10cSrcweir if ( it != m_pPackages->end() ) 260cdf0e10cSrcweir { 261*5e7dbebbSJohn Bampton // Already instantiated. Return package. 262cdf0e10cSrcweir return (*it).second->m_xNA; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir } 265cdf0e10cSrcweir else 266cdf0e10cSrcweir m_pPackages = new Packages; 267cdf0e10cSrcweir 268cdf0e10cSrcweir // Create new package... 269cdf0e10cSrcweir try 270cdf0e10cSrcweir { 271cdf0e10cSrcweir uno::Sequence< uno::Any > aArguments( 1 ); 272cdf0e10cSrcweir aArguments[ 0 ] <<= rURL; 273cdf0e10cSrcweir 274cdf0e10cSrcweir uno::Reference< uno::XInterface > xIfc 275cdf0e10cSrcweir = m_xSMgr->createInstanceWithArguments( 276cdf0e10cSrcweir rtl::OUString::createFromAscii( 277cdf0e10cSrcweir "com.sun.star.packages.comp.ZipPackage" ), 278cdf0e10cSrcweir aArguments ); 279cdf0e10cSrcweir 280cdf0e10cSrcweir if ( xIfc.is() ) 281cdf0e10cSrcweir { 282cdf0e10cSrcweir uno::Reference< 283cdf0e10cSrcweir container::XHierarchicalNameAccess > xNameAccess( 284cdf0e10cSrcweir xIfc, uno::UNO_QUERY ); 285cdf0e10cSrcweir 286cdf0e10cSrcweir OSL_ENSURE( xNameAccess.is(), 287cdf0e10cSrcweir "ContentProvider::createPackage - " 288cdf0e10cSrcweir "Got no hierarchical name access!" ); 289cdf0e10cSrcweir 290cdf0e10cSrcweir rtl::Reference< Package> xPackage 291cdf0e10cSrcweir = new Package( rURL, xNameAccess, this ); 292cdf0e10cSrcweir 293cdf0e10cSrcweir (*m_pPackages)[ rURL ] = xPackage.get(); 294cdf0e10cSrcweir 295cdf0e10cSrcweir return xPackage.get(); 296cdf0e10cSrcweir } 297cdf0e10cSrcweir } 298cdf0e10cSrcweir catch ( uno::RuntimeException const & ) 299cdf0e10cSrcweir { 300cdf0e10cSrcweir // createInstanceWithArguemts 301cdf0e10cSrcweir } 302cdf0e10cSrcweir catch ( uno::Exception const & ) 303cdf0e10cSrcweir { 304cdf0e10cSrcweir // createInstanceWithArguemts 305cdf0e10cSrcweir } 306cdf0e10cSrcweir 307cdf0e10cSrcweir return uno::Reference< container::XHierarchicalNameAccess >(); 308cdf0e10cSrcweir } 309cdf0e10cSrcweir 310cdf0e10cSrcweir //========================================================================= 311cdf0e10cSrcweir sal_Bool ContentProvider::removePackage( const rtl::OUString & rName ) 312cdf0e10cSrcweir { 313cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 314cdf0e10cSrcweir 315cdf0e10cSrcweir if ( m_pPackages ) 316cdf0e10cSrcweir { 317cdf0e10cSrcweir Packages::iterator it = m_pPackages->find( rName ); 318cdf0e10cSrcweir if ( it != m_pPackages->end() ) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir m_pPackages->erase( it ); 321cdf0e10cSrcweir return sal_True; 322cdf0e10cSrcweir } 323cdf0e10cSrcweir } 324cdf0e10cSrcweir return sal_False; 325cdf0e10cSrcweir } 326