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 :
23 :
24 : #include "xpackcreator.hxx"
25 :
26 : #include <comphelper/processfactory.hxx>
27 : #include <sot/stg.hxx>
28 : #include <sot/storage.hxx>
29 : #include <tools/stream.hxx>
30 : #include <unotools/tempfile.hxx>
31 : #include <unotools/ucbhelper.hxx>
32 : #include <ucbhelper/content.hxx>
33 : #include <ucbhelper/commandenvironment.hxx>
34 :
35 : using namespace ::com::sun::star;
36 :
37 : //-------------------------------------------------------------------------
38 0 : uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::impl_getStaticSupportedServiceNames()
39 : {
40 0 : uno::Sequence< ::rtl::OUString > aRet(2);
41 0 : aRet[0] = ::rtl::OUString("com.sun.star.embed.PackageStructureCreator");
42 0 : aRet[1] = ::rtl::OUString("com.sun.star.comp.embed.PackageStructureCreator");
43 0 : return aRet;
44 : }
45 :
46 : //-------------------------------------------------------------------------
47 44 : ::rtl::OUString SAL_CALL OPackageStructureCreator::impl_getStaticImplementationName()
48 : {
49 44 : return ::rtl::OUString("com.sun.star.comp.embed.PackageStructureCreator");
50 : }
51 :
52 : //-------------------------------------------------------------------------
53 0 : uno::Reference< lang::XSingleServiceFactory > SAL_CALL OPackageStructureCreator::impl_createFactory(
54 : const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
55 : {
56 : return ::cppu::createOneInstanceFactory( xServiceManager,
57 : OPackageStructureCreator::impl_getStaticImplementationName(),
58 : OPackageStructureCreator::impl_staticCreateSelfInstance,
59 0 : OPackageStructureCreator::impl_getStaticSupportedServiceNames() );
60 : }
61 :
62 : //-------------------------------------------------------------------------
63 0 : uno::Reference< uno::XInterface > SAL_CALL OPackageStructureCreator::impl_staticCreateSelfInstance(
64 : const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
65 : {
66 0 : return uno::Reference< uno::XInterface >( *new OPackageStructureCreator( xServiceManager ) );
67 : }
68 :
69 :
70 : //-------------------------------------------------------------------------
71 0 : void SAL_CALL OPackageStructureCreator::convertToPackage( const ::rtl::OUString& aFolderUrl,
72 : const uno::Reference< io::XOutputStream >& xTargetStream )
73 : throw ( io::IOException,
74 : uno::RuntimeException )
75 : {
76 0 : uno::Reference< ucb::XCommandEnvironment > xComEnv;
77 :
78 0 : if ( !xTargetStream.is() )
79 0 : throw io::IOException(); // TODO/LATER
80 :
81 0 : sal_Bool bSuccess = sal_False;
82 0 : ::ucbhelper::Content aContent;
83 0 : if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, comphelper::getProcessComponentContext(), aContent ) )
84 : {
85 0 : SvStream* pTempStream = NULL;
86 :
87 0 : ::rtl::OUString aTempURL = ::utl::TempFile().GetURL();
88 : try {
89 0 : if ( aContent.isFolder() )
90 : {
91 : UCBStorage* pUCBStorage = new UCBStorage( aContent,
92 : aFolderUrl,
93 : STREAM_READ,
94 : sal_False,
95 0 : sal_True );
96 0 : SotStorageRef aStorage = new SotStorage( pUCBStorage );
97 :
98 0 : if ( !aTempURL.isEmpty() )
99 : {
100 0 : pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE );
101 0 : SotStorageRef aTargetStorage = new SotStorage( sal_True, *pTempStream );
102 0 : aStorage->CopyTo( aTargetStorage );
103 0 : aTargetStorage->Commit();
104 :
105 0 : if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
106 0 : throw io::IOException();
107 :
108 0 : aTargetStorage = NULL;
109 0 : aStorage = NULL;
110 :
111 0 : pTempStream->Seek( 0 );
112 :
113 0 : uno::Sequence< sal_Int8 > aSeq( 32000 );
114 0 : sal_uInt32 nRead = 0;
115 0 : do {
116 0 : if ( aSeq.getLength() < 32000 )
117 0 : aSeq.realloc( 32000 );
118 :
119 0 : nRead = pTempStream->Read( aSeq.getArray(), 32000 );
120 0 : if ( nRead < 32000 )
121 0 : aSeq.realloc( nRead );
122 0 : xTargetStream->writeBytes( aSeq );
123 0 : } while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead );
124 :
125 0 : if ( pTempStream->GetError() )
126 0 : throw io::IOException();
127 :
128 0 : bSuccess = sal_True;
129 0 : }
130 : }
131 : }
132 0 : catch (const uno::RuntimeException&)
133 : {
134 0 : if ( pTempStream )
135 0 : delete pTempStream;
136 :
137 0 : if ( !aTempURL.isEmpty() )
138 0 : ::utl::UCBContentHelper::Kill( aTempURL );
139 :
140 0 : throw;
141 : }
142 0 : catch (const io::IOException&)
143 : {
144 0 : if ( pTempStream )
145 0 : delete pTempStream;
146 :
147 0 : if ( !aTempURL.isEmpty() )
148 0 : ::utl::UCBContentHelper::Kill( aTempURL );
149 :
150 0 : throw;
151 : }
152 0 : catch (const uno::Exception&)
153 : {
154 : }
155 :
156 0 : if ( pTempStream )
157 0 : delete pTempStream;
158 :
159 0 : if ( !aTempURL.isEmpty() )
160 0 : ::utl::UCBContentHelper::Kill( aTempURL );
161 : }
162 :
163 0 : if ( !bSuccess )
164 0 : throw io::IOException(); // TODO/LATER: can't proceed with creation
165 0 : }
166 :
167 : //-------------------------------------------------------------------------
168 0 : ::rtl::OUString SAL_CALL OPackageStructureCreator::getImplementationName()
169 : throw ( uno::RuntimeException )
170 : {
171 0 : return impl_getStaticImplementationName();
172 : }
173 :
174 : //-------------------------------------------------------------------------
175 0 : sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const ::rtl::OUString& ServiceName )
176 : throw ( uno::RuntimeException )
177 : {
178 0 : uno::Sequence< ::rtl::OUString > aSeq = impl_getStaticSupportedServiceNames();
179 :
180 0 : for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
181 0 : if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
182 0 : return sal_True;
183 :
184 0 : return sal_False;
185 : }
186 :
187 : //-------------------------------------------------------------------------
188 0 : uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
189 : throw ( uno::RuntimeException )
190 : {
191 0 : return impl_getStaticSupportedServiceNames();
192 : }
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|