bump product version to 4.2.0.1
[LibreOffice.git] / sfx2 / source / appl / xpackcreator.cxx
blobfbe0a40f7f2a2dbf090252cfe760c8e73c5a643b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
21 #include <com/sun/star/io/XOutputStream.hpp>
22 #include <cppuhelper/supportsservice.hxx>
24 #include "xpackcreator.hxx"
26 #include <comphelper/processfactory.hxx>
27 #include <sot/stg.hxx>
28 #include <sot/storage.hxx>
29 #include <tools/stream.hxx>
30 #include <unotools/tempfile.hxx>
31 #include <unotools/ucbhelper.hxx>
32 #include <ucbhelper/content.hxx>
33 #include <ucbhelper/commandenvironment.hxx>
35 using namespace ::com::sun::star;
37 //-------------------------------------------------------------------------
38 uno::Sequence< OUString > SAL_CALL OPackageStructureCreator::impl_getStaticSupportedServiceNames()
40 uno::Sequence< OUString > aRet(2);
41 aRet[0] = "com.sun.star.embed.PackageStructureCreator";
42 aRet[1] = "com.sun.star.comp.embed.PackageStructureCreator";
43 return aRet;
46 //-------------------------------------------------------------------------
47 OUString SAL_CALL OPackageStructureCreator::impl_getStaticImplementationName()
49 return OUString("com.sun.star.comp.embed.PackageStructureCreator");
52 //-------------------------------------------------------------------------
53 uno::Reference< lang::XSingleServiceFactory > SAL_CALL OPackageStructureCreator::impl_createFactory(
54 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
56 return ::cppu::createOneInstanceFactory( xServiceManager,
57 OPackageStructureCreator::impl_getStaticImplementationName(),
58 OPackageStructureCreator::impl_staticCreateSelfInstance,
59 OPackageStructureCreator::impl_getStaticSupportedServiceNames() );
62 //-------------------------------------------------------------------------
63 uno::Reference< uno::XInterface > SAL_CALL OPackageStructureCreator::impl_staticCreateSelfInstance(
64 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
66 return uno::Reference< uno::XInterface >( *new OPackageStructureCreator( xServiceManager ) );
70 //-------------------------------------------------------------------------
71 void SAL_CALL OPackageStructureCreator::convertToPackage( const OUString& aFolderUrl,
72 const uno::Reference< io::XOutputStream >& xTargetStream )
73 throw ( io::IOException,
74 uno::RuntimeException )
76 uno::Reference< ucb::XCommandEnvironment > xComEnv;
78 if ( !xTargetStream.is() )
79 throw io::IOException(); // TODO/LATER
81 sal_Bool bSuccess = sal_False;
82 ::ucbhelper::Content aContent;
83 if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, comphelper::getProcessComponentContext(), aContent ) )
85 SvStream* pTempStream = NULL;
87 OUString aTempURL = ::utl::TempFile().GetURL();
88 try {
89 if ( aContent.isFolder() )
91 UCBStorage* pUCBStorage = new UCBStorage( aContent,
92 aFolderUrl,
93 STREAM_READ,
94 sal_False,
95 sal_True );
96 SotStorageRef aStorage = new SotStorage( pUCBStorage );
98 if ( !aTempURL.isEmpty() )
100 pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE );
101 SotStorageRef aTargetStorage = new SotStorage( sal_True, *pTempStream );
102 aStorage->CopyTo( aTargetStorage );
103 aTargetStorage->Commit();
105 if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
106 throw io::IOException();
108 aTargetStorage = NULL;
109 aStorage = NULL;
111 pTempStream->Seek( 0 );
113 uno::Sequence< sal_Int8 > aSeq( 32000 );
114 sal_uInt32 nRead = 0;
115 do {
116 if ( aSeq.getLength() < 32000 )
117 aSeq.realloc( 32000 );
119 nRead = pTempStream->Read( aSeq.getArray(), 32000 );
120 if ( nRead < 32000 )
121 aSeq.realloc( nRead );
122 xTargetStream->writeBytes( aSeq );
123 } while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead );
125 if ( pTempStream->GetError() )
126 throw io::IOException();
128 bSuccess = sal_True;
132 catch (const uno::RuntimeException&)
134 if ( pTempStream )
135 delete pTempStream;
137 if ( !aTempURL.isEmpty() )
138 ::utl::UCBContentHelper::Kill( aTempURL );
140 throw;
142 catch (const io::IOException&)
144 if ( pTempStream )
145 delete pTempStream;
147 if ( !aTempURL.isEmpty() )
148 ::utl::UCBContentHelper::Kill( aTempURL );
150 throw;
152 catch (const uno::Exception&)
156 if ( pTempStream )
157 delete pTempStream;
159 if ( !aTempURL.isEmpty() )
160 ::utl::UCBContentHelper::Kill( aTempURL );
163 if ( !bSuccess )
164 throw io::IOException(); // TODO/LATER: can't proceed with creation
167 OUString SAL_CALL OPackageStructureCreator::getImplementationName()
168 throw ( uno::RuntimeException )
170 return impl_getStaticImplementationName();
173 sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const OUString& ServiceName )
174 throw ( uno::RuntimeException )
176 return cppu::supportsService(this, ServiceName);
179 uno::Sequence< OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
180 throw ( uno::RuntimeException )
182 return impl_getStaticSupportedServiceNames();
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */