LibreOffice Module sfx2 (master) 1
xpackcreator.cxx
Go to the documentation of this file.
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
29#include <sot/stg.hxx>
30#include <sot/storage.hxx>
31#include <tools/stream.hxx>
32#include <unotools/tempfile.hxx>
34#include <ucbhelper/content.hxx>
35
36using namespace css;
37
38namespace {
39
40class OPackageStructureCreator : public ::cppu::WeakImplHelper< embed::XPackageStructureCreator,
41 lang::XServiceInfo >
42{
43public:
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
56void 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 OUString aTempURL = ::utl::CreateTempURL();
69 try {
70 if ( aContent.isFolder() )
71 {
72 UCBStorage* pUCBStorage = new UCBStorage( aContent,
73 aFolderUrl,
74 StreamMode::READ,
75 false,
76 true );
77 tools::SvRef<SotStorage> aStorage = new SotStorage( pUCBStorage );
78
79 if ( !aTempURL.isEmpty() )
80 {
81 SvFileStream aTempStream( aTempURL, StreamMode::STD_READWRITE );
82 tools::SvRef<SotStorage> aTargetStorage = new SotStorage( true, aTempStream );
83 aStorage->CopyTo( aTargetStorage.get() );
84 aTargetStorage->Commit();
85
86 if ( aStorage->GetError() || aTargetStorage->GetError() || aTempStream.GetError() )
87 throw io::IOException();
88
89 aTargetStorage = nullptr;
90 aStorage = nullptr;
91
92 aTempStream.Seek( 0 );
93
94 uno::Sequence< sal_Int8 > aSeq( 32000 );
95 sal_uInt32 nRead = 0;
96 do {
97 if ( aSeq.getLength() < 32000 )
98 aSeq.realloc( 32000 );
99
100 nRead = aTempStream.ReadBytes(aSeq.getArray(), 32000);
101 if ( nRead < 32000 )
102 aSeq.realloc( nRead );
103 xTargetStream->writeBytes( aSeq );
104 } while (aTempStream.good() && nRead);
105
106 if ( aTempStream.GetError() )
107 throw io::IOException();
108
109 bSuccess = true;
110 }
111 }
112 }
113 catch (const uno::RuntimeException&)
114 {
115 if ( !aTempURL.isEmpty() )
116 ::utl::UCBContentHelper::Kill( aTempURL );
117
118 throw;
119 }
120 catch (const io::IOException&)
121 {
122 if ( !aTempURL.isEmpty() )
123 ::utl::UCBContentHelper::Kill( aTempURL );
124
125 throw;
126 }
127 catch (const uno::Exception&)
128 {
129 }
130
131 if ( !aTempURL.isEmpty() )
132 ::utl::UCBContentHelper::Kill( aTempURL );
133 }
134
135 if ( !bSuccess )
136 throw io::IOException(); // TODO/LATER: can't proceed with creation
137}
138
139OUString SAL_CALL OPackageStructureCreator::getImplementationName()
140{
141 return "com.sun.star.comp.embed.PackageStructureCreator";
142}
143
144sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const OUString& ServiceName )
145{
146 return cppu::supportsService(this, ServiceName);
147}
148
149uno::Sequence< OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
150{
151 return { "com.sun.star.embed.PackageStructureCreator", "com.sun.star.comp.embed.PackageStructureCreator" };
152}
153
154}
155
156extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
158 css::uno::XComponentContext *,
159 css::uno::Sequence<css::uno::Any> const &)
160{
161 return cppu::acquire(new OPackageStructureCreator());
162}
163
164/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
T * get() const
static bool create(const OUString &rURL, const css::uno::Reference< css::ucb::XCommandEnvironment > &rEnv, const css::uno::Reference< css::uno::XComponentContext > &rCtx, Content &rContent)
Sequence< sal_Int8 > aSeq
Definition: lnkbase2.cxx:83
Reference< XComponentContext > getProcessComponentContext()
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_embed_PackageStructureCreator_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)