bump product version to 5.0.4.1
[LibreOffice.git] / sot / source / sdstor / storage.cxx
blobfaa6fcd5d78734f96f5fc36a5b2dc944680d2437
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/uno/Sequence.hxx>
21 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
22 #include <com/sun/star/embed/XStorage.hpp>
23 #include <com/sun/star/embed/ElementModes.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <rtl/digest.h>
27 #include <osl/file.hxx>
28 #include <sot/stg.hxx>
29 #include <sot/storinfo.hxx>
30 #include <sot/storage.hxx>
31 #include <sot/formats.hxx>
32 #include <sot/exchange.hxx>
33 #include <unotools/ucbstreamhelper.hxx>
34 #include <tools/debug.hxx>
35 #include <tools/urlobj.hxx>
36 #include <unotools/localfilehelper.hxx>
37 #include <unotools/ucbhelper.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <boost/scoped_array.hpp>
40 #include <boost/scoped_ptr.hpp>
42 using namespace ::com::sun::star;
44 /************** class SotStorageStream ***********************************/
45 class SotStorageStreamFactory : public SotFactory
47 public:
48 TYPEINFO_OVERRIDE();
49 SotStorageStreamFactory( const SvGlobalName & rName,
50 const OUString & rClassName,
51 CreateInstanceType pCreateFuncP )
52 : SotFactory( rName, rClassName, pCreateFuncP )
55 TYPEINIT1(SotStorageStreamFactory,SotFactory);
58 SO2_IMPL_BASIC_CLASS1_DLL(SotStorageStream,SotStorageStreamFactory,SotObject,
59 SvGlobalName( 0xd7deb420, 0xf902, 0x11d0,
60 0xaa, 0xa1, 0x0, 0xa0, 0x24, 0x9d, 0x55, 0x90 ) )
62 SvLockBytesRef MakeLockBytes_Impl( const OUString & rName, StreamMode nMode )
64 SvLockBytesRef xLB;
65 if( !rName.isEmpty() )
67 SvStream * pFileStm = new SvFileStream( rName, nMode );
68 xLB = new SvLockBytes( pFileStm, true );
70 else
72 SvStream * pCacheStm = new SvMemoryStream();
73 xLB = new SvLockBytes( pCacheStm, true );
75 return xLB;
78 SotStorageStream::SotStorageStream( const OUString & rName, StreamMode nMode )
79 : SvStream( MakeLockBytes_Impl( rName, nMode ) )
80 , pOwnStm( NULL )
82 if( nMode & StreamMode::WRITE )
83 bIsWritable = true;
84 else
85 bIsWritable = false;
88 SotStorageStream::SotStorageStream( BaseStorageStream * pStm )
90 if( pStm )
92 if( StreamMode::WRITE & pStm->GetMode() )
93 bIsWritable = true;
94 else
95 bIsWritable = false;
97 pOwnStm = pStm;
98 SetError( pStm->GetError() );
99 pStm->ResetError();
101 else
103 pOwnStm = NULL;
104 bIsWritable = true;
105 SetError( SVSTREAM_INVALID_PARAMETER );
109 SotStorageStream::SotStorageStream()
110 : pOwnStm( NULL )
112 // ??? wenn Init virtuell ist, entsprechen setzen
113 bIsWritable = true;
116 SotStorageStream::~SotStorageStream()
118 Flush(); //SetBufferSize(0);
119 delete pOwnStm;
122 void SotStorageStream::ResetError()
124 SvStream::ResetError();
125 if( pOwnStm )
126 pOwnStm->ResetError();
129 sal_uLong SotStorageStream::GetData( void* pData, sal_uLong nSize )
131 sal_uLong nRet = 0;
133 if( pOwnStm )
135 nRet = pOwnStm->Read( pData, nSize );
136 SetError( pOwnStm->GetError() );
138 else
139 nRet = SvStream::GetData( pData, nSize );
141 return nRet;
144 sal_uLong SotStorageStream::PutData( const void* pData, sal_uLong nSize )
146 sal_uLong nRet = 0;
148 if( pOwnStm )
150 nRet = pOwnStm->Write( pData, nSize );
151 SetError( pOwnStm->GetError() );
153 else
154 nRet = SvStream::PutData( pData, nSize );
155 return nRet;
158 sal_uInt64 SotStorageStream::SeekPos(sal_uInt64 nPos)
160 sal_uLong nRet = 0;
162 if( pOwnStm )
164 nRet = pOwnStm->Seek( nPos );
165 SetError( pOwnStm->GetError() );
167 else
168 nRet = SvStream::SeekPos( nPos );
170 return nRet;
173 void SotStorageStream::FlushData()
175 if( pOwnStm )
177 pOwnStm->Flush();
178 SetError( pOwnStm->GetError() );
180 else
181 SvStream::FlushData();
184 void SotStorageStream::SetSize(sal_uInt64 const nNewSize)
186 sal_uInt64 const nPos = Tell();
187 if( pOwnStm )
189 pOwnStm->SetSize( nNewSize );
190 SetError( pOwnStm->GetError() );
192 else
193 SvStream::SetSize( nNewSize );
195 if( nNewSize < nPos )
196 // ans Ende setzen
197 Seek( nNewSize );
200 sal_uInt32 SotStorageStream::GetSize() const
202 sal_uLong nPos = Tell();
203 const_cast<SotStorageStream *>(this)->Seek( STREAM_SEEK_TO_END );
204 sal_uLong nSize = Tell();
205 const_cast<SotStorageStream *>(this)->Seek( nPos );
206 return nSize;
209 sal_uInt64 SotStorageStream::remainingSize()
211 if (pOwnStm)
212 return pOwnStm->GetSize() - Tell();
214 return SvStream::remainingSize();
217 bool SotStorageStream::CopyTo( SotStorageStream * pDestStm )
219 Flush(); // alle Daten schreiben
220 pDestStm->ClearBuffer();
221 if( !pOwnStm || !pDestStm->pOwnStm )
223 // Wenn Ole2 oder nicht nur eigene StorageStreams
224 sal_uLong nPos = Tell(); // Position merken
225 Seek( 0L );
226 pDestStm->SetSize( 0 ); // Ziel-Stream leeren
228 boost::scoped_array<sal_uInt8> pMem(new sal_uInt8[ 8192 ]);
229 sal_uLong nRead;
230 while( 0 != (nRead = Read( pMem.get(), 8192 )) )
232 if( nRead != pDestStm->Write( pMem.get(), nRead ) )
234 SetError( SVSTREAM_GENERALERROR );
235 break;
238 pMem.reset();
239 // Position setzen
240 pDestStm->Seek( nPos );
241 Seek( nPos );
243 else
245 pOwnStm->CopyTo( pDestStm->pOwnStm );
246 SetError( pOwnStm->GetError() );
248 return GetError() == SVSTREAM_OK;
251 bool SotStorageStream::Commit()
253 if( pOwnStm )
255 pOwnStm->Flush();
256 if( pOwnStm->GetError() == SVSTREAM_OK )
257 pOwnStm->Commit();
258 SetError( pOwnStm->GetError() );
260 return GetError() == SVSTREAM_OK;
263 bool SotStorageStream::SetProperty( const OUString& rName, const ::com::sun::star::uno::Any& rValue )
265 UCBStorageStream* pStg = PTR_CAST( UCBStorageStream, pOwnStm );
266 if ( pStg )
268 return pStg->SetProperty( rName, rValue );
270 else
272 OSL_FAIL("Not implemented!");
273 return false;
277 /************** class SotStorage ******************************************
278 *************************************************************************/
279 class SotStorageFactory : public SotFactory
281 public:
282 TYPEINFO_OVERRIDE();
283 SotStorageFactory( const SvGlobalName & rName,
284 const OUString & rClassName,
285 CreateInstanceType pCreateFuncP )
286 : SotFactory( rName, rClassName, pCreateFuncP )
289 TYPEINIT1(SotStorageFactory,SotFactory);
292 SO2_IMPL_BASIC_CLASS1_DLL(SotStorage,SotStorageFactory,SotObject,
293 SvGlobalName( 0x980ce7e0, 0xf905, 0x11d0,
294 0xaa, 0xa1, 0x0, 0xa0, 0x24, 0x9d, 0x55, 0x90 ) )
296 /************************************************************************
298 |* SotStorage::SotStorage()
300 |* Beschreibung Es muss ein I... Objekt an SvObject uebergeben
301 |* werden, da es sonst selbst ein IUnknown anlegt und
302 |* festlegt, dass alle weiteren I... Objekte mit
303 |* delete zerstoert werden (Owner() == true).
304 |* Es werden aber nur IStorage Objekte benutzt und nicht
305 |* selbst implementiert, deshalb wird so getan, als ob
306 |* das IStorage Objekt von aussen kam und es wird mit
307 |* Release() freigegeben.
308 |* Die CreateStorage Methoden werden benoetigt, um
309 |* ein IStorage Objekt vor dem Aufruf von SvObject
310 |* zu erzeugen (Own, !Own automatik).
311 |* Hat CreateStorage ein Objekt erzeugt, dann wurde
312 |* der RefCounter schon um 1 erhoet.
313 |* Die Uebergabe erfolgt in pStorageCTor. Die Variable
314 |* ist NULL, wenn es nicht geklappt hat.
316 *************************************************************************/
317 #define INIT_SotStorage() \
318 : m_pOwnStg( NULL ) \
319 , m_pStorStm( NULL ) \
320 , m_nError( SVSTREAM_OK ) \
321 , m_bIsRoot( false ) \
322 , m_bDelStm( false ) \
323 , m_nVersion( SOFFICE_FILEFORMAT_CURRENT )
325 SotStorage::SotStorage()
326 INIT_SotStorage()
328 // ??? What's this ???
331 #define ERASEMASK ( StreamMode::TRUNC | StreamMode::WRITE | StreamMode::SHARE_DENYALL )
332 #include <com/sun/star/uno/Reference.h>
333 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
334 #include <ucbhelper/content.hxx>
336 SotStorage::SotStorage( const OUString & rName, StreamMode nMode, bool transacted )
337 INIT_SotStorage()
339 m_aName = rName; // Namen merken
340 CreateStorage( true, nMode, transacted );
341 if ( IsOLEStorage() )
342 m_nVersion = SOFFICE_FILEFORMAT_50;
345 void SotStorage::CreateStorage( bool bForceUCBStorage, StreamMode nMode, bool transacted )
347 DBG_ASSERT( !m_pStorStm && !m_pOwnStg, "Use only in ctor!" );
348 if( !m_aName.isEmpty() )
350 // named storage
351 if( ( ( nMode & ERASEMASK ) == ERASEMASK ) )
352 ::utl::UCBContentHelper::Kill( m_aName );
354 INetURLObject aObj( m_aName );
355 if ( aObj.GetProtocol() == INetProtocol::NotValid )
357 OUString aURL;
358 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( m_aName, aURL );
359 aObj.SetURL( aURL );
360 m_aName = aObj.GetMainURL( INetURLObject::NO_DECODE );
363 // check the stream
364 m_pStorStm = ::utl::UcbStreamHelper::CreateStream( m_aName, nMode );
365 if ( m_pStorStm && m_pStorStm->GetError() )
366 DELETEZ( m_pStorStm );
368 if ( m_pStorStm )
370 // try as UCBStorage, next try as OLEStorage
371 bool bIsUCBStorage = UCBStorage::IsStorageFile( m_pStorStm );
372 if ( !bIsUCBStorage && bForceUCBStorage )
373 // if UCBStorage has priority, it should not be used only if it is really an OLEStorage
374 bIsUCBStorage = !Storage::IsStorageFile( m_pStorStm );
376 if ( bIsUCBStorage )
378 if ( !(UCBStorage::GetLinkedFile( *m_pStorStm ).isEmpty()) )
380 // detect special unpacked storages
381 m_pOwnStg = new UCBStorage( *m_pStorStm, !transacted );
382 m_bDelStm = true;
384 else
386 // UCBStorage always works directly on the UCB content, so discard the stream first
387 DELETEZ( m_pStorStm );
388 m_pOwnStg = new UCBStorage( m_aName, nMode, !transacted );
391 else
393 // OLEStorage can be opened with a stream
394 m_pOwnStg = new Storage( *m_pStorStm, !transacted );
395 m_bDelStm = true;
398 else if ( bForceUCBStorage )
400 m_pOwnStg = new UCBStorage( m_aName, nMode, !transacted );
401 SetError( ERRCODE_IO_NOTSUPPORTED );
403 else
405 m_pOwnStg = new Storage( m_aName, nMode, !transacted );
406 SetError( ERRCODE_IO_NOTSUPPORTED );
409 else
411 // temporary storage
412 if ( bForceUCBStorage )
413 m_pOwnStg = new UCBStorage( m_aName, nMode, !transacted );
414 else
415 m_pOwnStg = new Storage( m_aName, nMode, !transacted );
416 m_aName = m_pOwnStg->GetName();
419 SetError( m_pOwnStg->GetError() );
421 SignAsRoot( m_pOwnStg->IsRoot() );
424 SotStorage::SotStorage( bool bUCBStorage, const OUString & rName, StreamMode nMode )
425 INIT_SotStorage()
427 m_aName = rName;
428 CreateStorage( bUCBStorage, nMode, false );
429 if ( IsOLEStorage() )
430 m_nVersion = SOFFICE_FILEFORMAT_50;
433 SotStorage::SotStorage( BaseStorage * pStor )
434 INIT_SotStorage()
436 if ( pStor )
438 m_aName = pStor->GetName(); // Namen merken
439 SignAsRoot( pStor->IsRoot() );
440 SetError( pStor->GetError() );
443 m_pOwnStg = pStor;
444 sal_uLong nErr = m_pOwnStg ? m_pOwnStg->GetError() : SVSTREAM_CANNOT_MAKE;
445 SetError( nErr );
446 if ( IsOLEStorage() )
447 m_nVersion = SOFFICE_FILEFORMAT_50;
450 SotStorage::SotStorage( bool bUCBStorage, SvStream & rStm )
451 INIT_SotStorage()
453 SetError( rStm.GetError() );
455 // try as UCBStorage, next try as OLEStorage
456 if ( UCBStorage::IsStorageFile( &rStm ) || bUCBStorage )
457 m_pOwnStg = new UCBStorage( rStm, false );
458 else
459 m_pOwnStg = new Storage( rStm, false );
461 SetError( m_pOwnStg->GetError() );
463 if ( IsOLEStorage() )
464 m_nVersion = SOFFICE_FILEFORMAT_50;
466 SignAsRoot( m_pOwnStg->IsRoot() );
469 SotStorage::SotStorage( SvStream & rStm )
470 INIT_SotStorage()
472 SetError( rStm.GetError() );
474 // try as UCBStorage, next try as OLEStorage
475 if ( UCBStorage::IsStorageFile( &rStm ) )
476 m_pOwnStg = new UCBStorage( rStm, false );
477 else
478 m_pOwnStg = new Storage( rStm, false );
480 SetError( m_pOwnStg->GetError() );
482 if ( IsOLEStorage() )
483 m_nVersion = SOFFICE_FILEFORMAT_50;
485 SignAsRoot( m_pOwnStg->IsRoot() );
488 SotStorage::SotStorage( SvStream * pStm, bool bDelete )
489 INIT_SotStorage()
491 SetError( pStm->GetError() );
493 // try as UCBStorage, next try as OLEStorage
494 if ( UCBStorage::IsStorageFile( pStm ) )
495 m_pOwnStg = new UCBStorage( *pStm, false );
496 else
497 m_pOwnStg = new Storage( *pStm, false );
499 SetError( m_pOwnStg->GetError() );
501 m_pStorStm = pStm;
502 m_bDelStm = bDelete;
503 if ( IsOLEStorage() )
504 m_nVersion = SOFFICE_FILEFORMAT_50;
506 SignAsRoot( m_pOwnStg->IsRoot() );
509 SotStorage::~SotStorage()
511 delete m_pOwnStg;
512 if( m_bDelStm )
513 delete m_pStorStm;
516 SvMemoryStream * SotStorage::CreateMemoryStream()
518 SvMemoryStream * pStm = NULL;
519 pStm = new SvMemoryStream( 0x8000, 0x8000 );
520 tools::SvRef<SotStorage> aStg = new SotStorage( *pStm );
521 if( CopyTo( aStg ) )
523 aStg->Commit();
525 else
527 aStg.Clear(); // Storage vorher freigeben
528 delete pStm;
529 pStm = NULL;
531 return pStm;
534 bool SotStorage::IsStorageFile( const OUString & rFileName )
536 OUString aName( rFileName );
537 INetURLObject aObj( aName );
538 if ( aObj.GetProtocol() == INetProtocol::NotValid )
540 OUString aURL;
541 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aURL );
542 aObj.SetURL( aURL );
543 aName = aObj.GetMainURL( INetURLObject::NO_DECODE );
546 boost::scoped_ptr<SvStream> pStm(::utl::UcbStreamHelper::CreateStream( aName, STREAM_STD_READ ));
547 bool bRet = SotStorage::IsStorageFile( pStm.get() );
548 return bRet;
551 bool SotStorage::IsStorageFile( SvStream* pStream )
553 /** code for new storages must come first! **/
554 if ( pStream )
556 long nPos = pStream->Tell();
557 bool bRet = UCBStorage::IsStorageFile( pStream );
558 if ( !bRet )
559 bRet = Storage::IsStorageFile( pStream );
560 pStream->Seek( nPos );
561 return bRet;
563 else
564 return false;
567 const OUString & SotStorage::GetName() const
569 if( m_aName.isEmpty() )
571 DBG_ASSERT( Owner(), "must be owner" );
572 if( m_pOwnStg )
573 const_cast<SotStorage *>(this)->m_aName = m_pOwnStg->GetName();
575 return m_aName;
578 void SotStorage::SetClass( const SvGlobalName & rName,
579 SotClipboardFormatId nOriginalClipFormat,
580 const OUString & rUserTypeName )
582 DBG_ASSERT( Owner(), "must be owner" );
583 if( m_pOwnStg )
584 m_pOwnStg->SetClass( rName, nOriginalClipFormat, rUserTypeName );
585 else
586 SetError( SVSTREAM_GENERALERROR );
589 SvGlobalName SotStorage::GetClassName()
591 SvGlobalName aGN;
592 DBG_ASSERT( Owner(), "must be owner" );
593 if( m_pOwnStg )
594 aGN = m_pOwnStg->GetClassName();
595 else
596 SetError( SVSTREAM_GENERALERROR );
597 return aGN;
600 SotClipboardFormatId SotStorage::GetFormat()
602 SotClipboardFormatId nFormat = SotClipboardFormatId::NONE;
603 DBG_ASSERT( Owner(), "must be owner" );
604 if( m_pOwnStg )
605 nFormat = m_pOwnStg->GetFormat();
606 else
607 SetError( SVSTREAM_GENERALERROR );
608 return nFormat;
611 OUString SotStorage::GetUserName()
613 OUString aName;
614 DBG_ASSERT( Owner(), "must be owner" );
615 if( m_pOwnStg )
616 aName = m_pOwnStg->GetUserName();
617 else
618 SetError( SVSTREAM_GENERALERROR );
619 return aName;
622 void SotStorage::FillInfoList( SvStorageInfoList * pFillList ) const
624 DBG_ASSERT( Owner(), "must be owner" );
625 if( m_pOwnStg )
626 m_pOwnStg->FillInfoList( pFillList );
629 bool SotStorage::CopyTo( SotStorage * pDestStg )
631 DBG_ASSERT( Owner(), "must be owner" );
632 DBG_ASSERT( pDestStg->Owner(), "must be owner" );
633 if( m_pOwnStg && pDestStg->m_pOwnStg )
635 m_pOwnStg->CopyTo( pDestStg->m_pOwnStg );
636 SetError( m_pOwnStg->GetError() );
637 pDestStg->m_aKey = m_aKey;
638 pDestStg->m_nVersion = m_nVersion;
640 else
641 SetError( SVSTREAM_GENERALERROR );
643 return SVSTREAM_OK == GetError();
646 bool SotStorage::Commit()
648 DBG_ASSERT( Owner(), "must be owner" );
649 if( m_pOwnStg )
651 if( !m_pOwnStg->Commit() )
652 SetError( m_pOwnStg->GetError() );
654 else
655 SetError( SVSTREAM_GENERALERROR );
657 return SVSTREAM_OK == GetError();
660 SotStorageStream * SotStorage::OpenSotStream( const OUString & rEleName,
661 StreamMode nMode )
663 SotStorageStream * pStm = NULL;
664 DBG_ASSERT( Owner(), "must be owner" );
665 if( m_pOwnStg )
667 // volle Ole-Patches einschalten
668 // egal was kommt, nur exclusiv gestattet
669 nMode |= StreamMode::SHARE_DENYALL;
670 ErrCode nE = m_pOwnStg->GetError();
671 BaseStorageStream * p = m_pOwnStg->OpenStream( rEleName, nMode, true );
672 pStm = new SotStorageStream( p );
674 if( !nE )
675 m_pOwnStg->ResetError(); // kein Fehler setzen
676 if( nMode & StreamMode::TRUNC )
677 pStm->SetSize( 0 );
679 else
680 SetError( SVSTREAM_GENERALERROR );
682 return pStm;
685 SotStorage * SotStorage::OpenSotStorage( const OUString & rEleName,
686 StreamMode nMode,
687 bool transacted )
689 DBG_ASSERT( Owner(), "must be owner" );
690 if( m_pOwnStg )
692 nMode |= StreamMode::SHARE_DENYALL;
693 ErrCode nE = m_pOwnStg->GetError();
694 BaseStorage * p = m_pOwnStg->OpenStorage(rEleName, nMode, !transacted);
695 if( p )
697 SotStorage * pStor = new SotStorage( p );
698 if( !nE )
699 m_pOwnStg->ResetError(); // kein Fehler setzen
701 return pStor;
705 SetError( SVSTREAM_GENERALERROR );
707 return NULL;
710 bool SotStorage::IsStorage( const OUString & rEleName ) const
712 DBG_ASSERT( Owner(), "must be owner" );
713 // ein bisschen schneller
714 if( m_pOwnStg )
715 return m_pOwnStg->IsStorage( rEleName );
717 return false;
720 bool SotStorage::IsStream( const OUString & rEleName ) const
722 DBG_ASSERT( Owner(), "must be owner" );
723 // ein bisschen schneller
724 if( m_pOwnStg )
725 return m_pOwnStg->IsStream( rEleName );
727 return false;
730 bool SotStorage::IsContained( const OUString & rEleName ) const
732 DBG_ASSERT( Owner(), "must be owner" );
733 // ein bisschen schneller
734 if( m_pOwnStg )
735 return m_pOwnStg->IsContained( rEleName );
737 return false;
740 bool SotStorage::Remove( const OUString & rEleName )
742 DBG_ASSERT( Owner(), "must be owner" );
743 if( m_pOwnStg )
745 m_pOwnStg->Remove( rEleName );
746 SetError( m_pOwnStg->GetError() );
748 else
749 SetError( SVSTREAM_GENERALERROR );
751 return SVSTREAM_OK == GetError();
754 bool SotStorage::CopyTo( const OUString & rEleName,
755 SotStorage * pNewSt, const OUString & rNewName )
757 DBG_ASSERT( Owner(), "must be owner" );
758 DBG_ASSERT( pNewSt->Owner(), "must be owner" );
759 if( m_pOwnStg )
761 m_pOwnStg->CopyTo( rEleName, pNewSt->m_pOwnStg, rNewName );
762 SetError( m_pOwnStg->GetError() );
763 SetError( pNewSt->GetError() );
765 else
766 SetError( SVSTREAM_GENERALERROR );
768 return SVSTREAM_OK == GetError();
771 bool SotStorage::Validate()
773 DBG_ASSERT( m_bIsRoot, "Validate nur an Rootstorage" );
774 if( m_pOwnStg )
775 return m_pOwnStg->ValidateFAT();
776 else
777 return true;
780 bool SotStorage::IsOLEStorage() const
782 UCBStorage* pStg = PTR_CAST( UCBStorage, m_pOwnStg );
783 return !pStg;
786 bool SotStorage::IsOLEStorage( const OUString & rFileName )
788 return Storage::IsStorageFile( rFileName );
791 bool SotStorage::IsOLEStorage( SvStream* pStream )
793 return Storage::IsStorageFile( pStream );
796 SotStorage* SotStorage::OpenOLEStorage( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& xStorage,
797 const OUString& rEleName, StreamMode nMode )
799 sal_Int32 nEleMode = embed::ElementModes::SEEKABLEREAD;
800 if ( nMode & StreamMode::WRITE )
801 nEleMode |= embed::ElementModes::WRITE;
802 if ( nMode & StreamMode::TRUNC )
803 nEleMode |= embed::ElementModes::TRUNCATE;
804 if ( nMode & StreamMode::NOCREATE )
805 nEleMode |= embed::ElementModes::NOCREATE;
807 SvStream* pStream = NULL;
810 uno::Reference < io::XStream > xStream = xStorage->openStreamElement( rEleName, nEleMode );
812 // TODO/LATER: should it be done this way?
813 if ( nMode & StreamMode::WRITE )
815 uno::Reference < beans::XPropertySet > xStreamProps( xStream, uno::UNO_QUERY_THROW );
816 xStreamProps->setPropertyValue(
817 OUString( "MediaType" ),
818 uno::makeAny( OUString( "application/vnd.sun.star.oleobject" ) ) );
821 pStream = utl::UcbStreamHelper::CreateStream( xStream );
823 catch ( uno::Exception& )
825 //TODO/LATER: ErrorHandling
826 pStream = new SvMemoryStream;
827 pStream->SetError( ERRCODE_IO_GENERAL );
830 return new SotStorage( pStream, true );
833 SotClipboardFormatId SotStorage::GetFormatID( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& xStorage )
835 uno::Reference< beans::XPropertySet > xProps( xStorage, uno::UNO_QUERY );
836 if ( !xProps.is() )
837 return SotClipboardFormatId::NONE;
839 OUString aMediaType;
840 xProps->getPropertyValue("MediaType") >>= aMediaType;
841 if ( !aMediaType.isEmpty() )
843 ::com::sun::star::datatransfer::DataFlavor aDataFlavor;
844 aDataFlavor.MimeType = aMediaType;
845 return SotExchange::GetFormat( aDataFlavor );
848 return SotClipboardFormatId::NONE;
851 sal_Int32 SotStorage::GetVersion( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& xStorage )
853 SotClipboardFormatId nSotFormatID = SotStorage::GetFormatID( xStorage );
854 switch( nSotFormatID )
856 case SotClipboardFormatId::STARWRITER_8:
857 case SotClipboardFormatId::STARWRITER_8_TEMPLATE:
858 case SotClipboardFormatId::STARWRITERWEB_8:
859 case SotClipboardFormatId::STARWRITERGLOB_8:
860 case SotClipboardFormatId::STARWRITERGLOB_8_TEMPLATE:
861 case SotClipboardFormatId::STARDRAW_8:
862 case SotClipboardFormatId::STARDRAW_8_TEMPLATE:
863 case SotClipboardFormatId::STARIMPRESS_8:
864 case SotClipboardFormatId::STARIMPRESS_8_TEMPLATE:
865 case SotClipboardFormatId::STARCALC_8:
866 case SotClipboardFormatId::STARCALC_8_TEMPLATE:
867 case SotClipboardFormatId::STARCHART_8:
868 case SotClipboardFormatId::STARCHART_8_TEMPLATE:
869 case SotClipboardFormatId::STARMATH_8:
870 case SotClipboardFormatId::STARMATH_8_TEMPLATE:
871 return SOFFICE_FILEFORMAT_8;
872 case SotClipboardFormatId::STARWRITER_60:
873 case SotClipboardFormatId::STARWRITERWEB_60:
874 case SotClipboardFormatId::STARWRITERGLOB_60:
875 case SotClipboardFormatId::STARDRAW_60:
876 case SotClipboardFormatId::STARIMPRESS_60:
877 case SotClipboardFormatId::STARCALC_60:
878 case SotClipboardFormatId::STARCHART_60:
879 case SotClipboardFormatId::STARMATH_60:
880 return SOFFICE_FILEFORMAT_60;
881 default: break;
884 return 0;
887 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */