1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 //........................................................................
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 //====================================================================
48 //====================================================================
49 typedef ::cppu::WeakImplHelper2
< XStream
51 > StreamSupplier_Base
;
52 class StreamSupplier
: public StreamSupplier_Base
55 Reference
< XInputStream
> m_xInput
;
56 Reference
< XOutputStream
> m_xOutput
;
57 Reference
< XSeekable
> m_xSeekable
;
60 StreamSupplier( const Reference
< XInputStream
>& _rxInput
, const Reference
< XOutputStream
>& _rxOutput
);
64 virtual Reference
< XInputStream
> SAL_CALL
getInputStream( ) throw (RuntimeException
);
65 virtual Reference
< XOutputStream
> SAL_CALL
getOutputStream( ) throw (RuntimeException
);
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
)
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
)
90 //--------------------------------------------------------------------
91 Reference
< XOutputStream
> SAL_CALL
StreamSupplier::getOutputStream( ) throw (RuntimeException
)
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 //====================================================================
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 )
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() )
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!" );
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 //........................................................................
191 //........................................................................
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */