xref: /AOO41X/main/ucb/source/ucp/package/pkgprovider.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucb.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /**************************************************************************
32*cdf0e10cSrcweir                                 TODO
33*cdf0e10cSrcweir  **************************************************************************
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir  *************************************************************************/
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <hash_map>
38*cdf0e10cSrcweir #include <osl/diagnose.h>
39*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
40*cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx>
41*cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
42*cdf0e10cSrcweir #include "pkgprovider.hxx"
43*cdf0e10cSrcweir #include "pkgcontent.hxx"
44*cdf0e10cSrcweir #include "pkguri.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace com::sun::star;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace package_ucp
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //=========================================================================
52*cdf0e10cSrcweir //
53*cdf0e10cSrcweir // class Package.
54*cdf0e10cSrcweir //
55*cdf0e10cSrcweir //=========================================================================
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir class Package : public cppu::OWeakObject,
58*cdf0e10cSrcweir                 public container::XHierarchicalNameAccess
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     friend class ContentProvider;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     rtl::OUString                                        m_aName;
63*cdf0e10cSrcweir     uno::Reference< container::XHierarchicalNameAccess > m_xNA;
64*cdf0e10cSrcweir     ContentProvider*                                     m_pOwner;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir public:
67*cdf0e10cSrcweir     Package( const rtl::OUString& rName,
68*cdf0e10cSrcweir              const uno::Reference< container::XHierarchicalNameAccess > & xNA,
69*cdf0e10cSrcweir              ContentProvider* pOwner )
70*cdf0e10cSrcweir     : m_aName( rName ), m_xNA( xNA ), m_pOwner( pOwner ) {}
71*cdf0e10cSrcweir     virtual ~Package() { m_pOwner->removePackage( m_aName ); }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     // XInterface
74*cdf0e10cSrcweir     virtual uno::Any SAL_CALL
75*cdf0e10cSrcweir     queryInterface( const uno::Type& aType )
76*cdf0e10cSrcweir         throw( uno::RuntimeException )
77*cdf0e10cSrcweir     { return m_xNA->queryInterface( aType ); }
78*cdf0e10cSrcweir     virtual void SAL_CALL
79*cdf0e10cSrcweir     acquire() throw()
80*cdf0e10cSrcweir     { OWeakObject::acquire(); }
81*cdf0e10cSrcweir     virtual void SAL_CALL
82*cdf0e10cSrcweir     release() throw()
83*cdf0e10cSrcweir     { OWeakObject::release(); }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     // XHierarchicalNameAccess
86*cdf0e10cSrcweir     virtual uno::Any SAL_CALL
87*cdf0e10cSrcweir     getByHierarchicalName( const rtl::OUString& aName )
88*cdf0e10cSrcweir         throw( container::NoSuchElementException, uno::RuntimeException )
89*cdf0e10cSrcweir     { return m_xNA->getByHierarchicalName( aName ); }
90*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL
91*cdf0e10cSrcweir     hasByHierarchicalName( const rtl::OUString& aName )
92*cdf0e10cSrcweir         throw( uno::RuntimeException )
93*cdf0e10cSrcweir     { return m_xNA->hasByHierarchicalName( aName ); }
94*cdf0e10cSrcweir };
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir //=========================================================================
97*cdf0e10cSrcweir //
98*cdf0e10cSrcweir // Packages.
99*cdf0e10cSrcweir //
100*cdf0e10cSrcweir //=========================================================================
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir struct equalString
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir     bool operator()(
105*cdf0e10cSrcweir         const rtl::OUString& rKey1, const rtl::OUString& rKey2 ) const
106*cdf0e10cSrcweir     {
107*cdf0e10cSrcweir         return !!( rKey1 == rKey2 );
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir };
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir struct hashString
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir     size_t operator()( const rtl::OUString & rName ) const
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         return rName.hashCode();
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir };
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir typedef std::hash_map
120*cdf0e10cSrcweir <
121*cdf0e10cSrcweir     rtl::OUString,
122*cdf0e10cSrcweir     Package*,
123*cdf0e10cSrcweir     hashString,
124*cdf0e10cSrcweir     equalString
125*cdf0e10cSrcweir >
126*cdf0e10cSrcweir PackageMap;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir class Packages : public PackageMap {};
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir using namespace package_ucp;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir //=========================================================================
135*cdf0e10cSrcweir //=========================================================================
136*cdf0e10cSrcweir //
137*cdf0e10cSrcweir // ContentProvider Implementation.
138*cdf0e10cSrcweir //
139*cdf0e10cSrcweir //=========================================================================
140*cdf0e10cSrcweir //=========================================================================
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir ContentProvider::ContentProvider(
143*cdf0e10cSrcweir             const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
144*cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper( rSMgr ),
145*cdf0e10cSrcweir   m_pPackages( 0 )
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir //=========================================================================
150*cdf0e10cSrcweir // virtual
151*cdf0e10cSrcweir ContentProvider::~ContentProvider()
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir     delete m_pPackages;
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir //=========================================================================
157*cdf0e10cSrcweir //
158*cdf0e10cSrcweir // XInterface methods.
159*cdf0e10cSrcweir //
160*cdf0e10cSrcweir //=========================================================================
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir XINTERFACE_IMPL_3( ContentProvider,
163*cdf0e10cSrcweir                    lang::XTypeProvider,
164*cdf0e10cSrcweir                    lang::XServiceInfo,
165*cdf0e10cSrcweir                    ucb::XContentProvider );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir //=========================================================================
168*cdf0e10cSrcweir //
169*cdf0e10cSrcweir // XTypeProvider methods.
170*cdf0e10cSrcweir //
171*cdf0e10cSrcweir //=========================================================================
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( ContentProvider,
174*cdf0e10cSrcweir                       lang::XTypeProvider,
175*cdf0e10cSrcweir                       lang::XServiceInfo,
176*cdf0e10cSrcweir                       ucb::XContentProvider );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir //=========================================================================
179*cdf0e10cSrcweir //
180*cdf0e10cSrcweir // XServiceInfo methods.
181*cdf0e10cSrcweir //
182*cdf0e10cSrcweir //=========================================================================
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir XSERVICEINFO_IMPL_1( ContentProvider,
185*cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
186*cdf0e10cSrcweir                         "com.sun.star.comp.ucb.PackageContentProvider" ),
187*cdf0e10cSrcweir                      rtl::OUString::createFromAscii(
188*cdf0e10cSrcweir                         PACKAGE_CONTENT_PROVIDER_SERVICE_NAME ) );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir //=========================================================================
191*cdf0e10cSrcweir //
192*cdf0e10cSrcweir // Service factory implementation.
193*cdf0e10cSrcweir //
194*cdf0e10cSrcweir //=========================================================================
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir //=========================================================================
199*cdf0e10cSrcweir //
200*cdf0e10cSrcweir // XContentProvider methods.
201*cdf0e10cSrcweir //
202*cdf0e10cSrcweir //=========================================================================
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir // virtual
205*cdf0e10cSrcweir uno::Reference< ucb::XContent > SAL_CALL ContentProvider::queryContent(
206*cdf0e10cSrcweir             const uno::Reference< ucb::XContentIdentifier >& Identifier )
207*cdf0e10cSrcweir     throw( ucb::IllegalIdentifierException, uno::RuntimeException )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     if ( !Identifier.is() )
210*cdf0e10cSrcweir         return uno::Reference< ucb::XContent >();
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     PackageUri aUri( Identifier->getContentIdentifier() );
213*cdf0e10cSrcweir     if ( !aUri.isValid() )
214*cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     // Create a new identifier for the mormalized URL returned by
217*cdf0e10cSrcweir     // PackageUri::getUri().
218*cdf0e10cSrcweir     uno::Reference< ucb::XContentIdentifier > xId
219*cdf0e10cSrcweir                 = new ::ucbhelper::ContentIdentifier( m_xSMgr, aUri.getUri() );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aMutex );
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     // Check, if a content with given id already exists...
224*cdf0e10cSrcweir     uno::Reference< ucb::XContent > xContent
225*cdf0e10cSrcweir         = queryExistingContent( xId ).get();
226*cdf0e10cSrcweir     if ( xContent.is() )
227*cdf0e10cSrcweir         return xContent;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     // Create a new content.
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     xContent = Content::create( m_xSMgr, this, Identifier ); // not xId!!!
232*cdf0e10cSrcweir     registerNewContent( xContent );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir     if ( xContent.is() && !xContent->getIdentifier().is() )
235*cdf0e10cSrcweir         throw ucb::IllegalIdentifierException();
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     return xContent;
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir //=========================================================================
241*cdf0e10cSrcweir //
242*cdf0e10cSrcweir // Other methods.
243*cdf0e10cSrcweir //
244*cdf0e10cSrcweir //=========================================================================
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir uno::Reference< container::XHierarchicalNameAccess >
247*cdf0e10cSrcweir ContentProvider::createPackage( const rtl::OUString & rName, const rtl::OUString & rParam )
248*cdf0e10cSrcweir {
249*cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aMutex );
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     if ( !rName.getLength() )
252*cdf0e10cSrcweir     {
253*cdf0e10cSrcweir         OSL_ENSURE( sal_False,
254*cdf0e10cSrcweir                     "ContentProvider::createPackage - Invalid URL!" );
255*cdf0e10cSrcweir         return uno::Reference< container::XHierarchicalNameAccess >();
256*cdf0e10cSrcweir     }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 	rtl::OUString rURL = rName + rParam;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     if ( m_pPackages )
261*cdf0e10cSrcweir     {
262*cdf0e10cSrcweir         Packages::const_iterator it = m_pPackages->find( rURL );
263*cdf0e10cSrcweir         if ( it != m_pPackages->end() )
264*cdf0e10cSrcweir         {
265*cdf0e10cSrcweir             // Already instanciated. Return package.
266*cdf0e10cSrcweir             return (*it).second->m_xNA;
267*cdf0e10cSrcweir         }
268*cdf0e10cSrcweir     }
269*cdf0e10cSrcweir     else
270*cdf0e10cSrcweir         m_pPackages = new Packages;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir     // Create new package...
273*cdf0e10cSrcweir     try
274*cdf0e10cSrcweir     {
275*cdf0e10cSrcweir         uno::Sequence< uno::Any > aArguments( 1 );
276*cdf0e10cSrcweir         aArguments[ 0 ] <<= rURL;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         uno::Reference< uno::XInterface > xIfc
279*cdf0e10cSrcweir             = m_xSMgr->createInstanceWithArguments(
280*cdf0e10cSrcweir                 rtl::OUString::createFromAscii(
281*cdf0e10cSrcweir                                 "com.sun.star.packages.comp.ZipPackage" ),
282*cdf0e10cSrcweir                 aArguments );
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir         if ( xIfc.is() )
285*cdf0e10cSrcweir         {
286*cdf0e10cSrcweir             uno::Reference<
287*cdf0e10cSrcweir                 container::XHierarchicalNameAccess > xNameAccess(
288*cdf0e10cSrcweir                                                         xIfc, uno::UNO_QUERY );
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir             OSL_ENSURE( xNameAccess.is(),
291*cdf0e10cSrcweir                         "ContentProvider::createPackage - "
292*cdf0e10cSrcweir                         "Got no hierarchical name access!" );
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir             rtl::Reference< Package> xPackage
295*cdf0e10cSrcweir                 = new Package( rURL, xNameAccess, this );
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir             (*m_pPackages)[ rURL ] = xPackage.get();
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir             return xPackage.get();
300*cdf0e10cSrcweir         }
301*cdf0e10cSrcweir     }
302*cdf0e10cSrcweir     catch ( uno::RuntimeException const & )
303*cdf0e10cSrcweir     {
304*cdf0e10cSrcweir         // createInstanceWithArguemts
305*cdf0e10cSrcweir     }
306*cdf0e10cSrcweir     catch ( uno::Exception const & )
307*cdf0e10cSrcweir     {
308*cdf0e10cSrcweir         // createInstanceWithArguemts
309*cdf0e10cSrcweir     }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir     return uno::Reference< container::XHierarchicalNameAccess >();
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir //=========================================================================
315*cdf0e10cSrcweir sal_Bool ContentProvider::removePackage( const rtl::OUString & rName )
316*cdf0e10cSrcweir {
317*cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aMutex );
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir     if ( m_pPackages )
320*cdf0e10cSrcweir     {
321*cdf0e10cSrcweir         Packages::iterator it = m_pPackages->find( rName );
322*cdf0e10cSrcweir         if ( it != m_pPackages->end() )
323*cdf0e10cSrcweir         {
324*cdf0e10cSrcweir             m_pPackages->erase( it );
325*cdf0e10cSrcweir             return sal_True;
326*cdf0e10cSrcweir         }
327*cdf0e10cSrcweir     }
328*cdf0e10cSrcweir     return sal_False;
329*cdf0e10cSrcweir }
330*cdf0e10cSrcweir 
331