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 0 : static SwDoc* lcl_GetDocViaTunnel( Reference<XTextCursor> & rCursor )
52 : {
53 0 : Reference<XUnoTunnel> xTunnel( rCursor, UNO_QUERY);
54 : OSL_ENSURE(xTunnel.is(), "missing XUnoTunnel for XTextCursor");
55 : OTextCursorHelper *const pXCursor =
56 0 : ::sw::UnoTunnelGetImplementation<OTextCursorHelper>(xTunnel);
57 : OSL_ENSURE( pXCursor, "OTextCursorHelper missing" );
58 0 : return (pXCursor) ? pXCursor->GetDoc() : 0;
59 : }
60 :
61 0 : static SwDoc* lcl_GetDocViaTunnel( Reference<XTextRange> & rRange )
62 : {
63 0 : Reference<XUnoTunnel> xTunnel(rRange, UNO_QUERY);
64 : OSL_ENSURE(xTunnel.is(), "missing XUnoTunnel for XTextRange");
65 : SwXTextRange *const pXRange =
66 0 : ::sw::UnoTunnelGetImplementation<SwXTextRange>(xTunnel);
67 : // #i115174#: this may be a SvxUnoTextRange
68 : // OSL_ENSURE( pXRange, "SwXTextRange missing" );
69 0 : 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 0 : XTextRangeOrNodeIndexPosition::XTextRangeOrNodeIndexPosition() :
100 : xRange(NULL),
101 0 : pIndex(NULL)
102 : {
103 0 : }
104 :
105 0 : XTextRangeOrNodeIndexPosition::~XTextRangeOrNodeIndexPosition()
106 : {
107 0 : delete pIndex;
108 0 : }
109 :
110 0 : void XTextRangeOrNodeIndexPosition::Set( Reference<XTextRange> & rRange )
111 : {
112 0 : xRange = rRange->getStart(); // set bookmark
113 0 : if (NULL != pIndex)
114 : {
115 0 : delete pIndex;
116 0 : pIndex = NULL;
117 : }
118 0 : }
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 0 : 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 0 : if (NULL == pIndex)
161 : {
162 0 : SwUnoInternalPaM aUnoPaM(rDoc);
163 : #if OSL_DEBUG_LEVEL > 0
164 : sal_Bool bSuccess =
165 : #endif
166 0 : ::sw::XTextRangeToSwPaM(aUnoPaM, xRange);
167 : OSL_ENSURE(bSuccess, "illegal range");
168 :
169 0 : 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 0 : }
178 :
179 0 : SwDoc* XTextRangeOrNodeIndexPosition::GetDoc()
180 : {
181 : OSL_ENSURE(IsValid(), "Can't get Doc");
182 :
183 0 : return (NULL != pIndex) ? pIndex->GetNodes().GetDoc() : lcl_GetDocViaTunnel(xRange);
184 : }
185 :
186 0 : bool XTextRangeOrNodeIndexPosition::IsValid()
187 : {
188 0 : 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 0 : 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 0 : bNeedsAdjustment( false )
236 : {
237 0 : }
238 :
239 0 : RedlineInfo::~RedlineInfo()
240 : {
241 0 : delete pContentIndex;
242 0 : delete pNextRedline;
243 0 : }
244 :
245 : // XMLRedlineImportHelper
246 0 : XMLRedlineImportHelper::XMLRedlineImportHelper(
247 : bool bNoRedlinesPlease,
248 : const Reference<XPropertySet> & rModel,
249 : const Reference<XPropertySet> & rImportInfo ) :
250 : sEmpty(),
251 0 : sInsertion( GetXMLToken( XML_INSERTION )),
252 0 : sDeletion( GetXMLToken( XML_DELETION )),
253 0 : sFormatChange( GetXMLToken( XML_FORMAT_CHANGE )),
254 : sShowChanges("ShowChanges"),
255 : sRecordChanges("RecordChanges"),
256 : sRedlineProtectionKey("RedlineProtectionKey"),
257 : aRedlineMap(),
258 : bIgnoreRedlines(bNoRedlinesPlease),
259 : xModelPropertySet(rModel),
260 0 : xImportInfoPropertySet(rImportInfo)
261 : {
262 : // check to see if redline mode is handled outside of component
263 0 : bool bHandleShowChanges = true;
264 0 : bool bHandleRecordChanges = true;
265 0 : bool bHandleProtectionKey = true;
266 0 : if ( xImportInfoPropertySet.is() )
267 : {
268 : Reference<XPropertySetInfo> xInfo =
269 0 : xImportInfoPropertySet->getPropertySetInfo();
270 :
271 0 : bHandleShowChanges = ! xInfo->hasPropertyByName( sShowChanges );
272 0 : bHandleRecordChanges = ! xInfo->hasPropertyByName( sRecordChanges );
273 0 : bHandleProtectionKey = ! xInfo->hasPropertyByName( sRedlineProtectionKey );
274 : }
275 :
276 : // get redline mode
277 : bShowChanges = *(sal_Bool*)
278 : ( bHandleShowChanges ? xModelPropertySet : xImportInfoPropertySet )
279 0 : ->getPropertyValue( sShowChanges ).getValue();
280 : bRecordChanges = *(sal_Bool*)
281 : ( bHandleRecordChanges ? xModelPropertySet : xImportInfoPropertySet )
282 0 : ->getPropertyValue( sRecordChanges ).getValue();
283 : {
284 : Any aAny = (bHandleProtectionKey ? xModelPropertySet
285 : : xImportInfoPropertySet )
286 0 : ->getPropertyValue( sRedlineProtectionKey );
287 0 : aAny >>= aProtectionKey;
288 : }
289 :
290 : // set redline mode to "don't record changes"
291 0 : if( bHandleRecordChanges )
292 : {
293 0 : Any aAny;
294 0 : sal_Bool bTmp = sal_False;
295 0 : aAny.setValue( &bTmp, ::getBooleanCppuType() );
296 0 : xModelPropertySet->setPropertyValue( sRecordChanges, aAny );
297 : }
298 0 : }
299 :
300 0 : XMLRedlineImportHelper::~XMLRedlineImportHelper()
301 : {
302 : // delete all left over (and obviously incomplete) RedlineInfos (and map)
303 0 : RedlineMapType::iterator aFind = aRedlineMap.begin();
304 0 : 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 0 : aRedlineMap.clear();
337 :
338 : // set redline mode, either to info property set, or directly to
339 : // the document
340 0 : bool bHandleShowChanges = true;
341 0 : bool bHandleRecordChanges = true;
342 0 : bool bHandleProtectionKey = true;
343 0 : if ( xImportInfoPropertySet.is() )
344 : {
345 : Reference<XPropertySetInfo> xInfo =
346 0 : xImportInfoPropertySet->getPropertySetInfo();
347 :
348 0 : bHandleShowChanges = ! xInfo->hasPropertyByName( sShowChanges );
349 0 : bHandleRecordChanges = ! xInfo->hasPropertyByName( sRecordChanges );
350 0 : bHandleProtectionKey = ! xInfo->hasPropertyByName( sRedlineProtectionKey );
351 : }
352 :
353 : // set redline mode & key
354 : try
355 : {
356 0 : Any aAny;
357 :
358 0 : aAny.setValue( &bShowChanges, ::getBooleanCppuType() );
359 0 : if ( bHandleShowChanges )
360 0 : xModelPropertySet->setPropertyValue( sShowChanges, aAny );
361 : else
362 0 : xImportInfoPropertySet->setPropertyValue( sShowChanges, aAny );
363 :
364 0 : aAny.setValue( &bRecordChanges, ::getBooleanCppuType() );
365 0 : if ( bHandleRecordChanges )
366 0 : xModelPropertySet->setPropertyValue( sRecordChanges, aAny );
367 : else
368 0 : xImportInfoPropertySet->setPropertyValue( sRecordChanges, aAny );
369 :
370 0 : aAny <<= aProtectionKey;
371 0 : if ( bHandleProtectionKey )
372 0 : xModelPropertySet->setPropertyValue( sRedlineProtectionKey, aAny );
373 : else
374 0 : 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 0 : }
381 :
382 0 : 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 0 : if (rType.equals(sInsertion))
400 : {
401 0 : eType = nsRedlineType_t::REDLINE_INSERT;
402 : }
403 0 : else if (rType.equals(sDeletion))
404 : {
405 0 : 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 0 : return;
415 : }
416 :
417 : // ad 2) create a new RedlineInfo
418 0 : RedlineInfo* pInfo = new RedlineInfo();
419 :
420 : // fill entries
421 0 : pInfo->eType = eType;
422 0 : pInfo->sAuthor = rAuthor;
423 0 : pInfo->sComment = rComment;
424 0 : pInfo->aDateTime = rDateTime;
425 0 : pInfo->bMergeLastParagraph = bMergeLastPara;
426 :
427 : // ad 3)
428 0 : if (aRedlineMap.end() == aRedlineMap.find(rId))
429 : {
430 : // 3a) insert into map
431 0 : 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 0 : Reference<XTextCursor> XMLRedlineImportHelper::CreateRedlineTextSection(
451 : Reference<XTextCursor> xOldCursor,
452 : const OUString& rId)
453 : {
454 0 : Reference<XTextCursor> xReturn;
455 :
456 : // this method will modify the document directly -> lock SolarMutex
457 0 : SolarMutexGuard aGuard;
458 :
459 : // get RedlineInfo
460 0 : RedlineMapType::iterator aFind = aRedlineMap.find(rId);
461 0 : if (aRedlineMap.end() != aFind)
462 : {
463 : // get document from old cursor (via tunnel)
464 0 : SwDoc* pDoc = lcl_GetDocViaTunnel(xOldCursor);
465 :
466 0 : 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 0 : (RES_POOLCOLL_STANDARD, false );
476 0 : SwStartNode* pRedlineNode = pDoc->GetNodes().MakeTextSection(
477 0 : pDoc->GetNodes().GetEndOfRedlines(),
478 : SwNormalStartNode,
479 0 : pColl);
480 :
481 : // remember node-index in RedlineInfo
482 0 : SwNodeIndex aIndex(*pRedlineNode);
483 0 : aFind->second->pContentIndex = new SwNodeIndex(aIndex);
484 :
485 : // create XText for document
486 0 : SwXText* pXText = new SwXRedlineText(pDoc, aIndex);
487 0 : Reference<XText> xText = pXText; // keep Reference until end of method
488 :
489 : // create (UNO-) cursor
490 0 : SwPosition aPos(*pRedlineNode);
491 : SwXTextCursor *const pXCursor =
492 0 : new SwXTextCursor(*pDoc, pXText, CURSOR_REDLINE, aPos);
493 0 : pXCursor->GetCursor()->Move(fnMoveForward, fnGoNode);
494 : // cast to avoid ambiguity
495 0 : xReturn = static_cast<text::XWordCursor*>(pXCursor);
496 : }
497 : // else: unknown redline -> Ignore
498 :
499 0 : return xReturn;
500 : }
501 :
502 0 : void XMLRedlineImportHelper::SetCursor(
503 : const OUString& rId,
504 : sal_Bool bStart,
505 : Reference<XTextRange> & rRange,
506 : sal_Bool bIsOutsideOfParagraph)
507 : {
508 0 : RedlineMapType::iterator aFind = aRedlineMap.find(rId);
509 0 : if (aRedlineMap.end() != aFind)
510 : {
511 : // RedlineInfo found; now set Cursor
512 0 : RedlineInfo* pInfo = aFind->second;
513 0 : 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 0 : if (bStart)
532 0 : pInfo->aAnchorStart.Set(rRange);
533 : else
534 0 : 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 0 : if (IsReady(pInfo))
541 : {
542 0 : InsertIntoDocument(pInfo);
543 0 : aRedlineMap.erase(rId);
544 0 : delete pInfo;
545 : }
546 : }
547 : // else: unknown Id -> ignore
548 0 : }
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 0 : 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 0 : return ( pRedline->aAnchorEnd.IsValid() &&
585 0 : pRedline->aAnchorStart.IsValid() &&
586 0 : !pRedline->bNeedsAdjustment );
587 : }
588 :
589 0 : 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 0 : 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 0 : SwDoc* pDoc = pRedlineInfo->aAnchorStart.GetDoc();
603 :
604 0 : if (!pDoc)
605 : {
606 : OSL_TRACE("XMLRedlineImportHelper::InsertIntoDocument: "
607 : "no SwDoc => cannot insert redline.");
608 0 : return;
609 : }
610 :
611 : // now create the PaM for the redline
612 0 : SwPaM aPaM(pDoc->GetNodes().GetEndOfContent());
613 0 : pRedlineInfo->aAnchorStart.CopyPositionInto(*aPaM.GetPoint(), *pDoc);
614 0 : aPaM.SetMark();
615 0 : pRedlineInfo->aAnchorEnd.CopyPositionInto(*aPaM.GetPoint(), *pDoc);
616 :
617 : // collapse PaM if (start == end)
618 0 : if (*aPaM.GetPoint() == *aPaM.GetMark())
619 : {
620 0 : 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 0 : 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 0 : else if ( bIgnoreRedlines ||
635 0 : !CheckNodesRange( aPaM.GetPoint()->nNode,
636 0 : aPaM.GetMark()->nNode,
637 0 : 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 0 : SwRedlineData* pRedlineData = ConvertRedline(pRedlineInfo, pDoc);
665 : SwRangeRedline* pRedline =
666 : new SwRangeRedline( pRedlineData, *aPaM.GetPoint(), sal_True,
667 0 : !pRedlineInfo->bMergeLastParagraph, sal_False );
668 :
669 : // set mark
670 0 : if( aPaM.HasMark() )
671 : {
672 0 : pRedline->SetMark();
673 0 : *(pRedline->GetMark()) = *aPaM.GetMark();
674 : }
675 :
676 : // set content node (if necessary)
677 0 : if (NULL != pRedlineInfo->pContentIndex)
678 : {
679 0 : sal_uLong nPoint = aPaM.GetPoint()->nNode.GetIndex();
680 0 : if( nPoint < pRedlineInfo->pContentIndex->GetIndex() ||
681 0 : nPoint > pRedlineInfo->pContentIndex->GetNode().EndOfSectionIndex() )
682 0 : 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 0 : pDoc->SetRedlineMode_intern(nsRedlineMode_t::REDLINE_ON);
691 0 : pDoc->AppendRedline(pRedline, false);
692 0 : pDoc->SetRedlineMode_intern(nsRedlineMode_t::REDLINE_NONE);
693 0 : }
694 : }
695 :
696 0 : 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 0 : pDoc->InsertRedlineAuthor( pRedlineInfo->sAuthor );
704 :
705 : // 2) util::DateTime -> DateTime
706 0 : DateTime aDT( DateTime::EMPTY );
707 0 : aDT.SetYear( pRedlineInfo->aDateTime.Year );
708 0 : aDT.SetMonth( pRedlineInfo->aDateTime.Month );
709 0 : aDT.SetDay( pRedlineInfo->aDateTime.Day );
710 0 : aDT.SetHour( pRedlineInfo->aDateTime.Hours );
711 0 : aDT.SetMin( pRedlineInfo->aDateTime.Minutes );
712 0 : aDT.SetSec( pRedlineInfo->aDateTime.Seconds );
713 0 : aDT.SetNanoSec( pRedlineInfo->aDateTime.NanoSeconds );
714 :
715 : // 3) recursively convert next redline
716 : // ( check presence and sanity of hierarchical redline info )
717 0 : SwRedlineData* pNext = NULL;
718 0 : 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 0 : NULL); // no extra data
731 :
732 0 : return pData;
733 : }
734 :
735 0 : void XMLRedlineImportHelper::SetShowChanges( sal_Bool bShow )
736 : {
737 0 : bShowChanges = bShow;
738 0 : }
739 :
740 0 : void XMLRedlineImportHelper::SetRecordChanges( sal_Bool bRecord )
741 : {
742 0 : bRecordChanges = bRecord;
743 0 : }
744 :
745 0 : void XMLRedlineImportHelper::SetProtectionKey(
746 : const Sequence<sal_Int8> & rKey )
747 : {
748 0 : aProtectionKey = rKey;
749 0 : }
750 :
751 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|