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 <osl/diagnose.h>
29 : #include <unotools/ucbstreamhelper.hxx>
30 : #include <tools/stream.hxx>
31 : #include <unotools/streamwrap.hxx>
32 : #include <cppuhelper/implbase2.hxx>
33 : #include <comphelper/processfactory.hxx>
34 :
35 :
36 : namespace svt
37 : {
38 :
39 :
40 : using namespace ::utl;
41 : using namespace ::com::sun::star::io;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::com::sun::star::lang;
44 : using namespace ::com::sun::star::beans;
45 : using namespace ::com::sun::star::graphic;
46 :
47 :
48 : //= StreamSupplier
49 :
50 : typedef ::cppu::WeakImplHelper2 < XStream
51 : , XSeekable
52 : > StreamSupplier_Base;
53 0 : class StreamSupplier : public StreamSupplier_Base
54 : {
55 : private:
56 : Reference< XInputStream > m_xInput;
57 : Reference< XOutputStream > m_xOutput;
58 : Reference< XSeekable > m_xSeekable;
59 :
60 : public:
61 : StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput );
62 :
63 : protected:
64 : // XStream
65 : virtual Reference< XInputStream > SAL_CALL getInputStream( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
66 : virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
67 :
68 : // XSeekable
69 : virtual void SAL_CALL seek( ::sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
70 : virtual ::sal_Int64 SAL_CALL getPosition( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 : virtual ::sal_Int64 SAL_CALL getLength( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
72 : };
73 :
74 :
75 0 : StreamSupplier::StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput )
76 : :m_xInput( _rxInput )
77 0 : ,m_xOutput( _rxOutput )
78 : {
79 0 : m_xSeekable.set(m_xInput, css::uno::UNO_QUERY);
80 0 : if ( !m_xSeekable.is() )
81 0 : m_xSeekable.set(m_xOutput, css::uno::UNO_QUERY);
82 : OSL_ENSURE( m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!" );
83 0 : }
84 :
85 :
86 0 : Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream( ) throw (RuntimeException, std::exception)
87 : {
88 0 : return m_xInput;
89 : }
90 :
91 :
92 0 : Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream( ) throw (RuntimeException, std::exception)
93 : {
94 0 : return m_xOutput;
95 : }
96 :
97 :
98 0 : void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
99 : {
100 0 : if ( !m_xSeekable.is() )
101 0 : throw NotConnectedException();
102 :
103 0 : m_xSeekable->seek( location );
104 0 : }
105 :
106 :
107 0 : ::sal_Int64 SAL_CALL StreamSupplier::getPosition( ) throw (IOException, RuntimeException, std::exception)
108 : {
109 0 : if ( !m_xSeekable.is() )
110 0 : throw NotConnectedException();
111 :
112 0 : return m_xSeekable->getPosition();
113 : }
114 :
115 :
116 0 : ::sal_Int64 SAL_CALL StreamSupplier::getLength( ) throw (IOException, RuntimeException, std::exception)
117 : {
118 0 : if ( !m_xSeekable.is() )
119 0 : throw NotConnectedException();
120 :
121 0 : return m_xSeekable->getLength();
122 : }
123 :
124 :
125 : //= GraphicAccess
126 :
127 :
128 7 : bool GraphicAccess::isSupportedURL( const OUString& _rURL )
129 : {
130 14 : if ( _rURL.startsWith( "private:resource/" )
131 7 : || _rURL.startsWith( "private:graphicrepository/" )
132 7 : || _rURL.startsWith( "private:standardimage/" )
133 7 : || _rURL.startsWith( "vnd.sun.star.GraphicObject:" )
134 14 : || _rURL.startsWith( "vnd.sun.star.extension://" )
135 : )
136 0 : return true;
137 7 : return false;
138 : }
139 :
140 :
141 0 : SvStream* GraphicAccess::getImageStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL )
142 : {
143 0 : SvStream* pReturn = NULL;
144 :
145 : try
146 : {
147 : // get a GraphicProvider
148 0 : Reference< XGraphicProvider > xProvider = ::com::sun::star::graphic::GraphicProvider::create(_rxContext);
149 :
150 : // let it create a graphic from the given URL
151 0 : Sequence< PropertyValue > aMediaProperties( 1 );
152 0 : aMediaProperties[0].Name = "URL";
153 0 : aMediaProperties[0].Value <<= _rImageResourceURL;
154 0 : Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
155 : OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" );
156 0 : if ( !xGraphic.is() )
157 0 : return pReturn;
158 :
159 : // copy the graphic to a in-memory buffer
160 0 : SvMemoryStream* pMemBuffer = new SvMemoryStream;
161 : Reference< XStream > xBufferAccess = new StreamSupplier(
162 0 : new OSeekableInputStreamWrapper( *pMemBuffer ),
163 0 : new OSeekableOutputStreamWrapper( *pMemBuffer )
164 0 : );
165 :
166 0 : aMediaProperties.realloc( 2 );
167 0 : aMediaProperties[0].Name = "OutputStream";
168 0 : aMediaProperties[0].Value <<= xBufferAccess;
169 0 : aMediaProperties[1].Name = "MimeType";
170 0 : aMediaProperties[1].Value <<= OUString( "image/png" );
171 0 : xProvider->storeGraphic( xGraphic, aMediaProperties );
172 :
173 0 : pMemBuffer->Seek( 0 );
174 0 : pReturn = pMemBuffer;
175 : }
176 0 : catch( const Exception& )
177 : {
178 : OSL_FAIL( "GraphicAccess::getImageStream: caught an exception!" );
179 : }
180 :
181 0 : return pReturn;
182 : }
183 :
184 :
185 0 : Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL )
186 : {
187 0 : return new OSeekableInputStreamWrapper( getImageStream( _rxContext, _rImageResourceURL ), true ); // take ownership
188 : }
189 :
190 :
191 : } // namespace svt
192 :
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|