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