xref: /AOO41X/main/desktop/source/deployment/manager/dp_manager.cxx (revision 7bc7b19f1c17e2a5d9de38287a8d39c97bcc4343)
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     }
402*7bc7b19fSMichael Stahl     else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bak") )) {
403*7bc7b19fSMichael Stahl         that->m_activePackages = OUSTR(
404*7bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS/extensions");
405*7bc7b19fSMichael Stahl         that->m_registrationData = OUSTR(
406*7bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS");
407*7bc7b19fSMichael Stahl         that->m_registryCache = OUSTR(
408*7bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS/registry");
409*7bc7b19fSMichael Stahl         stampURL = OUSTR(
410*7bc7b19fSMichael Stahl             "vnd.sun.star.expand:$BAK_EXTENSIONS/stamp.sys");
411*7bc7b19fSMichael Stahl     }
412*7bc7b19fSMichael 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 {
539cdf0e10cSrcweir     check();
540cdf0e10cSrcweir     WeakComponentImplHelperBase::dispose();
541cdf0e10cSrcweir }
542cdf0e10cSrcweir 
543cdf0e10cSrcweir //______________________________________________________________________________
544cdf0e10cSrcweir void PackageManagerImpl::addEventListener(
545cdf0e10cSrcweir     Reference<lang::XEventListener> const & xListener ) throw (RuntimeException)
546cdf0e10cSrcweir {
547cdf0e10cSrcweir     check();
548cdf0e10cSrcweir     WeakComponentImplHelperBase::addEventListener( xListener );
549cdf0e10cSrcweir }
550cdf0e10cSrcweir 
551cdf0e10cSrcweir //______________________________________________________________________________
552cdf0e10cSrcweir void PackageManagerImpl::removeEventListener(
553cdf0e10cSrcweir     Reference<lang::XEventListener> const & xListener ) throw (RuntimeException)
554cdf0e10cSrcweir {
555cdf0e10cSrcweir     check();
556cdf0e10cSrcweir     WeakComponentImplHelperBase::removeEventListener( xListener );
557cdf0e10cSrcweir }
558cdf0e10cSrcweir 
559cdf0e10cSrcweir // XPackageManager
560cdf0e10cSrcweir //______________________________________________________________________________
561cdf0e10cSrcweir OUString PackageManagerImpl::getContext() throw (RuntimeException)
562cdf0e10cSrcweir {
563cdf0e10cSrcweir     check();
564cdf0e10cSrcweir     return m_context;
565cdf0e10cSrcweir }
566cdf0e10cSrcweir 
567cdf0e10cSrcweir //______________________________________________________________________________
568cdf0e10cSrcweir Sequence< Reference<deployment::XPackageTypeInfo> >
569cdf0e10cSrcweir PackageManagerImpl::getSupportedPackageTypes() throw (RuntimeException)
570cdf0e10cSrcweir {
571cdf0e10cSrcweir     OSL_ASSERT( m_xRegistry.is() );
572cdf0e10cSrcweir     return m_xRegistry->getSupportedPackageTypes();
573cdf0e10cSrcweir }
574cdf0e10cSrcweir 
575cdf0e10cSrcweir //______________________________________________________________________________
576cdf0e10cSrcweir Reference<task::XAbortChannel> PackageManagerImpl::createAbortChannel()
577cdf0e10cSrcweir     throw (RuntimeException)
578cdf0e10cSrcweir {
579cdf0e10cSrcweir     check();
580cdf0e10cSrcweir     return new AbortChannel;
581cdf0e10cSrcweir }
582cdf0e10cSrcweir 
583cdf0e10cSrcweir // XModifyBroadcaster
584cdf0e10cSrcweir //______________________________________________________________________________
585cdf0e10cSrcweir void PackageManagerImpl::addModifyListener(
586cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
587cdf0e10cSrcweir     throw (RuntimeException)
588cdf0e10cSrcweir {
589cdf0e10cSrcweir     check();
590cdf0e10cSrcweir     rBHelper.addListener( ::getCppuType( &xListener ), xListener );
591cdf0e10cSrcweir }
592cdf0e10cSrcweir 
593cdf0e10cSrcweir //______________________________________________________________________________
594cdf0e10cSrcweir void PackageManagerImpl::removeModifyListener(
595cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
596cdf0e10cSrcweir     throw (RuntimeException)
597cdf0e10cSrcweir {
598cdf0e10cSrcweir     check();
599cdf0e10cSrcweir     rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
600cdf0e10cSrcweir }
601cdf0e10cSrcweir 
602cdf0e10cSrcweir //______________________________________________________________________________
603cdf0e10cSrcweir OUString PackageManagerImpl::detectMediaType(
604cdf0e10cSrcweir     ::ucbhelper::Content const & ucbContent_, bool throw_exc )
605cdf0e10cSrcweir {
606cdf0e10cSrcweir     ::ucbhelper::Content ucbContent(ucbContent_);
607cdf0e10cSrcweir     OUString url( ucbContent.getURL() );
608cdf0e10cSrcweir     OUString mediaType;
609cdf0e10cSrcweir     if (url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.tdoc:") ) ||
610cdf0e10cSrcweir         url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.pkg:") ))
611cdf0e10cSrcweir     {
612cdf0e10cSrcweir         try {
613cdf0e10cSrcweir             ucbContent.getPropertyValue( OUSTR("MediaType") ) >>= mediaType;
614cdf0e10cSrcweir         }
615cdf0e10cSrcweir         catch (beans::UnknownPropertyException &) {
616cdf0e10cSrcweir         }
617cdf0e10cSrcweir         OSL_ENSURE( mediaType.getLength() > 0, "### no media-type?!" );
618cdf0e10cSrcweir     }
619cdf0e10cSrcweir     if (mediaType.getLength() == 0)
620cdf0e10cSrcweir     {
621cdf0e10cSrcweir         try {
622cdf0e10cSrcweir             Reference<deployment::XPackage> xPackage(
623cdf0e10cSrcweir                 m_xRegistry->bindPackage(
624cdf0e10cSrcweir                     url, OUString(), false, OUString(), ucbContent.getCommandEnvironment() ) );
625cdf0e10cSrcweir             const Reference<deployment::XPackageTypeInfo> xPackageType(
626cdf0e10cSrcweir                 xPackage->getPackageType() );
627cdf0e10cSrcweir             OSL_ASSERT( xPackageType.is() );
628cdf0e10cSrcweir             if (xPackageType.is())
629cdf0e10cSrcweir                 mediaType = xPackageType->getMediaType();
630cdf0e10cSrcweir         }
631cdf0e10cSrcweir         catch (lang::IllegalArgumentException & exc) {
632cdf0e10cSrcweir             if (throw_exc)
633cdf0e10cSrcweir                 throw;
634cdf0e10cSrcweir             (void) exc;
635cdf0e10cSrcweir             OSL_ENSURE( 0, ::rtl::OUStringToOString(
636cdf0e10cSrcweir                             exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
637cdf0e10cSrcweir         }
638cdf0e10cSrcweir     }
639cdf0e10cSrcweir     return mediaType;
640cdf0e10cSrcweir }
641cdf0e10cSrcweir 
642cdf0e10cSrcweir //______________________________________________________________________________
643cdf0e10cSrcweir OUString PackageManagerImpl::insertToActivationLayer(
644cdf0e10cSrcweir     Sequence<beans::NamedValue> const & properties,
645cdf0e10cSrcweir     OUString const & mediaType, ::ucbhelper::Content const & sourceContent_,
646cdf0e10cSrcweir     OUString const & title, ActivePackages::Data * dbData )
647cdf0e10cSrcweir {
648cdf0e10cSrcweir     ::ucbhelper::Content sourceContent(sourceContent_);
649cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv(
650cdf0e10cSrcweir         sourceContent.getCommandEnvironment() );
651cdf0e10cSrcweir 
652cdf0e10cSrcweir     String baseDir(m_activePackages_expanded);
653cdf0e10cSrcweir     ::utl::TempFile aTemp(&baseDir, sal_False);
654cdf0e10cSrcweir     OUString tempEntry = aTemp.GetURL();
655cdf0e10cSrcweir     tempEntry = tempEntry.copy(tempEntry.lastIndexOf('/') + 1);
656cdf0e10cSrcweir     OUString destFolder = makeURL( m_activePackages, tempEntry);
657cdf0e10cSrcweir     destFolder += OUSTR("_");
658cdf0e10cSrcweir 
659cdf0e10cSrcweir     // prepare activation folder:
660cdf0e10cSrcweir     ::ucbhelper::Content destFolderContent;
661cdf0e10cSrcweir     create_folder( &destFolderContent, destFolder, xCmdEnv );
662cdf0e10cSrcweir 
663cdf0e10cSrcweir     // copy content into activation temp dir:
664cdf0e10cSrcweir     if (mediaType.matchIgnoreAsciiCaseAsciiL(
665cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM(
666cdf0e10cSrcweir                 "application/vnd.sun.star.package-bundle") ) ||
667cdf0e10cSrcweir         // xxx todo: more sophisticated parsing
668cdf0e10cSrcweir         mediaType.matchIgnoreAsciiCaseAsciiL(
669cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM(
670cdf0e10cSrcweir                 "application/vnd.sun.star.legacy-package-bundle") ))
671cdf0e10cSrcweir     {
672cdf0e10cSrcweir         // inflate content:
673cdf0e10cSrcweir         ::rtl::OUStringBuffer buf;
674cdf0e10cSrcweir         if (!sourceContent.isFolder())
675cdf0e10cSrcweir         {
676cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.zip://") );
677cdf0e10cSrcweir             buf.append( ::rtl::Uri::encode( sourceContent.getURL(),
678cdf0e10cSrcweir                                             rtl_UriCharClassRegName,
679cdf0e10cSrcweir                                             rtl_UriEncodeIgnoreEscapes,
680cdf0e10cSrcweir                                             RTL_TEXTENCODING_UTF8 ) );
681cdf0e10cSrcweir         }
682cdf0e10cSrcweir         else
683cdf0e10cSrcweir         {
684cdf0e10cSrcweir             //Folder. No need to unzip, just copy
685cdf0e10cSrcweir             buf.append(sourceContent.getURL());
686cdf0e10cSrcweir         }
687cdf0e10cSrcweir         buf.append( static_cast<sal_Unicode>('/') );
688cdf0e10cSrcweir         sourceContent = ::ucbhelper::Content(
689cdf0e10cSrcweir             buf.makeStringAndClear(), xCmdEnv );
690cdf0e10cSrcweir     }
691cdf0e10cSrcweir     if (! destFolderContent.transferContent(
692cdf0e10cSrcweir             sourceContent, ::ucbhelper::InsertOperation_COPY,
693cdf0e10cSrcweir             title, NameClash::OVERWRITE ))
694cdf0e10cSrcweir         throw RuntimeException( OUSTR("UCB transferContent() failed!"), 0 );
695cdf0e10cSrcweir 
696cdf0e10cSrcweir 
697cdf0e10cSrcweir     // write to DB:
698cdf0e10cSrcweir     //bundled extensions should only be added by the synchronizeAddedExtensions
699cdf0e10cSrcweir     //functions. Moreover, there is no "temporary folder" for bundled extensions.
700cdf0e10cSrcweir     OSL_ASSERT(!m_context.equals(OUSTR("bundled")));
701cdf0e10cSrcweir     OUString sFolderUrl = makeURLAppendSysPathSegment(destFolderContent.getURL(), title);
702cdf0e10cSrcweir     DescriptionInfoset info =
703cdf0e10cSrcweir         dp_misc::getDescriptionInfoset(sFolderUrl);
704cdf0e10cSrcweir     dbData->temporaryName = tempEntry;
705cdf0e10cSrcweir     dbData->fileName = title;
706cdf0e10cSrcweir     dbData->mediaType = mediaType;
707cdf0e10cSrcweir     dbData->version = info.getVersion();
708cdf0e10cSrcweir 
709cdf0e10cSrcweir     //No write the properties file next to the extension
710cdf0e10cSrcweir     ExtensionProperties props(sFolderUrl, properties, xCmdEnv);
711cdf0e10cSrcweir     props.write();
712cdf0e10cSrcweir     return destFolder;
713cdf0e10cSrcweir }
714cdf0e10cSrcweir 
715cdf0e10cSrcweir //______________________________________________________________________________
716cdf0e10cSrcweir void PackageManagerImpl::insertToActivationLayerDB(
717cdf0e10cSrcweir     OUString const & id, ActivePackages::Data const & dbData )
718cdf0e10cSrcweir {
719cdf0e10cSrcweir     //access to the database must be guarded. See removePackage
720cdf0e10cSrcweir     const ::osl::MutexGuard guard( getMutex() );
721cdf0e10cSrcweir     m_activePackagesDB->put( id, dbData );
722cdf0e10cSrcweir }
723cdf0e10cSrcweir 
724cdf0e10cSrcweir //______________________________________________________________________________
725cdf0e10cSrcweir /* The function returns true if there is an extension with the same id already
726cdf0e10cSrcweir     installed which needs to be uninstalled, before the new extension can be installed.
727cdf0e10cSrcweir */
728cdf0e10cSrcweir bool PackageManagerImpl::isInstalled(
729cdf0e10cSrcweir     Reference<deployment::XPackage> const & package)
730cdf0e10cSrcweir {
731cdf0e10cSrcweir     OUString id(dp_misc::getIdentifier(package));
732cdf0e10cSrcweir     OUString fn(package->getName());
733cdf0e10cSrcweir     bool bInstalled = false;
734cdf0e10cSrcweir     if (m_activePackagesDB->has( id, fn ))
735cdf0e10cSrcweir     {
736cdf0e10cSrcweir         bInstalled = true;
737cdf0e10cSrcweir     }
738cdf0e10cSrcweir     return bInstalled;
739cdf0e10cSrcweir }
740cdf0e10cSrcweir 
741cdf0e10cSrcweir // XPackageManager
742cdf0e10cSrcweir //______________________________________________________________________________
743cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::importExtension(
744cdf0e10cSrcweir     Reference<deployment::XPackage> const & extension,
745cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
746cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
747cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
748cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
749cdf0e10cSrcweir            RuntimeException)
750cdf0e10cSrcweir {
751cdf0e10cSrcweir     return addPackage(extension->getURL(), Sequence<beans::NamedValue>(),
752cdf0e10cSrcweir                       OUString(), xAbortChannel, xCmdEnv_);
753cdf0e10cSrcweir }
754cdf0e10cSrcweir 
755cdf0e10cSrcweir /* The function adds an extension but does not register it!!!
756cdf0e10cSrcweir     It may not do any user interaction. This is done in XExtensionManager::addExtension
757cdf0e10cSrcweir */
758cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::addPackage(
759cdf0e10cSrcweir     OUString const & url,
760cdf0e10cSrcweir     css::uno::Sequence<css::beans::NamedValue> const & properties,
761cdf0e10cSrcweir     OUString const & mediaType_,
762cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
763cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
764cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
765cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
766cdf0e10cSrcweir            RuntimeException)
767cdf0e10cSrcweir {
768cdf0e10cSrcweir     check();
769cdf0e10cSrcweir     if (m_readOnly)
770cdf0e10cSrcweir 	{
771cdf0e10cSrcweir 		OUString message;
772cdf0e10cSrcweir 		if (m_context == OUSTR("shared"))
773cdf0e10cSrcweir 			message = OUSTR("You need write permissions to install a shared extension!");
774cdf0e10cSrcweir 		else
775cdf0e10cSrcweir 			message = OUSTR("You need write permissions to install this extension!");
776cdf0e10cSrcweir         throw deployment::DeploymentException(
777cdf0e10cSrcweir             message, static_cast<OWeakObject *>(this), Any() );
778cdf0e10cSrcweir 	}
779cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
780cdf0e10cSrcweir     if (m_xLogFile.is())
781cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
782cdf0e10cSrcweir     else
783cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
784cdf0e10cSrcweir 
785cdf0e10cSrcweir     try {
786cdf0e10cSrcweir         ::ucbhelper::Content sourceContent;
787cdf0e10cSrcweir         create_ucb_content( &sourceContent, url, xCmdEnv ); // throws exc
788cdf0e10cSrcweir         const OUString title(sourceContent.getPropertyValue(
789cdf0e10cSrcweir                              StrTitle::get() ).get<OUString>() );
790cdf0e10cSrcweir         const OUString title_enc( ::rtl::Uri::encode(
791cdf0e10cSrcweir                                       title, rtl_UriCharClassPchar,
792cdf0e10cSrcweir                                       rtl_UriEncodeIgnoreEscapes,
793cdf0e10cSrcweir                                       RTL_TEXTENCODING_UTF8 ) );
794cdf0e10cSrcweir         OUString destFolder;
795cdf0e10cSrcweir 
796cdf0e10cSrcweir         OUString mediaType(mediaType_);
797cdf0e10cSrcweir         if (mediaType.getLength() == 0)
798cdf0e10cSrcweir             mediaType = detectMediaType( sourceContent );
799cdf0e10cSrcweir 
800cdf0e10cSrcweir         Reference<deployment::XPackage> xPackage;
801cdf0e10cSrcweir         // copy file:
802cdf0e10cSrcweir         progressUpdate(
803cdf0e10cSrcweir             getResourceString(RID_STR_COPYING_PACKAGE) + title, xCmdEnv );
804cdf0e10cSrcweir         if (m_activePackages.getLength() == 0)
805cdf0e10cSrcweir         {
806cdf0e10cSrcweir             ::ucbhelper::Content docFolderContent;
807cdf0e10cSrcweir             create_folder( &docFolderContent, m_context, xCmdEnv );
808cdf0e10cSrcweir             // copy into document, first:
809cdf0e10cSrcweir             if (! docFolderContent.transferContent(
810cdf0e10cSrcweir                     sourceContent, ::ucbhelper::InsertOperation_COPY,
811cdf0e10cSrcweir                     OUString(),
812cdf0e10cSrcweir                     NameClash::ASK /* xxx todo: ASK not needed? */))
813cdf0e10cSrcweir                 throw RuntimeException(
814cdf0e10cSrcweir                     OUSTR("UCB transferContent() failed!"), 0 );
815cdf0e10cSrcweir             // set media-type:
816cdf0e10cSrcweir             ::ucbhelper::Content docContent(
817cdf0e10cSrcweir                 makeURL( m_context, title_enc ), xCmdEnv );
818cdf0e10cSrcweir                 //TODO #i73136#: using title instead of id can lead to
819cdf0e10cSrcweir                 // clashes, but the whole m_activePackages.getLength()==0
820cdf0e10cSrcweir                 // case (i.e., document-relative deployment) currently does
821cdf0e10cSrcweir                 // not work, anyway.
822cdf0e10cSrcweir             docContent.setPropertyValue(
823cdf0e10cSrcweir                 OUSTR("MediaType"), Any(mediaType) );
824cdf0e10cSrcweir 
825cdf0e10cSrcweir             // xxx todo: obsolete in the future
826cdf0e10cSrcweir             try {
827cdf0e10cSrcweir                 docFolderContent.executeCommand( OUSTR("flush"), Any() );
828cdf0e10cSrcweir             }
829cdf0e10cSrcweir             catch (UnsupportedCommandException &) {
830cdf0e10cSrcweir             }
831cdf0e10cSrcweir         }
832cdf0e10cSrcweir         ActivePackages::Data dbData;
833cdf0e10cSrcweir         destFolder = insertToActivationLayer(
834cdf0e10cSrcweir             properties, mediaType, sourceContent, title, &dbData );
835cdf0e10cSrcweir 
836cdf0e10cSrcweir 
837cdf0e10cSrcweir         // bind activation package:
838cdf0e10cSrcweir         //Because every shared/user extension will be unpacked in a folder,
839cdf0e10cSrcweir         //which was created with a unique name we will always have two different
840cdf0e10cSrcweir         //XPackage objects, even if the second extension is the same.
841cdf0e10cSrcweir         //Therefore bindPackage does not need a guard here.
842cdf0e10cSrcweir         xPackage = m_xRegistry->bindPackage(
843cdf0e10cSrcweir             makeURL( destFolder, title_enc ), mediaType, false, OUString(), xCmdEnv );
844cdf0e10cSrcweir 
845cdf0e10cSrcweir         OSL_ASSERT( xPackage.is() );
846cdf0e10cSrcweir         if (xPackage.is())
847cdf0e10cSrcweir         {
848cdf0e10cSrcweir             bool install = false;
849cdf0e10cSrcweir             try
850cdf0e10cSrcweir             {
851cdf0e10cSrcweir                 OUString const id = dp_misc::getIdentifier( xPackage );
852cdf0e10cSrcweir 
853cdf0e10cSrcweir                 ::osl::MutexGuard g(m_addMutex);
854cdf0e10cSrcweir                 if (isInstalled(xPackage))
855cdf0e10cSrcweir                 {
856cdf0e10cSrcweir                     //Do not guard the complete function with the getMutex
857cdf0e10cSrcweir                     removePackage(id, xPackage->getName(), xAbortChannel,
858cdf0e10cSrcweir                                   xCmdEnv);
859cdf0e10cSrcweir                 }
860cdf0e10cSrcweir                 install = true;
861cdf0e10cSrcweir                 insertToActivationLayerDB(id, dbData);
862cdf0e10cSrcweir             }
863cdf0e10cSrcweir             catch (...)
864cdf0e10cSrcweir             {
865cdf0e10cSrcweir                 deletePackageFromCache( xPackage, destFolder );
866cdf0e10cSrcweir                 throw;
867cdf0e10cSrcweir             }
868cdf0e10cSrcweir             if (!install)
869cdf0e10cSrcweir             {
870cdf0e10cSrcweir                 deletePackageFromCache( xPackage, destFolder );
871cdf0e10cSrcweir             }
872cdf0e10cSrcweir             //ToDo: We should notify only if the extension is registered
873cdf0e10cSrcweir             fireModified();
874cdf0e10cSrcweir         }
875cdf0e10cSrcweir         return xPackage;
876cdf0e10cSrcweir     }
877cdf0e10cSrcweir     catch (RuntimeException &) {
878cdf0e10cSrcweir         throw;
879cdf0e10cSrcweir     }
880cdf0e10cSrcweir     catch (CommandFailedException & exc) {
881cdf0e10cSrcweir         logIntern( Any(exc) );
882cdf0e10cSrcweir         throw;
883cdf0e10cSrcweir     }
884cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
885cdf0e10cSrcweir         logIntern( Any(exc) );
886cdf0e10cSrcweir         throw;
887cdf0e10cSrcweir     }
888cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
889cdf0e10cSrcweir         logIntern( Any(exc) );
890cdf0e10cSrcweir         throw;
891cdf0e10cSrcweir     }
892cdf0e10cSrcweir     catch (Exception &) {
893cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
894cdf0e10cSrcweir         logIntern( exc );
895cdf0e10cSrcweir         throw deployment::DeploymentException(
896cdf0e10cSrcweir             getResourceString(RID_STR_ERROR_WHILE_ADDING) + url,
897cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
898cdf0e10cSrcweir     }
899cdf0e10cSrcweir }
900cdf0e10cSrcweir void PackageManagerImpl::deletePackageFromCache(
901cdf0e10cSrcweir     Reference<deployment::XPackage> const & xPackage,
902cdf0e10cSrcweir     OUString const & destFolder)
903cdf0e10cSrcweir {
904cdf0e10cSrcweir     try_dispose( xPackage );
905cdf0e10cSrcweir 
906cdf0e10cSrcweir 	//we remove the package from the uno cache
907cdf0e10cSrcweir 	//no service from the package may be loaded at this time!!!
908cdf0e10cSrcweir 	erase_path( destFolder, Reference<XCommandEnvironment>(),
909cdf0e10cSrcweir         false /* no throw: ignore errors */ );
910cdf0e10cSrcweir 	//rm last character '_'
911cdf0e10cSrcweir 	OUString url = destFolder.copy(0, destFolder.getLength() - 1);
912cdf0e10cSrcweir 	erase_path( url, Reference<XCommandEnvironment>(),
913cdf0e10cSrcweir         false /* no throw: ignore errors */ );
914cdf0e10cSrcweir 
915cdf0e10cSrcweir }
916cdf0e10cSrcweir //______________________________________________________________________________
917cdf0e10cSrcweir void PackageManagerImpl::removePackage(
918cdf0e10cSrcweir     OUString const & id, ::rtl::OUString const & fileName,
919cdf0e10cSrcweir     Reference<task::XAbortChannel> const & /*xAbortChannel*/,
920cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
921cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
922cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
923cdf0e10cSrcweir            RuntimeException)
924cdf0e10cSrcweir {
925cdf0e10cSrcweir     check();
926cdf0e10cSrcweir 
927cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
928cdf0e10cSrcweir     if (m_xLogFile.is())
929cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
930cdf0e10cSrcweir     else
931cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
932cdf0e10cSrcweir 
933cdf0e10cSrcweir     try {
934cdf0e10cSrcweir         Reference<deployment::XPackage> xPackage;
935cdf0e10cSrcweir         {
936cdf0e10cSrcweir             const ::osl::MutexGuard guard(getMutex());
937cdf0e10cSrcweir             //Check if this extension exist and throw an IllegalArgumentException
938cdf0e10cSrcweir             //if it does not
939cdf0e10cSrcweir             //If the files of the extension are already removed, or there is a
940cdf0e10cSrcweir             //different extension at the same place, for example after updating the
941cdf0e10cSrcweir             //extension, then the returned object is that which uses the database data.
942cdf0e10cSrcweir             xPackage = getDeployedPackage_(id, fileName, xCmdEnv );
943cdf0e10cSrcweir 
944cdf0e10cSrcweir 
945cdf0e10cSrcweir             //Because the extension is only removed the next time the extension
946cdf0e10cSrcweir             //manager runs after restarting OOo, we need to indicate that a
947cdf0e10cSrcweir             //shared extension was "deleted". When a user starts OOo, then it
948cdf0e10cSrcweir             //will check if something changed in the shared repository. Based on
949cdf0e10cSrcweir             //the flag file it will then recognize, that the extension was
950cdf0e10cSrcweir             //deleted and can then update the extnesion database of the shared
951cdf0e10cSrcweir             //extensions in the user installation.
952cdf0e10cSrcweir             if ( xPackage.is() && !m_readOnly && !xPackage->isRemoved() && m_context.equals(OUSTR("shared")))
953cdf0e10cSrcweir             {
954cdf0e10cSrcweir                 ActivePackages::Data val;
955cdf0e10cSrcweir                 m_activePackagesDB->get( & val, id, fileName);
956cdf0e10cSrcweir                 OSL_ASSERT(val.temporaryName.getLength());
957cdf0e10cSrcweir                 OUString url(makeURL(m_activePackages_expanded,
958cdf0e10cSrcweir                                      val.temporaryName + OUSTR("removed")));
959cdf0e10cSrcweir                 ::ucbhelper::Content contentRemoved(url, xCmdEnv );
960cdf0e10cSrcweir                 OUString aUserName;
961cdf0e10cSrcweir                 ::osl::Security aSecurity;
962cdf0e10cSrcweir                 aSecurity.getUserName( aUserName );
963cdf0e10cSrcweir 
964cdf0e10cSrcweir                 ::rtl::OString stamp = ::rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8);
965cdf0e10cSrcweir                 Reference<css::io::XInputStream> xData(
966cdf0e10cSrcweir                     ::xmlscript::createInputStream(
967cdf0e10cSrcweir                         ::rtl::ByteSequence(
968cdf0e10cSrcweir                             reinterpret_cast<sal_Int8 const *>(stamp.getStr()),
969cdf0e10cSrcweir                             stamp.getLength() ) ) );
970cdf0e10cSrcweir                 contentRemoved.writeStream( xData, true /* replace existing */ );
971cdf0e10cSrcweir             }
972cdf0e10cSrcweir             m_activePackagesDB->erase( id, fileName ); // to be removed upon next start
973cdf0e10cSrcweir             //remove any cached data hold by the backend
974cdf0e10cSrcweir             m_xRegistry->packageRemoved(xPackage->getURL(), xPackage->getPackageType()->getMediaType());
975cdf0e10cSrcweir         }
976cdf0e10cSrcweir         try_dispose( xPackage );
977cdf0e10cSrcweir 
978cdf0e10cSrcweir         fireModified();
979cdf0e10cSrcweir     }
980cdf0e10cSrcweir     catch (RuntimeException &) {
981cdf0e10cSrcweir         throw;
982cdf0e10cSrcweir     }
983cdf0e10cSrcweir     catch (lang::IllegalArgumentException &) {
984cdf0e10cSrcweir         throw;
985cdf0e10cSrcweir     }
986cdf0e10cSrcweir     catch (CommandFailedException & exc) {
987cdf0e10cSrcweir         logIntern( Any(exc) );
988cdf0e10cSrcweir         throw;
989cdf0e10cSrcweir     }
990cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
991cdf0e10cSrcweir         logIntern( Any(exc) );
992cdf0e10cSrcweir         throw;
993cdf0e10cSrcweir     }
994cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
995cdf0e10cSrcweir         logIntern( Any(exc) );
996cdf0e10cSrcweir         throw;
997cdf0e10cSrcweir     }
998cdf0e10cSrcweir     catch (Exception &) {
999cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1000cdf0e10cSrcweir         logIntern( exc );
1001cdf0e10cSrcweir         throw deployment::DeploymentException(
1002cdf0e10cSrcweir             getResourceString(RID_STR_ERROR_WHILE_REMOVING) + id,
1003cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1004cdf0e10cSrcweir     }
1005cdf0e10cSrcweir }
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir //______________________________________________________________________________
1008cdf0e10cSrcweir OUString PackageManagerImpl::getDeployPath( ActivePackages::Data const & data )
1009cdf0e10cSrcweir {
1010cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
1011cdf0e10cSrcweir     buf.append( data.temporaryName );
1012cdf0e10cSrcweir     //The bundled extensions are not contained in an additional folder
1013cdf0e10cSrcweir     //with a unique name. data.temporaryName contains already the
1014cdf0e10cSrcweir     //UTF8 encoded folder name. See PackageManagerImpl::synchronize
1015cdf0e10cSrcweir     if (!m_context.equals(OUSTR("bundled"))
1016cdf0e10cSrcweir         && !m_context.equals(OUSTR("bundled_prereg")))
1017cdf0e10cSrcweir     {
1018cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("_/") );
1019cdf0e10cSrcweir         buf.append( ::rtl::Uri::encode( data.fileName, rtl_UriCharClassPchar,
1020cdf0e10cSrcweir                                     rtl_UriEncodeIgnoreEscapes,
1021cdf0e10cSrcweir                                     RTL_TEXTENCODING_UTF8 ) );
1022cdf0e10cSrcweir     }
1023cdf0e10cSrcweir     return makeURL( m_activePackages, buf.makeStringAndClear() );
1024cdf0e10cSrcweir }
1025cdf0e10cSrcweir 
1026cdf0e10cSrcweir //______________________________________________________________________________
1027cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::getDeployedPackage_(
1028cdf0e10cSrcweir     OUString const & id, OUString const & fileName,
1029cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
1030cdf0e10cSrcweir {
1031cdf0e10cSrcweir     ActivePackages::Data val;
1032cdf0e10cSrcweir     if (m_activePackagesDB->get( &val, id, fileName ))
1033cdf0e10cSrcweir     {
1034cdf0e10cSrcweir         return getDeployedPackage_( id, val, xCmdEnv, false );
1035cdf0e10cSrcweir     }
1036cdf0e10cSrcweir     throw lang::IllegalArgumentException(
1037cdf0e10cSrcweir         getResourceString(RID_STR_NO_SUCH_PACKAGE) + id,
1038cdf0e10cSrcweir         static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
1039cdf0e10cSrcweir }
1040cdf0e10cSrcweir 
1041cdf0e10cSrcweir //______________________________________________________________________________
1042cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::getDeployedPackage_(
1043cdf0e10cSrcweir     OUString const & id, ActivePackages::Data const & data,
1044cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv, bool ignoreAlienPlatforms )
1045cdf0e10cSrcweir {
1046cdf0e10cSrcweir     if (ignoreAlienPlatforms)
1047cdf0e10cSrcweir     {
1048cdf0e10cSrcweir         String type, subType;
1049cdf0e10cSrcweir         INetContentTypeParameterList params;
1050cdf0e10cSrcweir         if (INetContentTypes::parse( data.mediaType, type, subType, &params ))
1051cdf0e10cSrcweir         {
1052cdf0e10cSrcweir             INetContentTypeParameter const * param = params.find(
1053cdf0e10cSrcweir                 ByteString("platform") );
1054cdf0e10cSrcweir             if (param != 0 && !platform_fits( param->m_sValue ))
1055cdf0e10cSrcweir                 throw lang::IllegalArgumentException(
1056cdf0e10cSrcweir                     getResourceString(RID_STR_NO_SUCH_PACKAGE) + id,
1057cdf0e10cSrcweir                     static_cast<OWeakObject *>(this),
1058cdf0e10cSrcweir                     static_cast<sal_Int16>(-1) );
1059cdf0e10cSrcweir         }
1060cdf0e10cSrcweir     }
1061cdf0e10cSrcweir     Reference<deployment::XPackage> xExtension;
1062cdf0e10cSrcweir     try
1063cdf0e10cSrcweir     {
1064cdf0e10cSrcweir         //Ignore extensions where XPackage::checkPrerequisites failed.
1065cdf0e10cSrcweir         //They must not be usable for this user.
1066cdf0e10cSrcweir         if (data.failedPrerequisites.equals(OUSTR("0")))
1067cdf0e10cSrcweir         {
1068cdf0e10cSrcweir             xExtension = m_xRegistry->bindPackage(
1069cdf0e10cSrcweir                 getDeployPath( data ), data.mediaType, false, OUString(), xCmdEnv );
1070cdf0e10cSrcweir         }
1071cdf0e10cSrcweir     }
1072cdf0e10cSrcweir     catch (deployment::InvalidRemovedParameterException& e)
1073cdf0e10cSrcweir     {
1074cdf0e10cSrcweir         xExtension = e.Extension;
1075cdf0e10cSrcweir     }
1076cdf0e10cSrcweir     return xExtension;
1077cdf0e10cSrcweir }
1078cdf0e10cSrcweir 
1079cdf0e10cSrcweir //______________________________________________________________________________
1080cdf0e10cSrcweir Sequence< Reference<deployment::XPackage> >
1081cdf0e10cSrcweir PackageManagerImpl::getDeployedPackages_(
1082cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv )
1083cdf0e10cSrcweir {
1084cdf0e10cSrcweir     ::std::vector< Reference<deployment::XPackage> > packages;
1085cdf0e10cSrcweir     ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1086cdf0e10cSrcweir     ActivePackages::Entries::const_iterator iPos( id2temp.begin() );
1087cdf0e10cSrcweir     ActivePackages::Entries::const_iterator const iEnd( id2temp.end() );
1088cdf0e10cSrcweir     for ( ; iPos != iEnd; ++iPos )
1089cdf0e10cSrcweir     {
1090cdf0e10cSrcweir         if (! iPos->second.failedPrerequisites.equals(OUSTR("0")))
1091cdf0e10cSrcweir             continue;
1092cdf0e10cSrcweir         try {
1093cdf0e10cSrcweir             packages.push_back(
1094cdf0e10cSrcweir                 getDeployedPackage_(
1095cdf0e10cSrcweir                     iPos->first, iPos->second, xCmdEnv,
1096cdf0e10cSrcweir                     true /* xxx todo: think of GUI:
1097cdf0e10cSrcweir                             ignore other platforms than the current one */ ) );
1098cdf0e10cSrcweir         }
1099cdf0e10cSrcweir         catch (lang::IllegalArgumentException & exc) {
1100cdf0e10cSrcweir             // ignore
1101cdf0e10cSrcweir             (void) exc; // avoid warnings
1102cdf0e10cSrcweir             OSL_ENSURE( 0, ::rtl::OUStringToOString(
1103cdf0e10cSrcweir                             exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1104cdf0e10cSrcweir         }
1105cdf0e10cSrcweir         catch (deployment::DeploymentException& exc) {
1106cdf0e10cSrcweir             // ignore
1107cdf0e10cSrcweir             (void) exc; // avoid warnings
1108cdf0e10cSrcweir             OSL_ENSURE( 0, ::rtl::OUStringToOString(
1109cdf0e10cSrcweir                             exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1110cdf0e10cSrcweir         }
1111cdf0e10cSrcweir     }
1112cdf0e10cSrcweir     return comphelper::containerToSequence(packages);
1113cdf0e10cSrcweir }
1114cdf0e10cSrcweir 
1115cdf0e10cSrcweir //______________________________________________________________________________
1116cdf0e10cSrcweir Reference<deployment::XPackage> PackageManagerImpl::getDeployedPackage(
1117cdf0e10cSrcweir     OUString const & id, ::rtl::OUString const & fileName,
1118cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
1119cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
1120cdf0e10cSrcweir            lang::IllegalArgumentException, RuntimeException)
1121cdf0e10cSrcweir {
1122cdf0e10cSrcweir     check();
1123cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
1124cdf0e10cSrcweir     if (m_xLogFile.is())
1125cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
1126cdf0e10cSrcweir     else
1127cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
1128cdf0e10cSrcweir 
1129cdf0e10cSrcweir     try {
1130cdf0e10cSrcweir         const ::osl::MutexGuard guard( getMutex() );
1131cdf0e10cSrcweir         return getDeployedPackage_( id, fileName, xCmdEnv );
1132cdf0e10cSrcweir     }
1133cdf0e10cSrcweir     catch (RuntimeException &) {
1134cdf0e10cSrcweir         throw;
1135cdf0e10cSrcweir     }
1136cdf0e10cSrcweir     catch (CommandFailedException & exc) {
1137cdf0e10cSrcweir         logIntern( Any(exc) );
1138cdf0e10cSrcweir         throw;
1139cdf0e10cSrcweir     }
1140cdf0e10cSrcweir     catch (lang::IllegalArgumentException & exc) {
1141cdf0e10cSrcweir         logIntern( Any(exc) );
1142cdf0e10cSrcweir         throw;
1143cdf0e10cSrcweir     }
1144cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
1145cdf0e10cSrcweir         logIntern( Any(exc) );
1146cdf0e10cSrcweir         throw;
1147cdf0e10cSrcweir     }
1148cdf0e10cSrcweir     catch (Exception &) {
1149cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1150cdf0e10cSrcweir         logIntern( exc );
1151cdf0e10cSrcweir         throw deployment::DeploymentException(
1152cdf0e10cSrcweir             // ought never occur...
1153cdf0e10cSrcweir             OUSTR("error while accessing deployed package: ") + id,
1154cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1155cdf0e10cSrcweir     }
1156cdf0e10cSrcweir }
1157cdf0e10cSrcweir 
1158cdf0e10cSrcweir //______________________________________________________________________________
1159cdf0e10cSrcweir Sequence< Reference<deployment::XPackage> >
1160cdf0e10cSrcweir PackageManagerImpl::getDeployedPackages(
1161cdf0e10cSrcweir     Reference<task::XAbortChannel> const &,
1162cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
1163cdf0e10cSrcweir     throw (deployment::DeploymentException, CommandFailedException,
1164cdf0e10cSrcweir            CommandAbortedException, lang::IllegalArgumentException,
1165cdf0e10cSrcweir            RuntimeException)
1166cdf0e10cSrcweir {
1167cdf0e10cSrcweir     check();
1168cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
1169cdf0e10cSrcweir     if (m_xLogFile.is())
1170cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
1171cdf0e10cSrcweir     else
1172cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
1173cdf0e10cSrcweir 
1174cdf0e10cSrcweir     try {
1175cdf0e10cSrcweir         const ::osl::MutexGuard guard( getMutex() );
1176cdf0e10cSrcweir         return getDeployedPackages_( xCmdEnv );
1177cdf0e10cSrcweir     }
1178cdf0e10cSrcweir     catch (RuntimeException &) {
1179cdf0e10cSrcweir         throw;
1180cdf0e10cSrcweir     }
1181cdf0e10cSrcweir     catch (CommandFailedException & exc) {
1182cdf0e10cSrcweir         logIntern( Any(exc) );
1183cdf0e10cSrcweir         throw;
1184cdf0e10cSrcweir     }
1185cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
1186cdf0e10cSrcweir         logIntern( Any(exc) );
1187cdf0e10cSrcweir         throw;
1188cdf0e10cSrcweir     }
1189cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
1190cdf0e10cSrcweir         logIntern( Any(exc) );
1191cdf0e10cSrcweir         throw;
1192cdf0e10cSrcweir     }
1193cdf0e10cSrcweir     catch (Exception &) {
1194cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1195cdf0e10cSrcweir         logIntern( exc );
1196cdf0e10cSrcweir         throw deployment::DeploymentException(
1197cdf0e10cSrcweir             // ought never occur...
1198cdf0e10cSrcweir             OUSTR("error while getting all deployed packages: ") + m_context,
1199cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1200cdf0e10cSrcweir     }
1201cdf0e10cSrcweir }
1202cdf0e10cSrcweir 
1203cdf0e10cSrcweir //______________________________________________________________________________
1204cdf0e10cSrcweir 
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir //ToDo: the function must not call registerPackage, do this in
1207cdf0e10cSrcweir //XExtensionManager.reinstallDeployedExtensions
1208cdf0e10cSrcweir void PackageManagerImpl::reinstallDeployedPackages(
1209cdf0e10cSrcweir     Reference<task::XAbortChannel> const &  /*xAbortChannel*/,
1210cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xCmdEnv_ )
1211cdf0e10cSrcweir     throw (deployment::DeploymentException,
1212cdf0e10cSrcweir            CommandFailedException, CommandAbortedException,
1213cdf0e10cSrcweir            lang::IllegalArgumentException, RuntimeException)
1214cdf0e10cSrcweir {
1215cdf0e10cSrcweir     check();
1216cdf0e10cSrcweir     if (office_is_running())
1217cdf0e10cSrcweir         throw RuntimeException(
1218cdf0e10cSrcweir             OUSTR("You must close any running Office process before "
1219cdf0e10cSrcweir                   "reinstalling packages!"), static_cast<OWeakObject *>(this) );
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir     Reference<XCommandEnvironment> xCmdEnv;
1222cdf0e10cSrcweir     if (m_xLogFile.is())
1223cdf0e10cSrcweir         xCmdEnv.set( new CmdEnvWrapperImpl( xCmdEnv_, m_xLogFile ) );
1224cdf0e10cSrcweir     else
1225cdf0e10cSrcweir         xCmdEnv.set( xCmdEnv_ );
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir     try {
1228cdf0e10cSrcweir         ProgressLevel progress(
1229cdf0e10cSrcweir             xCmdEnv, OUSTR("Reinstalling all deployed packages...") );
1230cdf0e10cSrcweir 
1231cdf0e10cSrcweir         try_dispose( m_xRegistry );
1232cdf0e10cSrcweir         m_xRegistry.clear();
1233cdf0e10cSrcweir         if (m_registryCache.getLength() > 0)
1234cdf0e10cSrcweir             erase_path( m_registryCache, xCmdEnv );
1235cdf0e10cSrcweir         initRegistryBackends();
1236cdf0e10cSrcweir         Reference<util::XUpdatable> xUpdatable( m_xRegistry, UNO_QUERY );
1237cdf0e10cSrcweir         if (xUpdatable.is())
1238cdf0e10cSrcweir             xUpdatable->update();
1239cdf0e10cSrcweir 
1240cdf0e10cSrcweir         //registering is done by the ExtensionManager service.
1241cdf0e10cSrcweir     }
1242cdf0e10cSrcweir     catch (RuntimeException &) {
1243cdf0e10cSrcweir         throw;
1244cdf0e10cSrcweir     }
1245cdf0e10cSrcweir     catch (CommandFailedException & exc) {
1246cdf0e10cSrcweir         logIntern( Any(exc) );
1247cdf0e10cSrcweir         throw;
1248cdf0e10cSrcweir     }
1249cdf0e10cSrcweir     catch (CommandAbortedException & exc) {
1250cdf0e10cSrcweir         logIntern( Any(exc) );
1251cdf0e10cSrcweir         throw;
1252cdf0e10cSrcweir     }
1253cdf0e10cSrcweir     catch (deployment::DeploymentException & exc) {
1254cdf0e10cSrcweir         logIntern( Any(exc) );
1255cdf0e10cSrcweir         throw;
1256cdf0e10cSrcweir     }
1257cdf0e10cSrcweir     catch (Exception &) {
1258cdf0e10cSrcweir         Any exc( ::cppu::getCaughtException() );
1259cdf0e10cSrcweir         logIntern( exc );
1260cdf0e10cSrcweir         throw deployment::DeploymentException(
1261cdf0e10cSrcweir             OUSTR("Error while reinstalling all previously deployed "
1262cdf0e10cSrcweir                   "packages of context ") + m_context,
1263cdf0e10cSrcweir             static_cast<OWeakObject *>(this), exc );
1264cdf0e10cSrcweir     }
1265cdf0e10cSrcweir }
1266cdf0e10cSrcweir 
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir ::sal_Bool SAL_CALL PackageManagerImpl::isReadOnly(  )
1269cdf0e10cSrcweir         throw (::com::sun::star::uno::RuntimeException)
1270cdf0e10cSrcweir {
1271cdf0e10cSrcweir     return m_readOnly;
1272cdf0e10cSrcweir }
1273cdf0e10cSrcweir bool PackageManagerImpl::synchronizeRemovedExtensions(
1274cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1275cdf0e10cSrcweir     Reference<css::ucb::XCommandEnvironment> const & xCmdEnv)
1276cdf0e10cSrcweir {
1277cdf0e10cSrcweir 
1278cdf0e10cSrcweir     //find all which are in the extension data base but which
1279cdf0e10cSrcweir     //are removed already.
1280cdf0e10cSrcweir     OSL_ASSERT(!m_context.equals(OUSTR("user")));
1281cdf0e10cSrcweir     bool bModified = false;
1282cdf0e10cSrcweir     ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir     typedef ActivePackages::Entries::const_iterator ITActive;
1285cdf0e10cSrcweir     bool bShared = m_context.equals(OUSTR("shared"));
1286cdf0e10cSrcweir 
1287cdf0e10cSrcweir     for (ITActive i = id2temp.begin(); i != id2temp.end(); i++)
1288cdf0e10cSrcweir     {
1289cdf0e10cSrcweir         try
1290cdf0e10cSrcweir         {
1291cdf0e10cSrcweir             //Get the URL to the extensions folder, first make the url for the
1292cdf0e10cSrcweir             //shared repository including the temporary name
1293cdf0e10cSrcweir             OUString url = makeURL(m_activePackages, i->second.temporaryName);
1294cdf0e10cSrcweir             if (bShared)
1295cdf0e10cSrcweir                 url = makeURLAppendSysPathSegment( url + OUSTR("_"), i->second.fileName);
1296cdf0e10cSrcweir 
1297cdf0e10cSrcweir             bool bRemoved = false;
1298cdf0e10cSrcweir             //Check if the URL to the extension is still the same
1299cdf0e10cSrcweir             ::ucbhelper::Content contentExtension;
1300cdf0e10cSrcweir 
1301cdf0e10cSrcweir             if (!create_ucb_content(
1302cdf0e10cSrcweir                     &contentExtension, url,
1303cdf0e10cSrcweir                     Reference<XCommandEnvironment>(), false))
1304cdf0e10cSrcweir             {
1305cdf0e10cSrcweir                 bRemoved = true;
1306cdf0e10cSrcweir             }
1307cdf0e10cSrcweir 
1308cdf0e10cSrcweir             //The folder is in the extension database, but it can still be deleted.
1309cdf0e10cSrcweir             //look for the xxx.tmpremoved file
1310cdf0e10cSrcweir             //There can also be the case that a different extension was installed
1311cdf0e10cSrcweir             //in a "temp" folder with name that is already used.
1312cdf0e10cSrcweir             if (!bRemoved && bShared)
1313cdf0e10cSrcweir             {
1314cdf0e10cSrcweir                 ::ucbhelper::Content contentRemoved;
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir                 if (create_ucb_content(
1317cdf0e10cSrcweir                         &contentRemoved,
1318cdf0e10cSrcweir                         m_activePackages_expanded + OUSTR("/") +
1319cdf0e10cSrcweir                         i->second.temporaryName + OUSTR("removed"),
1320cdf0e10cSrcweir                         Reference<XCommandEnvironment>(), false))
1321cdf0e10cSrcweir                 {
1322cdf0e10cSrcweir                     bRemoved = true;
1323cdf0e10cSrcweir                 }
1324cdf0e10cSrcweir             }
1325cdf0e10cSrcweir 
1326cdf0e10cSrcweir             if (!bRemoved)
1327cdf0e10cSrcweir             {
1328cdf0e10cSrcweir                 //There may be another extensions at the same place
1329cdf0e10cSrcweir                 dp_misc::DescriptionInfoset infoset =
1330cdf0e10cSrcweir                     dp_misc::getDescriptionInfoset(url);
1331cdf0e10cSrcweir                 OSL_ENSURE(infoset.hasDescription(),
1332cdf0e10cSrcweir                            "Extension Manager: bundled and shared extensions "
1333cdf0e10cSrcweir                            "must have an identifer and a version");
1334cdf0e10cSrcweir                 if (infoset.hasDescription() &&
1335cdf0e10cSrcweir                     infoset.getIdentifier() &&
1336cdf0e10cSrcweir                     (! i->first.equals(*(infoset.getIdentifier()))
1337cdf0e10cSrcweir                      || ! i->second.version.equals(infoset.getVersion())))
1338cdf0e10cSrcweir                 {
1339cdf0e10cSrcweir                     bRemoved = true;
1340cdf0e10cSrcweir                 }
1341cdf0e10cSrcweir 
1342cdf0e10cSrcweir             }
1343cdf0e10cSrcweir             if (bRemoved)
1344cdf0e10cSrcweir             {
1345cdf0e10cSrcweir                 Reference<deployment::XPackage> xPackage = m_xRegistry->bindPackage(
1346cdf0e10cSrcweir                     url, i->second.mediaType, true, i->first, xCmdEnv );
1347cdf0e10cSrcweir                 OSL_ASSERT(xPackage.is()); //Even if the files are removed, we must get the object.
1348cdf0e10cSrcweir                 xPackage->revokePackage(xAbortChannel, xCmdEnv);
1349cdf0e10cSrcweir                 removePackage(xPackage->getIdentifier().Value, xPackage->getName(),
1350cdf0e10cSrcweir                               xAbortChannel, xCmdEnv);
1351cdf0e10cSrcweir                 bModified |= true;
1352cdf0e10cSrcweir             }
1353cdf0e10cSrcweir         }
1354cdf0e10cSrcweir         catch( uno::Exception & )
1355cdf0e10cSrcweir         {
1356cdf0e10cSrcweir             OSL_ASSERT(0);
1357cdf0e10cSrcweir         }
1358cdf0e10cSrcweir     }
1359cdf0e10cSrcweir     return bModified;
1360cdf0e10cSrcweir }
1361cdf0e10cSrcweir 
1362cdf0e10cSrcweir 
1363cdf0e10cSrcweir bool PackageManagerImpl::synchronizeAddedExtensions(
1364cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1365cdf0e10cSrcweir     Reference<css::ucb::XCommandEnvironment> const & xCmdEnv)
1366cdf0e10cSrcweir {
1367cdf0e10cSrcweir     bool bModified = false;
1368cdf0e10cSrcweir     OSL_ASSERT(!m_context.equals(OUSTR("user")));
1369cdf0e10cSrcweir 
1370cdf0e10cSrcweir     ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1371cdf0e10cSrcweir     //check if the folder exist at all. The shared extension folder
1372cdf0e10cSrcweir     //may not exist for a normal user.
1373cdf0e10cSrcweir     if (!create_ucb_content(
1374cdf0e10cSrcweir             NULL, m_activePackages_expanded, Reference<css::ucb::XCommandEnvironment>(), false))
1375cdf0e10cSrcweir         return bModified;
1376cdf0e10cSrcweir     ::ucbhelper::Content tempFolder(
1377cdf0e10cSrcweir         m_activePackages_expanded, xCmdEnv );
1378cdf0e10cSrcweir 
1379cdf0e10cSrcweir     Reference<sdbc::XResultSet> xResultSet(
1380cdf0e10cSrcweir         tempFolder.createCursor(
1381cdf0e10cSrcweir             Sequence<OUString>( &StrTitle::get(), 1 ),
1382cdf0e10cSrcweir             ::ucbhelper::INCLUDE_FOLDERS_ONLY ) );
1383cdf0e10cSrcweir 
1384cdf0e10cSrcweir     while (xResultSet->next())
1385cdf0e10cSrcweir     {
1386cdf0e10cSrcweir         try
1387cdf0e10cSrcweir         {
1388cdf0e10cSrcweir             OUString title(
1389cdf0e10cSrcweir                 Reference<sdbc::XRow>(
1390cdf0e10cSrcweir                     xResultSet, UNO_QUERY_THROW )->getString(
1391cdf0e10cSrcweir                         1 /* Title */ ) );
1392cdf0e10cSrcweir             //The temporary folders of user and shared have an '_' at then end.
1393cdf0e10cSrcweir             //But the name in ActivePackages.temporaryName is saved without.
1394cdf0e10cSrcweir             OUString title2 = title;
1395cdf0e10cSrcweir             bool bShared = m_context.equals(OUSTR("shared"));
1396cdf0e10cSrcweir             if (bShared)
1397cdf0e10cSrcweir             {
1398cdf0e10cSrcweir                 OSL_ASSERT(title2[title2.getLength() -1] == '_');
1399cdf0e10cSrcweir                 title2 = title2.copy(0, title2.getLength() -1);
1400cdf0e10cSrcweir             }
1401cdf0e10cSrcweir             OUString titleEncoded =  ::rtl::Uri::encode(
1402cdf0e10cSrcweir                 title2, rtl_UriCharClassPchar,
1403cdf0e10cSrcweir                 rtl_UriEncodeIgnoreEscapes,
1404cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8);
1405cdf0e10cSrcweir 
1406cdf0e10cSrcweir             //It it sufficient to check for the folder name, because when the administor
1407cdf0e10cSrcweir             //installed the extension it was already checked if there is one with the
1408cdf0e10cSrcweir             //same identifier.
1409cdf0e10cSrcweir             const MatchTempDir match(titleEncoded);
1410cdf0e10cSrcweir             if (::std::find_if( id2temp.begin(), id2temp.end(), match ) ==
1411cdf0e10cSrcweir                 id2temp.end())
1412cdf0e10cSrcweir             {
1413cdf0e10cSrcweir 
1414cdf0e10cSrcweir                 // The folder was not found in the data base, so it must be
1415cdf0e10cSrcweir                 // an added extension
1416cdf0e10cSrcweir                 OUString url(m_activePackages_expanded + OUSTR("/") + titleEncoded);
1417cdf0e10cSrcweir                 OUString sExtFolder;
1418cdf0e10cSrcweir                 if (bShared) //that is, shared
1419cdf0e10cSrcweir                 {
1420cdf0e10cSrcweir                     //Check if the extension was not "deleted" already which is indicated
1421cdf0e10cSrcweir                     //by a xxx.tmpremoved file
1422cdf0e10cSrcweir                     ::ucbhelper::Content contentRemoved;
1423cdf0e10cSrcweir                     if (create_ucb_content(&contentRemoved, url + OUSTR("removed"),
1424cdf0e10cSrcweir                                            Reference<XCommandEnvironment>(), false))
1425cdf0e10cSrcweir                         continue;
1426cdf0e10cSrcweir                     sExtFolder = getExtensionFolder(
1427cdf0e10cSrcweir                         m_activePackages_expanded +
1428cdf0e10cSrcweir                         OUString(OUSTR("/")) + titleEncoded + OUSTR("_"), xCmdEnv);
1429cdf0e10cSrcweir                     url = makeURLAppendSysPathSegment(m_activePackages_expanded, title);
1430cdf0e10cSrcweir                     url = makeURLAppendSysPathSegment(url, sExtFolder);
1431cdf0e10cSrcweir                 }
1432cdf0e10cSrcweir                 Reference<deployment::XPackage> xPackage = m_xRegistry->bindPackage(
1433cdf0e10cSrcweir                     url, OUString(), false, OUString(), xCmdEnv );
1434cdf0e10cSrcweir                 if (xPackage.is())
1435cdf0e10cSrcweir                 {
1436cdf0e10cSrcweir                     //Prepare the database entry
1437cdf0e10cSrcweir                     ActivePackages::Data dbData;
1438cdf0e10cSrcweir 
1439cdf0e10cSrcweir                     dbData.temporaryName = titleEncoded;
1440cdf0e10cSrcweir                     if (bShared)
1441cdf0e10cSrcweir                         dbData.fileName = sExtFolder;
1442cdf0e10cSrcweir                     else
1443cdf0e10cSrcweir                         dbData.fileName = title;
1444cdf0e10cSrcweir                     dbData.mediaType = xPackage->getPackageType()->getMediaType();
1445cdf0e10cSrcweir                     dbData.version = xPackage->getVersion();
1446cdf0e10cSrcweir                     OSL_ENSURE(dbData.version.getLength() > 0,
1447cdf0e10cSrcweir                                "Extension Manager: bundled and shared extensions must have "
1448cdf0e10cSrcweir                                "an identifier and a version");
1449cdf0e10cSrcweir 
1450cdf0e10cSrcweir                     OUString id = dp_misc::getIdentifier( xPackage );
1451cdf0e10cSrcweir 
1452cdf0e10cSrcweir                     //We provide a special command environment that will prevent
1453cdf0e10cSrcweir                     //showing a license if simple-licens/@accept-by = "admin"
1454cdf0e10cSrcweir                     //It will also prevent showing the license for bundled extensions
1455cdf0e10cSrcweir                     //which is not supported.
1456cdf0e10cSrcweir                     OSL_ASSERT(!m_context.equals(OUSTR("user")));
1457cdf0e10cSrcweir 
1458cdf0e10cSrcweir                     // shall the license be suppressed?
1459cdf0e10cSrcweir                     DescriptionInfoset info =
1460cdf0e10cSrcweir                         dp_misc::getDescriptionInfoset(url);
1461cdf0e10cSrcweir                     ::boost::optional<dp_misc::SimpleLicenseAttributes>
1462cdf0e10cSrcweir                           attr = info.getSimpleLicenseAttributes();
1463cdf0e10cSrcweir                     ExtensionProperties props(url,xCmdEnv);
1464cdf0e10cSrcweir                     bool bNoLicense = false;
1465cdf0e10cSrcweir                     if (attr && attr->suppressIfRequired && props.isSuppressedLicense())
1466cdf0e10cSrcweir                         bNoLicense = true;
1467cdf0e10cSrcweir 
1468cdf0e10cSrcweir                     Reference<ucb::XCommandEnvironment> licCmdEnv(
1469cdf0e10cSrcweir                         new LicenseCommandEnv(xCmdEnv->getInteractionHandler(),
1470cdf0e10cSrcweir                                               bNoLicense, m_context));
1471cdf0e10cSrcweir                     sal_Int32 failedPrereq = xPackage->checkPrerequisites(
1472cdf0e10cSrcweir                         xAbortChannel, licCmdEnv, false);
1473cdf0e10cSrcweir                     //Remember that this failed. For example, the user
1474cdf0e10cSrcweir                     //could have declined the license. Then the next time the
1475cdf0e10cSrcweir                     //extension folder is investigated we do not want to
1476cdf0e10cSrcweir                     //try to install the extension again.
1477cdf0e10cSrcweir                     dbData.failedPrerequisites = OUString::valueOf(failedPrereq);
1478cdf0e10cSrcweir                     insertToActivationLayerDB(id, dbData);
1479cdf0e10cSrcweir                     bModified |= true;
1480cdf0e10cSrcweir                 }
1481cdf0e10cSrcweir             }
1482cdf0e10cSrcweir         }
1483cdf0e10cSrcweir         catch (uno::Exception &)
1484cdf0e10cSrcweir         {
1485cdf0e10cSrcweir             OSL_ASSERT(0);
1486cdf0e10cSrcweir         }
1487cdf0e10cSrcweir     }
1488cdf0e10cSrcweir     return bModified;
1489cdf0e10cSrcweir }
1490cdf0e10cSrcweir 
1491cdf0e10cSrcweir sal_Bool PackageManagerImpl::synchronize(
1492cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1493cdf0e10cSrcweir     Reference<css::ucb::XCommandEnvironment> const & xCmdEnv)
1494cdf0e10cSrcweir     throw (css::deployment::DeploymentException,
1495cdf0e10cSrcweir            css::ucb::CommandFailedException,
1496cdf0e10cSrcweir            css::ucb::CommandAbortedException,
1497cdf0e10cSrcweir            css::uno::RuntimeException)
1498cdf0e10cSrcweir {
1499cdf0e10cSrcweir     check();
1500cdf0e10cSrcweir     bool bModified = false;
1501cdf0e10cSrcweir     if (m_context.equals(OUSTR("user")))
1502cdf0e10cSrcweir         return bModified;
1503cdf0e10cSrcweir     bModified |=
1504cdf0e10cSrcweir         synchronizeRemovedExtensions(xAbortChannel, xCmdEnv);
1505cdf0e10cSrcweir     bModified |= synchronizeAddedExtensions(xAbortChannel, xCmdEnv);
1506cdf0e10cSrcweir 
1507cdf0e10cSrcweir     return bModified;
1508cdf0e10cSrcweir }
1509cdf0e10cSrcweir 
1510cdf0e10cSrcweir Sequence< Reference<deployment::XPackage> > PackageManagerImpl::getExtensionsWithUnacceptedLicenses(
1511cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv)
1512cdf0e10cSrcweir     throw (deployment::DeploymentException, RuntimeException)
1513cdf0e10cSrcweir {
1514cdf0e10cSrcweir     ::std::vector<Reference<deployment::XPackage> > vec;
1515cdf0e10cSrcweir 
1516cdf0e10cSrcweir     try
1517cdf0e10cSrcweir     {
1518cdf0e10cSrcweir         const ::osl::MutexGuard guard( getMutex() );
1519cdf0e10cSrcweir         // clean up activation layer, scan for zombie temp dirs:
1520cdf0e10cSrcweir         ActivePackages::Entries id2temp( m_activePackagesDB->getEntries() );
1521cdf0e10cSrcweir 
1522cdf0e10cSrcweir         ActivePackages::Entries::const_iterator i = id2temp.begin();
1523cdf0e10cSrcweir         bool bShared = m_context.equals(OUSTR("shared"));
1524cdf0e10cSrcweir 
1525cdf0e10cSrcweir         for (; i != id2temp.end(); i++ )
1526cdf0e10cSrcweir         {
1527cdf0e10cSrcweir             //Get the database entry
1528cdf0e10cSrcweir             ActivePackages::Data const & dbData = i->second;
1529cdf0e10cSrcweir             sal_Int32 failedPrereq = dbData.failedPrerequisites.toInt32();
1530cdf0e10cSrcweir             //If the installation failed for other reason then the license then we
1531cdf0e10cSrcweir             //ignore it.
1532cdf0e10cSrcweir             if (failedPrereq ^= deployment::Prerequisites::LICENSE)
1533cdf0e10cSrcweir                 continue;
1534cdf0e10cSrcweir 
1535cdf0e10cSrcweir             //Prepare the URL to the extension
1536cdf0e10cSrcweir             OUString url = makeURL(m_activePackages, i->second.temporaryName);
1537cdf0e10cSrcweir             if (bShared)
1538cdf0e10cSrcweir                 url = makeURLAppendSysPathSegment( url + OUSTR("_"), i->second.fileName);
1539cdf0e10cSrcweir 
1540cdf0e10cSrcweir             Reference<deployment::XPackage> p = m_xRegistry->bindPackage(
1541cdf0e10cSrcweir                 url, OUString(), false, OUString(), xCmdEnv );
1542cdf0e10cSrcweir 
1543cdf0e10cSrcweir             if (p.is())
1544cdf0e10cSrcweir                 vec.push_back(p);
1545cdf0e10cSrcweir 
1546cdf0e10cSrcweir         }
1547cdf0e10cSrcweir         return ::comphelper::containerToSequence(vec);
1548cdf0e10cSrcweir     }
1549cdf0e10cSrcweir     catch (deployment::DeploymentException &)
1550cdf0e10cSrcweir     {
1551cdf0e10cSrcweir         throw;
1552cdf0e10cSrcweir     }
1553cdf0e10cSrcweir     catch (RuntimeException&)
1554cdf0e10cSrcweir     {
1555cdf0e10cSrcweir         throw;
1556cdf0e10cSrcweir     }
1557cdf0e10cSrcweir     catch (...)
1558cdf0e10cSrcweir     {
1559cdf0e10cSrcweir         Any exc = ::cppu::getCaughtException();
1560cdf0e10cSrcweir         deployment::DeploymentException de(
1561cdf0e10cSrcweir             OUSTR("PackageManagerImpl::getExtensionsWithUnacceptedLicenses"),
1562cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1563cdf0e10cSrcweir         exc <<= de;
1564cdf0e10cSrcweir         ::cppu::throwException(exc);
1565cdf0e10cSrcweir     }
1566cdf0e10cSrcweir 
1567cdf0e10cSrcweir     return ::comphelper::containerToSequence(vec);
1568cdf0e10cSrcweir }
1569cdf0e10cSrcweir 
1570cdf0e10cSrcweir sal_Int32 PackageManagerImpl::checkPrerequisites(
1571cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackage> const & extension,
1572cdf0e10cSrcweir     css::uno::Reference<css::task::XAbortChannel> const & xAbortChannel,
1573cdf0e10cSrcweir     css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
1574cdf0e10cSrcweir     throw (css::deployment::DeploymentException,
1575cdf0e10cSrcweir            css::ucb::CommandFailedException,
1576cdf0e10cSrcweir            css::ucb::CommandAbortedException,
1577cdf0e10cSrcweir            css::lang::IllegalArgumentException,
1578cdf0e10cSrcweir            css::uno::RuntimeException)
1579cdf0e10cSrcweir {
1580cdf0e10cSrcweir     try
1581cdf0e10cSrcweir     {
1582cdf0e10cSrcweir         if (!extension.is())
1583cdf0e10cSrcweir             return 0;
1584cdf0e10cSrcweir         if (!m_context.equals(extension->getRepositoryName()))
1585cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1586cdf0e10cSrcweir                 OUSTR("PackageManagerImpl::checkPrerequisites: extension is not"
1587cdf0e10cSrcweir                       " from this repository."), 0, 0);
1588cdf0e10cSrcweir 
1589cdf0e10cSrcweir         ActivePackages::Data dbData;
1590cdf0e10cSrcweir         OUString id = dp_misc::getIdentifier(extension);
1591cdf0e10cSrcweir         if (m_activePackagesDB->get( &dbData, id, OUString()))
1592cdf0e10cSrcweir         {
1593cdf0e10cSrcweir             //If the license was already displayed, then do not show it again
1594cdf0e10cSrcweir             Reference<ucb::XCommandEnvironment> _xCmdEnv = xCmdEnv;
1595cdf0e10cSrcweir             sal_Int32 prereq = dbData.failedPrerequisites.toInt32();
1596cdf0e10cSrcweir             if ( !(prereq & deployment::Prerequisites::LICENSE))
1597cdf0e10cSrcweir                 _xCmdEnv = new NoLicenseCommandEnv(xCmdEnv->getInteractionHandler());
1598cdf0e10cSrcweir 
1599cdf0e10cSrcweir             sal_Int32 failedPrereq = extension->checkPrerequisites(
1600cdf0e10cSrcweir                 xAbortChannel, _xCmdEnv, false);
1601cdf0e10cSrcweir             dbData.failedPrerequisites = OUString::valueOf(failedPrereq);
1602cdf0e10cSrcweir             insertToActivationLayerDB(id, dbData);
1603cdf0e10cSrcweir         }
1604cdf0e10cSrcweir         else
1605cdf0e10cSrcweir         {
1606cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1607cdf0e10cSrcweir                 OUSTR("PackageManagerImpl::checkPrerequisites: unknown extension"),
1608cdf0e10cSrcweir                 0, 0);
1609cdf0e10cSrcweir 
1610cdf0e10cSrcweir         }
1611cdf0e10cSrcweir         return 0;
1612cdf0e10cSrcweir     }
1613cdf0e10cSrcweir     catch (deployment::DeploymentException& ) {
1614cdf0e10cSrcweir         throw;
1615cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1616cdf0e10cSrcweir         throw;
1617cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1618cdf0e10cSrcweir         throw;
1619cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1620cdf0e10cSrcweir         throw;
1621cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1622cdf0e10cSrcweir         throw;
1623cdf0e10cSrcweir     } catch (...) {
1624cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
1625cdf0e10cSrcweir         deployment::DeploymentException exc(
1626cdf0e10cSrcweir             OUSTR("PackageManagerImpl::checkPrerequisites: exception "),
1627cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1628cdf0e10cSrcweir         throw exc;
1629cdf0e10cSrcweir     }
1630cdf0e10cSrcweir }
1631cdf0e10cSrcweir 
1632cdf0e10cSrcweir //##############################################################################
1633cdf0e10cSrcweir 
1634cdf0e10cSrcweir //______________________________________________________________________________
1635cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::~CmdEnvWrapperImpl()
1636cdf0e10cSrcweir {
1637cdf0e10cSrcweir }
1638cdf0e10cSrcweir 
1639cdf0e10cSrcweir //______________________________________________________________________________
1640cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::CmdEnvWrapperImpl(
1641cdf0e10cSrcweir     Reference<XCommandEnvironment> const & xUserCmdEnv,
1642cdf0e10cSrcweir     Reference<XProgressHandler> const & xLogFile )
1643cdf0e10cSrcweir     : m_xLogFile( xLogFile )
1644cdf0e10cSrcweir {
1645cdf0e10cSrcweir     if (xUserCmdEnv.is()) {
1646cdf0e10cSrcweir         m_xUserProgress.set( xUserCmdEnv->getProgressHandler() );
1647cdf0e10cSrcweir         m_xUserInteractionHandler.set( xUserCmdEnv->getInteractionHandler() );
1648cdf0e10cSrcweir     }
1649cdf0e10cSrcweir }
1650cdf0e10cSrcweir 
1651cdf0e10cSrcweir // XCommandEnvironment
1652cdf0e10cSrcweir //______________________________________________________________________________
1653cdf0e10cSrcweir Reference<task::XInteractionHandler>
1654cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::getInteractionHandler()
1655cdf0e10cSrcweir     throw (RuntimeException)
1656cdf0e10cSrcweir {
1657cdf0e10cSrcweir     return m_xUserInteractionHandler;
1658cdf0e10cSrcweir }
1659cdf0e10cSrcweir 
1660cdf0e10cSrcweir //______________________________________________________________________________
1661cdf0e10cSrcweir Reference<XProgressHandler>
1662cdf0e10cSrcweir PackageManagerImpl::CmdEnvWrapperImpl::getProgressHandler()
1663cdf0e10cSrcweir     throw (RuntimeException)
1664cdf0e10cSrcweir {
1665cdf0e10cSrcweir     return this;
1666cdf0e10cSrcweir }
1667cdf0e10cSrcweir 
1668cdf0e10cSrcweir // XProgressHandler
1669cdf0e10cSrcweir //______________________________________________________________________________
1670cdf0e10cSrcweir void PackageManagerImpl::CmdEnvWrapperImpl::push( Any const & Status )
1671cdf0e10cSrcweir     throw (RuntimeException)
1672cdf0e10cSrcweir {
1673cdf0e10cSrcweir     if (m_xLogFile.is())
1674cdf0e10cSrcweir         m_xLogFile->push( Status );
1675cdf0e10cSrcweir     if (m_xUserProgress.is())
1676cdf0e10cSrcweir         m_xUserProgress->push( Status );
1677cdf0e10cSrcweir }
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir //______________________________________________________________________________
1680cdf0e10cSrcweir void PackageManagerImpl::CmdEnvWrapperImpl::update( Any const & Status )
1681cdf0e10cSrcweir     throw (RuntimeException)
1682cdf0e10cSrcweir {
1683cdf0e10cSrcweir     if (m_xLogFile.is())
1684cdf0e10cSrcweir         m_xLogFile->update( Status );
1685cdf0e10cSrcweir     if (m_xUserProgress.is())
1686cdf0e10cSrcweir         m_xUserProgress->update( Status );
1687cdf0e10cSrcweir }
1688cdf0e10cSrcweir 
1689cdf0e10cSrcweir //______________________________________________________________________________
1690cdf0e10cSrcweir void PackageManagerImpl::CmdEnvWrapperImpl::pop() throw (RuntimeException)
1691cdf0e10cSrcweir {
1692cdf0e10cSrcweir     if (m_xLogFile.is())
1693cdf0e10cSrcweir         m_xLogFile->pop();
1694cdf0e10cSrcweir     if (m_xUserProgress.is())
1695cdf0e10cSrcweir         m_xUserProgress->pop();
1696cdf0e10cSrcweir }
1697cdf0e10cSrcweir 
1698cdf0e10cSrcweir } // namespace dp_manager
1699cdf0e10cSrcweir 
1700