LibreOffice Module sw (master)  1
rtfexportfilter.cxx
Go to the documentation of this file.
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 "rtfexportfilter.hxx"
21 #include "rtfexport.hxx"
22 
23 #include <docsh.hxx>
25 #include <unotxdoc.hxx>
26 #include <viewsh.hxx>
27 
30 
31 using namespace ::com::sun::star;
32 
33 RtfExportFilter::RtfExportFilter(uno::Reference<uno::XComponentContext> xCtx)
34  : m_xCtx(std::move(xCtx))
35 {
36 }
37 
39 
40 sal_Bool RtfExportFilter::filter(const uno::Sequence<beans::PropertyValue>& aDescriptor)
41 {
42  utl::MediaDescriptor aMediaDesc = aDescriptor;
43  uno::Reference<io::XStream> xStream = aMediaDesc.getUnpackedValueOrDefault(
44  utl::MediaDescriptor::PROP_STREAMFOROUTPUT(), uno::Reference<io::XStream>());
45  std::unique_ptr<SvStream> pStream = utl::UcbStreamHelper::CreateStream(xStream, true);
46  m_aWriter.SetStream(pStream.get());
47 
48  // get SwDoc*
49  uno::Reference<uno::XInterface> xIfc(m_xSrcDoc, uno::UNO_QUERY);
50  auto pTextDoc = dynamic_cast<SwXTextDocument*>(xIfc.get());
51  if (!pTextDoc)
52  {
53  return false;
54  }
55 
56  SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc();
57  if (!pDoc)
58  {
59  return false;
60  }
61 
62  // fdo#37161 - update layout (if present), for SwWriteTable
64  if (pViewShell != nullptr)
65  pViewShell->CalcLayout();
66 
67  // get SwPaM*
68  // we get SwPaM for the entire document; copy&paste is handled internally, not via UNO
69  SwPaM aPam(pDoc->GetNodes().GetEndOfContent());
70  aPam.SetMark();
71  aPam.Move(fnMoveBackward, GoInDoc);
72 
73  auto pCurPam = std::make_unique<SwPaM>(*aPam.End(), *aPam.Start());
74 
75  // export the document
76  // (in a separate block so that it's destructed before the commit)
77  {
78  RtfExport aExport(this, pDoc, pCurPam.get(), &aPam, nullptr);
79  aExport.ExportDocument(true);
80  }
81 
82  // delete the pCurPam
83  while (pCurPam->GetNext() != pCurPam.get())
84  delete pCurPam->GetNext();
85 
86  return true;
87 }
88 
90 
91 void RtfExportFilter::setSourceDocument(const uno::Reference<lang::XComponent>& xDoc)
92 {
93  m_xSrcDoc = xDoc;
94 }
95 
96 // UNO helpers
97 
98 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
100  uno::Sequence<uno::Any> const& /*rSeq*/)
101 {
102  return cppu::acquire(new RtfExportFilter(pCtx));
103 }
104 
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const OUString & PROP_STREAMFOROUTPUT()
SwDocShell * GetDocShell()
Definition: doc.hxx:1340
virtual void CalcLayout()
Definition: viewsh.cxx:983
css::uno::Reference< css::lang::XComponent > m_xSrcDoc
Definition: doc.hxx:185
void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
~RtfExportFilter() override
Reference< XInputStream > xStream
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
The class that does all the actual RTF export-related work.
Definition: rtfexport.hxx:36
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:164
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Writer_RtfExport_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)
SwDoc * GetDoc()
returns Doc. But be careful!
Definition: docsh.hxx:202
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &aDescriptor) override
unsigned char sal_Bool
bool GoInDoc(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:882
void SetStream(SvStream *const pStream)
Definition: writer.cxx:227
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:437
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
SwNodes & GetNodes()
Definition: doc.hxx:403
SwMoveFnCollection const & fnMoveBackward
Definition: paminit.cxx:58
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:457
RtfExportFilter(css::uno::Reference< css::uno::XComponentContext > xCtx)
void SAL_CALL cancel() override
The physical access to the RTF document (for writing).
ErrCode ExportDocument(bool bWriteAll)
The main function to export the document.
Definition: wrtww8.cxx:3154