LCOV - code coverage report
Current view: top level - sw/source/filter/xml - XMLRedlineImportHelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 188 250 75.2 %
Date: 2014-04-11 Functions: 22 25 88.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 "XMLRedlineImportHelper.hxx"
      21             : #include <unotextcursor.hxx>
      22             : #include <unotextrange.hxx>
      23             : #include <unocrsr.hxx>
      24             : #include "doc.hxx"
      25             : #include <tools/datetime.hxx>
      26             : #include "poolfmt.hxx"
      27             : #include "unoredline.hxx"
      28             : #include <xmloff/xmltoken.hxx>
      29             : #include <com/sun/star/frame/XModel.hpp>
      30             : 
      31             : // for locking SolarMutex: svapp + mutex
      32             : #include <vcl/svapp.hxx>
      33             : #include <osl/mutex.hxx>
      34             : 
      35             : using namespace ::com::sun::star;
      36             : using namespace ::com::sun::star::uno;
      37             : using namespace ::xmloff::token;
      38             : 
      39             : using ::com::sun::star::frame::XModel;
      40             : using ::com::sun::star::text::XTextCursor;
      41             : using ::com::sun::star::text::XTextRange;
      42             : using ::com::sun::star::text::XText;
      43             : using ::com::sun::star::text::XWordCursor;
      44             : using ::com::sun::star::lang::XUnoTunnel;
      45             : using ::com::sun::star::beans::XPropertySet;
      46             : using ::com::sun::star::beans::XPropertySetInfo;
      47             : // collision with tools/DateTime: use UNO DateTime as util::DateTime
      48             : // using util::DateTime;
      49             : 
      50             : // a few helper functions
      51           1 : static SwDoc* lcl_GetDocViaTunnel( Reference<XTextCursor> & rCursor )
      52             : {
      53           1 :     Reference<XUnoTunnel> xTunnel( rCursor, UNO_QUERY);
      54             :     OSL_ENSURE(xTunnel.is(), "missing XUnoTunnel for XTextCursor");
      55             :     OTextCursorHelper *const pXCursor =
      56           1 :         ::sw::UnoTunnelGetImplementation<OTextCursorHelper>(xTunnel);
      57             :     OSL_ENSURE( pXCursor, "OTextCursorHelper missing" );
      58           1 :     return (pXCursor) ? pXCursor->GetDoc() : 0;
      59             : }
      60             : 
      61           3 : static SwDoc* lcl_GetDocViaTunnel( Reference<XTextRange> & rRange )
      62             : {
      63           3 :     Reference<XUnoTunnel> xTunnel(rRange, UNO_QUERY);
      64             :     OSL_ENSURE(xTunnel.is(), "missing XUnoTunnel for XTextRange");
      65             :     SwXTextRange *const pXRange =
      66           3 :         ::sw::UnoTunnelGetImplementation<SwXTextRange>(xTunnel);
      67             :     // #i115174#: this may be a SvxUnoTextRange
      68             :     // OSL_ENSURE( pXRange, "SwXTextRange missing" );
      69           3 :     return (pXRange) ? pXRange->GetDoc() : 0;
      70             : }
      71             : 
      72             : // XTextRangeOrNodeIndexPosition: store a position into the text
      73             : // *either* as an XTextRange or as an SwNodeIndex. The reason is that
      74             : // we must store either pointers to StartNodes (because redlines may
      75             : // start on start nodes) or to a text position, and there appears to
      76             : // be no existing type that could do both. Things are complicated by
      77             : // the matter that (e.g in section import) we delete a few characters,
      78             : // which may cause bookmarks (as used by XTextRange) to be deleted.
      79             : 
      80             : class XTextRangeOrNodeIndexPosition
      81             : {
      82             :     Reference<XTextRange> xRange;
      83             :     SwNodeIndex* pIndex;    // pIndex will point to the *previous* node
      84             : 
      85             : public:
      86             :     XTextRangeOrNodeIndexPosition();
      87             :     ~XTextRangeOrNodeIndexPosition();
      88             : 
      89             :     void Set( Reference<XTextRange> & rRange );
      90             :     void Set( SwNodeIndex& rIndex );
      91             :     void SetAsNodeIndex( Reference<XTextRange> & rRange );
      92             : 
      93             :     void CopyPositionInto(SwPosition& rPos, SwDoc & rDoc);
      94             :     SwDoc* GetDoc();
      95             : 
      96             :     bool IsValid();
      97             : };
      98             : 
      99           6 : XTextRangeOrNodeIndexPosition::XTextRangeOrNodeIndexPosition() :
     100             :     xRange(NULL),
     101           6 :     pIndex(NULL)
     102             : {
     103           6 : }
     104             : 
     105          12 : XTextRangeOrNodeIndexPosition::~XTextRangeOrNodeIndexPosition()
     106             : {
     107           6 :     delete pIndex;
     108           6 : }
     109             : 
     110           6 : void XTextRangeOrNodeIndexPosition::Set( Reference<XTextRange> & rRange )
     111             : {
     112           6 :     xRange = rRange->getStart();    // set bookmark
     113           6 :     if (NULL != pIndex)
     114             :     {
     115           0 :         delete pIndex;
     116           0 :         pIndex = NULL;
     117             :     }
     118           6 : }
     119             : 
     120           0 : void XTextRangeOrNodeIndexPosition::Set( SwNodeIndex& rIndex )
     121             : {
     122           0 :     if (NULL != pIndex)
     123           0 :         delete pIndex;
     124             : 
     125           0 :     pIndex = new SwNodeIndex(rIndex);
     126           0 :     (*pIndex)-- ;   // previous node!!!
     127           0 :     xRange = NULL;
     128           0 : }
     129             : 
     130           0 : void XTextRangeOrNodeIndexPosition::SetAsNodeIndex(
     131             :     Reference<XTextRange> & rRange )
     132             : {
     133             :     // XTextRange -> XTunnel -> SwXTextRange
     134           0 :     SwDoc* pDoc = lcl_GetDocViaTunnel(rRange);
     135             : 
     136           0 :     if (!pDoc)
     137             :     {
     138             :         OSL_TRACE("SetAsNodeIndex: no SwDoc");
     139           0 :         return;
     140             :     }
     141             : 
     142             :     // SwXTextRange -> PaM
     143           0 :     SwUnoInternalPaM aPaM(*pDoc);
     144             : #if OSL_DEBUG_LEVEL > 0
     145             :     sal_Bool bSuccess =
     146             : #endif
     147           0 :         ::sw::XTextRangeToSwPaM(aPaM, rRange);
     148             :     OSL_ENSURE(bSuccess, "illegal range");
     149             : 
     150             :     // PaM -> Index
     151           0 :     Set(aPaM.GetPoint()->nNode);
     152             : }
     153             : 
     154             : void
     155           6 : XTextRangeOrNodeIndexPosition::CopyPositionInto(SwPosition& rPos, SwDoc & rDoc)
     156             : {
     157             :     OSL_ENSURE(IsValid(), "Can't get Position");
     158             : 
     159             :     // create PAM from start cursor (if no node index is present)
     160           6 :     if (NULL == pIndex)
     161             :     {
     162           6 :         SwUnoInternalPaM aUnoPaM(rDoc);
     163             : #if OSL_DEBUG_LEVEL > 0
     164             :         sal_Bool bSuccess =
     165             : #endif
     166           6 :             ::sw::XTextRangeToSwPaM(aUnoPaM, xRange);
     167             :         OSL_ENSURE(bSuccess, "illegal range");
     168             : 
     169           6 :         rPos = *aUnoPaM.GetPoint();
     170             :     }
     171             :     else
     172             :     {
     173           0 :         rPos.nNode = *pIndex;
     174           0 :         rPos.nNode++;           // pIndex points to previous index !!!
     175           0 :         rPos.nContent.Assign( rPos.nNode.GetNode().GetCntntNode(), 0 );
     176             :     }
     177           6 : }
     178             : 
     179           3 : SwDoc* XTextRangeOrNodeIndexPosition::GetDoc()
     180             : {
     181             :     OSL_ENSURE(IsValid(), "Can't get Doc");
     182             : 
     183           3 :     return (NULL != pIndex) ? pIndex->GetNodes().GetDoc() : lcl_GetDocViaTunnel(xRange);
     184             : }
     185             : 
     186           9 : bool XTextRangeOrNodeIndexPosition::IsValid()
     187             : {
     188           9 :     return ( xRange.is() || (pIndex != NULL) );
     189             : }
     190             : 
     191             : // RedlineInfo: temporary storage for redline data
     192             : class RedlineInfo
     193             : {
     194             : public:
     195             :     RedlineInfo();
     196             :     ~RedlineInfo();
     197             : 
     198             :     // redline type (insert, delete, ...)
     199             :     RedlineType_t eType;
     200             : 
     201             :     // info fields:
     202             :     OUString sAuthor;               // change author string
     203             :     OUString sComment;              // change comment string
     204             :     util::DateTime aDateTime;       // change DateTime
     205             :     sal_Bool bMergeLastParagraph;   // the SwRangeRedline::IsDelLastPara flag
     206             : 
     207             :     // each position can may be either empty, an XTextRange, or an SwNodeIndex
     208             : 
     209             :     // start pos of anchor (may be empty)
     210             :     XTextRangeOrNodeIndexPosition aAnchorStart;
     211             : 
     212             :     // end pos of anchor (may be empty)
     213             :     XTextRangeOrNodeIndexPosition aAnchorEnd;
     214             : 
     215             :     // index of content node (maybe NULL)
     216             :     SwNodeIndex* pContentIndex;
     217             : 
     218             :     // next redline info (for hierarchical redlines)
     219             :     RedlineInfo* pNextRedline;
     220             : 
     221             :     // store whether we expect an adjustment for this redline
     222             :     bool bNeedsAdjustment;
     223             : };
     224             : 
     225           3 : RedlineInfo::RedlineInfo() :
     226             :     eType(nsRedlineType_t::REDLINE_INSERT),
     227             :     sAuthor(),
     228             :     sComment(),
     229             :     aDateTime(),
     230             :     bMergeLastParagraph( sal_False ),
     231             :     aAnchorStart(),
     232             :     aAnchorEnd(),
     233             :     pContentIndex(NULL),
     234             :     pNextRedline(NULL),
     235           3 :     bNeedsAdjustment( false )
     236             : {
     237           3 : }
     238             : 
     239           6 : RedlineInfo::~RedlineInfo()
     240             : {
     241           3 :     delete pContentIndex;
     242           3 :     delete pNextRedline;
     243           3 : }
     244             : 
     245             : // XMLRedlineImportHelper
     246         493 : XMLRedlineImportHelper::XMLRedlineImportHelper(
     247             :     bool bNoRedlinesPlease,
     248             :     const Reference<XPropertySet> & rModel,
     249             :     const Reference<XPropertySet> & rImportInfo ) :
     250             :         sEmpty(),
     251         493 :         sInsertion( GetXMLToken( XML_INSERTION )),
     252         493 :         sDeletion( GetXMLToken( XML_DELETION )),
     253         493 :         sFormatChange( GetXMLToken( XML_FORMAT_CHANGE )),
     254             :         sShowChanges("ShowChanges"),
     255             :         sRecordChanges("RecordChanges"),
     256             :         sRedlineProtectionKey("RedlineProtectionKey"),
     257             :         aRedlineMap(),
     258             :         bIgnoreRedlines(bNoRedlinesPlease),
     259             :         xModelPropertySet(rModel),
     260        1972 :         xImportInfoPropertySet(rImportInfo)
     261             : {
     262             :     // check to see if redline mode is handled outside of component
     263         493 :     bool bHandleShowChanges = true;
     264         493 :     bool bHandleRecordChanges = true;
     265         493 :     bool bHandleProtectionKey = true;
     266         493 :     if ( xImportInfoPropertySet.is() )
     267             :     {
     268             :         Reference<XPropertySetInfo> xInfo =
     269         456 :             xImportInfoPropertySet->getPropertySetInfo();
     270             : 
     271         456 :         bHandleShowChanges = ! xInfo->hasPropertyByName( sShowChanges );
     272         456 :         bHandleRecordChanges = ! xInfo->hasPropertyByName( sRecordChanges );
     273         456 :         bHandleProtectionKey = ! xInfo->hasPropertyByName( sRedlineProtectionKey );
     274             :     }
     275             : 
     276             :     // get redline mode
     277             :     bShowChanges = *(sal_Bool*)
     278             :         ( bHandleShowChanges ? xModelPropertySet : xImportInfoPropertySet )
     279         493 :         ->getPropertyValue( sShowChanges ).getValue();
     280             :     bRecordChanges = *(sal_Bool*)
     281             :         ( bHandleRecordChanges ? xModelPropertySet : xImportInfoPropertySet )
     282         493 :         ->getPropertyValue( sRecordChanges ).getValue();
     283             :     {
     284             :         Any aAny = (bHandleProtectionKey  ? xModelPropertySet
     285             :                                           : xImportInfoPropertySet )
     286         493 :                         ->getPropertyValue( sRedlineProtectionKey );
     287         493 :         aAny >>= aProtectionKey;
     288             :     }
     289             : 
     290             :     // set redline mode to "don't record changes"
     291         493 :     if( bHandleRecordChanges )
     292             :     {
     293          37 :         Any aAny;
     294          37 :         sal_Bool bTmp = sal_False;
     295          37 :         aAny.setValue( &bTmp, ::getBooleanCppuType() );
     296          37 :         xModelPropertySet->setPropertyValue( sRecordChanges, aAny );
     297             :     }
     298         493 : }
     299             : 
     300        1479 : XMLRedlineImportHelper::~XMLRedlineImportHelper()
     301             : {
     302             :     // delete all left over (and obviously incomplete) RedlineInfos (and map)
     303         493 :     RedlineMapType::iterator aFind = aRedlineMap.begin();
     304         493 :     for( ; aRedlineMap.end() != aFind; ++aFind )
     305             :     {
     306           0 :         RedlineInfo* pInfo = aFind->second;
     307             : 
     308             :         // left-over redlines. Insert them if possible (but assert),
     309             :         // and delete the incomplete ones. Finally, delete it.
     310           0 :         if( IsReady(pInfo) )
     311             :         {
     312             :             OSL_FAIL("forgotten RedlineInfo; now inserted");
     313           0 :             InsertIntoDocument( pInfo );
     314             :         }
     315             :         else
     316             :         {
     317             :             // try if only the adjustment was missing
     318           0 :             pInfo->bNeedsAdjustment = false;
     319           0 :             if( IsReady(pInfo) )
     320             :             {
     321             :                 OSL_FAIL("RedlineInfo without adjustment; now inserted");
     322           0 :                 InsertIntoDocument( pInfo );
     323             :             }
     324             :             else
     325             :             {
     326             :                 // this situation occurs if redlines aren't closed
     327             :                 // (i.e. end without start, or start without
     328             :                 // end). This may well be a problem in the file,
     329             :                 // rather than the code.
     330             :                 OSL_FAIL("incomplete redline (maybe file was corrupt); "
     331             :                           "now deleted");
     332             :             }
     333             :         }
     334           0 :         delete pInfo;
     335             :     }
     336         493 :     aRedlineMap.clear();
     337             : 
     338             :     // set redline mode, either to info property set, or directly to
     339             :     // the document
     340         493 :     bool bHandleShowChanges = true;
     341         493 :     bool bHandleRecordChanges = true;
     342         493 :     bool bHandleProtectionKey = true;
     343         493 :     if ( xImportInfoPropertySet.is() )
     344             :     {
     345             :         Reference<XPropertySetInfo> xInfo =
     346         456 :             xImportInfoPropertySet->getPropertySetInfo();
     347             : 
     348         456 :         bHandleShowChanges = ! xInfo->hasPropertyByName( sShowChanges );
     349         456 :         bHandleRecordChanges = ! xInfo->hasPropertyByName( sRecordChanges );
     350         456 :         bHandleProtectionKey = ! xInfo->hasPropertyByName( sRedlineProtectionKey );
     351             :     }
     352             : 
     353             :     // set redline mode & key
     354             :     try
     355             :     {
     356         493 :         Any aAny;
     357             : 
     358         493 :         aAny.setValue( &bShowChanges, ::getBooleanCppuType() );
     359         493 :         if ( bHandleShowChanges )
     360          37 :             xModelPropertySet->setPropertyValue( sShowChanges, aAny );
     361             :         else
     362         456 :             xImportInfoPropertySet->setPropertyValue( sShowChanges, aAny );
     363             : 
     364         493 :         aAny.setValue( &bRecordChanges, ::getBooleanCppuType() );
     365         493 :         if ( bHandleRecordChanges )
     366          37 :             xModelPropertySet->setPropertyValue( sRecordChanges, aAny );
     367             :         else
     368         456 :             xImportInfoPropertySet->setPropertyValue( sRecordChanges, aAny );
     369             : 
     370         493 :         aAny <<= aProtectionKey;
     371         493 :         if ( bHandleProtectionKey )
     372          37 :             xModelPropertySet->setPropertyValue( sRedlineProtectionKey, aAny );
     373             :         else
     374         456 :             xImportInfoPropertySet->setPropertyValue( sRedlineProtectionKey, aAny);
     375             :     }
     376           0 :     catch (const uno::RuntimeException &) // fdo#65882
     377             :     {
     378             :         SAL_WARN( "sw", "potentially benign ordering issue during shutdown" );
     379             :     }
     380         986 : }
     381             : 
     382           3 : void XMLRedlineImportHelper::Add(
     383             :     const OUString& rType,
     384             :     const OUString& rId,
     385             :     const OUString& rAuthor,
     386             :     const OUString& rComment,
     387             :     const util::DateTime& rDateTime,
     388             :     sal_Bool bMergeLastPara)
     389             : {
     390             :     // we need to do the following:
     391             :     // 1) parse type string
     392             :     // 2) create RedlineInfo and fill it with data
     393             :     // 3) check for existing redline with same ID
     394             :     // 3a) insert redline into map
     395             :     // 3b) attach to existing redline
     396             : 
     397             :     // ad 1)
     398             :     RedlineType_t eType;
     399           3 :     if (rType.equals(sInsertion))
     400             :     {
     401           2 :         eType = nsRedlineType_t::REDLINE_INSERT;
     402             :     }
     403           1 :     else if (rType.equals(sDeletion))
     404             :     {
     405           1 :         eType = nsRedlineType_t::REDLINE_DELETE;
     406             :     }
     407           0 :     else if (rType.equals(sFormatChange))
     408             :     {
     409           0 :         eType = nsRedlineType_t::REDLINE_FORMAT;
     410             :     }
     411             :     else
     412             :     {
     413             :         // no proper type found: early out!
     414           3 :         return;
     415             :     }
     416             : 
     417             :     // ad 2) create a new RedlineInfo
     418           3 :     RedlineInfo* pInfo = new RedlineInfo();
     419             : 
     420             :     // fill entries
     421           3 :     pInfo->eType = eType;
     422           3 :     pInfo->sAuthor = rAuthor;
     423           3 :     pInfo->sComment = rComment;
     424           3 :     pInfo->aDateTime = rDateTime;
     425           3 :     pInfo->bMergeLastParagraph = bMergeLastPara;
     426             : 
     427             :     // ad 3)
     428           3 :     if (aRedlineMap.end() == aRedlineMap.find(rId))
     429             :     {
     430             :         // 3a) insert into map
     431           3 :         aRedlineMap[rId] = pInfo;
     432             :     }
     433             :     else
     434             :     {
     435             :         // 3b) we already have a redline with this name: hierarchical redlines
     436             :         // insert pInfo as last element in the chain.
     437             :         // (hierarchy sanity checking happens on insertino into the document)
     438             : 
     439             :         // find last element
     440             :         RedlineInfo* pInfoChain;
     441           0 :         for( pInfoChain = aRedlineMap[rId];
     442           0 :             NULL != pInfoChain->pNextRedline;
     443             :             pInfoChain = pInfoChain->pNextRedline) ; // empty loop
     444             : 
     445             :         // insert as last element
     446           0 :         pInfoChain->pNextRedline = pInfo;
     447             :     }
     448             : }
     449             : 
     450           1 : Reference<XTextCursor> XMLRedlineImportHelper::CreateRedlineTextSection(
     451             :     Reference<XTextCursor> xOldCursor,
     452             :     const OUString& rId)
     453             : {
     454           1 :     Reference<XTextCursor> xReturn;
     455             : 
     456             :     // this method will modify the document directly -> lock SolarMutex
     457           2 :     SolarMutexGuard aGuard;
     458             : 
     459             :     // get RedlineInfo
     460           1 :     RedlineMapType::iterator aFind = aRedlineMap.find(rId);
     461           1 :     if (aRedlineMap.end() != aFind)
     462             :     {
     463             :         // get document from old cursor (via tunnel)
     464           1 :         SwDoc* pDoc = lcl_GetDocViaTunnel(xOldCursor);
     465             : 
     466           1 :         if (!pDoc)
     467             :         {
     468             :             OSL_TRACE("XMLRedlineImportHelper::CreateRedlineTextSection: "
     469             :                 "no SwDoc => cannot create section.");
     470           0 :             return 0;
     471             :         }
     472             : 
     473             :         // create text section for redline
     474             :         SwTxtFmtColl *pColl = pDoc->GetTxtCollFromPool
     475           1 :             (RES_POOLCOLL_STANDARD, false );
     476           1 :         SwStartNode* pRedlineNode = pDoc->GetNodes().MakeTextSection(
     477           1 :             pDoc->GetNodes().GetEndOfRedlines(),
     478             :             SwNormalStartNode,
     479           2 :             pColl);
     480             : 
     481             :         // remember node-index in RedlineInfo
     482           1 :         SwNodeIndex aIndex(*pRedlineNode);
     483           1 :         aFind->second->pContentIndex = new SwNodeIndex(aIndex);
     484             : 
     485             :         // create XText for document
     486           1 :         SwXText* pXText = new SwXRedlineText(pDoc, aIndex);
     487           2 :         Reference<XText> xText = pXText;  // keep Reference until end of method
     488             : 
     489             :         // create (UNO-) cursor
     490           2 :         SwPosition aPos(*pRedlineNode);
     491             :         SwXTextCursor *const pXCursor =
     492           1 :             new SwXTextCursor(*pDoc, pXText, CURSOR_REDLINE, aPos);
     493           1 :         pXCursor->GetCursor()->Move(fnMoveForward, fnGoNode);
     494             :         // cast to avoid ambiguity
     495           2 :         xReturn = static_cast<text::XWordCursor*>(pXCursor);
     496             :     }
     497             :     // else: unknown redline -> Ignore
     498             : 
     499           2 :     return xReturn;
     500             : }
     501             : 
     502           6 : void XMLRedlineImportHelper::SetCursor(
     503             :     const OUString& rId,
     504             :     sal_Bool bStart,
     505             :     Reference<XTextRange> & rRange,
     506             :     sal_Bool bIsOutsideOfParagraph)
     507             : {
     508           6 :     RedlineMapType::iterator aFind = aRedlineMap.find(rId);
     509           6 :     if (aRedlineMap.end() != aFind)
     510             :     {
     511             :         // RedlineInfo found; now set Cursor
     512           6 :         RedlineInfo* pInfo = aFind->second;
     513           6 :         if (bIsOutsideOfParagraph)
     514             :         {
     515             :             // outside of paragraph: remember SwNodeIndex
     516           0 :             if (bStart)
     517             :             {
     518           0 :                 pInfo->aAnchorStart.SetAsNodeIndex(rRange);
     519             :             }
     520             :             else
     521             :             {
     522           0 :                 pInfo->aAnchorEnd.SetAsNodeIndex(rRange);
     523             :             }
     524             : 
     525             :             // also remember that we expect an adjustment for this redline
     526           0 :             pInfo->bNeedsAdjustment = true;
     527             :         }
     528             :         else
     529             :         {
     530             :             // inside of a paragraph: use regular XTextRanges (bookmarks)
     531           6 :             if (bStart)
     532           3 :                 pInfo->aAnchorStart.Set(rRange);
     533             :             else
     534           3 :                 pInfo->aAnchorEnd.Set(rRange);
     535             :         }
     536             : 
     537             :         // if this Cursor was the last missing info, we insert the
     538             :         // node into the document
     539             :         // then we can remove the entry from the map and destroy the object
     540           6 :         if (IsReady(pInfo))
     541             :         {
     542           3 :             InsertIntoDocument(pInfo);
     543           3 :             aRedlineMap.erase(rId);
     544           3 :             delete pInfo;
     545             :         }
     546             :     }
     547             :     // else: unknown Id -> ignore
     548           6 : }
     549             : 
     550           0 : void XMLRedlineImportHelper::AdjustStartNodeCursor(
     551             :     const OUString& rId,        /// ID used in RedlineAdd() call
     552             :     sal_Bool /*bStart*/,
     553             :     Reference<XTextRange> & /*rRange*/)
     554             : {
     555             :     // this method will modify the document directly -> lock SolarMutex
     556           0 :     SolarMutexGuard aGuard;
     557             : 
     558             :     // start + end nodes are treated the same. For either it's
     559             :     // necessary that the target node already exists.
     560             : 
     561           0 :     RedlineMapType::iterator aFind = aRedlineMap.find(rId);
     562           0 :     if (aRedlineMap.end() != aFind)
     563             :     {
     564             :         // RedlineInfo found; now set Cursor
     565           0 :         RedlineInfo* pInfo = aFind->second;
     566             : 
     567           0 :         pInfo->bNeedsAdjustment = false;
     568             : 
     569             :         // if now ready, insert into document
     570           0 :         if( IsReady(pInfo) )
     571             :         {
     572           0 :             InsertIntoDocument(pInfo);
     573           0 :             aRedlineMap.erase(rId);
     574           0 :             delete pInfo;
     575             :         }
     576           0 :     }
     577             :     // else: can't find redline -> ignore
     578           0 : }
     579             : 
     580           6 : inline bool XMLRedlineImportHelper::IsReady(RedlineInfo* pRedline)
     581             : {
     582             :     // we can insert a redline if we have start & end, and we don't
     583             :     // expect adjustments for either of these
     584           9 :     return ( pRedline->aAnchorEnd.IsValid() &&
     585           9 :              pRedline->aAnchorStart.IsValid() &&
     586           9 :              !pRedline->bNeedsAdjustment );
     587             : }
     588             : 
     589           3 : void XMLRedlineImportHelper::InsertIntoDocument(RedlineInfo* pRedlineInfo)
     590             : {
     591             :     OSL_ENSURE(NULL != pRedlineInfo, "need redline info");
     592             :     OSL_ENSURE(IsReady(pRedlineInfo), "redline info not complete yet!");
     593             : 
     594             :     // this method will modify the document directly -> lock SolarMutex
     595           3 :     SolarMutexGuard aGuard;
     596             : 
     597             :     // Insert the Redline as described by pRedlineInfo into the
     598             :     // document.  If we are in insert mode, don't insert any redlines
     599             :     // (and delete 'deleted' inline redlines)
     600             : 
     601             :     // get the document (from one of the positions)
     602           3 :     SwDoc* pDoc = pRedlineInfo->aAnchorStart.GetDoc();
     603             : 
     604           3 :     if (!pDoc)
     605             :     {
     606             :         OSL_TRACE("XMLRedlineImportHelper::InsertIntoDocument: "
     607             :                 "no SwDoc => cannot insert redline.");
     608           3 :         return;
     609             :     }
     610             : 
     611             :     // now create the PaM for the redline
     612           6 :     SwPaM aPaM(pDoc->GetNodes().GetEndOfContent());
     613           3 :     pRedlineInfo->aAnchorStart.CopyPositionInto(*aPaM.GetPoint(), *pDoc);
     614           3 :     aPaM.SetMark();
     615           3 :     pRedlineInfo->aAnchorEnd.CopyPositionInto(*aPaM.GetPoint(), *pDoc);
     616             : 
     617             :     // collapse PaM if (start == end)
     618           3 :     if (*aPaM.GetPoint() == *aPaM.GetMark())
     619             :     {
     620           1 :         aPaM.DeleteMark();
     621             :     }
     622             : 
     623             :     // cover three cases:
     624             :     // 1) empty redlines (no range, no content)
     625             :     // 2) check for:
     626             :     //    a) bIgnoreRedline (e.g. insert mode)
     627             :     //    b) illegal PaM range (CheckNodesRange())
     628             :     // 3) normal case: insert redline
     629           3 :     if( !aPaM.HasMark() && (pRedlineInfo->pContentIndex == NULL) )
     630             :     {
     631             :         // these redlines have no function, and will thus be ignored (just as
     632             :         // in sw3io), so no action here
     633             :     }
     634           6 :     else if ( bIgnoreRedlines ||
     635           3 :          !CheckNodesRange( aPaM.GetPoint()->nNode,
     636           3 :                            aPaM.GetMark()->nNode,
     637           6 :                            true ) )
     638             :     {
     639             :         // ignore redline (e.g. file loaded in insert mode):
     640             :         // delete 'deleted' redlines and forget about the whole thing
     641           0 :         if (nsRedlineType_t::REDLINE_DELETE == pRedlineInfo->eType)
     642             :         {
     643           0 :             pDoc->DeleteRange(aPaM);
     644             :             // And what about the "deleted nodes"?
     645             :             // They have to be deleted as well (#i80689)!
     646           0 :             if( bIgnoreRedlines && pRedlineInfo->pContentIndex != NULL )
     647             :             {
     648           0 :                 SwNodeIndex aIdx( *pRedlineInfo->pContentIndex );
     649           0 :                 const SwNode* pEnd = aIdx.GetNode().EndOfSectionNode();
     650           0 :                 if( pEnd )
     651             :                 {
     652           0 :                     SwNodeIndex aEnd( *pEnd, 1 );
     653           0 :                     SwPaM aDel( aIdx, aEnd );
     654           0 :                     pDoc->DeleteRange(aDel);
     655           0 :                 }
     656             :             }
     657             :         }
     658             :     }
     659             :     else
     660             :     {
     661             :         // regular file loading: insert redline
     662             : 
     663             :         // create redline (using pRedlineData which gets copied in SwRangeRedline())
     664           3 :         SwRedlineData* pRedlineData = ConvertRedline(pRedlineInfo, pDoc);
     665             :         SwRangeRedline* pRedline =
     666             :             new SwRangeRedline( pRedlineData, *aPaM.GetPoint(), sal_True,
     667           3 :                            !pRedlineInfo->bMergeLastParagraph, sal_False );
     668             : 
     669             :         // set mark
     670           3 :         if( aPaM.HasMark() )
     671             :         {
     672           2 :             pRedline->SetMark();
     673           2 :             *(pRedline->GetMark()) = *aPaM.GetMark();
     674             :         }
     675             : 
     676             :         // set content node (if necessary)
     677           3 :         if (NULL != pRedlineInfo->pContentIndex)
     678             :         {
     679           1 :             sal_uLong nPoint = aPaM.GetPoint()->nNode.GetIndex();
     680           2 :             if( nPoint < pRedlineInfo->pContentIndex->GetIndex() ||
     681           1 :                 nPoint > pRedlineInfo->pContentIndex->GetNode().EndOfSectionIndex() )
     682           1 :                 pRedline->SetContentIdx(pRedlineInfo->pContentIndex);
     683             : #if OSL_DEBUG_LEVEL > 1
     684             :             else
     685             :                 OSL_FAIL( "Recursive change tracking" );
     686             : #endif
     687             :         }
     688             : 
     689             :         // set redline mode (without doing the associated book-keeping)
     690           3 :         pDoc->SetRedlineMode_intern(nsRedlineMode_t::REDLINE_ON);
     691           3 :         pDoc->AppendRedline(pRedline, false);
     692           3 :         pDoc->SetRedlineMode_intern(nsRedlineMode_t::REDLINE_NONE);
     693           3 :     }
     694             : }
     695             : 
     696           3 : SwRedlineData* XMLRedlineImportHelper::ConvertRedline(
     697             :     RedlineInfo* pRedlineInfo,
     698             :     SwDoc* pDoc)
     699             : {
     700             :     // convert info:
     701             :     // 1) Author String -> Author ID (default to zero)
     702             :     sal_uInt16 nAuthorId = (NULL == pDoc) ? 0 :
     703           3 :         pDoc->InsertRedlineAuthor( pRedlineInfo->sAuthor );
     704             : 
     705             :     // 2) util::DateTime -> DateTime
     706           3 :     DateTime aDT( DateTime::EMPTY );
     707           3 :     aDT.SetYear(    pRedlineInfo->aDateTime.Year );
     708           3 :     aDT.SetMonth(   pRedlineInfo->aDateTime.Month );
     709           3 :     aDT.SetDay(     pRedlineInfo->aDateTime.Day );
     710           3 :     aDT.SetHour(    pRedlineInfo->aDateTime.Hours );
     711           3 :     aDT.SetMin(     pRedlineInfo->aDateTime.Minutes );
     712           3 :     aDT.SetSec(     pRedlineInfo->aDateTime.Seconds );
     713           3 :     aDT.SetNanoSec( pRedlineInfo->aDateTime.NanoSeconds );
     714             : 
     715             :     // 3) recursively convert next redline
     716             :     //    ( check presence and sanity of hierarchical redline info )
     717           3 :     SwRedlineData* pNext = NULL;
     718           3 :     if ( (NULL != pRedlineInfo->pNextRedline) &&
     719           0 :          (nsRedlineType_t::REDLINE_DELETE == pRedlineInfo->eType) &&
     720           0 :          (nsRedlineType_t::REDLINE_INSERT == pRedlineInfo->pNextRedline->eType) )
     721             :     {
     722           0 :         pNext = ConvertRedline(pRedlineInfo->pNextRedline, pDoc);
     723             :     }
     724             : 
     725             :     // create redline data
     726             :     SwRedlineData* pData = new SwRedlineData(pRedlineInfo->eType,
     727             :                                              nAuthorId, aDT,
     728             :                                              pRedlineInfo->sComment,
     729             :                                              pNext, // next data (if available)
     730           3 :                                              NULL); // no extra data
     731             : 
     732           3 :     return pData;
     733             : }
     734             : 
     735         112 : void XMLRedlineImportHelper::SetShowChanges( sal_Bool bShow )
     736             : {
     737         112 :     bShowChanges = bShow;
     738         112 : }
     739             : 
     740           2 : void XMLRedlineImportHelper::SetRecordChanges( sal_Bool bRecord )
     741             : {
     742           2 :     bRecordChanges = bRecord;
     743           2 : }
     744             : 
     745         112 : void XMLRedlineImportHelper::SetProtectionKey(
     746             :     const Sequence<sal_Int8> & rKey )
     747             : {
     748         112 :     aProtectionKey = rKey;
     749         112 : }
     750             : 
     751             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10