Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / appl / xpackcreator.cxx
blob07cc2256c961e67b93fecb19fb40f0dd55834fcc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
30 #include <com/sun/star/io/XOutputStream.hpp>
33 #include "xpackcreator.hxx"
35 #include <sot/stg.hxx>
36 #include <sot/storage.hxx>
37 #include <tools/stream.hxx>
38 #include <unotools/tempfile.hxx>
39 #include <unotools/ucbhelper.hxx>
40 #include <ucbhelper/content.hxx>
41 #include <ucbhelper/commandenvironment.hxx>
43 using namespace ::com::sun::star;
45 //-------------------------------------------------------------------------
46 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::impl_getStaticSupportedServiceNames()
48 uno::Sequence< ::rtl::OUString > aRet(2);
49 aRet[0] = ::rtl::OUString("com.sun.star.embed.PackageStructureCreator");
50 aRet[1] = ::rtl::OUString("com.sun.star.comp.embed.PackageStructureCreator");
51 return aRet;
54 //-------------------------------------------------------------------------
55 ::rtl::OUString SAL_CALL OPackageStructureCreator::impl_getStaticImplementationName()
57 return ::rtl::OUString("com.sun.star.comp.embed.PackageStructureCreator");
60 //-------------------------------------------------------------------------
61 uno::Reference< lang::XSingleServiceFactory > SAL_CALL OPackageStructureCreator::impl_createFactory(
62 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
64 return ::cppu::createOneInstanceFactory( xServiceManager,
65 OPackageStructureCreator::impl_getStaticImplementationName(),
66 OPackageStructureCreator::impl_staticCreateSelfInstance,
67 OPackageStructureCreator::impl_getStaticSupportedServiceNames() );
70 //-------------------------------------------------------------------------
71 uno::Reference< uno::XInterface > SAL_CALL OPackageStructureCreator::impl_staticCreateSelfInstance(
72 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
74 return uno::Reference< uno::XInterface >( *new OPackageStructureCreator( xServiceManager ) );
78 //-------------------------------------------------------------------------
79 void SAL_CALL OPackageStructureCreator::convertToPackage( const ::rtl::OUString& aFolderUrl,
80 const uno::Reference< io::XOutputStream >& xTargetStream )
81 throw ( io::IOException,
82 uno::RuntimeException )
84 uno::Reference< ucb::XCommandEnvironment > xComEnv;
86 if ( !xTargetStream.is() )
87 throw io::IOException(); // TODO/LATER
89 sal_Bool bSuccess = sal_False;
90 ::ucbhelper::Content aContent;
91 if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, aContent ) )
93 SvStream* pTempStream = NULL;
95 ::rtl::OUString aTempURL = ::utl::TempFile().GetURL();
96 try {
97 if ( aContent.isFolder() )
99 UCBStorage* pUCBStorage = new UCBStorage( aContent,
100 aFolderUrl,
101 STREAM_READ,
102 sal_False,
103 sal_True );
104 SotStorageRef aStorage = new SotStorage( pUCBStorage );
106 if ( !aTempURL.isEmpty() )
108 pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE );
109 SotStorageRef aTargetStorage = new SotStorage( sal_True, *pTempStream );
110 aStorage->CopyTo( aTargetStorage );
111 aTargetStorage->Commit();
113 if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
114 throw io::IOException();
116 aTargetStorage = NULL;
117 aStorage = NULL;
119 pTempStream->Seek( 0 );
121 uno::Sequence< sal_Int8 > aSeq( 32000 );
122 sal_uInt32 nRead = 0;
123 do {
124 if ( aSeq.getLength() < 32000 )
125 aSeq.realloc( 32000 );
127 nRead = pTempStream->Read( aSeq.getArray(), 32000 );
128 if ( nRead < 32000 )
129 aSeq.realloc( nRead );
130 xTargetStream->writeBytes( aSeq );
131 } while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead );
133 if ( pTempStream->GetError() )
134 throw io::IOException();
136 bSuccess = sal_True;
140 catch (const uno::RuntimeException&)
142 if ( pTempStream )
143 delete pTempStream;
145 if ( !aTempURL.isEmpty() )
146 ::utl::UCBContentHelper::Kill( aTempURL );
148 throw;
150 catch (const io::IOException&)
152 if ( pTempStream )
153 delete pTempStream;
155 if ( !aTempURL.isEmpty() )
156 ::utl::UCBContentHelper::Kill( aTempURL );
158 throw;
160 catch (const uno::Exception&)
164 if ( pTempStream )
165 delete pTempStream;
167 if ( !aTempURL.isEmpty() )
168 ::utl::UCBContentHelper::Kill( aTempURL );
171 if ( !bSuccess )
172 throw io::IOException(); // TODO/LATER: can't proceed with creation
175 //-------------------------------------------------------------------------
176 ::rtl::OUString SAL_CALL OPackageStructureCreator::getImplementationName()
177 throw ( uno::RuntimeException )
179 return impl_getStaticImplementationName();
182 //-------------------------------------------------------------------------
183 sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const ::rtl::OUString& ServiceName )
184 throw ( uno::RuntimeException )
186 uno::Sequence< ::rtl::OUString > aSeq = impl_getStaticSupportedServiceNames();
188 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
189 if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
190 return sal_True;
192 return sal_False;
195 //-------------------------------------------------------------------------
196 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
197 throw ( uno::RuntimeException )
199 return impl_getStaticSupportedServiceNames();
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */