1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sd.hxx"
26 #include <pptexsoundcollection.hxx>
27 #include "epptdef.hxx"
28 #include <tools/urlobj.hxx>
29 #ifndef _UCBHELPER_CONTENT_HXX_
30 #include <ucbhelper/content.hxx>
31 #endif
32 #ifndef _UCBHELPER_CONTENTBROKER_HXX_
33 #include <ucbhelper/contentbroker.hxx>
34 #endif
35 #ifndef _CPPUHELPER_PROPTYPEHLP_HXX_
36 #include <cppuhelper/proptypehlp.hxx>
37 #endif
38 #include <unotools/ucbstreamhelper.hxx>
39 
40 namespace ppt
41 {
42 
ExSoundEntry(const String & rString)43 ExSoundEntry::ExSoundEntry( const String& rString )
44 :	nFileSize( 0 )
45 ,	aSoundURL( rString )
46 {
47     try
48     {
49         ::ucbhelper::Content aCnt( aSoundURL,
50             ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >() );
51         sal_Int64 nVal = 0;
52         ::cppu::convertPropertyValue( nVal, aCnt.getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Size" ) ) ) );
53         nFileSize = (sal_uInt32)nVal;
54     }
55     catch( ::com::sun::star::uno::Exception& )
56     {
57 
58     }
59 };
60 
ImplGetName() const61 String ExSoundEntry::ImplGetName() const
62 {
63     INetURLObject aTmp( aSoundURL );
64     return aTmp.GetName();
65 }
66 
ImplGetExtension() const67 String ExSoundEntry::ImplGetExtension() const
68 {
69     INetURLObject aTmp( aSoundURL );
70     String aExtension( aTmp.GetExtension() );
71     if ( aExtension.Len() )
72         aExtension.Insert( (sal_Unicode)'.', 0 );
73     return aExtension;
74 }
75 
IsSameURL(const String & rURL) const76 sal_Bool ExSoundEntry::IsSameURL( const String& rURL ) const
77 {
78     return ( rURL == aSoundURL );
79 }
80 
GetSize(sal_uInt32 nId) const81 sal_uInt32 ExSoundEntry::GetSize( sal_uInt32 nId ) const
82 {
83     String aName( ImplGetName() );
84     String aExtension( ImplGetExtension() );
85 
86     sal_uInt32 nSize = 8;                           // SoundContainer Header
87     if ( aName.Len() )                              // String Atom          ( instance 0 - name of sound )
88         nSize += aName.Len() * 2 + 8;
89     if ( aExtension.Len() )                         // String Atom          ( instance 1 - extension of sound )
90         nSize += aExtension.Len() * 2 + 8;
91 
92     String aId( String::CreateFromInt32( nId ) );   // String Atom          ( instance 2 - reference id )
93     nSize += 2 * aId.Len() + 8;
94 
95     nSize += nFileSize + 8;                         // SoundData Atom
96 
97     return nSize;
98 }
99 
Write(SvStream & rSt,sal_uInt32 nId)100 void ExSoundEntry::Write( SvStream& rSt, sal_uInt32 nId )
101 {
102     try
103     {
104         ::ucbhelper::Content aCnt( aSoundURL,
105             ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >() );
106 
107         // create SoundContainer
108         rSt << (sal_uInt32)( ( EPP_Sound << 16 ) | 0xf ) << (sal_uInt32)( GetSize( nId ) - 8 );
109 
110         String aSoundName( ImplGetName() );
111         sal_uInt16 i, nSoundNameLen = aSoundName.Len();
112         if ( nSoundNameLen )
113         {
114             // name of sound ( instance 0 )
115             rSt << (sal_uInt32)( EPP_CString << 16 ) << (sal_uInt32)( nSoundNameLen * 2 );
116             for ( i = 0; i < nSoundNameLen; i++ )
117                 rSt << aSoundName.GetChar( i );
118         }
119         String aExtension( ImplGetExtension() );
120         sal_uInt32 nExtensionLen = aExtension.Len();
121         if ( nExtensionLen )
122         {
123             // extension of sound ( instance 1 )
124             rSt << (sal_uInt32)( ( EPP_CString << 16 ) | 16 ) << (sal_uInt32)( nExtensionLen * 2 );
125             for ( i = 0; i < nExtensionLen; i++ )
126                 rSt << aExtension.GetChar( i );
127         }
128         // id of sound ( instance 2 )
129         String aId( String::CreateFromInt32( nId ) );
130         sal_uInt32 nIdLen = aId.Len();
131         rSt << (sal_uInt32)( ( EPP_CString << 16 ) | 32 ) << (sal_uInt32)( nIdLen * 2 );
132         for ( i = 0; i < nIdLen; i++ )
133             rSt << aId.GetChar( i );
134 
135         rSt << (sal_uInt32)( EPP_SoundData << 16 ) << (sal_uInt32)( nFileSize );
136         sal_uInt32 nBytesLeft = nFileSize;
137         SvStream* pSourceFile = ::utl::UcbStreamHelper::CreateStream( aSoundURL, STREAM_READ );
138         if ( pSourceFile )
139         {
140             sal_uInt8* pBuf = new sal_uInt8[ 0x10000 ];   // 64 kB  Buffer
141             while ( nBytesLeft )
142             {
143                 sal_uInt32 nToDo = ( nBytesLeft > 0x10000 ) ? 0x10000 : nBytesLeft;
144                 pSourceFile->Read( pBuf, nToDo );
145                 rSt.Write( pBuf, nToDo );
146                 nBytesLeft -= nToDo;
147             }
148             delete pSourceFile;
149             delete[] pBuf;
150         }
151     }
152     catch( ::com::sun::star::uno::Exception& )
153     {
154 
155     }
156 }
157 
~ExSoundCollection()158 ExSoundCollection::~ExSoundCollection()
159 {
160     for( void* pPtr = List::First(); pPtr; pPtr = List::Next() )
161         delete (ExSoundEntry*)pPtr;
162 }
163 
GetId(const String & rString)164 sal_uInt32 ExSoundCollection::GetId( const String& rString )
165 {
166     sal_uInt32 nSoundId = 0;
167     if( rString.Len() )
168     {
169         const sal_uInt32 nSoundCount = Count();
170 
171         for( ; nSoundId < nSoundCount; nSoundId++ )
172             if( ImplGetByIndex( nSoundId )->IsSameURL( rString ) )
173                 break;
174         if ( nSoundId++ == nSoundCount )
175         {
176             ExSoundEntry* pEntry = new ExSoundEntry( rString );
177             if ( pEntry->GetFileSize() )
178                 List::Insert( pEntry, LIST_APPEND );
179             else
180             {
181                 nSoundId = 0;   // only insert sounds that are accessible
182                 delete pEntry;
183             }
184         }
185     }
186     return nSoundId;
187 }
188 
ImplGetByIndex(sal_uInt32 nIndex) const189 const ExSoundEntry* ExSoundCollection::ImplGetByIndex( sal_uInt32 nIndex ) const
190 {
191     return (ExSoundEntry*)List::GetObject( nIndex );
192 }
193 
GetSize() const194 sal_uInt32 ExSoundCollection::GetSize() const
195 {
196     sal_uInt32 nSize = 0;
197     sal_uInt32 i, nSoundCount = Count();
198     if ( nSoundCount )
199     {
200         nSize += 8 + 12;    // size of SoundCollectionContainerHeader + SoundCollAtom
201         for ( i = 0; i < nSoundCount; i++ )
202             nSize += ImplGetByIndex( i )->GetSize( i + 1 );
203     }
204     return nSize;
205 }
206 
Write(SvStream & rSt)207 void ExSoundCollection::Write( SvStream& rSt )
208 {
209     sal_uInt32 i, nSoundCount = Count();
210     if ( nSoundCount )
211     {
212         // create SoundCollection Container
213         rSt << (sal_uInt16)0xf << (sal_uInt16)EPP_SoundCollection << (sal_uInt32)( GetSize() - 8 );
214 
215         // create SoundCollAtom ( reference to the next free SoundId );
216         rSt << (sal_uInt32)( EPP_SoundCollAtom << 16 ) << (sal_uInt32)4 << nSoundCount;
217 
218         for ( i = 0; i < nSoundCount; i++ )
219             ((ExSoundEntry*)List::GetObject( i ))->Write( rSt, i + 1 );
220     }
221 }
222 
223 
224 } // namespace ppt;
225 
226