xref: /trunk/main/sfx2/source/appl/xpackcreator.cxx (revision d119d52d)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
27cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "xpackcreator.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <sot/stg.hxx>
33cdf0e10cSrcweir #include <sot/storage.hxx>
34cdf0e10cSrcweir #include <tools/stream.hxx>
35cdf0e10cSrcweir #include <unotools/tempfile.hxx>
36cdf0e10cSrcweir #include <unotools/ucbhelper.hxx>
37cdf0e10cSrcweir #include <ucbhelper/content.hxx>
38cdf0e10cSrcweir #include <ucbhelper/commandenvironment.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_getStaticSupportedServiceNames()43cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::impl_getStaticSupportedServiceNames()
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aRet(2);
46cdf0e10cSrcweir     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.PackageStructureCreator");
47cdf0e10cSrcweir     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator");
48cdf0e10cSrcweir     return aRet;
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_getStaticImplementationName()52cdf0e10cSrcweir ::rtl::OUString SAL_CALL OPackageStructureCreator::impl_getStaticImplementationName()
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.PackageStructureCreator");
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_createFactory(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)58cdf0e10cSrcweir uno::Reference< lang::XSingleServiceFactory > SAL_CALL OPackageStructureCreator::impl_createFactory(
59cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 	return ::cppu::createOneInstanceFactory( xServiceManager,
62cdf0e10cSrcweir 								OPackageStructureCreator::impl_getStaticImplementationName(),
63cdf0e10cSrcweir 								OPackageStructureCreator::impl_staticCreateSelfInstance,
64cdf0e10cSrcweir 								OPackageStructureCreator::impl_getStaticSupportedServiceNames() );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_staticCreateSelfInstance(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)68cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL OPackageStructureCreator::impl_staticCreateSelfInstance(
69cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	return uno::Reference< uno::XInterface >( *new OPackageStructureCreator( xServiceManager ) );
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 
75cdf0e10cSrcweir //-------------------------------------------------------------------------
convertToPackage(const::rtl::OUString & aFolderUrl,const uno::Reference<io::XOutputStream> & xTargetStream)76cdf0e10cSrcweir void SAL_CALL OPackageStructureCreator::convertToPackage( const ::rtl::OUString& aFolderUrl,
77cdf0e10cSrcweir 														  const uno::Reference< io::XOutputStream >& xTargetStream )
78cdf0e10cSrcweir 		throw ( io::IOException,
79cdf0e10cSrcweir 				uno::RuntimeException )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	uno::Reference< ucb::XCommandEnvironment > xComEnv;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	if ( !xTargetStream.is() )
84cdf0e10cSrcweir         throw io::IOException(); // TODO/LATER
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	sal_Bool bSuccess = sal_False;
87cdf0e10cSrcweir 	::ucbhelper::Content aContent;
88cdf0e10cSrcweir 	if( ::ucbhelper::Content::create( aFolderUrl, xComEnv, aContent ) )
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 		SvStream* pTempStream = NULL;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         ::rtl::OUString aTempURL = ::utl::TempFile().GetURL();
93cdf0e10cSrcweir 		try {
94cdf0e10cSrcweir 			if ( aContent.isFolder() )
95cdf0e10cSrcweir 			{
96cdf0e10cSrcweir 				UCBStorage* pUCBStorage = new UCBStorage( aContent,
97cdf0e10cSrcweir 												  		aFolderUrl,
98cdf0e10cSrcweir 												  		STREAM_READ,
99cdf0e10cSrcweir 												  		sal_False,
100cdf0e10cSrcweir 												  		sal_True );
101cdf0e10cSrcweir 				SotStorageRef aStorage = new SotStorage( pUCBStorage );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 				if ( aTempURL.getLength() )
104cdf0e10cSrcweir 				{
105cdf0e10cSrcweir             		pTempStream = new SvFileStream( aTempURL, STREAM_STD_READWRITE );
106cdf0e10cSrcweir 					SotStorageRef aTargetStorage = new SotStorage( sal_True, *pTempStream );
107cdf0e10cSrcweir 					aStorage->CopyTo( aTargetStorage );
108cdf0e10cSrcweir 					aTargetStorage->Commit();
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 					if ( aStorage->GetError() || aTargetStorage->GetError() || pTempStream->GetError() )
111cdf0e10cSrcweir 						throw io::IOException();
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 					aTargetStorage = NULL;
114cdf0e10cSrcweir 					aStorage = NULL;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 					pTempStream->Seek( 0 );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 					uno::Sequence< sal_Int8 > aSeq( 32000 );
119cdf0e10cSrcweir 					sal_uInt32 nRead = 0;
120cdf0e10cSrcweir             		do {
121cdf0e10cSrcweir 						if ( aSeq.getLength() < 32000 )
122cdf0e10cSrcweir 							aSeq.realloc( 32000 );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir                 		nRead = pTempStream->Read( aSeq.getArray(), 32000 );
125cdf0e10cSrcweir 						if ( nRead < 32000 )
126cdf0e10cSrcweir 							aSeq.realloc( nRead );
127cdf0e10cSrcweir                 		xTargetStream->writeBytes( aSeq );
128cdf0e10cSrcweir             		} while( !pTempStream->IsEof() && !pTempStream->GetError() && nRead );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 					if ( pTempStream->GetError() )
131cdf0e10cSrcweir 						throw io::IOException();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 					bSuccess = sal_True;
134cdf0e10cSrcweir 				}
135cdf0e10cSrcweir 			}
136cdf0e10cSrcweir     	}
137cdf0e10cSrcweir     	catch ( uno::RuntimeException& )
138cdf0e10cSrcweir     	{
139cdf0e10cSrcweir 			if ( pTempStream )
140cdf0e10cSrcweir 				delete pTempStream;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			if ( aTempURL.getLength() )
143cdf0e10cSrcweir 				::utl::UCBContentHelper::Kill( aTempURL );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 			throw;
146cdf0e10cSrcweir     	}
147cdf0e10cSrcweir     	catch ( io::IOException& )
148cdf0e10cSrcweir 		{
149cdf0e10cSrcweir 			if ( pTempStream )
150cdf0e10cSrcweir 				delete pTempStream;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 			if ( aTempURL.getLength() )
153cdf0e10cSrcweir 				::utl::UCBContentHelper::Kill( aTempURL );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			throw;
156cdf0e10cSrcweir 		}
157cdf0e10cSrcweir 		catch ( uno::Exception& )
158cdf0e10cSrcweir 		{
159cdf0e10cSrcweir 		}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 		if ( pTempStream )
162cdf0e10cSrcweir 			delete pTempStream;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 		if ( aTempURL.getLength() )
165cdf0e10cSrcweir 			::utl::UCBContentHelper::Kill( aTempURL );
166cdf0e10cSrcweir 	}
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	if ( !bSuccess )
169cdf0e10cSrcweir         throw io::IOException(); // TODO/LATER: can't proceed with creation
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir //-------------------------------------------------------------------------
getImplementationName()173cdf0e10cSrcweir ::rtl::OUString SAL_CALL OPackageStructureCreator::getImplementationName()
174cdf0e10cSrcweir 	throw ( uno::RuntimeException )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir 	return impl_getStaticImplementationName();
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)180cdf0e10cSrcweir sal_Bool SAL_CALL OPackageStructureCreator::supportsService( const ::rtl::OUString& ServiceName )
181cdf0e10cSrcweir 	throw ( uno::RuntimeException )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir 	uno::Sequence< ::rtl::OUString > aSeq = impl_getStaticSupportedServiceNames();
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
186cdf0e10cSrcweir     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
187cdf0e10cSrcweir         	return sal_True;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 	return sal_False;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir //-------------------------------------------------------------------------
getSupportedServiceNames()193cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OPackageStructureCreator::getSupportedServiceNames()
194cdf0e10cSrcweir 	throw ( uno::RuntimeException )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	return impl_getStaticSupportedServiceNames();
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199