LibreOffice Module starmath (master) 1
unofilter.cxx
Go to the documentation of this file.
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
12#include <sot/storage.hxx>
14
15#include <document.hxx>
16#include "mathtype.hxx"
17#include <unomodel.hxx>
19
20using namespace ::com::sun::star;
21
22namespace
23{
25class MathTypeFilter
26 : public cppu::WeakImplHelper<document::XFilter, document::XImporter, lang::XServiceInfo>
27{
28 uno::Reference<lang::XComponent> m_xDstDoc;
29
30public:
31 MathTypeFilter();
32
33 // XFilter
34 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
35 void SAL_CALL cancel() override;
36
37 // XImporter
38 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
39
40 // XServiceInfo
41 OUString SAL_CALL getImplementationName() override;
42 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
43 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
44};
45}
46
47MathTypeFilter::MathTypeFilter() = default;
48
49sal_Bool MathTypeFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
50{
51 bool bSuccess = false;
52 try
53 {
54 utl::MediaDescriptor aMediaDesc(rDescriptor);
55 aMediaDesc.addInputStream();
56 uno::Reference<io::XInputStream> xInputStream;
57 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM] >>= xInputStream;
58 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream));
59 if (pStream)
60 {
61 if (SotStorage::IsStorageFile(pStream.get()))
62 {
63 tools::SvRef<SotStorage> aStorage(new SotStorage(pStream.get(), false));
64 // Is this a MathType Storage?
65 if (aStorage->IsStream("Equation Native"))
66 {
67 if (auto pModel = dynamic_cast<SmModel*>(m_xDstDoc.get()))
68 {
69 auto pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
70 OUStringBuffer aText(pDocShell->GetText());
71 MathType aEquation(aText);
72 bSuccess = aEquation.Parse(aStorage.get());
73 if (bSuccess)
74 {
75 pDocShell->SetText(aText.makeStringAndClear());
76 pDocShell->Parse();
77 }
78 }
79 }
80 }
81 }
82 }
83 catch (const uno::Exception&)
84 {
85 DBG_UNHANDLED_EXCEPTION("starmath");
86 }
87 return bSuccess;
88}
89
90void MathTypeFilter::cancel() {}
91
92void MathTypeFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
93{
94 m_xDstDoc = xDoc;
95}
96
97OUString MathTypeFilter::getImplementationName() { return "com.sun.star.comp.Math.MathTypeFilter"; }
98
99sal_Bool MathTypeFilter::supportsService(const OUString& rServiceName)
100{
101 return cppu::supportsService(this, rServiceName);
102}
103
104uno::Sequence<OUString> MathTypeFilter::getSupportedServiceNames()
105{
106 return { "com.sun.star.document.ImportFilter" };
107}
108
109extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
111 uno::Sequence<uno::Any> const& /*rSeq*/)
112{
113 return cppu::acquire(new MathTypeFilter);
114}
115
116/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool IsStorageFile(OUString const &rFileName)
static constexpr OUStringLiteral PROP_INPUTSTREAM
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
#define DBG_UNHANDLED_EXCEPTION(...)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Math_MathTypeFilter_get_implementation(uno::XComponentContext *, uno::Sequence< uno::Any > const &)
Definition: unofilter.cxx:110