1cdf0e10cSrcweir /*************************************************************************
2cdf0e10cSrcweir  *
3cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4cdf0e10cSrcweir  *
5cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6cdf0e10cSrcweir  *
7cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8cdf0e10cSrcweir  *
9cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10cdf0e10cSrcweir  *
11cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14cdf0e10cSrcweir  *
15cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20cdf0e10cSrcweir  *
21cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  ************************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "comphelper/servicedecl.hxx"
34cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
35cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
36cdf0e10cSrcweir #include "com/sun/star/deployment/ExtensionManager.hpp"
37cdf0e10cSrcweir #include "com/sun/star/deployment/XExtensionManager.hpp"
38cdf0e10cSrcweir #include "com/sun/star/deployment/thePackageManagerFactory.hpp"
39cdf0e10cSrcweir #include "com/sun/star/deployment/XPackageManager.hpp"
40cdf0e10cSrcweir #include "com/sun/star/deployment/XPackageManagerFactory.hpp"
41cdf0e10cSrcweir #include "com/sun/star/deployment/XPackage.hpp"
42cdf0e10cSrcweir #include "com/sun/star/deployment/InstallException.hpp"
43cdf0e10cSrcweir #include "com/sun/star/deployment/VersionException.hpp"
44cdf0e10cSrcweir #include "com/sun/star/deployment/LicenseException.hpp"
45cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
46cdf0e10cSrcweir #include "com/sun/star/registry/XRegistryKey.hpp"
47cdf0e10cSrcweir #include "com/sun/star/beans/Optional.hpp"
48cdf0e10cSrcweir #include "com/sun/star/task/XInteractionApprove.hpp"
49cdf0e10cSrcweir #include "com/sun/star/beans/Ambiguous.hpp"
50cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
51cdf0e10cSrcweir #include "com/sun/star/io/XInputStream.hpp"
52cdf0e10cSrcweir #include "com/sun/star/util/XModifyBroadcaster.hpp"
53cdf0e10cSrcweir #include "comphelper/sequence.hxx"
54cdf0e10cSrcweir #include "xmlscript/xml_helper.hxx"
55cdf0e10cSrcweir #include "osl/diagnose.h"
56cdf0e10cSrcweir #include "dp_interact.h"
57cdf0e10cSrcweir #include "dp_resource.h"
58cdf0e10cSrcweir #include "dp_ucb.h"
59cdf0e10cSrcweir #include "dp_identifier.hxx"
60cdf0e10cSrcweir #include "dp_descriptioninfoset.hxx"
61cdf0e10cSrcweir #include "dp_extensionmanager.hxx"
62cdf0e10cSrcweir #include "dp_commandenvironments.hxx"
63cdf0e10cSrcweir #include "dp_properties.hxx"
64cdf0e10cSrcweir #include "boost/bind.hpp"
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #include <list>
67cdf0e10cSrcweir #include <hash_map>
68cdf0e10cSrcweir #include <algorithm>
69cdf0e10cSrcweir 
70cdf0e10cSrcweir namespace deploy = com::sun::star::deployment;
71cdf0e10cSrcweir namespace lang  = com::sun::star::lang;
72cdf0e10cSrcweir namespace registry = com::sun::star::registry;
73cdf0e10cSrcweir namespace task = com::sun::star::task;
74cdf0e10cSrcweir namespace ucb = com::sun::star::ucb;
75cdf0e10cSrcweir namespace uno = com::sun::star::uno;
76cdf0e10cSrcweir namespace beans = com::sun::star::beans;
77cdf0e10cSrcweir namespace util = com::sun::star::util;
78cdf0e10cSrcweir namespace css = com::sun::star;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //#define OUSTR(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
82cdf0e10cSrcweir 
83cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
84cdf0e10cSrcweir using ::rtl::OUString;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir namespace {
87cdf0e10cSrcweir 
88cdf0e10cSrcweir struct CompIdentifiers
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     bool operator() (::std::vector<Reference<deploy::XPackage> > const & a,
91cdf0e10cSrcweir                      ::std::vector<Reference<deploy::XPackage> > const & b)
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             if (getName(a).compareTo(getName(b)) < 0)
95cdf0e10cSrcweir                 return true;
96cdf0e10cSrcweir             return false;
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     OUString getName(::std::vector<Reference<deploy::XPackage> > const & a);
100cdf0e10cSrcweir };
101cdf0e10cSrcweir 
102cdf0e10cSrcweir OUString CompIdentifiers::getName(::std::vector<Reference<deploy::XPackage> > const & a)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     OSL_ASSERT(a.size() == 3);
105cdf0e10cSrcweir     //get the first non-null reference
106cdf0e10cSrcweir     Reference<deploy::XPackage>  extension;
107cdf0e10cSrcweir     ::std::vector<Reference<deploy::XPackage> >::const_iterator it = a.begin();
108cdf0e10cSrcweir     for (; it != a.end(); it++)
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         if (it->is())
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             extension = *it;
113cdf0e10cSrcweir             break;
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir     OSL_ASSERT(extension.is());
117cdf0e10cSrcweir     return extension->getDisplayName();
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir void writeLastModified(OUString & url, Reference<ucb::XCommandEnvironment> const & xCmdEnv)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     //Write the lastmodified file
123cdf0e10cSrcweir     try {
124cdf0e10cSrcweir         ::rtl::Bootstrap::expandMacros(url);
125cdf0e10cSrcweir         ::ucbhelper::Content ucbStamp(url, xCmdEnv );
126cdf0e10cSrcweir         dp_misc::erase_path( url, xCmdEnv );
127cdf0e10cSrcweir         ::rtl::OString stamp("1" );
128cdf0e10cSrcweir         Reference<css::io::XInputStream> xData(
129cdf0e10cSrcweir             ::xmlscript::createInputStream(
130cdf0e10cSrcweir                 ::rtl::ByteSequence(
131cdf0e10cSrcweir                     reinterpret_cast<sal_Int8 const *>(stamp.getStr()),
132cdf0e10cSrcweir                     stamp.getLength() ) ) );
133cdf0e10cSrcweir         ucbStamp.writeStream( xData, true /* replace existing */ );
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir     catch(...)
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         uno::Any exc(::cppu::getCaughtException());
138cdf0e10cSrcweir         throw deploy::DeploymentException(
139cdf0e10cSrcweir             OUSTR("Failed to update") + url, 0, exc);
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir class ExtensionRemoveGuard
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackage> m_extension;
146cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackageManager> m_xPackageManager;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir public:
1497bc7b19fSMichael Stahl     ExtensionRemoveGuard(){};
150cdf0e10cSrcweir     ExtensionRemoveGuard(
151cdf0e10cSrcweir         css::uno::Reference<css::deployment::XPackage> const & extension,
152cdf0e10cSrcweir         css::uno::Reference<css::deployment::XPackageManager> const & xPackageManager):
153cdf0e10cSrcweir         m_extension(extension), m_xPackageManager(xPackageManager) {}
154cdf0e10cSrcweir     ~ExtensionRemoveGuard();
155cdf0e10cSrcweir 
1567bc7b19fSMichael Stahl     void set(css::uno::Reference<css::deployment::XPackage> const & extension,
1577bc7b19fSMichael Stahl              css::uno::Reference<css::deployment::XPackageManager> const & xPackageManager) {
158cdf0e10cSrcweir         m_extension = extension;
1597bc7b19fSMichael Stahl         m_xPackageManager = xPackageManager;
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir };
162cdf0e10cSrcweir 
163cdf0e10cSrcweir ExtensionRemoveGuard::~ExtensionRemoveGuard()
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     try {
1667bc7b19fSMichael Stahl         OSL_ASSERT(!(m_extension.is() && !m_xPackageManager.is()));
167cdf0e10cSrcweir         if (m_xPackageManager.is() && m_extension.is())
168cdf0e10cSrcweir             m_xPackageManager->removePackage(
169cdf0e10cSrcweir                 dp_misc::getIdentifier(m_extension), ::rtl::OUString(),
170cdf0e10cSrcweir                 css::uno::Reference<css::task::XAbortChannel>(),
171cdf0e10cSrcweir                 css::uno::Reference<css::ucb::XCommandEnvironment>());
172cdf0e10cSrcweir     } catch (...) {
173cdf0e10cSrcweir         OSL_ASSERT(0);
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir } //end namespace
178cdf0e10cSrcweir 
179cdf0e10cSrcweir namespace dp_manager {
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 
183cdf0e10cSrcweir //------------------------------------------------------------------------------
184cdf0e10cSrcweir 
185cdf0e10cSrcweir //ToDo: bundled extension
186cdf0e10cSrcweir ExtensionManager::ExtensionManager( Reference< uno::XComponentContext > const& xContext) :
187cdf0e10cSrcweir     ::cppu::WeakComponentImplHelper1< css::deployment::XExtensionManager >(getMutex()),
188cdf0e10cSrcweir     m_xContext( xContext )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     m_xPackageManagerFactory = deploy::thePackageManagerFactory::get(m_xContext);
191cdf0e10cSrcweir     OSL_ASSERT(m_xPackageManagerFactory.is());
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     m_repositoryNames.push_back(OUSTR("user"));
194cdf0e10cSrcweir     m_repositoryNames.push_back(OUSTR("shared"));
195cdf0e10cSrcweir     m_repositoryNames.push_back(OUSTR("bundled"));
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir //------------------------------------------------------------------------------
199cdf0e10cSrcweir 
200cdf0e10cSrcweir ExtensionManager::~ExtensionManager()
201cdf0e10cSrcweir {
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir Reference<deploy::XPackageManager> ExtensionManager::getUserRepository()
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("user"));
207cdf0e10cSrcweir }
208cdf0e10cSrcweir Reference<deploy::XPackageManager>  ExtensionManager::getSharedRepository()
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("shared"));
211cdf0e10cSrcweir }
212cdf0e10cSrcweir Reference<deploy::XPackageManager>  ExtensionManager::getBundledRepository()
213cdf0e10cSrcweir {
214cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("bundled"));
215cdf0e10cSrcweir }
216cdf0e10cSrcweir Reference<deploy::XPackageManager>  ExtensionManager::getTmpRepository()
217cdf0e10cSrcweir {
218cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("tmp"));
219cdf0e10cSrcweir }
2207bc7b19fSMichael Stahl Reference<deploy::XPackageManager>  ExtensionManager::getBakRepository()
2217bc7b19fSMichael Stahl {
2227bc7b19fSMichael Stahl     return m_xPackageManagerFactory->getPackageManager(OUSTR("bak"));
2237bc7b19fSMichael Stahl }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir Reference<task::XAbortChannel> ExtensionManager::createAbortChannel()
226cdf0e10cSrcweir     throw (uno::RuntimeException)
227cdf0e10cSrcweir {
228cdf0e10cSrcweir     return new dp_misc::AbortChannel;
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir css::uno::Reference<css::deployment::XPackageManager>
232cdf0e10cSrcweir ExtensionManager::getPackageManager(::rtl::OUString const & repository)
233cdf0e10cSrcweir     throw (css::lang::IllegalArgumentException)
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     Reference<deploy::XPackageManager> xPackageManager;
236cdf0e10cSrcweir     if (repository.equals(OUSTR("user")))
237cdf0e10cSrcweir         xPackageManager = getUserRepository();
238cdf0e10cSrcweir     else if (repository.equals(OUSTR("shared")))
239cdf0e10cSrcweir         xPackageManager = getSharedRepository();
240cdf0e10cSrcweir     else if (repository.equals(OUSTR("bundled")))
241cdf0e10cSrcweir         xPackageManager = getBundledRepository();
242*8402cd44SMichael Stahl     else if (repository.equals(OUSTR("tmp")))
243*8402cd44SMichael Stahl         xPackageManager = getTmpRepository();
244*8402cd44SMichael Stahl     else if (repository.equals(OUSTR("bak")))
245*8402cd44SMichael Stahl         xPackageManager = getBakRepository();
246cdf0e10cSrcweir     else
247cdf0e10cSrcweir         throw lang::IllegalArgumentException(
248cdf0e10cSrcweir             OUSTR("No valid repository name provided."),
249cdf0e10cSrcweir             static_cast<cppu::OWeakObject*>(this), 0);
250cdf0e10cSrcweir     return xPackageManager;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 
254cdf0e10cSrcweir /*
255cdf0e10cSrcweir   Enters the XPackage objects into a map. They must be all from the
256cdf0e10cSrcweir   same repository. The value type of the map is a vector, where each vector
257cdf0e10cSrcweir   represents an extension with a particular identifier. The first member
258cdf0e10cSrcweir   is represents the user extension, the second the shared extension and the
259cdf0e10cSrcweir   third the bundled extension.
260cdf0e10cSrcweir  */
261cdf0e10cSrcweir void ExtensionManager::addExtensionsToMap(
262cdf0e10cSrcweir     id2extensions & mapExt,
263cdf0e10cSrcweir     uno::Sequence<Reference<deploy::XPackage> > const & seqExt,
264cdf0e10cSrcweir     OUString const & repository)
265cdf0e10cSrcweir {
266cdf0e10cSrcweir     //Determine the index in the vector where these extensions are to be
267cdf0e10cSrcweir     //added.
268cdf0e10cSrcweir     ::std::list<OUString>::const_iterator citNames =
269cdf0e10cSrcweir         m_repositoryNames.begin();
270cdf0e10cSrcweir     int index = 0;
271cdf0e10cSrcweir     for (;citNames != m_repositoryNames.end(); citNames++, index++)
272cdf0e10cSrcweir     {
273cdf0e10cSrcweir         if (citNames->equals(repository))
274cdf0e10cSrcweir             break;
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     for (int i = 0; i < seqExt.getLength(); i++)
278cdf0e10cSrcweir     {
279cdf0e10cSrcweir         Reference<deploy::XPackage> const & xExtension = seqExt[i];
280cdf0e10cSrcweir         OUString id = dp_misc::getIdentifier(xExtension);
281cdf0e10cSrcweir         id2extensions::iterator ivec =  mapExt.find(id);
282cdf0e10cSrcweir         if (ivec == mapExt.end())
283cdf0e10cSrcweir         {
284cdf0e10cSrcweir             ::std::vector<Reference<deploy::XPackage> > vec(3);
285cdf0e10cSrcweir             vec[index] = xExtension;
286cdf0e10cSrcweir             mapExt[id] = vec;
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir         else
289cdf0e10cSrcweir         {
290cdf0e10cSrcweir             ivec->second[index] = xExtension;
291cdf0e10cSrcweir         }
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir /*
296cdf0e10cSrcweir    returns a list containing extensions with the same identifier from
297cdf0e10cSrcweir    all repositories (user, shared, bundled) If one repository does not
298cdf0e10cSrcweir    have this extension, then the list contains an empty Referenc. The list
299cdf0e10cSrcweir    is ordered according to the priority of the repostories:
300cdf0e10cSrcweir    1. user
301cdf0e10cSrcweir    2. shared
302cdf0e10cSrcweir    3. bundled
303cdf0e10cSrcweir 
304cdf0e10cSrcweir    The number of elements is always three, unless the number of repository
305cdf0e10cSrcweir    changes.
306cdf0e10cSrcweir  */
307cdf0e10cSrcweir ::std::list<Reference<deploy::XPackage> >
308cdf0e10cSrcweir     ExtensionManager::getExtensionsWithSameId(
309cdf0e10cSrcweir         OUString const & identifier, OUString const & fileName,
310cdf0e10cSrcweir         Reference< ucb::XCommandEnvironment> const & /*xCmdEnv*/)
311cdf0e10cSrcweir 
312cdf0e10cSrcweir {
313cdf0e10cSrcweir     ::std::list<Reference<deploy::XPackage> > extensionList;
314cdf0e10cSrcweir     try
315cdf0e10cSrcweir     {   //will throw an exception if the extension does not exist
316cdf0e10cSrcweir         extensionList.push_back(getUserRepository()->getDeployedPackage(
317cdf0e10cSrcweir             identifier, fileName, Reference<ucb::XCommandEnvironment>()));
318cdf0e10cSrcweir     } catch(lang::IllegalArgumentException &)
319cdf0e10cSrcweir     {
320cdf0e10cSrcweir         extensionList.push_back(Reference<deploy::XPackage>());
321cdf0e10cSrcweir     }
322cdf0e10cSrcweir     try
323cdf0e10cSrcweir     {
324cdf0e10cSrcweir         extensionList.push_back(getSharedRepository()->getDeployedPackage(
325cdf0e10cSrcweir             identifier, fileName, Reference<ucb::XCommandEnvironment>()));
326cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &)
327cdf0e10cSrcweir     {
328cdf0e10cSrcweir         extensionList.push_back(Reference<deploy::XPackage>());
329cdf0e10cSrcweir     }
330cdf0e10cSrcweir     try
331cdf0e10cSrcweir     {
332cdf0e10cSrcweir        extensionList.push_back(getBundledRepository()->getDeployedPackage(
333cdf0e10cSrcweir            identifier, fileName, Reference<ucb::XCommandEnvironment>()));
334cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &)
335cdf0e10cSrcweir     {
336cdf0e10cSrcweir         extensionList.push_back(Reference<deploy::XPackage>());
337cdf0e10cSrcweir     }
338cdf0e10cSrcweir     OSL_ASSERT(extensionList.size() == 3);
339cdf0e10cSrcweir     return extensionList;
340cdf0e10cSrcweir }
341cdf0e10cSrcweir 
342cdf0e10cSrcweir uno::Sequence<Reference<deploy::XPackage> >
343cdf0e10cSrcweir ExtensionManager::getExtensionsWithSameIdentifier(
344cdf0e10cSrcweir         OUString const & identifier,
345cdf0e10cSrcweir         OUString const & fileName,
346cdf0e10cSrcweir         Reference< ucb::XCommandEnvironment> const & xCmdEnv )
347cdf0e10cSrcweir         throw (
348cdf0e10cSrcweir             deploy::DeploymentException,
349cdf0e10cSrcweir             ucb::CommandFailedException,
350cdf0e10cSrcweir             lang::IllegalArgumentException,
351cdf0e10cSrcweir             uno::RuntimeException)
352cdf0e10cSrcweir {
353cdf0e10cSrcweir     try
354cdf0e10cSrcweir     {
355cdf0e10cSrcweir         ::std::list<Reference<deploy::XPackage> > listExtensions =
356cdf0e10cSrcweir             getExtensionsWithSameId(
357cdf0e10cSrcweir                 identifier, fileName, xCmdEnv);
358cdf0e10cSrcweir         sal_Bool bHasExtension = false;
359cdf0e10cSrcweir 
360cdf0e10cSrcweir         //throw an IllegalArgumentException if there is no extension at all.
361cdf0e10cSrcweir         typedef  ::std::list<Reference<deploy::XPackage> >::const_iterator CIT;
362cdf0e10cSrcweir         for (CIT i = listExtensions.begin(); i != listExtensions.end(); i++)
363cdf0e10cSrcweir             bHasExtension |= i->is();
364cdf0e10cSrcweir         if (!bHasExtension)
365cdf0e10cSrcweir             throw lang::IllegalArgumentException(
366cdf0e10cSrcweir                 OUSTR("Could not find extension: ") + identifier + OUSTR(", ") + fileName,
367cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), -1);
368cdf0e10cSrcweir 
369cdf0e10cSrcweir         return comphelper::containerToSequence<
370cdf0e10cSrcweir             Reference<deploy::XPackage>,
371cdf0e10cSrcweir             ::std::list<Reference<deploy::XPackage> >
372cdf0e10cSrcweir             > (listExtensions);
373cdf0e10cSrcweir     }
374cdf0e10cSrcweir     catch (deploy::DeploymentException & )
375cdf0e10cSrcweir     {
376cdf0e10cSrcweir         throw;
377cdf0e10cSrcweir     }
378cdf0e10cSrcweir     catch ( ucb::CommandFailedException & )
379cdf0e10cSrcweir     {
380cdf0e10cSrcweir         throw;
381cdf0e10cSrcweir     }
382cdf0e10cSrcweir     catch (lang::IllegalArgumentException &)
383cdf0e10cSrcweir     {
384cdf0e10cSrcweir         throw;
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir     catch (...)
387cdf0e10cSrcweir     {
388cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
389cdf0e10cSrcweir         throw deploy::DeploymentException(
390cdf0e10cSrcweir             OUSTR("Extension Manager: exception during getExtensionsWithSameIdentifier"),
391cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 
397cdf0e10cSrcweir bool ExtensionManager::isUserDisabled(
398cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName)
399cdf0e10cSrcweir {
400cdf0e10cSrcweir 	::std::list<Reference<deploy::XPackage> > listExtensions;
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 	try {
403cdf0e10cSrcweir 		listExtensions = getExtensionsWithSameId(identifier, fileName);
404cdf0e10cSrcweir 	} catch (lang::IllegalArgumentException & ) {
405cdf0e10cSrcweir 	}
406cdf0e10cSrcweir     OSL_ASSERT(listExtensions.size() == 3);
407cdf0e10cSrcweir 
408cdf0e10cSrcweir     return isUserDisabled( ::comphelper::containerToSequence<
409cdf0e10cSrcweir                            Reference<deploy::XPackage>,
410cdf0e10cSrcweir                            ::std::list<Reference<deploy::XPackage> >
411cdf0e10cSrcweir                            > (listExtensions));
412cdf0e10cSrcweir }
413cdf0e10cSrcweir 
414cdf0e10cSrcweir bool ExtensionManager::isUserDisabled(
415cdf0e10cSrcweir     uno::Sequence<Reference<deploy::XPackage> > const & seqExtSameId)
416cdf0e10cSrcweir {
417cdf0e10cSrcweir     OSL_ASSERT(seqExtSameId.getLength() == 3);
418cdf0e10cSrcweir     Reference<deploy::XPackage> const & userExtension = seqExtSameId[0];
419cdf0e10cSrcweir     if (userExtension.is())
420cdf0e10cSrcweir     {
421cdf0e10cSrcweir         beans::Optional<beans::Ambiguous<sal_Bool> > reg =
422cdf0e10cSrcweir             userExtension->isRegistered(Reference<task::XAbortChannel>(),
423cdf0e10cSrcweir                                         Reference<ucb::XCommandEnvironment>());
424cdf0e10cSrcweir         //If the value is ambiguous is than we assume that the extension
425cdf0e10cSrcweir         //is enabled, but something went wrong during enabling. We do not
426cdf0e10cSrcweir         //automatically disable user extensions.
427cdf0e10cSrcweir         if (reg.IsPresent &&
428cdf0e10cSrcweir             ! reg.Value.IsAmbiguous && ! reg.Value.Value)
429cdf0e10cSrcweir             return true;
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir     return false;
432cdf0e10cSrcweir }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir /*
435cdf0e10cSrcweir     This method determines the active extension (XPackage.registerPackage) with a
436cdf0e10cSrcweir     particular identifier.
437cdf0e10cSrcweir 
438cdf0e10cSrcweir     The parameter bUserDisabled determines if the user extension is disabled.
439cdf0e10cSrcweir 
440cdf0e10cSrcweir     When the user repository contains an extension with the given identifier and
441cdf0e10cSrcweir     it is not disabled by the user, then it is always registered.  Otherwise an
442cdf0e10cSrcweir     extension is only registered when there is no registered extension in one of
443cdf0e10cSrcweir     the repositories with a higher priority. That is, if the extension is from
444cdf0e10cSrcweir     the shared repository and an active extension with the same identifer is in
445cdf0e10cSrcweir     the user repository, then the extension is not registered. Similarly a
446cdf0e10cSrcweir     bundled extension is not registered if there is an active extension with the
447cdf0e10cSrcweir     same identifier in the shared or user repository.
448cdf0e10cSrcweir */
449cdf0e10cSrcweir void ExtensionManager::activateExtension(
450cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName,
451cdf0e10cSrcweir     bool bUserDisabled,
452cdf0e10cSrcweir     bool bStartup,
453cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
454cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
455cdf0e10cSrcweir {
456cdf0e10cSrcweir     ::std::list<Reference<deploy::XPackage> > listExtensions;
457cdf0e10cSrcweir 	try {
458cdf0e10cSrcweir         listExtensions = getExtensionsWithSameId(identifier, fileName);
459cdf0e10cSrcweir 	} catch (lang::IllegalArgumentException &) {
460cdf0e10cSrcweir 	}
461cdf0e10cSrcweir     OSL_ASSERT(listExtensions.size() == 3);
462cdf0e10cSrcweir 
463cdf0e10cSrcweir     activateExtension(
464cdf0e10cSrcweir         ::comphelper::containerToSequence<
465cdf0e10cSrcweir         Reference<deploy::XPackage>,
466cdf0e10cSrcweir         ::std::list<Reference<deploy::XPackage> >
467cdf0e10cSrcweir         > (listExtensions),
468cdf0e10cSrcweir         bUserDisabled, bStartup, xAbortChannel, xCmdEnv);
469cdf0e10cSrcweir 
470cdf0e10cSrcweir     fireModified();
471cdf0e10cSrcweir }
472cdf0e10cSrcweir 
473cdf0e10cSrcweir void ExtensionManager::activateExtension(
474cdf0e10cSrcweir     uno::Sequence<Reference<deploy::XPackage> > const & seqExt,
475cdf0e10cSrcweir     bool bUserDisabled,
476cdf0e10cSrcweir     bool bStartup,
477cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
478cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
479cdf0e10cSrcweir {
480cdf0e10cSrcweir     bool bActive = false;
481cdf0e10cSrcweir     sal_Int32 len = seqExt.getLength();
482cdf0e10cSrcweir     for (sal_Int32 i = 0; i < len; i++)
483cdf0e10cSrcweir     {
484cdf0e10cSrcweir         Reference<deploy::XPackage> const & aExt =  seqExt[i];
485cdf0e10cSrcweir         if (aExt.is())
486cdf0e10cSrcweir         {
487cdf0e10cSrcweir             //get the registration value of the current iteration
488cdf0e10cSrcweir             beans::Optional<beans::Ambiguous<sal_Bool> > optReg =
489cdf0e10cSrcweir                 aExt->isRegistered(xAbortChannel, xCmdEnv);
490cdf0e10cSrcweir             //If nothing can be registered then break
491cdf0e10cSrcweir             if (!optReg.IsPresent)
492cdf0e10cSrcweir                 break;
493cdf0e10cSrcweir 
494cdf0e10cSrcweir             //Check if this is a disabled user extension,
495cdf0e10cSrcweir             if (i == 0 && bUserDisabled)
496cdf0e10cSrcweir             {
497cdf0e10cSrcweir                    aExt->revokePackage(xAbortChannel, xCmdEnv);
498cdf0e10cSrcweir                    continue;
499cdf0e10cSrcweir             }
500cdf0e10cSrcweir 
501cdf0e10cSrcweir             //If we have already determined an active extension then we must
502cdf0e10cSrcweir             //make sure to unregister all extensions with the same id in
503cdf0e10cSrcweir             //repositories with a lower priority
504cdf0e10cSrcweir             if (bActive)
505cdf0e10cSrcweir             {
506cdf0e10cSrcweir                 aExt->revokePackage(xAbortChannel, xCmdEnv);
507cdf0e10cSrcweir             }
508cdf0e10cSrcweir             else
509cdf0e10cSrcweir             {
510cdf0e10cSrcweir                 //This is the first extension in the ordered list, which becomes
511cdf0e10cSrcweir                 //the active extension
512cdf0e10cSrcweir                 bActive = true;
513cdf0e10cSrcweir                 //Register if not already done.
514cdf0e10cSrcweir                 //reregister if the value is ambiguous, which indicates that
515cdf0e10cSrcweir                 //something went wrong during last registration.
516cdf0e10cSrcweir                 aExt->registerPackage(bStartup, xAbortChannel, xCmdEnv);
517cdf0e10cSrcweir             }
518cdf0e10cSrcweir         }
519cdf0e10cSrcweir     }
520cdf0e10cSrcweir }
521cdf0e10cSrcweir 
522cdf0e10cSrcweir Reference<deploy::XPackage> ExtensionManager::backupExtension(
523cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName,
524cdf0e10cSrcweir     Reference<deploy::XPackageManager> const & xPackageManager,
525cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
526cdf0e10cSrcweir {
527cdf0e10cSrcweir     Reference<deploy::XPackage> xBackup;
528cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> tmpCmdEnv(
529cdf0e10cSrcweir         new TmpRepositoryCommandEnv(xCmdEnv->getInteractionHandler()));
530cdf0e10cSrcweir     Reference<deploy::XPackage> xOldExtension;
531cdf0e10cSrcweir     xOldExtension = xPackageManager->getDeployedPackage(
532cdf0e10cSrcweir             identifier, fileName, tmpCmdEnv);
533cdf0e10cSrcweir 
534cdf0e10cSrcweir     if (xOldExtension.is())
535cdf0e10cSrcweir     {
536cdf0e10cSrcweir         xBackup = getTmpRepository()->addPackage(
537cdf0e10cSrcweir             xOldExtension->getURL(), uno::Sequence<beans::NamedValue>(),
538cdf0e10cSrcweir             OUString(), Reference<task::XAbortChannel>(), tmpCmdEnv);
539cdf0e10cSrcweir 
540cdf0e10cSrcweir         OSL_ENSURE(xBackup.is(), "Failed to backup extension");
541cdf0e10cSrcweir     }
542cdf0e10cSrcweir     return xBackup;
543cdf0e10cSrcweir }
544cdf0e10cSrcweir 
545cdf0e10cSrcweir //The supported package types are actually determined by the registry. However
546cdf0e10cSrcweir //creating a registry
547cdf0e10cSrcweir //(desktop/source/deployment/registry/dp_registry.cxx:PackageRegistryImpl) will
548cdf0e10cSrcweir //create all the backends, so that the registry can obtain from them the package
549cdf0e10cSrcweir //types. Creating the registry will also set up the registry folder containing
550cdf0e10cSrcweir //all the subfolders for the respective backends.
551cdf0e10cSrcweir //Because all repositories support the same backends, we can just delegate this
552cdf0e10cSrcweir //call to one of the repositories.
553cdf0e10cSrcweir uno::Sequence< Reference<deploy::XPackageTypeInfo> >
554cdf0e10cSrcweir ExtensionManager::getSupportedPackageTypes()
555cdf0e10cSrcweir     throw (uno::RuntimeException)
556cdf0e10cSrcweir {
557cdf0e10cSrcweir     return getUserRepository()->getSupportedPackageTypes();
558cdf0e10cSrcweir }
559cdf0e10cSrcweir //Do some necessary checks and user interaction. This function does not
560cdf0e10cSrcweir //aquire the extension manager mutex and that mutex must not be aquired
561cdf0e10cSrcweir //when this function is called. doChecksForAddExtension does  synchronous
562cdf0e10cSrcweir //user interactions which may require aquiring the solar mutex.
563cdf0e10cSrcweir //Returns true if the extension can be installed.
564cdf0e10cSrcweir bool ExtensionManager::doChecksForAddExtension(
565cdf0e10cSrcweir     Reference<deploy::XPackageManager> const & xPackageMgr,
566cdf0e10cSrcweir     uno::Sequence<beans::NamedValue> const & properties,
567cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackage> const & xTmpExtension,
568cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
569cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv,
570cdf0e10cSrcweir     Reference<deploy::XPackage> & out_existingExtension )
571cdf0e10cSrcweir     throw (deploy::DeploymentException,
572cdf0e10cSrcweir            ucb::CommandFailedException,
573cdf0e10cSrcweir            ucb::CommandAbortedException,
574cdf0e10cSrcweir            lang::IllegalArgumentException,
575cdf0e10cSrcweir            uno::RuntimeException)
576cdf0e10cSrcweir {
577cdf0e10cSrcweir     try
578cdf0e10cSrcweir     {
579cdf0e10cSrcweir         Reference<deploy::XPackage> xOldExtension;
580cdf0e10cSrcweir         const OUString sIdentifier = dp_misc::getIdentifier(xTmpExtension);
581cdf0e10cSrcweir         const OUString sFileName = xTmpExtension->getName();
582cdf0e10cSrcweir         const OUString sDisplayName = xTmpExtension->getDisplayName();
583cdf0e10cSrcweir         const OUString sVersion = xTmpExtension->getVersion();
584cdf0e10cSrcweir 
585cdf0e10cSrcweir         try
586cdf0e10cSrcweir         {
587cdf0e10cSrcweir             xOldExtension = xPackageMgr->getDeployedPackage(
588cdf0e10cSrcweir                 sIdentifier, sFileName, xCmdEnv);
589cdf0e10cSrcweir             out_existingExtension = xOldExtension;
590cdf0e10cSrcweir         }
591cdf0e10cSrcweir         catch (lang::IllegalArgumentException &)
592cdf0e10cSrcweir         {
593cdf0e10cSrcweir         }
594cdf0e10cSrcweir         bool bCanInstall = false;
595cdf0e10cSrcweir 
596cdf0e10cSrcweir         //This part is not guarded against other threads removing, adding, disabling ...
597cdf0e10cSrcweir         //etc. the same extension.
598cdf0e10cSrcweir         //checkInstall is safe because it notifies the user if the extension is not yet
599cdf0e10cSrcweir         //installed in the same repository. Because addExtension has its own guard
600cdf0e10cSrcweir         //(m_addMutex), another thread cannot add the extension in the meantime.
601cdf0e10cSrcweir         //checkUpdate is called if the same extension exists in the same
602cdf0e10cSrcweir         //repository. The user is asked if they want to replace it.  Another
603cdf0e10cSrcweir         //thread
604cdf0e10cSrcweir         //could already remove the extension. So asking the user was not
605cdf0e10cSrcweir         //necessary. No harm is done. The other thread may also ask the user
606cdf0e10cSrcweir         //if he wants to remove the extension. This depends on the
607cdf0e10cSrcweir         //XCommandEnvironment which it passes to removeExtension.
608cdf0e10cSrcweir         if (xOldExtension.is())
609cdf0e10cSrcweir         {
610cdf0e10cSrcweir             //throws a CommandFailedException if the user cancels
611cdf0e10cSrcweir             //the action.
612cdf0e10cSrcweir             checkUpdate(sVersion, sDisplayName,xOldExtension, xCmdEnv);
613cdf0e10cSrcweir         }
614cdf0e10cSrcweir         else
615cdf0e10cSrcweir         {
616cdf0e10cSrcweir             //throws a CommandFailedException if the user cancels
617cdf0e10cSrcweir             //the action.
618cdf0e10cSrcweir             checkInstall(sDisplayName, xCmdEnv);
619cdf0e10cSrcweir         }
620cdf0e10cSrcweir         //Prevent showing the license if requested.
621cdf0e10cSrcweir         Reference<ucb::XCommandEnvironment> _xCmdEnv(xCmdEnv);
622cdf0e10cSrcweir         ExtensionProperties props(OUString(), properties, Reference<ucb::XCommandEnvironment>());
623cdf0e10cSrcweir 
624cdf0e10cSrcweir         dp_misc::DescriptionInfoset info(dp_misc::getDescriptionInfoset(xTmpExtension->getURL()));
625cdf0e10cSrcweir         const ::boost::optional<dp_misc::SimpleLicenseAttributes> licenseAttributes =
626cdf0e10cSrcweir             info.getSimpleLicenseAttributes();
627cdf0e10cSrcweir 
628cdf0e10cSrcweir         if (licenseAttributes && licenseAttributes->suppressIfRequired
629cdf0e10cSrcweir             && props.isSuppressedLicense())
630cdf0e10cSrcweir             _xCmdEnv = Reference<ucb::XCommandEnvironment>(
631cdf0e10cSrcweir                 new NoLicenseCommandEnv(xCmdEnv->getInteractionHandler()));
632cdf0e10cSrcweir 
633cdf0e10cSrcweir         bCanInstall = xTmpExtension->checkPrerequisites(
634cdf0e10cSrcweir             xAbortChannel, _xCmdEnv, xOldExtension.is() || props.isExtensionUpdate()) == 0 ? true : false;
635cdf0e10cSrcweir 
636cdf0e10cSrcweir         return bCanInstall;
637cdf0e10cSrcweir     }
638cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
639cdf0e10cSrcweir         throw;
640cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
641cdf0e10cSrcweir         throw;
642cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
643cdf0e10cSrcweir         throw;
644cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
645cdf0e10cSrcweir         throw;
646cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
647cdf0e10cSrcweir         throw;
648cdf0e10cSrcweir     } catch (uno::Exception &) {
649cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
650cdf0e10cSrcweir         deploy::DeploymentException exc(
651cdf0e10cSrcweir             OUSTR("Extension Manager: exception in doChecksForAddExtension"),
652cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
653cdf0e10cSrcweir         throw exc;
654cdf0e10cSrcweir     } catch (...) {
655cdf0e10cSrcweir         throw uno::RuntimeException(
656cdf0e10cSrcweir             OUSTR("Extension Manager: unexpected exception in doChecksForAddExtension"),
657cdf0e10cSrcweir             static_cast<OWeakObject*>(this));
658cdf0e10cSrcweir     }
659cdf0e10cSrcweir }
660cdf0e10cSrcweir 
661cdf0e10cSrcweir // Only add to shared and user repository
662cdf0e10cSrcweir Reference<deploy::XPackage> ExtensionManager::addExtension(
663cdf0e10cSrcweir     OUString const & url, uno::Sequence<beans::NamedValue> const & properties,
664cdf0e10cSrcweir     OUString const & repository,
665cdf0e10cSrcweir         Reference<task::XAbortChannel> const & xAbortChannel,
666cdf0e10cSrcweir         Reference<ucb::XCommandEnvironment> const & xCmdEnv )
667cdf0e10cSrcweir         throw (deploy::DeploymentException,
668cdf0e10cSrcweir                ucb::CommandFailedException,
669cdf0e10cSrcweir                ucb::CommandAbortedException,
670cdf0e10cSrcweir                lang::IllegalArgumentException,
671cdf0e10cSrcweir                uno::RuntimeException)
672cdf0e10cSrcweir {
673cdf0e10cSrcweir     Reference<deploy::XPackage> xNewExtension;
674cdf0e10cSrcweir     //Determine the repository to use
675cdf0e10cSrcweir     Reference<deploy::XPackageManager> xPackageManager;
676cdf0e10cSrcweir     if (repository.equals(OUSTR("user")))
677cdf0e10cSrcweir         xPackageManager = getUserRepository();
678cdf0e10cSrcweir     else if (repository.equals(OUSTR("shared")))
679cdf0e10cSrcweir         xPackageManager = getSharedRepository();
680cdf0e10cSrcweir     else
681cdf0e10cSrcweir         throw lang::IllegalArgumentException(
682cdf0e10cSrcweir             OUSTR("No valid repository name provided."),
683cdf0e10cSrcweir             static_cast<cppu::OWeakObject*>(this), 0);
684cdf0e10cSrcweir     //We must make sure that the xTmpExtension is not create twice, because this
685cdf0e10cSrcweir     //would remove the first one.
686cdf0e10cSrcweir     ::osl::MutexGuard addGuard(m_addMutex);
687cdf0e10cSrcweir 
688cdf0e10cSrcweir     Reference<deploy::XPackage> xTmpExtension =
689cdf0e10cSrcweir         getTempExtension(url, xAbortChannel, xCmdEnv);
690cdf0e10cSrcweir     //Make sure the extension is removed from the tmp repository in case
691cdf0e10cSrcweir     //of an exception
692cdf0e10cSrcweir     ExtensionRemoveGuard tmpExtensionRemoveGuard(xTmpExtension, getTmpRepository());
6937bc7b19fSMichael Stahl     ExtensionRemoveGuard bakExtensionRemoveGuard;
694cdf0e10cSrcweir     const OUString sIdentifier = dp_misc::getIdentifier(xTmpExtension);
695cdf0e10cSrcweir     const OUString sFileName = xTmpExtension->getName();
696cdf0e10cSrcweir     Reference<deploy::XPackage> xOldExtension;
697cdf0e10cSrcweir     Reference<deploy::XPackage> xExtensionBackup;
698cdf0e10cSrcweir 
699cdf0e10cSrcweir     uno::Any excOccurred2;
700cdf0e10cSrcweir     bool bUserDisabled = false;
701cdf0e10cSrcweir     bool bCanInstall = doChecksForAddExtension(
702cdf0e10cSrcweir         xPackageManager,
703cdf0e10cSrcweir         properties,
704cdf0e10cSrcweir         xTmpExtension,
705cdf0e10cSrcweir         xAbortChannel,
706cdf0e10cSrcweir         xCmdEnv,
707cdf0e10cSrcweir         xOldExtension );
708cdf0e10cSrcweir 
709cdf0e10cSrcweir     {
710cdf0e10cSrcweir         // In this garded section (getMutex) we must not use the argument xCmdEnv
711cdf0e10cSrcweir         // because it may bring up dialogs (XInteractionHandler::handle) this
712cdf0e10cSrcweir         //may potententially deadlock. See issue
713cdf0e10cSrcweir         //http://qa.openoffice.org/issues/show_bug.cgi?id=114933
714cdf0e10cSrcweir         //By not providing xCmdEnv the underlying APIs will throw an exception if
715cdf0e10cSrcweir         //the XInteractionRequest cannot be handled
716cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
717cdf0e10cSrcweir 
718cdf0e10cSrcweir         if (bCanInstall)
719cdf0e10cSrcweir         {
720cdf0e10cSrcweir             try
721cdf0e10cSrcweir             {
722cdf0e10cSrcweir                 bUserDisabled = isUserDisabled(sIdentifier, sFileName);
723cdf0e10cSrcweir                 if (xOldExtension.is())
724cdf0e10cSrcweir                 {
725cdf0e10cSrcweir                     try
726cdf0e10cSrcweir                     {
727cdf0e10cSrcweir                         xOldExtension->revokePackage(
728cdf0e10cSrcweir                             xAbortChannel, Reference<ucb::XCommandEnvironment>());
729cdf0e10cSrcweir                         //save the old user extension in case the user aborts
7307bc7b19fSMichael Stahl                         xExtensionBackup = getBakRepository()->importExtension(
731cdf0e10cSrcweir                             xOldExtension, Reference<task::XAbortChannel>(),
732cdf0e10cSrcweir                             Reference<ucb::XCommandEnvironment>());
7337bc7b19fSMichael Stahl                         bakExtensionRemoveGuard.set(xExtensionBackup, getBakRepository());
734cdf0e10cSrcweir                     }
735cdf0e10cSrcweir                     catch (lang::DisposedException &)
736cdf0e10cSrcweir                     {
737cdf0e10cSrcweir                         //Another thread might have removed the extension meanwhile
738cdf0e10cSrcweir                     }
739cdf0e10cSrcweir                 }
740cdf0e10cSrcweir                 //check again dependencies but prevent user interaction,
741cdf0e10cSrcweir                 //We can disregard the license, because the user must have already
742cdf0e10cSrcweir                 //accepted it, whe we called checkPrerequisites the first time
743cdf0e10cSrcweir                 SilentCheckPrerequisitesCommandEnv * pSilentCommandEnv =
744cdf0e10cSrcweir                     new SilentCheckPrerequisitesCommandEnv();
745cdf0e10cSrcweir                 Reference<ucb::XCommandEnvironment> silentCommandEnv(pSilentCommandEnv);
746cdf0e10cSrcweir 
747cdf0e10cSrcweir                 sal_Int32 failedPrereq = xTmpExtension->checkPrerequisites(
748cdf0e10cSrcweir                     xAbortChannel, silentCommandEnv, true);
749cdf0e10cSrcweir                 if (failedPrereq == 0)
750cdf0e10cSrcweir                 {
751cdf0e10cSrcweir                     xNewExtension = xPackageManager->addPackage(
752cdf0e10cSrcweir                         url, properties, OUString(), xAbortChannel,
753cdf0e10cSrcweir                         Reference<ucb::XCommandEnvironment>());
754cdf0e10cSrcweir                     //If we add a user extension and there is already one which was
755cdf0e10cSrcweir                     //disabled by a user, then the newly installed one is enabled. If we
756cdf0e10cSrcweir                     //add to another repository then the user extension remains
757cdf0e10cSrcweir                     //disabled.
758cdf0e10cSrcweir                     bool bUserDisabled2 = bUserDisabled;
759cdf0e10cSrcweir                     if (repository.equals(OUSTR("user")))
760cdf0e10cSrcweir                         bUserDisabled2 = false;
761cdf0e10cSrcweir 
762cdf0e10cSrcweir                     ::rtl::OUString const name(xNewExtension->getName());
763cdf0e10cSrcweir                     activateExtension(
764cdf0e10cSrcweir                         dp_misc::getIdentifier(xNewExtension),
765cdf0e10cSrcweir                         name, bUserDisabled2, false, xAbortChannel,
766cdf0e10cSrcweir                         Reference<ucb::XCommandEnvironment>());
767cdf0e10cSrcweir                 }
768cdf0e10cSrcweir                 else
769cdf0e10cSrcweir                 {
770cdf0e10cSrcweir                     if (pSilentCommandEnv->m_Exception.hasValue())
771cdf0e10cSrcweir                         ::cppu::throwException(pSilentCommandEnv->m_Exception);
772cdf0e10cSrcweir                     else if ( pSilentCommandEnv->m_UnknownException.hasValue())
773cdf0e10cSrcweir                         ::cppu::throwException(pSilentCommandEnv->m_UnknownException);
774cdf0e10cSrcweir                     else
775cdf0e10cSrcweir                         throw deploy::DeploymentException (
776cdf0e10cSrcweir                             OUSTR("Extension Manager: exception during addExtension, ckeckPrerequisites failed"),
777cdf0e10cSrcweir                             static_cast<OWeakObject*>(this), uno::Any());
778cdf0e10cSrcweir                 }
779cdf0e10cSrcweir             }
780cdf0e10cSrcweir             catch (deploy::DeploymentException& ) {
781cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
782cdf0e10cSrcweir             } catch (ucb::CommandFailedException & ) {
783cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
784cdf0e10cSrcweir             } catch (ucb::CommandAbortedException & ) {
785cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
786cdf0e10cSrcweir             } catch (lang::IllegalArgumentException &) {
787cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
788cdf0e10cSrcweir             } catch (uno::RuntimeException &) {
789cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
790cdf0e10cSrcweir             } catch (...) {
791cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
792cdf0e10cSrcweir                 deploy::DeploymentException exc(
793cdf0e10cSrcweir                     OUSTR("Extension Manager: exception during addExtension, url: ")
794cdf0e10cSrcweir                     + url, static_cast<OWeakObject*>(this), excOccurred2);
795cdf0e10cSrcweir                 excOccurred2 <<= exc;
796cdf0e10cSrcweir             }
797cdf0e10cSrcweir         }
798cdf0e10cSrcweir 
799cdf0e10cSrcweir         if (excOccurred2.hasValue())
800cdf0e10cSrcweir         {
801cdf0e10cSrcweir             //It does not matter what exception is thrown. We try to
802cdf0e10cSrcweir             //recover the original status.
803cdf0e10cSrcweir             //If the user aborted installation then a ucb::CommandAbortedException
804cdf0e10cSrcweir             //is thrown.
805cdf0e10cSrcweir             //Use a private AbortChannel so the user cannot interrupt.
806cdf0e10cSrcweir             try
807cdf0e10cSrcweir             {
808cdf0e10cSrcweir                 if (xExtensionBackup.is())
809cdf0e10cSrcweir                 {
810cdf0e10cSrcweir                     Reference<deploy::XPackage> xRestored =
811cdf0e10cSrcweir                         xPackageManager->importExtension(
812cdf0e10cSrcweir                             xExtensionBackup, Reference<task::XAbortChannel>(),
813cdf0e10cSrcweir                             Reference<ucb::XCommandEnvironment>());
814cdf0e10cSrcweir                 }
815cdf0e10cSrcweir                 activateExtension(
816cdf0e10cSrcweir                     sIdentifier, sFileName, bUserDisabled, false,
817cdf0e10cSrcweir                     Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>());
818cdf0e10cSrcweir             }
819cdf0e10cSrcweir             catch (...)
820cdf0e10cSrcweir             {
821cdf0e10cSrcweir             }
822cdf0e10cSrcweir             ::cppu::throwException(excOccurred2);
823cdf0e10cSrcweir         }
824cdf0e10cSrcweir     } // leaving the garded section (getMutex())
825cdf0e10cSrcweir 
826cdf0e10cSrcweir     try
827cdf0e10cSrcweir     {
828cdf0e10cSrcweir         fireModified();
829cdf0e10cSrcweir 
830cdf0e10cSrcweir     }catch (deploy::DeploymentException& ) {
831cdf0e10cSrcweir         throw;
832cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
833cdf0e10cSrcweir         throw;
834cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
835cdf0e10cSrcweir         throw;
836cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
837cdf0e10cSrcweir         throw;
838cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
839cdf0e10cSrcweir         throw;
840cdf0e10cSrcweir     } catch (uno::Exception &) {
841cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
842cdf0e10cSrcweir         deploy::DeploymentException exc(
843cdf0e10cSrcweir             OUSTR("Extension Manager: exception in doChecksForAddExtension"),
844cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
845cdf0e10cSrcweir         throw exc;
846cdf0e10cSrcweir     } catch (...) {
847cdf0e10cSrcweir         throw uno::RuntimeException(
848cdf0e10cSrcweir             OUSTR("Extension Manager: unexpected exception in doChecksForAddExtension"),
849cdf0e10cSrcweir             static_cast<OWeakObject*>(this));
850cdf0e10cSrcweir     }
851cdf0e10cSrcweir 
852cdf0e10cSrcweir     return xNewExtension;
853cdf0e10cSrcweir }
854cdf0e10cSrcweir 
855cdf0e10cSrcweir void ExtensionManager::removeExtension(
856cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName,
857cdf0e10cSrcweir     OUString const & repository,
858cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
859cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
860cdf0e10cSrcweir     throw (deploy::DeploymentException,
861cdf0e10cSrcweir            ucb::CommandFailedException,
862cdf0e10cSrcweir            ucb::CommandAbortedException,
863cdf0e10cSrcweir            lang::IllegalArgumentException,
864cdf0e10cSrcweir            uno::RuntimeException)
865cdf0e10cSrcweir {
866cdf0e10cSrcweir     uno::Any excOccurred1;
867cdf0e10cSrcweir     Reference<deploy::XPackage> xExtensionBackup;
868cdf0e10cSrcweir     Reference<deploy::XPackageManager> xPackageManager;
869cdf0e10cSrcweir     bool bUserDisabled = false;
870cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
871cdf0e10cSrcweir     try
872cdf0e10cSrcweir     {
873cdf0e10cSrcweir //Determine the repository to use
874cdf0e10cSrcweir         if (repository.equals(OUSTR("user")))
875cdf0e10cSrcweir             xPackageManager = getUserRepository();
876cdf0e10cSrcweir         else if (repository.equals(OUSTR("shared")))
877cdf0e10cSrcweir             xPackageManager = getSharedRepository();
878cdf0e10cSrcweir         else
879cdf0e10cSrcweir             throw lang::IllegalArgumentException(
880cdf0e10cSrcweir                 OUSTR("No valid repository name provided."),
881cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), 0);
882cdf0e10cSrcweir 
883cdf0e10cSrcweir         bUserDisabled = isUserDisabled(identifier, fileName);
884cdf0e10cSrcweir         //Backup the extension, in case the user cancels the action
885cdf0e10cSrcweir         xExtensionBackup = backupExtension(
886cdf0e10cSrcweir             identifier, fileName, xPackageManager, xCmdEnv);
887cdf0e10cSrcweir 
888cdf0e10cSrcweir         //revoke the extension if it is active
889cdf0e10cSrcweir         Reference<deploy::XPackage> xOldExtension =
890cdf0e10cSrcweir             xPackageManager->getDeployedPackage(
891cdf0e10cSrcweir                 identifier, fileName, xCmdEnv);
892cdf0e10cSrcweir         xOldExtension->revokePackage(xAbortChannel, xCmdEnv);
893cdf0e10cSrcweir 
894cdf0e10cSrcweir         xPackageManager->removePackage(
895cdf0e10cSrcweir             identifier, fileName, xAbortChannel, xCmdEnv);
896cdf0e10cSrcweir         activateExtension(identifier, fileName, bUserDisabled, false,
897cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
898cdf0e10cSrcweir         fireModified();
899cdf0e10cSrcweir     }
900cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
901cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
902cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
903cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
904cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
905cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
906cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
907cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
908cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
909cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
910cdf0e10cSrcweir     } catch (...) {
911cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
912cdf0e10cSrcweir         deploy::DeploymentException exc(
913cdf0e10cSrcweir             OUSTR("Extension Manager: exception during removeEtension"),
914cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred1);
915cdf0e10cSrcweir         excOccurred1 <<= exc;
916cdf0e10cSrcweir     }
917cdf0e10cSrcweir 
918cdf0e10cSrcweir     if (excOccurred1.hasValue())
919cdf0e10cSrcweir     {
920cdf0e10cSrcweir         //User aborted installation, restore the previous situation.
921cdf0e10cSrcweir         //Use a private AbortChannel so the user cannot interrupt.
922cdf0e10cSrcweir         try
923cdf0e10cSrcweir         {
924cdf0e10cSrcweir             Reference<ucb::XCommandEnvironment> tmpCmdEnv(
925cdf0e10cSrcweir                 new TmpRepositoryCommandEnv(xCmdEnv->getInteractionHandler()));
926cdf0e10cSrcweir             if (xExtensionBackup.is())
927cdf0e10cSrcweir             {
928cdf0e10cSrcweir                 Reference<deploy::XPackage> xRestored =
929cdf0e10cSrcweir                     xPackageManager->importExtension(
930cdf0e10cSrcweir                         xExtensionBackup, Reference<task::XAbortChannel>(),
931cdf0e10cSrcweir                         tmpCmdEnv);
932cdf0e10cSrcweir                 activateExtension(
933cdf0e10cSrcweir                     identifier, fileName, bUserDisabled, false,
934cdf0e10cSrcweir                     Reference<task::XAbortChannel>(),
935cdf0e10cSrcweir                     tmpCmdEnv);
936cdf0e10cSrcweir 
937cdf0e10cSrcweir                 getTmpRepository()->removePackage(
938cdf0e10cSrcweir                     dp_misc::getIdentifier(xExtensionBackup),
939cdf0e10cSrcweir                     xExtensionBackup->getName(), xAbortChannel, xCmdEnv);
940cdf0e10cSrcweir                 fireModified();
941cdf0e10cSrcweir             }
942cdf0e10cSrcweir         }
943cdf0e10cSrcweir         catch (...)
944cdf0e10cSrcweir         {
945cdf0e10cSrcweir         }
946cdf0e10cSrcweir         ::cppu::throwException(excOccurred1);
947cdf0e10cSrcweir     }
948cdf0e10cSrcweir 
949cdf0e10cSrcweir     if (xExtensionBackup.is())
950cdf0e10cSrcweir         getTmpRepository()->removePackage(
951cdf0e10cSrcweir             dp_misc::getIdentifier(xExtensionBackup),
952cdf0e10cSrcweir             xExtensionBackup->getName(), xAbortChannel, xCmdEnv);
953cdf0e10cSrcweir }
954cdf0e10cSrcweir 
955cdf0e10cSrcweir // Only enable extensions from shared and user repository
956cdf0e10cSrcweir void ExtensionManager::enableExtension(
957cdf0e10cSrcweir     Reference<deploy::XPackage> const & extension,
958cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
959cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv)
960cdf0e10cSrcweir     throw (deploy::DeploymentException,
961cdf0e10cSrcweir         ucb::CommandFailedException,
962cdf0e10cSrcweir         ucb::CommandAbortedException,
963cdf0e10cSrcweir         lang::IllegalArgumentException,
964cdf0e10cSrcweir         uno::RuntimeException)
965cdf0e10cSrcweir {
966cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
967cdf0e10cSrcweir     bool bUserDisabled = false;
968cdf0e10cSrcweir     uno::Any excOccurred;
969cdf0e10cSrcweir     try
970cdf0e10cSrcweir     {
971cdf0e10cSrcweir         if (!extension.is())
972cdf0e10cSrcweir             return;
973cdf0e10cSrcweir         OUString repository = extension->getRepositoryName();
974cdf0e10cSrcweir         if (!repository.equals(OUSTR("user")))
975cdf0e10cSrcweir             throw lang::IllegalArgumentException(
976cdf0e10cSrcweir                 OUSTR("No valid repository name provided."),
977cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), 0);
978cdf0e10cSrcweir 
979cdf0e10cSrcweir         bUserDisabled = isUserDisabled(dp_misc::getIdentifier(extension),
980cdf0e10cSrcweir                                        extension->getName());
981cdf0e10cSrcweir 
982cdf0e10cSrcweir         activateExtension(dp_misc::getIdentifier(extension),
983cdf0e10cSrcweir                           extension->getName(), false, false,
984cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
985cdf0e10cSrcweir     }
986cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
987cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
988cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
989cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
990cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
991cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
992cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
993cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
994cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
995cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
996cdf0e10cSrcweir     } catch (...) {
997cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
998cdf0e10cSrcweir         deploy::DeploymentException exc(
999cdf0e10cSrcweir             OUSTR("Extension Manager: exception during enableExtension"),
1000cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1001cdf0e10cSrcweir         excOccurred <<= exc;
1002cdf0e10cSrcweir     }
1003cdf0e10cSrcweir 
1004cdf0e10cSrcweir     if (excOccurred.hasValue())
1005cdf0e10cSrcweir     {
1006cdf0e10cSrcweir         try
1007cdf0e10cSrcweir         {
1008cdf0e10cSrcweir             activateExtension(dp_misc::getIdentifier(extension),
1009cdf0e10cSrcweir                               extension->getName(), bUserDisabled, false,
1010cdf0e10cSrcweir                               xAbortChannel, xCmdEnv);
1011cdf0e10cSrcweir         }
1012cdf0e10cSrcweir         catch (...)
1013cdf0e10cSrcweir         {
1014cdf0e10cSrcweir         }
1015cdf0e10cSrcweir         ::cppu::throwException(excOccurred);
1016cdf0e10cSrcweir     }
1017cdf0e10cSrcweir }
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir /**
1020cdf0e10cSrcweir  */
1021cdf0e10cSrcweir sal_Int32 ExtensionManager::checkPrerequisitesAndEnable(
1022cdf0e10cSrcweir     Reference<deploy::XPackage> const & extension,
1023cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1024cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv)
1025cdf0e10cSrcweir     throw (deploy::DeploymentException,
1026cdf0e10cSrcweir         ucb::CommandFailedException,
1027cdf0e10cSrcweir         ucb::CommandAbortedException,
1028cdf0e10cSrcweir         lang::IllegalArgumentException,
1029cdf0e10cSrcweir         uno::RuntimeException)
1030cdf0e10cSrcweir {
1031cdf0e10cSrcweir     try
1032cdf0e10cSrcweir     {
1033cdf0e10cSrcweir         if (!extension.is())
1034cdf0e10cSrcweir             return 0;
1035cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
1036cdf0e10cSrcweir         sal_Int32 ret = 0;
1037cdf0e10cSrcweir         Reference<deploy::XPackageManager> mgr =
1038cdf0e10cSrcweir             getPackageManager(extension->getRepositoryName());
1039cdf0e10cSrcweir         ret = mgr->checkPrerequisites(extension, xAbortChannel, xCmdEnv);
1040cdf0e10cSrcweir         if (ret)
1041cdf0e10cSrcweir         {
1042cdf0e10cSrcweir             //There are some unfulfilled prerequisites, try to revoke
1043cdf0e10cSrcweir             extension->revokePackage(xAbortChannel, xCmdEnv);
1044cdf0e10cSrcweir         }
1045cdf0e10cSrcweir         const OUString id(dp_misc::getIdentifier(extension));
1046cdf0e10cSrcweir         activateExtension(id, extension->getName(),
1047cdf0e10cSrcweir                           isUserDisabled(id, extension->getName()), false,
1048cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
1049cdf0e10cSrcweir         return ret;
1050cdf0e10cSrcweir     }
1051cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
1052cdf0e10cSrcweir         throw;
1053cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1054cdf0e10cSrcweir         throw;
1055cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1056cdf0e10cSrcweir         throw;
1057cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1058cdf0e10cSrcweir         throw;
1059cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1060cdf0e10cSrcweir         throw;
1061cdf0e10cSrcweir     } catch (...) {
1062cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
1063cdf0e10cSrcweir         deploy::DeploymentException exc(
1064cdf0e10cSrcweir             OUSTR("Extension Manager: exception during disableExtension"),
1065cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1066cdf0e10cSrcweir         throw exc;
1067cdf0e10cSrcweir     }
1068cdf0e10cSrcweir }
1069cdf0e10cSrcweir 
1070cdf0e10cSrcweir 
1071cdf0e10cSrcweir void ExtensionManager::disableExtension(
1072cdf0e10cSrcweir     Reference<deploy::XPackage> const & extension,
1073cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1074cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1075cdf0e10cSrcweir     throw (deploy::DeploymentException,
1076cdf0e10cSrcweir            ucb::CommandFailedException,
1077cdf0e10cSrcweir            ucb::CommandAbortedException,
1078cdf0e10cSrcweir            lang::IllegalArgumentException,
1079cdf0e10cSrcweir            uno::RuntimeException)
1080cdf0e10cSrcweir {
1081cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
1082cdf0e10cSrcweir     uno::Any excOccurred;
1083cdf0e10cSrcweir     bool bUserDisabled = false;
1084cdf0e10cSrcweir     try
1085cdf0e10cSrcweir     {
1086cdf0e10cSrcweir         if (!extension.is())
1087cdf0e10cSrcweir             return;
1088cdf0e10cSrcweir         const OUString repository( extension->getRepositoryName());
1089cdf0e10cSrcweir         if (!repository.equals(OUSTR("user")))
1090cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1091cdf0e10cSrcweir                 OUSTR("No valid repository name provided."),
1092cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), 0);
1093cdf0e10cSrcweir 
1094cdf0e10cSrcweir         const OUString id(dp_misc::getIdentifier(extension));
1095cdf0e10cSrcweir         bUserDisabled = isUserDisabled(id, extension->getName());
1096cdf0e10cSrcweir 
1097cdf0e10cSrcweir         activateExtension(id, extension->getName(), true, false,
1098cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
1099cdf0e10cSrcweir     }
1100cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
1101cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1102cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1103cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1104cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1105cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1106cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1107cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1108cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1109cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1110cdf0e10cSrcweir     } catch (...) {
1111cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1112cdf0e10cSrcweir         deploy::DeploymentException exc(
1113cdf0e10cSrcweir             OUSTR("Extension Manager: exception during disableExtension"),
1114cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1115cdf0e10cSrcweir         excOccurred <<= exc;
1116cdf0e10cSrcweir     }
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir     if (excOccurred.hasValue())
1119cdf0e10cSrcweir     {
1120cdf0e10cSrcweir         try
1121cdf0e10cSrcweir         {
1122cdf0e10cSrcweir             activateExtension(dp_misc::getIdentifier(extension),
1123cdf0e10cSrcweir                               extension->getName(), bUserDisabled, false,
1124cdf0e10cSrcweir                               xAbortChannel, xCmdEnv);
1125cdf0e10cSrcweir         }
1126cdf0e10cSrcweir         catch (...)
1127cdf0e10cSrcweir         {
1128cdf0e10cSrcweir         }
1129cdf0e10cSrcweir         ::cppu::throwException(excOccurred);
1130cdf0e10cSrcweir     }
1131cdf0e10cSrcweir }
1132cdf0e10cSrcweir 
1133cdf0e10cSrcweir uno::Sequence< Reference<deploy::XPackage> >
1134cdf0e10cSrcweir     ExtensionManager::getDeployedExtensions(
1135cdf0e10cSrcweir     OUString const & repository,
1136cdf0e10cSrcweir     Reference<task::XAbortChannel> const &xAbort,
1137cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1138cdf0e10cSrcweir     throw (deploy::DeploymentException,
1139cdf0e10cSrcweir         ucb::CommandFailedException,
1140cdf0e10cSrcweir         ucb::CommandAbortedException,
1141cdf0e10cSrcweir         lang::IllegalArgumentException,
1142cdf0e10cSrcweir         uno::RuntimeException)
1143cdf0e10cSrcweir {
1144cdf0e10cSrcweir     return getPackageManager(repository)->getDeployedPackages(
1145cdf0e10cSrcweir         xAbort, xCmdEnv);
1146cdf0e10cSrcweir }
1147cdf0e10cSrcweir 
1148cdf0e10cSrcweir Reference<deploy::XPackage>
1149cdf0e10cSrcweir     ExtensionManager::getDeployedExtension(
1150cdf0e10cSrcweir     OUString const & repository,
1151cdf0e10cSrcweir     OUString const & identifier,
1152cdf0e10cSrcweir     OUString const & filename,
1153cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1154cdf0e10cSrcweir     throw (deploy::DeploymentException,
1155cdf0e10cSrcweir         ucb::CommandFailedException,
1156cdf0e10cSrcweir         lang::IllegalArgumentException,
1157cdf0e10cSrcweir         uno::RuntimeException)
1158cdf0e10cSrcweir {
1159cdf0e10cSrcweir     return getPackageManager(repository)->getDeployedPackage(
1160cdf0e10cSrcweir         identifier, filename, xCmdEnv);
1161cdf0e10cSrcweir }
1162cdf0e10cSrcweir 
1163cdf0e10cSrcweir uno::Sequence< uno::Sequence<Reference<deploy::XPackage> > >
1164cdf0e10cSrcweir     ExtensionManager::getAllExtensions(
1165cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbort,
1166cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1167cdf0e10cSrcweir     throw (deploy::DeploymentException,
1168cdf0e10cSrcweir         ucb::CommandFailedException,
1169cdf0e10cSrcweir         ucb::CommandAbortedException,
1170cdf0e10cSrcweir         lang::IllegalArgumentException,
1171cdf0e10cSrcweir         uno::RuntimeException)
1172cdf0e10cSrcweir {
1173cdf0e10cSrcweir     try
1174cdf0e10cSrcweir     {
1175cdf0e10cSrcweir         id2extensions mapExt;
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > userExt =
1178cdf0e10cSrcweir             getUserRepository()->getDeployedPackages(xAbort, xCmdEnv);
1179cdf0e10cSrcweir         addExtensionsToMap(mapExt, userExt, OUSTR("user"));
1180cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > sharedExt =
1181cdf0e10cSrcweir             getSharedRepository()->getDeployedPackages(xAbort, xCmdEnv);
1182cdf0e10cSrcweir         addExtensionsToMap(mapExt, sharedExt, OUSTR("shared"));
1183cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > bundledExt =
1184cdf0e10cSrcweir             getBundledRepository()->getDeployedPackages(xAbort, xCmdEnv);
1185cdf0e10cSrcweir         addExtensionsToMap(mapExt, bundledExt, OUSTR("bundled"));
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir         //copy the values of the map to a vector for sorting
1188cdf0e10cSrcweir         ::std::vector< ::std::vector<Reference<deploy::XPackage> > >
1189cdf0e10cSrcweir               vecExtensions;
1190cdf0e10cSrcweir         id2extensions::const_iterator mapIt = mapExt.begin();
1191cdf0e10cSrcweir         for (;mapIt != mapExt.end(); mapIt++)
1192cdf0e10cSrcweir             vecExtensions.push_back(mapIt->second);
1193cdf0e10cSrcweir 
1194cdf0e10cSrcweir         //sort the element according to the identifier
1195cdf0e10cSrcweir         ::std::sort(vecExtensions.begin(), vecExtensions.end(), CompIdentifiers());
1196cdf0e10cSrcweir 
1197cdf0e10cSrcweir         ::std::vector< ::std::vector<Reference<deploy::XPackage> > >::const_iterator
1198cdf0e10cSrcweir               citVecVec = vecExtensions.begin();
1199cdf0e10cSrcweir         sal_Int32 j = 0;
1200cdf0e10cSrcweir         uno::Sequence< uno::Sequence<Reference<deploy::XPackage> > > seqSeq(vecExtensions.size());
1201cdf0e10cSrcweir         for (;citVecVec != vecExtensions.end(); citVecVec++, j++)
1202cdf0e10cSrcweir         {
1203cdf0e10cSrcweir             seqSeq[j] = comphelper::containerToSequence(*citVecVec);
1204cdf0e10cSrcweir         }
1205cdf0e10cSrcweir         return seqSeq;
1206cdf0e10cSrcweir 
1207cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1208cdf0e10cSrcweir         throw;
1209cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1210cdf0e10cSrcweir         throw;
1211cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1212cdf0e10cSrcweir         throw;
1213cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1214cdf0e10cSrcweir         throw;
1215cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1216cdf0e10cSrcweir         throw;
1217cdf0e10cSrcweir     } catch (...) {
1218cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1219cdf0e10cSrcweir         throw deploy::DeploymentException(
1220cdf0e10cSrcweir             OUSTR("Extension Manager: exception during enableExtension"),
1221cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1222cdf0e10cSrcweir    }
1223cdf0e10cSrcweir }
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir //only to be called from unopkg!!!
1226cdf0e10cSrcweir void ExtensionManager::reinstallDeployedExtensions(
1227cdf0e10cSrcweir     OUString const & repository,
1228cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1229cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1230cdf0e10cSrcweir     throw (deploy::DeploymentException,
1231cdf0e10cSrcweir         ucb::CommandFailedException, ucb::CommandAbortedException,
1232cdf0e10cSrcweir         lang::IllegalArgumentException, uno::RuntimeException)
1233cdf0e10cSrcweir {
1234cdf0e10cSrcweir     try
1235cdf0e10cSrcweir     {
1236cdf0e10cSrcweir         Reference<deploy::XPackageManager>
1237cdf0e10cSrcweir             xPackageManager = getPackageManager(repository);
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
1240cdf0e10cSrcweir         xPackageManager->reinstallDeployedPackages(xAbortChannel, xCmdEnv);
1241cdf0e10cSrcweir         //We must sync here, otherwise we will get exceptions when extensions
1242cdf0e10cSrcweir         //are removed.
1243cdf0e10cSrcweir         dp_misc::syncRepositories(xCmdEnv);
1244cdf0e10cSrcweir         const uno::Sequence< Reference<deploy::XPackage> > extensions(
1245cdf0e10cSrcweir             xPackageManager->getDeployedPackages(xAbortChannel, xCmdEnv));
1246cdf0e10cSrcweir 
1247cdf0e10cSrcweir         for ( sal_Int32 pos = 0; pos < extensions.getLength(); ++pos )
1248cdf0e10cSrcweir         {
1249cdf0e10cSrcweir             try
1250cdf0e10cSrcweir             {
1251cdf0e10cSrcweir                 const OUString id =  dp_misc::getIdentifier(extensions[ pos ]);
1252cdf0e10cSrcweir                 const OUString fileName = extensions[ pos ]->getName();
1253cdf0e10cSrcweir                 OSL_ASSERT(id.getLength());
1254cdf0e10cSrcweir                 activateExtension(id, fileName, false, true, xAbortChannel, xCmdEnv );
1255cdf0e10cSrcweir             }
1256cdf0e10cSrcweir             catch (lang::DisposedException &)
1257cdf0e10cSrcweir             {
1258cdf0e10cSrcweir             }
1259cdf0e10cSrcweir         }
1260cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1261cdf0e10cSrcweir         throw;
1262cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1263cdf0e10cSrcweir         throw;
1264cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1265cdf0e10cSrcweir         throw;
1266cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1267cdf0e10cSrcweir         throw;
1268cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1269cdf0e10cSrcweir         throw;
1270cdf0e10cSrcweir     } catch (...) {
1271cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1272cdf0e10cSrcweir         throw deploy::DeploymentException(
1273cdf0e10cSrcweir             OUSTR("Extension Manager: exception during enableExtension"),
1274cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1275cdf0e10cSrcweir     }
1276cdf0e10cSrcweir }
1277cdf0e10cSrcweir 
1278cdf0e10cSrcweir /** Works on the bundled repository. That is using the variables
1279cdf0e10cSrcweir     BUNDLED_EXTENSIONS and BUNDLED_EXTENSIONS_USER.
1280cdf0e10cSrcweir  */
1281cdf0e10cSrcweir void ExtensionManager::synchronizeBundledPrereg(
1282cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1283cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1284cdf0e10cSrcweir     throw (deploy::DeploymentException,
1285cdf0e10cSrcweir            uno::RuntimeException)
1286cdf0e10cSrcweir {
1287cdf0e10cSrcweir     try
1288cdf0e10cSrcweir     {
1289cdf0e10cSrcweir         String sSynchronizingBundled(StrSyncRepository::get());
1290cdf0e10cSrcweir         sSynchronizingBundled.SearchAndReplaceAllAscii( "%NAME", OUSTR("bundled"));
1291cdf0e10cSrcweir         dp_misc::ProgressLevel progressBundled(xCmdEnv, sSynchronizingBundled);
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir         Reference<deploy::XPackageManagerFactory> xPackageManagerFactory(
1294cdf0e10cSrcweir             deploy::thePackageManagerFactory::get(m_xContext));
1295cdf0e10cSrcweir 
1296cdf0e10cSrcweir         Reference<deploy::XPackageManager> xMgr =
1297cdf0e10cSrcweir             xPackageManagerFactory->getPackageManager(OUSTR("bundled_prereg"));
1298cdf0e10cSrcweir         xMgr->synchronize(xAbortChannel, xCmdEnv);
1299cdf0e10cSrcweir         progressBundled.update(OUSTR("\n\n"));
1300cdf0e10cSrcweir 
1301cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > extensions = xMgr->getDeployedPackages(
1302cdf0e10cSrcweir             xAbortChannel, xCmdEnv);
1303cdf0e10cSrcweir         try
1304cdf0e10cSrcweir         {
1305cdf0e10cSrcweir             for (sal_Int32 i = 0; i < extensions.getLength(); i++)
1306cdf0e10cSrcweir             {
1307cdf0e10cSrcweir                 extensions[i]->registerPackage(true, xAbortChannel, xCmdEnv);
1308cdf0e10cSrcweir             }
1309cdf0e10cSrcweir         }
1310cdf0e10cSrcweir         catch (...)
1311cdf0e10cSrcweir         {
1312cdf0e10cSrcweir             OSL_ASSERT(0);
1313cdf0e10cSrcweir         }
1314cdf0e10cSrcweir         OUString lastSyncBundled(RTL_CONSTASCII_USTRINGPARAM(
1315cdf0e10cSrcweir                                      "$BUNDLED_EXTENSIONS_PREREG/lastsynchronized"));
1316cdf0e10cSrcweir         writeLastModified(lastSyncBundled, xCmdEnv);
1317cdf0e10cSrcweir 
1318cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1319cdf0e10cSrcweir         throw;
1320cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1321cdf0e10cSrcweir         throw;
1322cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1323cdf0e10cSrcweir         throw;
1324cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1325cdf0e10cSrcweir         throw;
1326cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1327cdf0e10cSrcweir         throw;
1328cdf0e10cSrcweir     } catch (...) {
1329cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1330cdf0e10cSrcweir         throw deploy::DeploymentException(
1331cdf0e10cSrcweir             OUSTR("Extension Manager: exception in synchronize"),
1332cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1333cdf0e10cSrcweir     }
1334cdf0e10cSrcweir }
1335cdf0e10cSrcweir 
1336cdf0e10cSrcweir sal_Bool ExtensionManager::synchronize(
1337cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1338cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1339cdf0e10cSrcweir     throw (deploy::DeploymentException,
1340cdf0e10cSrcweir            ucb::CommandFailedException,
1341cdf0e10cSrcweir            ucb::CommandAbortedException,
1342cdf0e10cSrcweir            lang::IllegalArgumentException,
1343cdf0e10cSrcweir            uno::RuntimeException)
1344cdf0e10cSrcweir {
1345cdf0e10cSrcweir     try
1346cdf0e10cSrcweir     {
1347cdf0e10cSrcweir         sal_Bool bModified = sal_False;
1348cdf0e10cSrcweir 
1349cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
1350cdf0e10cSrcweir         String sSynchronizingShared(StrSyncRepository::get());
1351cdf0e10cSrcweir         sSynchronizingShared.SearchAndReplaceAllAscii( "%NAME", OUSTR("shared"));
1352cdf0e10cSrcweir         dp_misc::ProgressLevel progressShared(xCmdEnv, sSynchronizingShared);
1353cdf0e10cSrcweir         bModified = getSharedRepository()->synchronize(xAbortChannel, xCmdEnv);
1354cdf0e10cSrcweir         progressShared.update(OUSTR("\n\n"));
1355cdf0e10cSrcweir 
1356cdf0e10cSrcweir         String sSynchronizingBundled(StrSyncRepository::get());
1357cdf0e10cSrcweir         sSynchronizingBundled.SearchAndReplaceAllAscii( "%NAME", OUSTR("bundled"));
1358cdf0e10cSrcweir         dp_misc::ProgressLevel progressBundled(xCmdEnv, sSynchronizingBundled);
1359cdf0e10cSrcweir         bModified |= getBundledRepository()->synchronize(xAbortChannel, xCmdEnv);
1360cdf0e10cSrcweir         progressBundled.update(OUSTR("\n\n"));
1361cdf0e10cSrcweir 
1362cdf0e10cSrcweir         //Always determine the active extension. This is necessary for the
1363cdf0e10cSrcweir         //first-start optimization. The setup creates the registration data for the
1364cdf0e10cSrcweir         //bundled extensions (brand_layer/share/prereg/bundled), which is copied to the user
1365cdf0e10cSrcweir         //installation (user_installation/extension/bundled) when a user starts OOo
1366cdf0e10cSrcweir         //for the first time after running setup. All bundled extensions are registered
1367cdf0e10cSrcweir         //at that moment. However, extensions with the same identifier can be in the
1368cdf0e10cSrcweir         //shared or user repository, in which case the respective bundled extensions must
1369cdf0e10cSrcweir         //be revoked.
1370cdf0e10cSrcweir         try
1371cdf0e10cSrcweir         {
1372cdf0e10cSrcweir             const uno::Sequence<uno::Sequence<Reference<deploy::XPackage> > >
1373cdf0e10cSrcweir                 seqSeqExt = getAllExtensions(xAbortChannel, xCmdEnv);
1374cdf0e10cSrcweir             for (sal_Int32 i = 0; i < seqSeqExt.getLength(); i++)
1375cdf0e10cSrcweir             {
1376cdf0e10cSrcweir                 uno::Sequence<Reference<deploy::XPackage> > const & seqExt =
1377cdf0e10cSrcweir                     seqSeqExt[i];
1378cdf0e10cSrcweir                 activateExtension(seqExt, isUserDisabled(seqExt), true,
1379cdf0e10cSrcweir                                   xAbortChannel, xCmdEnv);
1380cdf0e10cSrcweir             }
1381cdf0e10cSrcweir         }
1382cdf0e10cSrcweir         catch (...)
1383cdf0e10cSrcweir         {
1384cdf0e10cSrcweir             //We catch the exception, so we can write the lastmodified file
1385cdf0e10cSrcweir             //so we will no repeat this everytime OOo starts.
1386cdf0e10cSrcweir             OSL_ENSURE(0, "Extensions Manager: synchronize");
1387cdf0e10cSrcweir         }
1388cdf0e10cSrcweir         OUString lastSyncBundled(RTL_CONSTASCII_USTRINGPARAM(
1389cdf0e10cSrcweir                                      "$BUNDLED_EXTENSIONS_USER/lastsynchronized"));
1390cdf0e10cSrcweir         writeLastModified(lastSyncBundled, xCmdEnv);
1391cdf0e10cSrcweir         OUString lastSyncShared(RTL_CONSTASCII_USTRINGPARAM(
1392cdf0e10cSrcweir                                     "$SHARED_EXTENSIONS_USER/lastsynchronized"));
1393cdf0e10cSrcweir         writeLastModified(lastSyncShared, xCmdEnv);
1394cdf0e10cSrcweir         return bModified;
1395cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1396cdf0e10cSrcweir         throw;
1397cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1398cdf0e10cSrcweir         throw;
1399cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1400cdf0e10cSrcweir         throw;
1401cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1402cdf0e10cSrcweir         throw;
1403cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1404cdf0e10cSrcweir         throw;
1405cdf0e10cSrcweir     } catch (...) {
1406cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1407cdf0e10cSrcweir         throw deploy::DeploymentException(
1408cdf0e10cSrcweir             OUSTR("Extension Manager: exception in synchronize"),
1409cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1410cdf0e10cSrcweir     }
1411cdf0e10cSrcweir }
1412cdf0e10cSrcweir 
1413cdf0e10cSrcweir // Notify the user when a new extension is to be installed. This is only the
1414cdf0e10cSrcweir // case when one uses the system integration to install an extension (double
1415cdf0e10cSrcweir // clicking on .oxt file etc.)). The function must only be called if there is no
1416cdf0e10cSrcweir // extension with the same identifier already deployed. Then the checkUpdate
1417cdf0e10cSrcweir // function will inform the user that the extension is about to be installed In
1418cdf0e10cSrcweir // case the user cancels the installation a CommandFailed exception is
1419cdf0e10cSrcweir // thrown.
1420cdf0e10cSrcweir void ExtensionManager::checkInstall(
1421cdf0e10cSrcweir 	OUString const & displayName,
1422cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & cmdEnv)
1423cdf0e10cSrcweir {
1424cdf0e10cSrcweir         uno::Any request(
1425cdf0e10cSrcweir 			deploy::InstallException(
1426cdf0e10cSrcweir 				OUSTR("Extension ") + displayName +
1427cdf0e10cSrcweir                 OUSTR(" is about to be installed."),
1428cdf0e10cSrcweir 				static_cast<OWeakObject *>(this), displayName));
1429cdf0e10cSrcweir 	    bool approve = false, abort = false;
1430cdf0e10cSrcweir         if (! dp_misc::interactContinuation(
1431cdf0e10cSrcweir                 request, task::XInteractionApprove::static_type(),
1432cdf0e10cSrcweir                 cmdEnv, &approve, &abort ))
1433cdf0e10cSrcweir 		{
1434cdf0e10cSrcweir             OSL_ASSERT( !approve && !abort );
1435cdf0e10cSrcweir             throw deploy::DeploymentException(
1436cdf0e10cSrcweir                 dp_misc::getResourceString(RID_STR_ERROR_WHILE_ADDING) + displayName,
1437cdf0e10cSrcweir                 static_cast<OWeakObject *>(this), request );
1438cdf0e10cSrcweir         }
1439cdf0e10cSrcweir         if (abort || !approve)
1440cdf0e10cSrcweir             throw ucb::CommandFailedException(
1441cdf0e10cSrcweir                 dp_misc::getResourceString(RID_STR_ERROR_WHILE_ADDING) + displayName,
1442cdf0e10cSrcweir                 static_cast<OWeakObject *>(this), request );
1443cdf0e10cSrcweir }
1444cdf0e10cSrcweir 
1445cdf0e10cSrcweir /* The function will make the user interaction in case there is an extension
1446cdf0e10cSrcweir installed with the same id. This function may only be called if there is already
1447cdf0e10cSrcweir an extension.
1448cdf0e10cSrcweir */
1449cdf0e10cSrcweir void ExtensionManager::checkUpdate(
1450cdf0e10cSrcweir     OUString const & newVersion,
1451cdf0e10cSrcweir     OUString const & newDisplayName,
1452cdf0e10cSrcweir     Reference<deploy::XPackage> const & oldExtension,
1453cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1454cdf0e10cSrcweir {
1455cdf0e10cSrcweir     // package already deployed, interact --force:
1456cdf0e10cSrcweir     uno::Any request(
1457cdf0e10cSrcweir         (deploy::VersionException(
1458cdf0e10cSrcweir             dp_misc::getResourceString(
1459cdf0e10cSrcweir                 RID_STR_PACKAGE_ALREADY_ADDED ) + newDisplayName,
1460cdf0e10cSrcweir             static_cast<OWeakObject *>(this), newVersion, newDisplayName,
1461cdf0e10cSrcweir             oldExtension ) ) );
1462cdf0e10cSrcweir     bool replace = false, abort = false;
1463cdf0e10cSrcweir     if (! dp_misc::interactContinuation(
1464cdf0e10cSrcweir             request, task::XInteractionApprove::static_type(),
1465cdf0e10cSrcweir             xCmdEnv, &replace, &abort )) {
1466cdf0e10cSrcweir         OSL_ASSERT( !replace && !abort );
1467cdf0e10cSrcweir         throw deploy::DeploymentException(
1468cdf0e10cSrcweir             dp_misc::getResourceString(
1469cdf0e10cSrcweir                 RID_STR_ERROR_WHILE_ADDING) + newDisplayName,
1470cdf0e10cSrcweir             static_cast<OWeakObject *>(this), request );
1471cdf0e10cSrcweir     }
1472cdf0e10cSrcweir     if (abort || !replace)
1473cdf0e10cSrcweir         throw ucb::CommandFailedException(
1474cdf0e10cSrcweir             dp_misc::getResourceString(
1475cdf0e10cSrcweir                 RID_STR_PACKAGE_ALREADY_ADDED) + newDisplayName,
1476cdf0e10cSrcweir             static_cast<OWeakObject *>(this), request );
1477cdf0e10cSrcweir }
1478cdf0e10cSrcweir 
1479cdf0e10cSrcweir Reference<deploy::XPackage> ExtensionManager::getTempExtension(
1480cdf0e10cSrcweir     OUString const & url,
1481cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1482cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & /*xCmdEnv*/)
1483cdf0e10cSrcweir 
1484cdf0e10cSrcweir {
1485cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> tmpCmdEnvA(new TmpRepositoryCommandEnv());
1486cdf0e10cSrcweir     Reference<deploy::XPackage> xTmpPackage = getTmpRepository()->addPackage(
1487cdf0e10cSrcweir         url, uno::Sequence<beans::NamedValue>(),OUString(), xAbortChannel, tmpCmdEnvA);
1488cdf0e10cSrcweir     if (!xTmpPackage.is())
1489cdf0e10cSrcweir     {
1490cdf0e10cSrcweir         throw deploy::DeploymentException(
1491cdf0e10cSrcweir             OUSTR("Extension Manager: Failed to create temporary XPackage for url: ") + url,
1492cdf0e10cSrcweir             static_cast<OWeakObject*>(this), uno::Any());
1493cdf0e10cSrcweir 
1494cdf0e10cSrcweir     }
1495cdf0e10cSrcweir     return xTmpPackage;
1496cdf0e10cSrcweir }
1497cdf0e10cSrcweir 
1498cdf0e10cSrcweir uno::Sequence<Reference<deploy::XPackage> > SAL_CALL
1499cdf0e10cSrcweir ExtensionManager::getExtensionsWithUnacceptedLicenses(
1500cdf0e10cSrcweir         OUString const & repository,
1501cdf0e10cSrcweir         Reference<ucb::XCommandEnvironment> const & xCmdEnv)
1502cdf0e10cSrcweir         throw (deploy::DeploymentException,
1503cdf0e10cSrcweir                uno::RuntimeException)
1504cdf0e10cSrcweir {
1505cdf0e10cSrcweir     Reference<deploy::XPackageManager>
1506cdf0e10cSrcweir         xPackageManager = getPackageManager(repository);
1507cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
1508cdf0e10cSrcweir     return xPackageManager->getExtensionsWithUnacceptedLicenses(xCmdEnv);
1509cdf0e10cSrcweir }
1510cdf0e10cSrcweir 
1511cdf0e10cSrcweir sal_Bool ExtensionManager::isReadOnlyRepository(::rtl::OUString const & repository)
1512cdf0e10cSrcweir         throw (uno::RuntimeException)
1513cdf0e10cSrcweir {
1514cdf0e10cSrcweir     return getPackageManager(repository)->isReadOnly();
1515cdf0e10cSrcweir }
1516cdf0e10cSrcweir //------------------------------------------------------------------------------
1517cdf0e10cSrcweir //------------------------------------------------------------------------------
1518cdf0e10cSrcweir //------------------------------------------------------------------------------
1519cdf0e10cSrcweir 
1520cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
1521cdf0e10cSrcweir sdecl::class_<ExtensionManager> servicePIP;
1522cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
1523cdf0e10cSrcweir     servicePIP,
1524cdf0e10cSrcweir     // a private one:
1525cdf0e10cSrcweir     "com.sun.star.comp.deployment.ExtensionManager",
1526cdf0e10cSrcweir     "com.sun.star.comp.deployment.ExtensionManager");
1527cdf0e10cSrcweir 
1528cdf0e10cSrcweir //------------------------------------------------------------------------------
1529cdf0e10cSrcweir bool singleton_entries(
1530cdf0e10cSrcweir     uno::Reference< registry::XRegistryKey > const & xRegistryKey )
1531cdf0e10cSrcweir {
1532cdf0e10cSrcweir     try {
1533cdf0e10cSrcweir         uno::Reference< registry::XRegistryKey > xKey(
1534cdf0e10cSrcweir             xRegistryKey->createKey(
1535cdf0e10cSrcweir                 serviceDecl.getImplementationName() +
1536cdf0e10cSrcweir                 // xxx todo: use future generated function to get singleton name
1537cdf0e10cSrcweir                 OUSTR("/UNO/SINGLETONS/"
1538cdf0e10cSrcweir                       "com.sun.star.deployment.ExtensionManager") ) );
1539cdf0e10cSrcweir         xKey->setStringValue( serviceDecl.getSupportedServiceNames()[0] );
1540cdf0e10cSrcweir         return true;
1541cdf0e10cSrcweir     }
1542cdf0e10cSrcweir     catch (registry::InvalidRegistryException & exc) {
1543cdf0e10cSrcweir         (void) exc; // avoid warnings
1544cdf0e10cSrcweir         OSL_ENSURE( 0, ::rtl::OUStringToOString(
1545cdf0e10cSrcweir                         exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1546cdf0e10cSrcweir         return false;
1547cdf0e10cSrcweir     }
1548cdf0e10cSrcweir }
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir // XModifyBroadcaster
1551cdf0e10cSrcweir //______________________________________________________________________________
1552cdf0e10cSrcweir void ExtensionManager::addModifyListener(
1553cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
1554cdf0e10cSrcweir     throw (uno::RuntimeException)
1555cdf0e10cSrcweir {
1556cdf0e10cSrcweir      check();
1557cdf0e10cSrcweir      rBHelper.addListener( ::getCppuType( &xListener ), xListener );
1558cdf0e10cSrcweir }
1559cdf0e10cSrcweir 
1560cdf0e10cSrcweir //______________________________________________________________________________
1561cdf0e10cSrcweir void ExtensionManager::removeModifyListener(
1562cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
1563cdf0e10cSrcweir     throw (uno::RuntimeException)
1564cdf0e10cSrcweir {
1565cdf0e10cSrcweir     check();
1566cdf0e10cSrcweir     rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
1567cdf0e10cSrcweir }
1568cdf0e10cSrcweir 
1569cdf0e10cSrcweir void ExtensionManager::check()
1570cdf0e10cSrcweir {
1571cdf0e10cSrcweir     ::osl::MutexGuard guard( getMutex() );
1572cdf0e10cSrcweir     if (rBHelper.bInDispose || rBHelper.bDisposed) {
1573cdf0e10cSrcweir         throw lang::DisposedException(
1574cdf0e10cSrcweir             OUSTR("ExtensionManager instance has already been disposed!"),
1575cdf0e10cSrcweir             static_cast<OWeakObject *>(this) );
1576cdf0e10cSrcweir     }
1577cdf0e10cSrcweir }
1578cdf0e10cSrcweir 
1579cdf0e10cSrcweir void ExtensionManager::fireModified()
1580cdf0e10cSrcweir {
1581cdf0e10cSrcweir     ::cppu::OInterfaceContainerHelper * pContainer = rBHelper.getContainer(
1582cdf0e10cSrcweir         util::XModifyListener::static_type() );
1583cdf0e10cSrcweir     if (pContainer != 0) {
1584cdf0e10cSrcweir         pContainer->forEach<util::XModifyListener>(
1585cdf0e10cSrcweir             boost::bind(&util::XModifyListener::modified, _1,
1586cdf0e10cSrcweir                         lang::EventObject(static_cast<OWeakObject *>(this))) );
1587cdf0e10cSrcweir     }
1588cdf0e10cSrcweir }
1589cdf0e10cSrcweir 
1590cdf0e10cSrcweir } // namespace dp_manager
1591cdf0e10cSrcweir 
1592cdf0e10cSrcweir 
1593