Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <svtools/imageresourceaccess.hxx>
22 :
23 : #include <com/sun/star/io/NotConnectedException.hpp>
24 : #include <com/sun/star/io/XSeekable.hpp>
25 : #include <com/sun/star/graphic/GraphicProvider.hpp>
26 : #include <com/sun/star/graphic/XGraphicProvider.hpp>
27 : #include <com/sun/star/io/XStream.hpp>
28 : #include <unotools/ucbstreamhelper.hxx>
29 : #include <tools/stream.hxx>
30 : #include <unotools/streamwrap.hxx>
31 : #include <cppuhelper/implbase2.hxx>
32 : #include <comphelper/processfactory.hxx>
33 :
34 : //........................................................................
35 : namespace svt
36 : {
37 : //........................................................................
38 :
39 : using namespace ::utl;
40 : using namespace ::com::sun::star::io;
41 : using namespace ::com::sun::star::uno;
42 : using namespace ::com::sun::star::lang;
43 : using namespace ::com::sun::star::beans;
44 : using namespace ::com::sun::star::graphic;
45 :
46 : //====================================================================
47 : //= StreamSupplier
48 : //====================================================================
49 : typedef ::cppu::WeakImplHelper2 < XStream
50 : , XSeekable
51 : > StreamSupplier_Base;
52 0 : class StreamSupplier : public StreamSupplier_Base
53 : {
54 : private:
55 : Reference< XInputStream > m_xInput;
56 : Reference< XOutputStream > m_xOutput;
57 : Reference< XSeekable > m_xSeekable;
58 :
59 : public:
60 : StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput );
61 :
62 : protected:
63 : // XStream
64 : virtual Reference< XInputStream > SAL_CALL getInputStream( ) throw (RuntimeException);
65 : virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException);
66 :
67 : // XSeekable
68 : virtual void SAL_CALL seek( ::sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
69 : virtual ::sal_Int64 SAL_CALL getPosition( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
70 : virtual ::sal_Int64 SAL_CALL getLength( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
71 : };
72 :
73 : //--------------------------------------------------------------------
74 0 : StreamSupplier::StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput )
75 : :m_xInput( _rxInput )
76 0 : ,m_xOutput( _rxOutput )
77 : {
78 0 : m_xSeekable = m_xSeekable.query( m_xInput );
79 0 : if ( !m_xSeekable.is() )
80 0 : m_xSeekable = m_xSeekable.query( m_xOutput );
81 : OSL_ENSURE( m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!" );
82 0 : }
83 :
84 : //--------------------------------------------------------------------
85 0 : Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream( ) throw (RuntimeException)
86 : {
87 0 : return m_xInput;
88 : }
89 :
90 : //--------------------------------------------------------------------
91 0 : Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream( ) throw (RuntimeException)
92 : {
93 0 : return m_xOutput;
94 : }
95 :
96 : //--------------------------------------------------------------------
97 0 : void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
98 : {
99 0 : if ( !m_xSeekable.is() )
100 0 : throw NotConnectedException();
101 :
102 0 : m_xSeekable->seek( location );
103 0 : }
104 :
105 : //--------------------------------------------------------------------
106 0 : ::sal_Int64 SAL_CALL StreamSupplier::getPosition( ) throw (IOException, RuntimeException)
107 : {
108 0 : if ( !m_xSeekable.is() )
109 0 : throw NotConnectedException();
110 :
111 0 : return m_xSeekable->getPosition();
112 : }
113 :
114 : //--------------------------------------------------------------------
115 0 : ::sal_Int64 SAL_CALL StreamSupplier::getLength( ) throw (IOException, RuntimeException)
116 : {
117 0 : if ( !m_xSeekable.is() )
118 0 : throw NotConnectedException();
119 :
120 0 : return m_xSeekable->getLength();
121 : }
122 :
123 : //====================================================================
124 : //= GraphicAccess
125 : //====================================================================
126 : //--------------------------------------------------------------------
127 0 : bool GraphicAccess::isSupportedURL( const ::rtl::OUString& _rURL )
128 : {
129 0 : if ( ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:resource/" ) ) == 0 )
130 0 : || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:graphicrepository/" ) ) == 0 )
131 0 : || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:standardimage/" ) ) == 0 )
132 0 : || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.GraphicObject:" ) ) == 0 )
133 0 : || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.extension://" ) ) == 0 )
134 : )
135 0 : return true;
136 0 : return false;
137 : }
138 :
139 : //--------------------------------------------------------------------
140 0 : SvStream* GraphicAccess::getImageStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
141 : {
142 0 : SvStream* pReturn = NULL;
143 :
144 : try
145 : {
146 : // get a GraphicProvider
147 0 : Reference< XGraphicProvider > xProvider = ::com::sun::star::graphic::GraphicProvider::create(comphelper::getComponentContext(_rxORB));
148 :
149 : // let it create a graphic from the given URL
150 0 : Sequence< PropertyValue > aMediaProperties( 1 );
151 0 : aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
152 0 : aMediaProperties[0].Value <<= _rImageResourceURL;
153 0 : Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
154 : OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" );
155 0 : if ( !xGraphic.is() )
156 0 : return pReturn;
157 :
158 : // copy the graphic to a in-memory buffer
159 0 : SvMemoryStream* pMemBuffer = new SvMemoryStream;
160 : Reference< XStream > xBufferAccess = new StreamSupplier(
161 0 : new OSeekableInputStreamWrapper( *pMemBuffer ),
162 0 : new OSeekableOutputStreamWrapper( *pMemBuffer )
163 0 : );
164 :
165 0 : aMediaProperties.realloc( 2 );
166 0 : aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OutputStream" ) );
167 0 : aMediaProperties[0].Value <<= xBufferAccess;
168 0 : aMediaProperties[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MimeType" ) );
169 0 : aMediaProperties[1].Value <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
170 0 : xProvider->storeGraphic( xGraphic, aMediaProperties );
171 :
172 0 : pMemBuffer->Seek( 0 );
173 0 : pReturn = pMemBuffer;
174 : }
175 0 : catch( const Exception& )
176 : {
177 : OSL_FAIL( "GraphicAccess::getImageStream: caught an exception!" );
178 : }
179 :
180 0 : return pReturn;
181 : }
182 :
183 : //--------------------------------------------------------------------
184 0 : Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rImageResourceURL )
185 : {
186 0 : return new OSeekableInputStreamWrapper( getImageStream( _rxORB, _rImageResourceURL ), sal_True ); // take ownership
187 : }
188 :
189 : //........................................................................
190 : } // namespace svt
191 : //........................................................................
192 :
193 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|