1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pptexsoundcollection.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
33 #include <pptexsoundcollection.hxx>
34 #include "epptdef.hxx"
35 #include <tools/urlobj.hxx>
36 #ifndef _UCBHELPER_CONTENT_HXX_
37 #include <ucbhelper/content.hxx>
39 #ifndef _UCBHELPER_CONTENTBROKER_HXX_
40 #include <ucbhelper/contentbroker.hxx>
42 #ifndef _CPPUHELPER_PROPTYPEHLP_HXX_
43 #include <cppuhelper/proptypehlp.hxx>
45 #include <unotools/ucbstreamhelper.hxx>
50 ExSoundEntry::ExSoundEntry( const String
& rString
)
52 , aSoundURL( rString
)
56 ::ucbhelper::Content
aCnt( aSoundURL
,
57 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>() );
59 ::cppu::convertPropertyValue( nVal
, aCnt
.getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Size" ) ) ) );
60 nFileSize
= (sal_uInt32
)nVal
;
62 catch( ::com::sun::star::uno::Exception
& )
68 String
ExSoundEntry::ImplGetName() const
70 INetURLObject
aTmp( aSoundURL
);
71 return aTmp
.GetName();
74 String
ExSoundEntry::ImplGetExtension() const
76 INetURLObject
aTmp( aSoundURL
);
77 String
aExtension( aTmp
.GetExtension() );
78 if ( aExtension
.Len() )
79 aExtension
.Insert( (sal_Unicode
)'.', 0 );
83 sal_Bool
ExSoundEntry::IsSameURL( const String
& rURL
) const
85 return ( rURL
== aSoundURL
);
88 sal_uInt32
ExSoundEntry::GetSize( sal_uInt32 nId
) const
90 String
aName( ImplGetName() );
91 String
aExtension( ImplGetExtension() );
93 sal_uInt32 nSize
= 8; // SoundContainer Header
94 if ( aName
.Len() ) // String Atom ( instance 0 - name of sound )
95 nSize
+= aName
.Len() * 2 + 8;
96 if ( aExtension
.Len() ) // String Atom ( instance 1 - extension of sound )
97 nSize
+= aExtension
.Len() * 2 + 8;
99 String
aId( String::CreateFromInt32( nId
) ); // String Atom ( instance 2 - reference id )
100 nSize
+= 2 * aId
.Len() + 8;
102 nSize
+= nFileSize
+ 8; // SoundData Atom
107 void ExSoundEntry::Write( SvStream
& rSt
, sal_uInt32 nId
)
111 ::ucbhelper::Content
aCnt( aSoundURL
,
112 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>() );
114 // create SoundContainer
115 rSt
<< (sal_uInt32
)( ( EPP_Sound
<< 16 ) | 0xf ) << (sal_uInt32
)( GetSize( nId
) - 8 );
117 String
aSoundName( ImplGetName() );
118 sal_uInt16 i
, nSoundNameLen
= aSoundName
.Len();
121 // name of sound ( instance 0 )
122 rSt
<< (sal_uInt32
)( EPP_CString
<< 16 ) << (sal_uInt32
)( nSoundNameLen
* 2 );
123 for ( i
= 0; i
< nSoundNameLen
; i
++ )
124 rSt
<< aSoundName
.GetChar( i
);
126 String
aExtension( ImplGetExtension() );
127 sal_uInt32 nExtensionLen
= aExtension
.Len();
130 // extension of sound ( instance 1 )
131 rSt
<< (sal_uInt32
)( ( EPP_CString
<< 16 ) | 16 ) << (sal_uInt32
)( nExtensionLen
* 2 );
132 for ( i
= 0; i
< nExtensionLen
; i
++ )
133 rSt
<< aExtension
.GetChar( i
);
135 // id of sound ( instance 2 )
136 String
aId( String::CreateFromInt32( nId
) );
137 sal_uInt32 nIdLen
= aId
.Len();
138 rSt
<< (sal_uInt32
)( ( EPP_CString
<< 16 ) | 32 ) << (sal_uInt32
)( nIdLen
* 2 );
139 for ( i
= 0; i
< nIdLen
; i
++ )
140 rSt
<< aId
.GetChar( i
);
142 rSt
<< (sal_uInt32
)( EPP_SoundData
<< 16 ) << (sal_uInt32
)( nFileSize
);
143 sal_uInt32 nBytesLeft
= nFileSize
;
144 SvStream
* pSourceFile
= ::utl::UcbStreamHelper::CreateStream( aSoundURL
, STREAM_READ
);
147 sal_uInt8
* pBuf
= new sal_uInt8
[ 0x10000 ]; // 64 kB Buffer
150 sal_uInt32 nToDo
= ( nBytesLeft
> 0x10000 ) ? 0x10000 : nBytesLeft
;
151 pSourceFile
->Read( pBuf
, nToDo
);
152 rSt
.Write( pBuf
, nToDo
);
159 catch( ::com::sun::star::uno::Exception
& )
165 ExSoundCollection::~ExSoundCollection()
167 for( void* pPtr
= List::First(); pPtr
; pPtr
= List::Next() )
168 delete (ExSoundEntry
*)pPtr
;
171 sal_uInt32
ExSoundCollection::GetId( const String
& rString
)
173 sal_uInt32 nSoundId
= 0;
176 const sal_uInt32 nSoundCount
= Count();
178 for( ; nSoundId
< nSoundCount
; nSoundId
++ )
179 if( ImplGetByIndex( nSoundId
)->IsSameURL( rString
) )
181 if ( nSoundId
++ == nSoundCount
)
183 ExSoundEntry
* pEntry
= new ExSoundEntry( rString
);
184 if ( pEntry
->GetFileSize() )
185 List::Insert( pEntry
, LIST_APPEND
);
188 nSoundId
= 0; // only insert sounds that are accessible
196 const ExSoundEntry
* ExSoundCollection::ImplGetByIndex( sal_uInt32 nIndex
) const
198 return (ExSoundEntry
*)List::GetObject( nIndex
);
201 sal_uInt32
ExSoundCollection::GetSize() const
203 sal_uInt32 nSize
= 0;
204 sal_uInt32 i
, nSoundCount
= Count();
207 nSize
+= 8 + 12; // size of SoundCollectionContainerHeader + SoundCollAtom
208 for ( i
= 0; i
< nSoundCount
; i
++ )
209 nSize
+= ImplGetByIndex( i
)->GetSize( i
+ 1 );
214 void ExSoundCollection::Write( SvStream
& rSt
)
216 sal_uInt32 i
, nSoundCount
= Count();
219 // create SoundCollection Container
220 rSt
<< (sal_uInt16
)0xf << (sal_uInt16
)EPP_SoundCollection
<< (sal_uInt32
)( GetSize() - 8 );
222 // create SoundCollAtom ( reference to the next free SoundId );
223 rSt
<< (sal_uInt32
)( EPP_SoundCollAtom
<< 16 ) << (sal_uInt32
)4 << nSoundCount
;
225 for ( i
= 0; i
< nSoundCount
; i
++ )
226 ((ExSoundEntry
*)List::GetObject( i
))->Write( rSt
, i
+ 1 );