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 OUString aTempURL
= ::utl::CreateTempURL();
70 if ( aContent
.isFolder() )
72 UCBStorage
* pUCBStorage
= new UCBStorage( aContent
,
77 rtl::Reference
<SotStorage
> aStorage
= new SotStorage(pUCBStorage
);
79 if ( !aTempURL
.isEmpty() )
81 SvFileStream
aTempStream( aTempURL
, StreamMode::STD_READWRITE
);
82 rtl::Reference
<SotStorage
> aTargetStorage
= new SotStorage(true, aTempStream
);
83 aStorage
->CopyTo( aTargetStorage
.get() );
84 aTargetStorage
->Commit();
86 if ( aStorage
->GetError() || aTargetStorage
->GetError() || aTempStream
.GetError() )
87 throw io::IOException();
89 aTargetStorage
= nullptr;
92 aTempStream
.Seek( 0 );
94 uno::Sequence
< sal_Int8
> aSeq( 32000 );
97 if ( aSeq
.getLength() < 32000 )
98 aSeq
.realloc( 32000 );
100 nRead
= aTempStream
.ReadBytes(aSeq
.getArray(), 32000);
102 aSeq
.realloc( nRead
);
103 xTargetStream
->writeBytes( aSeq
);
104 } while (aTempStream
.good() && nRead
);
106 if ( aTempStream
.GetError() )
107 throw io::IOException();
113 catch (const uno::RuntimeException
&)
115 if ( !aTempURL
.isEmpty() )
116 ::utl::UCBContentHelper::Kill( aTempURL
);
120 catch (const io::IOException
&)
122 if ( !aTempURL
.isEmpty() )
123 ::utl::UCBContentHelper::Kill( aTempURL
);
127 catch (const uno::Exception
&)
131 if ( !aTempURL
.isEmpty() )
132 ::utl::UCBContentHelper::Kill( aTempURL
);
136 throw io::IOException(); // TODO/LATER: can't proceed with creation
139 OUString SAL_CALL
OPackageStructureCreator::getImplementationName()
141 return u
"com.sun.star.comp.embed.PackageStructureCreator"_ustr
;
144 sal_Bool SAL_CALL
OPackageStructureCreator::supportsService( const OUString
& ServiceName
)
146 return cppu::supportsService(this, ServiceName
);
149 uno::Sequence
< OUString
> SAL_CALL
OPackageStructureCreator::getSupportedServiceNames()
151 return { u
"com.sun.star.embed.PackageStructureCreator"_ustr
, u
"com.sun.star.comp.embed.PackageStructureCreator"_ustr
};
156 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
157 com_sun_star_comp_embed_PackageStructureCreator_get_implementation(
158 css::uno::XComponentContext
*,
159 css::uno::Sequence
<css::uno::Any
> const &)
161 return cppu::acquire(new OPackageStructureCreator());
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */