lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / writerfilter / source / filter / RtfFilter.cxx
blobb024a2214e1eb268c0f66afa1fd0d843ed59f56c
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/document/XExporter.hpp>
23 #include <com/sun/star/document/XFilter.hpp>
24 #include <com/sun/star/document/XImporter.hpp>
25 #include <com/sun/star/io/WrongFormatException.hpp>
26 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <cppuhelper/exc_hlp.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <osl/file.hxx>
33 #include <sal/log.hxx>
34 #include <unotools/mediadescriptor.hxx>
35 #include <unotools/streamwrap.hxx>
36 #include <unotools/ucbstreamhelper.hxx>
38 #include <dmapper/DomainMapperFactory.hxx>
39 #include <rtftok/RTFDocument.hxx>
41 using namespace ::com::sun::star;
43 /// Invokes the RTF tokenizer + dmapper or RtfExportFilter in sw via UNO.
44 class RtfFilter : public cppu::WeakImplHelper
46 document::XFilter,
47 document::XImporter,
48 document::XExporter,
49 lang::XInitialization,
50 lang::XServiceInfo
53 uno::Reference<uno::XComponentContext> m_xContext;
54 uno::Reference<lang::XComponent> m_xSrcDoc, m_xDstDoc;
56 public:
57 explicit RtfFilter(uno::Reference<uno::XComponentContext> xContext);
59 // XFilter
60 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
61 void SAL_CALL cancel() override;
63 // XImporter
64 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
66 // XExporter
67 void SAL_CALL setSourceDocument(const uno::Reference<lang::XComponent>& xDoc) override;
69 // XInitialization
70 void SAL_CALL initialize(const uno::Sequence<uno::Any>& rArguments) override;
72 // XServiceInfo
73 OUString SAL_CALL getImplementationName() override;
74 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
75 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
79 RtfFilter::RtfFilter(uno::Reference<uno::XComponentContext> xContext)
80 : m_xContext(std::move(xContext))
84 sal_Bool RtfFilter::filter(const uno::Sequence< beans::PropertyValue >& rDescriptor)
86 sal_uInt32 nStartTime = osl_getGlobalTimer();
87 if (m_xSrcDoc.is())
89 uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
90 uno::Reference< uno::XInterface > xIfc(xMSF->createInstance("com.sun.star.comp.Writer.RtfExport"), uno::UNO_QUERY_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;
102 utl::MediaDescriptor aMediaDesc(rDescriptor);
103 bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault("RepairPackage", false);
104 bool bIsNewDoc = !aMediaDesc.getUnpackedValueOrDefault("InsertMode", false);
105 uno::Reference< io::XInputStream > xInputStream;
107 aMediaDesc.addInputStream();
108 aMediaDesc[ utl::MediaDescriptor::PROP_INPUTSTREAM() ] >>= xInputStream;
110 // If this is set, write to this file, instead of the real document during paste.
111 char* pEnv = getenv("SW_DEBUG_RTF_PASTE_TO");
112 OUString aOutStr;
113 if (!bIsNewDoc && pEnv && osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pEnv), aOutStr) == osl::FileBase::E_None)
115 std::unique_ptr<SvStream> pOut(utl::UcbStreamHelper::CreateStream(aOutStr, StreamMode::WRITE));
116 std::unique_ptr<SvStream> pIn(utl::UcbStreamHelper::CreateStream(xInputStream));
117 pOut->WriteStream(*pIn);
118 return true;
121 // If this is set, read from this file, instead of the real clipboard during paste.
122 pEnv = getenv("SW_DEBUG_RTF_PASTE_FROM");
123 if (!bIsNewDoc && pEnv)
125 OUString aInStr;
126 osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pEnv), aInStr);
127 std::unique_ptr<SvStream> pStream = utl::UcbStreamHelper::CreateStream(aInStr, StreamMode::READ);
128 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(std::move(pStream)));
129 xInputStream.set(xStream, uno::UNO_QUERY);
132 uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_FRAME(),
133 uno::Reference<frame::XFrame>());
135 xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_STATUSINDICATOR(),
136 uno::Reference<task::XStatusIndicator>());
138 writerfilter::Stream::Pointer_t pStream(
139 writerfilter::dmapper::DomainMapperFactory::createMapper(
140 m_xContext, xInputStream, m_xDstDoc, bRepairStorage,
141 writerfilter::dmapper::SourceDocumentType::RTF, aMediaDesc));
142 writerfilter::rtftok::RTFDocument::Pointer_t pDocument(
143 writerfilter::rtftok::RTFDocumentFactory::createDocument(m_xContext, xInputStream, m_xDstDoc, xFrame, xStatusIndicator, aMediaDesc));
144 pDocument->resolve(*pStream);
145 bResult = true;
146 sal_uInt32 nEndTime = osl_getGlobalTimer();
147 SAL_INFO("writerfilter.profile", "RtfFilter::filter: finished in " << nEndTime - nStartTime << " ms");
149 catch (const io::WrongFormatException&)
151 css::uno::Any anyEx = cppu::getCaughtException();
152 // cannot throw WrongFormatException directly :(
153 throw lang::WrappedTargetRuntimeException("",
154 static_cast<OWeakObject*>(this), anyEx);
156 catch (const uno::Exception& e)
158 SAL_INFO("writerfilter", "Exception caught: " << e);
161 if (xStatusIndicator.is())
162 xStatusIndicator->end();
163 return bResult;
166 void RtfFilter::cancel()
170 void RtfFilter::setSourceDocument(const uno::Reference< lang::XComponent >& xDoc)
172 m_xSrcDoc = xDoc;
175 void RtfFilter::setTargetDocument(const uno::Reference< lang::XComponent >& xDoc)
177 m_xDstDoc = xDoc;
180 void RtfFilter::initialize(const uno::Sequence< uno::Any >& /*aArguments*/)
182 // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
183 // 'Word Template' but we don't need it for RTF.
186 OUString RtfFilter::getImplementationName()
188 return OUString("com.sun.star.comp.Writer.RtfFilter");
191 sal_Bool RtfFilter::supportsService(const OUString& rServiceName)
193 return cppu::supportsService(this, rServiceName);
196 uno::Sequence<OUString> RtfFilter::getSupportedServiceNames()
198 uno::Sequence<OUString> aRet =
200 OUString("com.sun.star.document.ImportFilter"),
201 OUString("com.sun.star.document.ExportFilter")
203 return aRet;
206 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* com_sun_star_comp_Writer_RtfFilter_get_implementation(uno::XComponentContext* pComponent, uno::Sequence<uno::Any> const& /*rSequence*/)
208 return cppu::acquire(new RtfFilter(pComponent));
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */