Bump version to 21.06.18.1
[LibreOffice.git] / starmath / source / unofilter.cxx
blob719681af4b4bcca0eca56b75f2988c159e6b88ea
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/.
8 */
10 #include <memory>
12 #include <unotools/mediadescriptor.hxx>
13 #include <unotools/ucbstreamhelper.hxx>
14 #include <sot/storage.hxx>
15 #include <cppuhelper/supportsservice.hxx>
17 #include <document.hxx>
18 #include "mathtype.hxx"
19 #include <unomodel.hxx>
20 #include <tools/diagnose_ex.h>
22 using namespace ::com::sun::star;
24 namespace
26 /// Invokes the MathType importer via UNO.
27 class MathTypeFilter
28 : public cppu::WeakImplHelper<document::XFilter, document::XImporter, lang::XServiceInfo>
30 uno::Reference<lang::XComponent> m_xDstDoc;
32 public:
33 MathTypeFilter();
35 // XFilter
36 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
37 void SAL_CALL cancel() override;
39 // XImporter
40 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
42 // XServiceInfo
43 OUString SAL_CALL getImplementationName() override;
44 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
45 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
49 MathTypeFilter::MathTypeFilter() = default;
51 sal_Bool MathTypeFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
53 bool bSuccess = false;
54 try
56 utl::MediaDescriptor aMediaDesc(rDescriptor);
57 aMediaDesc.addInputStream();
58 uno::Reference<io::XInputStream> xInputStream;
59 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
60 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream));
61 if (pStream)
63 if (SotStorage::IsStorageFile(pStream.get()))
65 tools::SvRef<SotStorage> aStorage(new SotStorage(pStream.get(), false));
66 // Is this a MathType Storage?
67 if (aStorage->IsStream("Equation Native"))
69 if (auto pModel = dynamic_cast<SmModel*>(m_xDstDoc.get()))
71 auto pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
72 OUStringBuffer aText(pDocShell->GetText());
73 MathType aEquation(aText);
74 bSuccess = aEquation.Parse(aStorage.get());
75 if (bSuccess)
77 pDocShell->SetText(aText.makeStringAndClear());
78 pDocShell->Parse();
85 catch (const uno::Exception&)
87 DBG_UNHANDLED_EXCEPTION("starmath");
89 return bSuccess;
92 void MathTypeFilter::cancel() {}
94 void MathTypeFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
96 m_xDstDoc = xDoc;
99 OUString MathTypeFilter::getImplementationName() { return "com.sun.star.comp.Math.MathTypeFilter"; }
101 sal_Bool MathTypeFilter::supportsService(const OUString& rServiceName)
103 return cppu::supportsService(this, rServiceName);
106 uno::Sequence<OUString> MathTypeFilter::getSupportedServiceNames()
108 uno::Sequence<OUString> aRet = { OUString("com.sun.star.document.ImportFilter") };
109 return aRet;
112 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
113 com_sun_star_comp_Math_MathTypeFilter_get_implementation(uno::XComponentContext* /*pCtx*/,
114 uno::Sequence<uno::Any> const& /*rSeq*/)
116 return cppu::acquire(new MathTypeFilter);
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */