bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / filter / rtf / swparrtf.cxx
blobad43cc9879fbcc44c3bfc117546404a3c81ffd18
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 <tools/stream.hxx>
21 #include <poolfmt.hxx>
22 #include <shellio.hxx>
23 #include <ndtxt.hxx>
24 #include <doc.hxx>
25 #include <docsh.hxx>
26 #include <pam.hxx>
27 #include <swerror.h>
29 #include <unotextrange.hxx>
31 #include <unotools/streamwrap.hxx>
32 #include <comphelper/processfactory.hxx>
34 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
35 #include <com/sun/star/document/XFilter.hpp>
36 #include <com/sun/star/document/XImporter.hpp>
37 #include <com/sun/star/document/XExporter.hpp>
38 #include <com/sun/star/text/XTextRange.hpp>
40 using namespace ::com::sun::star;
42 /// Glue class to call RtfImport as an internal filter, needed by copy&paste support.
43 class SwRTFReader : public Reader
45 virtual sal_uLong Read( SwDoc &, const String& rBaseURL, SwPaM &,const String &);
48 sal_uLong SwRTFReader::Read( SwDoc &rDoc, const String& /*rBaseURL*/, SwPaM& rPam, const String &)
50 if (!pStrm)
51 return ERR_SWG_READ_ERROR;
53 // We want to work in an empty paragraph.
54 // Step 1: XTextRange will be updated when content is inserted, so we know
55 // the end position.
56 const uno::Reference<text::XTextRange> xInsertPosition =
57 SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
58 SwNodeIndex *pSttNdIdx = new SwNodeIndex(rDoc.GetNodes());
59 const SwPosition* pPos = rPam.GetPoint();
61 // Step 2: Split once and remember the node that has been split.
62 rDoc.SplitNode( *pPos, false );
63 *pSttNdIdx = pPos->nNode.GetIndex()-1;
65 // Step 3: Split again.
66 rDoc.SplitNode( *pPos, false );
68 // Step 4: Insert all content into the new node
69 rPam.Move( fnMoveBackward );
70 rDoc.SetTxtFmtColl
71 ( rPam, rDoc.GetTxtCollFromPool(RES_POOLCOLL_STANDARD, false ) );
73 SwDocShell *pDocShell(rDoc.GetDocShell());
74 uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
75 uno::Reference<uno::XInterface> xInterface(xMultiServiceFactory->createInstance(
76 "com.sun.star.comp.Writer.RtfFilter"), uno::UNO_QUERY_THROW);
78 uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
79 uno::Reference<lang::XComponent> xDstDoc(pDocShell->GetModel(), uno::UNO_QUERY_THROW);
80 xImporter->setTargetDocument(xDstDoc);
82 const uno::Reference<text::XTextRange> xInsertTextRange =
83 SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
85 uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
86 uno::Sequence<beans::PropertyValue> aDescriptor(3);
87 aDescriptor[0].Name = "InputStream";
88 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStrm));
89 aDescriptor[0].Value <<= xStream;
90 aDescriptor[1].Name = "IsNewDoc";
91 aDescriptor[1].Value <<= sal_False;
92 aDescriptor[2].Name = "TextInsertModeRange";
93 aDescriptor[2].Value <<= xInsertTextRange;
94 sal_uLong ret(0);
95 try {
96 xFilter->filter(aDescriptor);
98 catch (uno::Exception const& e)
100 SAL_WARN("sw.rtf", "SwRTFReader::Read(): exception: " << e.Message);
101 ret = ERR_SWG_READ_ERROR;
104 // Clean up the fake paragraphs.
105 SwUnoInternalPaM aPam(rDoc);
106 ::sw::XTextRangeToSwPaM(aPam, xInsertPosition);
107 if (pSttNdIdx->GetIndex())
109 // If we are in insert mode, join the split node that is in front
110 // of the new content with the first new node. Or in other words:
111 // Revert the first split node.
112 SwTxtNode* pTxtNode = pSttNdIdx->GetNode().GetTxtNode();
113 SwNodeIndex aNxtIdx( *pSttNdIdx );
114 if( pTxtNode && pTxtNode->CanJoinNext( &aNxtIdx ) &&
115 pSttNdIdx->GetIndex() + 1 == aNxtIdx.GetIndex() )
117 // If the PaM points to the first new node, move the PaM to the
118 // end of the previous node.
119 if( aPam.GetPoint()->nNode == aNxtIdx )
121 aPam.GetPoint()->nNode = *pSttNdIdx;
122 aPam.GetPoint()->nContent.Assign( pTxtNode,
123 pTxtNode->GetTxt().getLength() );
125 // If the first new node isn't empty, convert the node's text
126 // attributes into hints. Otherwise, set the new node's
127 // paragraph style at the previous (empty) node.
128 SwTxtNode* pDelNd = aNxtIdx.GetNode().GetTxtNode();
129 if (pTxtNode->GetTxt().getLength())
130 pDelNd->FmtToTxtAttr( pTxtNode );
131 else
132 pTxtNode->ChgFmtColl( pDelNd->GetTxtColl() );
133 pTxtNode->JoinNext();
137 return ret;
140 extern "C" SAL_DLLPUBLIC_EXPORT Reader* SAL_CALL ImportRTF()
142 return new SwRTFReader();
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */