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/.
12 #include <com/sun/star/document/XFilter.hpp>
13 #include <com/sun/star/document/XImporter.hpp>
14 #include <com/sun/star/uno/XComponentContext.hpp>
15 #include <cppuhelper/implbase.hxx>
16 #include <sot/storage.hxx>
17 #include <tools/stream.hxx>
18 #include <unotools/mediadescriptor.hxx>
19 #include <unotools/ucbstreamhelper.hxx>
21 #include <document.hxx>
22 #include <mathtype.hxx>
23 #include <unomodel.hxx>
25 using namespace ::com::sun::star
;
27 /// Invokes the MathType importer via UNO.
28 class MathTypeFilter
: public cppu::WeakImplHelper
35 uno::Reference
<uno::XComponentContext
> m_xContext
;
36 uno::Reference
<lang::XComponent
> m_xDstDoc
;
39 MathTypeFilter(const uno::Reference
<uno::XComponentContext
>& xContext
);
40 virtual ~MathTypeFilter();
43 virtual sal_Bool SAL_CALL
filter(const uno::Sequence
<beans::PropertyValue
>& rDescriptor
) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
44 virtual void SAL_CALL
cancel() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
47 virtual void SAL_CALL
setTargetDocument(const uno::Reference
<lang::XComponent
>& xDoc
) throw (lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
50 virtual OUString SAL_CALL
getImplementationName() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
51 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
52 virtual uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
55 MathTypeFilter::MathTypeFilter(const uno::Reference
< uno::XComponentContext
>& rxContext
)
56 : m_xContext(rxContext
)
60 MathTypeFilter::~MathTypeFilter()
64 sal_Bool
MathTypeFilter::filter(const uno::Sequence
<beans::PropertyValue
>& rDescriptor
) throw(uno::RuntimeException
, std::exception
)
66 bool bSuccess
= false;
69 utl::MediaDescriptor
aMediaDesc(rDescriptor
);
70 aMediaDesc
.addInputStream();
71 uno::Reference
<io::XInputStream
> xInputStream
;
72 aMediaDesc
[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream
;
73 std::unique_ptr
<SvStream
> pStream(utl::UcbStreamHelper::CreateStream(xInputStream
));
76 if (SotStorage::IsStorageFile(pStream
.get()))
78 tools::SvRef
<SotStorage
> aStorage(new SotStorage(pStream
.get(), false));
79 // Is this a MathType Storage?
80 if (aStorage
->IsStream(OUString("Equation Native")))
82 if (SmModel
* pModel
= dynamic_cast<SmModel
*>(m_xDstDoc
.get()))
84 SmDocShell
* pDocShell
= static_cast<SmDocShell
*>(pModel
->GetObjectShell());
85 OUString aText
= pDocShell
->GetText();
86 MathType
aEquation(aText
);
87 bSuccess
= aEquation
.Parse(aStorage
) == 1;
90 pDocShell
->SetText(aText
);
98 catch (const uno::Exception
& rException
)
100 SAL_WARN("starmath", "Exception caught: " << rException
.Message
);
105 void MathTypeFilter::cancel() throw(uno::RuntimeException
, std::exception
)
109 void MathTypeFilter::setTargetDocument(const uno::Reference
< lang::XComponent
>& xDoc
) throw(lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
114 OUString
MathTypeFilter::getImplementationName() throw(uno::RuntimeException
, std::exception
)
116 return OUString("com.sun.star.comp.Math.MathTypeFilter");
119 sal_Bool
MathTypeFilter::supportsService(const OUString
& rServiceName
) throw(uno::RuntimeException
, std::exception
)
121 return cppu::supportsService(this, rServiceName
);
124 uno::Sequence
<OUString
> MathTypeFilter::getSupportedServiceNames() throw(uno::RuntimeException
, std::exception
)
126 uno::Sequence
<OUString
> aRet
=
128 OUString("com.sun.star.document.ImportFilter")
133 extern "C" SAL_DLLPUBLIC_EXPORT
uno::XInterface
* SAL_CALL
com_sun_star_comp_Math_MathTypeFilter_get_implementation(uno::XComponentContext
* pComponent
, uno::Sequence
<uno::Any
> const&)
135 return cppu::acquire(new MathTypeFilter(pComponent
));
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */