merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / source / appl / xpackcreator.cxx
blob128374225bf9648933cf61b0d884b3e451bdee75
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xpackcreator.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
34 #include <com/sun/star/io/XOutputStream.hpp>
37 #include "xpackcreator.hxx"
39 #include <sot/stg.hxx>
40 #include <sot/storage.hxx>
41 #include <tools/stream.hxx>
42 #include <unotools/tempfile.hxx>
43 #include <unotools/ucbhelper.hxx>
44 #include <ucbhelper/content.hxx>
45 #include <ucbhelper/commandenvironment.hxx>
47 using namespace ::com::sun::star;
49 //-------------------------------------------------------------------------
50 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::impl_getStaticSupportedServiceNames()
52 uno::Sequence< ::rtl::OUString > aRet(2);
53 aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.PackageStructureCreator");
54 aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator");
55 return aRet;
58 //-------------------------------------------------------------------------
59 ::rtl::OUString SAL_CALL OPackageStructureCreator::impl_getStaticImplementationName()
61 return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator");
64 //-------------------------------------------------------------------------
65 uno::Reference< lang::XSingleServiceFactory > SAL_CALL OPackageStructureCreator::impl_createFactory(
66 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
68 return ::cppu::createOneInstanceFactory( xServiceManager,
69 OPackageStructureCreator::impl_getStaticImplementationName(),
70 OPackageStructureCreator::impl_staticCreateSelfInstance,
71 OPackageStructureCreator::impl_getStaticSupportedServiceNames() );
74 //-------------------------------------------------------------------------
75 uno::Reference< uno::XInterface > SAL_CALL OPackageStructureCreator::impl_staticCreateSelfInstance(
76 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
78 return uno::Reference< uno::XInterface >( *new OPackageStructureCreator( xServiceManager ) );
82 //-------------------------------------------------------------------------
83 void SAL_CALL OPackageStructureCreator::convertToPackage( const ::rtl::OUString& aFolderUrl,
84 const uno::Reference< io::XOutputStream >& xTargetStream )
85 throw ( io::IOException,
86 uno::RuntimeException )
88 uno::Reference< ucb::XCommandEnvironment > xComEnv;
90 if ( !xTargetStream.is() )
91 throw io::IOException(); // TODO/LATER
93 sal_Bool bSuccess = sal_False;
94 ::ucbhelper::Content aContent;
95 if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, aContent ) )
97 SvStream* pTempStream = NULL;
99 ::rtl::OUString aTempURL = ::utl::TempFile().GetURL();
100 try {
101 if ( aContent.isFolder() )
103 UCBStorage* pUCBStorage = new UCBStorage( aContent,
104 aFolderUrl,
105 STREAM_READ,
106 sal_False,
107 sal_True );
108 SotStorageRef aStorage = new SotStorage( pUCBStorage );
110 if ( aTempURL.getLength() )
112 pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE );
113 SotStorageRef aTargetStorage = new SotStorage( sal_True, *pTempStream );
114 aStorage->CopyTo( aTargetStorage );
115 aTargetStorage->Commit();
117 if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
118 throw io::IOException();
120 aTargetStorage = NULL;
121 aStorage = NULL;
123 pTempStream->Seek( 0 );
125 uno::Sequence< sal_Int8 > aSeq( 32000 );
126 sal_uInt32 nRead = 0;
127 do {
128 if ( aSeq.getLength() < 32000 )
129 aSeq.realloc( 32000 );
131 nRead = pTempStream->Read( aSeq.getArray(), 32000 );
132 if ( nRead < 32000 )
133 aSeq.realloc( nRead );
134 xTargetStream->writeBytes( aSeq );
135 } while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead );
137 if ( pTempStream->GetError() )
138 throw io::IOException();
140 bSuccess = sal_True;
144 catch ( uno::RuntimeException& )
146 if ( pTempStream )
147 delete pTempStream;
149 if ( aTempURL.getLength() )
150 ::utl::UCBContentHelper::Kill( aTempURL );
152 throw;
154 catch ( io::IOException& )
156 if ( pTempStream )
157 delete pTempStream;
159 if ( aTempURL.getLength() )
160 ::utl::UCBContentHelper::Kill( aTempURL );
162 throw;
164 catch ( uno::Exception& )
168 if ( pTempStream )
169 delete pTempStream;
171 if ( aTempURL.getLength() )
172 ::utl::UCBContentHelper::Kill( aTempURL );
175 if ( !bSuccess )
176 throw io::IOException(); // TODO/LATER: can't proceed with creation
179 //-------------------------------------------------------------------------
180 ::rtl::OUString SAL_CALL OPackageStructureCreator::getImplementationName()
181 throw ( uno::RuntimeException )
183 return impl_getStaticImplementationName();
186 //-------------------------------------------------------------------------
187 sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const ::rtl::OUString& ServiceName )
188 throw ( uno::RuntimeException )
190 uno::Sequence< ::rtl::OUString > aSeq = impl_getStaticSupportedServiceNames();
192 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
193 if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
194 return sal_True;
196 return sal_False;
199 //-------------------------------------------------------------------------
200 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
201 throw ( uno::RuntimeException )
203 return impl_getStaticSupportedServiceNames();