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

Generated by: LCOV version 1.11