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 <osl/diagnose.h>
29 #include <tools/stream.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <unotools/streamwrap.hxx>
32 #include <cppuhelper/implbase.hxx>
34 namespace svt::GraphicAccess
37 using namespace ::utl
;
40 typedef ::cppu::WeakImplHelper
<io::XStream
, io::XSeekable
> StreamSupplier_Base
;
44 class StreamSupplier
: public StreamSupplier_Base
47 uno::Reference
<io::XInputStream
> m_xInput
;
48 uno::Reference
<io::XOutputStream
> m_xOutput
;
49 uno::Reference
<io::XSeekable
> m_xSeekable
;
52 StreamSupplier(uno::Reference
<io::XInputStream
> const & rxInput
, uno::Reference
<io::XOutputStream
> const & rxOutput
);
56 virtual uno::Reference
<io::XInputStream
> SAL_CALL
getInputStream() override
;
57 virtual uno::Reference
<io::XOutputStream
> SAL_CALL
getOutputStream() override
;
60 virtual void SAL_CALL
seek(sal_Int64 location
) override
;
61 virtual sal_Int64 SAL_CALL
getPosition() override
;
62 virtual sal_Int64 SAL_CALL
getLength() override
;
67 StreamSupplier::StreamSupplier(uno::Reference
<io::XInputStream
> const & rxInput
, uno::Reference
<io::XOutputStream
> const & rxOutput
)
71 m_xSeekable
.set(m_xInput
, uno::UNO_QUERY
);
72 if (!m_xSeekable
.is())
73 m_xSeekable
.set(m_xOutput
, uno::UNO_QUERY
);
74 OSL_ENSURE(m_xSeekable
.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!");
77 uno::Reference
<io::XInputStream
> SAL_CALL
StreamSupplier::getInputStream()
82 uno::Reference
<io::XOutputStream
> SAL_CALL
StreamSupplier::getOutputStream()
87 void SAL_CALL
StreamSupplier::seek(sal_Int64 nLocation
)
89 if (!m_xSeekable
.is())
90 throw io::NotConnectedException();
91 m_xSeekable
->seek(nLocation
);
94 sal_Int64 SAL_CALL
StreamSupplier::getPosition()
96 if (!m_xSeekable
.is())
97 throw io::NotConnectedException();
98 return m_xSeekable
->getPosition();
101 sal_Int64 SAL_CALL
StreamSupplier::getLength()
103 if (!m_xSeekable
.is())
104 throw io::NotConnectedException();
106 return m_xSeekable
->getLength();
109 bool isSupportedURL(OUString
const & rURL
)
111 return rURL
.startsWith("private:resource/")
112 || rURL
.startsWith("private:graphicrepository/")
113 || rURL
.startsWith("private:standardimage/")
114 || rURL
.startsWith("vnd.sun.star.extension://");
117 std::unique_ptr
<SvStream
> getImageStream(uno::Reference
<uno::XComponentContext
> const & rxContext
, OUString
const & rImageResourceURL
)
119 std::unique_ptr
<SvMemoryStream
> pMemBuffer
;
123 // get a GraphicProvider
124 uno::Reference
<graphic::XGraphicProvider
> xProvider
= css::graphic::GraphicProvider::create(rxContext
);
126 // let it create a graphic from the given URL
127 uno::Sequence
<beans::PropertyValue
> aMediaProperties(1);
128 aMediaProperties
[0].Name
= "URL";
129 aMediaProperties
[0].Value
<<= rImageResourceURL
;
130 uno::Reference
<graphic::XGraphic
> xGraphic(xProvider
->queryGraphic(aMediaProperties
));
132 OSL_ENSURE(xGraphic
.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!");
136 // copy the graphic to an in-memory buffer
137 pMemBuffer
.reset(new SvMemoryStream
);
138 uno::Reference
<io::XStream
> xBufferAccess
= new StreamSupplier(
139 new OSeekableInputStreamWrapper(*pMemBuffer
),
140 new OSeekableOutputStreamWrapper(*pMemBuffer
));
142 aMediaProperties
.realloc(2);
143 aMediaProperties
[0].Name
= "OutputStream";
144 aMediaProperties
[0].Value
<<= xBufferAccess
;
145 aMediaProperties
[1].Name
= "MimeType";
146 aMediaProperties
[1].Value
<<= OUString("image/png");
147 xProvider
->storeGraphic(xGraphic
, aMediaProperties
);
151 catch (const uno::Exception
&)
153 TOOLS_WARN_EXCEPTION("svtools", "GraphicAccess::getImageStream");
160 uno::Reference
<io::XInputStream
> getImageXStream(uno::Reference
<uno::XComponentContext
> const & rxContext
, OUString
const & rImageResourceURL
)
162 return new OSeekableInputStreamWrapper(getImageStream(rxContext
, rImageResourceURL
).release(), true); // take ownership
165 } // namespace svt::GraphicAccess
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */