Update ooo320-m1
[ooovba.git] / desktop / source / deployment / registry / sfwk / dp_sfwk.cxx
blobc4604098901930750e4a9fe19afa4fcdf42c0abf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_sfwk.cxx,v $
10 * $Revision: 1.16.66.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
33 #include "dp_sfwk.hrc"
34 #include "dp_backend.h"
35 #include "dp_ucb.h"
36 #include "dp_parceldesc.hxx"
37 #include "rtl/uri.hxx"
38 #include "ucbhelper/content.hxx"
39 #include "cppuhelper/exc_hlp.hxx"
40 #include "comphelper/servicedecl.hxx"
41 #include "svtools/inettype.hxx"
42 #include <com/sun/star/container/XNameContainer.hpp>
43 #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
44 #include <memory>
47 using namespace ::dp_misc;
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::ucb;
51 using namespace ::com::sun::star::script;
53 using ::rtl::OUString;
54 namespace css = ::com::sun::star;
56 namespace dp_registry
58 namespace backend
60 namespace sfwk
63 //==============================================================================
64 class BackendImpl : public ::dp_registry::backend::PackageRegistryBackend
66 class PackageImpl : public ::dp_registry::backend::Package
68 BackendImpl * getMyBackend() const;
70 Reference< container::XNameContainer > m_xNameCntrPkgHandler;
71 OUString m_descr;
73 void initPackageHandler();
75 // Package
76 virtual beans::Optional< beans::Ambiguous<sal_Bool> > isRegistered_(
77 ::osl::ResettableMutexGuard & guard,
78 ::rtl::Reference<AbortChannel> const & abortChannel,
79 Reference<XCommandEnvironment> const & xCmdEnv );
80 virtual void processPackage_(
81 ::osl::ResettableMutexGuard & guard,
82 bool registerPackage,
83 ::rtl::Reference<AbortChannel> const & abortChannel,
84 Reference<XCommandEnvironment> const & xCmdEnv );
86 public:
87 PackageImpl( ::rtl::Reference<BackendImpl> const & myBackend,
88 OUString const & url, OUString const & libType );
89 // XPackage
90 virtual OUString SAL_CALL getDescription() throw (RuntimeException);
92 friend class PackageImpl;
94 // PackageRegistryBackend
95 virtual Reference<deployment::XPackage> bindPackage_(
96 OUString const & url, OUString const & mediaType,
97 Reference<XCommandEnvironment> const & xCmdEnv );
99 const Reference<deployment::XPackageTypeInfo> m_xTypeInfo;
101 public:
102 BackendImpl(
103 Sequence<Any> const & args,
104 Reference<XComponentContext> const & xComponentContext );
106 // XPackageRegistry
107 virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
108 getSupportedPackageTypes() throw (RuntimeException);
111 BackendImpl * BackendImpl::PackageImpl::getMyBackend() const
113 BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
114 if (NULL == pBackend)
116 //May throw a DisposedException
117 check();
118 //We should never get here...
119 throw RuntimeException(
120 OUSTR("Failed to get the BackendImpl"),
121 static_cast<OWeakObject*>(const_cast<PackageImpl *>(this)));
123 return pBackend;
125 //______________________________________________________________________________
126 OUString BackendImpl::PackageImpl::getDescription() throw (RuntimeException)
128 if (m_descr.getLength() == 0)
129 return Package::getDescription();
130 else
131 return m_descr;
134 //______________________________________________________________________________
135 BackendImpl::PackageImpl::PackageImpl(
136 ::rtl::Reference<BackendImpl> const & myBackend,
137 OUString const & url, OUString const & libType )
138 : Package( myBackend.get(), url, OUString(), OUString(),
139 myBackend->m_xTypeInfo ),
140 m_descr(libType)
142 initPackageHandler();
144 sal_Int32 segmEnd = url.getLength();
145 if (url.getLength() > 0 && url[ url.getLength() - 1 ] == '/')
146 --segmEnd;
147 sal_Int32 segmStart = (url.lastIndexOf( '/', segmEnd ) + 1);
148 if (segmStart < 0)
149 segmStart = 0;
150 // name and display name default the same:
151 m_displayName = ::rtl::Uri::decode(
152 url.copy( segmStart, segmEnd - segmStart ),
153 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
154 m_name = m_displayName;
156 dp_misc::TRACE(OUSTR("PakageImpl displayName is ") + m_displayName);
159 //______________________________________________________________________________
160 BackendImpl::BackendImpl(
161 Sequence<Any> const & args,
162 Reference<XComponentContext> const & xComponentContext )
163 : PackageRegistryBackend( args, xComponentContext ),
164 m_xTypeInfo( new Package::TypeInfo(
165 OUSTR("application/vnd.sun.star.framework-script"),
166 OUString() /* no file filter */,
167 OUSTR("Scripting Framework Script Library"),
168 RID_IMG_SCRIPTLIB, RID_IMG_SCRIPTLIB_HC ) )
170 if (! transientMode())
173 if (office_is_running())
175 Reference<XComponentContext> xContext( getComponentContext() );
176 m_xScriptLibs.set(
177 xContext->getServiceManager()->createInstanceWithContext(
178 OUSTR("com.sun.star."
179 "script.ApplicationScriptLibraryContainer"),
180 xContext ), UNO_QUERY_THROW );
181 m_xDialogLibs.set(
182 xContext->getServiceManager()->createInstanceWithContext(
183 OUSTR("com.sun.star."
184 "script.ApplicationDialogLibraryContainer"),
185 xContext ), UNO_QUERY_THROW );
187 else
189 OUString basic_path(
190 m_eContext == CONTEXT_USER
191 ? OUSTR("vnd.sun.star.expand:${$BRAND_BASE_DIR/program/"
192 SAL_CONFIGFILE("bootstrap")
193 ":UserInstallation}/user/basic")
194 : OUSTR("vnd.sun.star.expand:${$BRAND_BASE_DIR/program/"
195 SAL_CONFIGFILE("bootstrap")
196 ":BaseInstallation}/share/basic") );
197 m_basic_script_libs.reset(
198 new LibraryContainer(
199 makeURL( basic_path, OUSTR("script.xlc") ),
200 getMutex(),
201 getComponentContext() ) );
202 m_dialog_libs.reset(
203 new LibraryContainer(
204 makeURL( basic_path, OUSTR("dialog.xlc") ),
205 getMutex(),
206 getComponentContext() ) );
212 // XPackageRegistry
213 //______________________________________________________________________________
214 Sequence< Reference<deployment::XPackageTypeInfo> >
215 BackendImpl::getSupportedPackageTypes() throw (RuntimeException)
217 return Sequence< Reference<deployment::XPackageTypeInfo> >(&m_xTypeInfo, 1);
220 // PackageRegistryBackend
221 //______________________________________________________________________________
222 Reference<deployment::XPackage> BackendImpl::bindPackage_(
223 OUString const & url, OUString const & mediaType_,
224 Reference<XCommandEnvironment> const & xCmdEnv )
226 OUString mediaType( mediaType_ );
227 if (mediaType.getLength() == 0)
229 // detect media-type:
230 ::ucbhelper::Content ucbContent;
231 if (create_ucb_content( &ucbContent, url, xCmdEnv ) &&
232 ucbContent.isFolder())
234 // probe for parcel-descriptor.xml:
235 if (create_ucb_content(
236 0, makeURL( url, OUSTR("parcel-descriptor.xml") ),
237 xCmdEnv, false /* no throw */ ))
239 mediaType = OUSTR("application/vnd.sun.star.framework-script");
242 if (mediaType.getLength() == 0)
243 throw lang::IllegalArgumentException(
244 StrCannotDetectMediaType::get() + url,
245 static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
248 String type, subType;
249 INetContentTypeParameterList params;
250 if (INetContentTypes::parse( mediaType, type, subType, &params ))
252 if (type.EqualsIgnoreCaseAscii("application"))
254 if (subType.EqualsIgnoreCaseAscii("vnd.sun.star.framework-script"))
256 OUString lang = OUString::createFromAscii("Script");
257 OUString sParcelDescURL = makeURL(
258 url, OUSTR("parcel-descriptor.xml") );
260 ::ucbhelper::Content ucb_content;
262 if (create_ucb_content( &ucb_content, sParcelDescURL,
263 xCmdEnv, false /* no throw */ ))
265 ParcelDescDocHandler* pHandler =
266 new ParcelDescDocHandler();
267 Reference< xml::sax::XDocumentHandler >
268 xDocHandler = pHandler;
270 Reference<XComponentContext>
271 xContext( getComponentContext() );
273 Reference< xml::sax::XParser > xParser(
274 xContext->getServiceManager()->createInstanceWithContext(
275 OUSTR("com.sun.star.xml.sax.Parser"), xContext ),
276 UNO_QUERY_THROW );
278 xParser->setDocumentHandler( xDocHandler );
279 xml::sax::InputSource source;
280 source.aInputStream = ucb_content.openStream();
281 source.sSystemId = ucb_content.getURL();
282 xParser->parseStream( source );
284 if ( pHandler->isParsed() )
286 lang = pHandler->getParcelLanguage();
290 OUString sfwkLibType = getResourceString( RID_STR_SFWK_LIB );
291 // replace %MACRONAME placeholder with language name
292 OUString MACRONAME( OUSTR("%MACROLANG" ) );
293 sal_Int32 startOfReplace = sfwkLibType.indexOf( MACRONAME );
294 sal_Int32 charsToReplace = MACRONAME.getLength();
295 sfwkLibType = sfwkLibType.replaceAt( startOfReplace, charsToReplace, lang );
296 dp_misc::TRACE("******************************\n");
297 dp_misc::TRACE(OUSTR(" BackEnd detected lang = ") + lang + OUSTR("\n"));
298 dp_misc::TRACE(OUSTR(" for url ") + sParcelDescURL + OUSTR("\n") );
299 dp_misc::TRACE("******************************\n");
300 return new PackageImpl( this, url, sfwkLibType );
304 throw lang::IllegalArgumentException(
305 StrUnsupportedMediaType::get() + mediaType,
306 static_cast<OWeakObject *>(this),
307 static_cast<sal_Int16>(-1) );
310 //##############################################################################
312 void BackendImpl::PackageImpl:: initPackageHandler()
314 if (m_xNameCntrPkgHandler.is())
315 return;
317 BackendImpl * that = getMyBackend();
318 Any aContext;
320 if ( that->m_eContext == CONTEXT_USER )
322 aContext <<= OUSTR("user");
324 else if ( that->m_eContext == CONTEXT_SHARED )
326 aContext <<= OUSTR("share");
328 else
330 OSL_ASSERT( 0 );
331 // NOT supported at the momemtn // TODO
334 Reference< provider::XScriptProviderFactory > xFac(
335 that->getComponentContext()->getValueByName(
336 OUSTR( "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ), UNO_QUERY );
338 if ( xFac.is() )
340 Reference< container::XNameContainer > xName( xFac->createScriptProvider( aContext ), UNO_QUERY );
341 if ( xName.is() )
343 m_xNameCntrPkgHandler.set( xName );
346 // TODO what happens if above fails??
349 // Package
350 //______________________________________________________________________________
351 beans::Optional< beans::Ambiguous<sal_Bool> >
352 BackendImpl::PackageImpl::isRegistered_(
353 ::osl::ResettableMutexGuard &,
354 ::rtl::Reference<AbortChannel> const &,
355 Reference<XCommandEnvironment> const & )
357 return beans::Optional< beans::Ambiguous<sal_Bool> >(
358 true /* IsPresent */,
359 beans::Ambiguous<sal_Bool>(
360 m_xNameCntrPkgHandler.is() && m_xNameCntrPkgHandler->hasByName(
361 m_url ),
362 false /* IsAmbiguous */ ) );
365 //______________________________________________________________________________
366 void BackendImpl::PackageImpl::processPackage_(
367 ::osl::ResettableMutexGuard &,
368 bool doRegisterPackage,
369 ::rtl::Reference<AbortChannel> const &,
370 Reference<XCommandEnvironment> const & )
372 if ( !m_xNameCntrPkgHandler.is() )
374 dp_misc::TRACE("no package handler!!!!\n");
375 throw RuntimeException( OUSTR("No package Handler " ),
376 Reference< XInterface >() );
379 if (doRegisterPackage)
381 // will throw if it fails
382 m_xNameCntrPkgHandler->insertByName( m_url, makeAny( Reference< XPackage >(this) ) );
385 else // revokePackage()
387 m_xNameCntrPkgHandler->removeByName( m_url );
391 namespace sdecl = comphelper::service_decl;
392 sdecl::class_<BackendImpl, sdecl::with_args<true> > serviceBI;
393 extern sdecl::ServiceDecl const serviceDecl(
394 serviceBI,
395 "com.sun.star.comp.deployment.sfwk.PackageRegistryBackend",
396 BACKEND_SERVICE_NAME );
398 } // namespace sfwk
399 } // namespace backend
400 } // namespace dp_registry