1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <dp_services.hxx>
23 #include <strings.hrc>
24 #include <dp_backend.h>
26 #include "dp_parceldesc.hxx"
27 #include <rtl/uri.hxx>
28 #include <ucbhelper/content.hxx>
29 #include <cppuhelper/exc_hlp.hxx>
30 #include <comphelper/servicedecl.hxx>
31 #include <svl/inettype.hxx>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
34 #include <com/sun/star/xml/sax/Parser.hpp>
37 using namespace ::dp_misc
;
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::ucb
;
41 using namespace ::com::sun::star::script
;
52 class BackendImpl
: public ::dp_registry::backend::PackageRegistryBackend
54 class PackageImpl
: public ::dp_registry::backend::Package
56 BackendImpl
* getMyBackend() const;
58 Reference
< container::XNameContainer
> m_xNameCntrPkgHandler
;
61 void initPackageHandler();
64 virtual beans::Optional
< beans::Ambiguous
<sal_Bool
> > isRegistered_(
65 ::osl::ResettableMutexGuard
& guard
,
66 ::rtl::Reference
<AbortChannel
> const & abortChannel
,
67 Reference
<XCommandEnvironment
> const & xCmdEnv
) override
;
68 virtual void processPackage_(
69 ::osl::ResettableMutexGuard
& guard
,
72 ::rtl::Reference
<AbortChannel
> const & abortChannel
,
73 Reference
<XCommandEnvironment
> const & xCmdEnv
) override
;
77 ::rtl::Reference
<BackendImpl
> const & myBackend
,
78 OUString
const & url
, OUString
const & libType
, bool bRemoved
,
79 OUString
const & identifier
);
81 virtual OUString SAL_CALL
getDescription() override
;
82 virtual OUString SAL_CALL
getLicenseText() override
;
84 friend class PackageImpl
;
86 // PackageRegistryBackend
87 virtual Reference
<deployment::XPackage
> bindPackage_(
88 OUString
const & url
, OUString
const & mediaType
,
89 bool bRemoved
, OUString
const & identifier
,
90 Reference
<XCommandEnvironment
> const & xCmdEnv
) override
;
92 const Reference
<deployment::XPackageTypeInfo
> m_xTypeInfo
;
97 Sequence
<Any
> const & args
,
98 Reference
<XComponentContext
> const & xComponentContext
);
101 virtual Sequence
< Reference
<deployment::XPackageTypeInfo
> > SAL_CALL
102 getSupportedPackageTypes() override
;
103 virtual void SAL_CALL
packageRemoved(OUString
const & url
, OUString
const & mediaType
) override
;
106 BackendImpl
* BackendImpl::PackageImpl::getMyBackend() const
108 BackendImpl
* pBackend
= static_cast<BackendImpl
*>(m_myBackend
.get());
109 if (nullptr == pBackend
)
111 //May throw a DisposedException
113 //We should never get here...
114 throw RuntimeException("Failed to get the BackendImpl",
115 static_cast<OWeakObject
*>(const_cast<PackageImpl
*>(this)));
120 OUString
BackendImpl::PackageImpl::getDescription()
122 if (m_descr
.isEmpty())
123 return Package::getDescription();
128 OUString
BackendImpl::PackageImpl::getLicenseText()
130 return Package::getDescription();
133 BackendImpl::PackageImpl::PackageImpl(
134 ::rtl::Reference
<BackendImpl
> const & myBackend
,
135 OUString
const & url
, OUString
const & libType
, bool bRemoved
,
136 OUString
const & identifier
)
137 : Package( myBackend
.get(), url
, OUString(), OUString(),
138 myBackend
->m_xTypeInfo
, bRemoved
, identifier
),
141 initPackageHandler();
143 sal_Int32 segmEnd
= url
.getLength();
144 if ( url
.endsWith("/") )
146 sal_Int32 segmStart
= url
.lastIndexOf( '/', segmEnd
) + 1;
149 // name and display name default the same:
150 m_displayName
= ::rtl::Uri::decode(
151 url
.copy( segmStart
, segmEnd
- segmStart
),
152 rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
153 m_name
= m_displayName
;
155 dp_misc::TRACE("PackageImpl displayName is " + m_displayName
);
159 BackendImpl::BackendImpl(
160 Sequence
<Any
> const & args
,
161 Reference
<XComponentContext
> const & xComponentContext
)
162 : PackageRegistryBackend( args
, xComponentContext
),
163 m_xTypeInfo( new Package::TypeInfo(
164 "application/vnd.sun.star.framework-script",
165 OUString() /* no file filter */,
166 "Scripting Framework Script Library"
174 Sequence
< Reference
<deployment::XPackageTypeInfo
> >
175 BackendImpl::getSupportedPackageTypes()
177 return Sequence
< Reference
<deployment::XPackageTypeInfo
> >(&m_xTypeInfo
, 1);
180 void BackendImpl::packageRemoved(OUString
const & /*url*/, OUString
const & /*mediaType*/)
184 // PackageRegistryBackend
186 Reference
<deployment::XPackage
> BackendImpl::bindPackage_(
187 OUString
const & url
, OUString
const & mediaType_
, bool bRemoved
,
188 OUString
const & identifier
, Reference
<XCommandEnvironment
> const & xCmdEnv
)
190 OUString
mediaType( mediaType_
);
191 if (mediaType
.isEmpty())
193 // detect media-type:
194 ::ucbhelper::Content ucbContent
;
195 if (create_ucb_content( &ucbContent
, url
, xCmdEnv
) &&
196 ucbContent
.isFolder())
198 // probe for parcel-descriptor.xml:
199 if (create_ucb_content(
200 nullptr, makeURL( url
, "parcel-descriptor.xml" ),
201 xCmdEnv
, false /* no throw */ ))
203 mediaType
= "application/vnd.sun.star.framework-script";
206 if (mediaType
.isEmpty())
207 throw lang::IllegalArgumentException(
208 StrCannotDetectMediaType() + url
,
209 static_cast<OWeakObject
*>(this), static_cast<sal_Int16
>(-1) );
212 OUString type
, subType
;
213 INetContentTypeParameterList params
;
214 if (INetContentTypes::parse( mediaType
, type
, subType
, ¶ms
))
216 if (type
.equalsIgnoreAsciiCase("application"))
218 if (subType
.equalsIgnoreAsciiCase("vnd.sun.star.framework-script"))
220 OUString lang
= "Script";
221 OUString sParcelDescURL
= makeURL(
222 url
, "parcel-descriptor.xml" );
224 ::ucbhelper::Content ucb_content
;
226 if (create_ucb_content( &ucb_content
, sParcelDescURL
,
227 xCmdEnv
, false /* no throw */ ))
229 ParcelDescDocHandler
* pHandler
=
230 new ParcelDescDocHandler();
231 Reference
< xml::sax::XDocumentHandler
>
232 xDocHandler
= pHandler
;
234 Reference
<XComponentContext
>
235 xContext( getComponentContext() );
237 Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create(xContext
);
239 xParser
->setDocumentHandler( xDocHandler
);
240 xml::sax::InputSource source
;
241 source
.aInputStream
= ucb_content
.openStream();
242 source
.sSystemId
= ucb_content
.getURL();
243 xParser
->parseStream( source
);
245 if ( pHandler
->isParsed() )
247 lang
= pHandler
->getParcelLanguage();
251 OUString sfwkLibType
= DpResId( RID_STR_SFWK_LIB
);
252 // replace %MACRONAME placeholder with language name
253 OUString
MACRONAME( "%MACROLANG" );
254 sal_Int32 startOfReplace
= sfwkLibType
.indexOf( MACRONAME
);
255 sal_Int32 charsToReplace
= MACRONAME
.getLength();
256 sfwkLibType
= sfwkLibType
.replaceAt( startOfReplace
, charsToReplace
, lang
);
257 dp_misc::TRACE("******************************\n");
258 dp_misc::TRACE(" BackEnd detected lang = " + lang
+ "\n");
259 dp_misc::TRACE(" for url " + sParcelDescURL
+ "\n");
260 dp_misc::TRACE("******************************\n");
261 return new PackageImpl( this, url
, sfwkLibType
, bRemoved
, identifier
);
265 throw lang::IllegalArgumentException(
266 StrUnsupportedMediaType() + mediaType
,
267 static_cast<OWeakObject
*>(this),
268 static_cast<sal_Int16
>(-1) );
272 void BackendImpl::PackageImpl:: initPackageHandler()
274 if (m_xNameCntrPkgHandler
.is())
277 BackendImpl
* that
= getMyBackend();
280 if ( that
->m_eContext
== Context::User
)
282 aContext
<<= OUString("user");
284 else if ( that
->m_eContext
== Context::Shared
)
286 aContext
<<= OUString("share");
288 else if ( that
->m_eContext
== Context::Bundled
)
290 aContext
<<= OUString("bundled");
295 // NOT supported at the moment // TODO
298 Reference
< provider::XScriptProviderFactory
> xFac
=
299 provider::theMasterScriptProviderFactory::get( that
->getComponentContext() );
301 Reference
< container::XNameContainer
> xName( xFac
->createScriptProvider( aContext
), UNO_QUERY
);
304 m_xNameCntrPkgHandler
.set( xName
);
306 // TODO what happens if above fails??
311 beans::Optional
< beans::Ambiguous
<sal_Bool
> >
312 BackendImpl::PackageImpl::isRegistered_(
313 ::osl::ResettableMutexGuard
&,
314 ::rtl::Reference
<AbortChannel
> const &,
315 Reference
<XCommandEnvironment
> const & )
317 return beans::Optional
< beans::Ambiguous
<sal_Bool
> >(
318 true /* IsPresent */,
319 beans::Ambiguous
<sal_Bool
>(
320 m_xNameCntrPkgHandler
.is() && m_xNameCntrPkgHandler
->hasByName(
322 false /* IsAmbiguous */ ) );
326 void BackendImpl::PackageImpl::processPackage_(
327 ::osl::ResettableMutexGuard
&,
328 bool doRegisterPackage
,
330 ::rtl::Reference
<AbortChannel
> const &,
331 Reference
<XCommandEnvironment
> const & )
333 if ( !m_xNameCntrPkgHandler
.is() )
335 dp_misc::TRACE("no package handler!!!!\n");
336 throw RuntimeException( "No package Handler " );
339 if (doRegisterPackage
)
341 // will throw if it fails
342 m_xNameCntrPkgHandler
->insertByName( m_url
, makeAny( Reference
< XPackage
>(this) ) );
345 else // revokePackage()
347 m_xNameCntrPkgHandler
->removeByName( m_url
);
351 namespace sdecl
= comphelper::service_decl
;
352 sdecl::class_
<BackendImpl
, sdecl::with_args
<true> > const serviceBI
;
353 sdecl::ServiceDecl
const serviceDecl(
355 "com.sun.star.comp.deployment.sfwk.PackageRegistryBackend",
356 BACKEND_SERVICE_NAME
);
359 } // namespace backend
360 } // namespace dp_registry
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */