Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / starmath / source / unofilter.cxx
blob11b0728a53f7e0325a68a20fab94e3a51e6e74dd
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>
15 #include <document.hxx>
16 #include "mathtype.hxx"
17 #include <unomodel.hxx>
18 #include <tools/diagnose_ex.h>
20 using namespace ::com::sun::star;
22 /// Invokes the MathType importer via UNO.
23 class MathTypeFilter : public cppu::WeakImplHelper
25 document::XFilter,
26 document::XImporter,
27 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;
48 MathTypeFilter::MathTypeFilter() = default;
50 sal_Bool MathTypeFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
52 bool bSuccess = false;
53 try
55 utl::MediaDescriptor aMediaDesc(rDescriptor);
56 aMediaDesc.addInputStream();
57 uno::Reference<io::XInputStream> xInputStream;
58 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
59 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream));
60 if (pStream)
62 if (SotStorage::IsStorageFile(pStream.get()))
64 tools::SvRef<SotStorage> aStorage(new SotStorage(pStream.get(), false));
65 // Is this a MathType Storage?
66 if (aStorage->IsStream("Equation Native"))
68 if (auto pModel = dynamic_cast<SmModel*>(m_xDstDoc.get()))
70 auto pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
71 OUStringBuffer aText(pDocShell->GetText());
72 MathType aEquation(aText);
73 bSuccess = aEquation.Parse(aStorage.get());
74 if (bSuccess)
76 pDocShell->SetText(aText.makeStringAndClear());
77 pDocShell->Parse();
84 catch (const uno::Exception&)
86 DBG_UNHANDLED_EXCEPTION("starmath");
88 return bSuccess;
91 void MathTypeFilter::cancel()
95 void MathTypeFilter::setTargetDocument(const uno::Reference< lang::XComponent >& xDoc)
97 m_xDstDoc = xDoc;
100 OUString MathTypeFilter::getImplementationName()
102 return OUString("com.sun.star.comp.Math.MathTypeFilter");
105 sal_Bool MathTypeFilter::supportsService(const OUString& rServiceName)
107 return cppu::supportsService(this, rServiceName);
110 uno::Sequence<OUString> MathTypeFilter::getSupportedServiceNames()
112 uno::Sequence<OUString> aRet =
114 OUString("com.sun.star.document.ImportFilter")
116 return aRet;
119 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* com_sun_star_comp_Math_MathTypeFilter_get_implementation(uno::XComponentContext* /*pCtx*/, uno::Sequence<uno::Any> const& /*rSeq*/)
121 return cppu::acquire(new MathTypeFilter);
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */