1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_sfx2.hxx" 26 #include <com/sun/star/ucb/XCommandEnvironment.hpp> 27 #include <com/sun/star/io/XOutputStream.hpp> 28 29 30 #include "xpackcreator.hxx" 31 32 #include <sot/stg.hxx> 33 #include <sot/storage.hxx> 34 #include <tools/stream.hxx> 35 #include <unotools/tempfile.hxx> 36 #include <unotools/ucbhelper.hxx> 37 #include <ucbhelper/content.hxx> 38 #include <ucbhelper/commandenvironment.hxx> 39 40 using namespace ::com::sun::star; 41 42 //------------------------------------------------------------------------- 43 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::impl_getStaticSupportedServiceNames() 44 { 45 uno::Sequence< ::rtl::OUString > aRet(2); 46 aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.PackageStructureCreator"); 47 aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator"); 48 return aRet; 49 } 50 51 //------------------------------------------------------------------------- 52 ::rtl::OUString SAL_CALL OPackageStructureCreator::impl_getStaticImplementationName() 53 { 54 return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator"); 55 } 56 57 //------------------------------------------------------------------------- 58 uno::Reference< lang::XSingleServiceFactory > SAL_CALL OPackageStructureCreator::impl_createFactory( 59 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager ) 60 { 61 return ::cppu::createOneInstanceFactory( xServiceManager, 62 OPackageStructureCreator::impl_getStaticImplementationName(), 63 OPackageStructureCreator::impl_staticCreateSelfInstance, 64 OPackageStructureCreator::impl_getStaticSupportedServiceNames() ); 65 } 66 67 //------------------------------------------------------------------------- 68 uno::Reference< uno::XInterface > SAL_CALL OPackageStructureCreator::impl_staticCreateSelfInstance( 69 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager ) 70 { 71 return uno::Reference< uno::XInterface >( *new OPackageStructureCreator( xServiceManager ) ); 72 } 73 74 75 //------------------------------------------------------------------------- 76 void SAL_CALL OPackageStructureCreator::convertToPackage( const ::rtl::OUString& aFolderUrl, 77 const uno::Reference< io::XOutputStream >& xTargetStream ) 78 { 79 uno::Reference< ucb::XCommandEnvironment > xComEnv; 80 81 if ( !xTargetStream.is() ) 82 throw io::IOException(); // TODO/LATER 83 84 sal_Bool bSuccess = sal_False; 85 ::ucbhelper::Content aContent; 86 if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, aContent ) ) 87 { 88 SvStream* pTempStream = NULL; 89 90 ::rtl::OUString aTempURL = ::utl::TempFile().GetURL(); 91 try { 92 if ( aContent.isFolder() ) 93 { 94 UCBStorage* pUCBStorage = new UCBStorage( aContent, 95 aFolderUrl, 96 STREAM_READ, 97 sal_False, 98 sal_True ); 99 SotStorageRef aStorage = new SotStorage( pUCBStorage ); 100 101 if ( aTempURL.getLength() ) 102 { 103 pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE ); 104 SotStorageRef aTargetStorage = new SotStorage( sal_True, *pTempStream ); 105 aStorage->CopyTo( aTargetStorage ); 106 aTargetStorage->Commit(); 107 108 if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() ) 109 throw io::IOException(); 110 111 aTargetStorage = NULL; 112 aStorage = NULL; 113 114 pTempStream->Seek( 0 ); 115 116 uno::Sequence< sal_Int8 > aSeq( 32000 ); 117 sal_uInt32 nRead = 0; 118 do { 119 if ( aSeq.getLength() < 32000 ) 120 aSeq.realloc( 32000 ); 121 122 nRead = pTempStream->Read( aSeq.getArray(), 32000 ); 123 if ( nRead < 32000 ) 124 aSeq.realloc( nRead ); 125 xTargetStream->writeBytes( aSeq ); 126 } while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead ); 127 128 if ( pTempStream->GetError() ) 129 throw io::IOException(); 130 131 bSuccess = sal_True; 132 } 133 } 134 } 135 catch ( uno::RuntimeException& ) 136 { 137 if ( pTempStream ) 138 delete pTempStream; 139 140 if ( aTempURL.getLength() ) 141 ::utl::UCBContentHelper::Kill( aTempURL ); 142 143 throw; 144 } 145 catch ( io::IOException& ) 146 { 147 if ( pTempStream ) 148 delete pTempStream; 149 150 if ( aTempURL.getLength() ) 151 ::utl::UCBContentHelper::Kill( aTempURL ); 152 153 throw; 154 } 155 catch ( uno::Exception& ) 156 { 157 } 158 159 if ( pTempStream ) 160 delete pTempStream; 161 162 if ( aTempURL.getLength() ) 163 ::utl::UCBContentHelper::Kill( aTempURL ); 164 } 165 166 if ( !bSuccess ) 167 throw io::IOException(); // TODO/LATER: can't proceed with creation 168 } 169 170 //------------------------------------------------------------------------- 171 ::rtl::OUString SAL_CALL OPackageStructureCreator::getImplementationName() 172 { 173 return impl_getStaticImplementationName(); 174 } 175 176 //------------------------------------------------------------------------- 177 sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const ::rtl::OUString& ServiceName ) 178 { 179 uno::Sequence< ::rtl::OUString > aSeq = impl_getStaticSupportedServiceNames(); 180 181 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) 182 if ( ServiceName.compareTo( aSeq[nInd] ) == 0 ) 183 return sal_True; 184 185 return sal_False; 186 } 187 188 //------------------------------------------------------------------------- 189 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames() 190 { 191 return impl_getStaticSupportedServiceNames(); 192 } 193