Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <memory>
11 :
12 : #include <unotools/mediadescriptor.hxx>
13 : #include <unotools/ucbstreamhelper.hxx>
14 :
15 : #include <document.hxx>
16 : #include <mathtype.hxx>
17 : #include <unomodel.hxx>
18 :
19 : using namespace ::com::sun::star;
20 :
21 : /// Invokes the MathType importer via UNO.
22 : class MathTypeFilter : public cppu::WeakImplHelper
23 : <
24 : document::XFilter,
25 : document::XImporter,
26 : lang::XServiceInfo
27 : >
28 : {
29 : uno::Reference<uno::XComponentContext> m_xContext;
30 : uno::Reference<lang::XComponent> m_xDstDoc;
31 :
32 : public:
33 : explicit MathTypeFilter(const uno::Reference<uno::XComponentContext>& xContext);
34 : virtual ~MathTypeFilter();
35 :
36 : // XFilter
37 : virtual sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
38 : virtual void SAL_CALL cancel() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
39 :
40 : // XImporter
41 : virtual void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
42 :
43 : // XServiceInfo
44 : virtual OUString SAL_CALL getImplementationName() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
45 : virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
46 : virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
47 : };
48 :
49 3 : MathTypeFilter::MathTypeFilter(const uno::Reference< uno::XComponentContext >& rxContext)
50 3 : : m_xContext(rxContext)
51 : {
52 3 : }
53 :
54 6 : MathTypeFilter::~MathTypeFilter()
55 : {
56 6 : }
57 :
58 2 : sal_Bool MathTypeFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) throw(uno::RuntimeException, std::exception)
59 : {
60 2 : bool bSuccess = false;
61 : try
62 : {
63 2 : utl::MediaDescriptor aMediaDesc(rDescriptor);
64 2 : aMediaDesc.addInputStream();
65 4 : uno::Reference<io::XInputStream> xInputStream;
66 2 : aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
67 4 : std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream));
68 2 : if (pStream)
69 : {
70 2 : if (SotStorage::IsStorageFile(pStream.get()))
71 : {
72 2 : tools::SvRef<SotStorage> aStorage(new SotStorage(pStream.get(), false));
73 : // Is this a MathType Storage?
74 2 : if (aStorage->IsStream(OUString("Equation Native")))
75 : {
76 2 : if (SmModel* pModel = dynamic_cast<SmModel*>(m_xDstDoc.get()))
77 : {
78 2 : SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
79 2 : OUString aText = pDocShell->GetText();
80 4 : MathType aEquation(aText);
81 2 : bSuccess = aEquation.Parse(aStorage) == 1;
82 2 : if (bSuccess)
83 : {
84 2 : pDocShell->SetText(aText);
85 2 : pDocShell->Parse();
86 2 : }
87 : }
88 2 : }
89 : }
90 2 : }
91 : }
92 0 : catch (const uno::Exception& rException)
93 : {
94 : SAL_WARN("starmath", "Exception caught: " << rException.Message);
95 : }
96 2 : return bSuccess;
97 : }
98 :
99 0 : void MathTypeFilter::cancel() throw(uno::RuntimeException, std::exception)
100 : {
101 0 : }
102 :
103 2 : void MathTypeFilter::setTargetDocument(const uno::Reference< lang::XComponent >& xDoc) throw(lang::IllegalArgumentException, uno::RuntimeException, std::exception)
104 : {
105 2 : m_xDstDoc = xDoc;
106 2 : }
107 :
108 1 : OUString MathTypeFilter::getImplementationName() throw(uno::RuntimeException, std::exception)
109 : {
110 1 : return OUString("com.sun.star.comp.Math.MathTypeFilter");
111 : }
112 :
113 0 : sal_Bool MathTypeFilter::supportsService(const OUString& rServiceName) throw(uno::RuntimeException, std::exception)
114 : {
115 0 : return cppu::supportsService(this, rServiceName);
116 : }
117 :
118 1 : uno::Sequence<OUString> MathTypeFilter::getSupportedServiceNames() throw(uno::RuntimeException, std::exception)
119 : {
120 : uno::Sequence<OUString> aRet =
121 : {
122 : OUString("com.sun.star.document.ImportFilter")
123 1 : };
124 1 : return aRet;
125 : }
126 :
127 3 : 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&)
128 : {
129 3 : return cppu::acquire(new MathTypeFilter(pComponent));
130 42 : }
131 :
132 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|