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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <memory>
21
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 <tools/diagnose_ex.h>
35 #include <unotools/mediadescriptor.hxx>
36 #include <unotools/streamwrap.hxx>
37 #include <unotools/ucbstreamhelper.hxx>
38 #include <comphelper/scopeguard.hxx>
39
40 #include <dmapper/DomainMapperFactory.hxx>
41 #include <rtftok/RTFDocument.hxx>
42
43 using namespace ::com::sun::star;
44
45 namespace
46 {
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>
51 {
52 uno::Reference<uno::XComponentContext> m_xContext;
53 uno::Reference<lang::XComponent> m_xSrcDoc, m_xDstDoc;
54
55 public:
56 explicit RtfFilter(uno::Reference<uno::XComponentContext> xContext);
57
58 // XFilter
59 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
60 void SAL_CALL cancel() override;
61
62 // XImporter
63 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
64
65 // XExporter
66 void SAL_CALL setSourceDocument(const uno::Reference<lang::XComponent>& xDoc) override;
67
68 // XInitialization
69 void SAL_CALL initialize(const uno::Sequence<uno::Any>& rArguments) override;
70
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;
75 };
76 }
77
RtfFilter(uno::Reference<uno::XComponentContext> xContext)78 RtfFilter::RtfFilter(uno::Reference<uno::XComponentContext> xContext)
79 : m_xContext(std::move(xContext))
80 {
81 }
82
filter(const uno::Sequence<beans::PropertyValue> & rDescriptor)83 sal_Bool RtfFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
84 {
85 if (m_xSrcDoc.is())
86 {
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);
95 }
96
97 bool bResult(false);
98 uno::Reference<task::XStatusIndicator> xStatusIndicator;
99
100 uno::Reference<beans::XPropertySet> xDocProps;
101 if (m_xDstDoc.is()) // not in cppunittest?
102 {
103 xDocProps.set(m_xDstDoc, uno::UNO_QUERY);
104 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::makeAny(true));
105 }
106 comphelper::ScopeGuard g([xDocProps] {
107 if (xDocProps.is()) // not in cppunittest?
108 {
109 // note: pStream.clear calls RemoveLastParagraph()
110 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::makeAny(false));
111 }
112 });
113
114 try
115 {
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;
120
121 aMediaDesc.addInputStream();
122 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
123
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)
130 {
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;
136 }
137
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)
141 {
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);
148 }
149
150 uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(
151 utl::MediaDescriptor::PROP_FRAME(), uno::Reference<frame::XFrame>());
152
153 xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(
154 utl::MediaDescriptor::PROP_STATUSINDICATOR(), uno::Reference<task::XStatusIndicator>());
155
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;
165 }
166 catch (const io::WrongFormatException&)
167 {
168 css::uno::Any anyEx = cppu::getCaughtException();
169 // cannot throw WrongFormatException directly :(
170 throw lang::WrappedTargetRuntimeException("", static_cast<OWeakObject*>(this), anyEx);
171 }
172 catch (const uno::Exception&)
173 {
174 TOOLS_INFO_EXCEPTION("writerfilter", "Exception caught");
175 }
176
177 if (xStatusIndicator.is())
178 xStatusIndicator->end();
179 return bResult;
180 }
181
cancel()182 void RtfFilter::cancel() {}
183
setSourceDocument(const uno::Reference<lang::XComponent> & xDoc)184 void RtfFilter::setSourceDocument(const uno::Reference<lang::XComponent>& xDoc)
185 {
186 m_xSrcDoc = xDoc;
187 }
188
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)189 void RtfFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
190 {
191 m_xDstDoc = xDoc;
192 }
193
initialize(const uno::Sequence<uno::Any> &)194 void RtfFilter::initialize(const uno::Sequence<uno::Any>& /*aArguments*/)
195 {
196 // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
197 // 'Word Template' but we don't need it for RTF.
198 }
199
getImplementationName()200 OUString RtfFilter::getImplementationName() { return "com.sun.star.comp.Writer.RtfFilter"; }
201
supportsService(const OUString & rServiceName)202 sal_Bool RtfFilter::supportsService(const OUString& rServiceName)
203 {
204 return cppu::supportsService(this, rServiceName);
205 }
206
getSupportedServiceNames()207 uno::Sequence<OUString> RtfFilter::getSupportedServiceNames()
208 {
209 uno::Sequence<OUString> aRet = { OUString("com.sun.star.document.ImportFilter"),
210 OUString("com.sun.star.document.ExportFilter") };
211 return aRet;
212 }
213
214 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
com_sun_star_comp_Writer_RtfFilter_get_implementation(uno::XComponentContext * pComponent,uno::Sequence<uno::Any> const &)215 com_sun_star_comp_Writer_RtfFilter_get_implementation(uno::XComponentContext* pComponent,
216 uno::Sequence<uno::Any> const& /*rSequence*/)
217 {
218 return cppu::acquire(new RtfFilter(pComponent));
219 }
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
221