Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / appl / xpackcreator.cxx
blob825a334f30ee71d9554a65d304a2a53c59033d58
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/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>
36 using namespace css;
38 namespace {
40 class OPackageStructureCreator : public ::cppu::WeakImplHelper< embed::XPackageStructureCreator,
41 lang::XServiceInfo >
43 public:
44 OPackageStructureCreator() {}
46 // XPackageStructureCreator
47 virtual void SAL_CALL convertToPackage( const OUString& aFolderUrl, const uno::Reference< io::XOutputStream >& xTargetStream ) override;
49 // XServiceInfo
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();
71 try {
72 if ( aContent.isFolder() )
74 UCBStorage* pUCBStorage = new UCBStorage( aContent,
75 aFolderUrl,
76 StreamMode::READ,
77 false,
78 true );
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;
92 aStorage = nullptr;
94 pTempStream->Seek( 0 );
96 uno::Sequence< sal_Int8 > aSeq( 32000 );
97 sal_uInt32 nRead = 0;
98 do {
99 if ( aSeq.getLength() < 32000 )
100 aSeq.realloc( 32000 );
102 nRead = pTempStream->ReadBytes(aSeq.getArray(), 32000);
103 if ( nRead < 32000 )
104 aSeq.realloc( nRead );
105 xTargetStream->writeBytes( aSeq );
106 } while (pTempStream->good() && nRead);
108 if ( pTempStream->GetError() )
109 throw io::IOException();
111 bSuccess = true;
115 catch (const uno::RuntimeException&)
117 pTempStream.reset();
119 if ( !aTempURL.isEmpty() )
120 ::utl::UCBContentHelper::Kill( aTempURL );
122 throw;
124 catch (const io::IOException&)
126 pTempStream.reset();
128 if ( !aTempURL.isEmpty() )
129 ::utl::UCBContentHelper::Kill( aTempURL );
131 throw;
133 catch (const uno::Exception&)
137 pTempStream.reset();
139 if ( !aTempURL.isEmpty() )
140 ::utl::UCBContentHelper::Kill( aTempURL );
143 if ( !bSuccess )
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: */