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 : #include "dp_sfwk.hrc"
21 : #include "dp_backend.h"
22 : #include "dp_ucb.h"
23 : #include "dp_parceldesc.hxx"
24 : #include "rtl/uri.hxx"
25 : #include "ucbhelper/content.hxx"
26 : #include "cppuhelper/exc_hlp.hxx"
27 : #include "comphelper/servicedecl.hxx"
28 : #include "svl/inettype.hxx"
29 : #include <com/sun/star/container/XNameContainer.hpp>
30 : #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
31 : #include <com/sun/star/xml/sax/Parser.hpp>
32 :
33 :
34 : using namespace ::dp_misc;
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::ucb;
38 : using namespace ::com::sun::star::script;
39 :
40 :
41 : namespace dp_registry
42 : {
43 : namespace backend
44 : {
45 : namespace sfwk
46 : {
47 :
48 :
49 0 : class BackendImpl : public ::dp_registry::backend::PackageRegistryBackend
50 : {
51 0 : class PackageImpl : public ::dp_registry::backend::Package
52 : {
53 : BackendImpl * getMyBackend() const;
54 :
55 : Reference< container::XNameContainer > m_xNameCntrPkgHandler;
56 : OUString m_descr;
57 :
58 : void initPackageHandler();
59 :
60 : // Package
61 : virtual beans::Optional< beans::Ambiguous<sal_Bool> > isRegistered_(
62 : ::osl::ResettableMutexGuard & guard,
63 : ::rtl::Reference<AbortChannel> const & abortChannel,
64 : Reference<XCommandEnvironment> const & xCmdEnv ) SAL_OVERRIDE;
65 : virtual void processPackage_(
66 : ::osl::ResettableMutexGuard & guard,
67 : bool registerPackage,
68 : bool startup,
69 : ::rtl::Reference<AbortChannel> const & abortChannel,
70 : Reference<XCommandEnvironment> const & xCmdEnv ) SAL_OVERRIDE;
71 :
72 : public:
73 : PackageImpl(
74 : ::rtl::Reference<BackendImpl> const & myBackend,
75 : OUString const & url, OUString const & libType, bool bRemoved,
76 : OUString const & identifier);
77 : // XPackage
78 : virtual OUString SAL_CALL getDescription() throw (RuntimeException, std::exception) SAL_OVERRIDE;
79 : virtual OUString SAL_CALL getLicenseText() throw (RuntimeException, std::exception) SAL_OVERRIDE;
80 : };
81 : friend class PackageImpl;
82 :
83 : // PackageRegistryBackend
84 : virtual Reference<deployment::XPackage> bindPackage_(
85 : OUString const & url, OUString const & mediaType,
86 : sal_Bool bRemoved, OUString const & identifier,
87 : Reference<XCommandEnvironment> const & xCmdEnv ) SAL_OVERRIDE;
88 :
89 : const Reference<deployment::XPackageTypeInfo> m_xTypeInfo;
90 :
91 :
92 : public:
93 : BackendImpl(
94 : Sequence<Any> const & args,
95 : Reference<XComponentContext> const & xComponentContext );
96 :
97 : // XPackageRegistry
98 : virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
99 : getSupportedPackageTypes() throw (RuntimeException, std::exception) SAL_OVERRIDE;
100 : virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType)
101 : throw (deployment::DeploymentException,
102 : uno::RuntimeException, std::exception) SAL_OVERRIDE;
103 : };
104 :
105 0 : BackendImpl * BackendImpl::PackageImpl::getMyBackend() const
106 : {
107 0 : BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
108 0 : if (NULL == pBackend)
109 : {
110 : //May throw a DisposedException
111 0 : check();
112 : //We should never get here...
113 : throw RuntimeException("Failed to get the BackendImpl",
114 0 : static_cast<OWeakObject*>(const_cast<PackageImpl *>(this)));
115 : }
116 0 : return pBackend;
117 : }
118 :
119 0 : OUString BackendImpl::PackageImpl::getDescription() throw (RuntimeException, std::exception)
120 : {
121 0 : if (m_descr.isEmpty())
122 0 : return Package::getDescription();
123 : else
124 0 : return m_descr;
125 : }
126 :
127 :
128 0 : OUString BackendImpl::PackageImpl::getLicenseText() throw (RuntimeException, std::exception)
129 : {
130 0 : return Package::getDescription();
131 : }
132 :
133 :
134 0 : BackendImpl::PackageImpl::PackageImpl(
135 : ::rtl::Reference<BackendImpl> const & myBackend,
136 : OUString const & url, OUString const & libType, bool bRemoved,
137 : OUString const & identifier)
138 0 : : Package( myBackend.get(), url, OUString(), OUString(),
139 0 : myBackend->m_xTypeInfo, bRemoved, identifier),
140 0 : m_descr(libType)
141 : {
142 0 : initPackageHandler();
143 :
144 0 : sal_Int32 segmEnd = url.getLength();
145 0 : if ( url.endsWith("/") )
146 0 : --segmEnd;
147 0 : sal_Int32 segmStart = (url.lastIndexOf( '/', segmEnd ) + 1);
148 0 : if (segmStart < 0)
149 0 : segmStart = 0;
150 : // name and display name default the same:
151 0 : m_displayName = ::rtl::Uri::decode(
152 : url.copy( segmStart, segmEnd - segmStart ),
153 0 : rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
154 0 : m_name = m_displayName;
155 :
156 0 : dp_misc::TRACE("PakageImpl displayName is " + m_displayName);
157 0 : }
158 :
159 :
160 0 : BackendImpl::BackendImpl(
161 : Sequence<Any> const & args,
162 : Reference<XComponentContext> const & xComponentContext )
163 : : PackageRegistryBackend( args, xComponentContext ),
164 : m_xTypeInfo( new Package::TypeInfo(
165 : "application/vnd.sun.star.framework-script",
166 : OUString() /* no file filter */,
167 : "Scripting Framework Script Library",
168 0 : RID_IMG_SCRIPTLIB ) )
169 : {
170 0 : if (! transientMode())
171 : {
172 : }
173 0 : }
174 :
175 :
176 :
177 : // XPackageRegistry
178 :
179 : Sequence< Reference<deployment::XPackageTypeInfo> >
180 0 : BackendImpl::getSupportedPackageTypes() throw (RuntimeException, std::exception)
181 : {
182 0 : return Sequence< Reference<deployment::XPackageTypeInfo> >(&m_xTypeInfo, 1);
183 : }
184 :
185 0 : void BackendImpl::packageRemoved(OUString const & /*url*/, OUString const & /*mediaType*/)
186 : throw (deployment::DeploymentException,
187 : uno::RuntimeException, std::exception)
188 : {
189 0 : }
190 :
191 : // PackageRegistryBackend
192 :
193 0 : Reference<deployment::XPackage> BackendImpl::bindPackage_(
194 : OUString const & url, OUString const & mediaType_, sal_Bool bRemoved,
195 : OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
196 : {
197 0 : OUString mediaType( mediaType_ );
198 0 : if (mediaType.isEmpty())
199 : {
200 : // detect media-type:
201 0 : ::ucbhelper::Content ucbContent;
202 0 : if (create_ucb_content( &ucbContent, url, xCmdEnv ) &&
203 0 : ucbContent.isFolder())
204 : {
205 : // probe for parcel-descriptor.xml:
206 0 : if (create_ucb_content(
207 : 0, makeURL( url, "parcel-descriptor.xml" ),
208 0 : xCmdEnv, false /* no throw */ ))
209 : {
210 0 : mediaType = "application/vnd.sun.star.framework-script";
211 : }
212 : }
213 0 : if (mediaType.isEmpty())
214 : throw lang::IllegalArgumentException(
215 0 : StrCannotDetectMediaType::get() + url,
216 0 : static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
217 : }
218 :
219 0 : OUString type, subType;
220 0 : INetContentTypeParameterList params;
221 0 : if (INetContentTypes::parse( mediaType, type, subType, ¶ms ))
222 : {
223 0 : if (type.equalsIgnoreAsciiCase("application"))
224 : {
225 0 : if (subType.equalsIgnoreAsciiCase("vnd.sun.star.framework-script"))
226 : {
227 0 : OUString lang = "Script";
228 : OUString sParcelDescURL = makeURL(
229 0 : url, "parcel-descriptor.xml" );
230 :
231 0 : ::ucbhelper::Content ucb_content;
232 :
233 0 : if (create_ucb_content( &ucb_content, sParcelDescURL,
234 : xCmdEnv, false /* no throw */ ))
235 : {
236 : ParcelDescDocHandler* pHandler =
237 0 : new ParcelDescDocHandler();
238 : Reference< xml::sax::XDocumentHandler >
239 0 : xDocHandler = pHandler;
240 :
241 : Reference<XComponentContext>
242 0 : xContext( getComponentContext() );
243 :
244 0 : Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(xContext);
245 :
246 0 : xParser->setDocumentHandler( xDocHandler );
247 0 : xml::sax::InputSource source;
248 0 : source.aInputStream = ucb_content.openStream();
249 0 : source.sSystemId = ucb_content.getURL();
250 0 : xParser->parseStream( source );
251 :
252 0 : if ( pHandler->isParsed() )
253 : {
254 0 : lang = pHandler->getParcelLanguage();
255 0 : }
256 : }
257 :
258 0 : OUString sfwkLibType = getResourceString( RID_STR_SFWK_LIB );
259 : // replace %MACRONAME placeholder with language name
260 0 : OUString MACRONAME( "%MACROLANG" );
261 0 : sal_Int32 startOfReplace = sfwkLibType.indexOf( MACRONAME );
262 0 : sal_Int32 charsToReplace = MACRONAME.getLength();
263 0 : sfwkLibType = sfwkLibType.replaceAt( startOfReplace, charsToReplace, lang );
264 0 : dp_misc::TRACE("******************************\n");
265 0 : dp_misc::TRACE(" BackEnd detected lang = " + lang + "\n");
266 0 : dp_misc::TRACE(" for url " + sParcelDescURL + "\n");
267 0 : dp_misc::TRACE("******************************\n");
268 0 : return new PackageImpl( this, url, sfwkLibType, bRemoved, identifier);
269 : }
270 : }
271 : }
272 : throw lang::IllegalArgumentException(
273 0 : StrUnsupportedMediaType::get() + mediaType,
274 : static_cast<OWeakObject *>(this),
275 0 : static_cast<sal_Int16>(-1) );
276 : }
277 :
278 :
279 0 : void BackendImpl::PackageImpl:: initPackageHandler()
280 : {
281 0 : if (m_xNameCntrPkgHandler.is())
282 0 : return;
283 :
284 0 : BackendImpl * that = getMyBackend();
285 0 : Any aContext;
286 :
287 0 : if ( that->m_eContext == CONTEXT_USER )
288 : {
289 0 : aContext <<= OUString("user");
290 : }
291 0 : else if ( that->m_eContext == CONTEXT_SHARED )
292 : {
293 0 : aContext <<= OUString("share");
294 : }
295 0 : else if ( that->m_eContext == CONTEXT_BUNDLED )
296 : {
297 0 : aContext <<= OUString("bundled");
298 : }
299 : else
300 : {
301 : OSL_ASSERT( false );
302 : // NOT supported at the momemtn // TODO
303 : }
304 :
305 : Reference< provider::XScriptProviderFactory > xFac =
306 0 : provider::theMasterScriptProviderFactory::get( that->getComponentContext() );
307 :
308 0 : Reference< container::XNameContainer > xName( xFac->createScriptProvider( aContext ), UNO_QUERY );
309 0 : if ( xName.is() )
310 : {
311 0 : m_xNameCntrPkgHandler.set( xName );
312 0 : }
313 : // TODO what happens if above fails??
314 : }
315 :
316 : // Package
317 :
318 : beans::Optional< beans::Ambiguous<sal_Bool> >
319 0 : BackendImpl::PackageImpl::isRegistered_(
320 : ::osl::ResettableMutexGuard &,
321 : ::rtl::Reference<AbortChannel> const &,
322 : Reference<XCommandEnvironment> const & )
323 : {
324 : return beans::Optional< beans::Ambiguous<sal_Bool> >(
325 : true /* IsPresent */,
326 : beans::Ambiguous<sal_Bool>(
327 0 : m_xNameCntrPkgHandler.is() && m_xNameCntrPkgHandler->hasByName(
328 0 : m_url ),
329 0 : false /* IsAmbiguous */ ) );
330 : }
331 :
332 :
333 0 : void BackendImpl::PackageImpl::processPackage_(
334 : ::osl::ResettableMutexGuard &,
335 : bool doRegisterPackage,
336 : bool /* startup */,
337 : ::rtl::Reference<AbortChannel> const &,
338 : Reference<XCommandEnvironment> const & )
339 : {
340 0 : if ( !m_xNameCntrPkgHandler.is() )
341 : {
342 0 : dp_misc::TRACE("no package handler!!!!\n");
343 : throw RuntimeException( "No package Handler ",
344 0 : Reference< XInterface >() );
345 : }
346 :
347 0 : if (doRegisterPackage)
348 : {
349 : // will throw if it fails
350 0 : m_xNameCntrPkgHandler->insertByName( m_url, makeAny( Reference< XPackage >(this) ) );
351 :
352 : }
353 : else // revokePackage()
354 : {
355 0 : m_xNameCntrPkgHandler->removeByName( m_url );
356 : }
357 0 : }
358 :
359 : namespace sdecl = comphelper::service_decl;
360 0 : sdecl::class_<BackendImpl, sdecl::with_args<true> > serviceBI;
361 0 : extern sdecl::ServiceDecl const serviceDecl(
362 : serviceBI,
363 : "com.sun.star.comp.deployment.sfwk.PackageRegistryBackend",
364 : BACKEND_SERVICE_NAME );
365 :
366 : } // namespace sfwk
367 : } // namespace backend
368 0 : } // namespace dp_registry
369 :
370 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|