1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/embed/XPackageStructureCreator.hpp>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <comphelper/processfactory.hxx>
26 #include <cppuhelper/implbase2.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <rtl/ref.hxx>
29 #include <sot/stg.hxx>
30 #include <sot/storage.hxx>
31 #include <tools/stream.hxx>
32 #include <unotools/tempfile.hxx>
33 #include <unotools/ucbhelper.hxx>
34 #include <ucbhelper/content.hxx>
35 #include <ucbhelper/commandenvironment.hxx>
41 class OPackageStructureCreator
: public ::cppu::WeakImplHelper2
< embed::XPackageStructureCreator
,
45 OPackageStructureCreator() {}
47 // XPackageStructureCreator
48 virtual void SAL_CALL
convertToPackage( const OUString
& aFolderUrl
, const uno::Reference
< io::XOutputStream
>& xTargetStream
) throw (io::IOException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
51 virtual OUString SAL_CALL
getImplementationName() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
52 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
53 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
57 void SAL_CALL
OPackageStructureCreator::convertToPackage( const OUString
& aFolderUrl
,
58 const uno::Reference
< io::XOutputStream
>& xTargetStream
)
59 throw ( io::IOException
,
60 uno::RuntimeException
, std::exception
)
62 uno::Reference
< ucb::XCommandEnvironment
> xComEnv
;
64 if ( !xTargetStream
.is() )
65 throw io::IOException(); // TODO/LATER
67 bool bSuccess
= false;
68 ::ucbhelper::Content aContent
;
69 if( ::ucbhelper::Content::create( aFolderUrl
, xComEnv
, comphelper::getProcessComponentContext(), aContent
) )
71 SvStream
* pTempStream
= NULL
;
73 OUString aTempURL
= ::utl::TempFile().GetURL();
75 if ( aContent
.isFolder() )
77 UCBStorage
* pUCBStorage
= new UCBStorage( aContent
,
82 tools::SvRef
<SotStorage
> aStorage
= new SotStorage( pUCBStorage
);
84 if ( !aTempURL
.isEmpty() )
86 pTempStream
= new SvFileStream( aTempURL
, STREAM_STD_READWRITE
);
87 tools::SvRef
<SotStorage
> aTargetStorage
= new SotStorage( true, *pTempStream
);
88 aStorage
->CopyTo( aTargetStorage
);
89 aTargetStorage
->Commit();
91 if ( aStorage
->GetError() || aTargetStorage
->GetError() || pTempStream
->GetError() )
92 throw io::IOException();
94 aTargetStorage
= NULL
;
97 pTempStream
->Seek( 0 );
99 uno::Sequence
< sal_Int8
> aSeq( 32000 );
100 sal_uInt32 nRead
= 0;
102 if ( aSeq
.getLength() < 32000 )
103 aSeq
.realloc( 32000 );
105 nRead
= pTempStream
->Read( aSeq
.getArray(), 32000 );
107 aSeq
.realloc( nRead
);
108 xTargetStream
->writeBytes( aSeq
);
109 } while( !pTempStream
->IsEof() && !pTempStream
->GetError() && nRead
);
111 if ( pTempStream
->GetError() )
112 throw io::IOException();
118 catch (const uno::RuntimeException
&)
123 if ( !aTempURL
.isEmpty() )
124 ::utl::UCBContentHelper::Kill( aTempURL
);
128 catch (const io::IOException
&)
133 if ( !aTempURL
.isEmpty() )
134 ::utl::UCBContentHelper::Kill( aTempURL
);
138 catch (const uno::Exception
&)
145 if ( !aTempURL
.isEmpty() )
146 ::utl::UCBContentHelper::Kill( aTempURL
);
150 throw io::IOException(); // TODO/LATER: can't proceed with creation
153 OUString SAL_CALL
OPackageStructureCreator::getImplementationName()
154 throw ( uno::RuntimeException
, std::exception
)
156 return OUString("com.sun.star.comp.embed.PackageStructureCreator");
159 sal_Bool SAL_CALL
OPackageStructureCreator::supportsService( const OUString
& ServiceName
)
160 throw ( uno::RuntimeException
, std::exception
)
162 return cppu::supportsService(this, ServiceName
);
165 uno::Sequence
< OUString
> SAL_CALL
OPackageStructureCreator::getSupportedServiceNames()
166 throw ( uno::RuntimeException
, std::exception
)
168 uno::Sequence
< OUString
> aRet(2);
169 aRet
[0] = "com.sun.star.embed.PackageStructureCreator";
170 aRet
[1] = "com.sun.star.comp.embed.PackageStructureCreator";
176 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
177 com_sun_star_comp_embed_PackageStructureCreator_get_implementation(
178 css::uno::XComponentContext
*,
179 css::uno::Sequence
<css::uno::Any
> const &)
181 return cppu::acquire(new OPackageStructureCreator());
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */