LCOV - code coverage report
Current view: top level - sw/source/filter/rtf - swparrtf.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 46 53 86.8 %
Date: 2014-04-11 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          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           3 : class SwRTFReader : public Reader
      44             : {
      45             :     virtual sal_uLong Read( SwDoc &, const OUString& rBaseURL, SwPaM &,const OUString &) SAL_OVERRIDE;
      46             : };
      47             : 
      48           1 : sal_uLong SwRTFReader::Read( SwDoc &rDoc, const OUString& /*rBaseURL*/, SwPaM& rPam, const OUString &)
      49             : {
      50           1 :     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           1 :         SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
      58           1 :     SwNodeIndex *pSttNdIdx = new SwNodeIndex(rDoc.GetNodes());
      59           1 :     const SwPosition* pPos = rPam.GetPoint();
      60             : 
      61             :     // Step 2: Split once and remember the node that has been split.
      62           1 :     rDoc.SplitNode( *pPos, false );
      63           1 :     *pSttNdIdx = pPos->nNode.GetIndex()-1;
      64             : 
      65             :     // Step 3: Split again.
      66           1 :     rDoc.SplitNode( *pPos, false );
      67             : 
      68             :     // Step 4: Insert all content into the new node
      69           1 :     rPam.Move( fnMoveBackward );
      70             :     rDoc.SetTxtFmtColl
      71           1 :         ( rPam, rDoc.GetTxtCollFromPool(RES_POOLCOLL_STANDARD, false ) );
      72             : 
      73           1 :     SwDocShell *pDocShell(rDoc.GetDocShell());
      74           2 :     uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
      75           1 :     uno::Reference<uno::XInterface> xInterface(xMultiServiceFactory->createInstance(
      76           2 :         "com.sun.star.comp.Writer.RtfFilter"), uno::UNO_QUERY_THROW);
      77             : 
      78           2 :     uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
      79           2 :     uno::Reference<lang::XComponent> xDstDoc(pDocShell->GetModel(), uno::UNO_QUERY_THROW);
      80           1 :     xImporter->setTargetDocument(xDstDoc);
      81             : 
      82             :     const uno::Reference<text::XTextRange> xInsertTextRange =
      83           2 :         SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0);
      84             : 
      85           2 :     uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
      86           2 :     uno::Sequence<beans::PropertyValue> aDescriptor(3);
      87           1 :     aDescriptor[0].Name = "InputStream";
      88           2 :     uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStrm));
      89           1 :     aDescriptor[0].Value <<= xStream;
      90           1 :     aDescriptor[1].Name = "InsertMode";
      91           1 :     aDescriptor[1].Value <<= sal_True;
      92           1 :     aDescriptor[2].Name = "TextInsertModeRange";
      93           1 :     aDescriptor[2].Value <<= xInsertTextRange;
      94           1 :     sal_uLong ret(0);
      95             :     try {
      96           1 :         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           2 :     SwUnoInternalPaM aPam(rDoc);
     106           1 :     ::sw::XTextRangeToSwPaM(aPam, xInsertPosition);
     107           1 :     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           1 :         SwTxtNode* pTxtNode = pSttNdIdx->GetNode().GetTxtNode();
     113           1 :         SwNodeIndex aNxtIdx( *pSttNdIdx );
     114           2 :         if( pTxtNode && pTxtNode->CanJoinNext( &aNxtIdx ) &&
     115           1 :                 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           1 :             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           1 :             SwTxtNode* pDelNd = aNxtIdx.GetNode().GetTxtNode();
     129           1 :             if (pTxtNode->GetTxt().getLength())
     130           1 :                 pDelNd->FmtToTxtAttr( pTxtNode );
     131             :             else
     132           0 :                 pTxtNode->ChgFmtColl( pDelNd->GetTxtColl() );
     133           1 :             pTxtNode->JoinNext();
     134           1 :         }
     135             :     }
     136             : 
     137           2 :     return ret;
     138             : }
     139             : 
     140           1 : extern "C" SAL_DLLPUBLIC_EXPORT Reader* SAL_CALL ImportRTF()
     141             : {
     142           1 :     return new SwRTFReader();
     143             : }
     144             : 
     145             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10