Line data Source code
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/XOutputStream.hpp>
22 : #include <com/sun/star/embed/XPackageStructureCreator.hpp>
23 : #include <com/sun/star/lang/XServiceInfo.hpp>
24 :
25 : #include <comphelper/processfactory.hxx>
26 : #include <cppuhelper/implbase2.hxx>
27 : #include <cppuhelper/supportsservice.hxx>
28 : #include <rtl/ref.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 : #include <ucbhelper/commandenvironment.hxx>
36 :
37 : using namespace css;
38 :
39 : namespace {
40 :
41 0 : class OPackageStructureCreator : public ::cppu::WeakImplHelper2< embed::XPackageStructureCreator,
42 : lang::XServiceInfo >
43 : {
44 : public:
45 0 : OPackageStructureCreator() {}
46 :
47 : // XPackageStructureCreator
48 : virtual void SAL_CALL convertToPackage( const OUString& aFolderUrl, const uno::Reference< io::XOutputStream >& xTargetStream ) throw (io::IOException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
49 :
50 : // XServiceInfo
51 : virtual OUString SAL_CALL getImplementationName() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
52 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
53 : virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
54 : };
55 :
56 :
57 0 : void SAL_CALL OPackageStructureCreator::convertToPackage( const OUString& aFolderUrl,
58 : const uno::Reference< io::XOutputStream >& xTargetStream )
59 : throw ( io::IOException,
60 : uno::RuntimeException, std::exception )
61 : {
62 0 : uno::Reference< ucb::XCommandEnvironment > xComEnv;
63 :
64 0 : if ( !xTargetStream.is() )
65 0 : throw io::IOException(); // TODO/LATER
66 :
67 0 : bool bSuccess = false;
68 0 : ::ucbhelper::Content aContent;
69 0 : if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, comphelper::getProcessComponentContext(), aContent ) )
70 : {
71 0 : SvStream* pTempStream = NULL;
72 :
73 0 : OUString aTempURL = ::utl::TempFile().GetURL();
74 : try {
75 0 : if ( aContent.isFolder() )
76 : {
77 : UCBStorage* pUCBStorage = new UCBStorage( aContent,
78 : aFolderUrl,
79 : StreamMode::READ,
80 : false,
81 0 : true );
82 0 : tools::SvRef<SotStorage> aStorage = new SotStorage( pUCBStorage );
83 :
84 0 : if ( !aTempURL.isEmpty() )
85 : {
86 0 : pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE );
87 0 : tools::SvRef<SotStorage> aTargetStorage = new SotStorage( true, *pTempStream );
88 0 : aStorage->CopyTo( aTargetStorage );
89 0 : aTargetStorage->Commit();
90 :
91 0 : if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
92 0 : throw io::IOException();
93 :
94 0 : aTargetStorage = NULL;
95 0 : aStorage = NULL;
96 :
97 0 : pTempStream->Seek( 0 );
98 :
99 0 : uno::Sequence< sal_Int8 > aSeq( 32000 );
100 0 : sal_uInt32 nRead = 0;
101 0 : do {
102 0 : if ( aSeq.getLength() < 32000 )
103 0 : aSeq.realloc( 32000 );
104 :
105 0 : nRead = pTempStream->Read( aSeq.getArray(), 32000 );
106 0 : if ( nRead < 32000 )
107 0 : aSeq.realloc( nRead );
108 0 : xTargetStream->writeBytes( aSeq );
109 0 : } while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead );
110 :
111 0 : if ( pTempStream->GetError() )
112 0 : throw io::IOException();
113 :
114 0 : bSuccess = true;
115 0 : }
116 : }
117 : }
118 0 : catch (const uno::RuntimeException&)
119 : {
120 0 : if ( pTempStream )
121 0 : delete pTempStream;
122 :
123 0 : if ( !aTempURL.isEmpty() )
124 0 : ::utl::UCBContentHelper::Kill( aTempURL );
125 :
126 0 : throw;
127 : }
128 0 : catch (const io::IOException&)
129 : {
130 0 : if ( pTempStream )
131 0 : delete pTempStream;
132 :
133 0 : if ( !aTempURL.isEmpty() )
134 0 : ::utl::UCBContentHelper::Kill( aTempURL );
135 :
136 0 : throw;
137 : }
138 0 : catch (const uno::Exception&)
139 : {
140 : }
141 :
142 0 : if ( pTempStream )
143 0 : delete pTempStream;
144 :
145 0 : if ( !aTempURL.isEmpty() )
146 0 : ::utl::UCBContentHelper::Kill( aTempURL );
147 : }
148 :
149 0 : if ( !bSuccess )
150 0 : throw io::IOException(); // TODO/LATER: can't proceed with creation
151 0 : }
152 :
153 0 : OUString SAL_CALL OPackageStructureCreator::getImplementationName()
154 : throw ( uno::RuntimeException, std::exception )
155 : {
156 0 : return OUString("com.sun.star.comp.embed.PackageStructureCreator");
157 : }
158 :
159 0 : sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const OUString& ServiceName )
160 : throw ( uno::RuntimeException, std::exception )
161 : {
162 0 : return cppu::supportsService(this, ServiceName);
163 : }
164 :
165 0 : uno::Sequence< OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
166 : throw ( uno::RuntimeException, std::exception )
167 : {
168 0 : uno::Sequence< OUString > aRet(2);
169 0 : aRet[0] = "com.sun.star.embed.PackageStructureCreator";
170 0 : aRet[1] = "com.sun.star.comp.embed.PackageStructureCreator";
171 0 : return aRet;
172 : }
173 :
174 : }
175 :
176 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
177 0 : com_sun_star_comp_embed_PackageStructureCreator_get_implementation(
178 : css::uno::XComponentContext *,
179 : css::uno::Sequence<css::uno::Any> const &)
180 : {
181 0 : return cppu::acquire(new OPackageStructureCreator());
182 : }
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|