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 <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>
28 :
29 : #include <unotextrange.hxx>
30 :
31 : #include <unotools/streamwrap.hxx>
32 : #include <comphelper/processfactory.hxx>
33 :
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>
39 :
40 : using namespace ::com::sun::star;
41 :
42 : /// Glue class to call RtfImport as an internal filter, needed by copy&paste support.
43 0 : class SwRTFReader : public Reader
44 : {
45 : virtual sal_uLong Read( SwDoc &, const OUString& rBaseURL, SwPaM &,const OUString &) SAL_OVERRIDE;
46 : };
47 :
48 0 : sal_uLong SwRTFReader::Read( SwDoc &rDoc, const OUString& /*rBaseURL*/, SwPaM& rPam, const OUString &)
49 : {
50 0 : if (!pStrm)
51 0 : return ERR_SWG_READ_ERROR;
52 :
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 0 : SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
58 0 : SwNodeIndex *pSttNdIdx = new SwNodeIndex(rDoc.GetNodes());
59 0 : const SwPosition* pPos = rPam.GetPoint();
60 :
61 : // Step 2: Split once and remember the node that has been split.
62 0 : rDoc.SplitNode( *pPos, false );
63 0 : *pSttNdIdx = pPos->nNode.GetIndex()-1;
64 :
65 : // Step 3: Split again.
66 0 : rDoc.SplitNode( *pPos, false );
67 :
68 : // Step 4: Insert all content into the new node
69 0 : rPam.Move( fnMoveBackward );
70 : rDoc.SetTxtFmtColl
71 0 : ( rPam, rDoc.GetTxtCollFromPool(RES_POOLCOLL_STANDARD, false ) );
72 :
73 0 : SwDocShell *pDocShell(rDoc.GetDocShell());
74 0 : uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
75 0 : uno::Reference<uno::XInterface> xInterface(xMultiServiceFactory->createInstance(
76 0 : "com.sun.star.comp.Writer.RtfFilter"), uno::UNO_QUERY_THROW);
77 :
78 0 : uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
79 0 : uno::Reference<lang::XComponent> xDstDoc(pDocShell->GetModel(), uno::UNO_QUERY_THROW);
80 0 : xImporter->setTargetDocument(xDstDoc);
81 :
82 : const uno::Reference<text::XTextRange> xInsertTextRange =
83 0 : SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
84 :
85 0 : uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
86 0 : uno::Sequence<beans::PropertyValue> aDescriptor(3);
87 0 : aDescriptor[0].Name = "InputStream";
88 0 : uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStrm));
89 0 : aDescriptor[0].Value <<= xStream;
90 0 : aDescriptor[1].Name = "InsertMode";
91 0 : aDescriptor[1].Value <<= sal_True;
92 0 : aDescriptor[2].Name = "TextInsertModeRange";
93 0 : aDescriptor[2].Value <<= xInsertTextRange;
94 0 : sal_uLong ret(0);
95 : try {
96 0 : xFilter->filter(aDescriptor);
97 : }
98 0 : catch (uno::Exception const& e)
99 : {
100 : SAL_WARN("sw.rtf", "SwRTFReader::Read(): exception: " << e.Message);
101 0 : ret = ERR_SWG_READ_ERROR;
102 : }
103 :
104 : // Clean up the fake paragraphs.
105 0 : SwUnoInternalPaM aPam(rDoc);
106 0 : ::sw::XTextRangeToSwPaM(aPam, xInsertPosition);
107 0 : if (pSttNdIdx->GetIndex())
108 : {
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 0 : SwTxtNode* pTxtNode = pSttNdIdx->GetNode().GetTxtNode();
113 0 : SwNodeIndex aNxtIdx( *pSttNdIdx );
114 0 : if( pTxtNode && pTxtNode->CanJoinNext( &aNxtIdx ) &&
115 0 : pSttNdIdx->GetIndex() + 1 == aNxtIdx.GetIndex() )
116 : {
117 : // If the PaM points to the first new node, move the PaM to the
118 : // end of the previous node.
119 0 : if( aPam.GetPoint()->nNode == aNxtIdx )
120 : {
121 0 : aPam.GetPoint()->nNode = *pSttNdIdx;
122 0 : aPam.GetPoint()->nContent.Assign( pTxtNode,
123 0 : pTxtNode->GetTxt().getLength() );
124 : }
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 0 : SwTxtNode* pDelNd = aNxtIdx.GetNode().GetTxtNode();
129 0 : if (pTxtNode->GetTxt().getLength())
130 0 : pDelNd->FmtToTxtAttr( pTxtNode );
131 : else
132 0 : pTxtNode->ChgFmtColl( pDelNd->GetTxtColl() );
133 0 : pTxtNode->JoinNext();
134 0 : }
135 : }
136 :
137 0 : return ret;
138 : }
139 :
140 0 : extern "C" SAL_DLLPUBLIC_EXPORT Reader* SAL_CALL ImportRTF()
141 : {
142 0 : return new SwRTFReader();
143 : }
144 :
145 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|