diff options
author | Oliver Bolte <obo@openoffice.org> | 2007-07-17 12:44:56 +0000 |
---|---|---|
committer | Oliver Bolte <obo@openoffice.org> | 2007-07-17 12:44:56 +0000 |
commit | adcfb45fba519f3b649f304c7afc097045cfb529 (patch) | |
tree | 9b9cf6350eb0b6d400f9e79ca2de3de494a36d3c /sfx2/source/doc/oleprops.cxx | |
parent | 5aa311c1cd450081106fb8dcf04280b8a0c74de2 (diff) |
INTEGRATION: CWS basemodelrefactoring (1.1.2); FILE ADDED
2007/05/09 16:14:47 mba 1.1.2.4: define used at wrong place
2007/04/06 19:04:38 mba 1.1.2.3: #i75677#: string props no longer limited in length
2007/04/04 14:31:22 mba 1.1.2.2: #i75726#: encapsulate special invalid Windows time stamps
2007/03/24 19:31:11 mba 1.1.2.1: #i75677#,#i26672#: separation of DocInfo and DocumentInfoObject
Diffstat (limited to 'sfx2/source/doc/oleprops.cxx')
-rwxr-xr-x | sfx2/source/doc/oleprops.cxx | 1166 |
1 files changed, 1166 insertions, 0 deletions
diff --git a/sfx2/source/doc/oleprops.cxx b/sfx2/source/doc/oleprops.cxx new file mode 100755 index 000000000000..cfce745b1f5e --- /dev/null +++ b/sfx2/source/doc/oleprops.cxx @@ -0,0 +1,1166 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: oleprops.cxx,v $ + * + * $Revision: 1.2 $ + * + * last change: $Author: obo $ $Date: 2007-07-17 13:44:56 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sfx2.hxx" + +#include "oleprops.hxx" + +#include <com/sun/star/util/DateTime.hpp> +#include <comphelper/types.hxx> +#include <tools/debug.hxx> +#include <vcl/gdimtf.hxx> +#include <rtl/tencinfo.h> + +// ============================================================================ + + +// ============================================================================ + +static const char __FAR_DATA pDocInfoSlot[] = "SfxDocumentInfo"; +static const char __FAR_DATA pDocInfoHeader[] = "SfxDocumentInfo"; + +#define VERSION 11 +#define STREAM_BUFFER_SIZE 2048 + +// stream names +#define STREAM_SUMMARYINFO "\005SummaryInformation" +#define STREAM_DOCSUMMARYINFO "\005DocumentSummaryInformation" + +// usings +using ::rtl::OUString; +using ::com::sun::star::uno::Any; +using ::com::sun::star::uno::makeAny; + +using namespace ::com::sun::star; + +#define TIMESTAMP_INVALID_DATETIME ( DateTime ( Date ( 1, 1, 1601 ), Time ( 0, 0, 0 ) ) ) /// Invalid value for date and time to create invalid instance of TimeStamp. + +// ============================================================================ + +/** Property representing a signed 32-bit integer value. */ +class SfxOleInt32Property : public SfxOlePropertyBase +{ +public: + explicit SfxOleInt32Property( sal_Int32 nPropId, sal_Int32 nValue = 0 ); + + inline sal_Int32 GetValue() const { return mnValue; } + inline void SetValue( sal_Int32 nValue ) { mnValue = nValue; } + +private: + virtual void ImplLoad( SvStream& rStrm ); + virtual void ImplSave( SvStream& rStrm ); + +private: + sal_Int32 mnValue; +}; + +// ============================================================================ + +/** Property representing a floating-point value. */ +class SfxOleDoubleProperty : public SfxOlePropertyBase +{ +public: + explicit SfxOleDoubleProperty( sal_Int32 nPropId, double fValue = 0.0 ); + + inline double GetValue() const { return mfValue; } + inline void SetValue( double fValue ) { mfValue = fValue; } + +private: + virtual void ImplLoad( SvStream& rStrm ); + virtual void ImplSave( SvStream& rStrm ); + +private: + double mfValue; +}; + +// ============================================================================ + +/** Property representing a boolean value. */ +class SfxOleBoolProperty : public SfxOlePropertyBase +{ +public: + explicit SfxOleBoolProperty( sal_Int32 nPropId, bool bValue = false ); + + inline bool GetValue() const { return mbValue; } + inline void SetValue( bool bValue ) { mbValue = bValue; } + +private: + virtual void ImplLoad( SvStream& rStrm ); + virtual void ImplSave( SvStream& rStrm ); + +private: + bool mbValue; +}; + +// ============================================================================ + +/** Base class for properties that contain a single string value. */ +class SfxOleStringPropertyBase : public SfxOlePropertyBase, public SfxOleStringHelper +{ +public: + explicit SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, + const SfxOleTextEncoding& rTextEnc ); + explicit SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, + const SfxOleTextEncoding& rTextEnc, const String& rValue ); + explicit SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, + rtl_TextEncoding eTextEnc ); + explicit SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, + rtl_TextEncoding eTextEnc, const String& rValue ); + + inline const String& GetValue() const { return maValue; } + inline void SetValue( const String& rValue ) { maValue = rValue; } + +private: + String maValue; +}; + +// ============================================================================ + +/** Property representing a bytestring value. */ +class SfxOleString8Property : public SfxOleStringPropertyBase +{ +public: + explicit SfxOleString8Property( + sal_Int32 nPropId, const SfxOleTextEncoding& rTextEnc ); + explicit SfxOleString8Property( + sal_Int32 nPropId, const SfxOleTextEncoding& rTextEnc, + const String& rValue ); + +private: + virtual void ImplLoad( SvStream& rStrm ); + virtual void ImplSave( SvStream& rStrm ); +}; + +// ============================================================================ + +/** Property representing a Unicode string value. */ +class SfxOleString16Property : public SfxOleStringPropertyBase +{ +public: + explicit SfxOleString16Property( sal_Int32 nPropId ); + explicit SfxOleString16Property( sal_Int32 nPropId, const String& rValue ); + +private: + virtual void ImplLoad( SvStream& rStrm ); + virtual void ImplSave( SvStream& rStrm ); +}; + +// ============================================================================ + +/** Property representing a filetime value as defined by the Windows API. */ +class SfxOleFileTimeProperty : public SfxOlePropertyBase +{ +public: + explicit SfxOleFileTimeProperty( sal_Int32 nPropId ); + /** @param rDateTime Date and time as LOCAL time. */ + explicit SfxOleFileTimeProperty( sal_Int32 nPropId, const DateTime& rDateTime ); + + /** Returns the time value as LOCAL time. */ + inline const DateTime& GetValue() const { return maDateTime; } + /** @param rDateTime Date and time as LOCAL time. */ + inline void SetValue( const DateTime& rDateTime ) { maDateTime = rDateTime; } + +private: + virtual void ImplLoad( SvStream& rStrm ); + virtual void ImplSave( SvStream& rStrm ); + +private: + DateTime maDateTime; +}; + +// ============================================================================ + +/** Property representing a thumbnail picture. + + Currently, only saving this property is implemented. + */ +class SfxOleThumbnailProperty : public SfxOlePropertyBase +{ +public: + explicit SfxOleThumbnailProperty( sal_Int32 nPropId, const GDIMetaFile& rMetaFile ); + + inline bool IsValid() const { return maBitmapData.GetSize() > 0; } + +private: + virtual void ImplLoad( SvStream& rStrm ); + virtual void ImplSave( SvStream& rStrm ); + +private: + SvMemoryStream maBitmapData; +}; + +sal_uInt16 SfxOleTextEncoding::GetCodePage() const +{ + sal_uInt16 nCodePage = IsUnicode() ? CODEPAGE_UNICODE : + static_cast< sal_uInt16 >( rtl_getWindowsCodePageFromTextEncoding( *mxTextEnc ) ); + return (nCodePage == CODEPAGE_UNKNOWN) ? CODEPAGE_UTF8 : nCodePage; +} + +void SfxOleTextEncoding::SetCodePage( sal_uInt16 nCodePage ) +{ + if( nCodePage == CODEPAGE_UNICODE ) + SetUnicode(); + else + { + rtl_TextEncoding eTextEnc = rtl_getTextEncodingFromWindowsCodePage( nCodePage ); + if( eTextEnc != RTL_TEXTENCODING_DONTKNOW ) + *mxTextEnc = eTextEnc; + } +} + +// ---------------------------------------------------------------------------- + +String SfxOleStringHelper::LoadString8( SvStream& rStrm ) const +{ + return IsUnicode() ? ImplLoadString16( rStrm ) : ImplLoadString8( rStrm ); +} + +void SfxOleStringHelper::SaveString8( SvStream& rStrm, const String& rValue ) const +{ + if( IsUnicode() ) + ImplSaveString16( rStrm, rValue ); + else + ImplSaveString8( rStrm, rValue ); +} + +String SfxOleStringHelper::LoadString16( SvStream& rStrm ) const +{ + return ImplLoadString16( rStrm ); +} + +void SfxOleStringHelper::SaveString16( SvStream& rStrm, const String& rValue ) const +{ + ImplSaveString16( rStrm, rValue ); +} + +String SfxOleStringHelper::ImplLoadString8( SvStream& rStrm ) const +{ + String aValue; + // read size field (signed 32-bit) + sal_Int32 nSize; + rStrm >> nSize; + // size field includes trailing NUL character + DBG_ASSERT( (0 < nSize) && (nSize <= 0xFFFF), "SfxOleStringHelper::ImplLoadString8 - invalid string" ); + if( (0 < nSize) && (nSize <= 0xFFFF) ) + { + // load character buffer + ::std::vector< sal_Char > aBuffer( static_cast< size_t >( nSize + 1 ), 0 ); + rStrm.Read( &aBuffer.front(), static_cast< sal_Size >( nSize ) ); + // create string from encoded character array + aValue = String( &aBuffer.front(), GetTextEncoding() ); + } + return aValue; +} + +String SfxOleStringHelper::ImplLoadString16( SvStream& rStrm ) const +{ + String aValue; + // read size field (signed 32-bit), may be buffer size or character count + sal_Int32 nSize; + rStrm >> nSize; + DBG_ASSERT( (0 < nSize) && (nSize <= 0xFFFF), "SfxOleStringHelper::ImplLoadString16 - invalid string" ); + // size field includes trailing NUL character + if( (0 < nSize) && (nSize <= 0xFFFF) ) + { + // load character buffer + ::std::vector< sal_Unicode > aBuffer; + aBuffer.reserve( static_cast< size_t >( nSize + 1 ) ); + sal_uInt16 cChar; + for( sal_Int32 nIdx = 0; nIdx < nSize; ++nIdx ) + { + rStrm >> cChar; + aBuffer.push_back( static_cast< sal_Unicode >( cChar ) ); + } + // stream is always padded to 32-bit boundary, skip 2 bytes on odd character count + if( (nSize & 1) == 1 ) + rStrm.SeekRel( 2 ); + // create string from character array + aBuffer.push_back( 0 ); + aValue = String( &aBuffer.front() ); + } + return aValue; +} + +void SfxOleStringHelper::ImplSaveString8( SvStream& rStrm, const String& rValue ) const +{ + // encode to byte string + ByteString aEncoded( rValue, GetTextEncoding() ); + // write size field (including trailing NUL character) + sal_Int32 nSize = static_cast< sal_Int32 >( aEncoded.Len() + 1 ); + rStrm << nSize; + // write character array with trailing NUL character + rStrm.Write( aEncoded.GetBuffer(), aEncoded.Len() ); + rStrm << sal_uInt8( 0 ); +} + +void SfxOleStringHelper::ImplSaveString16( SvStream& rStrm, const String& rValue ) const +{ + // write size field (including trailing NUL character) + sal_Int32 nSize = static_cast< sal_Int32 >( rValue.Len() + 1 ); + rStrm << nSize; + // write character array with trailing NUL character + for( xub_StrLen nIdx = 0; nIdx < rValue.Len(); ++nIdx ) + rStrm << static_cast< sal_uInt16 >( rValue.GetChar( nIdx ) ); + rStrm << sal_uInt16( 0 ); + // stream is always padded to 32-bit boundary, add 2 bytes on odd character count + if( (nSize & 1) == 1 ) + rStrm << sal_uInt16( 0 ); +} + +// ---------------------------------------------------------------------------- + +SfxOleObjectBase::~SfxOleObjectBase() +{ +} + +ErrCode SfxOleObjectBase::Load( SvStream& rStrm ) +{ + mnErrCode = ERRCODE_NONE; + ImplLoad( rStrm ); + SetError( rStrm.GetErrorCode() ); + return GetError(); +} + +ErrCode SfxOleObjectBase::Save( SvStream& rStrm ) +{ + mnErrCode = ERRCODE_NONE; + ImplSave( rStrm ); + SetError( rStrm.GetErrorCode() ); + return GetError(); +} + +void SfxOleObjectBase::LoadObject( SvStream& rStrm, SfxOleObjectBase& rObj ) +{ + SetError( rObj.Load( rStrm ) ); +} + +void SfxOleObjectBase::SaveObject( SvStream& rStrm, SfxOleObjectBase& rObj ) +{ + SetError( rObj.Save( rStrm ) ); +} + +// ---------------------------------------------------------------------------- + +SfxOleCodePageProperty::SfxOleCodePageProperty() : + SfxOlePropertyBase( PROPID_CODEPAGE, PROPTYPE_INT16 ) +{ +} + +void SfxOleCodePageProperty::ImplLoad( SvStream& rStrm ) +{ + // property type is signed int16, but we use always unsigned int16 for codepages + sal_uInt16 nCodePage; + rStrm >> nCodePage; + SetCodePage( nCodePage ); +} + +void SfxOleCodePageProperty::ImplSave( SvStream& rStrm ) +{ + // property type is signed int16, but we use always unsigned int16 for codepages + rStrm << GetCodePage(); +} + +// ---------------------------------------------------------------------------- + +SfxOleInt32Property::SfxOleInt32Property( sal_Int32 nPropId, sal_Int32 nValue ) : + SfxOlePropertyBase( nPropId, PROPTYPE_INT32 ), + mnValue( nValue ) +{ +} + +void SfxOleInt32Property::ImplLoad( SvStream& rStrm ) +{ + rStrm >> mnValue; +} + +void SfxOleInt32Property::ImplSave( SvStream& rStrm ) +{ + rStrm << mnValue; +} + +// ---------------------------------------------------------------------------- + +SfxOleDoubleProperty::SfxOleDoubleProperty( sal_Int32 nPropId, double fValue ) : + SfxOlePropertyBase( nPropId, PROPTYPE_DOUBLE ), + mfValue( fValue ) +{ +} + +void SfxOleDoubleProperty::ImplLoad( SvStream& rStrm ) +{ + rStrm >> mfValue; +} + +void SfxOleDoubleProperty::ImplSave( SvStream& rStrm ) +{ + rStrm << mfValue; +} + +// ---------------------------------------------------------------------------- + +SfxOleBoolProperty::SfxOleBoolProperty( sal_Int32 nPropId, bool bValue ) : + SfxOlePropertyBase( nPropId, PROPTYPE_BOOL ), + mbValue( bValue ) +{ +} + +void SfxOleBoolProperty::ImplLoad( SvStream& rStrm ) +{ + sal_Int16 nValue; + rStrm >> nValue; + mbValue = nValue != 0; +} + +void SfxOleBoolProperty::ImplSave( SvStream& rStrm ) +{ + rStrm << static_cast< sal_Int16 >( mbValue ? -1 : 0 ); +} + +// ---------------------------------------------------------------------------- + +SfxOleStringPropertyBase::SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, const SfxOleTextEncoding& rTextEnc ) : + SfxOlePropertyBase( nPropId, nPropType ), + SfxOleStringHelper( rTextEnc ) +{ +} + +SfxOleStringPropertyBase::SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, const SfxOleTextEncoding& rTextEnc, const String& rValue ) : + SfxOlePropertyBase( nPropId, nPropType ), + SfxOleStringHelper( rTextEnc ), + maValue( rValue ) +{ +} + +SfxOleStringPropertyBase::SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, rtl_TextEncoding eTextEnc ) : + SfxOlePropertyBase( nPropId, nPropType ), + SfxOleStringHelper( eTextEnc ) +{ +} + +SfxOleStringPropertyBase::SfxOleStringPropertyBase( + sal_Int32 nPropId, sal_Int32 nPropType, rtl_TextEncoding eTextEnc, const String& rValue ) : + SfxOlePropertyBase( nPropId, nPropType ), + SfxOleStringHelper( eTextEnc ), + maValue( rValue ) +{ +} + +// ---------------------------------------------------------------------------- + +SfxOleString8Property::SfxOleString8Property( + sal_Int32 nPropId, const SfxOleTextEncoding& rTextEnc ) : + SfxOleStringPropertyBase( nPropId, PROPTYPE_STRING8, rTextEnc ) +{ +} + +SfxOleString8Property::SfxOleString8Property( + sal_Int32 nPropId, const SfxOleTextEncoding& rTextEnc, const String& rValue ) : + SfxOleStringPropertyBase( nPropId, PROPTYPE_STRING8, rTextEnc, rValue ) +{ +} + +void SfxOleString8Property::ImplLoad( SvStream& rStrm ) +{ + SetValue( LoadString8( rStrm ) ); +} + +void SfxOleString8Property::ImplSave( SvStream& rStrm ) +{ + SaveString8( rStrm, GetValue() ); +} + +// ---------------------------------------------------------------------------- + +SfxOleString16Property::SfxOleString16Property( sal_Int32 nPropId ) : + SfxOleStringPropertyBase( nPropId, PROPTYPE_STRING16, RTL_TEXTENCODING_UCS2 ) +{ +} + +SfxOleString16Property::SfxOleString16Property( sal_Int32 nPropId, const String& rValue ) : + SfxOleStringPropertyBase( nPropId, PROPTYPE_STRING16, RTL_TEXTENCODING_UCS2, rValue ) +{ +} + +void SfxOleString16Property::ImplLoad( SvStream& rStrm ) +{ + SetValue( LoadString16( rStrm ) ); +} + +void SfxOleString16Property::ImplSave( SvStream& rStrm ) +{ + SaveString16( rStrm, GetValue() ); +} + +// ---------------------------------------------------------------------------- + +SfxOleFileTimeProperty::SfxOleFileTimeProperty( sal_Int32 nPropId ) : + SfxOlePropertyBase( nPropId, PROPTYPE_FILETIME ) +{ +} + +SfxOleFileTimeProperty::SfxOleFileTimeProperty( sal_Int32 nPropId, const DateTime& rDateTime ) : + SfxOlePropertyBase( nPropId, PROPTYPE_FILETIME ), + maDateTime( rDateTime ) +{ +} + +void SfxOleFileTimeProperty::ImplLoad( SvStream& rStrm ) +{ + sal_uInt32 nLower, nUpper; + rStrm >> nLower >> nUpper; + maDateTime = DateTime::CreateFromWin32FileDateTime( nLower, nUpper ); + if ( maDateTime != TIMESTAMP_INVALID_DATETIME ) + maDateTime.ConvertToLocalTime(); +} + +void SfxOleFileTimeProperty::ImplSave( SvStream& rStrm ) +{ + DateTime aDateTimeUtc( maDateTime ); + // invalid time stamp is not converted to UTC + if( aDateTimeUtc.IsValid() && maDateTime != TIMESTAMP_INVALID_DATETIME ) + aDateTimeUtc.ConvertToUTC(); + sal_uInt32 nLower, nUpper; + aDateTimeUtc.GetWin32FileDateTime( nLower, nUpper ); + rStrm << nLower << nUpper; +} + +// ---------------------------------------------------------------------------- + +SfxOleThumbnailProperty::SfxOleThumbnailProperty( + sal_Int32 nPropId, const GDIMetaFile& rMetaFile ) : + SfxOlePropertyBase( nPropId, PROPTYPE_CLIPFMT ) +{ + BitmapEx aBitmap; + // magic value 160 taken from GraphicHelper::getThumbnailFormatFromGDI_Impl() + if( rMetaFile.CreateThumbnail( 160, aBitmap ) ) + aBitmap.GetBitmap().Write( maBitmapData, FALSE, FALSE ); +} + +void SfxOleThumbnailProperty::ImplLoad( SvStream& ) +{ + DBG_ERRORFILE( "SfxOleThumbnailProperty::ImplLoad - not implemented" ); + SetError( SVSTREAM_INVALID_ACCESS ); +} + +void SfxOleThumbnailProperty::ImplSave( SvStream& rStrm ) +{ + /* Type Contents + ----------------------------------------------------------------------- + int32 size of following data + int32 clipboard format tag (see below) + byte[] clipboard data (see below) + + Clipboard format tag: + -1 = Windows clipboard format + -2 = Macintosh clipboard format + -3 = GUID that contains a format identifier (FMTID) + >0 = custom clipboard format name plus data (see msdn site below) + 0 = no data + + References: + http://msdn.microsoft.com/library/default.asp?url=/library/en-us/stg/stg/propvariant.asp + http://jakarta.apache.org/poi/hpsf/thumbnails.html + http://linux.com.hk/docs/poi/org/apache/poi/hpsf/Thumbnail.html + http://sparks.discreet.com/knowledgebase/public/solutions/ExtractThumbnailImg.htm + */ + if( IsValid() ) + { + // clibboard size: clip_format_tag + data_format_tag + bitmap_len + sal_Int32 nClipSize = static_cast< sal_Int32 >( 4 + 4 + maBitmapData.GetSize() ); + rStrm << nClipSize << CLIPFMT_WIN << CLIPDATAFMT_DIB; + rStrm.Write( maBitmapData.GetData(), maBitmapData.GetSize() ); + } + else + { + DBG_ERRORFILE( "SfxOleThumbnailProperty::ImplSave - invalid thumbnail property" ); + SetError( SVSTREAM_INVALID_ACCESS ); + } +} + +// ---------------------------------------------------------------------------- + +SfxOleDictionaryProperty::SfxOleDictionaryProperty( const SfxOleTextEncoding& rTextEnc ) : + SfxOlePropertyBase( PROPID_DICTIONARY, 0 ), + SfxOleStringHelper( rTextEnc ) +{ +} + +const String& SfxOleDictionaryProperty::GetPropertyName( sal_Int32 nPropId ) const +{ + SfxOlePropNameMap::const_iterator aIt = maPropNameMap.find( nPropId ); + return (aIt == maPropNameMap.end()) ? String::EmptyString() : aIt->second; +} + +void SfxOleDictionaryProperty::SetPropertyName( sal_Int32 nPropId, const String& rPropName ) +{ + maPropNameMap[ nPropId ] = rPropName; + // dictionary property contains number of pairs in property type field + SetPropType( static_cast< sal_Int32 >( maPropNameMap.size() ) ); +} + +void SfxOleDictionaryProperty::ImplLoad( SvStream& rStrm ) +{ + // dictionary property contains number of pairs in property type field + sal_Int32 nNameCount = GetPropType(); + // read property ID/name pairs + maPropNameMap.clear(); + for( sal_Int32 nIdx = 0; (nIdx < nNameCount) && (rStrm.GetErrorCode() == SVSTREAM_OK); ++nIdx ) + { + sal_Int32 nPropId; + rStrm >> nPropId; + // name always stored as byte string + maPropNameMap[ nPropId ] = LoadString8( rStrm ); + } +} + +void SfxOleDictionaryProperty::ImplSave( SvStream& rStrm ) +{ + // write property ID/name pairs + for( SfxOlePropNameMap::const_iterator aIt = maPropNameMap.begin(), aEnd = maPropNameMap.end(); aIt != aEnd; ++aIt ) + { + rStrm << aIt->first; + // name always stored as byte string + SaveString8( rStrm, aIt->second ); + } +} + +// ---------------------------------------------------------------------------- + +SfxOleSection::SfxOleSection( bool bSupportsDict ) : + maDictProp( maCodePageProp ), + mnStartPos( 0 ), + mbSupportsDict( bSupportsDict ) +{ +} + +SfxOlePropertyRef SfxOleSection::GetProperty( sal_Int32 nPropId ) const +{ + SfxOlePropertyRef xProp; + SfxOlePropMap::const_iterator aIt = maPropMap.find( nPropId ); + if( aIt != maPropMap.end() ) + xProp = aIt->second; + return xProp; +} + +bool SfxOleSection::GetInt32Value( sal_Int32& rnValue, sal_Int32 nPropId ) const +{ + SfxOlePropertyRef xProp = GetProperty( nPropId ); + const SfxOleInt32Property* pProp = + dynamic_cast< const SfxOleInt32Property* >( xProp.get() ); + if( pProp ) + rnValue = pProp->GetValue(); + return pProp != 0; +} + +bool SfxOleSection::GetDoubleValue( double& rfValue, sal_Int32 nPropId ) const +{ + SfxOlePropertyRef xProp = GetProperty( nPropId ); + const SfxOleDoubleProperty* pProp = + dynamic_cast< const SfxOleDoubleProperty* >( xProp.get() ); + if( pProp ) + rfValue = pProp->GetValue(); + return pProp != 0; +} + +bool SfxOleSection::GetBoolValue( bool& rbValue, sal_Int32 nPropId ) const +{ + SfxOlePropertyRef xProp = GetProperty( nPropId ); + const SfxOleBoolProperty* pProp = + dynamic_cast< const SfxOleBoolProperty* >( xProp.get() ); + if( pProp ) + rbValue = pProp->GetValue(); + return pProp != 0; +} + +bool SfxOleSection::GetStringValue( String& rValue, sal_Int32 nPropId ) const +{ + SfxOlePropertyRef xProp = GetProperty( nPropId ); + const SfxOleStringPropertyBase* pProp = + dynamic_cast< const SfxOleStringPropertyBase* >( xProp.get() ); + if( pProp ) + rValue = pProp->GetValue(); + return pProp != 0; +} + +bool SfxOleSection::GetFileTimeValue( DateTime& rValue, sal_Int32 nPropId ) const +{ + SfxOlePropertyRef xProp = GetProperty( nPropId ); + const SfxOleFileTimeProperty* pProp = + dynamic_cast< const SfxOleFileTimeProperty* >( xProp.get() ); + if( pProp ) + { + if ( pProp->GetValue() == TIMESTAMP_INVALID_DATETIME ) + rValue = DateTime(0,0); + else + rValue = pProp->GetValue(); + } + return pProp != 0; +} + +void SfxOleSection::SetProperty( SfxOlePropertyRef xProp ) +{ + if( xProp.get() ) + maPropMap[ xProp->GetPropId() ] = xProp; +} + +void SfxOleSection::SetInt32Value( sal_Int32 nPropId, sal_Int32 nValue ) +{ + SetProperty( SfxOlePropertyRef( new SfxOleInt32Property( nPropId, nValue ) ) ); +} + +void SfxOleSection::SetDoubleValue( sal_Int32 nPropId, double fValue ) +{ + SetProperty( SfxOlePropertyRef( new SfxOleDoubleProperty( nPropId, fValue ) ) ); +} + +void SfxOleSection::SetBoolValue( sal_Int32 nPropId, bool bValue ) +{ + SetProperty( SfxOlePropertyRef( new SfxOleBoolProperty( nPropId, bValue ) ) ); +} + +bool SfxOleSection::SetStringValue( sal_Int32 nPropId, const String& rValue, bool bSkipEmpty ) +{ + bool bInserted = !bSkipEmpty || (rValue.Len() > 0); + if( bInserted ) + SetProperty( SfxOlePropertyRef( new SfxOleString8Property( nPropId, maCodePageProp, rValue ) ) ); + return bInserted; +} + +void SfxOleSection::SetFileTimeValue( sal_Int32 nPropId, const DateTime& rValue ) +{ + if ( !rValue.GetDate() ) + SetProperty( SfxOlePropertyRef( new SfxOleFileTimeProperty( nPropId, TIMESTAMP_INVALID_DATETIME ) ) ); + else + SetProperty( SfxOlePropertyRef( new SfxOleFileTimeProperty( nPropId, rValue ) ) ); +} + +void SfxOleSection::SetThumbnailValue( sal_Int32 nPropId, const GDIMetaFile& rMetaFile ) +{ + SfxOleThumbnailProperty* pThumbnail = new SfxOleThumbnailProperty( nPropId, rMetaFile ); + SfxOlePropertyRef xProp( pThumbnail ); // take ownership + if( pThumbnail->IsValid() ) + SetProperty( xProp ); +} + +Any SfxOleSection::GetAnyValue( sal_Int32 nPropId ) const +{ + Any aValue; + sal_Int32 nInt32 = 0; + double fDouble = 0.0; + bool bBool = false; + String aString; + DateTime aDateTime; + + if( GetInt32Value( nInt32, nPropId ) ) + aValue <<= nInt32; + else if( GetDoubleValue( fDouble, nPropId ) ) + aValue <<= fDouble; + else if( GetBoolValue( bBool, nPropId ) ) + ::comphelper::setBOOL( aValue, bBool ? sal_True : sal_False ); + else if( GetStringValue( aString, nPropId ) ) + aValue <<= OUString( aString ); + else if( GetFileTimeValue( aDateTime, nPropId ) ) + { + ::com::sun::star::util::DateTime aApiDateTime( + static_cast< sal_uInt16 >( aDateTime.Get100Sec() ), + static_cast< sal_uInt16 >( aDateTime.GetSec() ), + static_cast< sal_uInt16 >( aDateTime.GetMin() ), + static_cast< sal_uInt16 >( aDateTime.GetHour() ), + static_cast< sal_uInt16 >( aDateTime.GetDay() ), + static_cast< sal_uInt16 >( aDateTime.GetMonth() ), + static_cast< sal_uInt16 >( aDateTime.GetYear() ) ); + aValue <<= aApiDateTime; + } + return aValue; +} + +bool SfxOleSection::SetAnyValue( sal_Int32 nPropId, const Any& rValue ) +{ + bool bInserted = true; + sal_Int32 nInt32 = 0; + double fDouble = 0.0; + OUString aString; + ::com::sun::star::util::DateTime aApiDateTime; + + if( rValue.getValueType() == ::getBooleanCppuType() ) + SetBoolValue( nPropId, ::comphelper::getBOOL( rValue ) == sal_True ); + else if( rValue >>= nInt32 ) + SetInt32Value( nPropId, nInt32 ); + else if( rValue >>= fDouble ) + SetDoubleValue( nPropId, fDouble ); + else if( rValue >>= aString ) + bInserted = SetStringValue( nPropId, aString ); + else if( rValue >>= aApiDateTime ) + { + DateTime aDateTime( + Date( + static_cast< USHORT >( aApiDateTime.Day ), + static_cast< USHORT >( aApiDateTime.Month ), + static_cast< USHORT >( aApiDateTime.Year ) ), + Time( + static_cast< ULONG >( aApiDateTime.Hours ), + static_cast< ULONG >( aApiDateTime.Minutes ), + static_cast< ULONG >( aApiDateTime.Seconds ), + static_cast< ULONG >( aApiDateTime.HundredthSeconds ) ) ); + SetFileTimeValue( nPropId, aDateTime ); + } + else + bInserted = false; + return bInserted; +} + +const String& SfxOleSection::GetPropertyName( sal_Int32 nPropId ) const +{ + return maDictProp.GetPropertyName( nPropId ); +} + +void SfxOleSection::SetPropertyName( sal_Int32 nPropId, const String& rPropName ) +{ + maDictProp.SetPropertyName( nPropId, rPropName ); +} + +void SfxOleSection::GetPropertyIds( ::std::vector< sal_Int32 >& rPropIds ) const +{ + rPropIds.clear(); + for( SfxOlePropMap::const_iterator aIt = maPropMap.begin(), aEnd = maPropMap.end(); aIt != aEnd; ++aIt ) + rPropIds.push_back( aIt->first ); +} + +sal_Int32 SfxOleSection::GetFreePropertyId() const +{ + return maPropMap.empty() ? PROPID_FIRSTCUSTOM : (maPropMap.rbegin()->first + 1); +} + +void SfxOleSection::ImplLoad( SvStream& rStrm ) +{ + // read section header + mnStartPos = rStrm.Tell(); + sal_uInt32 nSize; + sal_Int32 nPropCount; + rStrm >> nSize >> nPropCount; + + // read property ID/position pairs + typedef ::std::map< sal_Int32, sal_uInt32 > SfxOlePropPosMap; + SfxOlePropPosMap aPropPosMap; + for( sal_Int32 nPropIdx = 0; (nPropIdx < nPropCount) && (rStrm.GetErrorCode() == SVSTREAM_OK); ++nPropIdx ) + { + sal_Int32 nPropId; + sal_uInt32 nPropPos; + rStrm >> nPropId >> nPropPos; + aPropPosMap[ nPropId ] = nPropPos; + } + + // read codepage property + SfxOlePropPosMap::iterator aCodePageIt = aPropPosMap.find( PROPID_CODEPAGE ); + if( (aCodePageIt != aPropPosMap.end()) && SeekToPropertyPos( rStrm, aCodePageIt->second ) ) + { + // codepage property must be of type signed int-16 + sal_Int32 nPropType; + rStrm >> nPropType; + if( nPropType == PROPTYPE_INT16 ) + LoadObject( rStrm, maCodePageProp ); + // remove property position + aPropPosMap.erase( aCodePageIt ); + } + + // read dictionary property + SfxOlePropPosMap::iterator aDictIt = aPropPosMap.find( PROPID_DICTIONARY ); + if( (aDictIt != aPropPosMap.end()) && SeekToPropertyPos( rStrm, aDictIt->second ) ) + { + // #i66214# #i66428# applications may write broken dictionary properties in wrong sections + if( mbSupportsDict ) + { + // dictionary property contains number of pairs in property type field + sal_Int32 nNameCount; + rStrm >> nNameCount; + maDictProp.SetNameCount( nNameCount ); + LoadObject( rStrm, maDictProp ); + } + // always remove position of dictionary property (do not try to read it again below) + aPropPosMap.erase( aDictIt ); + } + + // read other properties + maPropMap.clear(); + for( SfxOlePropPosMap::const_iterator aIt = aPropPosMap.begin(), aEnd = aPropPosMap.end(); aIt != aEnd; ++aIt ) + if( SeekToPropertyPos( rStrm, aIt->second ) ) + LoadProperty( rStrm, aIt->first ); +} + +void SfxOleSection::ImplSave( SvStream& rStrm ) +{ + /* Always export with UTF-8 encoding. All dependent properties (bytestring + and dictionary) will be updated automatically. */ + maCodePageProp.SetTextEncoding( RTL_TEXTENCODING_UTF8 ); + + // write section header + mnStartPos = rStrm.Tell(); + sal_Int32 nPropCount = static_cast< sal_Int32 >( maPropMap.size() + 1 ); + if( maDictProp.HasPropertyNames() ) + ++nPropCount; + rStrm << sal_uInt32( 0 ) << nPropCount; + + // write placeholders for property ID/position pairs + sal_Size nPropPosPos = rStrm.Tell(); + rStrm.SeekRel( static_cast< sal_sSize >( 8 * nPropCount ) ); + + // write dictionary property + if( maDictProp.HasPropertyNames() ) + SaveProperty( rStrm, maDictProp, nPropPosPos ); + // write codepage property + SaveProperty( rStrm, maCodePageProp, nPropPosPos ); + // write other properties + for( SfxOlePropMap::const_iterator aIt = maPropMap.begin(), aEnd = maPropMap.end(); aIt != aEnd; ++aIt ) + SaveProperty( rStrm, *aIt->second, nPropPosPos ); + + // write section size (first field in section header) + rStrm.Seek( STREAM_SEEK_TO_END ); + sal_uInt32 nSectSize = static_cast< sal_uInt32 >( rStrm.Tell() - mnStartPos ); + rStrm.Seek( mnStartPos ); + rStrm << nSectSize; +} + +bool SfxOleSection::SeekToPropertyPos( SvStream& rStrm, sal_uInt32 nPropPos ) const +{ + rStrm.Seek( static_cast< sal_Size >( mnStartPos + nPropPos ) ); + return rStrm.GetErrorCode() == SVSTREAM_OK; +} + +void SfxOleSection::LoadProperty( SvStream& rStrm, sal_Int32 nPropId ) +{ + // property data type + sal_Int32 nPropType; + rStrm >> nPropType; + // create empty property object + SfxOlePropertyRef xProp; + switch( nPropType ) + { + case PROPTYPE_INT32: + xProp.reset( new SfxOleInt32Property( nPropId ) ); + break; + case PROPTYPE_DOUBLE: + xProp.reset( new SfxOleDoubleProperty( nPropId ) ); + break; + case PROPTYPE_BOOL: + xProp.reset( new SfxOleBoolProperty( nPropId ) ); + break; + case PROPTYPE_STRING8: + xProp.reset( new SfxOleString8Property( nPropId, maCodePageProp ) ); + break; + case PROPTYPE_STRING16: + xProp.reset( new SfxOleString16Property( nPropId ) ); + break; + case PROPTYPE_FILETIME: + xProp.reset( new SfxOleFileTimeProperty( nPropId ) ); + break; + } + // load property contents + if( xProp.get() ) + { + SetError( xProp->Load( rStrm ) ); + maPropMap[ nPropId ] = xProp; + } +} + +void SfxOleSection::SaveProperty( SvStream& rStrm, SfxOlePropertyBase& rProp, sal_Size& rnPropPosPos ) +{ + rStrm.Seek( STREAM_SEEK_TO_END ); + sal_uInt32 nPropPos = static_cast< sal_uInt32 >( rStrm.Tell() - mnStartPos ); + // property data type + rStrm << rProp.GetPropType(); + // write property contents + SaveObject( rStrm, rProp ); + // align to 32-bit + while( (rStrm.Tell() & 3) != 0 ) + rStrm << sal_uInt8( 0 ); + // write property ID/position pair + rStrm.Seek( rnPropPosPos ); + rStrm << rProp.GetPropId() << nPropPos; + rnPropPosPos = rStrm.Tell(); +} + +// ---------------------------------------------------------------------------- + +ErrCode SfxOlePropertySet::LoadPropertySet( SotStorage* pStrg, const String& rStrmName ) +{ + if( pStrg ) + { + SotStorageStreamRef xStrm = pStrg->OpenSotStream( rStrmName, STREAM_STD_READ ); + if( xStrm.Is() && (xStrm->GetError() == SVSTREAM_OK) ) + { + xStrm->SetBufferSize( STREAM_BUFFER_SIZE ); + Load( *xStrm ); + } + else + SetError( ERRCODE_IO_ACCESSDENIED ); + } + else + SetError( ERRCODE_IO_ACCESSDENIED ); + return GetError(); +} + +ErrCode SfxOlePropertySet::SavePropertySet( SotStorage* pStrg, const String& rStrmName ) +{ + if( pStrg ) + { + SotStorageStreamRef xStrm = pStrg->OpenSotStream( rStrmName, STREAM_TRUNC | STREAM_STD_WRITE ); + if( xStrm.Is() ) + Save( *xStrm ); + else + SetError( ERRCODE_IO_ACCESSDENIED ); + } + else + SetError( ERRCODE_IO_ACCESSDENIED ); + return GetError(); +} + +SfxOleSectionRef SfxOlePropertySet::GetSection( SfxOleSectionType eSection ) const +{ + return GetSection( GetSectionGuid( eSection ) ); +} + +SfxOleSectionRef SfxOlePropertySet::GetSection( const SvGlobalName& rSectionGuid ) const +{ + SfxOleSectionRef xSection; + SfxOleSectionMap::const_iterator aIt = maSectionMap.find( rSectionGuid ); + if( aIt != maSectionMap.end() ) + xSection = aIt->second; + return xSection; +} + +SfxOleSection& SfxOlePropertySet::AddSection( SfxOleSectionType eSection ) +{ + return AddSection( GetSectionGuid( eSection ) ); +} + +SfxOleSection& SfxOlePropertySet::AddSection( const SvGlobalName& rSectionGuid ) +{ + SfxOleSectionRef xSection = GetSection( rSectionGuid ); + if( !xSection ) + { + // #i66214# #i66428# applications may write broken dictionary properties in wrong sections + bool bSupportsDict = rSectionGuid == GetSectionGuid( SECTION_CUSTOM ); + xSection.reset( new SfxOleSection( bSupportsDict ) ); + maSectionMap[ rSectionGuid ] = xSection; + } + return *xSection; +} + +void SfxOlePropertySet::ImplLoad( SvStream& rStrm ) +{ + // read property set header + sal_uInt16 nByteOrder; + sal_uInt16 nVersion; + sal_uInt16 nOsMinor; + sal_uInt16 nOsType; + SvGlobalName aGuid; + sal_Int32 nSectCount; + rStrm >> nByteOrder >> nVersion >> nOsMinor >> nOsType >> aGuid >> nSectCount; + + // read sections + sal_Size nSectPosPos = rStrm.Tell(); + for( sal_Int32 nSectIdx = 0; (nSectIdx < nSectCount) && (rStrm.GetErrorCode() == SVSTREAM_OK); ++nSectIdx ) + { + // read section guid/position pair + rStrm.Seek( nSectPosPos ); + SvGlobalName aSectGuid; + sal_uInt32 nSectPos; + rStrm >> aSectGuid >> nSectPos; + nSectPosPos = rStrm.Tell(); + // read section + rStrm.Seek( static_cast< sal_Size >( nSectPos ) ); + if( rStrm.GetErrorCode() == SVSTREAM_OK ) + LoadObject( rStrm, AddSection( aSectGuid ) ); + } +} + +void SfxOlePropertySet::ImplSave( SvStream& rStrm ) +{ + // write property set header + SvGlobalName aGuid; + sal_Int32 nSectCount = static_cast< sal_Int32 >( maSectionMap.size() ); + rStrm << sal_uInt16( 0xFFFE ) // byte order + << sal_uInt16( 0 ) // version + << sal_uInt16( 1 ) // OS minor version + << sal_uInt16( 2 ) // OS type always windows for text encoding + << aGuid // unused guid + << nSectCount; // number of sections + + // write placeholders for section guid/position pairs + sal_Size nSectPosPos = rStrm.Tell(); + rStrm.SeekRel( static_cast< sal_sSize >( 20 * nSectCount ) ); + + // write sections + for( SfxOleSectionMap::const_iterator aIt = maSectionMap.begin(), aEnd = maSectionMap.end(); aIt != aEnd; ++aIt ) + { + SfxOleSection& rSection = *aIt->second; + rStrm.Seek( STREAM_SEEK_TO_END ); + sal_uInt32 nSectPos = static_cast< sal_uInt32 >( rStrm.Tell() ); + // write the section + SaveObject( rStrm, rSection ); + // write section guid/position pair + rStrm.Seek( nSectPosPos ); + rStrm << aIt->first << nSectPos; + nSectPosPos = rStrm.Tell(); + } +} + +const SvGlobalName& SfxOlePropertySet::GetSectionGuid( SfxOleSectionType eSection ) +{ + static const SvGlobalName saGlobalGuid( 0xF29F85E0, 0x4FF9, 0x1068, 0xAB, 0x91, 0x08, 0x00, 0x2B, 0x27, 0xB3, 0xD9 ); + static const SvGlobalName saBuiltInGuid( 0xD5CDD502, 0x2E9C, 0x101B, 0x93, 0x97, 0x08, 0x00, 0x2B, 0x2C, 0xF9, 0xAE ); + static const SvGlobalName saCustomGuid( 0xD5CDD505, 0x2E9C, 0x101B, 0x93, 0x97, 0x08, 0x00, 0x2B, 0x2C, 0xF9, 0xAE ); + static const SvGlobalName saEmptyGuid; + switch( eSection ) + { + case SECTION_GLOBAL: return saGlobalGuid; + case SECTION_BUILTIN: return saBuiltInGuid; + case SECTION_CUSTOM: return saCustomGuid; + default: DBG_ERRORFILE( "SfxOlePropertySet::GetSectionGuid - unknown section type" ); + } + return saEmptyGuid; +} + +// ============================================================================ + +//} // namespace |