xref: /AOO41X/main/svtools/source/misc/imageresourceaccess.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <svtools/imageresourceaccess.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir /** === begin UNO includes === **/
34*cdf0e10cSrcweir #include <com/sun/star/io/NotConnectedException.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/graphic/XGraphicProvider.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
38*cdf0e10cSrcweir /** === end UNO includes === **/
39*cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
40*cdf0e10cSrcweir #include <tools/stream.hxx>
41*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
42*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //........................................................................
45*cdf0e10cSrcweir namespace svt
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir //........................................................................
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     using namespace ::utl;
50*cdf0e10cSrcweir     using namespace ::comphelper;
51*cdf0e10cSrcweir     using namespace ::com::sun::star::io;
52*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
53*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
54*cdf0e10cSrcweir     using namespace ::com::sun::star::beans;
55*cdf0e10cSrcweir     using namespace ::com::sun::star::graphic;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	//====================================================================
58*cdf0e10cSrcweir 	//= StreamSupplier
59*cdf0e10cSrcweir 	//====================================================================
60*cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper2 <   XStream
61*cdf0e10cSrcweir                                     ,   XSeekable
62*cdf0e10cSrcweir                                     >   StreamSupplier_Base;
63*cdf0e10cSrcweir     class StreamSupplier : public StreamSupplier_Base
64*cdf0e10cSrcweir     {
65*cdf0e10cSrcweir     private:
66*cdf0e10cSrcweir         Reference< XInputStream >   m_xInput;
67*cdf0e10cSrcweir         Reference< XOutputStream >  m_xOutput;
68*cdf0e10cSrcweir         Reference< XSeekable >      m_xSeekable;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     public:
71*cdf0e10cSrcweir         StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput );
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     protected:
74*cdf0e10cSrcweir         // XStream
75*cdf0e10cSrcweir         virtual Reference< XInputStream > SAL_CALL getInputStream(  ) throw (RuntimeException);
76*cdf0e10cSrcweir         virtual Reference< XOutputStream > SAL_CALL getOutputStream(  ) throw (RuntimeException);
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         // XSeekable
79*cdf0e10cSrcweir         virtual void SAL_CALL seek( ::sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
80*cdf0e10cSrcweir         virtual ::sal_Int64 SAL_CALL getPosition(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
81*cdf0e10cSrcweir         virtual ::sal_Int64 SAL_CALL getLength(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
82*cdf0e10cSrcweir     };
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	//--------------------------------------------------------------------
85*cdf0e10cSrcweir     StreamSupplier::StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput )
86*cdf0e10cSrcweir         :m_xInput( _rxInput )
87*cdf0e10cSrcweir         ,m_xOutput( _rxOutput )
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         m_xSeekable = m_xSeekable.query( m_xInput );
90*cdf0e10cSrcweir         if ( !m_xSeekable.is() )
91*cdf0e10cSrcweir             m_xSeekable = m_xSeekable.query( m_xOutput );
92*cdf0e10cSrcweir         OSL_ENSURE( m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!" );
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     //--------------------------------------------------------------------
96*cdf0e10cSrcweir     Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream(  ) throw (RuntimeException)
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         return m_xInput;
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     //--------------------------------------------------------------------
102*cdf0e10cSrcweir     Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream(  ) throw (RuntimeException)
103*cdf0e10cSrcweir     {
104*cdf0e10cSrcweir         return m_xOutput;
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     //--------------------------------------------------------------------
108*cdf0e10cSrcweir     void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir         if ( !m_xSeekable.is() )
111*cdf0e10cSrcweir             throw NotConnectedException();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         m_xSeekable->seek( location );
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     //--------------------------------------------------------------------
117*cdf0e10cSrcweir     ::sal_Int64 SAL_CALL StreamSupplier::getPosition(  ) throw (IOException, RuntimeException)
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         if ( !m_xSeekable.is() )
120*cdf0e10cSrcweir             throw NotConnectedException();
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         return m_xSeekable->getPosition();
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     //--------------------------------------------------------------------
126*cdf0e10cSrcweir     ::sal_Int64 SAL_CALL StreamSupplier::getLength(  ) throw (IOException, RuntimeException)
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         if ( !m_xSeekable.is() )
129*cdf0e10cSrcweir             throw NotConnectedException();
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         return m_xSeekable->getLength();
132*cdf0e10cSrcweir     }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     //====================================================================
135*cdf0e10cSrcweir 	//= GraphicAccess
136*cdf0e10cSrcweir 	//====================================================================
137*cdf0e10cSrcweir 	//--------------------------------------------------------------------
138*cdf0e10cSrcweir     bool GraphicAccess::isSupportedURL( const ::rtl::OUString& _rURL )
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir         if  (   ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:resource/" ) ) == 0 )
141*cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:graphicrepository/" ) ) == 0 )
142*cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:standardimage/" ) ) == 0 )
143*cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.GraphicObject:" ) ) == 0 )
144*cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.extension://" ) ) == 0 )
145*cdf0e10cSrcweir             )
146*cdf0e10cSrcweir             return true;
147*cdf0e10cSrcweir         return false;
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 	//--------------------------------------------------------------------
151*cdf0e10cSrcweir     SvStream* GraphicAccess::getImageStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
152*cdf0e10cSrcweir     {
153*cdf0e10cSrcweir         SvStream* pReturn = NULL;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         try
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             // get a GraphicProvider
158*cdf0e10cSrcweir             Reference< XGraphicProvider > xProvider;
159*cdf0e10cSrcweir             if ( _rxORB.is() )
160*cdf0e10cSrcweir                 xProvider = xProvider.query( _rxORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ) ) );
161*cdf0e10cSrcweir             OSL_ENSURE( xProvider.is(), "GraphicAccess::getImageStream: could not create a graphic provider!" );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir             if ( !xProvider.is() )
164*cdf0e10cSrcweir                 return pReturn;
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir             // let it create a graphic from the given URL
167*cdf0e10cSrcweir             Sequence< PropertyValue > aMediaProperties( 1 );
168*cdf0e10cSrcweir             aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
169*cdf0e10cSrcweir             aMediaProperties[0].Value <<= _rImageResourceURL;
170*cdf0e10cSrcweir             Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
171*cdf0e10cSrcweir             OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" );
172*cdf0e10cSrcweir             if ( !xGraphic.is() )
173*cdf0e10cSrcweir                 return pReturn;
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir             // copy the graphic to a in-memory buffer
176*cdf0e10cSrcweir             SvMemoryStream* pMemBuffer = new SvMemoryStream;
177*cdf0e10cSrcweir             Reference< XStream > xBufferAccess = new StreamSupplier(
178*cdf0e10cSrcweir                 new OSeekableInputStreamWrapper( *pMemBuffer ),
179*cdf0e10cSrcweir                 new OSeekableOutputStreamWrapper( *pMemBuffer )
180*cdf0e10cSrcweir             );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir             aMediaProperties.realloc( 2 );
183*cdf0e10cSrcweir             aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OutputStream" ) );
184*cdf0e10cSrcweir             aMediaProperties[0].Value <<= xBufferAccess;
185*cdf0e10cSrcweir             aMediaProperties[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MimeType" ) );
186*cdf0e10cSrcweir             aMediaProperties[1].Value <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
187*cdf0e10cSrcweir             xProvider->storeGraphic( xGraphic, aMediaProperties );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir             pMemBuffer->Seek( 0 );
190*cdf0e10cSrcweir             pReturn = pMemBuffer;
191*cdf0e10cSrcweir         }
192*cdf0e10cSrcweir         catch( const Exception& )
193*cdf0e10cSrcweir         {
194*cdf0e10cSrcweir         	OSL_ENSURE( sal_False, "GraphicAccess::getImageStream: caught an exception!" );
195*cdf0e10cSrcweir         }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         return pReturn;
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 	//--------------------------------------------------------------------
201*cdf0e10cSrcweir     Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         return new OSeekableInputStreamWrapper( getImageStream( _rxORB, _rImageResourceURL ), sal_True );   // take ownership
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir //........................................................................
207*cdf0e10cSrcweir } // namespace svt
208*cdf0e10cSrcweir //........................................................................
209*cdf0e10cSrcweir 
210