1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5900e8ecSAndrew Rist  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5900e8ecSAndrew Rist  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19*5900e8ecSAndrew Rist  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <svtools/imageresourceaccess.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /** === begin UNO includes === **/
30cdf0e10cSrcweir #include <com/sun/star/io/NotConnectedException.hpp>
31cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
32cdf0e10cSrcweir #include <com/sun/star/graphic/XGraphicProvider.hpp>
33cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
34cdf0e10cSrcweir /** === end UNO includes === **/
35cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
36cdf0e10cSrcweir #include <tools/stream.hxx>
37cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
38cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //........................................................................
41cdf0e10cSrcweir namespace svt
42cdf0e10cSrcweir {
43cdf0e10cSrcweir //........................................................................
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     using namespace ::utl;
46cdf0e10cSrcweir     using namespace ::com::sun::star::io;
47cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
49cdf0e10cSrcweir     using namespace ::com::sun::star::beans;
50cdf0e10cSrcweir     using namespace ::com::sun::star::graphic;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	//====================================================================
53cdf0e10cSrcweir 	//= StreamSupplier
54cdf0e10cSrcweir 	//====================================================================
55cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper2 <   XStream
56cdf0e10cSrcweir                                     ,   XSeekable
57cdf0e10cSrcweir                                     >   StreamSupplier_Base;
58cdf0e10cSrcweir     class StreamSupplier : public StreamSupplier_Base
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir     private:
61cdf0e10cSrcweir         Reference< XInputStream >   m_xInput;
62cdf0e10cSrcweir         Reference< XOutputStream >  m_xOutput;
63cdf0e10cSrcweir         Reference< XSeekable >      m_xSeekable;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     public:
66cdf0e10cSrcweir         StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     protected:
69cdf0e10cSrcweir         // XStream
70cdf0e10cSrcweir         virtual Reference< XInputStream > SAL_CALL getInputStream(  ) throw (RuntimeException);
71cdf0e10cSrcweir         virtual Reference< XOutputStream > SAL_CALL getOutputStream(  ) throw (RuntimeException);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         // XSeekable
74cdf0e10cSrcweir         virtual void SAL_CALL seek( ::sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
75cdf0e10cSrcweir         virtual ::sal_Int64 SAL_CALL getPosition(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
76cdf0e10cSrcweir         virtual ::sal_Int64 SAL_CALL getLength(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
77cdf0e10cSrcweir     };
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	//--------------------------------------------------------------------
StreamSupplier(const Reference<XInputStream> & _rxInput,const Reference<XOutputStream> & _rxOutput)80cdf0e10cSrcweir     StreamSupplier::StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput )
81cdf0e10cSrcweir         :m_xInput( _rxInput )
82cdf0e10cSrcweir         ,m_xOutput( _rxOutput )
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         m_xSeekable = m_xSeekable.query( m_xInput );
85cdf0e10cSrcweir         if ( !m_xSeekable.is() )
86cdf0e10cSrcweir             m_xSeekable = m_xSeekable.query( m_xOutput );
87cdf0e10cSrcweir         OSL_ENSURE( m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!" );
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     //--------------------------------------------------------------------
getInputStream()91cdf0e10cSrcweir     Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream(  ) throw (RuntimeException)
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         return m_xInput;
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     //--------------------------------------------------------------------
getOutputStream()97cdf0e10cSrcweir     Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream(  ) throw (RuntimeException)
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         return m_xOutput;
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     //--------------------------------------------------------------------
seek(::sal_Int64 location)103cdf0e10cSrcweir     void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         if ( !m_xSeekable.is() )
106cdf0e10cSrcweir             throw NotConnectedException();
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         m_xSeekable->seek( location );
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     //--------------------------------------------------------------------
getPosition()112cdf0e10cSrcweir     ::sal_Int64 SAL_CALL StreamSupplier::getPosition(  ) throw (IOException, RuntimeException)
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         if ( !m_xSeekable.is() )
115cdf0e10cSrcweir             throw NotConnectedException();
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         return m_xSeekable->getPosition();
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     //--------------------------------------------------------------------
getLength()121cdf0e10cSrcweir     ::sal_Int64 SAL_CALL StreamSupplier::getLength(  ) throw (IOException, RuntimeException)
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         if ( !m_xSeekable.is() )
124cdf0e10cSrcweir             throw NotConnectedException();
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         return m_xSeekable->getLength();
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     //====================================================================
130cdf0e10cSrcweir 	//= GraphicAccess
131cdf0e10cSrcweir 	//====================================================================
132cdf0e10cSrcweir 	//--------------------------------------------------------------------
isSupportedURL(const::rtl::OUString & _rURL)133cdf0e10cSrcweir     bool GraphicAccess::isSupportedURL( const ::rtl::OUString& _rURL )
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         if  (   ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:resource/" ) ) == 0 )
136cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:graphicrepository/" ) ) == 0 )
137cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:standardimage/" ) ) == 0 )
138cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.GraphicObject:" ) ) == 0 )
139cdf0e10cSrcweir             ||  ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.extension://" ) ) == 0 )
140cdf0e10cSrcweir             )
141cdf0e10cSrcweir             return true;
142cdf0e10cSrcweir         return false;
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	//--------------------------------------------------------------------
getImageStream(const Reference<XMultiServiceFactory> & _rxORB,const::rtl::OUString & _rImageResourceURL)146cdf0e10cSrcweir     SvStream* GraphicAccess::getImageStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         SvStream* pReturn = NULL;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         try
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             // get a GraphicProvider
153cdf0e10cSrcweir             Reference< XGraphicProvider > xProvider;
154cdf0e10cSrcweir             if ( _rxORB.is() )
155cdf0e10cSrcweir                 xProvider = xProvider.query( _rxORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ) ) );
156cdf0e10cSrcweir             OSL_ENSURE( xProvider.is(), "GraphicAccess::getImageStream: could not create a graphic provider!" );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             if ( !xProvider.is() )
159cdf0e10cSrcweir                 return pReturn;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             // let it create a graphic from the given URL
162cdf0e10cSrcweir             Sequence< PropertyValue > aMediaProperties( 1 );
163cdf0e10cSrcweir             aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
164cdf0e10cSrcweir             aMediaProperties[0].Value <<= _rImageResourceURL;
165cdf0e10cSrcweir             Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
166cdf0e10cSrcweir             OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" );
167cdf0e10cSrcweir             if ( !xGraphic.is() )
168cdf0e10cSrcweir                 return pReturn;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir             // copy the graphic to a in-memory buffer
171cdf0e10cSrcweir             SvMemoryStream* pMemBuffer = new SvMemoryStream;
172cdf0e10cSrcweir             Reference< XStream > xBufferAccess = new StreamSupplier(
173cdf0e10cSrcweir                 new OSeekableInputStreamWrapper( *pMemBuffer ),
174cdf0e10cSrcweir                 new OSeekableOutputStreamWrapper( *pMemBuffer )
175cdf0e10cSrcweir             );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             aMediaProperties.realloc( 2 );
178cdf0e10cSrcweir             aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OutputStream" ) );
179cdf0e10cSrcweir             aMediaProperties[0].Value <<= xBufferAccess;
180cdf0e10cSrcweir             aMediaProperties[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MimeType" ) );
181cdf0e10cSrcweir             aMediaProperties[1].Value <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
182cdf0e10cSrcweir             xProvider->storeGraphic( xGraphic, aMediaProperties );
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             pMemBuffer->Seek( 0 );
185cdf0e10cSrcweir             pReturn = pMemBuffer;
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir         catch( const Exception& )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir         	OSL_ENSURE( sal_False, "GraphicAccess::getImageStream: caught an exception!" );
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         return pReturn;
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	//--------------------------------------------------------------------
getImageXStream(const Reference<XMultiServiceFactory> & _rxORB,const::rtl::OUString & _rImageResourceURL)196cdf0e10cSrcweir     Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
197cdf0e10cSrcweir     {
198cdf0e10cSrcweir         return new OSeekableInputStreamWrapper( getImageStream( _rxORB, _rImageResourceURL ), sal_True );   // take ownership
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir //........................................................................
202cdf0e10cSrcweir } // namespace svt
203cdf0e10cSrcweir //........................................................................
204cdf0e10cSrcweir 
205