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