Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <com/sun/star/embed/ElementModes.hpp>
31 : : #include <com/sun/star/embed/XTransactedObject.hpp>
32 : : #include <rtl/oustringostreaminserter.hxx>
33 : : #include <sot/stg.hxx>
34 : : #include <sfx2/docfile.hxx>
35 : : #include <tools/urlobj.hxx>
36 : : #include <unotools/localfilehelper.hxx>
37 : : #include <unotools/ucbstreamhelper.hxx>
38 : :
39 : : #include <comphelper/storagehelper.hxx>
40 : : #include <doc.hxx>
41 : : #include <IDocumentUndoRedo.hxx>
42 : : #include <docsh.hxx>
43 : : #include <pam.hxx>
44 : : #include <swblocks.hxx>
45 : : #include <ndtxt.hxx>
46 : : #include <shellio.hxx>
47 : : #include <poolfmt.hxx>
48 : : #include <SwXMLTextBlocks.hxx>
49 : : #include <SwXMLBlockImport.hxx>
50 : : #include <SwXMLBlockExport.hxx>
51 : : #include <swerror.h>
52 : :
53 : : #define STREAM_STGREAD ( STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE )
54 : : #define STREAM_STGWRITE ( STREAM_READ | STREAM_WRITE | STREAM_SHARE_DENYWRITE )
55 : :
56 : : using namespace ::com::sun::star;
57 : :
58 : :
59 : 162 : void SwXMLTextBlocks::InitBlockMode ( const uno::Reference < embed::XStorage >& rStorage )
60 : : {
61 : 162 : xBlkRoot = rStorage;
62 : 162 : xRoot = 0;
63 : 162 : }
64 : :
65 : 270 : void SwXMLTextBlocks::ResetBlockMode ( )
66 : : {
67 : 270 : xBlkRoot = 0;
68 : 270 : xRoot = 0;
69 : 270 : }
70 : :
71 : 118 : SwXMLTextBlocks::SwXMLTextBlocks( const String& rFile )
72 : 118 : : SwImpBlocks( rFile ), bAutocorrBlock( sal_False ), nFlags ( 0 )
73 : : {
74 [ + - ][ + - ]: 118 : SwDocShell* pDocSh = new SwDocShell ( SFX_CREATE_MODE_INTERNAL );
75 [ + - ][ + - ]: 118 : if( !pDocSh->DoInitNew( 0 ) )
76 : 118 : return;
77 : 118 : bReadOnly = sal_True;
78 : 118 : pDoc = pDocSh->GetDoc();
79 [ + - ]: 118 : xDocShellRef = pDocSh;
80 [ + - ]: 118 : pDoc->SetOle2Link( Link() );
81 [ + - ][ + - ]: 118 : pDoc->GetIDocumentUndoRedo().DoUndo(false);
82 [ + - ]: 118 : pDoc->acquire();
83 : 118 : uno::Reference< embed::XStorage > refStg;
84 [ - + ][ + + ]: 118 : if( !aDateModified.GetDate() || !aTimeModified.GetTime() )
[ + + ]
85 [ + - ]: 4 : Touch(); // falls neu angelegt -> neuen ZeitStempel besorgen
86 : : try
87 : : {
88 [ + - ][ + - ]: 118 : refStg = comphelper::OStorageHelper::GetStorageFromURL( rFile, embed::ElementModes::READWRITE );
[ + - ][ # # ]
89 : 118 : bReadOnly = sal_False;
90 : : }
91 [ # # ]: 0 : catch(const uno::Exception&)
92 : : {
93 : : //couldn't open the file - maybe it's readonly
94 : : }
95 [ - + ]: 118 : if( !refStg.is())
96 : : {
97 : : try
98 : : {
99 [ # # ][ # # ]: 0 : refStg = comphelper::OStorageHelper::GetStorageFromURL( rFile, embed::ElementModes::READ );
[ # # ][ # # ]
100 : : }
101 [ # # ]: 0 : catch(const uno::Exception&)
102 : : {
103 : : OSL_FAIL("exception while creating AutoText storage");
104 : : }
105 : : }
106 [ + - ]: 118 : InitBlockMode ( refStg );
107 [ + - ]: 118 : ReadInfo();
108 [ + - ]: 118 : ResetBlockMode ();
109 : 118 : bInfoChanged = sal_False;
110 : : }
111 : :
112 : 0 : SwXMLTextBlocks::SwXMLTextBlocks( const uno::Reference < embed::XStorage >& rStg, const String& rName )
113 : : : SwImpBlocks( rName )
114 : : , bAutocorrBlock( sal_True )
115 : 0 : , nFlags ( 0 )
116 : : {
117 [ # # ][ # # ]: 0 : SwDocShell* pDocSh = new SwDocShell ( SFX_CREATE_MODE_INTERNAL );
118 [ # # ][ # # ]: 0 : if( !pDocSh->DoInitNew( 0 ) )
119 : 0 : return;
120 : 0 : bReadOnly = sal_False;
121 : 0 : pDoc = pDocSh->GetDoc();
122 [ # # ]: 0 : xDocShellRef = pDocSh;
123 [ # # ]: 0 : pDoc->SetOle2Link( Link() );
124 [ # # ][ # # ]: 0 : pDoc->GetIDocumentUndoRedo().DoUndo(false);
125 [ # # ]: 0 : pDoc->acquire();
126 : :
127 [ # # ]: 0 : InitBlockMode ( rStg );
128 [ # # ]: 0 : ReadInfo();
129 : 0 : bInfoChanged = sal_False;
130 : : }
131 : :
132 [ + - ][ + - ]: 116 : SwXMLTextBlocks::~SwXMLTextBlocks()
133 : : {
134 [ + + ]: 116 : if ( bInfoChanged )
135 [ + - ]: 8 : WriteInfo();
136 [ + - ]: 116 : ResetBlockMode ();
137 [ + - ]: 116 : if(xDocShellRef.Is())
138 [ + - ]: 116 : xDocShellRef->DoClose();
139 [ + - ]: 116 : xDocShellRef = 0;
140 [ + - ][ + - ]: 116 : if( pDoc && !pDoc->release() )
[ + - ][ + - ]
141 [ + - ][ + - ]: 116 : delete pDoc;
142 [ - + ]: 232 : }
143 : :
144 : 22 : void SwXMLTextBlocks::ClearDoc()
145 : : {
146 : 22 : SwDocShell * pDocShell = pDoc->GetDocShell();
147 : 22 : pDocShell->InvalidateModel();
148 : 22 : pDocShell->ReactivateModel();
149 : :
150 : 22 : pDoc->ClearDoc();
151 : 22 : pDocShell->ClearEmbeddedObjects();
152 : 22 : }
153 : 14 : void SwXMLTextBlocks::AddName( const String& rShort, const String& rLong, sal_Bool bOnlyTxt )
154 : : {
155 [ + - ]: 14 : sal_uInt16 nIdx = GetIndex( rShort );
156 : 14 : SwBlockName* pNew = NULL;
157 [ - + ]: 14 : if( nIdx != (sal_uInt16) -1 )
158 : : {
159 [ # # ][ # # ]: 0 : delete aNames[nIdx];
[ # # ]
160 [ # # ][ # # ]: 0 : aNames.erase( aNames.begin() + nIdx );
161 : : }
162 : :
163 [ + - ]: 14 : aPackageName = GeneratePackageName( rShort );
164 [ + - ][ + - ]: 14 : pNew = new SwBlockName( rShort, rLong, aPackageName );
[ + - ][ + - ]
165 : :
166 : 14 : pNew->bIsOnlyTxtFlagInit = sal_True;
167 : 14 : pNew->bIsOnlyTxt = bOnlyTxt;
168 [ + - ]: 14 : aNames.insert( pNew );
169 : 14 : bInfoChanged = sal_True;
170 : 14 : }
171 : 68 : void SwXMLTextBlocks::AddName( const String& rShort, const String& rLong,
172 : : const String& rPackageName, sal_Bool bOnlyTxt )
173 : : {
174 [ + - ]: 68 : sal_uInt16 nIdx = GetIndex( rShort );
175 [ - + ]: 68 : if( nIdx != (sal_uInt16) -1 )
176 : : {
177 [ # # ][ # # ]: 0 : delete aNames[nIdx];
[ # # ]
178 [ # # ][ # # ]: 0 : aNames.erase( aNames.begin() + nIdx );
179 : : }
180 [ + - ][ + - ]: 68 : SwBlockName* pNew = new SwBlockName( rShort, rLong, rPackageName );
181 : 68 : pNew->bIsOnlyTxtFlagInit = sal_True;
182 : 68 : pNew->bIsOnlyTxt = bOnlyTxt;
183 [ + - ]: 68 : aNames.insert( pNew );
184 : 68 : bInfoChanged = sal_True;
185 : 68 : }
186 : :
187 : 10 : sal_uLong SwXMLTextBlocks::Delete( sal_uInt16 n )
188 : : {
189 [ + - ][ + - ]: 10 : String aPckName (aNames[n]->aPackageName);
190 [ + - ]: 10 : uno::Reference < container::XNameAccess > xAccess( xBlkRoot, uno::UNO_QUERY );
191 [ + - ][ + - ]: 40 : if ( xAccess.is() &&
[ - + ][ - + ]
192 [ + - ][ + - ]: 30 : xAccess->hasByName( aPckName ) && xBlkRoot->isStreamElement( aPckName ) )
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ # # # # ]
193 : : {
194 : : try
195 : : {
196 [ # # ][ # # ]: 0 : xBlkRoot->removeElement ( aPckName );
[ # # ]
197 [ # # ]: 0 : uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
198 [ # # ]: 0 : if ( xTrans.is() )
199 [ # # ][ # # ]: 0 : xTrans->commit();
200 [ # # ]: 0 : return 0;
201 : : }
202 [ # # ]: 0 : catch (const uno::Exception&)
203 : : {
204 : 0 : return ERR_SWG_WRITE_ERROR;
205 : : }
206 : : }
207 [ + - ]: 10 : return 0;
208 : : }
209 : :
210 : 2 : sal_uLong SwXMLTextBlocks::Rename( sal_uInt16 nIdx, const String& rNewShort, const String& )
211 : : {
212 : : OSL_ENSURE( xBlkRoot.is(), "No storage set" );
213 [ - + ]: 2 : if(!xBlkRoot.is())
214 : 0 : return 0;
215 [ + - ]: 2 : rtl::OUString aOldName (aNames[nIdx]->aPackageName);
216 [ + - ]: 2 : aShort = rNewShort;
217 [ + - ]: 2 : aPackageName = GeneratePackageName( aShort );
218 : :
219 [ + - ]: 2 : if(aOldName != aPackageName)
220 : : {
221 [ + - ][ + - ]: 2 : if (IsOnlyTextBlock ( nIdx ) )
222 : : {
223 : 2 : rtl::OUString sExt(".xml");
224 : 2 : rtl::OUString aOldStreamName( aOldName ); aOldStreamName += sExt;
225 : 2 : rtl::OUString aNewStreamName( aPackageName ); aNewStreamName += sExt;
226 : :
227 [ + - ][ + - ]: 2 : xRoot = xBlkRoot->openStorageElement( aOldName, embed::ElementModes::READWRITE );
[ # # ][ + - ]
228 : : try
229 : : {
230 [ + - ][ + - ]: 2 : xRoot->renameElement ( aOldStreamName, aNewStreamName );
231 : : }
232 [ # # ]: 0 : catch(const container::ElementExistException&)
233 : : {
234 : : SAL_WARN("sw", "Couldn't rename " << aOldStreamName << " to " << aNewStreamName);
235 : : }
236 [ + - ]: 2 : uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY );
237 [ + - ]: 2 : if ( xTrans.is() )
238 [ + - ][ + - ]: 2 : xTrans->commit();
239 [ + - ][ # # ]: 2 : xRoot = 0;
240 : : }
241 : :
242 : : try
243 : : {
244 [ + - ][ + - ]: 2 : xBlkRoot->renameElement ( aOldName, aPackageName );
245 : : }
246 [ # # ]: 0 : catch(const container::ElementExistException&)
247 : : {
248 : : SAL_WARN("sw", "Couldn't rename " << aOldName << " to " << aPackageName);
249 : : }
250 : : }
251 [ + - ]: 2 : uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
252 [ + - ]: 2 : if ( xTrans.is() )
253 [ + - ][ + - ]: 2 : xTrans->commit();
254 : : // No need to commit xBlkRoot here as SwTextBlocks::Rename calls
255 : : // WriteInfo which does the commit
256 : 2 : return 0;
257 : : }
258 : :
259 : 0 : sal_uLong SwXMLTextBlocks::CopyBlock( SwImpBlocks& rDestImp, String& rShort,
260 : : const String& rLong)
261 : : {
262 : 0 : sal_uLong nError = 0;
263 [ # # ]: 0 : OpenFile(sal_True);
264 [ # # ]: 0 : rDestImp.OpenFile(sal_False);
265 [ # # ]: 0 : String aGroup( rShort );
266 [ # # ]: 0 : sal_Bool bTextOnly = IsOnlyTextBlock ( rShort ) ;//pImp->pBlkRoot->IsStream( aGroup );
267 [ # # ]: 0 : sal_uInt16 nIndex = GetIndex ( rShort );
268 [ # # ][ # # ]: 0 : String sDestShortName( GetPackageName (nIndex) );
269 : 0 : sal_uInt16 nIdx = 0;
270 : :
271 : : OSL_ENSURE( xBlkRoot.is(), "No storage set" );
272 [ # # ]: 0 : if(!xBlkRoot.is())
273 : 0 : return ERR_SWG_WRITE_ERROR;
274 : :
275 [ # # ]: 0 : uno::Reference < container::XNameAccess > xAccess( ((SwXMLTextBlocks&)rDestImp).xBlkRoot, uno::UNO_QUERY );
276 [ # # ][ # # ]: 0 : while ( xAccess->hasByName( sDestShortName ) )
[ # # ][ # # ]
277 : : {
278 : 0 : ++nIdx;
279 : : //falls wirklich mal einer so verrueckt ist
280 [ # # ]: 0 : if(USHRT_MAX == nIdx)
281 : : {
282 [ # # ]: 0 : CloseFile();
283 [ # # ]: 0 : rDestImp.CloseFile();
284 : 0 : return ERR_SWG_WRITE_ERROR;
285 : : }
286 [ # # ][ # # ]: 0 : sDestShortName += String::CreateFromInt32( nIdx );
[ # # ]
287 : : }
288 : :
289 : : try
290 : : {
291 [ # # ][ # # ]: 0 : uno::Reference < embed::XStorage > rSourceRoot = xBlkRoot->openStorageElement( aGroup, embed::ElementModes::READ );
[ # # ]
292 [ # # ][ # # ]: 0 : uno::Reference < embed::XStorage > rDestRoot = ((SwXMLTextBlocks&)rDestImp).xBlkRoot->openStorageElement( sDestShortName, embed::ElementModes::READWRITE );
[ # # ]
293 [ # # ][ # # ]: 0 : rSourceRoot->copyToStorage( rDestRoot );
[ # # ]
294 : : }
295 [ # # ]: 0 : catch (const uno::Exception&)
296 : : {
297 : 0 : nError = ERR_SWG_WRITE_ERROR;
298 : : }
299 : :
300 [ # # ]: 0 : if(!nError)
301 : : {
302 [ # # ]: 0 : rShort = sDestShortName;
303 [ # # ]: 0 : ((SwXMLTextBlocks&)rDestImp).AddName( rShort, rLong, bTextOnly );
304 [ # # ]: 0 : ((SwXMLTextBlocks&)rDestImp).MakeBlockList();
305 : : }
306 [ # # ]: 0 : CloseFile();
307 [ # # ]: 0 : rDestImp.CloseFile();
308 [ # # ][ # # ]: 0 : return nError;
309 : : }
310 : :
311 : :
312 : 2 : sal_uLong SwXMLTextBlocks::StartPutBlock( const String& rShort, const String& rPackageName )
313 : : {
314 : : OSL_ENSURE( xBlkRoot.is(), "No storage set" );
315 [ - + ]: 2 : if(!xBlkRoot.is())
316 : 0 : return 0;
317 : 2 : GetIndex ( rShort );
318 : : try
319 : : {
320 [ + - ][ + - ]: 2 : xRoot = xBlkRoot->openStorageElement( rPackageName, embed::ElementModes::READWRITE );
[ + - ][ + - ]
321 : :
322 [ + - ]: 2 : uno::Reference< beans::XPropertySet > xRootProps( xRoot, uno::UNO_QUERY_THROW );
323 [ + - ]: 2 : ::rtl::OUString aPropName( RTL_CONSTASCII_USTRINGPARAM("MediaType") );
324 [ + - ][ + - ]: 2 : ::rtl::OUString aMime( SotExchange::GetFormatMimeType( SOT_FORMATSTR_ID_STARWRITER_8 ) );
[ + - ]
325 [ + - ][ + - ]: 2 : xRootProps->setPropertyValue( aPropName, uno::makeAny( aMime ) );
[ + - ][ # # ]
326 : : }
327 : 0 : catch (const uno::Exception&)
328 : : {
329 : : }
330 : 2 : return 0;
331 : : }
332 : 2 : sal_uLong SwXMLTextBlocks::BeginPutDoc( const String& rShort, const String& rLong )
333 : : {
334 : : // In der Basisklasse ablegen!
335 : 2 : aShort = rShort;
336 : 2 : aLong = rLong;
337 : 2 : aPackageName = GeneratePackageName( rShort );
338 : 2 : SetIsTextOnly( rShort, sal_False);
339 [ + - ]: 2 : return StartPutBlock (rShort, aPackageName);
340 : : }
341 : :
342 : 2 : sal_uLong SwXMLTextBlocks::PutBlock( SwPaM& , const String& )
343 : : {
344 : 2 : sal_uLong nRes = 0;
345 : 2 : sal_uInt16 nCommitFlags = nFlags & (SWXML_CONVBLOCK|SWXML_NOROOTCOMMIT);
346 : :
347 : 2 : nFlags |= nCommitFlags;
348 : :
349 : 2 : WriterRef xWrt;
350 [ + - ]: 2 : ::GetXMLWriter ( aEmptyStr, GetBaseURL(), xWrt);
351 [ + - ]: 2 : SwWriter aWriter (xRoot, *pDoc );
352 : :
353 : 2 : xWrt->bBlock = sal_True;
354 [ + - ]: 2 : nRes = aWriter.Write ( xWrt );
355 : 2 : xWrt->bBlock = sal_False;
356 : : // Save OLE objects if there are some
357 : 2 : SwDocShell *pDocSh = pDoc->GetDocShell();
358 : :
359 [ + - ][ + - ]: 2 : sal_Bool bHasChildren = pDocSh && pDocSh->GetEmbeddedObjectContainer().HasEmbeddedObjects();
[ - + ][ + - ]
360 [ + - ][ - + ]: 2 : if( !nRes && bHasChildren )
361 : : {
362 : : // we have to write to the temporary storage first, since the used below functions are optimized
363 : : // TODO/LATER: it is only a temporary solution, that should be changed soon, the used methods should be
364 : : // called without optimization
365 : :
366 : 0 : sal_Bool bOK = sal_False;
367 : :
368 [ # # ]: 0 : if ( xRoot.is() )
369 : : {
370 : 0 : SfxMedium* pTmpMedium = NULL;
371 : : try
372 : : {
373 : : uno::Reference< embed::XStorage > xTempStorage =
374 [ # # ]: 0 : ::comphelper::OStorageHelper::GetTemporaryStorage();
375 : :
376 [ # # ][ # # ]: 0 : xRoot->copyToStorage( xTempStorage );
377 : :
378 : : // TODO/LATER: no progress bar?!
379 : : // TODO/MBA: strange construct
380 [ # # ][ # # ]: 0 : pTmpMedium = new SfxMedium( xTempStorage, GetBaseURL() );
381 [ # # ]: 0 : sal_Bool bTmpOK = pDocSh->SaveAsChildren( *pTmpMedium );
382 [ # # ]: 0 : if( bTmpOK )
383 [ # # ]: 0 : bTmpOK = pDocSh->SaveCompletedChildren( sal_False );
384 : :
385 [ # # ][ # # ]: 0 : xTempStorage->copyToStorage( xRoot );
386 [ # # ]: 0 : bOK = bTmpOK;
387 : : }
388 [ # # ]: 0 : catch(const uno::Exception&)
389 : : {
390 : : }
391 : :
392 [ # # ]: 0 : if ( pTmpMedium )
393 [ # # ][ # # ]: 0 : DELETEZ( pTmpMedium );
394 : : }
395 : :
396 [ # # ]: 0 : if( !bOK )
397 : 0 : nRes = ERR_SWG_WRITE_ERROR;
398 : : }
399 : :
400 : : try
401 : : {
402 [ + - ]: 2 : uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY );
403 [ + - ]: 2 : if ( xTrans.is() )
404 [ + - ][ + - ]: 2 : xTrans->commit();
405 [ + - ]: 2 : xRoot = 0;
406 [ + - ]: 2 : if ( !nCommitFlags )
407 : : {
408 [ + - ]: 2 : uno::Reference < embed::XTransactedObject > xTmpTrans( xBlkRoot, uno::UNO_QUERY );
409 [ + - ]: 2 : if ( xTmpTrans.is() )
410 [ + - ][ + - ]: 2 : xTmpTrans->commit();
411 [ # # ]: 2 : }
412 : : }
413 [ # # ]: 0 : catch (const uno::Exception&)
414 : : {
415 : : }
416 : :
417 : : //TODO/LATER: error handling
418 [ + - ][ + - ]: 2 : return 0;
419 : : }
420 : :
421 : 2 : sal_uLong SwXMLTextBlocks::PutDoc()
422 : : {
423 : 2 : SwPaM* pPaM = MakePaM();
424 : 2 : sal_uLong nErr = PutBlock(*pPaM, aLong);
425 [ + - ]: 2 : delete pPaM;
426 : 2 : return nErr;
427 : : }
428 : :
429 : 0 : sal_uLong SwXMLTextBlocks::GetText( sal_uInt16 nIdx, String& rText )
430 : : {
431 : 0 : return GetBlockText( aNames[ nIdx ]->aShort, rText );
432 : : }
433 : :
434 : 0 : sal_uLong SwXMLTextBlocks::GetText( const String& rShort, String& rText )
435 : : {
436 : 0 : return GetBlockText( rShort, rText );
437 : : }
438 : :
439 : :
440 : 26 : sal_uLong SwXMLTextBlocks::MakeBlockList()
441 : : {
442 : 26 : WriteInfo();
443 : 26 : return 0;
444 : : }
445 : :
446 : 0 : sal_Bool SwXMLTextBlocks::PutMuchEntries( sal_Bool bOn )
447 : : {
448 : 0 : sal_Bool bRet = sal_False;
449 [ # # ]: 0 : if( bOn )
450 : : {
451 [ # # ]: 0 : if( bInPutMuchBlocks )
452 : : {
453 : : OSL_ENSURE( !this, "verschachtelte Aufrufe sind nicht erlaubt" );
454 : : }
455 [ # # ]: 0 : else if( !IsFileChanged() )
456 : : {
457 : 0 : bRet = 0 == OpenFile( sal_False );
458 [ # # ]: 0 : if( bRet )
459 : : {
460 : 0 : nFlags |= SWXML_NOROOTCOMMIT;
461 : 0 : bInPutMuchBlocks = sal_True;
462 : : }
463 : : }
464 : : }
465 [ # # ]: 0 : else if( bInPutMuchBlocks )
466 : : {
467 : 0 : nFlags &= ~SWXML_NOROOTCOMMIT;
468 [ # # ]: 0 : if( xBlkRoot.is() )
469 : : {
470 : : try
471 : : {
472 [ # # ]: 0 : uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
473 [ # # ]: 0 : if ( xTrans.is() )
474 [ # # ][ # # ]: 0 : xTrans->commit();
475 [ # # ]: 0 : MakeBlockList();
476 [ # # ]: 0 : CloseFile();
477 [ # # ]: 0 : Touch();
478 : 0 : bInPutMuchBlocks = sal_False;
479 [ # # ]: 0 : bRet = sal_True;
480 : : }
481 : 0 : catch (const uno::Exception&)
482 : : {
483 : : }
484 : : }
485 : : }
486 : 0 : return bRet;
487 : : }
488 : :
489 : 44 : sal_uLong SwXMLTextBlocks::OpenFile( sal_Bool bRdOnly )
490 : : {
491 [ - + ]: 44 : if( bAutocorrBlock )
492 : 0 : return 0;
493 : 44 : sal_uLong nRet = 0;
494 : : try
495 : : {
496 : : uno::Reference < embed::XStorage > refStg = comphelper::OStorageHelper::GetStorageFromURL( aFile,
497 [ + + ][ + - ]: 44 : bRdOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE );
[ + - ]
498 [ # # ][ + - ]: 44 : InitBlockMode ( refStg );
499 : : }
500 : 0 : catch (const uno::Exception&)
501 : : {
502 : : //TODO/LATER: error handling
503 : 0 : nRet = 1;
504 : : }
505 : :
506 : 44 : return nRet;
507 : : }
508 : :
509 : 36 : void SwXMLTextBlocks::CloseFile()
510 : : {
511 [ + - ]: 36 : if ( !bAutocorrBlock )
512 : : {
513 [ - + ]: 36 : if (bInfoChanged)
514 : 0 : WriteInfo();
515 : 36 : ResetBlockMode();
516 : : }
517 : 36 : }
518 : :
519 : 14 : void SwXMLTextBlocks::SetIsTextOnly( const String& rShort, sal_Bool bNewValue )
520 : : {
521 : 14 : sal_uInt16 nIdx = GetIndex ( rShort );
522 [ + - ][ + + ]: 14 : if (nIdx != (sal_uInt16) -1 && nIdx != USHRT_MAX)
523 : 2 : aNames[nIdx]->bIsOnlyTxt = bNewValue;
524 : 14 : }
525 : :
526 : 0 : void SwXMLTextBlocks::SetIsTextOnly( sal_uInt16 nIdx, sal_Bool bNewValue )
527 : : {
528 : 0 : aNames[nIdx]->bIsOnlyTxt = bNewValue;
529 : 0 : }
530 : :
531 : 0 : sal_Bool SwXMLTextBlocks::IsOnlyTextBlock( const String& rShort ) const
532 : : {
533 : 0 : sal_uInt16 nIdx = GetIndex ( rShort );
534 : 0 : sal_Bool bRet = sal_False;
535 [ # # ][ # # ]: 0 : if (nIdx != (sal_uInt16) -1 && nIdx != USHRT_MAX)
536 : : {
537 : 0 : bRet = aNames[nIdx]->bIsOnlyTxt;
538 : : }
539 : 0 : return bRet;
540 : : }
541 : 30 : sal_Bool SwXMLTextBlocks::IsOnlyTextBlock( sal_uInt16 nIdx ) const
542 : : {
543 : 30 : return aNames[nIdx]->bIsOnlyTxt;
544 : : }
545 : :
546 : 114 : sal_Bool SwXMLTextBlocks::IsFileUCBStorage( const String & rFileName)
547 : : {
548 [ + - ]: 114 : rtl::OUString aName( rFileName );
549 [ + - ]: 114 : INetURLObject aObj( aName );
550 [ - + ]: 114 : if ( aObj.GetProtocol() == INET_PROT_NOT_VALID )
551 : : {
552 : 0 : rtl::OUString aURL;
553 [ # # ]: 0 : ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aURL );
554 [ # # ]: 0 : aObj.SetURL( aURL );
555 [ # # ]: 0 : aName = aObj.GetMainURL( INetURLObject::NO_DECODE );
556 : : }
557 : :
558 [ + - ][ + - ]: 114 : SvStream * pStm = ::utl::UcbStreamHelper::CreateStream( aName, STREAM_STD_READ );
[ + - ]
559 [ + - ]: 114 : sal_Bool bRet = UCBStorage::IsStorageFile( pStm );
560 [ + - ][ + - ]: 114 : delete pStm;
561 [ + - ]: 114 : return bRet;
562 : : }
563 : :
564 : :
565 : :
566 : 0 : short SwXMLTextBlocks::GetFileType ( void ) const
567 : : {
568 : 0 : return SWBLK_XML;
569 : : }
570 : :
571 : 30 : rtl::OUString SwXMLTextBlocks::GeneratePackageName ( const String& rShort )
572 : : {
573 [ + - ]: 30 : String aRet = rShort;
574 : 30 : xub_StrLen nPos = 0;
575 : 30 : sal_Unicode pDelims[] = { '!', '/', ':', '.', '\\', 0 };
576 [ + - ][ + - ]: 30 : rtl::OString sByte(rtl::OUStringToOString(aRet, RTL_TEXTENCODING_UTF7));
577 [ + - ][ + - ]: 30 : aRet = rtl::OStringToOUString(sByte, RTL_TEXTENCODING_ASCII_US);
578 [ + - ][ - + ]: 30 : while( STRING_NOTFOUND != ( nPos = aRet.SearchChar( pDelims, nPos )))
579 : : {
580 [ # # ]: 0 : aRet.SetChar( nPos, '_' );
581 : 0 : ++nPos;
582 : : }
583 [ + - ][ + - ]: 30 : return aRet;
584 : : }
585 : :
586 : 12 : sal_uLong SwXMLTextBlocks::PutText( const String& rShort, const String& rName,
587 : : const String& rText )
588 : : {
589 : 12 : sal_uLong nRes = 0;
590 : 12 : aShort = rShort;
591 : 12 : aLong = rName;
592 : 12 : aCur = rText;
593 : 12 : SetIsTextOnly( aShort, sal_True );
594 : 12 : aPackageName = GeneratePackageName( rShort );
595 : 12 : ClearDoc();
596 [ + - ]: 12 : nRes = PutBlockText( rShort, rName, rText, aPackageName );
597 : 12 : return nRes;
598 : : }
599 : :
600 : 20 : void SwXMLTextBlocks::MakeBlockText( const String& rText )
601 : : {
602 [ + - ][ + - ]: 40 : SwTxtNode* pTxtNode = pDoc->GetNodes()[ pDoc->GetNodes().GetEndOfContent().
603 [ + - ]: 40 : GetIndex() - 1 ]->GetTxtNode();
604 [ - + ]: 20 : if( pTxtNode->GetTxtColl() == pDoc->GetDfltTxtFmtColl() )
605 [ # # ][ # # ]: 0 : pTxtNode->ChgFmtColl( pDoc->GetTxtCollFromPool( RES_POOLCOLL_STANDARD ));
606 : :
607 : 20 : xub_StrLen nPos = 0;
608 [ + + ]: 36 : do
609 : : {
610 [ + + ]: 36 : if ( nPos )
611 : : {
612 [ + - ][ + - ]: 16 : pTxtNode = (SwTxtNode*)pTxtNode->AppendNode( SwPosition( *pTxtNode ) );
[ + - ]
613 : : }
614 [ + - ][ + - ]: 36 : SwIndex aIdx( pTxtNode );
615 [ + - ]: 36 : String sTemp(rText.GetToken( 0, '\015', nPos ) );
616 [ + - ][ + - ]: 36 : pTxtNode->InsertText( sTemp, aIdx );
[ + - ]
617 : : } while ( STRING_NOTFOUND != nPos );
618 : 20 : }
619 : :
620 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|