bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / filter / eppt / pptexsoundcollection.cxx
blob0e8958589891e501abcf7e328fe57f281d9fa4d1
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 <pptexsoundcollection.hxx>
21 #include "epptdef.hxx"
22 #include <tools/urlobj.hxx>
23 #include <ucbhelper/content.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <cppuhelper/proptypehlp.hxx>
26 #include <unotools/ucbstreamhelper.hxx>
28 namespace ppt
31 ExSoundEntry::ExSoundEntry(const OUString& rString)
32 : nFileSize(0)
33 , aSoundURL(rString)
35 try
37 ::ucbhelper::Content aCnt( aSoundURL,
38 ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
39 comphelper::getProcessComponentContext() );
40 sal_Int64 nVal = 0;
41 ::cppu::convertPropertyValue( nVal, aCnt.getPropertyValue("Size") );
42 nFileSize = (sal_uInt32)nVal;
44 catch( ::com::sun::star::uno::Exception& )
50 OUString ExSoundEntry::ImplGetName() const
52 INetURLObject aTmp( aSoundURL );
53 return aTmp.GetName();
56 OUString ExSoundEntry::ImplGetExtension() const
58 INetURLObject aTmp( aSoundURL );
59 OUString aExtension( aTmp.GetExtension() );
60 if ( !aExtension.isEmpty() )
62 aExtension = "." + aExtension;
64 return aExtension;
67 bool ExSoundEntry::IsSameURL(const OUString& rURL) const
69 return ( rURL == aSoundURL );
72 sal_uInt32 ExSoundEntry::GetSize( sal_uInt32 nId ) const
74 OUString aName( ImplGetName() );
75 OUString aExtension( ImplGetExtension() );
77 sal_uInt32 nSize = 8; // SoundContainer Header
78 if ( !aName.isEmpty() ) // String Atom ( instance 0 - name of sound )
79 nSize += aName.getLength() * 2 + 8;
80 if ( !aExtension.isEmpty() ) // String Atom ( instance 1 - extension of sound )
81 nSize += aExtension.getLength() * 2 + 8;
83 OUString aId( OUString::number(nId) ); // String Atom ( instance 2 - reference id )
84 nSize += 2 * aId.getLength() + 8;
86 nSize += nFileSize + 8; // SoundData Atom
88 return nSize;
91 void ExSoundEntry::Write( SvStream& rSt, sal_uInt32 nId ) const
93 try
95 ::ucbhelper::Content aCnt( aSoundURL,
96 ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
97 comphelper::getProcessComponentContext() );
99 // create SoundContainer
100 rSt.WriteUInt32( ( EPP_Sound << 16 ) | 0xf ).WriteUInt32( GetSize( nId ) - 8 );
102 OUString aSoundName( ImplGetName() );
103 sal_Int32 i, nSoundNameLen = aSoundName.getLength();
104 if ( nSoundNameLen )
106 // name of sound ( instance 0 )
107 rSt.WriteUInt32( EPP_CString << 16 ).WriteUInt32( nSoundNameLen * 2 );
108 for ( i = 0; i < nSoundNameLen; ++i )
109 rSt.WriteUInt16( aSoundName[i] );
111 OUString aExtension( ImplGetExtension() );
112 sal_Int32 nExtensionLen = aExtension.getLength();
113 if ( nExtensionLen )
115 // extension of sound ( instance 1 )
116 rSt.WriteUInt32( ( EPP_CString << 16 ) | 16 ).WriteUInt32( nExtensionLen * 2 );
117 for ( i = 0; i < nExtensionLen; ++i )
118 rSt.WriteUInt16( aExtension[i] );
120 // id of sound ( instance 2 )
121 OUString aId( OUString::number(nId ) );
122 sal_Int32 nIdLen = aId.getLength();
123 rSt.WriteUInt32( ( EPP_CString << 16 ) | 32 ).WriteUInt32( nIdLen * 2 );
124 for ( i = 0; i < nIdLen; ++i )
125 rSt.WriteUInt16( aId[i] );
127 rSt.WriteUInt32( EPP_SoundData << 16 ).WriteUInt32( nFileSize );
128 sal_uInt32 nBytesLeft = nFileSize;
129 SvStream* pSourceFile = ::utl::UcbStreamHelper::CreateStream( aSoundURL, StreamMode::READ );
130 if ( pSourceFile )
132 sal_uInt8* pBuf = new sal_uInt8[ 0x10000 ]; // 64 kB Buffer
133 while ( nBytesLeft )
135 sal_uInt32 nToDo = ( nBytesLeft > 0x10000 ) ? 0x10000 : nBytesLeft;
136 pSourceFile->Read( pBuf, nToDo );
137 rSt.Write( pBuf, nToDo );
138 nBytesLeft -= nToDo;
140 delete pSourceFile;
141 delete[] pBuf;
144 catch( ::com::sun::star::uno::Exception& )
150 sal_uInt32 ExSoundCollection::GetId(const OUString& rString)
152 sal_uInt32 nSoundId = 0;
153 if (!rString.isEmpty())
155 const sal_uInt32 nSoundCount = maEntries.size();
156 boost::ptr_vector<ExSoundEntry>::const_iterator iter;
158 for (iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++nSoundId)
160 if (iter->IsSameURL(rString))
161 break;
164 if ( nSoundId++ == nSoundCount )
166 ExSoundEntry* pEntry = new ExSoundEntry( rString );
167 if ( pEntry->GetFileSize() )
168 maEntries.push_back(pEntry);
169 else
171 nSoundId = 0; // only insert sounds that are accessible
172 delete pEntry;
176 return nSoundId;
179 sal_uInt32 ExSoundCollection::GetSize() const
181 sal_uInt32 nSize = 0;
182 if (!maEntries.empty())
184 nSize += 8 + 12; // size of SoundCollectionContainerHeader + SoundCollAtom
185 boost::ptr_vector<ExSoundEntry>::const_iterator iter;
186 sal_uInt32 i = 1;
187 for ( iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++i)
188 nSize += iter->GetSize(i);
190 return nSize;
193 void ExSoundCollection::Write( SvStream& rSt ) const
195 if (!maEntries.empty())
197 sal_uInt32 i = 1;
198 sal_uInt32 nSoundCount = maEntries.size();
200 // create SoundCollection Container
201 rSt.WriteUInt16( 0xf ).WriteUInt16( EPP_SoundCollection ).WriteUInt32( GetSize() - 8 );
203 // create SoundCollAtom ( reference to the next free SoundId );
204 rSt.WriteUInt32( EPP_SoundCollAtom << 16 ).WriteUInt32( 4 ).WriteUInt32( nSoundCount );
206 boost::ptr_vector<ExSoundEntry>::const_iterator iter;
207 for ( iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++i)
208 iter->Write(rSt,i);
212 } // namespace ppt;
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */