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