xref: /AOO41X/main/desktop/source/deployment/manager/dp_manager.cxx (revision 8402cd44417f129945a876644c3077e66211c2c3)
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 "dp_ucb.h"
32cdf0e10cSrcweir #include "dp_resource.h"
33cdf0e10cSrcweir #include "dp_platform.hxx"
34cdf0e10cSrcweir #include "dp_manager.h"
35cdf0e10cSrcweir #include "dp_identifier.hxx"
36cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
37cdf0e10cSrcweir #include "rtl/string.hxx"
38cdf0e10cSrcweir #include "rtl/uri.hxx"
39cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
40cdf0e10cSrcweir #include "osl/diagnose.h"
41cdf0e10cSrcweir #include "osl/file.hxx"
42cdf0e10cSrcweir #include "osl/security.hxx"
43cdf0e10cSrcweir #include "cppuhelper/weakref.hxx"
44cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
45cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
46cdf0e10cSrcweir #include "cppuhelper/interfacecontainer.hxx"
47cdf0e10cSrcweir #include "comphelper/servicedecl.hxx"
48cdf0e10cSrcweir #include "comphelper/sequence.hxx"
49cdf0e10cSrcweir #include "xmlscript/xml_helper.hxx"
50cdf0e10cSrcweir #include "svl/inettype.hxx"
51cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
52cdf0e10cSrcweir #include "com/sun/star/lang/WrappedTargetRuntimeException.hpp"
53cdf0e10cSrcweir #include "com/sun/star/beans/UnknownPropertyException.hpp"
54cdf0e10cSrcweir #include "com/sun/star/util/XUpdatable.hpp"
55cdf0e10cSrcweir #include "com/sun/star/sdbc/XResultSet.hpp"
56cdf0e10cSrcweir #include "com/sun/star/sdbc/XRow.hpp"
57cdf0e10cSrcweir #include "com/sun/star/ucb/XContentAccess.hpp"
58cdf0e10cSrcweir #include "com/sun/star/ucb/NameClash.hpp"
59cdf0e10cSrcweir #include "com/sun/star/deployment/VersionException.hpp"
60cdf0e10cSrcweir #include "com/sun/star/deployment/InstallException.hpp"
61cdf0e10cSrcweir #include "com/sun/star/deployment/Prerequisites.hpp"
62cdf0e10cSrcweir #include "com/sun/star/task/XInteractionApprove.hpp"
63cdf0e10cSrcweir #include "com/sun/star/ucb/UnsupportedCommandException.hpp"
64cdf0e10cSrcweir #include "boost/bind.hpp"
65cdf0e10cSrcweir #include "tools/urlobj.hxx"
66cdf0e10cSrcweir #include "unotools/tempfile.hxx"
67cdf0e10cSrcweir 
68cdf0e10cSrcweir #include "osl/file.hxx"
69cdf0e10cSrcweir #include <vector>
70cdf0e10cSrcweir #include <list>
71cdf0e10cSrcweir #include "dp_descriptioninfoset.hxx"
72cdf0e10cSrcweir #include "dp_commandenvironments.hxx"
73cdf0e10cSrcweir #include "dp_properties.hxx"
74cdf0e10cSrcweir 
75cdf0e10cSrcweir using namespace ::dp_misc;
76cdf0e10cSrcweir using namespace ::com::sun::star;
77cdf0e10cSrcweir using namespace ::com::sun::star::uno;
78cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
79cdf0e10cSrcweir using ::rtl::OUString;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir namespace dp_log {
82cdf0e10cSrcweir extern comphelper::service_decl::ServiceDecl const serviceDecl;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir namespace dp_registry {
86cdf0e10cSrcweir Reference<deployment::XPackageRegistry> create(
87cdf0e10cSrcweir     OUString const & context,
88cdf0e10cSrcweir     OUString const & cachePath, bool readOnly,
89cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext );
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir namespace dp_manager {
93cdf0e10cSrcweir 
94cdf0e10cSrcweir struct MatchTempDir
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     OUString m_str;
97cdf0e10cSrcweir     MatchTempDir( OUString const & str ) : m_str( str ) {}
98cdf0e10cSrcweir     bool operator () ( ActivePackages::Entries::value_type const & v ) const {
99cdf0e10cSrcweir         return v.second.temporaryName.equalsIgnoreAsciiCase( m_str );
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir };
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 
104cdf0e10cSrcweir namespace {
105cdf0e10cSrcweir OUString getExtensionFolder(OUString const &  parentFolder,
106cdf0e10cSrcweir                             Reference<ucb::XCommandEnvironment> const & xCmdEnv)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     ::ucbhelper::Content tempFolder(
109cdf0e10cSrcweir         parentFolder, xCmdEnv );
110cdf0e10cSrcweir     Reference<sdbc::XResultSet> xResultSet(
111cdf0e10cSrcweir         tempFolder.createCursor(
112cdf0e10cSrcweir             Sequence<OUString>( &StrTitle::get(), 1 ),
113cdf0e10cSrcweir             ::ucbhelper::INCLUDE_FOLDERS_ONLY ) );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     OUString title;
116cdf0e10cSrcweir     while (xResultSet->next())
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         title = Reference<sdbc::XRow>(
119cdf0e10cSrcweir             xResultSet, UNO_QUERY_THROW )->getString(1 /* Title */ ) ;
120cdf0e10cSrcweir         break;
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir     return title;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir }
125cdf0e10cSrcweir //______________________________________________________________________________
126cdf0e10cSrcweir void PackageManagerImpl::initActivationLayer(
127cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     if (m_activePackages.getLength() == 0)
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         OSL_ASSERT( m_registryCache.getLength() == 0 );
132cdf0e10cSrcweir         // documents temp activation:
133cdf0e10cSrcweir         m_activePackagesDB.reset( new ActivePackages );
134cdf0e10cSrcweir         ::ucbhelper::Content ucbContent;
135cdf0e10cSrcweir         if (create_ucb_content( &ucbContent, m_context, xCmdEnv,
136cdf0e10cSrcweir                                 false /* no throw */ ))
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             // scan for all entries in m_packagesDir:
139cdf0e10cSrcweir             Reference<sdbc::XResultSet> xResultSet(
140cdf0e10cSrcweir                 ucbContent.createCursor(
141cdf0e10cSrcweir                     Sequence<OUString>( &StrTitle::get(), 1 ),
142cdf0e10cSrcweir                     ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS ) );
143cdf0e10cSrcweir             while (xResultSet->next())
144cdf0e10cSrcweir             {
145cdf0e10cSrcweir                 Reference<sdbc::XRow> xRow( xResultSet, UNO_QUERY_THROW );
146cdf0e10cSrcweir                 OUString title( xRow->getString( 1 /* Title */ ) );
147cdf0e10cSrcweir                 // xxx todo: remove workaround for tdoc
148cdf0e10cSrcweir                 if (title.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
149cdf0e10cSrcweir                                             "this_is_a_dummy_stream_just_there_"
150cdf0e10cSrcweir                                             "as_a_workaround_for_a_"
151cdf0e10cSrcweir                                             "temporary_limitation_of_the_"
152cdf0e10cSrcweir                                             "storage_api_implementation") ))
153cdf0e10cSrcweir                     continue;
154cdf0e10cSrcweir                 if (title.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
155cdf0e10cSrcweir                                              "META-INF") ) )
156cdf0e10cSrcweir                     continue;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir                 ::ucbhelper::Content sourceContent(
159cdf0e10cSrcweir                     Reference<XContentAccess>(
160cdf0e10cSrcweir                         xResultSet, UNO_QUERY_THROW )->queryContent(),
161cdf0e10cSrcweir                     xCmdEnv );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir                 OUString mediaType( detectMediaType( sourceContent,
164cdf0e10cSrcweir                                                      false /* no throw */) );
165cdf0e10cSrcweir                 if (mediaType.getLength() >0)
166cdf0e10cSrcweir                 {
167cdf0e10cSrcweir                     ActivePackages::Data dbData;
168cdf0e10cSrcweir                     insertToActivationLayer(
169cdf0e10cSrcweir                         Sequence<css::beans::NamedValue>(),mediaType, sourceContent,
170cdf0e10cSrcweir                         title, &dbData );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir                     insertToActivationLayerDB( title, dbData );
173cdf0e10cSrcweir                         //TODO #i73136#: insertToActivationLayerDB needs id not
174cdf0e10cSrcweir                         // title, but the whole m_activePackages.getLength()==0
175cdf0e10cSrcweir                         // case (i.e., document-relative deployment) currently
176cdf0e10cSrcweir                         // does not work, anyway.
177cdf0e10cSrcweir                 }
178cdf0e10cSrcweir             }
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir     else
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         // user|share:
184cdf0e10cSrcweir         OSL_ASSERT( m_activePackages.getLength() > 0 );
185cdf0e10cSrcweir         m_activePackages_expanded = expandUnoRcUrl( m_activePackages );
186cdf0e10cSrcweir         m_registrationData_expanded = expandUnoRcUrl(m_registrationData);
187cdf0e10cSrcweir         if (!m_readOnly)
188cdf0e10cSrcweir             create_folder( 0, m_activePackages_expanded, xCmdEnv, true);
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         OUString dbName;
191cdf0e10cSrcweir         if (m_context.equals(OUSTR("user")))
192cdf0e10cSrcweir             dbName = m_activePackages_expanded + OUSTR(".db");
193cdf0e10cSrcweir         else
194cdf0e10cSrcweir         {
195cdf0e10cSrcweir             //Create the extension data base in the user installation
196cdf0e10cSrcweir             create_folder( 0, m_registrationData_expanded, xCmdEnv, true);
197cdf0e10cSrcweir             dbName = m_registrationData_expanded + OUSTR("/extensions.db");
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir         //The data base can always be written because it it always in the user installation
200cdf0e10cSrcweir         m_activePackagesDB.reset(
201cdf0e10cSrcweir             new ActivePackages( dbName, false ) );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         if (! m_readOnly && ! m_context.equals(OUSTR("bundled")))
204cdf0e10cSrcweir         {
205cdf0e10cSrcweir             // clean up activation layer, scan for zombie temp dirs:
206cdf0e10cSrcweir             ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir             ::ucbhelper::Content tempFolder(
209cdf0e10cSrcweir                 m_activePackages_expanded, xCmdEnv );
210cdf0e10cSrcweir             Reference<sdbc::XResultSet> xResultSet(
211cdf0e10cSrcweir                 tempFolder.createCursor(
212cdf0e10cSrcweir                     Sequence<OUString>( &StrTitle::get(), 1 ),
213cdf0e10cSrcweir                     ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
214cdf0e10cSrcweir             // get all temp directories:
215cdf0e10cSrcweir             ::std::vector<OUString> tempEntries;
216cdf0e10cSrcweir             ::std::vector<OUString> removedEntries;
217cdf0e10cSrcweir             while (xResultSet->next())
218cdf0e10cSrcweir             {
219cdf0e10cSrcweir                 OUString title(
220cdf0e10cSrcweir                     Reference<sdbc::XRow>(
221cdf0e10cSrcweir                         xResultSet, UNO_QUERY_THROW )->getString(
222cdf0e10cSrcweir                             1 /* Title */ ) );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                 const char extensionRemoved[] = "removed";
225cdf0e10cSrcweir                 if (title.endsWithAsciiL(
226cdf0e10cSrcweir                         extensionRemoved, sizeof(extensionRemoved) - 1))
227cdf0e10cSrcweir                 {
228cdf0e10cSrcweir                     //save the file name withouth the "removed" part
229cdf0e10cSrcweir                     sal_Int32 index = title.lastIndexOfAsciiL(
230cdf0e10cSrcweir                         extensionRemoved, sizeof(extensionRemoved) - 1);
231cdf0e10cSrcweir                     OUString remFile = title.copy(0, index);
232cdf0e10cSrcweir                     removedEntries.push_back(::rtl::Uri::encode(
233cdf0e10cSrcweir                                                 remFile, rtl_UriCharClassPchar,
234cdf0e10cSrcweir                                                 rtl_UriEncodeIgnoreEscapes,
235cdf0e10cSrcweir                                                 RTL_TEXTENCODING_UTF8 ) );
236cdf0e10cSrcweir                 }
237cdf0e10cSrcweir                 else
238cdf0e10cSrcweir                 {
239cdf0e10cSrcweir                     tempEntries.push_back( ::rtl::Uri::encode(
240cdf0e10cSrcweir                                                title, rtl_UriCharClassPchar,
241cdf0e10cSrcweir                                                rtl_UriEncodeIgnoreEscapes,
242cdf0e10cSrcweir                                                RTL_TEXTENCODING_UTF8 ) );
243cdf0e10cSrcweir                 }
244cdf0e10cSrcweir             }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir             bool bShared = m_context.equals(OUSTR("shared")) ? true : false;
247cdf0e10cSrcweir             for ( ::std::size_t pos = 0; pos < tempEntries.size(); ++pos )
248cdf0e10cSrcweir             {
249cdf0e10cSrcweir                 OUString const & tempEntry = tempEntries[ pos ];
250cdf0e10cSrcweir                 const MatchTempDir match( tempEntry );
251cdf0e10cSrcweir                 if (::std::find_if( id2temp.begin(), id2temp.end(), match ) ==
252cdf0e10cSrcweir                     id2temp.end())
253cdf0e10cSrcweir                 {
254cdf0e10cSrcweir                     const OUString url(
255cdf0e10cSrcweir                         makeURL(m_activePackages_expanded, tempEntry ) );
256cdf0e10cSrcweir 
257cdf0e10cSrcweir                     //In case of shared extensions, new entries are regarded as
258cdf0e10cSrcweir                     //added extensions if there is no xxx.tmpremoved file.
259cdf0e10cSrcweir                     if (bShared)
260cdf0e10cSrcweir                     {
261cdf0e10cSrcweir                         if (::std::find(removedEntries.begin(), removedEntries.end(), tempEntry) ==
262cdf0e10cSrcweir                             removedEntries.end())
263cdf0e10cSrcweir                         {
264cdf0e10cSrcweir                             continue;
265cdf0e10cSrcweir                         }
266cdf0e10cSrcweir                         else
267cdf0e10cSrcweir                         {
268cdf0e10cSrcweir                             //Make sure only the same user removes the extension, who
269cdf0e10cSrcweir                             //previously unregistered it. This is avoid races if multiple instances
270cdf0e10cSrcweir                             //of OOo are running which all have write access to the shared installation.
271cdf0e10cSrcweir                             //For example, a user removes the extension, but keeps OOo
272cdf0e10cSrcweir                             //running. Parts of the extension may still be loaded and used by OOo.
273cdf0e10cSrcweir                             //Therefore the extension is only deleted the next time the extension manager is
274cdf0e10cSrcweir                             //run after restarting OOo. While OOo is still running, another user starts OOo
275cdf0e10cSrcweir                             //which would deleted the extension files. If the same user starts another
276cdf0e10cSrcweir                             //instance of OOo then the lock file will prevent this.
277cdf0e10cSrcweir                             OUString aUserName;
278cdf0e10cSrcweir                             ::osl::Security aSecurity;
279cdf0e10cSrcweir                             aSecurity.getUserName( aUserName );
280cdf0e10cSrcweir                             ucbhelper::Content remFileContent(
281cdf0e10cSrcweir                                 url + OUSTR("removed"), Reference<XCommandEnvironment>());
282cdf0e10cSrcweir                             ::rtl::ByteSequence data = dp_misc::readFile(remFileContent);
283cdf0e10cSrcweir                             ::rtl::OString osData(reinterpret_cast<const sal_Char*>(data.getConstArray()),
284cdf0e10cSrcweir                                                   data.getLength());
285cdf0e10cSrcweir                             OUString sData = ::rtl::OStringToOUString(
286cdf0e10cSrcweir                                 osData, RTL_TEXTENCODING_UTF8);
287cdf0e10cSrcweir                             if (!sData.equals(aUserName))
288cdf0e10cSrcweir                                 continue;
289cdf0e10cSrcweir                         }
290cdf0e10cSrcweir                     }
291cdf0e10cSrcweir                     // temp entry not needed anymore:
292cdf0e10cSrcweir                     erase_path( url + OUSTR("_"),
293cdf0e10cSrcweir                                 Reference<XCommandEnvironment>(),
294cdf0e10cSrcweir                                 false /* no throw: ignore errors */ );
295cdf0e10cSrcweir                     erase_path( url, Reference<XCommandEnvironment>(),
296cdf0e10cSrcweir                                 false /* no throw: ignore errors */ );
297cdf0e10cSrcweir                     //delete the xxx.tmpremoved file
298cdf0e10cSrcweir                     erase_path(url + OUSTR("removed"),
299cdf0e10cSrcweir                                Reference<XCommandEnvironment>(), false);
300cdf0e10cSrcweir                 }
301cdf0e10cSrcweir             }
302cdf0e10cSrcweir         }
303cdf0e10cSrcweir     }
304cdf0e10cSrcweir }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir //______________________________________________________________________________
307cdf0e10cSrcweir void PackageManagerImpl::initRegistryBackends()
308cdf0e10cSrcweir {
309cdf0e10cSrcweir     if (m_registryCache.getLength() > 0)
310cdf0e10cSrcweir         create_folder( 0, m_registryCache,
311cdf0e10cSrcweir                        Reference<XCommandEnvironment>(), false);
312cdf0e10cSrcweir     m_xRegistry.set( ::dp_registry::create(
313cdf0e10cSrcweir                          m_context, m_registryCache, false,
314cdf0e10cSrcweir                          m_xComponentContext ) );
315cdf0e10cSrcweir }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir //______________________________________________________________________________
318cdf0e10cSrcweir Reference<deployment::XPackageManager> PackageManagerImpl::create(
319cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext,
320cdf0e10cSrcweir     OUString const & context )
321cdf0e10cSrcweir {
322cdf0e10cSrcweir     PackageManagerImpl * that = new PackageManagerImpl(
323cdf0e10cSrcweir         xComponentContext, context );
324cdf0e10cSrcweir     Reference<deployment::XPackageManager> xPackageManager( that );
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     OUString packages, logFile, stampURL;
327cdf0e10cSrcweir     if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("user") )) {
328cdf0e10cSrcweir         that->m_activePackages = OUSTR(
329cdf0e10cSrcweir             "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages");
330cdf0e10cSrcweir         that->m_registrationData = OUSTR(
331cdf0e10cSrcweir             "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE");
332cdf0e10cSrcweir         that->m_registryCache = OUSTR(
333cdf0e10cSrcweir             "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/registry");
334cdf0e10cSrcweir         logFile = OUSTR(
335cdf0e10cSrcweir             "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/log.txt");
336cdf0e10cSrcweir         //We use the extension .sys for the file because on Windows Vista a sys
337cdf0e10cSrcweir         //(as well as exe and dll) file
338cdf0e10cSrcweir         //will not be written in the VirtualStore. For example if the process has no
339cdf0e10cSrcweir         //admin right once cannot write to the %programfiles% folder. However, when
340cdf0e10cSrcweir         //virtualization is used, the file will be written into the VirtualStore and
341cdf0e10cSrcweir         //it appears as if one could write to %programfiles%. When we test for write
342cdf0e10cSrcweir         //access to the office/shared folder for shared extensions then this typically
343cdf0e10cSrcweir         //fails because a normal user typically cannot write to this folder. However,
344cdf0e10cSrcweir         //using virtualization it appears that he/she can. Then a shared extension can
345cdf0e10cSrcweir         //be installed but is only visible for the user (because the extension is in
346cdf0e10cSrcweir         //the virtual store).
347cdf0e10cSrcweir         stampURL = OUSTR(
348cdf0e10cSrcweir             "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/stamp.sys");
349cdf0e10cSrcweir     }
350cdf0e10cSrcweir     else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("shared") )) {
351cdf0e10cSrcweir         that->m_activePackages = OUSTR(
352cdf0e10cSrcweir             "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE/uno_packages");
353cdf0e10cSrcweir         that->m_registrationData = OUSTR(
354cdf0e10cSrcweir             "vnd.sun.star.expand:$SHARED_EXTENSIONS_USER");
355cdf0e10cSrcweir         that->m_registryCache = OUSTR(
356cdf0e10cSrcweir             "vnd.sun.star.expand:$SHARED_EXTENSIONS_USER/registry");
357cdf0e10cSrcweir         logFile = OUSTR(
358cdf0e10cSrcweir             "vnd.sun.star.expand:$SHARED_EXTENSIONS_USER/log.txt");
359cdf0e10cSrcweir         stampURL = OUSTR(
360cdf0e10cSrcweir             "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE/stamp.sys");
361cdf0e10cSrcweir     }
362cdf0e10cSrcweir     else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bundled") )) {
363cdf0e10cSrcweir         that->m_activePackages = OUSTR(
364cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS");
365cdf0e10cSrcweir         that->m_registrationData = OUSTR(
366cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS_USER");
367cdf0e10cSrcweir         that->m_registryCache = OUSTR(
368cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS_USER/registry");
369cdf0e10cSrcweir         logFile = OUSTR(
370cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS_USER/log.txt");
371cdf0e10cSrcweir         //No stamp file. We assume that bundled is always readonly. It must not be
372cdf0e10cSrcweir         //modified from ExtensionManager but only by the installer
373cdf0e10cSrcweir     }
374cdf0e10cSrcweir     else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bundled_prereg") )) {
375cdf0e10cSrcweir         //This is a bundled repository but the registration data
376cdf0e10cSrcweir         //is in the brand layer: share/prereg
377cdf0e10cSrcweir         //It is special because the registration data are copied at the first startup
378cdf0e10cSrcweir         //into the user installation. The processed help and xcu files are not
379cdf0e10cSrcweir         //copied. Instead the backenddb.xml for the help backend references the help
380cdf0e10cSrcweir         //by using $BUNDLED_EXTENSION_PREREG instead $BUNDLED_EXTENSIONS_USER. The
381cdf0e10cSrcweir         //configmgr.ini also used $BUNDLED_EXTENSIONS_PREREG to refer to the xcu file
382cdf0e10cSrcweir         //which contain the replacement for %origin%.
383cdf0e10cSrcweir         that->m_activePackages = OUSTR(
384cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS");
385cdf0e10cSrcweir         that->m_registrationData = OUSTR(
386cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS_PREREG");
387cdf0e10cSrcweir         that->m_registryCache = OUSTR(
388cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS_PREREG/registry");
389cdf0e10cSrcweir         logFile = OUSTR(
390cdf0e10cSrcweir             "vnd.sun.star.expand:$BUNDLED_EXTENSIONS_PREREG/log.txt");
391cdf0e10cSrcweir     }
392cdf0e10cSrcweir     else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("tmp") )) {
393cdf0e10cSrcweir         that->m_activePackages = OUSTR(
394cdf0e10cSrcweir             "vnd.sun.star.expand:$TMP_EXTENSIONS/extensions");
395cdf0e10cSrcweir         that->m_registrationData = OUSTR(
396cdf0e10cSrcweir             "vnd.sun.star.expand:$TMP_EXTENSIONS");
397cdf0e10cSrcweir         that->m_registryCache = OUSTR(
398cdf0e10cSrcweir             "vnd.sun.star.expand:$TMP_EXTENSIONS/registry");
399cdf0e10cSrcweir         stampURL = OUSTR(
400cdf0e10cSrcweir             "vnd.sun.star.expand:$TMP_EXTENSIONS/stamp.sys");
401cdf0e10cSrcweir     }
4027bc7b19fSMichael Stahl     else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bak") )) {
4037bc7b19fSMichael Stahl         that->m_activePackages = OUSTR(
4047bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS/extensions");
4057bc7b19fSMichael Stahl         that->m_registrationData = OUSTR(
4067bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS");
4077bc7b19fSMichael Stahl         that->m_registryCache = OUSTR(
4087bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS/registry");
4097bc7b19fSMichael Stahl         stampURL = OUSTR(
4107bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS/stamp.sys");
4117bc7b19fSMichael Stahl     }
4127bc7b19fSMichael Stahl 
413cdf0e10cSrcweir     else if (! context.matchAsciiL(
414cdf0e10cSrcweir                  RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.tdoc:/") )) {
415cdf0e10cSrcweir         throw lang::IllegalArgumentException(
416cdf0e10cSrcweir             OUSTR("invalid context given: ") + context,
417cdf0e10cSrcweir             Reference<XInterface>(), static_cast<sal_Int16>(-1) );
418cdf0e10cSrcweir     }
419cdf0e10cSrcweir 
420cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
421cdf0e10cSrcweir 
422cdf0e10cSrcweir     try {
423cdf0e10cSrcweir         //There is no stampURL for the bundled folder
424cdf0e10cSrcweir         if (stampURL.getLength() > 0)
425cdf0e10cSrcweir         {
426cdf0e10cSrcweir #define CURRENT_STAMP "1"
427cdf0e10cSrcweir             try {
428cdf0e10cSrcweir                 //The osl file API does not allow to find out if one can write
429cdf0e10cSrcweir                 //into a folder. Therefore we try to write a file. Then we delete
430cdf0e10cSrcweir                 //it, so that it does not hinder uninstallation of OOo
431cdf0e10cSrcweir                 // probe writing:
432cdf0e10cSrcweir                 ::ucbhelper::Content ucbStamp( stampURL, xCmdEnv );
433cdf0e10cSrcweir                 ::rtl::OString stamp(
434cdf0e10cSrcweir                     RTL_CONSTASCII_STRINGPARAM(CURRENT_STAMP) );
435cdf0e10cSrcweir                 Reference<io::XInputStream> xData(
436cdf0e10cSrcweir                     ::xmlscript::createInputStream(
437cdf0e10cSrcweir                         ::rtl::ByteSequence(
438cdf0e10cSrcweir                             reinterpret_cast<sal_Int8 const *>(stamp.getStr()),
439cdf0e10cSrcweir                             stamp.getLength() ) ) );
440cdf0e10cSrcweir                 ucbStamp.writeStream( xData, true /* replace existing */ );
441cdf0e10cSrcweir                 that->m_readOnly = false;
442cdf0e10cSrcweir                 erase_path( stampURL, xCmdEnv );
443cdf0e10cSrcweir             }
444cdf0e10cSrcweir             catch (RuntimeException &) {
445cdf0e10cSrcweir                 try {
446cdf0e10cSrcweir                     erase_path( stampURL, xCmdEnv );
447cdf0e10cSrcweir                 } catch (...)
448cdf0e10cSrcweir                 {
449cdf0e10cSrcweir                 }
450cdf0e10cSrcweir                 throw;
451cdf0e10cSrcweir             }
452cdf0e10cSrcweir             catch (Exception &) {
453cdf0e10cSrcweir                 that->m_readOnly = true;
454cdf0e10cSrcweir             }
455cdf0e10cSrcweir         }
456cdf0e10cSrcweir 
457cdf0e10cSrcweir         if (!that->m_readOnly && logFile.getLength() > 0)
458cdf0e10cSrcweir         {
459cdf0e10cSrcweir             const Any any_logFile(logFile);
460cdf0e10cSrcweir             that->m_xLogFile.set(
461cdf0e10cSrcweir                 that->m_xComponentContext->getServiceManager()
462cdf0e10cSrcweir                 ->createInstanceWithArgumentsAndContext(
463cdf0e10cSrcweir                     dp_log::serviceDecl.getSupportedServiceNames()[0],
464cdf0e10cSrcweir                     Sequence<Any>( &any_logFile, 1 ),
465cdf0e10cSrcweir                     that->m_xComponentContext ),
466cdf0e10cSrcweir                 UNO_QUERY_THROW );
467cdf0e10cSrcweir             xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv, that->m_xLogFile ) );
468cdf0e10cSrcweir         }
469cdf0e10cSrcweir 
470cdf0e10cSrcweir         that->initRegistryBackends();
471cdf0e10cSrcweir         that->initActivationLayer( xCmdEnv );
472cdf0e10cSrcweir 
473cdf0e10cSrcweir         return xPackageManager;
474cdf0e10cSrcweir 
475cdf0e10cSrcweir     }
476cdf0e10cSrcweir     catch (RuntimeException &) {
477cdf0e10cSrcweir         throw;
478cdf0e10cSrcweir     }
479cdf0e10cSrcweir     catch (Exception &) {
480cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
481cdf0e10cSrcweir         ::rtl::OUStringBuffer buf;
482cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("[context=\"") );
483cdf0e10cSrcweir         buf.append( context );
484cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
485cdf0e10cSrcweir                              "\"] caught unexpected exception!") );
486cdf0e10cSrcweir         throw lang::WrappedTargetRuntimeException(
487cdf0e10cSrcweir             buf.makeStringAndClear(), Reference<XInterface>(), exc );
488cdf0e10cSrcweir     }
489cdf0e10cSrcweir }
490cdf0e10cSrcweir 
491cdf0e10cSrcweir //______________________________________________________________________________
492cdf0e10cSrcweir PackageManagerImpl::~PackageManagerImpl()
493cdf0e10cSrcweir {
494cdf0e10cSrcweir }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir //______________________________________________________________________________
497cdf0e10cSrcweir void PackageManagerImpl::fireModified()
498cdf0e10cSrcweir {
499cdf0e10cSrcweir     ::cppu::OInterfaceContainerHelper * pContainer = rBHelper.getContainer(
500cdf0e10cSrcweir         util::XModifyListener::static_type() );
501cdf0e10cSrcweir     if (pContainer != 0) {
502cdf0e10cSrcweir         pContainer->forEach<util::XModifyListener>(
503cdf0e10cSrcweir             boost::bind(&util::XModifyListener::modified, _1,
504cdf0e10cSrcweir                         lang::EventObject(static_cast<OWeakObject *>(this))) );
505cdf0e10cSrcweir     }
506cdf0e10cSrcweir }
507cdf0e10cSrcweir 
508cdf0e10cSrcweir //______________________________________________________________________________
509cdf0e10cSrcweir void PackageManagerImpl::disposing()
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     try {
512cdf0e10cSrcweir //     // xxx todo: guarding?
513cdf0e10cSrcweir //     ::osl::MutexGuard guard( getMutex() );
514cdf0e10cSrcweir         try_dispose( m_xLogFile );
515cdf0e10cSrcweir         m_xLogFile.clear();
516cdf0e10cSrcweir         try_dispose( m_xRegistry );
517cdf0e10cSrcweir         m_xRegistry.clear();
518cdf0e10cSrcweir         m_activePackagesDB.reset(0);
519cdf0e10cSrcweir         m_xComponentContext.clear();
520cdf0e10cSrcweir 
521cdf0e10cSrcweir         t_pm_helper::disposing();
522cdf0e10cSrcweir 
523cdf0e10cSrcweir     }
524cdf0e10cSrcweir     catch (RuntimeException &) {
525cdf0e10cSrcweir         throw;
526cdf0e10cSrcweir     }
527cdf0e10cSrcweir     catch (Exception &) {
528cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
529cdf0e10cSrcweir         throw lang::WrappedTargetRuntimeException(
530cdf0e10cSrcweir             OUSTR("caught unexpected exception while disposing..."),
531cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
532cdf0e10cSrcweir     }
533cdf0e10cSrcweir }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir // XComponent
536cdf0e10cSrcweir //______________________________________________________________________________
537cdf0e10cSrcweir void PackageManagerImpl::dispose() throw (RuntimeException)
538cdf0e10cSrcweir {
539*8402cd44SMichael Stahl     //Do not call check here. We must not throw an exception here if the object
540*8402cd44SMichael Stahl     //is being disposed or is already disposed. See com.sun.star.lang.XComponent
541cdf0e10cSrcweir     WeakComponentImplHelperBase::dispose();
542cdf0e10cSrcweir }
543cdf0e10cSrcweir 
544cdf0e10cSrcweir //______________________________________________________________________________
545cdf0e10cSrcweir void PackageManagerImpl::addEventListener(
546cdf0e10cSrcweir     Reference<lang::XEventListener> const & xListener ) throw (RuntimeException)
547cdf0e10cSrcweir {
548*8402cd44SMichael Stahl     //Do not call check here. We must not throw an exception here if the object
549*8402cd44SMichael Stahl     //is being disposed or is already disposed. See com.sun.star.lang.XComponent
550cdf0e10cSrcweir     WeakComponentImplHelperBase::addEventListener( xListener );
551cdf0e10cSrcweir }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir //______________________________________________________________________________
554cdf0e10cSrcweir void PackageManagerImpl::removeEventListener(
555cdf0e10cSrcweir     Reference<lang::XEventListener> const & xListener ) throw (RuntimeException)
556cdf0e10cSrcweir {
557*8402cd44SMichael Stahl     //Do not call check here. We must not throw an exception here if the object
558*8402cd44SMichael Stahl     //is being disposed or is already disposed. See com.sun.star.lang.XComponent
559cdf0e10cSrcweir     WeakComponentImplHelperBase::removeEventListener( xListener );
560cdf0e10cSrcweir }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir // XPackageManager
563cdf0e10cSrcweir //______________________________________________________________________________
564cdf0e10cSrcweir OUString PackageManagerImpl::getContext() throw (RuntimeException)
565cdf0e10cSrcweir {
566cdf0e10cSrcweir     check();
567cdf0e10cSrcweir     return m_context;
568cdf0e10cSrcweir }
569cdf0e10cSrcweir 
570cdf0e10cSrcweir //______________________________________________________________________________
571cdf0e10cSrcweir Sequence< Reference<deployment::XPackageTypeInfo> >
572cdf0e10cSrcweir PackageManagerImpl::getSupportedPackageTypes() throw (RuntimeException)
573cdf0e10cSrcweir {
574cdf0e10cSrcweir     OSL_ASSERT( m_xRegistry.is() );
575cdf0e10cSrcweir     return m_xRegistry->getSupportedPackageTypes();
576cdf0e10cSrcweir }
577cdf0e10cSrcweir 
578cdf0e10cSrcweir //______________________________________________________________________________
579cdf0e10cSrcweir Reference<task::XAbortChannel> PackageManagerImpl::createAbortChannel()
580cdf0e10cSrcweir     throw (RuntimeException)
581cdf0e10cSrcweir {
582cdf0e10cSrcweir     check();
583cdf0e10cSrcweir     return new AbortChannel;
584cdf0e10cSrcweir }
585cdf0e10cSrcweir 
586cdf0e10cSrcweir // XModifyBroadcaster
587cdf0e10cSrcweir //______________________________________________________________________________
588cdf0e10cSrcweir void PackageManagerImpl::addModifyListener(
589cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
590cdf0e10cSrcweir     throw (RuntimeException)
591cdf0e10cSrcweir {
592cdf0e10cSrcweir     check();
593cdf0e10cSrcweir     rBHelper.addListener( ::getCppuType( &xListener ), xListener );
594cdf0e10cSrcweir }
595cdf0e10cSrcweir 
596cdf0e10cSrcweir //______________________________________________________________________________
597cdf0e10cSrcweir void PackageManagerImpl::removeModifyListener(
598cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
599cdf0e10cSrcweir     throw (RuntimeException)
600cdf0e10cSrcweir {
601cdf0e10cSrcweir     check();
602cdf0e10cSrcweir     rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
603cdf0e10cSrcweir }
604cdf0e10cSrcweir 
605cdf0e10cSrcweir //______________________________________________________________________________
606cdf0e10cSrcweir OUString PackageManagerImpl::detectMediaType(
607cdf0e10cSrcweir     ::ucbhelper::Content const & ucbContent_, bool throw_exc )
608cdf0e10cSrcweir {
609cdf0e10cSrcweir     ::ucbhelper::Content ucbContent(ucbContent_);
610cdf0e10cSrcweir     OUString url( ucbContent.getURL() );
611cdf0e10cSrcweir     OUString mediaType;
612cdf0e10cSrcweir     if (url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.tdoc:") ) ||
613cdf0e10cSrcweir         url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.pkg:") ))
614cdf0e10cSrcweir     {
615cdf0e10cSrcweir         try {
616cdf0e10cSrcweir             ucbContent.getPropertyValue( OUSTR("MediaType") ) >>= mediaType;
617cdf0e10cSrcweir         }
618cdf0e10cSrcweir         catch (beans::UnknownPropertyException &) {
619cdf0e10cSrcweir         }
620cdf0e10cSrcweir         OSL_ENSURE( mediaType.getLength() > 0, "### no media-type?!" );
621cdf0e10cSrcweir     }
622cdf0e10cSrcweir     if (mediaType.getLength() == 0)
623cdf0e10cSrcweir     {
624cdf0e10cSrcweir         try {
625cdf0e10cSrcweir             Reference<deployment::XPackage> xPackage(
626cdf0e10cSrcweir                 m_xRegistry->bindPackage(
627cdf0e10cSrcweir                     url, OUString(), false, OUString(), ucbContent.getCommandEnvironment() ) );
628cdf0e10cSrcweir             const Reference<deployment::XPackageTypeInfo> xPackageType(
629cdf0e10cSrcweir                 xPackage->getPackageType() );
630cdf0e10cSrcweir             OSL_ASSERT( xPackageType.is() );
631cdf0e10cSrcweir             if (xPackageType.is())
632cdf0e10cSrcweir                 mediaType = xPackageType->getMediaType();
633cdf0e10cSrcweir         }
634cdf0e10cSrcweir         catch (lang::IllegalArgumentException & exc) {
635cdf0e10cSrcweir             if (throw_exc)
636cdf0e10cSrcweir                 throw;
637cdf0e10cSrcweir             (void) exc;
638cdf0e10cSrcweir             OSL_ENSURE( 0, ::rtl::OUStringToOString(
639cdf0e10cSrcweir                             exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
640cdf0e10cSrcweir         }
641cdf0e10cSrcweir     }
642cdf0e10cSrcweir     return mediaType;
643cdf0e10cSrcweir }
644cdf0e10cSrcweir 
645cdf0e10cSrcweir //______________________________________________________________________________
646cdf0e10cSrcweir OUString PackageManagerImpl::insertToActivationLayer(
647cdf0e10cSrcweir     Sequence<beans::NamedValue> const & properties,
648cdf0e10cSrcweir     OUString const & mediaType, ::ucbhelper::Content const & sourceContent_,
649cdf0e10cSrcweir     OUString const & title, ActivePackages::Data * dbData )
650cdf0e10cSrcweir {
651cdf0e10cSrcweir     ::ucbhelper::Content sourceContent(sourceContent_);
652cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv(
653cdf0e10cSrcweir         sourceContent.getCommandEnvironment() );
654cdf0e10cSrcweir 
655cdf0e10cSrcweir     String baseDir(m_activePackages_expanded);
656cdf0e10cSrcweir     ::utl::TempFile aTemp(&baseDir, sal_False);
657cdf0e10cSrcweir     OUString tempEntry = aTemp.GetURL();
658cdf0e10cSrcweir     tempEntry = tempEntry.copy(tempEntry.lastIndexOf('/') + 1);
659cdf0e10cSrcweir     OUString destFolder = makeURL( m_activePackages, tempEntry);
660cdf0e10cSrcweir     destFolder += OUSTR("_");
661cdf0e10cSrcweir 
662cdf0e10cSrcweir     // prepare activation folder:
663cdf0e10cSrcweir     ::ucbhelper::Content destFolderContent;
664cdf0e10cSrcweir     create_folder( &destFolderContent, destFolder, xCmdEnv );
665cdf0e10cSrcweir 
666cdf0e10cSrcweir     // copy content into activation temp dir:
667cdf0e10cSrcweir     if (mediaType.matchIgnoreAsciiCaseAsciiL(
668cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM(
669cdf0e10cSrcweir                 "application/vnd.sun.star.package-bundle") ) ||
670cdf0e10cSrcweir         // xxx todo: more sophisticated parsing
671cdf0e10cSrcweir         mediaType.matchIgnoreAsciiCaseAsciiL(
672cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM(
673cdf0e10cSrcweir                 "application/vnd.sun.star.legacy-package-bundle") ))
674cdf0e10cSrcweir     {
675cdf0e10cSrcweir         // inflate content:
676cdf0e10cSrcweir         ::rtl::OUStringBuffer buf;
677cdf0e10cSrcweir         if (!sourceContent.isFolder())
678cdf0e10cSrcweir         {
679cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.zip://") );
680cdf0e10cSrcweir             buf.append( ::rtl::Uri::encode( sourceContent.getURL(),
681cdf0e10cSrcweir                                             rtl_UriCharClassRegName,
682cdf0e10cSrcweir                                             rtl_UriEncodeIgnoreEscapes,
683cdf0e10cSrcweir                                             RTL_TEXTENCODING_UTF8 ) );
684cdf0e10cSrcweir         }
685cdf0e10cSrcweir         else
686cdf0e10cSrcweir         {
687cdf0e10cSrcweir             //Folder. No need to unzip, just copy
688cdf0e10cSrcweir             buf.append(sourceContent.getURL());
689cdf0e10cSrcweir         }
690cdf0e10cSrcweir         buf.append( static_cast<sal_Unicode>('/') );
691cdf0e10cSrcweir         sourceContent = ::ucbhelper::Content(
692cdf0e10cSrcweir             buf.makeStringAndClear(), xCmdEnv );
693cdf0e10cSrcweir     }
694cdf0e10cSrcweir     if (! destFolderContent.transferContent(
695cdf0e10cSrcweir             sourceContent, ::ucbhelper::InsertOperation_COPY,
696cdf0e10cSrcweir             title, NameClash::OVERWRITE ))
697cdf0e10cSrcweir         throw RuntimeException( OUSTR("UCB transferContent() failed!"), 0 );
698cdf0e10cSrcweir 
699cdf0e10cSrcweir 
700cdf0e10cSrcweir     // write to DB:
701cdf0e10cSrcweir     //bundled extensions should only be added by the synchronizeAddedExtensions
702cdf0e10cSrcweir     //functions. Moreover, there is no "temporary folder" for bundled extensions.
703cdf0e10cSrcweir     OSL_ASSERT(!m_context.equals(OUSTR("bundled")));
704cdf0e10cSrcweir     OUString sFolderUrl = makeURLAppendSysPathSegment(destFolderContent.getURL(), title);
705cdf0e10cSrcweir     DescriptionInfoset info =
706cdf0e10cSrcweir         dp_misc::getDescriptionInfoset(sFolderUrl);
707cdf0e10cSrcweir     dbData->temporaryName = tempEntry;
708cdf0e10cSrcweir     dbData->fileName = title;
709cdf0e10cSrcweir     dbData->mediaType = mediaType;
710cdf0e10cSrcweir     dbData->version = info.getVersion();
711cdf0e10cSrcweir 
712cdf0e10cSrcweir     //No write the properties file next to the extension
713cdf0e10cSrcweir     ExtensionProperties props(sFolderUrl, properties, xCmdEnv);
714cdf0e10cSrcweir     props.write();
715cdf0e10cSrcweir     return destFolder;
716cdf0e10cSrcweir }
717cdf0e10cSrcweir 
718cdf0e10cSrcweir //______________________________________________________________________________
719cdf0e10cSrcweir void PackageManagerImpl::insertToActivationLayerDB(
720cdf0e10cSrcweir     OUString const & id, ActivePackages::Data const & dbData )
721cdf0e10cSrcweir {
722cdf0e10cSrcweir     //access to the database must be guarded. See removePackage
723cdf0e10cSrcweir     const ::osl::MutexGuard guard( getMutex() );
724cdf0e10cSrcweir     m_activePackagesDB->put( id, dbData );
725cdf0e10cSrcweir }
726cdf0e10cSrcweir 
727cdf0e10cSrcweir //______________________________________________________________________________
728cdf0e10cSrcweir /* The function returns true if there is an extension with the same id already
729cdf0e10cSrcweir     installed which needs to be uninstalled, before the new extension can be installed.
730cdf0e10cSrcweir */
731cdf0e10cSrcweir bool PackageManagerImpl::isInstalled(
732cdf0e10cSrcweir     Reference<deployment::XPackage> const & package)
733cdf0e10cSrcweir {
734cdf0e10cSrcweir     OUString id(dp_misc::getIdentifier(package));
735cdf0e10cSrcweir     OUString fn(package->getName());
736cdf0e10cSrcweir     bool bInstalled = false;
737cdf0e10cSrcweir     if (m_activePackagesDB->has( id, fn ))
738cdf0e10cSrcweir     {
739cdf0e10cSrcweir         bInstalled = true;
740cdf0e10cSrcweir     }
741cdf0e10cSrcweir     return bInstalled;
742cdf0e10cSrcweir }
743cdf0e10cSrcweir 
744cdf0e10cSrcweir // XPackageManager
745cdf0e10cSrcweir //______________________________________________________________________________
746cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::importExtension(
747cdf0e10cSrcweir     Reference<deployment::XPackage> const & extension,
748cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
749cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
750cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
751cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
752cdf0e10cSrcweir            RuntimeException)
753cdf0e10cSrcweir {
754cdf0e10cSrcweir     return addPackage(extension->getURL(), Sequence<beans::NamedValue>(),
755cdf0e10cSrcweir                       OUString(), xAbortChannel, xCmdEnv_);
756cdf0e10cSrcweir }
757cdf0e10cSrcweir 
758cdf0e10cSrcweir /* The function adds an extension but does not register it!!!
759cdf0e10cSrcweir     It may not do any user interaction. This is done in XExtensionManager::addExtension
760cdf0e10cSrcweir */
761cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::addPackage(
762cdf0e10cSrcweir     OUString const & url,
763cdf0e10cSrcweir     css::uno::Sequence<css::beans::NamedValue> const & properties,
764cdf0e10cSrcweir     OUString const & mediaType_,
765cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
766cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
767cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
768cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
769cdf0e10cSrcweir            RuntimeException)
770cdf0e10cSrcweir {
771cdf0e10cSrcweir     check();
772cdf0e10cSrcweir     if (m_readOnly)
773cdf0e10cSrcweir 	{
774cdf0e10cSrcweir 		OUString message;
775cdf0e10cSrcweir 		if (m_context == OUSTR("shared"))
776cdf0e10cSrcweir 			message = OUSTR("You need write permissions to install a shared extension!");
777cdf0e10cSrcweir 		else
778cdf0e10cSrcweir 			message = OUSTR("You need write permissions to install this extension!");
779cdf0e10cSrcweir         throw deployment::DeploymentException(
780cdf0e10cSrcweir             message, static_cast<OWeakObject *>(this), Any() );
781cdf0e10cSrcweir 	}
782cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
783cdf0e10cSrcweir     if (m_xLogFile.is())
784cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
785cdf0e10cSrcweir     else
786cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
787cdf0e10cSrcweir 
788cdf0e10cSrcweir     try {
789cdf0e10cSrcweir         ::ucbhelper::Content sourceContent;
790cdf0e10cSrcweir         create_ucb_content( &sourceContent, url, xCmdEnv ); // throws exc
791cdf0e10cSrcweir         const OUString title(sourceContent.getPropertyValue(
792cdf0e10cSrcweir                              StrTitle::get() ).get<OUString>() );
793cdf0e10cSrcweir         const OUString title_enc( ::rtl::Uri::encode(
794cdf0e10cSrcweir                                       title, rtl_UriCharClassPchar,
795cdf0e10cSrcweir                                       rtl_UriEncodeIgnoreEscapes,
796cdf0e10cSrcweir                                       RTL_TEXTENCODING_UTF8 ) );
797cdf0e10cSrcweir         OUString destFolder;
798cdf0e10cSrcweir 
799cdf0e10cSrcweir         OUString mediaType(mediaType_);
800cdf0e10cSrcweir         if (mediaType.getLength() == 0)
801cdf0e10cSrcweir             mediaType = detectMediaType( sourceContent );
802cdf0e10cSrcweir 
803cdf0e10cSrcweir         Reference<deployment::XPackage> xPackage;
804cdf0e10cSrcweir         // copy file:
805cdf0e10cSrcweir         progressUpdate(
806cdf0e10cSrcweir             getResourceString(RID_STR_COPYING_PACKAGE) + title, xCmdEnv );
807cdf0e10cSrcweir         if (m_activePackages.getLength() == 0)
808cdf0e10cSrcweir         {
809cdf0e10cSrcweir             ::ucbhelper::Content docFolderContent;
810cdf0e10cSrcweir             create_folder( &docFolderContent, m_context, xCmdEnv );
811cdf0e10cSrcweir             // copy into document, first:
812cdf0e10cSrcweir             if (! docFolderContent.transferContent(
813cdf0e10cSrcweir                     sourceContent, ::ucbhelper::InsertOperation_COPY,
814cdf0e10cSrcweir                     OUString(),
815cdf0e10cSrcweir                     NameClash::ASK /* xxx todo: ASK not needed? */))
816cdf0e10cSrcweir                 throw RuntimeException(
817cdf0e10cSrcweir                     OUSTR("UCB transferContent() failed!"), 0 );
818cdf0e10cSrcweir             // set media-type:
819cdf0e10cSrcweir             ::ucbhelper::Content docContent(
820cdf0e10cSrcweir                 makeURL( m_context, title_enc ), xCmdEnv );
821cdf0e10cSrcweir                 //TODO #i73136#: using title instead of id can lead to
822cdf0e10cSrcweir                 // clashes, but the whole m_activePackages.getLength()==0
823cdf0e10cSrcweir                 // case (i.e., document-relative deployment) currently does
824cdf0e10cSrcweir                 // not work, anyway.
825cdf0e10cSrcweir             docContent.setPropertyValue(
826cdf0e10cSrcweir                 OUSTR("MediaType"), Any(mediaType) );
827cdf0e10cSrcweir 
828cdf0e10cSrcweir             // xxx todo: obsolete in the future
829cdf0e10cSrcweir             try {
830cdf0e10cSrcweir                 docFolderContent.executeCommand( OUSTR("flush"), Any() );
831cdf0e10cSrcweir             }
832cdf0e10cSrcweir             catch (UnsupportedCommandException &) {
833cdf0e10cSrcweir             }
834cdf0e10cSrcweir         }
835cdf0e10cSrcweir         ActivePackages::Data dbData;
836cdf0e10cSrcweir         destFolder = insertToActivationLayer(
837cdf0e10cSrcweir             properties, mediaType, sourceContent, title, &dbData );
838cdf0e10cSrcweir 
839cdf0e10cSrcweir 
840cdf0e10cSrcweir         // bind activation package:
841cdf0e10cSrcweir         //Because every shared/user extension will be unpacked in a folder,
842cdf0e10cSrcweir         //which was created with a unique name we will always have two different
843cdf0e10cSrcweir         //XPackage objects, even if the second extension is the same.
844cdf0e10cSrcweir         //Therefore bindPackage does not need a guard here.
845cdf0e10cSrcweir         xPackage = m_xRegistry->bindPackage(
846cdf0e10cSrcweir             makeURL( destFolder, title_enc ), mediaType, false, OUString(), xCmdEnv );
847cdf0e10cSrcweir 
848cdf0e10cSrcweir         OSL_ASSERT( xPackage.is() );
849cdf0e10cSrcweir         if (xPackage.is())
850cdf0e10cSrcweir         {
851cdf0e10cSrcweir             bool install = false;
852cdf0e10cSrcweir             try
853cdf0e10cSrcweir             {
854cdf0e10cSrcweir                 OUString const id = dp_misc::getIdentifier( xPackage );
855cdf0e10cSrcweir 
856cdf0e10cSrcweir                 ::osl::MutexGuard g(m_addMutex);
857cdf0e10cSrcweir                 if (isInstalled(xPackage))
858cdf0e10cSrcweir                 {
859cdf0e10cSrcweir                     //Do not guard the complete function with the getMutex
860cdf0e10cSrcweir                     removePackage(id, xPackage->getName(), xAbortChannel,
861cdf0e10cSrcweir                                   xCmdEnv);
862cdf0e10cSrcweir                 }
863cdf0e10cSrcweir                 install = true;
864cdf0e10cSrcweir                 insertToActivationLayerDB(id, dbData);
865cdf0e10cSrcweir             }
866cdf0e10cSrcweir             catch (...)
867cdf0e10cSrcweir             {
868cdf0e10cSrcweir                 deletePackageFromCache( xPackage, destFolder );
869cdf0e10cSrcweir                 throw;
870cdf0e10cSrcweir             }
871cdf0e10cSrcweir             if (!install)
872cdf0e10cSrcweir             {
873cdf0e10cSrcweir                 deletePackageFromCache( xPackage, destFolder );
874cdf0e10cSrcweir             }
875cdf0e10cSrcweir             //ToDo: We should notify only if the extension is registered
876cdf0e10cSrcweir             fireModified();
877cdf0e10cSrcweir         }
878cdf0e10cSrcweir         return xPackage;
879cdf0e10cSrcweir     }
880cdf0e10cSrcweir     catch (RuntimeException &) {
881cdf0e10cSrcweir         throw;
882cdf0e10cSrcweir     }
883cdf0e10cSrcweir     catch (CommandFailedException & exc) {
884cdf0e10cSrcweir         logIntern( Any(exc) );
885cdf0e10cSrcweir         throw;
886cdf0e10cSrcweir     }
887cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
888cdf0e10cSrcweir         logIntern( Any(exc) );
889cdf0e10cSrcweir         throw;
890cdf0e10cSrcweir     }
891cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
892cdf0e10cSrcweir         logIntern( Any(exc) );
893cdf0e10cSrcweir         throw;
894cdf0e10cSrcweir     }
895cdf0e10cSrcweir     catch (Exception &) {
896cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
897cdf0e10cSrcweir         logIntern( exc );
898cdf0e10cSrcweir         throw deployment::DeploymentException(
899cdf0e10cSrcweir             getResourceString(RID_STR_ERROR_WHILE_ADDING) + url,
900cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
901cdf0e10cSrcweir     }
902cdf0e10cSrcweir }
903cdf0e10cSrcweir void PackageManagerImpl::deletePackageFromCache(
904cdf0e10cSrcweir     Reference<deployment::XPackage> const & xPackage,
905cdf0e10cSrcweir     OUString const & destFolder)
906cdf0e10cSrcweir {
907cdf0e10cSrcweir     try_dispose( xPackage );
908cdf0e10cSrcweir 
909cdf0e10cSrcweir 	//we remove the package from the uno cache
910cdf0e10cSrcweir 	//no service from the package may be loaded at this time!!!
911cdf0e10cSrcweir 	erase_path( destFolder, Reference<XCommandEnvironment>(),
912cdf0e10cSrcweir         false /* no throw: ignore errors */ );
913cdf0e10cSrcweir 	//rm last character '_'
914cdf0e10cSrcweir 	OUString url = destFolder.copy(0, destFolder.getLength() - 1);
915cdf0e10cSrcweir 	erase_path( url, Reference<XCommandEnvironment>(),
916cdf0e10cSrcweir         false /* no throw: ignore errors */ );
917cdf0e10cSrcweir 
918cdf0e10cSrcweir }
919cdf0e10cSrcweir //______________________________________________________________________________
920cdf0e10cSrcweir void PackageManagerImpl::removePackage(
921cdf0e10cSrcweir     OUString const & id, ::rtl::OUString const & fileName,
922cdf0e10cSrcweir     Reference<task::XAbortChannel> const & /*xAbortChannel*/,
923cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
924cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
925cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
926cdf0e10cSrcweir            RuntimeException)
927cdf0e10cSrcweir {
928cdf0e10cSrcweir     check();
929cdf0e10cSrcweir 
930cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
931cdf0e10cSrcweir     if (m_xLogFile.is())
932cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
933cdf0e10cSrcweir     else
934cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
935cdf0e10cSrcweir 
936cdf0e10cSrcweir     try {
937cdf0e10cSrcweir         Reference<deployment::XPackage> xPackage;
938cdf0e10cSrcweir         {
939cdf0e10cSrcweir             const ::osl::MutexGuard guard(getMutex());
940cdf0e10cSrcweir             //Check if this extension exist and throw an IllegalArgumentException
941cdf0e10cSrcweir             //if it does not
942cdf0e10cSrcweir             //If the files of the extension are already removed, or there is a
943cdf0e10cSrcweir             //different extension at the same place, for example after updating the
944cdf0e10cSrcweir             //extension, then the returned object is that which uses the database data.
945cdf0e10cSrcweir             xPackage = getDeployedPackage_(id, fileName, xCmdEnv );
946cdf0e10cSrcweir 
947cdf0e10cSrcweir 
948cdf0e10cSrcweir             //Because the extension is only removed the next time the extension
949cdf0e10cSrcweir             //manager runs after restarting OOo, we need to indicate that a
950cdf0e10cSrcweir             //shared extension was "deleted". When a user starts OOo, then it
951cdf0e10cSrcweir             //will check if something changed in the shared repository. Based on
952cdf0e10cSrcweir             //the flag file it will then recognize, that the extension was
953cdf0e10cSrcweir             //deleted and can then update the extnesion database of the shared
954cdf0e10cSrcweir             //extensions in the user installation.
955cdf0e10cSrcweir             if ( xPackage.is() && !m_readOnly && !xPackage->isRemoved() && m_context.equals(OUSTR("shared")))
956cdf0e10cSrcweir             {
957cdf0e10cSrcweir                 ActivePackages::Data val;
958cdf0e10cSrcweir                 m_activePackagesDB->get( & val, id, fileName);
959cdf0e10cSrcweir                 OSL_ASSERT(val.temporaryName.getLength());
960cdf0e10cSrcweir                 OUString url(makeURL(m_activePackages_expanded,
961cdf0e10cSrcweir                                      val.temporaryName + OUSTR("removed")));
962cdf0e10cSrcweir                 ::ucbhelper::Content contentRemoved(url, xCmdEnv );
963cdf0e10cSrcweir                 OUString aUserName;
964cdf0e10cSrcweir                 ::osl::Security aSecurity;
965cdf0e10cSrcweir                 aSecurity.getUserName( aUserName );
966cdf0e10cSrcweir 
967cdf0e10cSrcweir                 ::rtl::OString stamp = ::rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8);
968cdf0e10cSrcweir                 Reference<css::io::XInputStream> xData(
969cdf0e10cSrcweir                     ::xmlscript::createInputStream(
970cdf0e10cSrcweir                         ::rtl::ByteSequence(
971cdf0e10cSrcweir                             reinterpret_cast<sal_Int8 const *>(stamp.getStr()),
972cdf0e10cSrcweir                             stamp.getLength() ) ) );
973cdf0e10cSrcweir                 contentRemoved.writeStream( xData, true /* replace existing */ );
974cdf0e10cSrcweir             }
975cdf0e10cSrcweir             m_activePackagesDB->erase( id, fileName ); // to be removed upon next start
976cdf0e10cSrcweir             //remove any cached data hold by the backend
977cdf0e10cSrcweir             m_xRegistry->packageRemoved(xPackage->getURL(), xPackage->getPackageType()->getMediaType());
978cdf0e10cSrcweir         }
979cdf0e10cSrcweir         try_dispose( xPackage );
980cdf0e10cSrcweir 
981cdf0e10cSrcweir         fireModified();
982cdf0e10cSrcweir     }
983cdf0e10cSrcweir     catch (RuntimeException &) {
984cdf0e10cSrcweir         throw;
985cdf0e10cSrcweir     }
986cdf0e10cSrcweir     catch (lang::IllegalArgumentException &) {
987cdf0e10cSrcweir         throw;
988cdf0e10cSrcweir     }
989cdf0e10cSrcweir     catch (CommandFailedException & exc) {
990cdf0e10cSrcweir         logIntern( Any(exc) );
991cdf0e10cSrcweir         throw;
992cdf0e10cSrcweir     }
993cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
994cdf0e10cSrcweir         logIntern( Any(exc) );
995cdf0e10cSrcweir         throw;
996cdf0e10cSrcweir     }
997cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
998cdf0e10cSrcweir         logIntern( Any(exc) );
999cdf0e10cSrcweir         throw;
1000cdf0e10cSrcweir     }
1001cdf0e10cSrcweir     catch (Exception &) {
1002cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1003cdf0e10cSrcweir         logIntern( exc );
1004cdf0e10cSrcweir         throw deployment::DeploymentException(
1005cdf0e10cSrcweir             getResourceString(RID_STR_ERROR_WHILE_REMOVING) + id,
1006cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1007cdf0e10cSrcweir     }
1008cdf0e10cSrcweir }
1009cdf0e10cSrcweir 
1010cdf0e10cSrcweir //______________________________________________________________________________
1011cdf0e10cSrcweir OUString PackageManagerImpl::getDeployPath( ActivePackages::Data const & data )
1012cdf0e10cSrcweir {
1013cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
1014cdf0e10cSrcweir     buf.append( data.temporaryName );
1015cdf0e10cSrcweir     //The bundled extensions are not contained in an additional folder
1016cdf0e10cSrcweir     //with a unique name. data.temporaryName contains already the
1017cdf0e10cSrcweir     //UTF8 encoded folder name. See PackageManagerImpl::synchronize
1018cdf0e10cSrcweir     if (!m_context.equals(OUSTR("bundled"))
1019cdf0e10cSrcweir         && !m_context.equals(OUSTR("bundled_prereg")))
1020cdf0e10cSrcweir     {
1021cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("_/") );
1022cdf0e10cSrcweir         buf.append( ::rtl::Uri::encode( data.fileName, rtl_UriCharClassPchar,
1023cdf0e10cSrcweir                                     rtl_UriEncodeIgnoreEscapes,
1024cdf0e10cSrcweir                                     RTL_TEXTENCODING_UTF8 ) );
1025cdf0e10cSrcweir     }
1026cdf0e10cSrcweir     return makeURL( m_activePackages, buf.makeStringAndClear() );
1027cdf0e10cSrcweir }
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir //______________________________________________________________________________
1030cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::getDeployedPackage_(
1031cdf0e10cSrcweir     OUString const & id, OUString const & fileName,
1032cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
1033cdf0e10cSrcweir {
1034cdf0e10cSrcweir     ActivePackages::Data val;
1035cdf0e10cSrcweir     if (m_activePackagesDB->get( &val, id, fileName ))
1036cdf0e10cSrcweir     {
1037cdf0e10cSrcweir         return getDeployedPackage_( id, val, xCmdEnv, false );
1038cdf0e10cSrcweir     }
1039cdf0e10cSrcweir     throw lang::IllegalArgumentException(
1040cdf0e10cSrcweir         getResourceString(RID_STR_NO_SUCH_PACKAGE) + id,
1041cdf0e10cSrcweir         static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
1042cdf0e10cSrcweir }
1043cdf0e10cSrcweir 
1044cdf0e10cSrcweir //______________________________________________________________________________
1045cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::getDeployedPackage_(
1046cdf0e10cSrcweir     OUString const & id, ActivePackages::Data const & data,
1047cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv, bool ignoreAlienPlatforms )
1048cdf0e10cSrcweir {
1049cdf0e10cSrcweir     if (ignoreAlienPlatforms)
1050cdf0e10cSrcweir     {
1051cdf0e10cSrcweir         String type, subType;
1052cdf0e10cSrcweir         INetContentTypeParameterList params;
1053cdf0e10cSrcweir         if (INetContentTypes::parse( data.mediaType, type, subType, &params ))
1054cdf0e10cSrcweir         {
1055cdf0e10cSrcweir             INetContentTypeParameter const * param = params.find(
1056cdf0e10cSrcweir                 ByteString("platform") );
1057cdf0e10cSrcweir             if (param != 0 && !platform_fits( param->m_sValue ))
1058cdf0e10cSrcweir                 throw lang::IllegalArgumentException(
1059cdf0e10cSrcweir                     getResourceString(RID_STR_NO_SUCH_PACKAGE) + id,
1060cdf0e10cSrcweir                     static_cast<OWeakObject *>(this),
1061cdf0e10cSrcweir                     static_cast<sal_Int16>(-1) );
1062cdf0e10cSrcweir         }
1063cdf0e10cSrcweir     }
1064cdf0e10cSrcweir     Reference<deployment::XPackage> xExtension;
1065cdf0e10cSrcweir     try
1066cdf0e10cSrcweir     {
1067cdf0e10cSrcweir         //Ignore extensions where XPackage::checkPrerequisites failed.
1068cdf0e10cSrcweir         //They must not be usable for this user.
1069cdf0e10cSrcweir         if (data.failedPrerequisites.equals(OUSTR("0")))
1070cdf0e10cSrcweir         {
1071cdf0e10cSrcweir             xExtension = m_xRegistry->bindPackage(
1072cdf0e10cSrcweir                 getDeployPath( data ), data.mediaType, false, OUString(), xCmdEnv );
1073cdf0e10cSrcweir         }
1074cdf0e10cSrcweir     }
1075cdf0e10cSrcweir     catch (deployment::InvalidRemovedParameterException& e)
1076cdf0e10cSrcweir     {
1077cdf0e10cSrcweir         xExtension = e.Extension;
1078cdf0e10cSrcweir     }
1079cdf0e10cSrcweir     return xExtension;
1080cdf0e10cSrcweir }
1081cdf0e10cSrcweir 
1082cdf0e10cSrcweir //______________________________________________________________________________
1083cdf0e10cSrcweir Sequence< Reference<deployment::XPackage> >
1084cdf0e10cSrcweir PackageManagerImpl::getDeployedPackages_(
1085cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
1086cdf0e10cSrcweir {
1087cdf0e10cSrcweir     ::std::vector< Reference<deployment::XPackage> > packages;
1088cdf0e10cSrcweir     ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1089cdf0e10cSrcweir     ActivePackages::Entries::const_iterator iPos( id2temp.begin() );
1090cdf0e10cSrcweir     ActivePackages::Entries::const_iterator const iEnd( id2temp.end() );
1091cdf0e10cSrcweir     for ( ; iPos != iEnd; ++iPos )
1092cdf0e10cSrcweir     {
1093cdf0e10cSrcweir         if (! iPos->second.failedPrerequisites.equals(OUSTR("0")))
1094cdf0e10cSrcweir             continue;
1095cdf0e10cSrcweir         try {
1096cdf0e10cSrcweir             packages.push_back(
1097cdf0e10cSrcweir                 getDeployedPackage_(
1098cdf0e10cSrcweir                     iPos->first, iPos->second, xCmdEnv,
1099cdf0e10cSrcweir                     true /* xxx todo: think of GUI:
1100cdf0e10cSrcweir                             ignore other platforms than the current one */ ) );
1101cdf0e10cSrcweir         }
1102cdf0e10cSrcweir         catch (lang::IllegalArgumentException & exc) {
1103cdf0e10cSrcweir             // ignore
1104cdf0e10cSrcweir             (void) exc; // avoid warnings
1105cdf0e10cSrcweir             OSL_ENSURE( 0, ::rtl::OUStringToOString(
1106cdf0e10cSrcweir                             exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1107cdf0e10cSrcweir         }
1108cdf0e10cSrcweir         catch (deployment::DeploymentException& exc) {
1109cdf0e10cSrcweir             // ignore
1110cdf0e10cSrcweir             (void) exc; // avoid warnings
1111cdf0e10cSrcweir             OSL_ENSURE( 0, ::rtl::OUStringToOString(
1112cdf0e10cSrcweir                             exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1113cdf0e10cSrcweir         }
1114cdf0e10cSrcweir     }
1115cdf0e10cSrcweir     return comphelper::containerToSequence(packages);
1116cdf0e10cSrcweir }
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir //______________________________________________________________________________
1119cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::getDeployedPackage(
1120cdf0e10cSrcweir     OUString const & id, ::rtl::OUString const & fileName,
1121cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
1122cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
1123cdf0e10cSrcweir            lang::IllegalArgumentException, RuntimeException)
1124cdf0e10cSrcweir {
1125cdf0e10cSrcweir     check();
1126cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
1127cdf0e10cSrcweir     if (m_xLogFile.is())
1128cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
1129cdf0e10cSrcweir     else
1130cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
1131cdf0e10cSrcweir 
1132cdf0e10cSrcweir     try {
1133cdf0e10cSrcweir         const ::osl::MutexGuard guard( getMutex() );
1134cdf0e10cSrcweir         return getDeployedPackage_( id, fileName, xCmdEnv );
1135cdf0e10cSrcweir     }
1136cdf0e10cSrcweir     catch (RuntimeException &) {
1137cdf0e10cSrcweir         throw;
1138cdf0e10cSrcweir     }
1139cdf0e10cSrcweir     catch (CommandFailedException & exc) {
1140cdf0e10cSrcweir         logIntern( Any(exc) );
1141cdf0e10cSrcweir         throw;
1142cdf0e10cSrcweir     }
1143cdf0e10cSrcweir     catch (lang::IllegalArgumentException & exc) {
1144cdf0e10cSrcweir         logIntern( Any(exc) );
1145cdf0e10cSrcweir         throw;
1146cdf0e10cSrcweir     }
1147cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
1148cdf0e10cSrcweir         logIntern( Any(exc) );
1149cdf0e10cSrcweir         throw;
1150cdf0e10cSrcweir     }
1151cdf0e10cSrcweir     catch (Exception &) {
1152cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1153cdf0e10cSrcweir         logIntern( exc );
1154cdf0e10cSrcweir         throw deployment::DeploymentException(
1155cdf0e10cSrcweir             // ought never occur...
1156cdf0e10cSrcweir             OUSTR("error while accessing deployed package: ") + id,
1157cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1158cdf0e10cSrcweir     }
1159cdf0e10cSrcweir }
1160cdf0e10cSrcweir 
1161cdf0e10cSrcweir //______________________________________________________________________________
1162cdf0e10cSrcweir Sequence< Reference<deployment::XPackage> >
1163cdf0e10cSrcweir PackageManagerImpl::getDeployedPackages(
1164cdf0e10cSrcweir     Reference<task::XAbortChannel> const &,
1165cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
1166cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
1167cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
1168cdf0e10cSrcweir            RuntimeException)
1169cdf0e10cSrcweir {
1170cdf0e10cSrcweir     check();
1171cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
1172cdf0e10cSrcweir     if (m_xLogFile.is())
1173cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
1174cdf0e10cSrcweir     else
1175cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir     try {
1178cdf0e10cSrcweir         const ::osl::MutexGuard guard( getMutex() );
1179cdf0e10cSrcweir         return getDeployedPackages_( xCmdEnv );
1180cdf0e10cSrcweir     }
1181cdf0e10cSrcweir     catch (RuntimeException &) {
1182cdf0e10cSrcweir         throw;
1183cdf0e10cSrcweir     }
1184cdf0e10cSrcweir     catch (CommandFailedException & exc) {
1185cdf0e10cSrcweir         logIntern( Any(exc) );
1186cdf0e10cSrcweir         throw;
1187cdf0e10cSrcweir     }
1188cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
1189cdf0e10cSrcweir         logIntern( Any(exc) );
1190cdf0e10cSrcweir         throw;
1191cdf0e10cSrcweir     }
1192cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
1193cdf0e10cSrcweir         logIntern( Any(exc) );
1194cdf0e10cSrcweir         throw;
1195cdf0e10cSrcweir     }
1196cdf0e10cSrcweir     catch (Exception &) {
1197cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1198cdf0e10cSrcweir         logIntern( exc );
1199cdf0e10cSrcweir         throw deployment::DeploymentException(
1200cdf0e10cSrcweir             // ought never occur...
1201cdf0e10cSrcweir             OUSTR("error while getting all deployed packages: ") + m_context,
1202cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1203cdf0e10cSrcweir     }
1204cdf0e10cSrcweir }
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir //______________________________________________________________________________
1207cdf0e10cSrcweir 
1208cdf0e10cSrcweir 
1209cdf0e10cSrcweir //ToDo: the function must not call registerPackage, do this in
1210cdf0e10cSrcweir //XExtensionManager.reinstallDeployedExtensions
1211cdf0e10cSrcweir void PackageManagerImpl::reinstallDeployedPackages(
1212cdf0e10cSrcweir     Reference<task::XAbortChannel> const &  /*xAbortChannel*/,
1213cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
1214cdf0e10cSrcweir     throw (deployment::DeploymentException,
1215cdf0e10cSrcweir            CommandFailedException, CommandAbortedException,
1216cdf0e10cSrcweir            lang::IllegalArgumentException, RuntimeException)
1217cdf0e10cSrcweir {
1218cdf0e10cSrcweir     check();
1219cdf0e10cSrcweir     if (office_is_running())
1220cdf0e10cSrcweir         throw RuntimeException(
1221cdf0e10cSrcweir             OUSTR("You must close any running Office process before "
1222cdf0e10cSrcweir                   "reinstalling packages!"), static_cast<OWeakObject *>(this) );
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
1225cdf0e10cSrcweir     if (m_xLogFile.is())
1226cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
1227cdf0e10cSrcweir     else
1228cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir     try {
1231cdf0e10cSrcweir         ProgressLevel progress(
1232cdf0e10cSrcweir             xCmdEnv, OUSTR("Reinstalling all deployed packages...") );
1233cdf0e10cSrcweir 
1234cdf0e10cSrcweir         try_dispose( m_xRegistry );
1235cdf0e10cSrcweir         m_xRegistry.clear();
1236cdf0e10cSrcweir         if (m_registryCache.getLength() > 0)
1237cdf0e10cSrcweir             erase_path( m_registryCache, xCmdEnv );
1238cdf0e10cSrcweir         initRegistryBackends();
1239cdf0e10cSrcweir         Reference<util::XUpdatable> xUpdatable( m_xRegistry, UNO_QUERY );
1240cdf0e10cSrcweir         if (xUpdatable.is())
1241cdf0e10cSrcweir             xUpdatable->update();
1242cdf0e10cSrcweir 
1243cdf0e10cSrcweir         //registering is done by the ExtensionManager service.
1244cdf0e10cSrcweir     }
1245cdf0e10cSrcweir     catch (RuntimeException &) {
1246cdf0e10cSrcweir         throw;
1247cdf0e10cSrcweir     }
1248cdf0e10cSrcweir     catch (CommandFailedException & exc) {
1249cdf0e10cSrcweir         logIntern( Any(exc) );
1250cdf0e10cSrcweir         throw;
1251cdf0e10cSrcweir     }
1252cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
1253cdf0e10cSrcweir         logIntern( Any(exc) );
1254cdf0e10cSrcweir         throw;
1255cdf0e10cSrcweir     }
1256cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
1257cdf0e10cSrcweir         logIntern( Any(exc) );
1258cdf0e10cSrcweir         throw;
1259cdf0e10cSrcweir     }
1260cdf0e10cSrcweir     catch (Exception &) {
1261cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1262cdf0e10cSrcweir         logIntern( exc );
1263cdf0e10cSrcweir         throw deployment::DeploymentException(
1264cdf0e10cSrcweir             OUSTR("Error while reinstalling all previously deployed "
1265cdf0e10cSrcweir                   "packages of context ") + m_context,
1266cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1267cdf0e10cSrcweir     }
1268cdf0e10cSrcweir }
1269cdf0e10cSrcweir 
1270cdf0e10cSrcweir 
1271cdf0e10cSrcweir ::sal_Bool SAL_CALL PackageManagerImpl::isReadOnly(  )
1272cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException)
1273cdf0e10cSrcweir {
1274cdf0e10cSrcweir     return m_readOnly;
1275cdf0e10cSrcweir }
1276cdf0e10cSrcweir bool PackageManagerImpl::synchronizeRemovedExtensions(
1277cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1278cdf0e10cSrcweir     Reference<css::ucb::XCommandEnvironment> const & xCmdEnv)
1279cdf0e10cSrcweir {
1280cdf0e10cSrcweir 
1281cdf0e10cSrcweir     //find all which are in the extension data base but which
1282cdf0e10cSrcweir     //are removed already.
1283cdf0e10cSrcweir     OSL_ASSERT(!m_context.equals(OUSTR("user")));
1284cdf0e10cSrcweir     bool bModified = false;
1285cdf0e10cSrcweir     ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1286cdf0e10cSrcweir 
1287cdf0e10cSrcweir     typedef ActivePackages::Entries::const_iterator ITActive;
1288cdf0e10cSrcweir     bool bShared = m_context.equals(OUSTR("shared"));
1289cdf0e10cSrcweir 
1290cdf0e10cSrcweir     for (ITActive i = id2temp.begin(); i != id2temp.end(); i++)
1291cdf0e10cSrcweir     {
1292cdf0e10cSrcweir         try
1293cdf0e10cSrcweir         {
1294cdf0e10cSrcweir             //Get the URL to the extensions folder, first make the url for the
1295cdf0e10cSrcweir             //shared repository including the temporary name
1296cdf0e10cSrcweir             OUString url = makeURL(m_activePackages, i->second.temporaryName);
1297cdf0e10cSrcweir             if (bShared)
1298cdf0e10cSrcweir                 url = makeURLAppendSysPathSegment( url + OUSTR("_"), i->second.fileName);
1299cdf0e10cSrcweir 
1300cdf0e10cSrcweir             bool bRemoved = false;
1301cdf0e10cSrcweir             //Check if the URL to the extension is still the same
1302cdf0e10cSrcweir             ::ucbhelper::Content contentExtension;
1303cdf0e10cSrcweir 
1304cdf0e10cSrcweir             if (!create_ucb_content(
1305cdf0e10cSrcweir                     &contentExtension, url,
1306cdf0e10cSrcweir                     Reference<XCommandEnvironment>(), false))
1307cdf0e10cSrcweir             {
1308cdf0e10cSrcweir                 bRemoved = true;
1309cdf0e10cSrcweir             }
1310cdf0e10cSrcweir 
1311cdf0e10cSrcweir             //The folder is in the extension database, but it can still be deleted.
1312cdf0e10cSrcweir             //look for the xxx.tmpremoved file
1313cdf0e10cSrcweir             //There can also be the case that a different extension was installed
1314cdf0e10cSrcweir             //in a "temp" folder with name that is already used.
1315cdf0e10cSrcweir             if (!bRemoved && bShared)
1316cdf0e10cSrcweir             {
1317cdf0e10cSrcweir                 ::ucbhelper::Content contentRemoved;
1318cdf0e10cSrcweir 
1319cdf0e10cSrcweir                 if (create_ucb_content(
1320cdf0e10cSrcweir                         &contentRemoved,
1321cdf0e10cSrcweir                         m_activePackages_expanded + OUSTR("/") +
1322cdf0e10cSrcweir                         i->second.temporaryName + OUSTR("removed"),
1323cdf0e10cSrcweir                         Reference<XCommandEnvironment>(), false))
1324cdf0e10cSrcweir                 {
1325cdf0e10cSrcweir                     bRemoved = true;
1326cdf0e10cSrcweir                 }
1327cdf0e10cSrcweir             }
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir             if (!bRemoved)
1330cdf0e10cSrcweir             {
1331cdf0e10cSrcweir                 //There may be another extensions at the same place
1332cdf0e10cSrcweir                 dp_misc::DescriptionInfoset infoset =
1333cdf0e10cSrcweir                     dp_misc::getDescriptionInfoset(url);
1334cdf0e10cSrcweir                 OSL_ENSURE(infoset.hasDescription(),
1335cdf0e10cSrcweir                            "Extension Manager: bundled and shared extensions "
1336cdf0e10cSrcweir                            "must have an identifer and a version");
1337cdf0e10cSrcweir                 if (infoset.hasDescription() &&
1338cdf0e10cSrcweir                     infoset.getIdentifier() &&
1339cdf0e10cSrcweir                     (! i->first.equals(*(infoset.getIdentifier()))
1340cdf0e10cSrcweir                      || ! i->second.version.equals(infoset.getVersion())))
1341cdf0e10cSrcweir                 {
1342cdf0e10cSrcweir                     bRemoved = true;
1343cdf0e10cSrcweir                 }
1344cdf0e10cSrcweir 
1345cdf0e10cSrcweir             }
1346cdf0e10cSrcweir             if (bRemoved)
1347cdf0e10cSrcweir             {
1348cdf0e10cSrcweir                 Reference<deployment::XPackage> xPackage = m_xRegistry->bindPackage(
1349cdf0e10cSrcweir                     url, i->second.mediaType, true, i->first, xCmdEnv );
1350cdf0e10cSrcweir                 OSL_ASSERT(xPackage.is()); //Even if the files are removed, we must get the object.
1351cdf0e10cSrcweir                 xPackage->revokePackage(xAbortChannel, xCmdEnv);
1352cdf0e10cSrcweir                 removePackage(xPackage->getIdentifier().Value, xPackage->getName(),
1353cdf0e10cSrcweir                               xAbortChannel, xCmdEnv);
1354cdf0e10cSrcweir                 bModified |= true;
1355cdf0e10cSrcweir             }
1356cdf0e10cSrcweir         }
1357cdf0e10cSrcweir         catch( uno::Exception & )
1358cdf0e10cSrcweir         {
1359cdf0e10cSrcweir             OSL_ASSERT(0);
1360cdf0e10cSrcweir         }
1361cdf0e10cSrcweir     }
1362cdf0e10cSrcweir     return bModified;
1363cdf0e10cSrcweir }
1364cdf0e10cSrcweir 
1365cdf0e10cSrcweir 
1366cdf0e10cSrcweir bool PackageManagerImpl::synchronizeAddedExtensions(
1367cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1368cdf0e10cSrcweir     Reference<css::ucb::XCommandEnvironment> const & xCmdEnv)
1369cdf0e10cSrcweir {
1370cdf0e10cSrcweir     bool bModified = false;
1371cdf0e10cSrcweir     OSL_ASSERT(!m_context.equals(OUSTR("user")));
1372cdf0e10cSrcweir 
1373cdf0e10cSrcweir     ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1374cdf0e10cSrcweir     //check if the folder exist at all. The shared extension folder
1375cdf0e10cSrcweir     //may not exist for a normal user.
1376cdf0e10cSrcweir     if (!create_ucb_content(
1377cdf0e10cSrcweir             NULL, m_activePackages_expanded, Reference<css::ucb::XCommandEnvironment>(), false))
1378cdf0e10cSrcweir         return bModified;
1379cdf0e10cSrcweir     ::ucbhelper::Content tempFolder(
1380cdf0e10cSrcweir         m_activePackages_expanded, xCmdEnv );
1381cdf0e10cSrcweir 
1382cdf0e10cSrcweir     Reference<sdbc::XResultSet> xResultSet(
1383cdf0e10cSrcweir         tempFolder.createCursor(
1384cdf0e10cSrcweir             Sequence<OUString>( &StrTitle::get(), 1 ),
1385cdf0e10cSrcweir             ::ucbhelper::INCLUDE_FOLDERS_ONLY ) );
1386cdf0e10cSrcweir 
1387cdf0e10cSrcweir     while (xResultSet->next())
1388cdf0e10cSrcweir     {
1389cdf0e10cSrcweir         try
1390cdf0e10cSrcweir         {
1391cdf0e10cSrcweir             OUString title(
1392cdf0e10cSrcweir                 Reference<sdbc::XRow>(
1393cdf0e10cSrcweir                     xResultSet, UNO_QUERY_THROW )->getString(
1394cdf0e10cSrcweir                         1 /* Title */ ) );
1395cdf0e10cSrcweir             //The temporary folders of user and shared have an '_' at then end.
1396cdf0e10cSrcweir             //But the name in ActivePackages.temporaryName is saved without.
1397cdf0e10cSrcweir             OUString title2 = title;
1398cdf0e10cSrcweir             bool bShared = m_context.equals(OUSTR("shared"));
1399cdf0e10cSrcweir             if (bShared)
1400cdf0e10cSrcweir             {
1401cdf0e10cSrcweir                 OSL_ASSERT(title2[title2.getLength() -1] == '_');
1402cdf0e10cSrcweir                 title2 = title2.copy(0, title2.getLength() -1);
1403cdf0e10cSrcweir             }
1404cdf0e10cSrcweir             OUString titleEncoded =  ::rtl::Uri::encode(
1405cdf0e10cSrcweir                 title2, rtl_UriCharClassPchar,
1406cdf0e10cSrcweir                 rtl_UriEncodeIgnoreEscapes,
1407cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8);
1408cdf0e10cSrcweir 
1409cdf0e10cSrcweir             //It it sufficient to check for the folder name, because when the administor
1410cdf0e10cSrcweir             //installed the extension it was already checked if there is one with the
1411cdf0e10cSrcweir             //same identifier.
1412cdf0e10cSrcweir             const MatchTempDir match(titleEncoded);
1413cdf0e10cSrcweir             if (::std::find_if( id2temp.begin(), id2temp.end(), match ) ==
1414cdf0e10cSrcweir                 id2temp.end())
1415cdf0e10cSrcweir             {
1416cdf0e10cSrcweir 
1417cdf0e10cSrcweir                 // The folder was not found in the data base, so it must be
1418cdf0e10cSrcweir                 // an added extension
1419cdf0e10cSrcweir                 OUString url(m_activePackages_expanded + OUSTR("/") + titleEncoded);
1420cdf0e10cSrcweir                 OUString sExtFolder;
1421cdf0e10cSrcweir                 if (bShared) //that is, shared
1422cdf0e10cSrcweir                 {
1423cdf0e10cSrcweir                     //Check if the extension was not "deleted" already which is indicated
1424cdf0e10cSrcweir                     //by a xxx.tmpremoved file
1425cdf0e10cSrcweir                     ::ucbhelper::Content contentRemoved;
1426cdf0e10cSrcweir                     if (create_ucb_content(&contentRemoved, url + OUSTR("removed"),
1427cdf0e10cSrcweir                                            Reference<XCommandEnvironment>(), false))
1428cdf0e10cSrcweir                         continue;
1429cdf0e10cSrcweir                     sExtFolder = getExtensionFolder(
1430cdf0e10cSrcweir                         m_activePackages_expanded +
1431cdf0e10cSrcweir                         OUString(OUSTR("/")) + titleEncoded + OUSTR("_"), xCmdEnv);
1432cdf0e10cSrcweir                     url = makeURLAppendSysPathSegment(m_activePackages_expanded, title);
1433cdf0e10cSrcweir                     url = makeURLAppendSysPathSegment(url, sExtFolder);
1434cdf0e10cSrcweir                 }
1435cdf0e10cSrcweir                 Reference<deployment::XPackage> xPackage = m_xRegistry->bindPackage(
1436cdf0e10cSrcweir                     url, OUString(), false, OUString(), xCmdEnv );
1437cdf0e10cSrcweir                 if (xPackage.is())
1438cdf0e10cSrcweir                 {
1439cdf0e10cSrcweir                     //Prepare the database entry
1440cdf0e10cSrcweir                     ActivePackages::Data dbData;
1441cdf0e10cSrcweir 
1442cdf0e10cSrcweir                     dbData.temporaryName = titleEncoded;
1443cdf0e10cSrcweir                     if (bShared)
1444cdf0e10cSrcweir                         dbData.fileName = sExtFolder;
1445cdf0e10cSrcweir                     else
1446cdf0e10cSrcweir                         dbData.fileName = title;
1447cdf0e10cSrcweir                     dbData.mediaType = xPackage->getPackageType()->getMediaType();
1448cdf0e10cSrcweir                     dbData.version = xPackage->getVersion();
1449cdf0e10cSrcweir                     OSL_ENSURE(dbData.version.getLength() > 0,
1450cdf0e10cSrcweir                                "Extension Manager: bundled and shared extensions must have "
1451cdf0e10cSrcweir                                "an identifier and a version");
1452cdf0e10cSrcweir 
1453cdf0e10cSrcweir                     OUString id = dp_misc::getIdentifier( xPackage );
1454cdf0e10cSrcweir 
1455cdf0e10cSrcweir                     //We provide a special command environment that will prevent
1456cdf0e10cSrcweir                     //showing a license if simple-licens/@accept-by = "admin"
1457cdf0e10cSrcweir                     //It will also prevent showing the license for bundled extensions
1458cdf0e10cSrcweir                     //which is not supported.
1459cdf0e10cSrcweir                     OSL_ASSERT(!m_context.equals(OUSTR("user")));
1460cdf0e10cSrcweir 
1461cdf0e10cSrcweir                     // shall the license be suppressed?
1462cdf0e10cSrcweir                     DescriptionInfoset info =
1463cdf0e10cSrcweir                         dp_misc::getDescriptionInfoset(url);
1464cdf0e10cSrcweir                     ::boost::optional<dp_misc::SimpleLicenseAttributes>
1465cdf0e10cSrcweir                           attr = info.getSimpleLicenseAttributes();
1466cdf0e10cSrcweir                     ExtensionProperties props(url,xCmdEnv);
1467cdf0e10cSrcweir                     bool bNoLicense = false;
1468cdf0e10cSrcweir                     if (attr && attr->suppressIfRequired && props.isSuppressedLicense())
1469cdf0e10cSrcweir                         bNoLicense = true;
1470cdf0e10cSrcweir 
1471cdf0e10cSrcweir                     Reference<ucb::XCommandEnvironment> licCmdEnv(
1472cdf0e10cSrcweir                         new LicenseCommandEnv(xCmdEnv->getInteractionHandler(),
1473cdf0e10cSrcweir                                               bNoLicense, m_context));
1474cdf0e10cSrcweir                     sal_Int32 failedPrereq = xPackage->checkPrerequisites(
1475cdf0e10cSrcweir                         xAbortChannel, licCmdEnv, false);
1476cdf0e10cSrcweir                     //Remember that this failed. For example, the user
1477cdf0e10cSrcweir                     //could have declined the license. Then the next time the
1478cdf0e10cSrcweir                     //extension folder is investigated we do not want to
1479cdf0e10cSrcweir                     //try to install the extension again.
1480cdf0e10cSrcweir                     dbData.failedPrerequisites = OUString::valueOf(failedPrereq);
1481cdf0e10cSrcweir                     insertToActivationLayerDB(id, dbData);
1482cdf0e10cSrcweir                     bModified |= true;
1483cdf0e10cSrcweir                 }
1484cdf0e10cSrcweir             }
1485cdf0e10cSrcweir         }
1486cdf0e10cSrcweir         catch (uno::Exception &)
1487cdf0e10cSrcweir         {
1488cdf0e10cSrcweir             OSL_ASSERT(0);
1489cdf0e10cSrcweir         }
1490cdf0e10cSrcweir     }
1491cdf0e10cSrcweir     return bModified;
1492cdf0e10cSrcweir }
1493cdf0e10cSrcweir 
1494cdf0e10cSrcweir sal_Bool PackageManagerImpl::synchronize(
1495cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1496cdf0e10cSrcweir     Reference<css::ucb::XCommandEnvironment> const & xCmdEnv)
1497cdf0e10cSrcweir     throw (css::deployment::DeploymentException,
1498cdf0e10cSrcweir            css::ucb::CommandFailedException,
1499cdf0e10cSrcweir            css::ucb::CommandAbortedException,
1500cdf0e10cSrcweir            css::uno::RuntimeException)
1501cdf0e10cSrcweir {
1502cdf0e10cSrcweir     check();
1503cdf0e10cSrcweir     bool bModified = false;
1504cdf0e10cSrcweir     if (m_context.equals(OUSTR("user")))
1505cdf0e10cSrcweir         return bModified;
1506cdf0e10cSrcweir     bModified |=
1507cdf0e10cSrcweir         synchronizeRemovedExtensions(xAbortChannel, xCmdEnv);
1508cdf0e10cSrcweir     bModified |= synchronizeAddedExtensions(xAbortChannel, xCmdEnv);
1509cdf0e10cSrcweir 
1510cdf0e10cSrcweir     return bModified;
1511cdf0e10cSrcweir }
1512cdf0e10cSrcweir 
1513cdf0e10cSrcweir Sequence< Reference<deployment::XPackage> > PackageManagerImpl::getExtensionsWithUnacceptedLicenses(
1514cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv)
1515cdf0e10cSrcweir     throw (deployment::DeploymentException, RuntimeException)
1516cdf0e10cSrcweir {
1517cdf0e10cSrcweir     ::std::vector<Reference<deployment::XPackage> > vec;
1518cdf0e10cSrcweir 
1519cdf0e10cSrcweir     try
1520cdf0e10cSrcweir     {
1521cdf0e10cSrcweir         const ::osl::MutexGuard guard( getMutex() );
1522cdf0e10cSrcweir         // clean up activation layer, scan for zombie temp dirs:
1523cdf0e10cSrcweir         ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1524cdf0e10cSrcweir 
1525cdf0e10cSrcweir         ActivePackages::Entries::const_iterator i = id2temp.begin();
1526cdf0e10cSrcweir         bool bShared = m_context.equals(OUSTR("shared"));
1527cdf0e10cSrcweir 
1528cdf0e10cSrcweir         for (; i != id2temp.end(); i++ )
1529cdf0e10cSrcweir         {
1530cdf0e10cSrcweir             //Get the database entry
1531cdf0e10cSrcweir             ActivePackages::Data const & dbData = i->second;
1532cdf0e10cSrcweir             sal_Int32 failedPrereq = dbData.failedPrerequisites.toInt32();
1533cdf0e10cSrcweir             //If the installation failed for other reason then the license then we
1534cdf0e10cSrcweir             //ignore it.
1535cdf0e10cSrcweir             if (failedPrereq ^= deployment::Prerequisites::LICENSE)
1536cdf0e10cSrcweir                 continue;
1537cdf0e10cSrcweir 
1538cdf0e10cSrcweir             //Prepare the URL to the extension
1539cdf0e10cSrcweir             OUString url = makeURL(m_activePackages, i->second.temporaryName);
1540cdf0e10cSrcweir             if (bShared)
1541cdf0e10cSrcweir                 url = makeURLAppendSysPathSegment( url + OUSTR("_"), i->second.fileName);
1542cdf0e10cSrcweir 
1543cdf0e10cSrcweir             Reference<deployment::XPackage> p = m_xRegistry->bindPackage(
1544cdf0e10cSrcweir                 url, OUString(), false, OUString(), xCmdEnv );
1545cdf0e10cSrcweir 
1546cdf0e10cSrcweir             if (p.is())
1547cdf0e10cSrcweir                 vec.push_back(p);
1548cdf0e10cSrcweir 
1549cdf0e10cSrcweir         }
1550cdf0e10cSrcweir         return ::comphelper::containerToSequence(vec);
1551cdf0e10cSrcweir     }
1552cdf0e10cSrcweir     catch (deployment::DeploymentException &)
1553cdf0e10cSrcweir     {
1554cdf0e10cSrcweir         throw;
1555cdf0e10cSrcweir     }
1556cdf0e10cSrcweir     catch (RuntimeException&)
1557cdf0e10cSrcweir     {
1558cdf0e10cSrcweir         throw;
1559cdf0e10cSrcweir     }
1560cdf0e10cSrcweir     catch (...)
1561cdf0e10cSrcweir     {
1562cdf0e10cSrcweir         Any exc = ::cppu::getCaughtException();
1563cdf0e10cSrcweir         deployment::DeploymentException de(
1564cdf0e10cSrcweir             OUSTR("PackageManagerImpl::getExtensionsWithUnacceptedLicenses"),
1565cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1566cdf0e10cSrcweir         exc <<= de;
1567cdf0e10cSrcweir         ::cppu::throwException(exc);
1568cdf0e10cSrcweir     }
1569cdf0e10cSrcweir 
1570cdf0e10cSrcweir     return ::comphelper::containerToSequence(vec);
1571cdf0e10cSrcweir }
1572cdf0e10cSrcweir 
1573cdf0e10cSrcweir sal_Int32 PackageManagerImpl::checkPrerequisites(
1574cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackage> const & extension,
1575cdf0e10cSrcweir     css::uno::Reference<css::task::XAbortChannel> const & xAbortChannel,
1576cdf0e10cSrcweir     css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
1577cdf0e10cSrcweir     throw (css::deployment::DeploymentException,
1578cdf0e10cSrcweir            css::ucb::CommandFailedException,
1579cdf0e10cSrcweir            css::ucb::CommandAbortedException,
1580cdf0e10cSrcweir            css::lang::IllegalArgumentException,
1581cdf0e10cSrcweir            css::uno::RuntimeException)
1582cdf0e10cSrcweir {
1583cdf0e10cSrcweir     try
1584cdf0e10cSrcweir     {
1585cdf0e10cSrcweir         if (!extension.is())
1586cdf0e10cSrcweir             return 0;
1587cdf0e10cSrcweir         if (!m_context.equals(extension->getRepositoryName()))
1588cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1589cdf0e10cSrcweir                 OUSTR("PackageManagerImpl::checkPrerequisites: extension is not"
1590cdf0e10cSrcweir                       " from this repository."), 0, 0);
1591cdf0e10cSrcweir 
1592cdf0e10cSrcweir         ActivePackages::Data dbData;
1593cdf0e10cSrcweir         OUString id = dp_misc::getIdentifier(extension);
1594cdf0e10cSrcweir         if (m_activePackagesDB->get( &dbData, id, OUString()))
1595cdf0e10cSrcweir         {
1596cdf0e10cSrcweir             //If the license was already displayed, then do not show it again
1597cdf0e10cSrcweir             Reference<ucb::XCommandEnvironment> _xCmdEnv = xCmdEnv;
1598cdf0e10cSrcweir             sal_Int32 prereq = dbData.failedPrerequisites.toInt32();
1599cdf0e10cSrcweir             if ( !(prereq & deployment::Prerequisites::LICENSE))
1600cdf0e10cSrcweir                 _xCmdEnv = new NoLicenseCommandEnv(xCmdEnv->getInteractionHandler());
1601cdf0e10cSrcweir 
1602cdf0e10cSrcweir             sal_Int32 failedPrereq = extension->checkPrerequisites(
1603cdf0e10cSrcweir                 xAbortChannel, _xCmdEnv, false);
1604cdf0e10cSrcweir             dbData.failedPrerequisites = OUString::valueOf(failedPrereq);
1605cdf0e10cSrcweir             insertToActivationLayerDB(id, dbData);
1606cdf0e10cSrcweir         }
1607cdf0e10cSrcweir         else
1608cdf0e10cSrcweir         {
1609cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1610cdf0e10cSrcweir                 OUSTR("PackageManagerImpl::checkPrerequisites: unknown extension"),
1611cdf0e10cSrcweir                 0, 0);
1612cdf0e10cSrcweir 
1613cdf0e10cSrcweir         }
1614cdf0e10cSrcweir         return 0;
1615cdf0e10cSrcweir     }
1616cdf0e10cSrcweir     catch (deployment::DeploymentException& ) {
1617cdf0e10cSrcweir         throw;
1618cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1619cdf0e10cSrcweir         throw;
1620cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1621cdf0e10cSrcweir         throw;
1622cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1623cdf0e10cSrcweir         throw;
1624cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1625cdf0e10cSrcweir         throw;
1626cdf0e10cSrcweir     } catch (...) {
1627cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
1628cdf0e10cSrcweir         deployment::DeploymentException exc(
1629cdf0e10cSrcweir             OUSTR("PackageManagerImpl::checkPrerequisites: exception "),
1630cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1631cdf0e10cSrcweir         throw exc;
1632cdf0e10cSrcweir     }
1633cdf0e10cSrcweir }
1634cdf0e10cSrcweir 
1635cdf0e10cSrcweir //##############################################################################
1636cdf0e10cSrcweir 
1637cdf0e10cSrcweir //______________________________________________________________________________
1638cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::~CmdEnvWrapperImpl()
1639cdf0e10cSrcweir {
1640cdf0e10cSrcweir }
1641cdf0e10cSrcweir 
1642cdf0e10cSrcweir //______________________________________________________________________________
1643cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::CmdEnvWrapperImpl(
1644cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xUserCmdEnv,
1645cdf0e10cSrcweir     Reference<XProgressHandler> const & xLogFile )
1646cdf0e10cSrcweir     : m_xLogFile( xLogFile )
1647cdf0e10cSrcweir {
1648cdf0e10cSrcweir     if (xUserCmdEnv.is()) {
1649cdf0e10cSrcweir         m_xUserProgress.set( xUserCmdEnv->getProgressHandler() );
1650cdf0e10cSrcweir         m_xUserInteractionHandler.set( xUserCmdEnv->getInteractionHandler() );
1651cdf0e10cSrcweir     }
1652cdf0e10cSrcweir }
1653cdf0e10cSrcweir 
1654cdf0e10cSrcweir // XCommandEnvironment
1655cdf0e10cSrcweir //______________________________________________________________________________
1656cdf0e10cSrcweir Reference<task::XInteractionHandler>
1657cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::getInteractionHandler()
1658cdf0e10cSrcweir     throw (RuntimeException)
1659cdf0e10cSrcweir {
1660cdf0e10cSrcweir     return m_xUserInteractionHandler;
1661cdf0e10cSrcweir }
1662cdf0e10cSrcweir 
1663cdf0e10cSrcweir //______________________________________________________________________________
1664cdf0e10cSrcweir Reference<XProgressHandler>
1665cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::getProgressHandler()
1666cdf0e10cSrcweir     throw (RuntimeException)
1667cdf0e10cSrcweir {
1668cdf0e10cSrcweir     return this;
1669cdf0e10cSrcweir }
1670cdf0e10cSrcweir 
1671cdf0e10cSrcweir // XProgressHandler
1672cdf0e10cSrcweir //______________________________________________________________________________
1673cdf0e10cSrcweir void PackageManagerImpl::CmdEnvWrapperImpl::push( Any const & Status )
1674cdf0e10cSrcweir     throw (RuntimeException)
1675cdf0e10cSrcweir {
1676cdf0e10cSrcweir     if (m_xLogFile.is())
1677cdf0e10cSrcweir         m_xLogFile->push( Status );
1678cdf0e10cSrcweir     if (m_xUserProgress.is())
1679cdf0e10cSrcweir         m_xUserProgress->push( Status );
1680cdf0e10cSrcweir }
1681cdf0e10cSrcweir 
1682cdf0e10cSrcweir //______________________________________________________________________________
1683cdf0e10cSrcweir void PackageManagerImpl::CmdEnvWrapperImpl::update( Any const & Status )
1684cdf0e10cSrcweir     throw (RuntimeException)
1685cdf0e10cSrcweir {
1686cdf0e10cSrcweir     if (m_xLogFile.is())
1687cdf0e10cSrcweir         m_xLogFile->update( Status );
1688cdf0e10cSrcweir     if (m_xUserProgress.is())
1689cdf0e10cSrcweir         m_xUserProgress->update( Status );
1690cdf0e10cSrcweir }
1691cdf0e10cSrcweir 
1692cdf0e10cSrcweir //______________________________________________________________________________
1693cdf0e10cSrcweir void PackageManagerImpl::CmdEnvWrapperImpl::pop() throw (RuntimeException)
1694cdf0e10cSrcweir {
1695cdf0e10cSrcweir     if (m_xLogFile.is())
1696cdf0e10cSrcweir         m_xLogFile->pop();
1697cdf0e10cSrcweir     if (m_xUserProgress.is())
1698cdf0e10cSrcweir         m_xUserProgress->pop();
1699cdf0e10cSrcweir }
1700cdf0e10cSrcweir 
1701cdf0e10cSrcweir } // namespace dp_manager
1702cdf0e10cSrcweir 
1703