cURL: follow redirects
[LibreOffice.git] / starmath / source / unofilter.cxx
blob95fc3e8e09680e6426b7f2aa350b4fd82669b5c7
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>
19 using namespace ::com::sun::star;
21 /// Invokes the MathType importer via UNO.
22 class MathTypeFilter : public cppu::WeakImplHelper
24 document::XFilter,
25 document::XImporter,
26 lang::XServiceInfo
29 uno::Reference<lang::XComponent> m_xDstDoc;
31 public:
32 MathTypeFilter();
33 ~MathTypeFilter() override;
35 // XFilter
36 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) throw (uno::RuntimeException, std::exception) override;
37 void SAL_CALL cancel() throw (uno::RuntimeException, std::exception) override;
39 // XImporter
40 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) override;
42 // XServiceInfo
43 OUString SAL_CALL getImplementationName() throw (uno::RuntimeException, std::exception) override;
44 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (uno::RuntimeException, std::exception) override;
45 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() throw (uno::RuntimeException, std::exception) override;
48 MathTypeFilter::MathTypeFilter() = default;
50 MathTypeFilter::~MathTypeFilter() = default;
52 sal_Bool MathTypeFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) throw(uno::RuntimeException, std::exception)
54 bool bSuccess = false;
55 try
57 utl::MediaDescriptor aMediaDesc(rDescriptor);
58 aMediaDesc.addInputStream();
59 uno::Reference<io::XInputStream> xInputStream;
60 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
61 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream));
62 if (pStream)
64 if (SotStorage::IsStorageFile(pStream.get()))
66 tools::SvRef<SotStorage> aStorage(new SotStorage(pStream.get(), false));
67 // Is this a MathType Storage?
68 if (aStorage->IsStream("Equation Native"))
70 if (SmModel* pModel = dynamic_cast<SmModel*>(m_xDstDoc.get()))
72 SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
73 OUString aText = pDocShell->GetText();
74 MathType aEquation(aText);
75 bSuccess = aEquation.Parse(aStorage.get());
76 if (bSuccess)
78 pDocShell->SetText(aText);
79 pDocShell->Parse();
86 catch (const uno::Exception& rException)
88 SAL_WARN("starmath", "Exception caught: " << rException.Message);
90 return bSuccess;
93 void MathTypeFilter::cancel() throw(uno::RuntimeException, std::exception)
97 void MathTypeFilter::setTargetDocument(const uno::Reference< lang::XComponent >& xDoc) throw(lang::IllegalArgumentException, uno::RuntimeException, std::exception)
99 m_xDstDoc = xDoc;
102 OUString MathTypeFilter::getImplementationName() throw(uno::RuntimeException, std::exception)
104 return OUString("com.sun.star.comp.Math.MathTypeFilter");
107 sal_Bool MathTypeFilter::supportsService(const OUString& rServiceName) throw(uno::RuntimeException, std::exception)
109 return cppu::supportsService(this, rServiceName);
112 uno::Sequence<OUString> MathTypeFilter::getSupportedServiceNames() throw(uno::RuntimeException, std::exception)
114 uno::Sequence<OUString> aRet =
116 OUString("com.sun.star.document.ImportFilter")
118 return aRet;
121 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* SAL_CALL com_sun_star_comp_Math_MathTypeFilter_get_implementation(uno::XComponentContext*, uno::Sequence<uno::Any> const&)
123 return cppu::acquire(new MathTypeFilter);
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */