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