Bump version to 5.0-14
[LibreOffice.git] / svtools / source / misc / imageresourceaccess.cxx
blob30d96fdd4b2e8d4b7b0258dad4483c2000eebbc5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <svtools/imageresourceaccess.hxx>
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>
36 namespace svt
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;
48 //= StreamSupplier
50 typedef ::cppu::WeakImplHelper2 < XStream
51 , XSeekable
52 > StreamSupplier_Base;
53 class StreamSupplier : public StreamSupplier_Base
55 private:
56 Reference< XInputStream > m_xInput;
57 Reference< XOutputStream > m_xOutput;
58 Reference< XSeekable > m_xSeekable;
60 public:
61 StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput );
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;
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;
75 StreamSupplier::StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput )
76 :m_xInput( _rxInput )
77 ,m_xOutput( _rxOutput )
79 m_xSeekable.set(m_xInput, css::uno::UNO_QUERY);
80 if ( !m_xSeekable.is() )
81 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!" );
86 Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream( ) throw (RuntimeException, std::exception)
88 return m_xInput;
92 Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream( ) throw (RuntimeException, std::exception)
94 return m_xOutput;
98 void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
100 if ( !m_xSeekable.is() )
101 throw NotConnectedException();
103 m_xSeekable->seek( location );
107 ::sal_Int64 SAL_CALL StreamSupplier::getPosition( ) throw (IOException, RuntimeException, std::exception)
109 if ( !m_xSeekable.is() )
110 throw NotConnectedException();
112 return m_xSeekable->getPosition();
116 ::sal_Int64 SAL_CALL StreamSupplier::getLength( ) throw (IOException, RuntimeException, std::exception)
118 if ( !m_xSeekable.is() )
119 throw NotConnectedException();
121 return m_xSeekable->getLength();
125 //= GraphicAccess
128 bool GraphicAccess::isSupportedURL( const OUString& _rURL )
130 if ( _rURL.startsWith( "private:resource/" )
131 || _rURL.startsWith( "private:graphicrepository/" )
132 || _rURL.startsWith( "private:standardimage/" )
133 || _rURL.startsWith( "vnd.sun.star.GraphicObject:" )
134 || _rURL.startsWith( "vnd.sun.star.extension://" )
136 return true;
137 return false;
141 SvStream* GraphicAccess::getImageStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL )
143 SvStream* pReturn = NULL;
147 // get a GraphicProvider
148 Reference< XGraphicProvider > xProvider = ::com::sun::star::graphic::GraphicProvider::create(_rxContext);
150 // let it create a graphic from the given URL
151 Sequence< PropertyValue > aMediaProperties( 1 );
152 aMediaProperties[0].Name = "URL";
153 aMediaProperties[0].Value <<= _rImageResourceURL;
154 Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
155 OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" );
156 if ( !xGraphic.is() )
157 return pReturn;
159 // copy the graphic to a in-memory buffer
160 SvMemoryStream* pMemBuffer = new SvMemoryStream;
161 Reference< XStream > xBufferAccess = new StreamSupplier(
162 new OSeekableInputStreamWrapper( *pMemBuffer ),
163 new OSeekableOutputStreamWrapper( *pMemBuffer )
166 aMediaProperties.realloc( 2 );
167 aMediaProperties[0].Name = "OutputStream";
168 aMediaProperties[0].Value <<= xBufferAccess;
169 aMediaProperties[1].Name = "MimeType";
170 aMediaProperties[1].Value <<= OUString( "image/png" );
171 xProvider->storeGraphic( xGraphic, aMediaProperties );
173 pMemBuffer->Seek( 0 );
174 pReturn = pMemBuffer;
176 catch( const Exception& )
178 OSL_FAIL( "GraphicAccess::getImageStream: caught an exception!" );
181 return pReturn;
185 Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL )
187 return new OSeekableInputStreamWrapper( getImageStream( _rxContext, _rImageResourceURL ), true ); // take ownership
191 } // namespace svt
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */