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/IOException.hpp>
22 #include <com/sun/star/io/XOutputStream.hpp>
23 #include <com/sun/star/embed/XPackageStructureCreator.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <cppuhelper/implbase.hxx>
28 #include <cppuhelper/supportsservice.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>
40 class OPackageStructureCreator
: public ::cppu::WeakImplHelper
< embed::XPackageStructureCreator
,
44 OPackageStructureCreator() {}
46 // XPackageStructureCreator
47 virtual void SAL_CALL
convertToPackage( const OUString
& aFolderUrl
, const uno::Reference
< io::XOutputStream
>& xTargetStream
) override
;
50 virtual OUString SAL_CALL
getImplementationName() override
;
51 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
52 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
56 void SAL_CALL
OPackageStructureCreator::convertToPackage( const OUString
& aFolderUrl
,
57 const uno::Reference
< io::XOutputStream
>& xTargetStream
)
59 uno::Reference
< ucb::XCommandEnvironment
> xComEnv
;
61 if ( !xTargetStream
.is() )
62 throw io::IOException(); // TODO/LATER
64 bool bSuccess
= false;
65 ::ucbhelper::Content aContent
;
66 if( ::ucbhelper::Content::create( aFolderUrl
, xComEnv
, comphelper::getProcessComponentContext(), aContent
) )
68 std::unique_ptr
<SvStream
> pTempStream
;
70 OUString aTempURL
= ::utl::TempFile().GetURL();
72 if ( aContent
.isFolder() )
74 UCBStorage
* pUCBStorage
= new UCBStorage( aContent
,
79 tools::SvRef
<SotStorage
> aStorage
= new SotStorage( pUCBStorage
);
81 if ( !aTempURL
.isEmpty() )
83 pTempStream
.reset(new SvFileStream( aTempURL
, StreamMode::STD_READWRITE
));
84 tools::SvRef
<SotStorage
> aTargetStorage
= new SotStorage( true, *pTempStream
);
85 aStorage
->CopyTo( aTargetStorage
.get() );
86 aTargetStorage
->Commit();
88 if ( aStorage
->GetError() || aTargetStorage
->GetError() || pTempStream
->GetError() )
89 throw io::IOException();
91 aTargetStorage
= nullptr;
94 pTempStream
->Seek( 0 );
96 uno::Sequence
< sal_Int8
> aSeq( 32000 );
99 if ( aSeq
.getLength() < 32000 )
100 aSeq
.realloc( 32000 );
102 nRead
= pTempStream
->ReadBytes(aSeq
.getArray(), 32000);
104 aSeq
.realloc( nRead
);
105 xTargetStream
->writeBytes( aSeq
);
106 } while (pTempStream
->good() && nRead
);
108 if ( pTempStream
->GetError() )
109 throw io::IOException();
115 catch (const uno::RuntimeException
&)
119 if ( !aTempURL
.isEmpty() )
120 ::utl::UCBContentHelper::Kill( aTempURL
);
124 catch (const io::IOException
&)
128 if ( !aTempURL
.isEmpty() )
129 ::utl::UCBContentHelper::Kill( aTempURL
);
133 catch (const uno::Exception
&)
139 if ( !aTempURL
.isEmpty() )
140 ::utl::UCBContentHelper::Kill( aTempURL
);
144 throw io::IOException(); // TODO/LATER: can't proceed with creation
147 OUString SAL_CALL
OPackageStructureCreator::getImplementationName()
149 return "com.sun.star.comp.embed.PackageStructureCreator";
152 sal_Bool SAL_CALL
OPackageStructureCreator::supportsService( const OUString
& ServiceName
)
154 return cppu::supportsService(this, ServiceName
);
157 uno::Sequence
< OUString
> SAL_CALL
OPackageStructureCreator::getSupportedServiceNames()
159 return { "com.sun.star.embed.PackageStructureCreator", "com.sun.star.comp.embed.PackageStructureCreator" };
164 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
165 com_sun_star_comp_embed_PackageStructureCreator_get_implementation(
166 css::uno::XComponentContext
*,
167 css::uno::Sequence
<css::uno::Any
> const &)
169 return cppu::acquire(new OPackageStructureCreator());
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */