xref: /trunk/main/sfx2/source/appl/xpackcreator.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sfx2.hxx"
30 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 
33 
34 #include "xpackcreator.hxx"
35 
36 #include <sot/stg.hxx>
37 #include <sot/storage.hxx>
38 #include <tools/stream.hxx>
39 #include <unotools/tempfile.hxx>
40 #include <unotools/ucbhelper.hxx>
41 #include <ucbhelper/content.hxx>
42 #include <ucbhelper/commandenvironment.hxx>
43 
44 using namespace ::com::sun::star;
45 
46 //-------------------------------------------------------------------------
47 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::impl_getStaticSupportedServiceNames()
48 {
49     uno::Sequence< ::rtl::OUString > aRet(2);
50     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.PackageStructureCreator");
51     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator");
52     return aRet;
53 }
54 
55 //-------------------------------------------------------------------------
56 ::rtl::OUString SAL_CALL OPackageStructureCreator::impl_getStaticImplementationName()
57 {
58     return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator");
59 }
60 
61 //-------------------------------------------------------------------------
62 uno::Reference< lang::XSingleServiceFactory > SAL_CALL OPackageStructureCreator::impl_createFactory(
63 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
64 {
65 	return ::cppu::createOneInstanceFactory( xServiceManager,
66 								OPackageStructureCreator::impl_getStaticImplementationName(),
67 								OPackageStructureCreator::impl_staticCreateSelfInstance,
68 								OPackageStructureCreator::impl_getStaticSupportedServiceNames() );
69 }
70 
71 //-------------------------------------------------------------------------
72 uno::Reference< uno::XInterface > SAL_CALL OPackageStructureCreator::impl_staticCreateSelfInstance(
73 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
74 {
75 	return uno::Reference< uno::XInterface >( *new OPackageStructureCreator( xServiceManager ) );
76 }
77 
78 
79 //-------------------------------------------------------------------------
80 void SAL_CALL OPackageStructureCreator::convertToPackage( const ::rtl::OUString& aFolderUrl,
81 														  const uno::Reference< io::XOutputStream >& xTargetStream )
82 		throw ( io::IOException,
83 				uno::RuntimeException )
84 {
85 	uno::Reference< ucb::XCommandEnvironment > xComEnv;
86 
87 	if ( !xTargetStream.is() )
88         throw io::IOException(); // TODO/LATER
89 
90 	sal_Bool bSuccess = sal_False;
91 	::ucbhelper::Content aContent;
92 	if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, aContent ) )
93 	{
94 		SvStream* pTempStream = NULL;
95 
96         ::rtl::OUString aTempURL = ::utl::TempFile().GetURL();
97 		try {
98 			if ( aContent.isFolder() )
99 			{
100 				UCBStorage* pUCBStorage = new UCBStorage( aContent,
101 												  		aFolderUrl,
102 												  		STREAM_READ,
103 												  		sal_False,
104 												  		sal_True );
105 				SotStorageRef aStorage = new SotStorage( pUCBStorage );
106 
107 				if ( aTempURL.getLength() )
108 				{
109             		pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE );
110 					SotStorageRef aTargetStorage = new SotStorage( sal_True, *pTempStream );
111 					aStorage->CopyTo( aTargetStorage );
112 					aTargetStorage->Commit();
113 
114 					if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
115 						throw io::IOException();
116 
117 					aTargetStorage = NULL;
118 					aStorage = NULL;
119 
120 					pTempStream->Seek( 0 );
121 
122 					uno::Sequence< sal_Int8 > aSeq( 32000 );
123 					sal_uInt32 nRead = 0;
124             		do {
125 						if ( aSeq.getLength() < 32000 )
126 							aSeq.realloc( 32000 );
127 
128                 		nRead = pTempStream->Read( aSeq.getArray(), 32000 );
129 						if ( nRead < 32000 )
130 							aSeq.realloc( nRead );
131                 		xTargetStream->writeBytes( aSeq );
132             		} while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead );
133 
134 					if ( pTempStream->GetError() )
135 						throw io::IOException();
136 
137 					bSuccess = sal_True;
138 				}
139 			}
140     	}
141     	catch ( uno::RuntimeException& )
142     	{
143 			if ( pTempStream )
144 				delete pTempStream;
145 
146 			if ( aTempURL.getLength() )
147 				::utl::UCBContentHelper::Kill( aTempURL );
148 
149 			throw;
150     	}
151     	catch ( io::IOException& )
152 		{
153 			if ( pTempStream )
154 				delete pTempStream;
155 
156 			if ( aTempURL.getLength() )
157 				::utl::UCBContentHelper::Kill( aTempURL );
158 
159 			throw;
160 		}
161 		catch ( uno::Exception& )
162 		{
163 		}
164 
165 		if ( pTempStream )
166 			delete pTempStream;
167 
168 		if ( aTempURL.getLength() )
169 			::utl::UCBContentHelper::Kill( aTempURL );
170 	}
171 
172 	if ( !bSuccess )
173         throw io::IOException(); // TODO/LATER: can't proceed with creation
174 }
175 
176 //-------------------------------------------------------------------------
177 ::rtl::OUString SAL_CALL OPackageStructureCreator::getImplementationName()
178 	throw ( uno::RuntimeException )
179 {
180 	return impl_getStaticImplementationName();
181 }
182 
183 //-------------------------------------------------------------------------
184 sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const ::rtl::OUString& ServiceName )
185 	throw ( uno::RuntimeException )
186 {
187 	uno::Sequence< ::rtl::OUString > aSeq = impl_getStaticSupportedServiceNames();
188 
189 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
190     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
191         	return sal_True;
192 
193 	return sal_False;
194 }
195 
196 //-------------------------------------------------------------------------
197 uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
198 	throw ( uno::RuntimeException )
199 {
200 	return impl_getStaticSupportedServiceNames();
201 }
202 
203