Line data Source code
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 <poolfmt.hxx>
21 : #include <shellio.hxx>
22 : #include <ndtxt.hxx>
23 : #include <doc.hxx>
24 : #include <docsh.hxx>
25 : #include <IDocumentStylePoolAccess.hxx>
26 : #include <swerror.h>
27 :
28 : #include <unotextrange.hxx>
29 :
30 : #include <unotools/streamwrap.hxx>
31 : #include <comphelper/processfactory.hxx>
32 :
33 : #include <com/sun/star/document/XFilter.hpp>
34 : #include <com/sun/star/document/XImporter.hpp>
35 :
36 : using namespace ::com::sun::star;
37 :
38 : /// Glue class to call RtfImport as an internal filter, needed by copy&paste support.
39 3 : class SwRTFReader : public Reader
40 : {
41 : virtual sal_uLong Read(SwDoc&, const OUString& rBaseURL, SwPaM&, const OUString&) SAL_OVERRIDE;
42 : };
43 :
44 1 : sal_uLong SwRTFReader::Read(SwDoc& rDoc, const OUString& /*rBaseURL*/, SwPaM& rPam, const OUString&)
45 : {
46 1 : if (!pStrm)
47 0 : return ERR_SWG_READ_ERROR;
48 :
49 : // We want to work in an empty paragraph.
50 : // Step 1: XTextRange will be updated when content is inserted, so we know
51 : // the end position.
52 1 : const uno::Reference<text::XTextRange> xInsertPosition = SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
53 2 : std::shared_ptr<SwNodeIndex> pSttNdIdx(new SwNodeIndex(rDoc.GetNodes()));
54 1 : const SwPosition* pPos = rPam.GetPoint();
55 :
56 : // Step 2: Split once and remember the node that has been split.
57 1 : rDoc.getIDocumentContentOperations().SplitNode(*pPos, false);
58 1 : *pSttNdIdx = pPos->nNode.GetIndex()-1;
59 :
60 : // Step 3: Split again.
61 1 : rDoc.getIDocumentContentOperations().SplitNode(*pPos, false);
62 2 : std::shared_ptr<SwNodeIndex> pSttNdIdx2(new SwNodeIndex(rDoc.GetNodes()));
63 1 : *pSttNdIdx2 = pPos->nNode.GetIndex();
64 :
65 : // Step 4: Insert all content into the new node
66 1 : rPam.Move(fnMoveBackward);
67 1 : rDoc.SetTextFormatColl(rPam, rDoc.getIDocumentStylePoolAccess().GetTextCollFromPool(RES_POOLCOLL_STANDARD, false));
68 :
69 1 : SwDocShell* pDocShell(rDoc.GetDocShell());
70 2 : uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
71 2 : uno::Reference<uno::XInterface> xInterface(xMultiServiceFactory->createInstance("com.sun.star.comp.Writer.RtfFilter"), uno::UNO_QUERY_THROW);
72 :
73 2 : uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
74 2 : uno::Reference<lang::XComponent> xDstDoc(pDocShell->GetModel(), uno::UNO_QUERY_THROW);
75 1 : xImporter->setTargetDocument(xDstDoc);
76 :
77 : const uno::Reference<text::XTextRange> xInsertTextRange =
78 2 : SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
79 :
80 2 : uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
81 2 : uno::Sequence<beans::PropertyValue> aDescriptor(3);
82 1 : aDescriptor[0].Name = "InputStream";
83 2 : uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStrm));
84 1 : aDescriptor[0].Value <<= xStream;
85 1 : aDescriptor[1].Name = "InsertMode";
86 1 : aDescriptor[1].Value <<= sal_True;
87 1 : aDescriptor[2].Name = "TextInsertModeRange";
88 1 : aDescriptor[2].Value <<= xInsertTextRange;
89 1 : sal_uLong ret(0);
90 : try
91 : {
92 1 : xFilter->filter(aDescriptor);
93 : }
94 0 : catch (uno::Exception const& e)
95 : {
96 : SAL_WARN("sw.rtf", "SwRTFReader::Read(): exception: " << e.Message);
97 0 : ret = ERR_SWG_READ_ERROR;
98 : }
99 :
100 : // Clean up the fake paragraphs.
101 2 : SwUnoInternalPaM aPam(rDoc);
102 1 : ::sw::XTextRangeToSwPaM(aPam, xInsertPosition);
103 1 : if (pSttNdIdx->GetIndex())
104 : {
105 : // If we are in insert mode, join the split node that is in front
106 : // of the new content with the first new node. Or in other words:
107 : // Revert the first split node.
108 1 : SwTextNode* pTextNode = pSttNdIdx->GetNode().GetTextNode();
109 1 : SwNodeIndex aNxtIdx(*pSttNdIdx);
110 1 : if (pTextNode && pTextNode->CanJoinNext(&aNxtIdx) && pSttNdIdx->GetIndex() + 1 == aNxtIdx.GetIndex())
111 : {
112 : // If the PaM points to the first new node, move the PaM to the
113 : // end of the previous node.
114 1 : if (aPam.GetPoint()->nNode == aNxtIdx)
115 : {
116 0 : aPam.GetPoint()->nNode = *pSttNdIdx;
117 0 : aPam.GetPoint()->nContent.Assign(pTextNode, pTextNode->GetText().getLength());
118 : }
119 : // If the first new node isn't empty, convert the node's text
120 : // attributes into hints. Otherwise, set the new node's
121 : // paragraph style at the previous (empty) node.
122 1 : SwTextNode* pDelNd = aNxtIdx.GetNode().GetTextNode();
123 1 : if (pTextNode->GetText().getLength())
124 1 : pDelNd->FormatToTextAttr(pTextNode);
125 : else
126 0 : pTextNode->ChgFormatColl(pDelNd->GetTextColl());
127 1 : pTextNode->JoinNext();
128 1 : }
129 : }
130 :
131 1 : if (pSttNdIdx2->GetIndex())
132 : {
133 : // If we are in insert mode, join the split node that is after
134 : // the new content with the last new node. Or in other words:
135 : // Revert the second split node.
136 1 : SwTextNode* pTextNode = pSttNdIdx2->GetNode().GetTextNode();
137 1 : SwNodeIndex aPrevIdx(*pSttNdIdx2);
138 1 : if (pTextNode && pTextNode->CanJoinPrev(&aPrevIdx) && pSttNdIdx2->GetIndex() - 1 == aPrevIdx.GetIndex())
139 : {
140 : // If the last new node isn't empty, convert the node's text
141 : // attributes into hints. Otherwise, set the new node's
142 : // paragraph style at the next (empty) node.
143 1 : SwTextNode* pDelNd = aPrevIdx.GetNode().GetTextNode();
144 1 : if (pTextNode->GetText().getLength())
145 1 : pDelNd->FormatToTextAttr(pTextNode);
146 : else
147 0 : pTextNode->ChgFormatColl(pDelNd->GetTextColl());
148 1 : pTextNode->JoinPrev();
149 1 : }
150 : }
151 :
152 2 : return ret;
153 : }
154 :
155 1 : extern "C" SAL_DLLPUBLIC_EXPORT Reader* SAL_CALL ImportRTF()
156 : {
157 1 : return new SwRTFReader();
158 60 : }
159 :
160 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|