12722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52722ceddSAndrew Rist  * distributed with this work for additional information
62722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
92722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
102722ceddSAndrew Rist  *
112722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122722ceddSAndrew Rist  *
132722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142722ceddSAndrew Rist  * software distributed under the License is distributed on an
152722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
172722ceddSAndrew Rist  * specific language governing permissions and limitations
182722ceddSAndrew Rist  * under the License.
192722ceddSAndrew Rist  *
202722ceddSAndrew Rist  *************************************************************/
212722ceddSAndrew Rist 
222722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "oo3extensionmigration.hxx"
28cdf0e10cSrcweir #include <rtl/instance.hxx>
29cdf0e10cSrcweir #include <osl/file.hxx>
30cdf0e10cSrcweir #include <osl/thread.h>
31cdf0e10cSrcweir #include <tools/urlobj.hxx>
32cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
33cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
34cdf0e10cSrcweir #include <unotools/textsearch.hxx>
35cdf0e10cSrcweir #include <comphelper/sequence.hxx>
36cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
37cdf0e10cSrcweir #include <ucbhelper/content.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <com/sun/star/task/XInteractionApprove.hpp>
40cdf0e10cSrcweir #include <com/sun/star/task/XInteractionAbort.hpp>
41cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandInfo.hpp>
42cdf0e10cSrcweir #include <com/sun/star/ucb/TransferInfo.hpp>
43cdf0e10cSrcweir #include <com/sun/star/ucb/NameClash.hpp>
44cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
45cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
46cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
47cdf0e10cSrcweir #include <com/sun/star/deployment/ExtensionManager.hpp>
48cdf0e10cSrcweir 
495e679a6cSOliver-Rainer Wittmann #include <com/sun/star/deployment/VersionException.hpp>
505e679a6cSOliver-Rainer Wittmann #include <dp_gui_handleversionexception.hxx>
51*13443bc8SOliver-Rainer Wittmann #include <com/sun/star/deployment/DeploymentException.hpp>
52fd6d8138SAndre Fischer 
53cdf0e10cSrcweir using namespace ::com::sun::star;
54cdf0e10cSrcweir using namespace ::com::sun::star::uno;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace migration
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 
59cdf0e10cSrcweir static ::rtl::OUString sExtensionSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/uno_packages/" ) );
60cdf0e10cSrcweir static ::rtl::OUString sSubDirName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "cache" ) );
61cdf0e10cSrcweir static ::rtl::OUString sConfigDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/registry/data" ) );
62cdf0e10cSrcweir static ::rtl::OUString sOrgDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/registry/data/org" ) );
63cdf0e10cSrcweir static ::rtl::OUString sExcludeDir1 = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/registry/data/org" ) );
64cdf0e10cSrcweir static ::rtl::OUString sExcludeDir2 = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/registry/data/org/openoffice" ) );
65cdf0e10cSrcweir static ::rtl::OUString sDescriptionXmlFile = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/description.xml" ) );
66cdf0e10cSrcweir static ::rtl::OUString sExtensionRootSubDirName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/uno_packages" ) );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir static ::rtl::OUString sConfigurationDataType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("application/vnd.sun.star.configuration-data"));
69cdf0e10cSrcweir static ::rtl::OUString sConfigurationSchemaType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("application/vnd.sun.star.configuration-schema"));
70cdf0e10cSrcweir 
71cdf0e10cSrcweir // =============================================================================
72cdf0e10cSrcweir // component operations
73cdf0e10cSrcweir // =============================================================================
74cdf0e10cSrcweir 
75cdf0e10cSrcweir ::rtl::OUString OO3ExtensionMigration_getImplementationName()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     static ::rtl::OUString* pImplName = 0;
78cdf0e10cSrcweir     if ( !pImplName )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
81cdf0e10cSrcweir         if ( !pImplName )
82cdf0e10cSrcweir 	    {
83cdf0e10cSrcweir             static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.OOo3Extensions" ) );
84cdf0e10cSrcweir 		    pImplName = &aImplName;
85cdf0e10cSrcweir 	    }
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir     return *pImplName;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir // -----------------------------------------------------------------------------
91cdf0e10cSrcweir 
92cdf0e10cSrcweir Sequence< ::rtl::OUString > OO3ExtensionMigration_getSupportedServiceNames()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     static Sequence< ::rtl::OUString >* pNames = 0;
95cdf0e10cSrcweir     if ( !pNames )
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
98cdf0e10cSrcweir 	    if ( !pNames )
99cdf0e10cSrcweir 	    {
100cdf0e10cSrcweir             static Sequence< ::rtl::OUString > aNames(1);
101cdf0e10cSrcweir             aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Extensions" ) );
102cdf0e10cSrcweir             pNames = &aNames;
103cdf0e10cSrcweir 	    }
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir     return *pNames;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir // =============================================================================
109cdf0e10cSrcweir // ExtensionMigration
110cdf0e10cSrcweir // =============================================================================
111cdf0e10cSrcweir 
112cdf0e10cSrcweir OO3ExtensionMigration::OO3ExtensionMigration(Reference< XComponentContext > const & ctx) :
113cdf0e10cSrcweir m_ctx(ctx)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir // -----------------------------------------------------------------------------
118cdf0e10cSrcweir 
119cdf0e10cSrcweir OO3ExtensionMigration::~OO3ExtensionMigration()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir ::osl::FileBase::RC OO3ExtensionMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
126cdf0e10cSrcweir     if ( aResult == ::osl::FileBase::E_NOENT )
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         INetURLObject aBaseURL( rDirURL );
129cdf0e10cSrcweir         aBaseURL.removeSegment();
130cdf0e10cSrcweir         checkAndCreateDirectory( aBaseURL );
131cdf0e10cSrcweir         return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir     else
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         return aResult;
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir void OO3ExtensionMigration::scanUserExtensions( const ::rtl::OUString& sSourceDir, TStringVector& aMigrateExtensions )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     osl::Directory    aScanRootDir( sSourceDir );
142cdf0e10cSrcweir     osl::FileStatus   fs(FileStatusMask_Type | FileStatusMask_FileURL);
143cdf0e10cSrcweir     osl::FileBase::RC nRetCode = aScanRootDir.open();
144cdf0e10cSrcweir     if ( nRetCode == osl::Directory::E_None )
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         sal_uInt32    nHint( 0 );
147cdf0e10cSrcweir         osl::DirectoryItem aItem;
148cdf0e10cSrcweir         while ( aScanRootDir.getNextItem( aItem, nHint ) == osl::Directory::E_None )
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             if (( aItem.getFileStatus(fs) == osl::FileBase::E_None ) &&
151cdf0e10cSrcweir                 ( fs.getFileType() == osl::FileStatus::Directory   ))
152cdf0e10cSrcweir             {
153cdf0e10cSrcweir                 //Check next folder as the "real" extension folder is below a temp folder!
154cdf0e10cSrcweir                 ::rtl::OUString sExtensionFolderURL = fs.getFileURL();
155cdf0e10cSrcweir 
156cdf0e10cSrcweir                 osl::DirectoryItem aExtDirItem;
157cdf0e10cSrcweir                 osl::Directory     aExtensionRootDir( sExtensionFolderURL );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                 nRetCode = aExtensionRootDir.open();
160cdf0e10cSrcweir                 if (( nRetCode == osl::Directory::E_None ) &&
161cdf0e10cSrcweir                     ( aExtensionRootDir.getNextItem( aExtDirItem, nHint ) == osl::Directory::E_None ))
162cdf0e10cSrcweir                 {
163cdf0e10cSrcweir                     bool bFileStatus = aExtDirItem.getFileStatus(fs) == osl::FileBase::E_None;
164cdf0e10cSrcweir                     bool bIsDir      = fs.getFileType() == osl::FileStatus::Directory;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir                     if ( bFileStatus && bIsDir )
167cdf0e10cSrcweir                     {
168cdf0e10cSrcweir                         sExtensionFolderURL = fs.getFileURL();
169cdf0e10cSrcweir                         ScanResult eResult = scanExtensionFolder( sExtensionFolderURL );
170cdf0e10cSrcweir                         if ( eResult == SCANRESULT_MIGRATE_EXTENSION )
171cdf0e10cSrcweir                             aMigrateExtensions.push_back( sExtensionFolderURL );
172cdf0e10cSrcweir                     }
173cdf0e10cSrcweir                 }
174cdf0e10cSrcweir             }
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir OO3ExtensionMigration::ScanResult OO3ExtensionMigration::scanExtensionFolder( const ::rtl::OUString& sExtFolder )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir     ScanResult     aResult = SCANRESULT_NOTFOUND;
182cdf0e10cSrcweir     osl::Directory aDir(sExtFolder);
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     // get sub dirs
185cdf0e10cSrcweir     if (aDir.open() == osl::FileBase::E_None)
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         // work through directory contents...
188cdf0e10cSrcweir         osl::DirectoryItem item;
189cdf0e10cSrcweir         osl::FileStatus fs(FileStatusMask_Type | FileStatusMask_FileURL);
190cdf0e10cSrcweir         TStringVector aDirectories;
191cdf0e10cSrcweir         while ((aDir.getNextItem(item) == osl::FileBase::E_None ) &&
192cdf0e10cSrcweir                ( aResult == SCANRESULT_NOTFOUND ))
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir             if (item.getFileStatus(fs) == osl::FileBase::E_None)
195cdf0e10cSrcweir             {
196cdf0e10cSrcweir                 ::rtl::OUString aDirEntryURL;
197cdf0e10cSrcweir                 if (fs.getFileType() == osl::FileStatus::Directory)
198cdf0e10cSrcweir                     aDirectories.push_back( fs.getFileURL() );
199cdf0e10cSrcweir                 else
200cdf0e10cSrcweir                 {
201cdf0e10cSrcweir                     aDirEntryURL = fs.getFileURL();
202cdf0e10cSrcweir                     if ( aDirEntryURL.indexOf( sDescriptionXmlFile ) > 0 )
203cdf0e10cSrcweir                         aResult = scanDescriptionXml( aDirEntryURL ) ? SCANRESULT_MIGRATE_EXTENSION : SCANRESULT_DONTMIGRATE_EXTENSION;
204cdf0e10cSrcweir                 }
205cdf0e10cSrcweir             }
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         TStringVector::const_iterator pIter = aDirectories.begin();
209cdf0e10cSrcweir         while ( pIter != aDirectories.end() && aResult == SCANRESULT_NOTFOUND )
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             aResult = scanExtensionFolder( *pIter );
212cdf0e10cSrcweir             ++pIter;
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir     return aResult;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir bool OO3ExtensionMigration::scanDescriptionXml( const ::rtl::OUString& sDescriptionXmlURL )
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     if ( !m_xDocBuilder.is() )
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         m_xDocBuilder = uno::Reference< xml::dom::XDocumentBuilder >(
223cdf0e10cSrcweir             m_ctx->getServiceManager()->createInstanceWithContext(
224cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.dom.DocumentBuilder")),
225cdf0e10cSrcweir                 m_ctx ), uno::UNO_QUERY );
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     if ( !m_xSimpleFileAccess.is() )
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         m_xSimpleFileAccess = uno::Reference< ucb::XSimpleFileAccess >(
231cdf0e10cSrcweir             m_ctx->getServiceManager()->createInstanceWithContext(
232cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.SimpleFileAccess")),
233cdf0e10cSrcweir                 m_ctx ), uno::UNO_QUERY );
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     ::rtl::OUString aExtIdentifier;
237cdf0e10cSrcweir     if ( m_xDocBuilder.is() && m_xSimpleFileAccess.is() )
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir         try
240cdf0e10cSrcweir         {
241cdf0e10cSrcweir             uno::Reference< io::XInputStream > xIn =
242cdf0e10cSrcweir                 m_xSimpleFileAccess->openFileRead( sDescriptionXmlURL );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir             if ( xIn.is() )
245cdf0e10cSrcweir             {
246cdf0e10cSrcweir                 uno::Reference< xml::dom::XDocument > xDoc = m_xDocBuilder->parse( xIn );
247cdf0e10cSrcweir                 if ( xDoc.is() )
248cdf0e10cSrcweir                 {
249cdf0e10cSrcweir                     uno::Reference< xml::dom::XElement > xRoot = xDoc->getDocumentElement();
250cdf0e10cSrcweir                     if ( xRoot.is() &&
251cdf0e10cSrcweir                          xRoot->getTagName().equals(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("description"))) )
252cdf0e10cSrcweir                     {
253cdf0e10cSrcweir                         uno::Reference< xml::xpath::XXPathAPI > xPath(
254cdf0e10cSrcweir                             m_ctx->getServiceManager()->createInstanceWithContext(
255cdf0e10cSrcweir                                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.xpath.XPathAPI")),
256cdf0e10cSrcweir                                 m_ctx),
257cdf0e10cSrcweir                             uno::UNO_QUERY);
258cdf0e10cSrcweir 
259cdf0e10cSrcweir                         xPath->registerNS(
260cdf0e10cSrcweir                             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("desc")),
261cdf0e10cSrcweir                             xRoot->getNamespaceURI());
262cdf0e10cSrcweir                         xPath->registerNS(
263cdf0e10cSrcweir                             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xlink")),
264cdf0e10cSrcweir                             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("http://www.w3.org/1999/xlink")));
265cdf0e10cSrcweir 
266cdf0e10cSrcweir                         try
267cdf0e10cSrcweir                         {
268cdf0e10cSrcweir                             uno::Reference< xml::dom::XNode > xRootNode( xRoot, uno::UNO_QUERY );
269cdf0e10cSrcweir                             uno::Reference< xml::dom::XNode > xNode(
270cdf0e10cSrcweir                                 xPath->selectSingleNode(
271cdf0e10cSrcweir                                     xRootNode,
272cdf0e10cSrcweir                                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("desc:identifier/@value")) ));
273cdf0e10cSrcweir                             if ( xNode.is() )
274cdf0e10cSrcweir                                 aExtIdentifier = xNode->getNodeValue();
275cdf0e10cSrcweir                         }
276cdf0e10cSrcweir                         catch ( xml::xpath::XPathException& )
277cdf0e10cSrcweir                         {
278cdf0e10cSrcweir                         }
279cdf0e10cSrcweir                         catch ( xml::dom::DOMException& )
280cdf0e10cSrcweir                         {
281cdf0e10cSrcweir                         }
282cdf0e10cSrcweir                     }
283cdf0e10cSrcweir                 }
284cdf0e10cSrcweir             }
285cdf0e10cSrcweir 
286cdf0e10cSrcweir             if ( aExtIdentifier.getLength() > 0 )
287cdf0e10cSrcweir             {
288cdf0e10cSrcweir                 // scan extension identifier and try to match with our black list entries
289cdf0e10cSrcweir                 for ( sal_uInt32 i = 0; i < m_aBlackList.size(); i++ )
290cdf0e10cSrcweir                 {
291cdf0e10cSrcweir                     utl::SearchParam param(m_aBlackList[i], utl::SearchParam::SRCH_REGEXP);
292cdf0e10cSrcweir                     utl::TextSearch  ts(param, LANGUAGE_DONTKNOW);
293cdf0e10cSrcweir 
294cdf0e10cSrcweir                     xub_StrLen start = 0;
295cdf0e10cSrcweir                     xub_StrLen end   = static_cast<sal_uInt16>(aExtIdentifier.getLength());
296cdf0e10cSrcweir                     if (ts.SearchFrwrd(aExtIdentifier, &start, &end))
297cdf0e10cSrcweir                         return false;
298cdf0e10cSrcweir                 }
299cdf0e10cSrcweir             }
300cdf0e10cSrcweir         }
301cdf0e10cSrcweir         catch ( ucb::CommandAbortedException& )
302cdf0e10cSrcweir         {
303cdf0e10cSrcweir         }
304cdf0e10cSrcweir         catch ( uno::RuntimeException& )
305cdf0e10cSrcweir         {
306cdf0e10cSrcweir         }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir         if ( aExtIdentifier.getLength() == 0 )
309cdf0e10cSrcweir         {
310cdf0e10cSrcweir             // Fallback:
311cdf0e10cSrcweir             // Try to use the folder name to match our black list
312cdf0e10cSrcweir             // as some extensions don't provide an identifier in the
313cdf0e10cSrcweir             // description.xml!
314cdf0e10cSrcweir             for ( sal_uInt32 i = 0; i < m_aBlackList.size(); i++ )
315cdf0e10cSrcweir             {
316cdf0e10cSrcweir                 utl::SearchParam param(m_aBlackList[i], utl::SearchParam::SRCH_REGEXP);
317cdf0e10cSrcweir                 utl::TextSearch  ts(param, LANGUAGE_DONTKNOW);
318cdf0e10cSrcweir 
319cdf0e10cSrcweir                 xub_StrLen start = 0;
320cdf0e10cSrcweir                 xub_StrLen end   = static_cast<sal_uInt16>(sDescriptionXmlURL.getLength());
321cdf0e10cSrcweir                 if (ts.SearchFrwrd(sDescriptionXmlURL, &start, &end))
322cdf0e10cSrcweir                     return false;
323cdf0e10cSrcweir             }
324cdf0e10cSrcweir         }
325cdf0e10cSrcweir     }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir     return true;
328cdf0e10cSrcweir }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir bool OO3ExtensionMigration::migrateExtension( const ::rtl::OUString& sSourceDir )
331cdf0e10cSrcweir {
332cdf0e10cSrcweir     if ( !m_xExtensionManager.is() )
333cdf0e10cSrcweir     {
334cdf0e10cSrcweir         try
335cdf0e10cSrcweir         {
336cdf0e10cSrcweir             m_xExtensionManager = deployment::ExtensionManager::get( m_ctx );
337cdf0e10cSrcweir         }
338cdf0e10cSrcweir         catch ( ucb::CommandFailedException & ){}
339cdf0e10cSrcweir         catch ( uno::RuntimeException & ) {}
340cdf0e10cSrcweir     }
341cdf0e10cSrcweir 
342cdf0e10cSrcweir     if ( m_xExtensionManager.is() )
343cdf0e10cSrcweir     {
344cdf0e10cSrcweir         try
345cdf0e10cSrcweir         {
346cdf0e10cSrcweir             TmpRepositoryCommandEnv* pCmdEnv = new TmpRepositoryCommandEnv();
347cdf0e10cSrcweir 
348cdf0e10cSrcweir             uno::Reference< ucb::XCommandEnvironment > xCmdEnv(
349cdf0e10cSrcweir                 static_cast< cppu::OWeakObject* >( pCmdEnv ), uno::UNO_QUERY );
350cdf0e10cSrcweir             uno::Reference< task::XAbortChannel > xAbortChannel;
351cdf0e10cSrcweir             uno::Reference< deployment::XPackage > xPackage =
352cdf0e10cSrcweir                 m_xExtensionManager->addExtension(
353cdf0e10cSrcweir                     sSourceDir, uno::Sequence<beans::NamedValue>(),
354cdf0e10cSrcweir                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("user")), xAbortChannel, xCmdEnv );
355cdf0e10cSrcweir 
356cdf0e10cSrcweir             if ( xPackage.is() )
357cdf0e10cSrcweir                 return true;
358cdf0e10cSrcweir         }
359cdf0e10cSrcweir         catch ( ucb::CommandFailedException& )
360cdf0e10cSrcweir         {
361cdf0e10cSrcweir         }
362cdf0e10cSrcweir         catch ( ucb::CommandAbortedException& )
363cdf0e10cSrcweir         {
364cdf0e10cSrcweir         }
365cdf0e10cSrcweir         catch ( lang::IllegalArgumentException& )
366cdf0e10cSrcweir         {
367cdf0e10cSrcweir         }
368*13443bc8SOliver-Rainer Wittmann         catch ( deployment::DeploymentException& )
369*13443bc8SOliver-Rainer Wittmann         {
370*13443bc8SOliver-Rainer Wittmann         }
371*13443bc8SOliver-Rainer Wittmann         catch ( uno::RuntimeException & )
372*13443bc8SOliver-Rainer Wittmann         {
373*13443bc8SOliver-Rainer Wittmann         }
374cdf0e10cSrcweir     }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir     return false;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 
380cdf0e10cSrcweir // -----------------------------------------------------------------------------
381cdf0e10cSrcweir // XServiceInfo
382cdf0e10cSrcweir // -----------------------------------------------------------------------------
383cdf0e10cSrcweir 
384cdf0e10cSrcweir ::rtl::OUString OO3ExtensionMigration::getImplementationName() throw (RuntimeException)
385cdf0e10cSrcweir {
386cdf0e10cSrcweir     return OO3ExtensionMigration_getImplementationName();
387cdf0e10cSrcweir }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir // -----------------------------------------------------------------------------
390cdf0e10cSrcweir 
391cdf0e10cSrcweir sal_Bool OO3ExtensionMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
392cdf0e10cSrcweir {
393cdf0e10cSrcweir     Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
394cdf0e10cSrcweir     const ::rtl::OUString* pNames = aNames.getConstArray();
395cdf0e10cSrcweir     const ::rtl::OUString* pEnd = pNames + aNames.getLength();
396cdf0e10cSrcweir     for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
397cdf0e10cSrcweir 	    ;
398cdf0e10cSrcweir 
399cdf0e10cSrcweir     return pNames != pEnd;
400cdf0e10cSrcweir }
401cdf0e10cSrcweir 
402cdf0e10cSrcweir // -----------------------------------------------------------------------------
403cdf0e10cSrcweir 
404cdf0e10cSrcweir Sequence< ::rtl::OUString > OO3ExtensionMigration::getSupportedServiceNames() throw (RuntimeException)
405cdf0e10cSrcweir {
406cdf0e10cSrcweir     return OO3ExtensionMigration_getSupportedServiceNames();
407cdf0e10cSrcweir }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir // -----------------------------------------------------------------------------
410cdf0e10cSrcweir // XInitialization
411cdf0e10cSrcweir // -----------------------------------------------------------------------------
412cdf0e10cSrcweir 
413cdf0e10cSrcweir void OO3ExtensionMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
414cdf0e10cSrcweir {
415cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
416cdf0e10cSrcweir 
417cdf0e10cSrcweir     const Any* pIter = aArguments.getConstArray();
418cdf0e10cSrcweir     const Any* pEnd = pIter + aArguments.getLength();
419cdf0e10cSrcweir     for ( ; pIter != pEnd ; ++pIter )
420cdf0e10cSrcweir     {
421cdf0e10cSrcweir         beans::NamedValue aValue;
422cdf0e10cSrcweir         *pIter >>= aValue;
423cdf0e10cSrcweir         if ( aValue.Name.equalsAscii( "UserData" ) )
424cdf0e10cSrcweir         {
425cdf0e10cSrcweir             if ( !(aValue.Value >>= m_sSourceDir) )
426cdf0e10cSrcweir             {
427cdf0e10cSrcweir                 OSL_ENSURE( false, "ExtensionMigration::initialize: argument UserData has wrong type!" );
428cdf0e10cSrcweir             }
429cdf0e10cSrcweir         }
430cdf0e10cSrcweir         else if ( aValue.Name.equalsAscii( "ExtensionBlackList" ) )
431cdf0e10cSrcweir         {
432cdf0e10cSrcweir             Sequence< ::rtl::OUString > aBlackList;
433cdf0e10cSrcweir             if ( (aValue.Value >>= aBlackList ) && ( aBlackList.getLength() > 0 ))
434cdf0e10cSrcweir             {
435cdf0e10cSrcweir                 m_aBlackList.resize( aBlackList.getLength() );
436cdf0e10cSrcweir                 ::comphelper::sequenceToArray< ::rtl::OUString >( &m_aBlackList[0], aBlackList );
437cdf0e10cSrcweir             }
438cdf0e10cSrcweir         }
439cdf0e10cSrcweir     }
440cdf0e10cSrcweir }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir // -----------------------------------------------------------------------------
443cdf0e10cSrcweir 
444cdf0e10cSrcweir TStringVectorPtr getContent( const ::rtl::OUString& rBaseURL )
445cdf0e10cSrcweir {
446cdf0e10cSrcweir     TStringVectorPtr aResult( new TStringVector );
447cdf0e10cSrcweir     ::osl::Directory aDir( rBaseURL);
448cdf0e10cSrcweir     if ( aDir.open() == ::osl::FileBase::E_None )
449cdf0e10cSrcweir     {
450cdf0e10cSrcweir         // iterate over directory content
451cdf0e10cSrcweir         TStringVector aSubDirs;
452cdf0e10cSrcweir         ::osl::DirectoryItem aItem;
453cdf0e10cSrcweir         while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
454cdf0e10cSrcweir         {
455cdf0e10cSrcweir             ::osl::FileStatus aFileStatus( FileStatusMask_Type | FileStatusMask_FileURL );
456cdf0e10cSrcweir             if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
457cdf0e10cSrcweir                 aResult->push_back( aFileStatus.getFileURL() );
458cdf0e10cSrcweir         }
459cdf0e10cSrcweir     }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir     return aResult;
462cdf0e10cSrcweir }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir Any OO3ExtensionMigration::execute( const Sequence< beans::NamedValue >& )
465cdf0e10cSrcweir     throw (lang::IllegalArgumentException, Exception, RuntimeException)
466cdf0e10cSrcweir {
467cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
468cdf0e10cSrcweir 
469cdf0e10cSrcweir 	::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( m_sTargetDir );
470cdf0e10cSrcweir 	if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
471cdf0e10cSrcweir 	{
472cdf0e10cSrcweir 		// copy all extensions
473cdf0e10cSrcweir 		::rtl::OUString sSourceDir( m_sSourceDir );
474cdf0e10cSrcweir         sSourceDir += sExtensionSubDir;
475cdf0e10cSrcweir 		sSourceDir += sSubDirName;
476cdf0e10cSrcweir         sSourceDir += sExtensionRootSubDirName;
477cdf0e10cSrcweir         TStringVector aExtensionToMigrate;
478cdf0e10cSrcweir         scanUserExtensions( sSourceDir, aExtensionToMigrate );
479cdf0e10cSrcweir         if ( aExtensionToMigrate.size() > 0 )
480cdf0e10cSrcweir         {
481cdf0e10cSrcweir             TStringVector::iterator pIter = aExtensionToMigrate.begin();
482cdf0e10cSrcweir             while ( pIter != aExtensionToMigrate.end() )
483cdf0e10cSrcweir             {
484cdf0e10cSrcweir                 migrateExtension( *pIter );
485cdf0e10cSrcweir                 ++pIter;
486cdf0e10cSrcweir             }
487cdf0e10cSrcweir         }
488cdf0e10cSrcweir 	}
489cdf0e10cSrcweir 
490cdf0e10cSrcweir     return Any();
491cdf0e10cSrcweir }
492cdf0e10cSrcweir 
493cdf0e10cSrcweir // -----------------------------------------------------------------------------
494cdf0e10cSrcweir // TmpRepositoryCommandEnv
495cdf0e10cSrcweir // -----------------------------------------------------------------------------
496cdf0e10cSrcweir 
497cdf0e10cSrcweir TmpRepositoryCommandEnv::TmpRepositoryCommandEnv()
498cdf0e10cSrcweir {
499cdf0e10cSrcweir }
500cdf0e10cSrcweir 
501cdf0e10cSrcweir TmpRepositoryCommandEnv::~TmpRepositoryCommandEnv()
502cdf0e10cSrcweir {
503cdf0e10cSrcweir }
504cdf0e10cSrcweir // XCommandEnvironment
505cdf0e10cSrcweir //______________________________________________________________________________
506cdf0e10cSrcweir uno::Reference< task::XInteractionHandler > TmpRepositoryCommandEnv::getInteractionHandler()
507cdf0e10cSrcweir throw ( uno::RuntimeException )
508cdf0e10cSrcweir {
509cdf0e10cSrcweir     return this;
510cdf0e10cSrcweir }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir //______________________________________________________________________________
513cdf0e10cSrcweir uno::Reference< ucb::XProgressHandler > TmpRepositoryCommandEnv::getProgressHandler()
514cdf0e10cSrcweir throw ( uno::RuntimeException )
515cdf0e10cSrcweir {
516cdf0e10cSrcweir     return this;
517cdf0e10cSrcweir }
518cdf0e10cSrcweir 
519cdf0e10cSrcweir // XInteractionHandler
520cdf0e10cSrcweir void TmpRepositoryCommandEnv::handle(
521cdf0e10cSrcweir     uno::Reference< task::XInteractionRequest> const & xRequest )
522cdf0e10cSrcweir     throw ( uno::RuntimeException )
523cdf0e10cSrcweir {
524cdf0e10cSrcweir     uno::Any request( xRequest->getRequest() );
525cdf0e10cSrcweir     OSL_ASSERT( request.getValueTypeClass() == uno::TypeClass_EXCEPTION );
526cdf0e10cSrcweir 
527cdf0e10cSrcweir     bool approve = true;
528cdf0e10cSrcweir     bool abort   = false;
529cdf0e10cSrcweir 
5305e679a6cSOliver-Rainer Wittmann     deployment::VersionException verExc;
5315e679a6cSOliver-Rainer Wittmann     if ( xRequest->getRequest() >>= verExc )
5325e679a6cSOliver-Rainer Wittmann     {
5335e679a6cSOliver-Rainer Wittmann         // user interaction, if an extension is already been installed.
534fd6d8138SAndre Fischer         approve = handleVersionException( verExc );
5355e679a6cSOliver-Rainer Wittmann         abort = !approve;
5365e679a6cSOliver-Rainer Wittmann     }
5375e679a6cSOliver-Rainer Wittmann 
538cdf0e10cSrcweir     // select:
539cdf0e10cSrcweir     uno::Sequence< Reference< task::XInteractionContinuation > > conts(
540cdf0e10cSrcweir         xRequest->getContinuations() );
541cdf0e10cSrcweir     Reference< task::XInteractionContinuation > const * pConts =
542cdf0e10cSrcweir         conts.getConstArray();
543cdf0e10cSrcweir     sal_Int32 len = conts.getLength();
544cdf0e10cSrcweir     for ( sal_Int32 pos = 0; pos < len; ++pos )
545cdf0e10cSrcweir     {
546cdf0e10cSrcweir         if (approve) {
547cdf0e10cSrcweir             uno::Reference< task::XInteractionApprove > xInteractionApprove(
548cdf0e10cSrcweir                 pConts[ pos ], uno::UNO_QUERY );
549cdf0e10cSrcweir             if (xInteractionApprove.is()) {
550cdf0e10cSrcweir                 xInteractionApprove->select();
551cdf0e10cSrcweir                 // don't query again for ongoing continuations:
552cdf0e10cSrcweir                 approve = false;
553cdf0e10cSrcweir             }
554cdf0e10cSrcweir         }
555cdf0e10cSrcweir         else if (abort) {
556cdf0e10cSrcweir             uno::Reference< task::XInteractionAbort > xInteractionAbort(
557cdf0e10cSrcweir                 pConts[ pos ], uno::UNO_QUERY );
558cdf0e10cSrcweir             if (xInteractionAbort.is()) {
559cdf0e10cSrcweir                 xInteractionAbort->select();
560cdf0e10cSrcweir                 // don't query again for ongoing continuations:
561cdf0e10cSrcweir                 abort = false;
562cdf0e10cSrcweir             }
563cdf0e10cSrcweir         }
564cdf0e10cSrcweir 	}
565cdf0e10cSrcweir }
566cdf0e10cSrcweir 
567cdf0e10cSrcweir // XProgressHandler
568cdf0e10cSrcweir void TmpRepositoryCommandEnv::push( uno::Any const & /*Status*/ )
569cdf0e10cSrcweir throw (uno::RuntimeException)
570cdf0e10cSrcweir {
571cdf0e10cSrcweir }
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 
574cdf0e10cSrcweir void TmpRepositoryCommandEnv::update( uno::Any const & /*Status */)
575cdf0e10cSrcweir throw (uno::RuntimeException)
576cdf0e10cSrcweir {
577cdf0e10cSrcweir }
578cdf0e10cSrcweir 
579cdf0e10cSrcweir void TmpRepositoryCommandEnv::pop() throw (uno::RuntimeException)
580cdf0e10cSrcweir {
581cdf0e10cSrcweir }
582cdf0e10cSrcweir 
583cdf0e10cSrcweir // =============================================================================
584cdf0e10cSrcweir // component operations
585cdf0e10cSrcweir // =============================================================================
586cdf0e10cSrcweir 
587cdf0e10cSrcweir Reference< XInterface > SAL_CALL OO3ExtensionMigration_create(
588cdf0e10cSrcweir     Reference< XComponentContext > const & ctx )
589cdf0e10cSrcweir     SAL_THROW( () )
590cdf0e10cSrcweir {
591cdf0e10cSrcweir     return static_cast< lang::XTypeProvider * >( new OO3ExtensionMigration(
592cdf0e10cSrcweir         ctx) );
593cdf0e10cSrcweir }
594cdf0e10cSrcweir 
595cdf0e10cSrcweir // -----------------------------------------------------------------------------
596cdf0e10cSrcweir 
597cdf0e10cSrcweir }	// namespace migration
598