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