xref: /aoo42x/main/svx/source/xml/xmlgrhlp.cxx (revision 72583341)
1f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f6e50924SAndrew Rist  * distributed with this work for additional information
6f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10f6e50924SAndrew Rist  *
11f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12f6e50924SAndrew Rist  *
13f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17f6e50924SAndrew Rist  * specific language governing permissions and limitations
18f6e50924SAndrew Rist  * under the License.
19f6e50924SAndrew Rist  *
20f6e50924SAndrew Rist  *************************************************************/
21f6e50924SAndrew Rist 
22f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
27cdf0e10cSrcweir #ifndef _COM_SUN_STAR_EMBED_ElementModes_HPP_
28cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
34cdf0e10cSrcweir #include <cppuhelper/compbase4.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
37cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
38cdf0e10cSrcweir #include <unotools/tempfile.hxx>
39cdf0e10cSrcweir #include <tools/debug.hxx>
40cdf0e10cSrcweir #include <vcl/cvtgrf.hxx>
41cdf0e10cSrcweir #include <vcl/gfxlink.hxx>
42cdf0e10cSrcweir #include <vcl/metaact.hxx>
43cdf0e10cSrcweir #include <tools/zcodec.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include "svtools/filter.hxx"
46cdf0e10cSrcweir #include "svx/xmlgrhlp.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <algorithm>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // -----------
51cdf0e10cSrcweir // - Defines -
52cdf0e10cSrcweir // -----------
53cdf0e10cSrcweir 
54cdf0e10cSrcweir using namespace com::sun::star;
55cdf0e10cSrcweir using namespace com::sun::star::uno;
56cdf0e10cSrcweir using namespace com::sun::star::io;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #define XML_GRAPHICSTORAGE_NAME		"Pictures"
61cdf0e10cSrcweir #define XML_PACKAGE_URL_BASE		"vnd.sun.star.Package:"
62cdf0e10cSrcweir #define XML_GRAPHICOBJECT_URL_BASE	"vnd.sun.star.GraphicObject:"
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // ---------------------------
65cdf0e10cSrcweir // - SvXMLGraphicInputStream -
66cdf0e10cSrcweir // ---------------------------
67cdf0e10cSrcweir 
ImplCheckForEPS(GDIMetaFile & rMtf)68cdf0e10cSrcweir const MetaCommentAction* ImplCheckForEPS( GDIMetaFile& rMtf )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	static ByteString aComment( (const sal_Char*)"EPSReplacementGraphic" );
71cdf0e10cSrcweir 	const MetaCommentAction* pComment = NULL;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	if ( ( rMtf.GetActionCount() >= 2 )
74cdf0e10cSrcweir 			&& ( rMtf.FirstAction()->GetType() == META_EPS_ACTION )
75cdf0e10cSrcweir 			&& ( ((const MetaAction*)rMtf.GetAction( 1 ))->GetType() == META_COMMENT_ACTION )
76cdf0e10cSrcweir 			&& ( ((const MetaCommentAction*)rMtf.GetAction( 1 ))->GetComment() == aComment ) )
77cdf0e10cSrcweir 		pComment = (const MetaCommentAction*)rMtf.GetAction( 1 );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	return pComment;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir class SvXMLGraphicInputStream : public::cppu::WeakImplHelper1< XInputStream >
83cdf0e10cSrcweir {
84cdf0e10cSrcweir private:
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     virtual sal_Int32	SAL_CALL	readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
87cdf0e10cSrcweir 	virtual sal_Int32	SAL_CALL	readSomeBytes(Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
88cdf0e10cSrcweir 	virtual void		SAL_CALL	skipBytes(sal_Int32 nBytesToSkip) throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
89cdf0e10cSrcweir 	virtual sal_Int32	SAL_CALL	available() throw(NotConnectedException, RuntimeException);
90cdf0e10cSrcweir 	virtual void		SAL_CALL	closeInput() throw(NotConnectedException, RuntimeException);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir private:
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     ::utl::TempFile                 maTmp;
95cdf0e10cSrcweir     Reference< XInputStream >       mxStmWrapper;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir                                     // not available
98cdf0e10cSrcweir                                     SvXMLGraphicInputStream();
99cdf0e10cSrcweir                                     SvXMLGraphicInputStream( const SvXMLGraphicInputStream& );
100cdf0e10cSrcweir     SvXMLGraphicInputStream&        operator==( SvXMLGraphicInputStream& );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir public:
103cdf0e10cSrcweir 
104cdf0e10cSrcweir                                     SvXMLGraphicInputStream( const ::rtl::OUString& rGraphicId );
105cdf0e10cSrcweir     virtual                         ~SvXMLGraphicInputStream();
106cdf0e10cSrcweir 
Exists() const107cdf0e10cSrcweir     sal_Bool                        Exists() const { return mxStmWrapper.is(); }
108cdf0e10cSrcweir };
109cdf0e10cSrcweir 
110cdf0e10cSrcweir // -----------------------------------------------------------------------------
111cdf0e10cSrcweir 
SvXMLGraphicInputStream(const::rtl::OUString & rGraphicId)112cdf0e10cSrcweir SvXMLGraphicInputStream::SvXMLGraphicInputStream( const ::rtl::OUString& rGraphicId )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir 	String			aGraphicId( rGraphicId );
115cdf0e10cSrcweir 	GraphicObject	aGrfObject( ByteString( aGraphicId, RTL_TEXTENCODING_ASCII_US ) );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     maTmp.EnableKillingFile();
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	if( aGrfObject.GetType() != GRAPHIC_NONE )
120cdf0e10cSrcweir 	{
121cdf0e10cSrcweir         SvStream* pStm = ::utl::UcbStreamHelper::CreateStream( maTmp.GetURL(), STREAM_WRITE | STREAM_TRUNC );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         if( pStm )
124cdf0e10cSrcweir         {
125cdf0e10cSrcweir 			Graphic			aGraphic( (Graphic&) aGrfObject.GetGraphic() );
126cdf0e10cSrcweir 			const GfxLink	aGfxLink( aGraphic.GetLink() );
127cdf0e10cSrcweir             sal_Bool        bRet = sal_False;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 			if( aGfxLink.GetDataSize() && aGfxLink.GetData() )
130cdf0e10cSrcweir             {
131cdf0e10cSrcweir 				pStm->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
132cdf0e10cSrcweir 			    bRet = ( pStm->GetError() == 0 );
133cdf0e10cSrcweir             }
134cdf0e10cSrcweir 			else
135cdf0e10cSrcweir 			{
136cdf0e10cSrcweir 				if( aGraphic.GetType() == GRAPHIC_BITMAP )
137cdf0e10cSrcweir 				{
138cdf0e10cSrcweir 					GraphicFilter*  pFilter = GraphicFilter::GetGraphicFilter();
139cdf0e10cSrcweir                     String          aFormat;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 					if( aGraphic.IsAnimated() )
142cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "gif" ) );
143cdf0e10cSrcweir 					else
144cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "png" ) );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 					bRet = ( pFilter->ExportGraphic( aGraphic, String(), *pStm, pFilter->GetExportFormatNumberForShortName( aFormat ) ) == 0 );
147cdf0e10cSrcweir 				}
148cdf0e10cSrcweir 				else if( aGraphic.GetType() == GRAPHIC_GDIMETAFILE )
149cdf0e10cSrcweir 				{
150cdf0e10cSrcweir 					pStm->SetVersion( SOFFICE_FILEFORMAT_8 );
151cdf0e10cSrcweir 					pStm->SetCompressMode( COMPRESSMODE_ZBITMAP );
152ddde725dSArmin Le Grand 					( (GDIMetaFile&) aGraphic.GetGDIMetaFile() ).Write( *pStm );
153cdf0e10cSrcweir     			    bRet = ( pStm->GetError() == 0 );
154cdf0e10cSrcweir 				}
155cdf0e10cSrcweir 			}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             if( bRet )
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir                 pStm->Seek( 0 );
160cdf0e10cSrcweir                 mxStmWrapper = new ::utl::OInputStreamWrapper( pStm, sal_True );
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir             else
163cdf0e10cSrcweir                 delete pStm;
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir // -----------------------------------------------------------------------------
169cdf0e10cSrcweir 
~SvXMLGraphicInputStream()170cdf0e10cSrcweir SvXMLGraphicInputStream::~SvXMLGraphicInputStream()
171cdf0e10cSrcweir {
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir // -----------------------------------------------------------------------------
175cdf0e10cSrcweir 
readBytes(Sequence<sal_Int8> & rData,sal_Int32 nBytesToRead)176cdf0e10cSrcweir sal_Int32 SAL_CALL SvXMLGraphicInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
177cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, RuntimeException )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     if( !mxStmWrapper.is() )
180cdf0e10cSrcweir         throw NotConnectedException();
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     return mxStmWrapper->readBytes( rData, nBytesToRead );
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir // -----------------------------------------------------------------------------
186cdf0e10cSrcweir 
readSomeBytes(Sequence<sal_Int8> & rData,sal_Int32 nMaxBytesToRead)187cdf0e10cSrcweir sal_Int32 SAL_CALL SvXMLGraphicInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
188cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, RuntimeException )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     if( !mxStmWrapper.is() )
191cdf0e10cSrcweir         throw NotConnectedException() ;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     return mxStmWrapper->readSomeBytes( rData, nMaxBytesToRead );
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir // -----------------------------------------------------------------------------
197cdf0e10cSrcweir 
skipBytes(sal_Int32 nBytesToSkip)198cdf0e10cSrcweir void SAL_CALL SvXMLGraphicInputStream::skipBytes( sal_Int32 nBytesToSkip )
199cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, RuntimeException )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     if( !mxStmWrapper.is() )
202cdf0e10cSrcweir         throw NotConnectedException() ;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     mxStmWrapper->skipBytes( nBytesToSkip );
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir // -----------------------------------------------------------------------------
208cdf0e10cSrcweir 
available()209cdf0e10cSrcweir sal_Int32 SAL_CALL SvXMLGraphicInputStream::available() throw( NotConnectedException, RuntimeException )
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     if( !mxStmWrapper.is() )
212cdf0e10cSrcweir         throw NotConnectedException() ;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     return mxStmWrapper->available();
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir // -----------------------------------------------------------------------------
218cdf0e10cSrcweir 
closeInput()219cdf0e10cSrcweir void SAL_CALL SvXMLGraphicInputStream::closeInput() throw( NotConnectedException, RuntimeException )
220cdf0e10cSrcweir {
221cdf0e10cSrcweir     if( !mxStmWrapper.is() )
222cdf0e10cSrcweir         throw NotConnectedException() ;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir     mxStmWrapper->closeInput();
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir // ----------------------------
228cdf0e10cSrcweir // - SvXMLGraphicOutputStream -
229cdf0e10cSrcweir // ----------------------------
230cdf0e10cSrcweir 
231cdf0e10cSrcweir class SvXMLGraphicOutputStream : public::cppu::WeakImplHelper1< XOutputStream >
232cdf0e10cSrcweir {
233cdf0e10cSrcweir private:
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     // XOutputStream
236cdf0e10cSrcweir     virtual void SAL_CALL           writeBytes( const Sequence< sal_Int8 >& rData ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
237cdf0e10cSrcweir     virtual void SAL_CALL           flush() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
238cdf0e10cSrcweir     virtual void SAL_CALL           closeOutput() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
239cdf0e10cSrcweir 
240cdf0e10cSrcweir private:
241cdf0e10cSrcweir 
242cdf0e10cSrcweir     ::utl::TempFile*                mpTmp;
243cdf0e10cSrcweir     SvStream*                       mpOStm;
244cdf0e10cSrcweir     Reference< XOutputStream >      mxStmWrapper;
245cdf0e10cSrcweir     GraphicObject                   maGrfObj;
246cdf0e10cSrcweir     sal_Bool                        mbClosed;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir                                     // not available
249cdf0e10cSrcweir                                     SvXMLGraphicOutputStream( const SvXMLGraphicOutputStream& );
250cdf0e10cSrcweir     SvXMLGraphicOutputStream&       operator==( SvXMLGraphicOutputStream& );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir public:
253cdf0e10cSrcweir 
254cdf0e10cSrcweir                                     SvXMLGraphicOutputStream();
255cdf0e10cSrcweir     virtual                         ~SvXMLGraphicOutputStream();
256cdf0e10cSrcweir 
Exists() const257cdf0e10cSrcweir     sal_Bool                        Exists() const { return mxStmWrapper.is(); }
258cdf0e10cSrcweir     const GraphicObject&            GetGraphicObject();
259cdf0e10cSrcweir };
260cdf0e10cSrcweir 
261cdf0e10cSrcweir // -----------------------------------------------------------------------------
262cdf0e10cSrcweir 
SvXMLGraphicOutputStream()263cdf0e10cSrcweir SvXMLGraphicOutputStream::SvXMLGraphicOutputStream() :
264cdf0e10cSrcweir     mpTmp( new ::utl::TempFile ),
265cdf0e10cSrcweir     mbClosed( sal_False )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir     mpTmp->EnableKillingFile();
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     mpOStm = ::utl::UcbStreamHelper::CreateStream( mpTmp->GetURL(), STREAM_WRITE | STREAM_TRUNC );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     if( mpOStm )
272cdf0e10cSrcweir         mxStmWrapper = new ::utl::OOutputStreamWrapper( *mpOStm );
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir // -----------------------------------------------------------------------------
276cdf0e10cSrcweir 
~SvXMLGraphicOutputStream()277cdf0e10cSrcweir SvXMLGraphicOutputStream::~SvXMLGraphicOutputStream()
278cdf0e10cSrcweir {
279cdf0e10cSrcweir     delete mpTmp;
280cdf0e10cSrcweir     delete mpOStm;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir // -----------------------------------------------------------------------------
284cdf0e10cSrcweir 
writeBytes(const Sequence<sal_Int8> & rData)285cdf0e10cSrcweir void SAL_CALL SvXMLGraphicOutputStream::writeBytes( const Sequence< sal_Int8 >& rData )
286cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
287cdf0e10cSrcweir {
288cdf0e10cSrcweir     if( !mxStmWrapper.is() )
289cdf0e10cSrcweir         throw NotConnectedException() ;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     mxStmWrapper->writeBytes( rData );
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir // -----------------------------------------------------------------------------
295cdf0e10cSrcweir 
flush()296cdf0e10cSrcweir void SAL_CALL SvXMLGraphicOutputStream::flush()
297cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     if( !mxStmWrapper.is() )
300cdf0e10cSrcweir         throw NotConnectedException() ;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     mxStmWrapper->flush();
303cdf0e10cSrcweir }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir // -----------------------------------------------------------------------------
306cdf0e10cSrcweir 
closeOutput()307cdf0e10cSrcweir void SAL_CALL SvXMLGraphicOutputStream::closeOutput()
308cdf0e10cSrcweir     throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
309cdf0e10cSrcweir {
310cdf0e10cSrcweir     if( !mxStmWrapper.is() )
311cdf0e10cSrcweir         throw NotConnectedException() ;
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     mxStmWrapper->closeOutput();
314cdf0e10cSrcweir     mxStmWrapper = Reference< XOutputStream >();
315cdf0e10cSrcweir 
316cdf0e10cSrcweir     mbClosed = sal_True;
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir // ------------------------------------------------------------------------------
320cdf0e10cSrcweir 
GetGraphicObject()321cdf0e10cSrcweir const GraphicObject& SvXMLGraphicOutputStream::GetGraphicObject()
322cdf0e10cSrcweir {
323cdf0e10cSrcweir     if( mbClosed && ( maGrfObj.GetType() == GRAPHIC_NONE ) && mpOStm )
324cdf0e10cSrcweir     {
325cdf0e10cSrcweir     	Graphic aGraphic;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         mpOStm->Seek( 0 );
328cdf0e10cSrcweir 		sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW;
329cdf0e10cSrcweir 		sal_uInt16 pDeterminedFormat = GRFILTER_FORMAT_DONTKNOW;
330cdf0e10cSrcweir         GraphicFilter::GetGraphicFilter()->ImportGraphic( aGraphic, String(), *mpOStm ,nFormat,&pDeterminedFormat );
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 		if (pDeterminedFormat == GRFILTER_FORMAT_DONTKNOW)
333cdf0e10cSrcweir 		{
334cdf0e10cSrcweir 			//Read the first two byte to check whether it is a gzipped stream, is so it may be in wmz or emz format
335cdf0e10cSrcweir 			//unzip them and try again
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 			sal_uInt8    sFirstBytes[ 2 ];
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 			mpOStm->Seek( STREAM_SEEK_TO_END );
340cdf0e10cSrcweir 			sal_uIntPtr nStreamLen = mpOStm->Tell();
341cdf0e10cSrcweir 			mpOStm->Seek( 0 );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 			if ( !nStreamLen )
344cdf0e10cSrcweir 			{
345cdf0e10cSrcweir 				SvLockBytes* pLockBytes = mpOStm->GetLockBytes();
346cdf0e10cSrcweir 				if ( pLockBytes  )
347cdf0e10cSrcweir 					pLockBytes->SetSynchronMode( sal_True );
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 				mpOStm->Seek( STREAM_SEEK_TO_END );
350cdf0e10cSrcweir 				nStreamLen = mpOStm->Tell();
351cdf0e10cSrcweir 				mpOStm->Seek( 0 );
352cdf0e10cSrcweir 			}
353cdf0e10cSrcweir 			if( nStreamLen >= 2 )
354cdf0e10cSrcweir 			{
355cdf0e10cSrcweir 				//read two byte
356cdf0e10cSrcweir 				mpOStm->Read( sFirstBytes, 2 );
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 				if( sFirstBytes[0] == 0x1f && sFirstBytes[1] == 0x8b )
359cdf0e10cSrcweir 				{
360cdf0e10cSrcweir 					SvMemoryStream* pDest = new SvMemoryStream;
361cdf0e10cSrcweir 					ZCodec aZCodec( 0x8000, 0x8000 );
362cdf0e10cSrcweir 					aZCodec.BeginCompression(ZCODEC_GZ_LIB);
363cdf0e10cSrcweir 					mpOStm->Seek( 0 );
364cdf0e10cSrcweir 					aZCodec.Decompress( *mpOStm, *pDest );
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 					if (aZCodec.EndCompression() && pDest )
367cdf0e10cSrcweir 					{
368cdf0e10cSrcweir 						pDest->Seek( STREAM_SEEK_TO_END );
369cdf0e10cSrcweir 						sal_uIntPtr nStreamLen_ = pDest->Tell();
370cdf0e10cSrcweir 						if (nStreamLen_)
371cdf0e10cSrcweir 						{
372cdf0e10cSrcweir 							pDest->Seek(0L);
373cdf0e10cSrcweir 					        GraphicFilter::GetGraphicFilter()->ImportGraphic( aGraphic, String(), *pDest ,nFormat,&pDeterminedFormat );
374cdf0e10cSrcweir 						}
375cdf0e10cSrcweir 					}
376cdf0e10cSrcweir 					delete pDest;
377cdf0e10cSrcweir 				}
378cdf0e10cSrcweir 			}
379cdf0e10cSrcweir 		}
380cdf0e10cSrcweir 
381cdf0e10cSrcweir 		maGrfObj = aGraphic;
382cdf0e10cSrcweir         if( maGrfObj.GetType() != GRAPHIC_NONE )
383cdf0e10cSrcweir 	    {
384cdf0e10cSrcweir 		    delete mpOStm, mpOStm = NULL;
385cdf0e10cSrcweir 			delete mpTmp, mpTmp = NULL;
386cdf0e10cSrcweir 		}
387cdf0e10cSrcweir     }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir     return maGrfObj;
390cdf0e10cSrcweir }
391cdf0e10cSrcweir 
392cdf0e10cSrcweir // ----------------------
393cdf0e10cSrcweir // - SvXMLGraphicHelper -
394cdf0e10cSrcweir // ----------------------
395cdf0e10cSrcweir 
SvXMLGraphicHelper(SvXMLGraphicHelperMode eCreateMode)396cdf0e10cSrcweir SvXMLGraphicHelper::SvXMLGraphicHelper( SvXMLGraphicHelperMode eCreateMode ) :
397cdf0e10cSrcweir 	::cppu::WeakComponentImplHelper2< ::com::sun::star::document::XGraphicObjectResolver,
398cdf0e10cSrcweir                                       ::com::sun::star::document::XBinaryStreamResolver >( maMutex )
399cdf0e10cSrcweir {
400cdf0e10cSrcweir 	Init( NULL, eCreateMode, sal_False );
401cdf0e10cSrcweir }
402cdf0e10cSrcweir 
SvXMLGraphicHelper()403cdf0e10cSrcweir SvXMLGraphicHelper::SvXMLGraphicHelper() :
404cdf0e10cSrcweir 	::cppu::WeakComponentImplHelper2< ::com::sun::star::document::XGraphicObjectResolver,
405cdf0e10cSrcweir                                       ::com::sun::star::document::XBinaryStreamResolver >( maMutex )
406cdf0e10cSrcweir {
407cdf0e10cSrcweir }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir // -----------------------------------------------------------------------------
410cdf0e10cSrcweir 
~SvXMLGraphicHelper()411cdf0e10cSrcweir SvXMLGraphicHelper::~SvXMLGraphicHelper()
412cdf0e10cSrcweir {
413cdf0e10cSrcweir }
414cdf0e10cSrcweir 
415cdf0e10cSrcweir // -----------------------------------------------------------------------------
416cdf0e10cSrcweir 
disposing()417cdf0e10cSrcweir void SAL_CALL SvXMLGraphicHelper::disposing()
418cdf0e10cSrcweir {
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir // -----------------------------------------------------------------------------
422cdf0e10cSrcweir 
ImplGetStreamNames(const::rtl::OUString & rURLStr,::rtl::OUString & rPictureStorageName,::rtl::OUString & rPictureStreamName)423cdf0e10cSrcweir sal_Bool SvXMLGraphicHelper::ImplGetStreamNames( const ::rtl::OUString& rURLStr,
424cdf0e10cSrcweir 												 ::rtl::OUString& rPictureStorageName,
425cdf0e10cSrcweir 												 ::rtl::OUString& rPictureStreamName )
426cdf0e10cSrcweir {
427cdf0e10cSrcweir 	String		aURLStr( rURLStr );
428cdf0e10cSrcweir 	sal_Bool	bRet = sal_False;
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 	if( aURLStr.Len() )
431cdf0e10cSrcweir 	{
432cdf0e10cSrcweir 		aURLStr = aURLStr.GetToken( aURLStr.GetTokenCount( ':' ) - 1, ':' );
433cdf0e10cSrcweir 		const sal_uInt32 nTokenCount = aURLStr.GetTokenCount( '/' );
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 		if( 1 == nTokenCount )
436cdf0e10cSrcweir 		{
437cdf0e10cSrcweir 			rPictureStorageName = String( RTL_CONSTASCII_USTRINGPARAM( XML_GRAPHICSTORAGE_NAME ) );
438cdf0e10cSrcweir 			rPictureStreamName = aURLStr;
439cdf0e10cSrcweir 			bRet = sal_True;
440cdf0e10cSrcweir 		}
441cdf0e10cSrcweir 		else if( 2 == nTokenCount )
442cdf0e10cSrcweir 		{
443cdf0e10cSrcweir 			rPictureStorageName = aURLStr.GetToken( 0, '/' );
444cdf0e10cSrcweir 
445cdf0e10cSrcweir 			DBG_ASSERT( rPictureStorageName.getLength() &&
446cdf0e10cSrcweir 					   rPictureStorageName.getStr()[ 0 ] != '#',
447cdf0e10cSrcweir 					   "invalid relative URL" );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 			rPictureStreamName = aURLStr.GetToken( 1, '/' );
450cdf0e10cSrcweir 			bRet = sal_True;
451cdf0e10cSrcweir 		}
452cdf0e10cSrcweir 		else
453cdf0e10cSrcweir 		{
454cdf0e10cSrcweir 			DBG_ERROR( "SvXMLGraphicHelper::ImplInsertGraphicURL: invalid scheme" );
455cdf0e10cSrcweir 		}
456cdf0e10cSrcweir 	}
457cdf0e10cSrcweir 
458cdf0e10cSrcweir 	return bRet;
459cdf0e10cSrcweir }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir // -----------------------------------------------------------------------------
462cdf0e10cSrcweir 
ImplGetGraphicStorage(const::rtl::OUString & rStorageName)463cdf0e10cSrcweir uno::Reference < embed::XStorage > SvXMLGraphicHelper::ImplGetGraphicStorage( const ::rtl::OUString& rStorageName )
464cdf0e10cSrcweir {
465cdf0e10cSrcweir     uno::Reference < embed::XStorage > xRetStorage;
466cdf0e10cSrcweir     if( mxRootStorage.is() )
467cdf0e10cSrcweir 	{
468cdf0e10cSrcweir         try
469cdf0e10cSrcweir         {
470cdf0e10cSrcweir             xRetStorage = mxRootStorage->openStorageElement(
471cdf0e10cSrcweir                 maCurStorageName = rStorageName,
472cdf0e10cSrcweir                 ( GRAPHICHELPER_MODE_WRITE == meCreateMode )
473cdf0e10cSrcweir                     ? embed::ElementModes::READWRITE
474cdf0e10cSrcweir                     : embed::ElementModes::READ );
475cdf0e10cSrcweir         }
476cdf0e10cSrcweir         catch ( uno::Exception& )
477cdf0e10cSrcweir         {
478cdf0e10cSrcweir         }
479cdf0e10cSrcweir         //#i43196# try again to open the storage element - this time readonly
480cdf0e10cSrcweir         if(!xRetStorage.is())
481cdf0e10cSrcweir         {
482cdf0e10cSrcweir             try
483cdf0e10cSrcweir             {
484cdf0e10cSrcweir                 xRetStorage = mxRootStorage->openStorageElement( maCurStorageName = rStorageName, embed::ElementModes::READ );
485cdf0e10cSrcweir             }
486cdf0e10cSrcweir             catch ( uno::Exception& )
487cdf0e10cSrcweir             {
488cdf0e10cSrcweir             }
489cdf0e10cSrcweir         }
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir 	return xRetStorage;
493cdf0e10cSrcweir }
494cdf0e10cSrcweir 
495cdf0e10cSrcweir // -----------------------------------------------------------------------------
496cdf0e10cSrcweir 
ImplGetGraphicStream(const::rtl::OUString & rPictureStorageName,const::rtl::OUString & rPictureStreamName,sal_Bool bTruncate)497cdf0e10cSrcweir SvxGraphicHelperStream_Impl SvXMLGraphicHelper::ImplGetGraphicStream( const ::rtl::OUString& rPictureStorageName,
498cdf0e10cSrcweir 															  const ::rtl::OUString& rPictureStreamName,
499cdf0e10cSrcweir 															  sal_Bool bTruncate )
500cdf0e10cSrcweir {
501cdf0e10cSrcweir     SvxGraphicHelperStream_Impl aRet;
502cdf0e10cSrcweir     aRet.xStorage = ImplGetGraphicStorage( rPictureStorageName );
503cdf0e10cSrcweir 
504cdf0e10cSrcweir     if( aRet.xStorage.is() )
505cdf0e10cSrcweir 	{
506cdf0e10cSrcweir         sal_Int32 nMode = embed::ElementModes::READ;
507cdf0e10cSrcweir         if ( GRAPHICHELPER_MODE_WRITE == meCreateMode )
508cdf0e10cSrcweir         {
509cdf0e10cSrcweir             nMode = embed::ElementModes::READWRITE;
510cdf0e10cSrcweir             if ( bTruncate )
511cdf0e10cSrcweir                 nMode |= embed::ElementModes::TRUNCATE;
512cdf0e10cSrcweir         }
513cdf0e10cSrcweir 
514cdf0e10cSrcweir         aRet.xStream = aRet.xStorage->openStreamElement( rPictureStreamName, nMode );
515cdf0e10cSrcweir         if( aRet.xStream.is() && ( GRAPHICHELPER_MODE_WRITE == meCreateMode ) )
516cdf0e10cSrcweir 		{
517cdf0e10cSrcweir //REMOVE				::rtl::OUString aPropName( RTL_CONSTASCII_USTRINGPARAM("Encrypted") );
518cdf0e10cSrcweir 			::rtl::OUString aPropName( RTL_CONSTASCII_USTRINGPARAM("UseCommonStoragePasswordEncryption") );
519cdf0e10cSrcweir             uno::Reference < beans::XPropertySet > xProps( aRet.xStream, uno::UNO_QUERY );
520cdf0e10cSrcweir             xProps->setPropertyValue( aPropName, uno::makeAny( sal_True) );
521cdf0e10cSrcweir 		}
522cdf0e10cSrcweir 	}
523cdf0e10cSrcweir 
524cdf0e10cSrcweir     return aRet;
525cdf0e10cSrcweir }
526cdf0e10cSrcweir 
527cdf0e10cSrcweir // -----------------------------------------------------------------------------
528cdf0e10cSrcweir 
ImplGetGraphicMimeType(const String & rFileName) const529cdf0e10cSrcweir String SvXMLGraphicHelper::ImplGetGraphicMimeType( const String& rFileName ) const
530cdf0e10cSrcweir {
531cdf0e10cSrcweir     struct XMLGraphicMimeTypeMapper
532cdf0e10cSrcweir     {
533cdf0e10cSrcweir 	    const char*	pExt;
534cdf0e10cSrcweir 	    const char*	pMimeType;
535cdf0e10cSrcweir     };
536cdf0e10cSrcweir 
537cdf0e10cSrcweir     static XMLGraphicMimeTypeMapper aMapper[] =
538cdf0e10cSrcweir     {
539cdf0e10cSrcweir         { "gif", "image/gif" },
540cdf0e10cSrcweir         { "png", "image/png" },
541cdf0e10cSrcweir         { "jpg", "image/jpeg" },
542cdf0e10cSrcweir         { "tif", "image/tiff" },
543cdf0e10cSrcweir         { "svg", "image/svg+xml" }
544cdf0e10cSrcweir     };
545cdf0e10cSrcweir 
546cdf0e10cSrcweir     String aMimeType;
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     if( ( rFileName.Len() >= 4 ) && ( rFileName.GetChar( rFileName.Len() - 4 ) == '.' ) )
549cdf0e10cSrcweir     {
550cdf0e10cSrcweir         const ByteString aExt( rFileName.Copy( rFileName.Len() - 3 ), RTL_TEXTENCODING_ASCII_US );
551cdf0e10cSrcweir 
552cdf0e10cSrcweir         for( long i = 0, nCount = sizeof( aMapper ) / sizeof( aMapper[ 0 ] ); ( i < nCount ) && !aMimeType.Len(); i++ )
553cdf0e10cSrcweir             if( aExt == aMapper[ i ].pExt )
554cdf0e10cSrcweir                 aMimeType = String( aMapper[ i ].pMimeType, RTL_TEXTENCODING_ASCII_US );
555cdf0e10cSrcweir     }
556cdf0e10cSrcweir 
557cdf0e10cSrcweir     return aMimeType;
558cdf0e10cSrcweir }
559cdf0e10cSrcweir 
560cdf0e10cSrcweir // -----------------------------------------------------------------------------
561cdf0e10cSrcweir 
ImplReadGraphic(const::rtl::OUString & rPictureStorageName,const::rtl::OUString & rPictureStreamName)562cdf0e10cSrcweir Graphic SvXMLGraphicHelper::ImplReadGraphic( const ::rtl::OUString& rPictureStorageName,
563cdf0e10cSrcweir 											 const ::rtl::OUString& rPictureStreamName )
564cdf0e10cSrcweir {
565cdf0e10cSrcweir 	Graphic				aGraphic;
566cdf0e10cSrcweir     SvxGraphicHelperStream_Impl aStream( ImplGetGraphicStream( rPictureStorageName, rPictureStreamName, sal_False ) );
567cdf0e10cSrcweir     if( aStream.xStream.is() )
568cdf0e10cSrcweir     {
569cdf0e10cSrcweir         SvStream* pStream = utl::UcbStreamHelper::CreateStream( aStream.xStream );
570cdf0e10cSrcweir         GraphicFilter::GetGraphicFilter()->ImportGraphic( aGraphic, String(), *pStream );
571cdf0e10cSrcweir         delete pStream;
572cdf0e10cSrcweir     }
573cdf0e10cSrcweir 
574cdf0e10cSrcweir 	return aGraphic;
575cdf0e10cSrcweir }
576cdf0e10cSrcweir 
577cdf0e10cSrcweir // -----------------------------------------------------------------------------
578cdf0e10cSrcweir 
ImplWriteGraphic(const::rtl::OUString & rPictureStorageName,const::rtl::OUString & rPictureStreamName,const::rtl::OUString & rGraphicId)579cdf0e10cSrcweir sal_Bool SvXMLGraphicHelper::ImplWriteGraphic( const ::rtl::OUString& rPictureStorageName,
580cdf0e10cSrcweir 											   const ::rtl::OUString& rPictureStreamName,
581cdf0e10cSrcweir 											   const ::rtl::OUString& rGraphicId )
582cdf0e10cSrcweir {
583cdf0e10cSrcweir 	String			aGraphicId( rGraphicId );
584cdf0e10cSrcweir 	GraphicObject	aGrfObject( ByteString( aGraphicId, RTL_TEXTENCODING_ASCII_US ) );
585cdf0e10cSrcweir 	sal_Bool		bRet = sal_False;
586cdf0e10cSrcweir 
587cdf0e10cSrcweir 	if( aGrfObject.GetType() != GRAPHIC_NONE )
588cdf0e10cSrcweir 	{
589cdf0e10cSrcweir         SvxGraphicHelperStream_Impl aStream( ImplGetGraphicStream( rPictureStorageName, rPictureStreamName, sal_False ) );
590cdf0e10cSrcweir         if( aStream.xStream.is() )
591cdf0e10cSrcweir         {
592cdf0e10cSrcweir 			Graphic			aGraphic( (Graphic&) aGrfObject.GetGraphic() );
593cdf0e10cSrcweir 			const GfxLink	aGfxLink( aGraphic.GetLink() );
594cdf0e10cSrcweir             const ::rtl::OUString  aMimeType( ImplGetGraphicMimeType( rPictureStreamName ) );
595cdf0e10cSrcweir             uno::Any        aAny;
596cdf0e10cSrcweir             uno::Reference < beans::XPropertySet > xProps( aStream.xStream, uno::UNO_QUERY );
597cdf0e10cSrcweir 
598cdf0e10cSrcweir             // set stream properties (MediaType/Compression)
599cdf0e10cSrcweir             if( aMimeType.getLength() )
600cdf0e10cSrcweir             {
601cdf0e10cSrcweir 	            aAny <<= aMimeType;
602cdf0e10cSrcweir                 xProps->setPropertyValue( String( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ), aAny );
603cdf0e10cSrcweir             }
604cdf0e10cSrcweir 
605cdf0e10cSrcweir             const sal_Bool bCompressed = ( ( 0 == aMimeType.getLength() ) || ( aMimeType == ::rtl::OUString::createFromAscii( "image/tiff" ) ) );
606cdf0e10cSrcweir             aAny <<= bCompressed;
607cdf0e10cSrcweir             xProps->setPropertyValue( String( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) ), aAny );
608cdf0e10cSrcweir 
609cdf0e10cSrcweir             SvStream* pStream = utl::UcbStreamHelper::CreateStream( aStream.xStream );
610cdf0e10cSrcweir 			if( aGfxLink.GetDataSize() && aGfxLink.GetData() )
611cdf0e10cSrcweir                 pStream->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
612cdf0e10cSrcweir 			else
613cdf0e10cSrcweir 			{
614cdf0e10cSrcweir 				if( aGraphic.GetType() == GRAPHIC_BITMAP )
615cdf0e10cSrcweir 				{
616cdf0e10cSrcweir 					GraphicFilter*  pFilter = GraphicFilter::GetGraphicFilter();
617cdf0e10cSrcweir                     String          aFormat;
618cdf0e10cSrcweir 
619cdf0e10cSrcweir 					if( aGraphic.IsAnimated() )
620cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "gif" ) );
621cdf0e10cSrcweir 					else
622cdf0e10cSrcweir 						aFormat = String( RTL_CONSTASCII_USTRINGPARAM( "png" ) );
623cdf0e10cSrcweir 
624cdf0e10cSrcweir                     bRet = ( pFilter->ExportGraphic( aGraphic, String(), *pStream,
625cdf0e10cSrcweir 													 pFilter->GetExportFormatNumberForShortName( aFormat ) ) == 0 );
626cdf0e10cSrcweir 				}
627cdf0e10cSrcweir 				else if( aGraphic.GetType() == GRAPHIC_GDIMETAFILE )
628cdf0e10cSrcweir 				{
629cdf0e10cSrcweir 					pStream->SetVersion( SOFFICE_FILEFORMAT_8 );
630cdf0e10cSrcweir 					pStream->SetCompressMode( COMPRESSMODE_ZBITMAP );
631cdf0e10cSrcweir 
632cdf0e10cSrcweir 					// SJ: first check if this metafile is just a eps file, then we will store the eps instead of svm
633cdf0e10cSrcweir 					GDIMetaFile& rMtf( (GDIMetaFile&)aGraphic.GetGDIMetaFile() );
634cdf0e10cSrcweir 					const MetaCommentAction* pComment = ImplCheckForEPS( rMtf );
635cdf0e10cSrcweir 					if ( pComment )
636cdf0e10cSrcweir 					{
637cdf0e10cSrcweir 						sal_uInt32	nSize = pComment->GetDataSize();
638cdf0e10cSrcweir 						const sal_uInt8* pData = pComment->GetData();
639cdf0e10cSrcweir 						if ( nSize && pData )
640cdf0e10cSrcweir 							pStream->Write( pData, nSize );
641cdf0e10cSrcweir 
642cdf0e10cSrcweir 						const MetaEPSAction* pAct = ( (const MetaEPSAction*)rMtf.FirstAction() );
643cdf0e10cSrcweir 						const GfxLink&		 rLink = pAct->GetLink();
644cdf0e10cSrcweir 
645cdf0e10cSrcweir 						pStream->Write( rLink.GetData(), rLink.GetDataSize() );
646cdf0e10cSrcweir 					}
647cdf0e10cSrcweir 					else
648ddde725dSArmin Le Grand 						rMtf.Write( *pStream );
649cdf0e10cSrcweir 
650cdf0e10cSrcweir 					bRet = ( pStream->GetError() == 0 );
651cdf0e10cSrcweir 				}
652cdf0e10cSrcweir 			}
653cdf0e10cSrcweir             uno::Reference < embed::XTransactedObject > xStorage(
654cdf0e10cSrcweir                                     aStream.xStorage, uno::UNO_QUERY);
655cdf0e10cSrcweir             delete pStream;
656cdf0e10cSrcweir             aStream.xStream->getOutputStream()->closeOutput();
657cdf0e10cSrcweir             if( xStorage.is() )
658cdf0e10cSrcweir                 xStorage->commit();
659cdf0e10cSrcweir 		}
660cdf0e10cSrcweir 	}
661cdf0e10cSrcweir 
662cdf0e10cSrcweir 	return bRet;
663cdf0e10cSrcweir }
664cdf0e10cSrcweir 
665cdf0e10cSrcweir // -----------------------------------------------------------------------------
666cdf0e10cSrcweir 
ImplInsertGraphicURL(const::rtl::OUString & rURLStr,sal_uInt32 nInsertPos,rtl::OUString & rRequestedFileName)667cdf0e10cSrcweir void SvXMLGraphicHelper::ImplInsertGraphicURL( const ::rtl::OUString& rURLStr, sal_uInt32 nInsertPos, rtl::OUString& rRequestedFileName )
668cdf0e10cSrcweir {
669cdf0e10cSrcweir 	rtl::OUString aURLString( rURLStr );
670cdf0e10cSrcweir 	::rtl::OUString	aPictureStorageName, aPictureStreamName;
671cdf0e10cSrcweir 	if( ( maURLSet.find( aURLString ) != maURLSet.end() ) )
672cdf0e10cSrcweir 	{
673cdf0e10cSrcweir 		URLPairVector::iterator aIter( maGrfURLs.begin() ), aEnd( maGrfURLs.end() );
674cdf0e10cSrcweir 		while( aIter != aEnd )
675cdf0e10cSrcweir 		{
676cdf0e10cSrcweir 			if( aURLString == (*aIter).first )
677cdf0e10cSrcweir 			{
678cdf0e10cSrcweir 				maGrfURLs[ nInsertPos ].second = (*aIter).second;
679cdf0e10cSrcweir 				aIter = aEnd;
680cdf0e10cSrcweir 			}
681cdf0e10cSrcweir 			else
682cdf0e10cSrcweir 				aIter++;
683cdf0e10cSrcweir 		}
684cdf0e10cSrcweir 	}
685cdf0e10cSrcweir 	else if( ImplGetStreamNames( aURLString, aPictureStorageName, aPictureStreamName ) )
686cdf0e10cSrcweir 	{
687cdf0e10cSrcweir 		URLPair& rURLPair = maGrfURLs[ nInsertPos ];
688cdf0e10cSrcweir 
689cdf0e10cSrcweir 		if( GRAPHICHELPER_MODE_READ == meCreateMode )
690cdf0e10cSrcweir 		{
691cdf0e10cSrcweir 			const GraphicObject aObj( ImplReadGraphic( aPictureStorageName, aPictureStreamName ) );
692cdf0e10cSrcweir 
693cdf0e10cSrcweir 			if( aObj.GetType() != GRAPHIC_NONE )
694cdf0e10cSrcweir 			{
695cdf0e10cSrcweir 				const static ::rtl::OUString aBaseURL( RTL_CONSTASCII_USTRINGPARAM( XML_GRAPHICOBJECT_URL_BASE ) );
696cdf0e10cSrcweir 
697cdf0e10cSrcweir 				maGrfObjs.push_back( aObj );
698cdf0e10cSrcweir 				rURLPair.second = aBaseURL;
699cdf0e10cSrcweir 				rURLPair.second += String( aObj.GetUniqueID().GetBuffer(), RTL_TEXTENCODING_ASCII_US );
700cdf0e10cSrcweir 			}
701cdf0e10cSrcweir 			else
702cdf0e10cSrcweir 				rURLPair.second = String();
703cdf0e10cSrcweir 		}
704cdf0e10cSrcweir 		else
705cdf0e10cSrcweir 		{
706cdf0e10cSrcweir 			const String		aGraphicObjectId( aPictureStreamName );
707cdf0e10cSrcweir             const ByteString    aAsciiObjectID( aGraphicObjectId, RTL_TEXTENCODING_ASCII_US );
708cdf0e10cSrcweir 			const GraphicObject	aGrfObject( aAsciiObjectID );
709cdf0e10cSrcweir 			if( aGrfObject.GetType() != GRAPHIC_NONE )
710cdf0e10cSrcweir 			{
711cdf0e10cSrcweir 				String			aStreamName( aGraphicObjectId );
712cdf0e10cSrcweir 				Graphic			aGraphic( (Graphic&) aGrfObject.GetGraphic() );
713cdf0e10cSrcweir 				const GfxLink	aGfxLink( aGraphic.GetLink() );
714cdf0e10cSrcweir 				String			aExtension;
715cdf0e10cSrcweir 
716cdf0e10cSrcweir 				if( aGfxLink.GetDataSize() )
717cdf0e10cSrcweir 				{
718cdf0e10cSrcweir 					switch( aGfxLink.GetType() )
719cdf0e10cSrcweir 					{
720cdf0e10cSrcweir 						case( GFX_LINK_TYPE_EPS_BUFFER ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".eps" ) ); break;
721cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_GIF ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".gif" ) ); break;
722*72583341SArmin Le Grand 
723*72583341SArmin Le Grand                         // #15508# added BMP type for better exports (checked, works)
724*72583341SArmin Le Grand                         case( GFX_LINK_TYPE_NATIVE_BMP ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".bmp" ) ); break;
725*72583341SArmin Le Grand 
726cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_JPG ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".jpg" ) ); break;
727cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_PNG ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".png" ) ); break;
728cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_TIF ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".tif" ) ); break;
729cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_WMF ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".wmf" ) ); break;
730cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_MET ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".met" ) ); break;
731cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_PCT ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".pct" ) ); break;
732cdf0e10cSrcweir 						case( GFX_LINK_TYPE_NATIVE_SVG ): aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".svg" ) ); break;
733cdf0e10cSrcweir 
734cdf0e10cSrcweir 						default:
735cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".grf" ) );
736cdf0e10cSrcweir 						break;
737cdf0e10cSrcweir 					}
738cdf0e10cSrcweir 				}
739cdf0e10cSrcweir 				else
740cdf0e10cSrcweir 				{
741cdf0e10cSrcweir 					if( aGrfObject.GetType() == GRAPHIC_BITMAP )
742cdf0e10cSrcweir 					{
743cdf0e10cSrcweir 						if( aGrfObject.IsAnimated() )
744cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".gif" ) );
745cdf0e10cSrcweir 						else
746cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".png" ) );
747cdf0e10cSrcweir 					}
748cdf0e10cSrcweir 					else if( aGrfObject.GetType() == GRAPHIC_GDIMETAFILE )
749cdf0e10cSrcweir 					{
750cdf0e10cSrcweir 						// SJ: first check if this metafile is just a eps file, then we will store the eps instead of svm
751cdf0e10cSrcweir 						GDIMetaFile& rMtf( (GDIMetaFile&)aGraphic.GetGDIMetaFile() );
752cdf0e10cSrcweir 						if ( ImplCheckForEPS( rMtf ) )
753cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".eps" ) );
754cdf0e10cSrcweir 						else
755cdf0e10cSrcweir 							aExtension = String( RTL_CONSTASCII_USTRINGPARAM( ".svm" ) );
756cdf0e10cSrcweir 					}
757cdf0e10cSrcweir 				}
758cdf0e10cSrcweir 
759cdf0e10cSrcweir 				rtl::OUString aURLEntry;
760cdf0e10cSrcweir 				const String sPictures( RTL_CONSTASCII_USTRINGPARAM( "Pictures/" ) );
761cdf0e10cSrcweir 
762cdf0e10cSrcweir 				if ( rRequestedFileName.getLength() )
763cdf0e10cSrcweir 				{
764cdf0e10cSrcweir 					aURLEntry = sPictures;
765cdf0e10cSrcweir 					aURLEntry += rRequestedFileName;
766cdf0e10cSrcweir 					aURLEntry += aExtension;
767cdf0e10cSrcweir 
768cdf0e10cSrcweir 					URLPairVector::iterator aIter( maGrfURLs.begin() ), aEnd( maGrfURLs.end() );
769cdf0e10cSrcweir 					while( aIter != aEnd )
770cdf0e10cSrcweir 					{
771cdf0e10cSrcweir 						if( aURLEntry == (*aIter).second )
772cdf0e10cSrcweir 							break;
773cdf0e10cSrcweir 						aIter++;
774cdf0e10cSrcweir 					}
775cdf0e10cSrcweir 					if ( aIter == aEnd )
776cdf0e10cSrcweir 						aStreamName = rRequestedFileName;
777cdf0e10cSrcweir 				}
778cdf0e10cSrcweir 
779cdf0e10cSrcweir 				aStreamName += aExtension;
780cdf0e10cSrcweir 
781cdf0e10cSrcweir 				if( mbDirect && aStreamName.Len() )
782cdf0e10cSrcweir 					ImplWriteGraphic( aPictureStorageName, aStreamName, aGraphicObjectId );
783cdf0e10cSrcweir 
784cdf0e10cSrcweir 				rURLPair.second = sPictures;
785cdf0e10cSrcweir 				rURLPair.second += aStreamName;
786cdf0e10cSrcweir 			}
787cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
788cdf0e10cSrcweir             else
789cdf0e10cSrcweir             {
790cdf0e10cSrcweir                 ByteString sMessage = "graphic object with ID '";
791cdf0e10cSrcweir                 sMessage += aAsciiObjectID;
792cdf0e10cSrcweir                 sMessage += "' has an unknown type";
793cdf0e10cSrcweir                 OSL_ENSURE( false, sMessage.GetBuffer() );
794cdf0e10cSrcweir             }
795cdf0e10cSrcweir #endif
796cdf0e10cSrcweir 		}
797cdf0e10cSrcweir 
798cdf0e10cSrcweir 		maURLSet.insert( aURLString );
799cdf0e10cSrcweir 	}
800cdf0e10cSrcweir }
801cdf0e10cSrcweir 
802cdf0e10cSrcweir // -----------------------------------------------------------------------------
803cdf0e10cSrcweir 
Init(const uno::Reference<embed::XStorage> & rXMLStorage,SvXMLGraphicHelperMode eCreateMode,sal_Bool bDirect)804cdf0e10cSrcweir void SvXMLGraphicHelper::Init( const uno::Reference < embed::XStorage >& rXMLStorage,
805cdf0e10cSrcweir 							   SvXMLGraphicHelperMode eCreateMode,
806cdf0e10cSrcweir 							   sal_Bool bDirect )
807cdf0e10cSrcweir {
808cdf0e10cSrcweir     mxRootStorage = rXMLStorage;
809cdf0e10cSrcweir 	meCreateMode = eCreateMode;
810cdf0e10cSrcweir 	mbDirect = ( ( GRAPHICHELPER_MODE_READ == meCreateMode ) ? bDirect : sal_True );
811cdf0e10cSrcweir }
812cdf0e10cSrcweir 
813cdf0e10cSrcweir // -----------------------------------------------------------------------------
814cdf0e10cSrcweir 
Create(const uno::Reference<embed::XStorage> & rXMLStorage,SvXMLGraphicHelperMode eCreateMode,sal_Bool bDirect)815cdf0e10cSrcweir SvXMLGraphicHelper* SvXMLGraphicHelper::Create( const uno::Reference < embed::XStorage >& rXMLStorage,
816cdf0e10cSrcweir 												SvXMLGraphicHelperMode eCreateMode,
817cdf0e10cSrcweir 												sal_Bool bDirect )
818cdf0e10cSrcweir {
819cdf0e10cSrcweir 	SvXMLGraphicHelper* pThis = new SvXMLGraphicHelper;
820cdf0e10cSrcweir 
821cdf0e10cSrcweir 	pThis->acquire();
822cdf0e10cSrcweir     pThis->Init( rXMLStorage, eCreateMode, bDirect );
823cdf0e10cSrcweir 
824cdf0e10cSrcweir 	return pThis;
825cdf0e10cSrcweir }
826cdf0e10cSrcweir 
827cdf0e10cSrcweir // -----------------------------------------------------------------------------
828cdf0e10cSrcweir 
Create(SvXMLGraphicHelperMode eCreateMode)829cdf0e10cSrcweir SvXMLGraphicHelper*	SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode eCreateMode )
830cdf0e10cSrcweir {
831cdf0e10cSrcweir 	SvXMLGraphicHelper* pThis = new SvXMLGraphicHelper;
832cdf0e10cSrcweir 
833cdf0e10cSrcweir 	pThis->acquire();
834cdf0e10cSrcweir 	pThis->Init( NULL, eCreateMode, sal_False );
835cdf0e10cSrcweir 
836cdf0e10cSrcweir 	return pThis;
837cdf0e10cSrcweir }
838cdf0e10cSrcweir 
839cdf0e10cSrcweir // -----------------------------------------------------------------------------
840cdf0e10cSrcweir 
Destroy(SvXMLGraphicHelper * pSvXMLGraphicHelper)841cdf0e10cSrcweir void SvXMLGraphicHelper::Destroy( SvXMLGraphicHelper* pSvXMLGraphicHelper )
842cdf0e10cSrcweir {
843cdf0e10cSrcweir 	if( pSvXMLGraphicHelper )
844cdf0e10cSrcweir 	{
845cdf0e10cSrcweir 		pSvXMLGraphicHelper->dispose();
846cdf0e10cSrcweir 		pSvXMLGraphicHelper->release();
847cdf0e10cSrcweir 	}
848cdf0e10cSrcweir }
849cdf0e10cSrcweir 
850cdf0e10cSrcweir // -----------------------------------------------------------------------------
851cdf0e10cSrcweir 
852cdf0e10cSrcweir // XGraphicObjectResolver
resolveGraphicObjectURL(const::rtl::OUString & rURL)853cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicHelper::resolveGraphicObjectURL( const ::rtl::OUString& rURL )
854cdf0e10cSrcweir 	throw(uno::RuntimeException)
855cdf0e10cSrcweir {
856cdf0e10cSrcweir 	::osl::MutexGuard   aGuard( maMutex );
857cdf0e10cSrcweir 	const sal_Int32     nIndex = maGrfURLs.size();
858cdf0e10cSrcweir 
859cdf0e10cSrcweir 	rtl::OUString aURL( rURL );
860cdf0e10cSrcweir 	rtl::OUString aUserData;
861cdf0e10cSrcweir 	rtl::OUString aRequestedFileName;
862cdf0e10cSrcweir 
863cdf0e10cSrcweir 	sal_Int32 nUser = rURL.indexOf( '?', 0 );
864cdf0e10cSrcweir 	if ( nUser >= 0 )
865cdf0e10cSrcweir 	{
866cdf0e10cSrcweir 		aURL = rtl::OUString( rURL.copy( 0, nUser ) );
867cdf0e10cSrcweir 		nUser++;
868cdf0e10cSrcweir 		aUserData = rURL.copy( nUser, rURL.getLength() - nUser );
869cdf0e10cSrcweir 	}
870cdf0e10cSrcweir 	if ( aUserData.getLength() )
871cdf0e10cSrcweir 	{
872cdf0e10cSrcweir         sal_Int32 nIndex2 = 0;
873cdf0e10cSrcweir         do
874cdf0e10cSrcweir         {
875cdf0e10cSrcweir 			rtl::OUString aToken = aUserData.getToken( 0, ';', nIndex2 );
876cdf0e10cSrcweir 			sal_Int32 n = aToken.indexOf( '=' );
877cdf0e10cSrcweir 			if ( ( n > 0 ) && ( ( n + 1 ) < aToken.getLength() ) )
878cdf0e10cSrcweir 			{
879cdf0e10cSrcweir 				rtl::OUString aParam( aToken.copy( 0, n ) );
880cdf0e10cSrcweir 				rtl::OUString aValue( aToken.copy( n + 1, aToken.getLength() - ( n + 1 ) ) );
881cdf0e10cSrcweir 
882cdf0e10cSrcweir 				const rtl::OUString sRequestedName( RTL_CONSTASCII_USTRINGPARAM("requestedName") );
883cdf0e10cSrcweir 				if ( aParam.match( sRequestedName ) )
884cdf0e10cSrcweir 					aRequestedFileName = aValue;
885cdf0e10cSrcweir 			}
886cdf0e10cSrcweir         }
887cdf0e10cSrcweir         while ( nIndex2 >= 0 );
888cdf0e10cSrcweir 	}
889cdf0e10cSrcweir 
890cdf0e10cSrcweir     maGrfURLs.push_back( ::std::make_pair( aURL, ::rtl::OUString() ) );
891cdf0e10cSrcweir 	ImplInsertGraphicURL( aURL, nIndex, aRequestedFileName );
892cdf0e10cSrcweir 
893cdf0e10cSrcweir     return maGrfURLs[ nIndex ].second;
894cdf0e10cSrcweir }
895cdf0e10cSrcweir 
896cdf0e10cSrcweir // -----------------------------------------------------------------------------
897cdf0e10cSrcweir 
898cdf0e10cSrcweir // XBinaryStreamResolver
getInputStream(const::rtl::OUString & rURL)899cdf0e10cSrcweir Reference< XInputStream > SAL_CALL SvXMLGraphicHelper::getInputStream( const ::rtl::OUString& rURL )
900cdf0e10cSrcweir     throw( RuntimeException )
901cdf0e10cSrcweir {
902cdf0e10cSrcweir     Reference< XInputStream >   xRet;
903cdf0e10cSrcweir     ::rtl::OUString                    aPictureStorageName, aGraphicId;
904cdf0e10cSrcweir 
905cdf0e10cSrcweir 
906cdf0e10cSrcweir 	if( ( GRAPHICHELPER_MODE_WRITE == meCreateMode ) &&
907cdf0e10cSrcweir         ImplGetStreamNames( rURL, aPictureStorageName, aGraphicId ) )
908cdf0e10cSrcweir     {
909cdf0e10cSrcweir         SvXMLGraphicInputStream* pInputStream = new SvXMLGraphicInputStream( aGraphicId );
910cdf0e10cSrcweir 
911cdf0e10cSrcweir         if( pInputStream->Exists() )
912cdf0e10cSrcweir             xRet = pInputStream;
913cdf0e10cSrcweir         else
914cdf0e10cSrcweir             delete pInputStream;
915cdf0e10cSrcweir     }
916cdf0e10cSrcweir 
917cdf0e10cSrcweir     return xRet;
918cdf0e10cSrcweir }
919cdf0e10cSrcweir 
920cdf0e10cSrcweir // -----------------------------------------------------------------------------
921cdf0e10cSrcweir 
createOutputStream()922cdf0e10cSrcweir Reference< XOutputStream > SAL_CALL SvXMLGraphicHelper::createOutputStream()
923cdf0e10cSrcweir     throw( RuntimeException )
924cdf0e10cSrcweir {
925cdf0e10cSrcweir     Reference< XOutputStream > xRet;
926cdf0e10cSrcweir 
927cdf0e10cSrcweir 	if( GRAPHICHELPER_MODE_READ == meCreateMode )
928cdf0e10cSrcweir     {
929cdf0e10cSrcweir         SvXMLGraphicOutputStream* pOutputStream = new SvXMLGraphicOutputStream;
930cdf0e10cSrcweir 
931cdf0e10cSrcweir         if( pOutputStream->Exists() )
932cdf0e10cSrcweir             maGrfStms.push_back( xRet = pOutputStream );
933cdf0e10cSrcweir         else
934cdf0e10cSrcweir             delete pOutputStream;
935cdf0e10cSrcweir     }
936cdf0e10cSrcweir 
937cdf0e10cSrcweir     return xRet;
938cdf0e10cSrcweir }
939cdf0e10cSrcweir 
940cdf0e10cSrcweir // -----------------------------------------------------------------------------
941cdf0e10cSrcweir 
resolveOutputStream(const Reference<XOutputStream> & rxBinaryStream)942cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicHelper::resolveOutputStream( const Reference< XOutputStream >& rxBinaryStream )
943cdf0e10cSrcweir     throw( RuntimeException )
944cdf0e10cSrcweir {
945cdf0e10cSrcweir     ::rtl::OUString aRet;
946cdf0e10cSrcweir 
947cdf0e10cSrcweir 	if( ( GRAPHICHELPER_MODE_READ == meCreateMode ) && rxBinaryStream.is() )
948cdf0e10cSrcweir     {
949cdf0e10cSrcweir         if( ::std::find( maGrfStms.begin(), maGrfStms.end(), rxBinaryStream ) != maGrfStms.end() )
950cdf0e10cSrcweir         {
951cdf0e10cSrcweir             SvXMLGraphicOutputStream* pOStm = static_cast< SvXMLGraphicOutputStream* >( rxBinaryStream.get() );
952cdf0e10cSrcweir 
953cdf0e10cSrcweir             if( pOStm )
954cdf0e10cSrcweir             {
955cdf0e10cSrcweir                 const GraphicObject&    rGrfObj = pOStm->GetGraphicObject();
956cdf0e10cSrcweir                 const ::rtl::OUString          aId( ::rtl::OUString::createFromAscii( rGrfObj.GetUniqueID().GetBuffer() ) );
957cdf0e10cSrcweir 
958cdf0e10cSrcweir                 if( aId.getLength() )
959cdf0e10cSrcweir                 {
960cdf0e10cSrcweir                     aRet = ::rtl::OUString::createFromAscii( XML_GRAPHICOBJECT_URL_BASE );
961cdf0e10cSrcweir                     aRet += aId;
962cdf0e10cSrcweir                 }
963cdf0e10cSrcweir             }
964cdf0e10cSrcweir         }
965cdf0e10cSrcweir     }
966cdf0e10cSrcweir 
967cdf0e10cSrcweir     return aRet;
968cdf0e10cSrcweir }
969cdf0e10cSrcweir 
970cdf0e10cSrcweir 
971cdf0e10cSrcweir // --------------------------------------------------------------------------------
972cdf0e10cSrcweir 
973cdf0e10cSrcweir // for instantiation via service manager
974cdf0e10cSrcweir namespace svx
975cdf0e10cSrcweir {
976cdf0e10cSrcweir 
977cdf0e10cSrcweir namespace impl
978cdf0e10cSrcweir {
979cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper4<
980cdf0e10cSrcweir         lang::XInitialization,
981cdf0e10cSrcweir         document::XGraphicObjectResolver,
982cdf0e10cSrcweir         document::XBinaryStreamResolver,
983cdf0e10cSrcweir         lang::XServiceInfo >
984cdf0e10cSrcweir     SvXMLGraphicImportExportHelper_Base;
985cdf0e10cSrcweir class MutexContainer
986cdf0e10cSrcweir {
987cdf0e10cSrcweir public:
988cdf0e10cSrcweir     virtual ~MutexContainer();
989cdf0e10cSrcweir 
990cdf0e10cSrcweir protected:
991cdf0e10cSrcweir     mutable ::osl::Mutex m_aMutex;
992cdf0e10cSrcweir };
~MutexContainer()993cdf0e10cSrcweir MutexContainer::~MutexContainer()
994cdf0e10cSrcweir {}
995cdf0e10cSrcweir } // namespace impl
996cdf0e10cSrcweir 
997cdf0e10cSrcweir class SvXMLGraphicImportExportHelper :
998cdf0e10cSrcweir     public impl::MutexContainer,
999cdf0e10cSrcweir     public impl::SvXMLGraphicImportExportHelper_Base
1000cdf0e10cSrcweir {
1001cdf0e10cSrcweir public:
1002cdf0e10cSrcweir     SvXMLGraphicImportExportHelper( SvXMLGraphicHelperMode eMode );
1003cdf0e10cSrcweir 
1004cdf0e10cSrcweir protected:
1005cdf0e10cSrcweir     // is called from WeakComponentImplHelper when XComponent::dispose() was
1006cdf0e10cSrcweir     // called from outside
1007cdf0e10cSrcweir     virtual void SAL_CALL disposing();
1008cdf0e10cSrcweir 
1009cdf0e10cSrcweir     // ____ XInitialization ____
1010cdf0e10cSrcweir     // one argument is allowed, which is the XStorage
1011cdf0e10cSrcweir     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
1012cdf0e10cSrcweir         throw (Exception,
1013cdf0e10cSrcweir                RuntimeException);
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir     // ____ XGraphicObjectResolver ____
1016cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL resolveGraphicObjectURL( const ::rtl::OUString& aURL )
1017cdf0e10cSrcweir         throw (RuntimeException);
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir     // ____ XBinaryStreamResolver ____
1020cdf0e10cSrcweir     virtual Reference< io::XInputStream > SAL_CALL getInputStream( const ::rtl::OUString& aURL )
1021cdf0e10cSrcweir         throw (RuntimeException);
1022cdf0e10cSrcweir     virtual Reference< io::XOutputStream > SAL_CALL createOutputStream()
1023cdf0e10cSrcweir         throw (RuntimeException);
1024cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL resolveOutputStream( const Reference< io::XOutputStream >& aBinaryStream )
1025cdf0e10cSrcweir         throw (RuntimeException);
1026cdf0e10cSrcweir 
1027cdf0e10cSrcweir     // ____ XServiceInfo ____
1028cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName()
1029cdf0e10cSrcweir         throw (RuntimeException);
1030cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
1031cdf0e10cSrcweir         throw (RuntimeException);
1032cdf0e10cSrcweir     virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames()
1033cdf0e10cSrcweir         throw (RuntimeException);
1034cdf0e10cSrcweir 
1035cdf0e10cSrcweir private:
1036cdf0e10cSrcweir     SvXMLGraphicHelperMode              m_eGraphicHelperMode;
1037cdf0e10cSrcweir     Reference< XGraphicObjectResolver > m_xGraphicObjectResolver;
1038cdf0e10cSrcweir     Reference< XBinaryStreamResolver >  m_xBinaryStreamResolver;
1039cdf0e10cSrcweir };
1040cdf0e10cSrcweir 
SvXMLGraphicImportExportHelper(SvXMLGraphicHelperMode eMode)1041cdf0e10cSrcweir SvXMLGraphicImportExportHelper::SvXMLGraphicImportExportHelper( SvXMLGraphicHelperMode eMode ) :
1042cdf0e10cSrcweir         impl::SvXMLGraphicImportExportHelper_Base( m_aMutex ),
1043cdf0e10cSrcweir         m_eGraphicHelperMode( eMode )
1044cdf0e10cSrcweir {}
1045cdf0e10cSrcweir 
disposing()1046cdf0e10cSrcweir void SAL_CALL SvXMLGraphicImportExportHelper::disposing()
1047cdf0e10cSrcweir {
1048cdf0e10cSrcweir     Reference< XComponent > xComp( m_xGraphicObjectResolver, UNO_QUERY );
1049cdf0e10cSrcweir     OSL_ASSERT( xComp.is());
1050cdf0e10cSrcweir     if( xComp.is())
1051cdf0e10cSrcweir         xComp->dispose();
1052cdf0e10cSrcweir     // m_xBinaryStreamResolver is a reference to the same object => don't call
1053cdf0e10cSrcweir     // dispose() again
1054cdf0e10cSrcweir }
1055cdf0e10cSrcweir 
1056cdf0e10cSrcweir // ____ XInitialization ____
initialize(const Sequence<Any> & aArguments)1057cdf0e10cSrcweir void SAL_CALL SvXMLGraphicImportExportHelper::initialize(
1058cdf0e10cSrcweir     const Sequence< Any >& aArguments )
1059cdf0e10cSrcweir     throw (Exception, RuntimeException)
1060cdf0e10cSrcweir {
1061cdf0e10cSrcweir     Reference< embed::XStorage > xStorage;
1062cdf0e10cSrcweir     if( aArguments.getLength() > 0 )
1063cdf0e10cSrcweir         aArguments[0] >>= xStorage;
1064cdf0e10cSrcweir 
1065cdf0e10cSrcweir     SvXMLGraphicHelper * pHelper( SvXMLGraphicHelper::Create( xStorage, m_eGraphicHelperMode ));
1066cdf0e10cSrcweir     m_xGraphicObjectResolver.set( pHelper );
1067cdf0e10cSrcweir     m_xBinaryStreamResolver.set( pHelper );
1068cdf0e10cSrcweir     // SvXMLGraphicHelper::Create calls acquire.  Since we have two references
1069cdf0e10cSrcweir     // now it is safe (and necessary) to undo this acquire
1070cdf0e10cSrcweir     pHelper->release();
1071cdf0e10cSrcweir }
1072cdf0e10cSrcweir 
1073cdf0e10cSrcweir // ____ XGraphicObjectResolver ____
resolveGraphicObjectURL(const::rtl::OUString & aURL)1074cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportExportHelper::resolveGraphicObjectURL( const ::rtl::OUString& aURL )
1075cdf0e10cSrcweir     throw (uno::RuntimeException)
1076cdf0e10cSrcweir {
1077cdf0e10cSrcweir     return m_xGraphicObjectResolver->resolveGraphicObjectURL( aURL );
1078cdf0e10cSrcweir }
1079cdf0e10cSrcweir 
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir // ____ XBinaryStreamResolver ____
getInputStream(const::rtl::OUString & aURL)1082cdf0e10cSrcweir Reference< io::XInputStream > SAL_CALL SvXMLGraphicImportExportHelper::getInputStream( const ::rtl::OUString& aURL )
1083cdf0e10cSrcweir     throw (uno::RuntimeException)
1084cdf0e10cSrcweir {
1085cdf0e10cSrcweir     return m_xBinaryStreamResolver->getInputStream( aURL );
1086cdf0e10cSrcweir }
createOutputStream()1087cdf0e10cSrcweir Reference< io::XOutputStream > SAL_CALL SvXMLGraphicImportExportHelper::createOutputStream()
1088cdf0e10cSrcweir     throw (uno::RuntimeException)
1089cdf0e10cSrcweir {
1090cdf0e10cSrcweir     return m_xBinaryStreamResolver->createOutputStream();
1091cdf0e10cSrcweir }
resolveOutputStream(const Reference<io::XOutputStream> & aBinaryStream)1092cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportExportHelper::resolveOutputStream( const Reference< io::XOutputStream >& aBinaryStream )
1093cdf0e10cSrcweir     throw (uno::RuntimeException)
1094cdf0e10cSrcweir {
1095cdf0e10cSrcweir     return m_xBinaryStreamResolver->resolveOutputStream( aBinaryStream );
1096cdf0e10cSrcweir }
1097cdf0e10cSrcweir 
1098cdf0e10cSrcweir // ____ XServiceInfo ____
getImplementationName()1099cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportExportHelper::getImplementationName()
1100cdf0e10cSrcweir     throw (uno::RuntimeException)
1101cdf0e10cSrcweir {
1102cdf0e10cSrcweir     if( m_eGraphicHelperMode == GRAPHICHELPER_MODE_READ )
1103cdf0e10cSrcweir         return SvXMLGraphicImportHelper_getImplementationName();
1104cdf0e10cSrcweir     return SvXMLGraphicExportHelper_getImplementationName();
1105cdf0e10cSrcweir }
supportsService(const::rtl::OUString & ServiceName)1106cdf0e10cSrcweir ::sal_Bool SAL_CALL SvXMLGraphicImportExportHelper::supportsService( const ::rtl::OUString& ServiceName )
1107cdf0e10cSrcweir     throw (uno::RuntimeException)
1108cdf0e10cSrcweir {
1109cdf0e10cSrcweir     Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames());
1110cdf0e10cSrcweir     const ::rtl::OUString * pBegin = aServiceNames.getConstArray();
1111cdf0e10cSrcweir     const ::rtl::OUString * pEnd = pBegin + aServiceNames.getLength();
1112cdf0e10cSrcweir     return (::std::find( pBegin, pEnd, ServiceName ) != pEnd);
1113cdf0e10cSrcweir }
getSupportedServiceNames()1114cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SvXMLGraphicImportExportHelper::getSupportedServiceNames()
1115cdf0e10cSrcweir     throw (uno::RuntimeException)
1116cdf0e10cSrcweir {
1117cdf0e10cSrcweir     if( m_eGraphicHelperMode == GRAPHICHELPER_MODE_READ )
1118cdf0e10cSrcweir         return SvXMLGraphicImportHelper_getSupportedServiceNames();
1119cdf0e10cSrcweir     return SvXMLGraphicExportHelper_getSupportedServiceNames();
1120cdf0e10cSrcweir }
1121cdf0e10cSrcweir 
1122cdf0e10cSrcweir // import
SvXMLGraphicImportHelper_createInstance(const Reference<XMultiServiceFactory> &)1123cdf0e10cSrcweir Reference< XInterface > SAL_CALL SvXMLGraphicImportHelper_createInstance(const Reference< XMultiServiceFactory > & /* rSMgr */ )
1124cdf0e10cSrcweir     throw( Exception )
1125cdf0e10cSrcweir {
1126cdf0e10cSrcweir     return static_cast< XWeak* >( new SvXMLGraphicImportExportHelper( GRAPHICHELPER_MODE_READ ));
1127cdf0e10cSrcweir }
SvXMLGraphicImportHelper_getImplementationName()1128cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicImportHelper_getImplementationName()
1129cdf0e10cSrcweir     throw()
1130cdf0e10cSrcweir {
1131cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.GraphicImportHelper" ));
1132cdf0e10cSrcweir }
SvXMLGraphicImportHelper_getSupportedServiceNames()1133cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SvXMLGraphicImportHelper_getSupportedServiceNames()
1134cdf0e10cSrcweir     throw()
1135cdf0e10cSrcweir {
1136cdf0e10cSrcweir     // XGraphicObjectResolver and XBinaryStreamResolver are not part of any service
1137cdf0e10cSrcweir     Sequence< ::rtl::OUString > aSupportedServiceNames( 2 );
1138cdf0e10cSrcweir     aSupportedServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.GraphicObjectResolver" ) );
1139cdf0e10cSrcweir     aSupportedServiceNames[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.BinaryStreamResolver" ) );
1140cdf0e10cSrcweir     return aSupportedServiceNames;
1141cdf0e10cSrcweir }
1142cdf0e10cSrcweir 
1143cdf0e10cSrcweir // export
SvXMLGraphicExportHelper_createInstance(const Reference<XMultiServiceFactory> &)1144cdf0e10cSrcweir Reference< XInterface > SAL_CALL SvXMLGraphicExportHelper_createInstance(const Reference< XMultiServiceFactory > & /* rSMgr */ )
1145cdf0e10cSrcweir     throw( Exception )
1146cdf0e10cSrcweir {
1147cdf0e10cSrcweir     return static_cast< XWeak* >( new SvXMLGraphicImportExportHelper( GRAPHICHELPER_MODE_WRITE ));
1148cdf0e10cSrcweir }
SvXMLGraphicExportHelper_getImplementationName()1149cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvXMLGraphicExportHelper_getImplementationName()
1150cdf0e10cSrcweir     throw()
1151cdf0e10cSrcweir {
1152cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.GraphicExportHelper" ));
1153cdf0e10cSrcweir }
SvXMLGraphicExportHelper_getSupportedServiceNames()1154cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SvXMLGraphicExportHelper_getSupportedServiceNames()
1155cdf0e10cSrcweir     throw()
1156cdf0e10cSrcweir {
1157cdf0e10cSrcweir     // XGraphicObjectResolver and XBinaryStreamResolver are not part of any service
1158cdf0e10cSrcweir     Sequence< ::rtl::OUString > aSupportedServiceNames( 2 );
1159cdf0e10cSrcweir     aSupportedServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.GraphicObjectResolver" ) );
1160cdf0e10cSrcweir     aSupportedServiceNames[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.BinaryStreamResolver" ) );
1161cdf0e10cSrcweir     return aSupportedServiceNames;
1162cdf0e10cSrcweir }
1163cdf0e10cSrcweir 
1164cdf0e10cSrcweir } // namespace svx
1165