Update ooo320-m1
[ooovba.git] / svtools / source / misc / imageresourceaccess.cxx
blob4847a8a480f7c890df56a7fb547a4a10df5de05e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: imageresourceaccess.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #ifndef SVTOOLS_SOURCE_MISC_IMAGERESOURCEACCESS_HXX
35 #include "imageresourceaccess.hxx"
36 #endif
38 /** === begin UNO includes === **/
39 #include <com/sun/star/io/NotConnectedException.hpp>
40 #include <com/sun/star/io/XSeekable.hpp>
41 #include <com/sun/star/graphic/XGraphicProvider.hpp>
42 #include <com/sun/star/io/XStream.hpp>
43 /** === end UNO includes === **/
44 #include <unotools/ucbstreamhelper.hxx>
45 #include <tools/stream.hxx>
46 #include <unotools/streamwrap.hxx>
47 #include <cppuhelper/implbase2.hxx>
49 //........................................................................
50 namespace svt
52 //........................................................................
54 #define GRAPHOBJ_URLPREFIX "vnd.sun.star.GraphicObject:"
56 using namespace ::utl;
57 using namespace ::comphelper;
58 using namespace ::com::sun::star::io;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::beans;
62 using namespace ::com::sun::star::graphic;
64 //====================================================================
65 //= StreamSupplier
66 //====================================================================
67 typedef ::cppu::WeakImplHelper2 < XStream
68 , XSeekable
69 > StreamSupplier_Base;
70 class StreamSupplier : public StreamSupplier_Base
72 private:
73 Reference< XInputStream > m_xInput;
74 Reference< XOutputStream > m_xOutput;
75 Reference< XSeekable > m_xSeekable;
77 public:
78 StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput );
80 protected:
81 // XStream
82 virtual Reference< XInputStream > SAL_CALL getInputStream( ) throw (RuntimeException);
83 virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException);
85 // XSeekable
86 virtual void SAL_CALL seek( ::sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
87 virtual ::sal_Int64 SAL_CALL getPosition( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
88 virtual ::sal_Int64 SAL_CALL getLength( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
91 //--------------------------------------------------------------------
92 StreamSupplier::StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput )
93 :m_xInput( _rxInput )
94 ,m_xOutput( _rxOutput )
96 m_xSeekable = m_xSeekable.query( m_xInput );
97 if ( !m_xSeekable.is() )
98 m_xSeekable = m_xSeekable.query( m_xOutput );
99 OSL_ENSURE( m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!" );
102 //--------------------------------------------------------------------
103 Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream( ) throw (RuntimeException)
105 return m_xInput;
108 //--------------------------------------------------------------------
109 Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream( ) throw (RuntimeException)
111 return m_xOutput;
114 //--------------------------------------------------------------------
115 void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
117 if ( !m_xSeekable.is() )
118 throw NotConnectedException();
120 m_xSeekable->seek( location );
123 //--------------------------------------------------------------------
124 ::sal_Int64 SAL_CALL StreamSupplier::getPosition( ) throw (IOException, RuntimeException)
126 if ( !m_xSeekable.is() )
127 throw NotConnectedException();
129 return m_xSeekable->getPosition();
132 //--------------------------------------------------------------------
133 ::sal_Int64 SAL_CALL StreamSupplier::getLength( ) throw (IOException, RuntimeException)
135 if ( !m_xSeekable.is() )
136 throw NotConnectedException();
138 return m_xSeekable->getLength();
141 //====================================================================
142 //= GraphicAccess
143 //====================================================================
144 //--------------------------------------------------------------------
145 bool GraphicAccess::isSupportedURL( const ::rtl::OUString& _rURL )
147 ::rtl::OUString sIndicator( RTL_CONSTASCII_USTRINGPARAM( "private:resource/" ) );
148 return ( ( _rURL.indexOf( sIndicator ) == 0 ) || ( _rURL.compareToAscii( GRAPHOBJ_URLPREFIX, RTL_CONSTASCII_LENGTH( GRAPHOBJ_URLPREFIX ) ) == 0 ) );
151 //--------------------------------------------------------------------
152 SvStream* GraphicAccess::getImageStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
154 SvStream* pReturn = NULL;
158 // get a GraphicProvider
159 Reference< XGraphicProvider > xProvider;
160 if ( _rxORB.is() )
161 xProvider = xProvider.query( _rxORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ) ) );
162 OSL_ENSURE( xProvider.is(), "GraphicAccess::getImageStream: could not create a graphic provider!" );
164 if ( !xProvider.is() )
165 return pReturn;
167 // let it create a graphic from the given URL
168 Sequence< PropertyValue > aMediaProperties( 1 );
169 aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
170 aMediaProperties[0].Value <<= _rImageResourceURL;
171 Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
172 OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" );
173 if ( !xGraphic.is() )
174 return pReturn;
176 // copy the graphic to a in-memory buffer
177 SvMemoryStream* pMemBuffer = new SvMemoryStream;
178 Reference< XStream > xBufferAccess = new StreamSupplier(
179 new OSeekableInputStreamWrapper( *pMemBuffer ),
180 new OSeekableOutputStreamWrapper( *pMemBuffer )
183 aMediaProperties.realloc( 2 );
184 aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OutputStream" ) );
185 aMediaProperties[0].Value <<= xBufferAccess;
186 aMediaProperties[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MimeType" ) );
187 aMediaProperties[1].Value <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
188 xProvider->storeGraphic( xGraphic, aMediaProperties );
190 pMemBuffer->Seek( 0 );
191 pReturn = pMemBuffer;
193 catch( const Exception& )
195 OSL_ENSURE( sal_False, "GraphicAccess::getImageStream: caught an exception!" );
198 return pReturn;
201 //--------------------------------------------------------------------
202 Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
204 return new OSeekableInputStreamWrapper( getImageStream( _rxORB, _rImageResourceURL ), sal_True ); // take ownership
207 //........................................................................
208 } // namespace svt
209 //........................................................................