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 17 : Package( const OUString& rName,
60 : const uno::Reference< container::XHierarchicalNameAccess > & xNA,
61 : ContentProvider* pOwner )
62 17 : : m_aName( rName ), m_xNA( xNA ), m_pOwner( pOwner ) {}
63 34 : virtual ~Package() { m_pOwner->removePackage( m_aName ); }
64 :
65 : // XInterface
66 : virtual uno::Any SAL_CALL
67 16 : queryInterface( const uno::Type& aType )
68 : throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE
69 16 : { return m_xNA->queryInterface( aType ); }
70 : virtual void SAL_CALL
71 84 : acquire() throw() SAL_OVERRIDE
72 84 : { OWeakObject::acquire(); }
73 : virtual void SAL_CALL
74 84 : release() throw() SAL_OVERRIDE
75 84 : { OWeakObject::release(); }
76 :
77 : // XHierarchicalNameAccess
78 : virtual uno::Any SAL_CALL
79 33 : getByHierarchicalName( const OUString& aName )
80 : throw( container::NoSuchElementException, uno::RuntimeException, std::exception ) SAL_OVERRIDE
81 33 : { return m_xNA->getByHierarchicalName( aName ); }
82 : virtual sal_Bool SAL_CALL
83 33 : hasByHierarchicalName( const OUString& aName )
84 : throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE
85 33 : { return m_xNA->hasByHierarchicalName( aName ); }
86 : };
87 :
88 :
89 :
90 : // Packages.
91 :
92 :
93 :
94 : struct equalString
95 : {
96 33 : bool operator()(
97 : const OUString& rKey1, const OUString& rKey2 ) const
98 : {
99 33 : return !!( rKey1 == rKey2 );
100 : }
101 : };
102 :
103 : struct hashString
104 : {
105 60 : size_t operator()( const OUString & rName ) const
106 : {
107 60 : 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 14 : class Packages : public PackageMap {};
121 :
122 : }
123 :
124 : using namespace package_ucp;
125 :
126 :
127 :
128 :
129 : // ContentProvider Implementation.
130 :
131 :
132 :
133 :
134 7 : ContentProvider::ContentProvider(
135 : const uno::Reference< uno::XComponentContext >& rxContext )
136 : : ::ucbhelper::ContentProviderImplHelper( rxContext ),
137 7 : m_pPackages( 0 )
138 : {
139 7 : }
140 :
141 :
142 : // virtual
143 21 : ContentProvider::~ContentProvider()
144 : {
145 7 : delete m_pPackages;
146 14 : }
147 :
148 : // XInterface methods.
149 678 : void SAL_CALL ContentProvider::acquire()
150 : throw()
151 : {
152 678 : OWeakObject::acquire();
153 678 : }
154 :
155 678 : void SAL_CALL ContentProvider::release()
156 : throw()
157 : {
158 678 : OWeakObject::release();
159 678 : }
160 :
161 56 : 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 56 : );
169 56 : 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 30 : 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 7 : ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
198 :
199 :
200 :
201 : // XContentProvider methods.
202 :
203 :
204 :
205 : // virtual
206 27 : 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 27 : if ( !Identifier.is() )
211 0 : return uno::Reference< ucb::XContent >();
212 :
213 27 : PackageUri aUri( Identifier->getContentIdentifier() );
214 27 : if ( !aUri.isValid() )
215 0 : throw ucb::IllegalIdentifierException();
216 :
217 : // Create a new identifier for the mormalized URL returned by
218 : // PackageUri::getUri().
219 54 : uno::Reference< ucb::XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aUri.getUri() );
220 :
221 54 : osl::MutexGuard aGuard( m_aMutex );
222 :
223 : // Check, if a content with given id already exists...
224 : uno::Reference< ucb::XContent > xContent
225 54 : = queryExistingContent( xId ).get();
226 27 : if ( xContent.is() )
227 2 : return xContent;
228 :
229 : // Create a new content.
230 :
231 25 : xContent = Content::create( m_xContext, this, Identifier ); // not xId!!!
232 25 : registerNewContent( xContent );
233 :
234 25 : if ( xContent.is() && !xContent->getIdentifier().is() )
235 0 : throw ucb::IllegalIdentifierException();
236 :
237 52 : return xContent;
238 : }
239 :
240 :
241 :
242 : // Other methods.
243 :
244 :
245 :
246 : uno::Reference< container::XHierarchicalNameAccess >
247 33 : ContentProvider::createPackage( const PackageUri & rURI )
248 : {
249 33 : osl::MutexGuard aGuard( m_aMutex );
250 :
251 66 : OUString rURL = rURI.getPackage() + rURI.getParam();
252 :
253 33 : if ( m_pPackages )
254 : {
255 26 : Packages::const_iterator it = m_pPackages->find( rURL );
256 26 : if ( it != m_pPackages->end() )
257 : {
258 : // Already instanciated. Return package.
259 16 : return (*it).second->m_xNA;
260 : }
261 : }
262 : else
263 7 : m_pPackages = new Packages;
264 :
265 : // Create new package...
266 34 : uno::Sequence< uno::Any > aArguments( 1 );
267 17 : aArguments[ 0 ] <<= rURL;
268 34 : uno::Reference< container::XHierarchicalNameAccess > xNameAccess;
269 : try
270 : {
271 34 : xNameAccess = uno::Reference< container::XHierarchicalNameAccess >(
272 34 : m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
273 : "com.sun.star.packages.comp.ZipPackage",
274 17 : aArguments, m_xContext ),
275 17 : 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 34 : rtl::Reference< Package> xPackage = new Package( rURL, xNameAccess, this );
288 17 : (*m_pPackages)[ rURL ] = xPackage.get();
289 50 : return xPackage.get();
290 : }
291 :
292 :
293 17 : sal_Bool ContentProvider::removePackage( const OUString & rName )
294 : {
295 17 : osl::MutexGuard aGuard( m_aMutex );
296 :
297 17 : if ( m_pPackages )
298 : {
299 17 : Packages::iterator it = m_pPackages->find( rName );
300 17 : if ( it != m_pPackages->end() )
301 : {
302 17 : m_pPackages->erase( it );
303 17 : return sal_True;
304 : }
305 : }
306 0 : return sal_False;
307 : }
308 :
309 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|