update credits
[LibreOffice.git] / writerfilter / source / filter / RtfFilter.cxx
blob466afd672a62ebc53a17a9e61e1f0520d5863612
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 <rtl/ustring.hxx>
21 #include <cppuhelper/implementationentry.hxx>
22 #include <osl/module.hxx>
23 #include <tools/solar.h>
24 #include <RtfFilter.hxx>
25 #include <comphelper/mediadescriptor.hxx>
26 #include <dmapper/DomainMapper.hxx>
27 #include <rtftok/RTFDocument.hxx>
28 #include <com/sun/star/frame/XFrame.hpp>
29 #include <com/sun/star/task/XStatusIndicator.hpp>
30 #include <com/sun/star/io/WrongFormatException.hpp>
31 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
32 #include <com/sun/star/text/XTextRange.hpp>
33 #include <unotools/localfilehelper.hxx>
34 #include <tools/stream.hxx>
35 #include <unotools/ucbstreamhelper.hxx>
36 #include <unotools/streamwrap.hxx>
38 using namespace ::rtl;
39 using namespace ::cppu;
40 using namespace ::com::sun::star;
41 using ::comphelper::MediaDescriptor;
43 RtfFilter::RtfFilter( const uno::Reference< uno::XComponentContext >& rxContext) :
44 m_xContext( rxContext )
48 RtfFilter::~RtfFilter()
52 sal_Bool RtfFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
53 throw (uno::RuntimeException)
55 sal_uInt32 nStartTime = osl_getGlobalTimer();
56 if( m_xSrcDoc.is() )
58 uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
59 uno::Reference< uno::XInterface > xIfc( xMSF->createInstance("com.sun.star.comp.Writer.RtfExport"), uno::UNO_QUERY_THROW);
60 if (!xIfc.is())
61 return sal_False;
62 uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
63 uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
64 if (!xExprtr.is() || !xFltr.is())
65 return sal_False;
66 xExprtr->setSourceDocument(m_xSrcDoc);
67 return xFltr->filter(aDescriptor);
70 sal_Bool bResult(sal_False);
71 uno::Reference<task::XStatusIndicator> xStatusIndicator;
73 try
75 MediaDescriptor aMediaDesc( aDescriptor );
76 bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
77 bool bIsNewDoc = aMediaDesc.getUnpackedValueOrDefault( "IsNewDoc", true );
78 uno::Reference<text::XTextRange> xInsertTextRange = aMediaDesc.getUnpackedValueOrDefault( "TextInsertModeRange", uno::Reference<text::XTextRange>());
79 #ifdef DEBUG_IMPORT
80 OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
81 ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
83 writerfilter::TagLogger::Pointer_t dmapperLogger
84 (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
85 dmapperLogger->setFileName(sURLc);
86 dmapperLogger->startDocument();
87 #endif
88 uno::Reference< io::XInputStream > xInputStream;
90 aMediaDesc.addInputStream();
91 aMediaDesc[ MediaDescriptor::PROP_INPUTSTREAM() ] >>= xInputStream;
93 // If this is set, write to this file, instead of the real document during paste.
94 char* pEnv = getenv("SW_DEBUG_RTF_PASTE_TO");
95 OUString aOutStr;
96 if (!bIsNewDoc && pEnv && utl::LocalFileHelper::ConvertPhysicalNameToURL(OStringToOUString(pEnv, RTL_TEXTENCODING_UTF8), aOutStr))
98 SvStream* pOut = utl::UcbStreamHelper::CreateStream(aOutStr, STREAM_WRITE);
99 SvStream* pIn = utl::UcbStreamHelper::CreateStream(xInputStream);
100 *pOut << *pIn;
101 delete pOut;
102 return true;
105 // If this is set, read from this file, instead of the real clipboard during paste.
106 pEnv = getenv("SW_DEBUG_RTF_PASTE_FROM");
107 if (!bIsNewDoc && pEnv)
109 OUString aInStr;
110 utl::LocalFileHelper::ConvertPhysicalNameToURL(OStringToOUString(pEnv, RTL_TEXTENCODING_UTF8), aInStr);
111 SvStream* pStream = utl::UcbStreamHelper::CreateStream(aInStr, STREAM_READ);
112 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStream));
113 xInputStream.set(xStream, uno::UNO_QUERY);
116 uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_FRAME(),
117 uno::Reference<frame::XFrame>());
119 xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_STATUSINDICATOR(),
120 uno::Reference<task::XStatusIndicator>());
122 writerfilter::Stream::Pointer_t pStream(
123 new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, bRepairStorage, writerfilter::dmapper::DOCUMENT_RTF, xInsertTextRange, bIsNewDoc));
124 writerfilter::rtftok::RTFDocument::Pointer_t const pDocument(
125 writerfilter::rtftok::RTFDocumentFactory::createDocument(m_xContext, xInputStream, m_xDstDoc, xFrame, xStatusIndicator));
126 pDocument->resolve(*pStream);
127 bResult = sal_True;
128 #ifdef DEBUG_IMPORT
129 dmapperLogger->endDocument();
130 #endif
131 sal_uInt32 nEndTime = osl_getGlobalTimer();
132 SAL_INFO("writerfilter.profile", OSL_THIS_FUNC << " finished in " << nEndTime - nStartTime << " ms");
134 catch (const io::WrongFormatException& e)
136 // cannot throw WrongFormatException directly :(
137 throw lang::WrappedTargetRuntimeException("",
138 static_cast<OWeakObject*>(this), uno::makeAny(e));
140 catch (const uno::Exception& e)
142 SAL_INFO("writerfilter", "Exception caught: " << e.Message);
145 if (xStatusIndicator.is())
146 xStatusIndicator->end();
147 return bResult;
150 void RtfFilter::cancel( ) throw (uno::RuntimeException)
154 void RtfFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
155 throw (lang::IllegalArgumentException, uno::RuntimeException)
157 m_xSrcDoc = xDoc;
160 void RtfFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
161 throw (lang::IllegalArgumentException, uno::RuntimeException)
163 m_xDstDoc = xDoc;
166 void RtfFilter::initialize( const uno::Sequence< uno::Any >& /*aArguments*/ ) throw (uno::Exception, uno::RuntimeException)
168 // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
169 // 'Word Template' but we don't need it for RTF.
172 OUString RtfFilter::getImplementationName( ) throw (uno::RuntimeException)
174 return RtfFilter_getImplementationName();
177 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
178 #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
179 sal_Bool RtfFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
181 return ( rServiceName == SERVICE_NAME1 || rServiceName == SERVICE_NAME2 );
184 uno::Sequence< OUString > RtfFilter::getSupportedServiceNames( ) throw (uno::RuntimeException)
186 return RtfFilter_getSupportedServiceNames();
189 /* Helpers, used by shared lib exports. */
191 OUString RtfFilter_getImplementationName () throw (uno::RuntimeException)
193 return OUString ( "com.sun.star.comp.Writer.RtfFilter" );
196 uno::Sequence< OUString > RtfFilter_getSupportedServiceNames( ) throw (uno::RuntimeException)
198 uno::Sequence < OUString > aRet(2);
199 OUString* pArray = aRet.getArray();
200 pArray[0] = SERVICE_NAME1;
201 pArray[1] = SERVICE_NAME2;
202 return aRet;
204 #undef SERVICE_NAME1
205 #undef SERVICE_NAME2
207 uno::Reference< uno::XInterface > RtfFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
208 throw( uno::Exception )
210 return (cppu::OWeakObject*) new RtfFilter( xContext );
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */