fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svtools / source / misc / imageresourceaccess.cxx
blob40df75d7e98f67a52b0ca44f577729e471549bc9
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 <unotools/ucbstreamhelper.hxx>
29 #include <tools/stream.hxx>
30 #include <unotools/streamwrap.hxx>
31 #include <cppuhelper/implbase2.hxx>
32 #include <comphelper/processfactory.hxx>
34 //........................................................................
35 namespace svt
37 //........................................................................
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;
46 //====================================================================
47 //= StreamSupplier
48 //====================================================================
49 typedef ::cppu::WeakImplHelper2 < XStream
50 , XSeekable
51 > StreamSupplier_Base;
52 class StreamSupplier : public StreamSupplier_Base
54 private:
55 Reference< XInputStream > m_xInput;
56 Reference< XOutputStream > m_xOutput;
57 Reference< XSeekable > m_xSeekable;
59 public:
60 StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput );
62 protected:
63 // XStream
64 virtual Reference< XInputStream > SAL_CALL getInputStream( ) throw (RuntimeException);
65 virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException);
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);
73 //--------------------------------------------------------------------
74 StreamSupplier::StreamSupplier( const Reference< XInputStream >& _rxInput, const Reference< XOutputStream >& _rxOutput )
75 :m_xInput( _rxInput )
76 ,m_xOutput( _rxOutput )
78 m_xSeekable = m_xSeekable.query( m_xInput );
79 if ( !m_xSeekable.is() )
80 m_xSeekable = m_xSeekable.query( m_xOutput );
81 OSL_ENSURE( m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!" );
84 //--------------------------------------------------------------------
85 Reference< XInputStream > SAL_CALL StreamSupplier::getInputStream( ) throw (RuntimeException)
87 return m_xInput;
90 //--------------------------------------------------------------------
91 Reference< XOutputStream > SAL_CALL StreamSupplier::getOutputStream( ) throw (RuntimeException)
93 return m_xOutput;
96 //--------------------------------------------------------------------
97 void SAL_CALL StreamSupplier::seek( ::sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
99 if ( !m_xSeekable.is() )
100 throw NotConnectedException();
102 m_xSeekable->seek( location );
105 //--------------------------------------------------------------------
106 ::sal_Int64 SAL_CALL StreamSupplier::getPosition( ) throw (IOException, RuntimeException)
108 if ( !m_xSeekable.is() )
109 throw NotConnectedException();
111 return m_xSeekable->getPosition();
114 //--------------------------------------------------------------------
115 ::sal_Int64 SAL_CALL StreamSupplier::getLength( ) throw (IOException, RuntimeException)
117 if ( !m_xSeekable.is() )
118 throw NotConnectedException();
120 return m_xSeekable->getLength();
123 //====================================================================
124 //= GraphicAccess
125 //====================================================================
126 //--------------------------------------------------------------------
127 bool GraphicAccess::isSupportedURL( const OUString& _rURL )
129 if ( ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:resource/" ) ) == 0 )
130 || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:graphicrepository/" ) ) == 0 )
131 || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:standardimage/" ) ) == 0 )
132 || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.GraphicObject:" ) ) == 0 )
133 || ( _rURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.extension://" ) ) == 0 )
135 return true;
136 return false;
139 //--------------------------------------------------------------------
140 SvStream* GraphicAccess::getImageStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL )
142 SvStream* pReturn = NULL;
146 // get a GraphicProvider
147 Reference< XGraphicProvider > xProvider = ::com::sun::star::graphic::GraphicProvider::create(_rxContext);
149 // let it create a graphic from the given URL
150 Sequence< PropertyValue > aMediaProperties( 1 );
151 aMediaProperties[0].Name = OUString( "URL" );
152 aMediaProperties[0].Value <<= _rImageResourceURL;
153 Reference< XGraphic > xGraphic( xProvider->queryGraphic( aMediaProperties ) );
154 OSL_ENSURE( xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!" );
155 if ( !xGraphic.is() )
156 return pReturn;
158 // copy the graphic to a in-memory buffer
159 SvMemoryStream* pMemBuffer = new SvMemoryStream;
160 Reference< XStream > xBufferAccess = new StreamSupplier(
161 new OSeekableInputStreamWrapper( *pMemBuffer ),
162 new OSeekableOutputStreamWrapper( *pMemBuffer )
165 aMediaProperties.realloc( 2 );
166 aMediaProperties[0].Name = OUString( "OutputStream" );
167 aMediaProperties[0].Value <<= xBufferAccess;
168 aMediaProperties[1].Name = OUString( "MimeType" );
169 aMediaProperties[1].Value <<= OUString( "image/png" );
170 xProvider->storeGraphic( xGraphic, aMediaProperties );
172 pMemBuffer->Seek( 0 );
173 pReturn = pMemBuffer;
175 catch( const Exception& )
177 OSL_FAIL( "GraphicAccess::getImageStream: caught an exception!" );
180 return pReturn;
183 //--------------------------------------------------------------------
184 Reference< XInputStream > GraphicAccess::getImageXStream( const Reference< XComponentContext >& _rxContext, const OUString& _rImageResourceURL )
186 return new OSeekableInputStreamWrapper( getImageStream( _rxContext, _rImageResourceURL ), sal_True ); // take ownership
189 //........................................................................
190 } // namespace svt
191 //........................................................................
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */