Update ooo320-m1
[ooovba.git] / ucb / source / ucp / package / pkgprovider.cxx
blob434fe969e04d33b274d62f3b2ff19cd49ddea206
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pkgprovider.cxx,v $
10 * $Revision: 1.22 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include <hash_map>
41 #include <osl/diagnose.h>
42 #include <cppuhelper/weak.hxx>
43 #include <ucbhelper/contentidentifier.hxx>
44 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
45 #include "pkgprovider.hxx"
46 #include "pkgcontent.hxx"
47 #include "pkguri.hxx"
49 using namespace com::sun::star;
51 namespace package_ucp
54 //=========================================================================
56 // class Package.
58 //=========================================================================
60 class Package : public cppu::OWeakObject,
61 public container::XHierarchicalNameAccess
63 friend class ContentProvider;
65 rtl::OUString m_aName;
66 uno::Reference< container::XHierarchicalNameAccess > m_xNA;
67 ContentProvider* m_pOwner;
69 public:
70 Package( const rtl::OUString& rName,
71 const uno::Reference< container::XHierarchicalNameAccess > & xNA,
72 ContentProvider* pOwner )
73 : m_aName( rName ), m_xNA( xNA ), m_pOwner( pOwner ) {}
74 virtual ~Package() { m_pOwner->removePackage( m_aName ); }
76 // XInterface
77 virtual uno::Any SAL_CALL
78 queryInterface( const uno::Type& aType )
79 throw( uno::RuntimeException )
80 { return m_xNA->queryInterface( aType ); }
81 virtual void SAL_CALL
82 acquire() throw()
83 { OWeakObject::acquire(); }
84 virtual void SAL_CALL
85 release() throw()
86 { OWeakObject::release(); }
88 // XHierarchicalNameAccess
89 virtual uno::Any SAL_CALL
90 getByHierarchicalName( const rtl::OUString& aName )
91 throw( container::NoSuchElementException, uno::RuntimeException )
92 { return m_xNA->getByHierarchicalName( aName ); }
93 virtual sal_Bool SAL_CALL
94 hasByHierarchicalName( const rtl::OUString& aName )
95 throw( uno::RuntimeException )
96 { return m_xNA->hasByHierarchicalName( aName ); }
99 //=========================================================================
101 // Packages.
103 //=========================================================================
105 struct equalString
107 bool operator()(
108 const rtl::OUString& rKey1, const rtl::OUString& rKey2 ) const
110 return !!( rKey1 == rKey2 );
114 struct hashString
116 size_t operator()( const rtl::OUString & rName ) const
118 return rName.hashCode();
122 typedef std::hash_map
124 rtl::OUString,
125 Package*,
126 hashString,
127 equalString
129 PackageMap;
131 class Packages : public PackageMap {};
135 using namespace package_ucp;
137 //=========================================================================
138 //=========================================================================
140 // ContentProvider Implementation.
142 //=========================================================================
143 //=========================================================================
145 ContentProvider::ContentProvider(
146 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
147 : ::ucbhelper::ContentProviderImplHelper( rSMgr ),
148 m_pPackages( 0 )
152 //=========================================================================
153 // virtual
154 ContentProvider::~ContentProvider()
156 delete m_pPackages;
159 //=========================================================================
161 // XInterface methods.
163 //=========================================================================
165 XINTERFACE_IMPL_3( ContentProvider,
166 lang::XTypeProvider,
167 lang::XServiceInfo,
168 ucb::XContentProvider );
170 //=========================================================================
172 // XTypeProvider methods.
174 //=========================================================================
176 XTYPEPROVIDER_IMPL_3( ContentProvider,
177 lang::XTypeProvider,
178 lang::XServiceInfo,
179 ucb::XContentProvider );
181 //=========================================================================
183 // XServiceInfo methods.
185 //=========================================================================
187 XSERVICEINFO_IMPL_1( ContentProvider,
188 rtl::OUString::createFromAscii(
189 "com.sun.star.comp.ucb.PackageContentProvider" ),
190 rtl::OUString::createFromAscii(
191 PACKAGE_CONTENT_PROVIDER_SERVICE_NAME ) );
193 //=========================================================================
195 // Service factory implementation.
197 //=========================================================================
199 ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
201 //=========================================================================
203 // XContentProvider methods.
205 //=========================================================================
207 // virtual
208 uno::Reference< ucb::XContent > SAL_CALL ContentProvider::queryContent(
209 const uno::Reference< ucb::XContentIdentifier >& Identifier )
210 throw( ucb::IllegalIdentifierException, uno::RuntimeException )
212 if ( !Identifier.is() )
213 return uno::Reference< ucb::XContent >();
215 PackageUri aUri( Identifier->getContentIdentifier() );
216 if ( !aUri.isValid() )
217 throw ucb::IllegalIdentifierException();
219 // Create a new identifier for the mormalized URL returned by
220 // PackageUri::getUri().
221 uno::Reference< ucb::XContentIdentifier > xId
222 = new ::ucbhelper::ContentIdentifier( m_xSMgr, aUri.getUri() );
224 osl::MutexGuard aGuard( m_aMutex );
226 // Check, if a content with given id already exists...
227 uno::Reference< ucb::XContent > xContent
228 = queryExistingContent( xId ).get();
229 if ( xContent.is() )
230 return xContent;
232 // Create a new content.
234 xContent = Content::create( m_xSMgr, this, Identifier ); // not xId!!!
235 registerNewContent( xContent );
237 if ( xContent.is() && !xContent->getIdentifier().is() )
238 throw ucb::IllegalIdentifierException();
240 return xContent;
243 //=========================================================================
245 // Other methods.
247 //=========================================================================
249 uno::Reference< container::XHierarchicalNameAccess >
250 ContentProvider::createPackage( const rtl::OUString & rName, const rtl::OUString & rParam )
252 osl::MutexGuard aGuard( m_aMutex );
254 if ( !rName.getLength() )
256 OSL_ENSURE( sal_False,
257 "ContentProvider::createPackage - Invalid URL!" );
258 return uno::Reference< container::XHierarchicalNameAccess >();
261 rtl::OUString rURL = rName + rParam;
263 if ( m_pPackages )
265 Packages::const_iterator it = m_pPackages->find( rURL );
266 if ( it != m_pPackages->end() )
268 // Already instanciated. Return package.
269 return (*it).second->m_xNA;
272 else
273 m_pPackages = new Packages;
275 // Create new package...
278 uno::Sequence< uno::Any > aArguments( 1 );
279 aArguments[ 0 ] <<= rURL;
281 uno::Reference< uno::XInterface > xIfc
282 = m_xSMgr->createInstanceWithArguments(
283 rtl::OUString::createFromAscii(
284 "com.sun.star.packages.comp.ZipPackage" ),
285 aArguments );
287 if ( xIfc.is() )
289 uno::Reference<
290 container::XHierarchicalNameAccess > xNameAccess(
291 xIfc, uno::UNO_QUERY );
293 OSL_ENSURE( xNameAccess.is(),
294 "ContentProvider::createPackage - "
295 "Got no hierarchical name access!" );
297 rtl::Reference< Package> xPackage
298 = new Package( rURL, xNameAccess, this );
300 (*m_pPackages)[ rURL ] = xPackage.get();
302 return xPackage.get();
305 catch ( uno::RuntimeException const & )
307 // createInstanceWithArguemts
309 catch ( uno::Exception const & )
311 // createInstanceWithArguemts
314 return uno::Reference< container::XHierarchicalNameAccess >();
317 //=========================================================================
318 sal_Bool ContentProvider::removePackage( const rtl::OUString & rName )
320 osl::MutexGuard aGuard( m_aMutex );
322 if ( m_pPackages )
324 Packages::iterator it = m_pPackages->find( rName );
325 if ( it != m_pPackages->end() )
327 m_pPackages->erase( it );
328 return sal_True;
331 return sal_False;