android: Update app-specific/MIME type icons
[LibreOffice.git] / writerfilter / source / filter / RtfFilter.cxx
blob8f80b85353b210d6d3f1f8f3fabfd475dff023b5
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <memory>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/document/XExporter.hpp>
24 #include <com/sun/star/document/XFilter.hpp>
25 #include <com/sun/star/document/XImporter.hpp>
26 #include <com/sun/star/io/WrongFormatException.hpp>
27 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
28 #include <com/sun/star/lang/XInitialization.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <cppuhelper/exc_hlp.hxx>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <osl/file.hxx>
34 #include <comphelper/diagnose_ex.hxx>
35 #include <unotools/mediadescriptor.hxx>
36 #include <unotools/streamwrap.hxx>
37 #include <unotools/ucbstreamhelper.hxx>
38 #include <comphelper/scopeguard.hxx>
40 #include <dmapper/DomainMapperFactory.hxx>
41 #include <rtftok/RTFDocument.hxx>
43 using namespace ::com::sun::star;
45 namespace
47 /// Invokes the RTF tokenizer + dmapper or RtfExportFilter in sw via UNO.
48 class RtfFilter
49 : public cppu::WeakImplHelper<document::XFilter, document::XImporter, document::XExporter,
50 lang::XInitialization, lang::XServiceInfo>
52 uno::Reference<uno::XComponentContext> m_xContext;
53 uno::Reference<lang::XComponent> m_xSrcDoc, m_xDstDoc;
55 public:
56 explicit RtfFilter(uno::Reference<uno::XComponentContext> xContext);
58 // XFilter
59 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
60 void SAL_CALL cancel() override;
62 // XImporter
63 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
65 // XExporter
66 void SAL_CALL setSourceDocument(const uno::Reference<lang::XComponent>& xDoc) override;
68 // XInitialization
69 void SAL_CALL initialize(const uno::Sequence<uno::Any>& rArguments) override;
71 // XServiceInfo
72 OUString SAL_CALL getImplementationName() override;
73 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
74 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
78 RtfFilter::RtfFilter(uno::Reference<uno::XComponentContext> xContext)
79 : m_xContext(std::move(xContext))
83 sal_Bool RtfFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
85 if (m_xSrcDoc.is())
87 uno::Reference<lang::XMultiServiceFactory> xMSF(m_xContext->getServiceManager(),
88 uno::UNO_QUERY_THROW);
89 uno::Reference<uno::XInterface> xIfc(
90 xMSF->createInstance("com.sun.star.comp.Writer.RtfExport"), uno::UNO_SET_THROW);
91 uno::Reference<document::XExporter> xExporter(xIfc, uno::UNO_QUERY_THROW);
92 uno::Reference<document::XFilter> xFilter(xIfc, uno::UNO_QUERY_THROW);
93 xExporter->setSourceDocument(m_xSrcDoc);
94 return xFilter->filter(rDescriptor);
97 bool bResult(false);
98 uno::Reference<task::XStatusIndicator> xStatusIndicator;
100 uno::Reference<beans::XPropertySet> xDocProps;
101 if (m_xDstDoc.is()) // not in cppunittest?
103 xDocProps.set(m_xDstDoc, uno::UNO_QUERY);
104 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::Any(true));
106 comphelper::ScopeGuard g([xDocProps] {
107 if (xDocProps.is()) // not in cppunittest?
109 // note: pStream.clear calls RemoveLastParagraph()
110 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::Any(false));
116 utl::MediaDescriptor aMediaDesc(rDescriptor);
117 bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault("RepairPackage", false);
118 bool bIsNewDoc = !aMediaDesc.getUnpackedValueOrDefault("InsertMode", false);
119 uno::Reference<io::XInputStream> xInputStream;
121 aMediaDesc.addInputStream();
122 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM] >>= xInputStream;
124 // If this is set, write to this file, instead of the real document during paste.
125 char* pEnv = getenv("SW_DEBUG_RTF_PASTE_TO");
126 OUString aOutStr;
127 if (!bIsNewDoc && pEnv
128 && osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pEnv), aOutStr)
129 == osl::FileBase::E_None)
131 std::unique_ptr<SvStream> pOut(
132 utl::UcbStreamHelper::CreateStream(aOutStr, StreamMode::WRITE));
133 std::unique_ptr<SvStream> pIn(utl::UcbStreamHelper::CreateStream(xInputStream));
134 pOut->WriteStream(*pIn);
135 return true;
138 // If this is set, read from this file, instead of the real clipboard during paste.
139 pEnv = getenv("SW_DEBUG_RTF_PASTE_FROM");
140 if (!bIsNewDoc && pEnv)
142 OUString aInStr;
143 osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pEnv), aInStr);
144 std::unique_ptr<SvStream> pStream
145 = utl::UcbStreamHelper::CreateStream(aInStr, StreamMode::READ);
146 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(std::move(pStream)));
147 xInputStream.set(xStream, uno::UNO_QUERY);
150 uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(
151 utl::MediaDescriptor::PROP_FRAME, uno::Reference<frame::XFrame>());
153 xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(
154 utl::MediaDescriptor::PROP_STATUSINDICATOR, uno::Reference<task::XStatusIndicator>());
156 writerfilter::Stream::Pointer_t pStream(
157 writerfilter::dmapper::DomainMapperFactory::createMapper(
158 m_xContext, xInputStream, m_xDstDoc, bRepairStorage,
159 writerfilter::dmapper::SourceDocumentType::RTF, aMediaDesc));
160 writerfilter::rtftok::RTFDocument::Pointer_t pDocument(
161 writerfilter::rtftok::RTFDocumentFactory::createDocument(
162 m_xContext, xInputStream, m_xDstDoc, xFrame, xStatusIndicator, aMediaDesc));
163 pDocument->resolve(*pStream);
164 bResult = true;
166 catch (const io::WrongFormatException&)
168 css::uno::Any anyEx = cppu::getCaughtException();
169 // cannot throw WrongFormatException directly :(
170 throw lang::WrappedTargetRuntimeException("", getXWeak(), anyEx);
172 catch (const uno::Exception&)
174 TOOLS_INFO_EXCEPTION("writerfilter", "Exception caught");
177 if (xStatusIndicator.is())
178 xStatusIndicator->end();
179 return bResult;
182 void RtfFilter::cancel() {}
184 void RtfFilter::setSourceDocument(const uno::Reference<lang::XComponent>& xDoc)
186 m_xSrcDoc = xDoc;
189 void RtfFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
191 m_xDstDoc = xDoc;
194 void RtfFilter::initialize(const uno::Sequence<uno::Any>& /*aArguments*/)
196 // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
197 // 'Word Template' but we don't need it for RTF.
200 OUString RtfFilter::getImplementationName() { return "com.sun.star.comp.Writer.RtfFilter"; }
202 sal_Bool RtfFilter::supportsService(const OUString& rServiceName)
204 return cppu::supportsService(this, rServiceName);
207 uno::Sequence<OUString> RtfFilter::getSupportedServiceNames()
209 uno::Sequence<OUString> aRet = { OUString("com.sun.star.document.ImportFilter"),
210 OUString("com.sun.star.document.ExportFilter") };
211 return aRet;
214 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
215 com_sun_star_comp_Writer_RtfFilter_get_implementation(uno::XComponentContext* pComponent,
216 uno::Sequence<uno::Any> const& /*rSequence*/)
218 return cppu::acquire(new RtfFilter(pComponent));
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */