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 <strings.hrc>
23 #include <dp_backend.h>
26 #include "dp_parceldesc.hxx"
27 #include <rtl/uri.hxx>
28 #include <ucbhelper/content.hxx>
29 #include <svl/inettype.hxx>
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
32 #include <com/sun/star/xml/sax/Parser.hpp>
33 #include <cppuhelper/supportsservice.hxx>
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
;
44 namespace dp_registry::backend::sfwk
49 class BackendImpl
: public ::dp_registry::backend::PackageRegistryBackend
51 class PackageImpl
: public ::dp_registry::backend::Package
53 BackendImpl
* getMyBackend() const;
55 Reference
< container::XNameContainer
> m_xNameCntrPkgHandler
;
58 void initPackageHandler();
61 virtual beans::Optional
< beans::Ambiguous
<sal_Bool
> > isRegistered_(
62 ::osl::ResettableMutexGuard
& guard
,
63 ::rtl::Reference
<AbortChannel
> const & abortChannel
,
64 Reference
<XCommandEnvironment
> const & xCmdEnv
) override
;
65 virtual void processPackage_(
66 ::osl::ResettableMutexGuard
& guard
,
69 ::rtl::Reference
<AbortChannel
> const & abortChannel
,
70 Reference
<XCommandEnvironment
> const & xCmdEnv
) override
;
74 ::rtl::Reference
<BackendImpl
> const & myBackend
,
75 OUString
const & url
, OUString libType
, bool bRemoved
,
76 OUString
const & identifier
);
78 virtual OUString SAL_CALL
getDescription() override
;
79 virtual OUString SAL_CALL
getLicenseText() override
;
81 friend class PackageImpl
;
83 // PackageRegistryBackend
84 virtual Reference
<deployment::XPackage
> bindPackage_(
85 OUString
const & url
, OUString
const & mediaType
,
86 bool bRemoved
, OUString
const & identifier
,
87 Reference
<XCommandEnvironment
> const & xCmdEnv
) override
;
89 const Reference
<deployment::XPackageTypeInfo
> m_xTypeInfo
;
94 Sequence
<Any
> const & args
,
95 Reference
<XComponentContext
> const & xComponentContext
);
98 virtual OUString SAL_CALL
getImplementationName() override
;
99 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
100 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
103 virtual Sequence
< Reference
<deployment::XPackageTypeInfo
> > SAL_CALL
104 getSupportedPackageTypes() override
;
105 virtual void SAL_CALL
packageRemoved(OUString
const & url
, OUString
const & mediaType
) override
;
110 BackendImpl
* BackendImpl::PackageImpl::getMyBackend() const
112 BackendImpl
* pBackend
= static_cast<BackendImpl
*>(m_myBackend
.get());
113 if (nullptr == pBackend
)
115 //May throw a DisposedException
117 //We should never get here...
118 throw RuntimeException(u
"Failed to get the BackendImpl"_ustr
,
119 static_cast<OWeakObject
*>(const_cast<PackageImpl
*>(this)));
124 OUString
BackendImpl::PackageImpl::getDescription()
126 if (m_descr
.isEmpty())
127 return Package::getDescription();
132 OUString
BackendImpl::PackageImpl::getLicenseText()
134 return Package::getDescription();
137 BackendImpl::PackageImpl::PackageImpl(
138 ::rtl::Reference
<BackendImpl
> const & myBackend
,
139 OUString
const & url
, OUString libType
, bool bRemoved
,
140 OUString
const & identifier
)
141 : Package( myBackend
, url
, OUString(), OUString(),
142 myBackend
->m_xTypeInfo
, bRemoved
, identifier
),
143 m_descr(std::move(libType
))
145 initPackageHandler();
147 sal_Int32 segmEnd
= url
.getLength();
148 if ( url
.endsWith("/") )
150 sal_Int32 segmStart
= url
.lastIndexOf( '/', segmEnd
) + 1;
153 // name and display name default the same:
154 m_displayName
= ::rtl::Uri::decode(
155 url
.copy( segmStart
, segmEnd
- segmStart
),
156 rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
157 m_name
= m_displayName
;
159 dp_misc::TRACE("PackageImpl displayName is " + m_displayName
);
163 BackendImpl::BackendImpl(
164 Sequence
<Any
> const & args
,
165 Reference
<XComponentContext
> const & xComponentContext
)
166 : PackageRegistryBackend( args
, xComponentContext
),
167 m_xTypeInfo( new Package::TypeInfo(
168 u
"application/vnd.sun.star.framework-script"_ustr
,
169 OUString() /* no file filter */,
170 u
"Scripting Framework Script Library"_ustr
177 OUString
BackendImpl::getImplementationName()
179 return u
"com.sun.star.comp.deployment.sfwk.PackageRegistryBackend"_ustr
;
182 sal_Bool
BackendImpl::supportsService( const OUString
& ServiceName
)
184 return cppu::supportsService(this, ServiceName
);
187 css::uno::Sequence
< OUString
> BackendImpl::getSupportedServiceNames()
189 return { BACKEND_SERVICE_NAME
};
194 Sequence
< Reference
<deployment::XPackageTypeInfo
> >
195 BackendImpl::getSupportedPackageTypes()
197 return Sequence
< Reference
<deployment::XPackageTypeInfo
> >(&m_xTypeInfo
, 1);
200 void BackendImpl::packageRemoved(OUString
const & /*url*/, OUString
const & /*mediaType*/)
204 // PackageRegistryBackend
206 Reference
<deployment::XPackage
> BackendImpl::bindPackage_(
207 OUString
const & url
, OUString
const & mediaType_
, bool bRemoved
,
208 OUString
const & identifier
, Reference
<XCommandEnvironment
> const & xCmdEnv
)
210 OUString
mediaType( mediaType_
);
211 if (mediaType
.isEmpty())
213 // detect media-type:
214 ::ucbhelper::Content ucbContent
;
215 if (create_ucb_content( &ucbContent
, url
, xCmdEnv
) &&
216 ucbContent
.isFolder())
218 // probe for parcel-descriptor.xml:
219 if (create_ucb_content(
220 nullptr, makeURL( url
, u
"parcel-descriptor.xml"_ustr
),
221 xCmdEnv
, false /* no throw */ ))
223 mediaType
= "application/vnd.sun.star.framework-script";
226 if (mediaType
.isEmpty())
227 throw lang::IllegalArgumentException(
228 StrCannotDetectMediaType() + url
,
229 static_cast<OWeakObject
*>(this), static_cast<sal_Int16
>(-1) );
232 OUString type
, subType
;
233 INetContentTypeParameterList params
;
234 if (INetContentTypes::parse( mediaType
, type
, subType
, ¶ms
))
236 if (type
.equalsIgnoreAsciiCase("application"))
238 if (subType
.equalsIgnoreAsciiCase("vnd.sun.star.framework-script"))
240 OUString lang
= u
"Script"_ustr
;
241 OUString sParcelDescURL
= makeURL(
242 url
, u
"parcel-descriptor.xml"_ustr
);
244 ::ucbhelper::Content ucb_content
;
246 if (create_ucb_content( &ucb_content
, sParcelDescURL
,
247 xCmdEnv
, false /* no throw */ ))
249 rtl::Reference
<ParcelDescDocHandler
> pHandler
=
250 new ParcelDescDocHandler();
252 Reference
<XComponentContext
>
253 xContext( getComponentContext() );
255 Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create(xContext
);
257 xParser
->setDocumentHandler( pHandler
);
258 xml::sax::InputSource source
;
259 source
.aInputStream
= ucb_content
.openStream();
260 source
.sSystemId
= ucb_content
.getURL();
261 xParser
->parseStream( source
);
263 if ( pHandler
->isParsed() )
265 lang
= pHandler
->getParcelLanguage();
269 OUString sfwkLibType
= DpResId( RID_STR_SFWK_LIB
);
270 // replace %MACRONAME placeholder with language name
271 OUString
MACRONAME( u
"%MACROLANG"_ustr
);
272 sal_Int32 startOfReplace
= sfwkLibType
.indexOf( MACRONAME
);
273 sal_Int32 charsToReplace
= MACRONAME
.getLength();
274 sfwkLibType
= sfwkLibType
.replaceAt( startOfReplace
, charsToReplace
, lang
);
275 dp_misc::TRACE(u
"******************************\n"_ustr
);
276 dp_misc::TRACE(" BackEnd detected lang = " + lang
+ "\n");
277 dp_misc::TRACE(" for url " + sParcelDescURL
+ "\n");
278 dp_misc::TRACE(u
"******************************\n"_ustr
);
279 return new PackageImpl( this, url
, sfwkLibType
, bRemoved
, identifier
);
283 throw lang::IllegalArgumentException(
284 StrUnsupportedMediaType() + mediaType
,
285 static_cast<OWeakObject
*>(this),
286 static_cast<sal_Int16
>(-1) );
290 void BackendImpl::PackageImpl:: initPackageHandler()
292 if (m_xNameCntrPkgHandler
.is())
295 BackendImpl
* that
= getMyBackend();
298 if ( that
->m_eContext
== Context::User
)
300 aContext
<<= u
"user"_ustr
;
302 else if ( that
->m_eContext
== Context::Shared
)
304 aContext
<<= u
"share"_ustr
;
306 else if ( that
->m_eContext
== Context::Bundled
)
308 aContext
<<= u
"bundled"_ustr
;
313 // NOT supported at the moment // TODO
316 Reference
< provider::XScriptProviderFactory
> xFac
=
317 provider::theMasterScriptProviderFactory::get( that
->getComponentContext() );
319 Reference
< container::XNameContainer
> xName( xFac
->createScriptProvider( aContext
), UNO_QUERY
);
322 m_xNameCntrPkgHandler
.set( xName
);
324 // TODO what happens if above fails??
329 beans::Optional
< beans::Ambiguous
<sal_Bool
> >
330 BackendImpl::PackageImpl::isRegistered_(
331 ::osl::ResettableMutexGuard
&,
332 ::rtl::Reference
<AbortChannel
> const &,
333 Reference
<XCommandEnvironment
> const & )
335 return beans::Optional
< beans::Ambiguous
<sal_Bool
> >(
336 true /* IsPresent */,
337 beans::Ambiguous
<sal_Bool
>(
338 m_xNameCntrPkgHandler
.is() && m_xNameCntrPkgHandler
->hasByName(
340 false /* IsAmbiguous */ ) );
344 void BackendImpl::PackageImpl::processPackage_(
345 ::osl::ResettableMutexGuard
&,
346 bool doRegisterPackage
,
348 ::rtl::Reference
<AbortChannel
> const &,
349 Reference
<XCommandEnvironment
> const & )
351 if ( !m_xNameCntrPkgHandler
.is() )
353 dp_misc::TRACE(u
"no package handler!!!!\n"_ustr
);
354 throw RuntimeException( u
"No package Handler "_ustr
);
357 if (doRegisterPackage
)
359 // will throw if it fails
360 m_xNameCntrPkgHandler
->insertByName( m_url
, Any( Reference
< XPackage
>(this) ) );
363 else // revokePackage()
365 m_xNameCntrPkgHandler
->removeByName( m_url
);
369 } // namespace dp_registry::backend::sfwk
371 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
372 com_sun_star_comp_deployment_sfwk_PackageRegistryBackend_get_implementation(
373 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& args
)
375 return cppu::acquire(new dp_registry::backend::sfwk::BackendImpl(args
, context
));
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */