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/.
10 #include <unotools/mediadescriptor.hxx>
11 #include <unotools/ucbstreamhelper.hxx>
12 #include <sot/storage.hxx>
13 #include <cppuhelper/supportsservice.hxx>
15 #include <document.hxx>
16 #include "mathtype.hxx"
17 #include <unomodel.hxx>
18 #include <comphelper/diagnose_ex.hxx>
20 using namespace ::com::sun::star
;
24 /// Invokes the MathType importer via UNO.
26 : public cppu::WeakImplHelper
<document::XFilter
, document::XImporter
, lang::XServiceInfo
>
28 uno::Reference
<lang::XComponent
> m_xDstDoc
;
34 sal_Bool SAL_CALL
filter(const uno::Sequence
<beans::PropertyValue
>& rDescriptor
) override
;
35 void SAL_CALL
cancel() override
;
38 void SAL_CALL
setTargetDocument(const uno::Reference
<lang::XComponent
>& xDoc
) override
;
41 OUString SAL_CALL
getImplementationName() override
;
42 sal_Bool SAL_CALL
supportsService(const OUString
& rServiceName
) override
;
43 uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
;
47 MathTypeFilter::MathTypeFilter() = default;
49 sal_Bool
MathTypeFilter::filter(const uno::Sequence
<beans::PropertyValue
>& rDescriptor
)
51 bool bSuccess
= false;
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
));
61 if (SotStorage::IsStorageFile(pStream
.get()))
63 rtl::Reference
<SotStorage
> aStorage(new SotStorage(pStream
.get(), false));
64 // Is this a MathType Storage?
65 if (aStorage
->IsStream(u
"Equation Native"_ustr
))
67 if (auto pModel
= dynamic_cast<SmModel
*>(m_xDstDoc
.get()))
69 auto pDocShell
= static_cast<SmDocShell
*>(pModel
->GetObjectShell());
70 OUStringBuffer
aText(pDocShell
->GetText());
71 MathType
aEquation(aText
);
72 bSuccess
= aEquation
.Parse(aStorage
.get());
75 pDocShell
->SetText(aText
.makeStringAndClear());
83 catch (const uno::Exception
&)
85 DBG_UNHANDLED_EXCEPTION("starmath");
90 void MathTypeFilter::cancel() {}
92 void MathTypeFilter::setTargetDocument(const uno::Reference
<lang::XComponent
>& xDoc
)
97 OUString
MathTypeFilter::getImplementationName()
99 return u
"com.sun.star.comp.Math.MathTypeFilter"_ustr
;
102 sal_Bool
MathTypeFilter::supportsService(const OUString
& rServiceName
)
104 return cppu::supportsService(this, rServiceName
);
107 uno::Sequence
<OUString
> MathTypeFilter::getSupportedServiceNames()
109 return { u
"com.sun.star.document.ImportFilter"_ustr
};
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: */