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 <hintids.hxx>
21 : #include <unotools/tempfile.hxx>
22 : #include <svl/urihelper.hxx>
23 : #include <svl/stritem.hxx>
24 : #include <svl/eitem.hxx>
25 : #include <sfx2/app.hxx>
26 : #include <sfx2/docfile.hxx>
27 : #include <sfx2/docfilt.hxx>
28 : #include <sfx2/fcontnr.hxx>
29 : #include <sfx2/bindings.hxx>
30 : #include <sfx2/request.hxx>
31 : #include <fmtinfmt.hxx>
32 : #include <fmtanchr.hxx>
33 : #include <doc.hxx>
34 : #include <IDocumentUndoRedo.hxx>
35 : #include <docary.hxx>
36 : #include <pam.hxx>
37 : #include <ndtxt.hxx>
38 : #include <docsh.hxx>
39 : #include <globdoc.hxx>
40 : #include <shellio.hxx>
41 : #include <swundo.hxx> // for the UndoIds
42 : #include <section.hxx>
43 : #include <doctxm.hxx>
44 : #include <poolfmt.hxx>
45 : #include <switerator.hxx>
46 : #include <com/sun/star/uno/Reference.h>
47 : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
48 : #include <com/sun/star/document/XDocumentProperties.hpp>
49 :
50 : using namespace ::com::sun::star;
51 :
52 : enum SwSplitDocType
53 : {
54 : SPLITDOC_TO_GLOBALDOC,
55 : SPLITDOC_TO_HTML
56 : };
57 :
58 0 : sal_Bool SwDoc::GenerateGlobalDoc( const String& rPath,
59 : const SwTxtFmtColl* pSplitColl )
60 : {
61 0 : return SplitDoc( SPLITDOC_TO_GLOBALDOC, rPath, false, pSplitColl, 0 );
62 : }
63 :
64 0 : sal_Bool SwDoc::GenerateGlobalDoc( const String& rPath, int nOutlineLevel )
65 : {
66 0 : return SplitDoc( SPLITDOC_TO_GLOBALDOC, rPath, true, 0, nOutlineLevel );
67 : }
68 :
69 0 : sal_Bool SwDoc::GenerateHTMLDoc( const String& rPath, int nOutlineLevel )
70 : {
71 0 : return SplitDoc( SPLITDOC_TO_HTML, rPath, true, 0, nOutlineLevel );
72 : }
73 :
74 0 : sal_Bool SwDoc::GenerateHTMLDoc( const String& rPath,
75 : const SwTxtFmtColl* pSplitColl )
76 : {
77 0 : return SplitDoc( SPLITDOC_TO_HTML, rPath, false, pSplitColl, 0 );
78 : }
79 :
80 : // two helpers for outline mode
81 0 : SwNodePtr GetStartNode( SwOutlineNodes* pOutlNds, int nOutlineLevel, sal_uInt16* nOutl )
82 : {
83 : SwNodePtr pNd;
84 :
85 0 : for( ; *nOutl < pOutlNds->size(); ++(*nOutl) )
86 0 : if( ( pNd = (*pOutlNds)[ *nOutl ])->GetTxtNode()->GetAttrOutlineLevel() == nOutlineLevel && !pNd->FindTableNode() )
87 : {
88 0 : return pNd;
89 : }
90 :
91 0 : return 0;
92 : }
93 :
94 0 : SwNodePtr GetEndNode( SwOutlineNodes* pOutlNds, int nOutlineLevel, sal_uInt16* nOutl )
95 : {
96 : SwNodePtr pNd;
97 :
98 0 : for( ++(*nOutl); (*nOutl) < pOutlNds->size(); ++(*nOutl) )
99 : {
100 0 : pNd = (*pOutlNds)[ *nOutl ];
101 :
102 0 : const int nLevel = pNd->GetTxtNode()->GetAttrOutlineLevel();
103 :
104 0 : if( ( 0 < nLevel && nLevel <= nOutlineLevel ) &&
105 0 : !pNd->FindTableNode() )
106 : {
107 0 : return pNd;
108 : }
109 : }
110 0 : return 0;
111 : }
112 :
113 : // two helpers for collection mode
114 0 : SwNodePtr GetStartNode( const SwOutlineNodes* pOutlNds, const SwTxtFmtColl* pSplitColl, sal_uInt16* nOutl )
115 : {
116 : SwNodePtr pNd;
117 0 : for( ; *nOutl < pOutlNds->size(); ++(*nOutl) )
118 0 : if( ( pNd = (*pOutlNds)[ *nOutl ])->GetTxtNode()->
119 0 : GetTxtColl() == pSplitColl &&
120 0 : !pNd->FindTableNode() )
121 : {
122 0 : return pNd;
123 : }
124 0 : return 0;
125 : }
126 :
127 0 : SwNodePtr GetEndNode( const SwOutlineNodes* pOutlNds, const SwTxtFmtColl* pSplitColl, sal_uInt16* nOutl )
128 : {
129 : SwNodePtr pNd;
130 :
131 0 : for( ++(*nOutl); *nOutl < pOutlNds->size(); ++(*nOutl) )
132 : {
133 0 : pNd = (*pOutlNds)[ *nOutl ];
134 0 : SwTxtFmtColl* pTColl = pNd->GetTxtNode()->GetTxtColl();
135 :
136 0 : if( ( pTColl == pSplitColl ||
137 0 : ( pSplitColl->GetAttrOutlineLevel() > 0 &&
138 0 : pTColl->GetAttrOutlineLevel() > 0 &&
139 0 : pTColl->GetAttrOutlineLevel() <
140 0 : pSplitColl->GetAttrOutlineLevel() )) &&
141 0 : !pNd->FindTableNode() )
142 : {
143 0 : return pNd;
144 : }
145 : }
146 0 : return 0;
147 : }
148 :
149 0 : bool SwDoc::SplitDoc( sal_uInt16 eDocType, const String& rPath, bool bOutline, const SwTxtFmtColl* pSplitColl, int nOutlineLevel )
150 : {
151 : // Iterate over all the template's Nodes, creating an own
152 : // document for every single one and replace linked sections (GlobalDoc) for links (HTML).
153 : // Finally, we save this document as a GlobalDoc/HTMLDoc.
154 0 : if( !pDocShell || !pDocShell->GetMedium() ||
155 0 : ( SPLITDOC_TO_GLOBALDOC == eDocType && get(IDocumentSettingAccess::GLOBAL_DOCUMENT) ) )
156 0 : return false;
157 :
158 0 : sal_uInt16 nOutl = 0;
159 0 : SwOutlineNodes* pOutlNds = (SwOutlineNodes*)&GetNodes().GetOutLineNds();
160 : SwNodePtr pStartNd;
161 :
162 0 : if ( !bOutline) {
163 0 : if( pSplitColl )
164 : {
165 : // If it isn't a OutlineNumbering, then use an own array and collect the Nodes.
166 0 : if( pSplitColl->GetAttrOutlineLevel() == 0 )//<-end,zhaojianwei, 0814
167 : {
168 0 : pOutlNds = new SwOutlineNodes;
169 0 : SwIterator<SwTxtNode,SwFmtColl> aIter( *pSplitColl );
170 0 : for( SwTxtNode* pTNd = aIter.First(); pTNd; pTNd = aIter.Next() )
171 0 : if( pTNd->GetNodes().IsDocNodes() )
172 0 : pOutlNds->insert( pTNd );
173 :
174 0 : if( pOutlNds->empty() )
175 : {
176 0 : delete pOutlNds;
177 0 : return false;
178 0 : }
179 : }
180 : }
181 : else
182 : {
183 : // Look for the 1st level OutlineTemplate
184 0 : const SwTxtFmtColls& rFmtColls =*GetTxtFmtColls();
185 0 : for( sal_uInt16 n = rFmtColls.size(); n; )
186 0 : if ( rFmtColls[ --n ]->GetAttrOutlineLevel() == 1 )//<-end,zhaojianwei
187 : {
188 0 : pSplitColl = rFmtColls[ n ];
189 0 : break;
190 : }
191 :
192 0 : if( !pSplitColl )
193 0 : return false;
194 : }
195 : }
196 :
197 : const SfxFilter* pFilter;
198 0 : switch( eDocType )
199 : {
200 : case SPLITDOC_TO_HTML:
201 0 : pFilter = SwIoSystem::GetFilterOfFormat(rtl::OUString("HTML"));
202 0 : break;
203 :
204 : default:
205 0 : pFilter = SwIoSystem::GetFilterOfFormat(rtl::OUString(FILTER_XML));
206 0 : eDocType = SPLITDOC_TO_GLOBALDOC;
207 0 : break;
208 : }
209 :
210 0 : if( !pFilter )
211 0 : return false;
212 :
213 : // Deactivate Undo/Redline in any case
214 0 : GetIDocumentUndoRedo().DoUndo(false);
215 0 : SetRedlineMode_intern( (RedlineMode_t)(GetRedlineMode() & ~nsRedlineMode_t::REDLINE_ON));
216 :
217 0 : String sExt( pFilter->GetSuffixes().GetToken(0, ',') );
218 0 : if( !sExt.Len() )
219 0 : sExt.AssignAscii( "sxw" );
220 0 : if( '.' != sExt.GetChar( 0 ) )
221 0 : sExt.Insert( '.', 0 );
222 :
223 0 : INetURLObject aEntry(rPath);
224 0 : String sLeading(aEntry.GetBase());
225 0 : aEntry.removeSegment();
226 0 : String sPath = aEntry.GetMainURL( INetURLObject::NO_DECODE );
227 0 : utl::TempFile aTemp(sLeading,&sExt,&sPath );
228 0 : aTemp.EnableKillingFile();
229 :
230 0 : DateTime aTmplDate( DateTime::SYSTEM );
231 : {
232 0 : Time a2Min( 0 ); a2Min.SetMin( 2 );
233 0 : aTmplDate += a2Min;
234 : }
235 :
236 :
237 : // Skip all invalid ones
238 0 : while( nOutl < pOutlNds->size() &&
239 0 : (*pOutlNds)[ nOutl ]->GetIndex() < GetNodes().GetEndOfExtras().GetIndex() )
240 0 : ++nOutl;
241 :
242 0 : do {
243 0 : if( bOutline )
244 0 : pStartNd = GetStartNode( pOutlNds, nOutlineLevel, &nOutl );
245 : else
246 0 : pStartNd = GetStartNode( pOutlNds, pSplitColl, &nOutl );
247 :
248 0 : if( pStartNd )
249 : {
250 : SwNodePtr pEndNd;
251 0 : if( bOutline )
252 0 : pEndNd = GetEndNode( pOutlNds, nOutlineLevel, &nOutl );
253 : else
254 0 : pEndNd = GetEndNode( pOutlNds, pSplitColl, &nOutl );
255 : SwNodeIndex aEndIdx( pEndNd ? *pEndNd
256 0 : : GetNodes().GetEndOfContent() );
257 :
258 : // Write out the Nodes completely
259 0 : String sFileName;
260 0 : if( pStartNd->GetIndex() + 1 < aEndIdx.GetIndex() )
261 : {
262 0 : SfxObjectShellLock xDocSh( new SwDocShell( SFX_CREATE_MODE_INTERNAL ));
263 0 : if( xDocSh->DoInitNew( 0 ) )
264 : {
265 0 : SwDoc* pDoc = ((SwDocShell*)(&xDocSh))->GetDoc();
266 :
267 : uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
268 0 : ((SwDocShell*)(&xDocSh))->GetModel(),
269 0 : uno::UNO_QUERY_THROW);
270 : uno::Reference<document::XDocumentProperties> xDocProps(
271 0 : xDPS->getDocumentProperties());
272 : OSL_ENSURE(xDocProps.is(), "Doc has no DocumentProperties");
273 : // the GlobalDoc is the template
274 0 : xDocProps->setTemplateName(aEmptyStr);
275 0 : ::util::DateTime uDT(aTmplDate.Get100Sec(),
276 0 : aTmplDate.GetSec(), aTmplDate.GetMin(),
277 0 : aTmplDate.GetHour(), aTmplDate.GetDay(),
278 0 : aTmplDate.GetMonth(), aTmplDate.GetYear());
279 0 : xDocProps->setTemplateDate(uDT);
280 0 : xDocProps->setTemplateURL(rPath);
281 : // Set the new doc's title to the text of the "split para".
282 : // If the current doc has a title, insert it at the begin.
283 0 : String sTitle( xDocProps->getTitle() );
284 0 : if( sTitle.Len() )
285 0 : sTitle.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ": " ));
286 0 : sTitle += ((SwTxtNode*)pStartNd)->GetExpandTxt();
287 0 : xDocProps->setTitle( sTitle );
288 :
289 : // Replace template
290 0 : pDoc->ReplaceStyles( *this );
291 :
292 : // Take over chapter numbering
293 0 : if( pOutlineRule )
294 0 : pDoc->SetOutlineNumRule( *pOutlineRule );
295 :
296 0 : SwNodeRange aRg( *pStartNd, 0, aEndIdx.GetNode() );
297 0 : SwNodeIndex aTmpIdx( pDoc->GetNodes().GetEndOfContent() );
298 0 : GetNodes()._Copy( aRg, aTmpIdx, sal_False );
299 :
300 : // Delete the initial TextNode
301 0 : SwNodeIndex aIdx( pDoc->GetNodes().GetEndOfExtras(), 2 );
302 0 : if( aIdx.GetIndex() + 1 !=
303 0 : pDoc->GetNodes().GetEndOfContent().GetIndex() )
304 0 : pDoc->GetNodes().Delete( aIdx, 1 );
305 :
306 : // All Flys in the section
307 0 : CopyFlyInFlyImpl( aRg, 0, aIdx );
308 :
309 :
310 : // And what's with all the Bookmarks?
311 : // ?????
312 :
313 0 : utl::TempFile aTempFile2(sLeading,&sExt,&sPath );
314 0 : sFileName = aTempFile2.GetURL();
315 : SfxMedium* pTmpMed = new SfxMedium( sFileName,
316 0 : STREAM_STD_READWRITE );
317 0 : pTmpMed->SetFilter( pFilter );
318 :
319 : // We need to have a Layout for the HTMLFilter, so that
320 : // TextFrames/Controls/OLE objects can be exported correctly as graphics.
321 0 : if( SPLITDOC_TO_HTML == eDocType &&
322 0 : !pDoc->GetSpzFrmFmts()->empty() )
323 : {
324 0 : SfxViewFrame::LoadHiddenDocument( *xDocSh, 0 );
325 : }
326 0 : xDocSh->DoSaveAs( *pTmpMed );
327 0 : xDocSh->DoSaveCompleted( pTmpMed );
328 :
329 : // do not insert a FileLinkSection in case of error
330 0 : if( xDocSh->GetError() )
331 0 : sFileName.Erase();
332 : }
333 0 : xDocSh->DoClose();
334 : }
335 :
336 : // We can now insert the section
337 0 : if( sFileName.Len() )
338 : {
339 0 : switch( eDocType )
340 : {
341 : case SPLITDOC_TO_HTML:
342 : {
343 : // Delete all nodes in the section and, in the "start node",
344 : // set the Link to the saved document.
345 0 : sal_uLong nNodeDiff = aEndIdx.GetIndex() -
346 0 : pStartNd->GetIndex() - 1;
347 0 : if( nNodeDiff )
348 : {
349 0 : SwPaM aTmp( *pStartNd, aEndIdx.GetNode(), 1, -1 );
350 0 : aTmp.GetPoint()->nContent.Assign( 0, 0 );
351 0 : aTmp.GetMark()->nContent.Assign( 0, 0 );
352 0 : SwNodeIndex aSIdx( aTmp.GetMark()->nNode );
353 0 : SwNodeIndex aEIdx( aTmp.GetPoint()->nNode );
354 :
355 : // Try to move past the end
356 0 : if( !aTmp.Move( fnMoveForward, fnGoNode ) )
357 : {
358 : // well then, back to the beginning
359 0 : aTmp.Exchange();
360 0 : if( !aTmp.Move( fnMoveBackward, fnGoNode ))
361 : {
362 : OSL_FAIL( "no more Nodes!" );
363 : }
364 : }
365 : // Move Bookmarks and so forth
366 0 : CorrAbs( aSIdx, aEIdx, *aTmp.GetPoint(), sal_True);
367 :
368 : // If FlyFrames are still around, delete these too
369 0 : for( sal_uInt16 n = 0; n < GetSpzFrmFmts()->size(); ++n )
370 : {
371 0 : SwFrmFmt* pFly = (*GetSpzFrmFmts())[n];
372 0 : const SwFmtAnchor* pAnchor = &pFly->GetAnchor();
373 : SwPosition const*const pAPos =
374 0 : pAnchor->GetCntntAnchor();
375 0 : if (pAPos &&
376 0 : ((FLY_AT_PARA == pAnchor->GetAnchorId()) ||
377 0 : (FLY_AT_CHAR == pAnchor->GetAnchorId())) &&
378 0 : aSIdx <= pAPos->nNode &&
379 0 : pAPos->nNode < aEIdx )
380 : {
381 0 : DelLayoutFmt( pFly );
382 0 : --n;
383 : }
384 : }
385 :
386 0 : GetNodes().Delete( aSIdx, nNodeDiff );
387 : }
388 :
389 : // set the link in the StartNode
390 0 : SwFmtINetFmt aINet( sFileName , aEmptyStr );
391 0 : SwTxtNode* pTNd = (SwTxtNode*)pStartNd;
392 0 : pTNd->InsertItem( aINet, 0, pTNd->GetTxt().Len() );
393 :
394 : // If the link cannot be found anymore,
395 : // it has to be a bug!
396 0 : if( !pOutlNds->Seek_Entry( pStartNd, &nOutl ))
397 0 : pStartNd = 0;
398 0 : ++nOutl;
399 : }
400 0 : break;
401 :
402 : default:
403 : {
404 0 : String sNm( INetURLObject( sFileName ).GetName() );
405 : SwSectionData aSectData( FILE_LINK_SECTION,
406 0 : GetUniqueSectionName( &sNm ));
407 0 : SwSectionFmt* pFmt = MakeSectionFmt( 0 );
408 0 : aSectData.SetLinkFileName(sFileName);
409 0 : aSectData.SetProtectFlag(true);
410 :
411 0 : aEndIdx--; // in the InsertSection the end is inclusive
412 0 : while( aEndIdx.GetNode().IsStartNode() )
413 0 : aEndIdx--;
414 :
415 : // If any Section ends or starts in the new sectionrange,
416 : // they must end or start before or after the range!
417 0 : SwSectionNode* pSectNd = pStartNd->FindSectionNode();
418 0 : while( pSectNd && pSectNd->EndOfSectionIndex()
419 0 : <= aEndIdx.GetIndex() )
420 : {
421 0 : const SwNode* pSectEnd = pSectNd->EndOfSectionNode();
422 0 : if( pSectNd->GetIndex() + 1 ==
423 0 : pStartNd->GetIndex() )
424 : {
425 0 : bool bMvIdx = aEndIdx == *pSectEnd;
426 0 : DelSectionFmt( pSectNd->GetSection().GetFmt() );
427 0 : if( bMvIdx )
428 0 : aEndIdx--;
429 : }
430 : else
431 : {
432 0 : SwNodeRange aRg( *pStartNd, *pSectEnd );
433 0 : SwNodeIndex aIdx( *pSectEnd, 1 );
434 0 : GetNodes()._MoveNodes( aRg, GetNodes(), aIdx );
435 : }
436 0 : pSectNd = pStartNd->FindSectionNode();
437 : }
438 :
439 0 : pSectNd = aEndIdx.GetNode().FindSectionNode();
440 0 : while( pSectNd && pSectNd->GetIndex() >
441 0 : pStartNd->GetIndex() )
442 : {
443 : // #i15712# don't attempt to split sections if
444 : // they are fully enclosed in [pSectNd,aEndIdx].
445 0 : if( aEndIdx < pSectNd->EndOfSectionIndex() )
446 : {
447 0 : SwNodeRange aRg( *pSectNd, 1, aEndIdx, 1 );
448 0 : SwNodeIndex aIdx( *pSectNd );
449 0 : GetNodes()._MoveNodes( aRg, GetNodes(), aIdx );
450 : }
451 :
452 0 : pSectNd = pStartNd->FindSectionNode();
453 : }
454 :
455 : // -> #i26762#
456 : // Ensure order of start and end of section is sane.
457 0 : SwNodeIndex aStartIdx(*pStartNd);
458 :
459 0 : if (aEndIdx >= aStartIdx)
460 : {
461 0 : pSectNd = GetNodes().InsertTextSection(aStartIdx,
462 0 : *pFmt, aSectData, 0, &aEndIdx, false);
463 : }
464 : else
465 : {
466 0 : pSectNd = GetNodes().InsertTextSection(aEndIdx,
467 0 : *pFmt, aSectData, 0, &aStartIdx, false);
468 : }
469 : // <- #i26762#
470 :
471 0 : pSectNd->GetSection().CreateLink( CREATE_CONNECT );
472 : }
473 0 : break;
474 : }
475 0 : }
476 : }
477 : } while( pStartNd );
478 :
479 0 : if( pOutlNds != &GetNodes().GetOutLineNds() )
480 0 : delete pOutlNds;
481 :
482 0 : switch( eDocType )
483 : {
484 : case SPLITDOC_TO_HTML:
485 0 : if( get(IDocumentSettingAccess::GLOBAL_DOCUMENT) )
486 : {
487 : // save all remaining sections
488 0 : while( !GetSections().empty() )
489 0 : DelSectionFmt( GetSections().front() );
490 :
491 0 : SfxFilterContainer* pFCntnr = pDocShell->GetFactory().GetFilterContainer();
492 0 : pFilter = pFCntnr->GetFilter4EA( pFilter->GetTypeName(), SFX_FILTER_EXPORT );
493 : }
494 0 : break;
495 :
496 : default:
497 : // save the Globaldoc
498 0 : set(IDocumentSettingAccess::GLOBAL_DOCUMENT, true);
499 0 : set(IDocumentSettingAccess::GLOBAL_DOCUMENT_SAVE_LINKS, false);
500 : }
501 :
502 : // The medium isn't locked after reopening the document.
503 0 : SfxRequest aReq( SID_SAVEASDOC, SFX_CALLMODE_SYNCHRON, GetAttrPool() );
504 0 : aReq.AppendItem( SfxStringItem( SID_FILE_NAME, rPath ) );
505 0 : aReq.AppendItem( SfxBoolItem( SID_SAVETO, sal_True ) );
506 0 : if(pFilter)
507 0 : aReq.AppendItem( SfxStringItem( SID_FILTER_NAME, pFilter->GetName() ) );
508 0 : const SfxBoolItem *pRet = (const SfxBoolItem*)pDocShell->ExecuteSlot( aReq );
509 :
510 0 : return pRet && pRet->GetValue();
511 : }
512 :
513 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|