1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2722ceddSAndrew Rist  * distributed with this work for additional information
6*2722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2722ceddSAndrew Rist  *
11*2722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2722ceddSAndrew Rist  *
13*2722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist  * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2722ceddSAndrew Rist  * specific language governing permissions and limitations
18*2722ceddSAndrew Rist  * under the License.
19*2722ceddSAndrew Rist  *
20*2722ceddSAndrew Rist  *************************************************************/
21*2722ceddSAndrew Rist 
22*2722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "dp_script.hrc"
28cdf0e10cSrcweir #include "dp_lib_container.h"
29cdf0e10cSrcweir #include "dp_backend.h"
30cdf0e10cSrcweir #include "dp_ucb.h"
31cdf0e10cSrcweir #include "rtl/uri.hxx"
32cdf0e10cSrcweir #include "ucbhelper/content.hxx"
33cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
34cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
35cdf0e10cSrcweir #include "comphelper/servicedecl.hxx"
36cdf0e10cSrcweir #include "svl/inettype.hxx"
37cdf0e10cSrcweir #include "com/sun/star/util/XUpdatable.hpp"
38cdf0e10cSrcweir #include "com/sun/star/script/XLibraryContainer3.hpp"
39cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
40cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
41cdf0e10cSrcweir #include <com/sun/star/uri/XUriReferenceFactory.hpp>
42cdf0e10cSrcweir #include <memory>
43cdf0e10cSrcweir #include "dp_scriptbackenddb.hxx"
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::dp_misc;
46cdf0e10cSrcweir using namespace ::com::sun::star;
47cdf0e10cSrcweir using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
49cdf0e10cSrcweir using ::rtl::OUString;
50cdf0e10cSrcweir namespace css = ::com::sun::star;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace dp_registry {
53cdf0e10cSrcweir namespace backend {
54cdf0e10cSrcweir namespace script {
55cdf0e10cSrcweir namespace {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir typedef ::cppu::ImplInheritanceHelper1<
58cdf0e10cSrcweir     ::dp_registry::backend::PackageRegistryBackend, util::XUpdatable > t_helper;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //==============================================================================
61cdf0e10cSrcweir class BackendImpl : public t_helper
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     class PackageImpl : public ::dp_registry::backend::Package
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         BackendImpl * getMyBackend() const;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         const OUString m_scriptURL;
68cdf0e10cSrcweir         const OUString m_dialogURL;
69cdf0e10cSrcweir         OUString m_dialogName;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         // Package
72cdf0e10cSrcweir         virtual beans::Optional< beans::Ambiguous<sal_Bool> > isRegistered_(
73cdf0e10cSrcweir             ::osl::ResettableMutexGuard & guard,
74cdf0e10cSrcweir             ::rtl::Reference<AbortChannel> const & abortChannel,
75cdf0e10cSrcweir             Reference<XCommandEnvironment> const & xCmdEnv );
76cdf0e10cSrcweir         virtual void processPackage_(
77cdf0e10cSrcweir             ::osl::ResettableMutexGuard & guard,
78cdf0e10cSrcweir             bool registerPackage,
79cdf0e10cSrcweir             bool startup,
80cdf0e10cSrcweir             ::rtl::Reference<AbortChannel> const & abortChannel,
81cdf0e10cSrcweir             Reference<XCommandEnvironment> const & xCmdEnv );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     public:
84cdf0e10cSrcweir         PackageImpl(
85cdf0e10cSrcweir             ::rtl::Reference<BackendImpl> const & myBackend,
86cdf0e10cSrcweir             OUString const & url,
87cdf0e10cSrcweir             Reference<XCommandEnvironment> const &xCmdEnv,
88cdf0e10cSrcweir             OUString const & scriptURL, OUString const & dialogURL,
89cdf0e10cSrcweir             bool bRemoved, OUString const & identifier);
90cdf0e10cSrcweir     };
91cdf0e10cSrcweir     friend class PackageImpl;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     // PackageRegistryBackend
94cdf0e10cSrcweir     virtual Reference<deployment::XPackage> bindPackage_(
95cdf0e10cSrcweir         OUString const & url, OUString const & mediaType,
96cdf0e10cSrcweir         sal_Bool bRemoved, OUString const & identifier,
97cdf0e10cSrcweir         Reference<XCommandEnvironment> const & xCmdEnv );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     void addDataToDb(OUString const & url);
100cdf0e10cSrcweir     bool hasActiveEntry(OUString const & url);
101cdf0e10cSrcweir     void revokeEntryFromDb(OUString const & url);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     const Reference<deployment::XPackageTypeInfo> m_xBasicLibTypeInfo;
104cdf0e10cSrcweir     const Reference<deployment::XPackageTypeInfo> m_xDialogLibTypeInfo;
105cdf0e10cSrcweir     Sequence< Reference<deployment::XPackageTypeInfo> > m_typeInfos;
106cdf0e10cSrcweir     std::auto_ptr<ScriptBackendDb> m_backendDb;
107cdf0e10cSrcweir public:
108cdf0e10cSrcweir     BackendImpl( Sequence<Any> const & args,
109cdf0e10cSrcweir                  Reference<XComponentContext> const & xComponentContext );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     // XUpdatable
112cdf0e10cSrcweir     virtual void SAL_CALL update() throw (RuntimeException);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     // XPackageRegistry
115cdf0e10cSrcweir     virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
116cdf0e10cSrcweir     getSupportedPackageTypes() throw (RuntimeException);
117cdf0e10cSrcweir     virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType)
118cdf0e10cSrcweir         throw (deployment::DeploymentException,
119cdf0e10cSrcweir                uno::RuntimeException);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir };
122cdf0e10cSrcweir 
123cdf0e10cSrcweir //______________________________________________________________________________
PackageImpl(::rtl::Reference<BackendImpl> const & myBackend,OUString const & url,Reference<XCommandEnvironment> const & xCmdEnv,OUString const & scriptURL,OUString const & dialogURL,bool bRemoved,OUString const & identifier)124cdf0e10cSrcweir BackendImpl::PackageImpl::PackageImpl(
125cdf0e10cSrcweir     ::rtl::Reference<BackendImpl> const & myBackend,
126cdf0e10cSrcweir     OUString const & url,
127cdf0e10cSrcweir     Reference<XCommandEnvironment> const &xCmdEnv,
128cdf0e10cSrcweir     OUString const & scriptURL, OUString const & dialogURL, bool bRemoved,
129cdf0e10cSrcweir     OUString const & identifier)
130cdf0e10cSrcweir     : Package( myBackend.get(), url,
131cdf0e10cSrcweir                OUString(), OUString(), // will be late-initialized
132cdf0e10cSrcweir                scriptURL.getLength() > 0 ? myBackend->m_xBasicLibTypeInfo
133cdf0e10cSrcweir                : myBackend->m_xDialogLibTypeInfo, bRemoved, identifier),
134cdf0e10cSrcweir       m_scriptURL( scriptURL ),
135cdf0e10cSrcweir       m_dialogURL( dialogURL )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     // name, displayName:
138cdf0e10cSrcweir     if (dialogURL.getLength() > 0) {
139cdf0e10cSrcweir         m_dialogName = LibraryContainer::get_libname(
140cdf0e10cSrcweir             dialogURL, xCmdEnv, myBackend->getComponentContext() );
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir     if (scriptURL.getLength() > 0) {
143cdf0e10cSrcweir         m_name = LibraryContainer::get_libname(
144cdf0e10cSrcweir             scriptURL, xCmdEnv, myBackend->getComponentContext() );
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir     else
147cdf0e10cSrcweir         m_name = m_dialogName;
148cdf0e10cSrcweir     m_displayName = m_name;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir //______________________________________________________________________________
BackendImpl(Sequence<Any> const & args,Reference<XComponentContext> const & xComponentContext)152cdf0e10cSrcweir BackendImpl::BackendImpl(
153cdf0e10cSrcweir     Sequence<Any> const & args,
154cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext )
155cdf0e10cSrcweir     : t_helper( args, xComponentContext ),
156cdf0e10cSrcweir       m_xBasicLibTypeInfo( new Package::TypeInfo(
157cdf0e10cSrcweir                                OUSTR("application/"
158cdf0e10cSrcweir                                      "vnd.sun.star.basic-library"),
159cdf0e10cSrcweir                                OUString() /* no file filter */,
160cdf0e10cSrcweir                                getResourceString(RID_STR_BASIC_LIB),
161cdf0e10cSrcweir                                RID_IMG_SCRIPTLIB, RID_IMG_SCRIPTLIB_HC ) ),
162cdf0e10cSrcweir       m_xDialogLibTypeInfo( new Package::TypeInfo(
163cdf0e10cSrcweir                                 OUSTR("application/"
164cdf0e10cSrcweir                                       "vnd.sun.star.dialog-library"),
165cdf0e10cSrcweir                                 OUString() /* no file filter */,
166cdf0e10cSrcweir                                 getResourceString(RID_STR_DIALOG_LIB),
167cdf0e10cSrcweir                                 RID_IMG_DIALOGLIB, RID_IMG_DIALOGLIB_HC ) ),
168cdf0e10cSrcweir       m_typeInfos( 2 )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     m_typeInfos[ 0 ] = m_xBasicLibTypeInfo;
171cdf0e10cSrcweir     m_typeInfos[ 1 ] = m_xDialogLibTypeInfo;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     OSL_ASSERT( ! transientMode() );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     if (!transientMode())
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         OUString dbFile = makeURL(getCachePath(), OUSTR("backenddb.xml"));
178cdf0e10cSrcweir         m_backendDb.reset(
179cdf0e10cSrcweir             new ScriptBackendDb(getComponentContext(), dbFile));
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir }
addDataToDb(OUString const & url)183cdf0e10cSrcweir void BackendImpl::addDataToDb(OUString const & url)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir     if (m_backendDb.get())
186cdf0e10cSrcweir         m_backendDb->addEntry(url);
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
hasActiveEntry(OUString const & url)189cdf0e10cSrcweir bool BackendImpl::hasActiveEntry(OUString const & url)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir     if (m_backendDb.get())
192cdf0e10cSrcweir         return m_backendDb->hasActiveEntry(url);
193cdf0e10cSrcweir     return false;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir // XUpdatable
197cdf0e10cSrcweir //______________________________________________________________________________
update()198cdf0e10cSrcweir void BackendImpl::update() throw (RuntimeException)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir 	// Nothing to do here after fixing i70283!?
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir // XPackageRegistry
204cdf0e10cSrcweir //______________________________________________________________________________
205cdf0e10cSrcweir Sequence< Reference<deployment::XPackageTypeInfo> >
getSupportedPackageTypes()206cdf0e10cSrcweir BackendImpl::getSupportedPackageTypes() throw (RuntimeException)
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     return m_typeInfos;
209cdf0e10cSrcweir }
revokeEntryFromDb(OUString const & url)210cdf0e10cSrcweir void BackendImpl::revokeEntryFromDb(OUString const & url)
211cdf0e10cSrcweir {
212cdf0e10cSrcweir     if (m_backendDb.get())
213cdf0e10cSrcweir         m_backendDb->revokeEntry(url);
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
packageRemoved(OUString const & url,OUString const &)216cdf0e10cSrcweir void BackendImpl::packageRemoved(OUString const & url, OUString const & /*mediaType*/)
217cdf0e10cSrcweir         throw (deployment::DeploymentException,
218cdf0e10cSrcweir                uno::RuntimeException)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     if (m_backendDb.get())
221cdf0e10cSrcweir         m_backendDb->removeEntry(url);
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir // PackageRegistryBackend
225cdf0e10cSrcweir //______________________________________________________________________________
bindPackage_(OUString const & url,OUString const & mediaType_,sal_Bool bRemoved,OUString const & identifier,Reference<XCommandEnvironment> const & xCmdEnv)226cdf0e10cSrcweir Reference<deployment::XPackage> BackendImpl::bindPackage_(
227cdf0e10cSrcweir     OUString const & url, OUString const & mediaType_,
228cdf0e10cSrcweir     sal_Bool bRemoved, OUString const & identifier,
229cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     OUString mediaType( mediaType_ );
232cdf0e10cSrcweir     if (mediaType.getLength() == 0)
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         // detect media-type:
235cdf0e10cSrcweir         ::ucbhelper::Content ucbContent;
236cdf0e10cSrcweir         if (create_ucb_content( &ucbContent, url, xCmdEnv ) &&
237cdf0e10cSrcweir             ucbContent.isFolder())
238cdf0e10cSrcweir         {
239cdf0e10cSrcweir             // probe for script.xlb:
240cdf0e10cSrcweir             if (create_ucb_content(
241cdf0e10cSrcweir                     0, makeURL( url, OUSTR("script.xlb") ),
242cdf0e10cSrcweir                     xCmdEnv, false /* no throw */ ))
243cdf0e10cSrcweir                 mediaType = OUSTR("application/vnd.sun.star.basic-library");
244cdf0e10cSrcweir             // probe for dialog.xlb:
245cdf0e10cSrcweir             else if (create_ucb_content(
246cdf0e10cSrcweir                          0, makeURL( url, OUSTR("dialog.xlb") ),
247cdf0e10cSrcweir                          xCmdEnv, false /* no throw */ ))
248cdf0e10cSrcweir                 mediaType = OUSTR("application/vnd.sun.star.dialog-library");
249cdf0e10cSrcweir         }
250cdf0e10cSrcweir         if (mediaType.getLength() == 0)
251cdf0e10cSrcweir             throw lang::IllegalArgumentException(
252cdf0e10cSrcweir                 StrCannotDetectMediaType::get() + url,
253cdf0e10cSrcweir                 static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     String type, subType;
257cdf0e10cSrcweir     INetContentTypeParameterList params;
258cdf0e10cSrcweir     if (INetContentTypes::parse( mediaType, type, subType, &params ))
259cdf0e10cSrcweir     {
260cdf0e10cSrcweir         if (type.EqualsIgnoreCaseAscii("application"))
261cdf0e10cSrcweir         {
262cdf0e10cSrcweir             OUString dialogURL( makeURL( url, OUSTR("dialog.xlb") ) );
263cdf0e10cSrcweir             if (! create_ucb_content(
264cdf0e10cSrcweir                     0, dialogURL, xCmdEnv, false /* no throw */ )) {
265cdf0e10cSrcweir                 dialogURL = OUString();
266cdf0e10cSrcweir             }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir             if (subType.EqualsIgnoreCaseAscii("vnd.sun.star.basic-library"))
269cdf0e10cSrcweir             {
270cdf0e10cSrcweir                 OUString scriptURL( makeURL( url, OUSTR("script.xlb")));
271cdf0e10cSrcweir                 if (! create_ucb_content(
272cdf0e10cSrcweir                         0, scriptURL, xCmdEnv, false /* no throw */ )) {
273cdf0e10cSrcweir                     scriptURL = OUString();
274cdf0e10cSrcweir                 }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir                 return new PackageImpl(
277cdf0e10cSrcweir                     this, url, xCmdEnv, scriptURL,
278cdf0e10cSrcweir                     dialogURL, bRemoved, identifier);
279cdf0e10cSrcweir             }
280cdf0e10cSrcweir             else if (subType.EqualsIgnoreCaseAscii(
281cdf0e10cSrcweir                          "vnd.sun.star.dialog-library")) {
282cdf0e10cSrcweir                 return new PackageImpl(
283cdf0e10cSrcweir                     this, url, xCmdEnv,
284cdf0e10cSrcweir                     OUString() /* no script lib */,
285cdf0e10cSrcweir                     dialogURL,
286cdf0e10cSrcweir                     bRemoved, identifier);
287cdf0e10cSrcweir             }
288cdf0e10cSrcweir         }
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir     throw lang::IllegalArgumentException(
291cdf0e10cSrcweir         StrUnsupportedMediaType::get() + mediaType,
292cdf0e10cSrcweir         static_cast<OWeakObject *>(this),
293cdf0e10cSrcweir         static_cast<sal_Int16>(-1) );
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir //##############################################################################
297cdf0e10cSrcweir 
298cdf0e10cSrcweir // Package
getMyBackend() const299cdf0e10cSrcweir BackendImpl * BackendImpl::PackageImpl::getMyBackend() const
300cdf0e10cSrcweir {
301cdf0e10cSrcweir     BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
302cdf0e10cSrcweir     if (NULL == pBackend)
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir         //May throw a DisposedException
305cdf0e10cSrcweir         check();
306cdf0e10cSrcweir         //We should never get here...
307cdf0e10cSrcweir         throw RuntimeException(
308cdf0e10cSrcweir             OUSTR("Failed to get the BackendImpl"),
309cdf0e10cSrcweir             static_cast<OWeakObject*>(const_cast<PackageImpl *>(this)));
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir     return pBackend;
312cdf0e10cSrcweir }
313cdf0e10cSrcweir //______________________________________________________________________________
314cdf0e10cSrcweir beans::Optional< beans::Ambiguous<sal_Bool> >
isRegistered_(::osl::ResettableMutexGuard &,::rtl::Reference<AbortChannel> const &,Reference<XCommandEnvironment> const & xCmdEnv)315cdf0e10cSrcweir BackendImpl::PackageImpl::isRegistered_(
316cdf0e10cSrcweir     ::osl::ResettableMutexGuard &,
317cdf0e10cSrcweir     ::rtl::Reference<AbortChannel> const &,
318cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
319cdf0e10cSrcweir {
320cdf0e10cSrcweir 	(void)xCmdEnv;
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     BackendImpl * that = getMyBackend();
323cdf0e10cSrcweir 	Reference< deployment::XPackage > xThisPackage( this );
324cdf0e10cSrcweir 
325cdf0e10cSrcweir     bool registered = that->hasActiveEntry(getURL());
326cdf0e10cSrcweir     return beans::Optional< beans::Ambiguous<sal_Bool> >(
327cdf0e10cSrcweir         true /* IsPresent */,
328cdf0e10cSrcweir         beans::Ambiguous<sal_Bool>( registered, false /* IsAmbiguous */ ) );
329cdf0e10cSrcweir }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir //______________________________________________________________________________
processPackage_(::osl::ResettableMutexGuard &,bool doRegisterPackage,bool startup,::rtl::Reference<AbortChannel> const &,Reference<XCommandEnvironment> const & xCmdEnv)332cdf0e10cSrcweir void BackendImpl::PackageImpl::processPackage_(
333cdf0e10cSrcweir     ::osl::ResettableMutexGuard &,
334cdf0e10cSrcweir     bool doRegisterPackage,
335cdf0e10cSrcweir     bool startup,
336cdf0e10cSrcweir     ::rtl::Reference<AbortChannel> const &,
337cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
338cdf0e10cSrcweir {
339cdf0e10cSrcweir 	(void)xCmdEnv;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir     BackendImpl * that = getMyBackend();
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	Reference< deployment::XPackage > xThisPackage( this );
344cdf0e10cSrcweir 	Reference<XComponentContext> const & xComponentContext = that->getComponentContext();
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 	bool bScript = (m_scriptURL.getLength() > 0);
347cdf0e10cSrcweir     Reference<css::script::XLibraryContainer3> xScriptLibs;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 	bool bDialog = (m_dialogURL.getLength() > 0);
350cdf0e10cSrcweir     Reference<css::script::XLibraryContainer3> xDialogLibs;
351cdf0e10cSrcweir 
352cdf0e10cSrcweir 	bool bRunning = office_is_running();
353cdf0e10cSrcweir     if( bRunning )
354cdf0e10cSrcweir 	{
355cdf0e10cSrcweir 		if( bScript )
356cdf0e10cSrcweir 		{
357cdf0e10cSrcweir 			xScriptLibs.set(
358cdf0e10cSrcweir 				xComponentContext->getServiceManager()->createInstanceWithContext(
359cdf0e10cSrcweir 					OUSTR("com.sun.star.script.ApplicationScriptLibraryContainer"),
360cdf0e10cSrcweir 					xComponentContext ), UNO_QUERY_THROW );
361cdf0e10cSrcweir 		}
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 		if( bDialog )
364cdf0e10cSrcweir 		{
365cdf0e10cSrcweir 			xDialogLibs.set(
366cdf0e10cSrcweir 				xComponentContext->getServiceManager()->createInstanceWithContext(
367cdf0e10cSrcweir 					OUSTR("com.sun.star.script.ApplicationDialogLibraryContainer"),
368cdf0e10cSrcweir 					xComponentContext ), UNO_QUERY_THROW );
369cdf0e10cSrcweir 		}
370cdf0e10cSrcweir 	}
371cdf0e10cSrcweir     bool bRegistered = getMyBackend()->hasActiveEntry(getURL());
372cdf0e10cSrcweir 	if( !doRegisterPackage )
373cdf0e10cSrcweir 	{
374cdf0e10cSrcweir         //We cannot just call removeLibrary(name) because this could remove a
375cdf0e10cSrcweir         //script which was added by an extension in a different repository. For
376cdf0e10cSrcweir         //example, extension foo is contained in the bundled repository and then
377cdf0e10cSrcweir         //the user adds it it to the user repository. The extension manager will
378cdf0e10cSrcweir         //then register the new script and revoke the script from the bundled
379cdf0e10cSrcweir         //extension. removeLibrary(name) would now remove the script from the
380cdf0e10cSrcweir         //user repository. That is, the script of the newly added user extension does
381cdf0e10cSrcweir         //not work anymore. Therefore we must check if the currently active
382cdf0e10cSrcweir         //script comes in fact from the currently processed extension.
383cdf0e10cSrcweir 
384cdf0e10cSrcweir 		if (bRegistered)
385cdf0e10cSrcweir 		{
386cdf0e10cSrcweir             //we also prevent and live deployment at startup
387cdf0e10cSrcweir             if (!isRemoved() && !startup)
388cdf0e10cSrcweir             {
389cdf0e10cSrcweir                 if (bScript && xScriptLibs.is() && xScriptLibs->hasByName(m_name))
390cdf0e10cSrcweir                 {
391cdf0e10cSrcweir                     const OUString sScriptUrl = xScriptLibs->getOriginalLibraryLinkURL(m_name);
392cdf0e10cSrcweir                     if (sScriptUrl.equals(m_scriptURL))
393cdf0e10cSrcweir                         xScriptLibs->removeLibrary(m_name);
394cdf0e10cSrcweir                 }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir                 if (bDialog && xDialogLibs.is() && xDialogLibs->hasByName(m_dialogName))
397cdf0e10cSrcweir                 {
398cdf0e10cSrcweir                     const OUString sDialogUrl = xDialogLibs->getOriginalLibraryLinkURL(m_dialogName);
399cdf0e10cSrcweir                     if (sDialogUrl.equals(m_dialogURL))
400cdf0e10cSrcweir                         xDialogLibs->removeLibrary(m_dialogName);
401cdf0e10cSrcweir                 }
402cdf0e10cSrcweir             }
403cdf0e10cSrcweir             getMyBackend()->revokeEntryFromDb(getURL());
404cdf0e10cSrcweir             return;
405cdf0e10cSrcweir         }
406cdf0e10cSrcweir     }
407cdf0e10cSrcweir 	if (bRegistered)
408cdf0e10cSrcweir 		return;		// Already registered
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 	// Update LibraryContainer
411cdf0e10cSrcweir 	bool bScriptSuccess = false;
412cdf0e10cSrcweir 	const bool bReadOnly = false;
413cdf0e10cSrcweir 
414cdf0e10cSrcweir     bool bDialogSuccess = false;
415cdf0e10cSrcweir     if (!startup)
416cdf0e10cSrcweir     {
417cdf0e10cSrcweir         //If there is a bundled extension, and the user installes the same extension
418cdf0e10cSrcweir         //then the script from the bundled extension must be removed. If this does not work
419cdf0e10cSrcweir         //then live deployment does not work for scripts.
420cdf0e10cSrcweir         if (bScript && xScriptLibs.is())
421cdf0e10cSrcweir         {
422cdf0e10cSrcweir             bool bCanAdd = true;
423cdf0e10cSrcweir             if (xScriptLibs->hasByName(m_name))
424cdf0e10cSrcweir             {
425cdf0e10cSrcweir                 const OUString sOriginalUrl = xScriptLibs->getOriginalLibraryLinkURL(m_name);
426cdf0e10cSrcweir                 //We assume here that library names in extensions are unique, which may not be the case
427cdf0e10cSrcweir                 //ToDo: If the script exist in another extension, then both extensions must have the
428cdf0e10cSrcweir                 //same id
429cdf0e10cSrcweir                 if (sOriginalUrl.match(OUSTR("vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE"))
430cdf0e10cSrcweir                     || sOriginalUrl.match(OUSTR("vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE"))
431cdf0e10cSrcweir                     || sOriginalUrl.match(OUSTR("vnd.sun.star.expand:$BUNDLED_EXTENSIONS")))
432cdf0e10cSrcweir                 {
433cdf0e10cSrcweir                     xScriptLibs->removeLibrary(m_name);
434cdf0e10cSrcweir                     bCanAdd = true;
435cdf0e10cSrcweir                 }
436cdf0e10cSrcweir                 else
437cdf0e10cSrcweir                 {
438cdf0e10cSrcweir                     bCanAdd = false;
439cdf0e10cSrcweir                 }
440cdf0e10cSrcweir             }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir             if (bCanAdd)
443cdf0e10cSrcweir             {
444cdf0e10cSrcweir                 xScriptLibs->createLibraryLink( m_name, m_scriptURL, bReadOnly );
445cdf0e10cSrcweir                 bScriptSuccess = xScriptLibs->hasByName( m_name );
446cdf0e10cSrcweir             }
447cdf0e10cSrcweir         }
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 
450cdf0e10cSrcweir         if (bDialog && xDialogLibs.is())
451cdf0e10cSrcweir         {
452cdf0e10cSrcweir             bool bCanAdd = true;
453cdf0e10cSrcweir             if (xDialogLibs->hasByName(m_dialogName))
454cdf0e10cSrcweir             {
455cdf0e10cSrcweir                 const OUString sOriginalUrl = xDialogLibs->getOriginalLibraryLinkURL(m_dialogName);
456cdf0e10cSrcweir                 //We assume here that library names in extensions are unique, which may not be the case
457cdf0e10cSrcweir                 //ToDo: If the script exist in another extension, then both extensions must have the
458cdf0e10cSrcweir                 //same id
459cdf0e10cSrcweir                 if (sOriginalUrl.match(OUSTR("vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE"))
460cdf0e10cSrcweir                     || sOriginalUrl.match(OUSTR("vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE"))
461cdf0e10cSrcweir                     || sOriginalUrl.match(OUSTR("vnd.sun.star.expand:$BUNDLED_EXTENSIONS")))
462cdf0e10cSrcweir                 {
463cdf0e10cSrcweir                     xDialogLibs->removeLibrary(m_dialogName);
464cdf0e10cSrcweir                     bCanAdd = true;
465cdf0e10cSrcweir                 }
466cdf0e10cSrcweir                 else
467cdf0e10cSrcweir                 {
468cdf0e10cSrcweir                     bCanAdd = false;
469cdf0e10cSrcweir                 }
470cdf0e10cSrcweir             }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir             if (bCanAdd)
473cdf0e10cSrcweir             {
474cdf0e10cSrcweir                 xDialogLibs->createLibraryLink( m_dialogName, m_dialogURL, bReadOnly );
475cdf0e10cSrcweir                 bDialogSuccess = xDialogLibs->hasByName(m_dialogName);
476cdf0e10cSrcweir             }
477cdf0e10cSrcweir         }
478cdf0e10cSrcweir     }
479cdf0e10cSrcweir 	bool bSuccess = bScript || bDialog;		// Something must have happened
480cdf0e10cSrcweir 	if( bRunning && !startup)
481cdf0e10cSrcweir 		if( (bScript && !bScriptSuccess) || (bDialog && !bDialogSuccess) )
482cdf0e10cSrcweir 			bSuccess = false;
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 	if (bSuccess)
485cdf0e10cSrcweir         getMyBackend()->addDataToDb(getURL());
486cdf0e10cSrcweir }
487cdf0e10cSrcweir 
488cdf0e10cSrcweir } // anon namespace
489cdf0e10cSrcweir 
490cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
491cdf0e10cSrcweir sdecl::class_<BackendImpl, sdecl::with_args<true> > serviceBI;
492cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
493cdf0e10cSrcweir     serviceBI,
494cdf0e10cSrcweir     "com.sun.star.comp.deployment.script.PackageRegistryBackend",
495cdf0e10cSrcweir     BACKEND_SERVICE_NAME );
496cdf0e10cSrcweir 
497cdf0e10cSrcweir } // namespace script
498cdf0e10cSrcweir } // namespace backend
499cdf0e10cSrcweir } // namespace dp_registry
500cdf0e10cSrcweir 
501