1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
25
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>
35
36 using namespace css;
37
38 namespace {
39
40 class OPackageStructureCreator : public ::cppu::WeakImplHelper< embed::XPackageStructureCreator,
41 lang::XServiceInfo >
42 {
43 public:
OPackageStructureCreator()44 OPackageStructureCreator() {}
45
46 // XPackageStructureCreator
47 virtual void SAL_CALL convertToPackage( const OUString& aFolderUrl, const uno::Reference< io::XOutputStream >& xTargetStream ) override;
48
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;
53 };
54
55
convertToPackage(const OUString & aFolderUrl,const uno::Reference<io::XOutputStream> & xTargetStream)56 void SAL_CALL OPackageStructureCreator::convertToPackage( const OUString& aFolderUrl,
57 const uno::Reference< io::XOutputStream >& xTargetStream )
58 {
59 uno::Reference< ucb::XCommandEnvironment > xComEnv;
60
61 if ( !xTargetStream.is() )
62 throw io::IOException(); // TODO/LATER
63
64 bool bSuccess = false;
65 ::ucbhelper::Content aContent;
66 if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, comphelper::getProcessComponentContext(), aContent ) )
67 {
68 std::unique_ptr<SvStream> pTempStream;
69
70 OUString aTempURL = ::utl::TempFile().GetURL();
71 try {
72 if ( aContent.isFolder() )
73 {
74 UCBStorage* pUCBStorage = new UCBStorage( aContent,
75 aFolderUrl,
76 StreamMode::READ,
77 false,
78 true );
79 tools::SvRef<SotStorage> aStorage = new SotStorage( pUCBStorage );
80
81 if ( !aTempURL.isEmpty() )
82 {
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();
87
88 if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
89 throw io::IOException();
90
91 aTargetStorage = nullptr;
92 aStorage = nullptr;
93
94 pTempStream->Seek( 0 );
95
96 uno::Sequence< sal_Int8 > aSeq( 32000 );
97 sal_uInt32 nRead = 0;
98 do {
99 if ( aSeq.getLength() < 32000 )
100 aSeq.realloc( 32000 );
101
102 nRead = pTempStream->ReadBytes(aSeq.getArray(), 32000);
103 if ( nRead < 32000 )
104 aSeq.realloc( nRead );
105 xTargetStream->writeBytes( aSeq );
106 } while (pTempStream->good() && nRead);
107
108 if ( pTempStream->GetError() )
109 throw io::IOException();
110
111 bSuccess = true;
112 }
113 }
114 }
115 catch (const uno::RuntimeException&)
116 {
117 pTempStream.reset();
118
119 if ( !aTempURL.isEmpty() )
120 ::utl::UCBContentHelper::Kill( aTempURL );
121
122 throw;
123 }
124 catch (const io::IOException&)
125 {
126 pTempStream.reset();
127
128 if ( !aTempURL.isEmpty() )
129 ::utl::UCBContentHelper::Kill( aTempURL );
130
131 throw;
132 }
133 catch (const uno::Exception&)
134 {
135 }
136
137 pTempStream.reset();
138
139 if ( !aTempURL.isEmpty() )
140 ::utl::UCBContentHelper::Kill( aTempURL );
141 }
142
143 if ( !bSuccess )
144 throw io::IOException(); // TODO/LATER: can't proceed with creation
145 }
146
getImplementationName()147 OUString SAL_CALL OPackageStructureCreator::getImplementationName()
148 {
149 return "com.sun.star.comp.embed.PackageStructureCreator";
150 }
151
supportsService(const OUString & ServiceName)152 sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const OUString& ServiceName )
153 {
154 return cppu::supportsService(this, ServiceName);
155 }
156
getSupportedServiceNames()157 uno::Sequence< OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
158 {
159 return { "com.sun.star.embed.PackageStructureCreator", "com.sun.star.comp.embed.PackageStructureCreator" };
160 }
161
162 }
163
164 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_embed_PackageStructureCreator_get_implementation(css::uno::XComponentContext *,css::uno::Sequence<css::uno::Any> const &)165 com_sun_star_comp_embed_PackageStructureCreator_get_implementation(
166 css::uno::XComponentContext *,
167 css::uno::Sequence<css::uno::Any> const &)
168 {
169 return cppu::acquire(new OPackageStructureCreator());
170 }
171
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
173