bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / filter / eppt / pptexsoundcollection.cxx
blob1019d4daf25548562802425b038ef7aee36c9e06
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 <memory>
21 #include "pptexsoundcollection.hxx"
22 #include "epptdef.hxx"
23 #include <tools/stream.hxx>
24 #include <tools/urlobj.hxx>
25 #include <ucbhelper/content.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <unotools/ucbstreamhelper.hxx>
29 namespace ppt
32 ExSoundEntry::ExSoundEntry(const OUString& rString)
33 : nFileSize(0)
34 , aSoundURL(rString)
36 try
38 ::ucbhelper::Content aCnt( aSoundURL,
39 css::uno::Reference< css::ucb::XCommandEnvironment >(),
40 comphelper::getProcessComponentContext() );
41 sal_Int64 nVal = 0;
42 aCnt.getPropertyValue("Size") >>= nVal;
43 nFileSize = static_cast<sal_uInt32>(nVal);
45 catch( css::uno::Exception& )
51 OUString ExSoundEntry::ImplGetName() const
53 INetURLObject aTmp( aSoundURL );
54 return aTmp.GetName();
57 OUString ExSoundEntry::ImplGetExtension() const
59 INetURLObject aTmp( aSoundURL );
60 OUString aExtension( aTmp.GetExtension() );
61 if ( !aExtension.isEmpty() )
63 aExtension = "." + aExtension;
65 return aExtension;
68 bool ExSoundEntry::IsSameURL(const OUString& rURL) const
70 return ( rURL == aSoundURL );
73 sal_uInt32 ExSoundEntry::GetSize( sal_uInt32 nId ) const
75 OUString aName( ImplGetName() );
76 OUString aExtension( ImplGetExtension() );
78 sal_uInt32 nSize = 8; // SoundContainer Header
79 if ( !aName.isEmpty() ) // String Atom ( instance 0 - name of sound )
80 nSize += aName.getLength() * 2 + 8;
81 if ( !aExtension.isEmpty() ) // String Atom ( instance 1 - extension of sound )
82 nSize += aExtension.getLength() * 2 + 8;
84 OUString aId( OUString::number(nId) ); // String Atom ( instance 2 - reference id )
85 nSize += 2 * aId.getLength() + 8;
87 nSize += nFileSize + 8; // SoundData Atom
89 return nSize;
92 void ExSoundEntry::Write( SvStream& rSt, sal_uInt32 nId ) const
94 try
96 ::ucbhelper::Content aCnt( aSoundURL,
97 css::uno::Reference< css::ucb::XCommandEnvironment >(),
98 comphelper::getProcessComponentContext() );
100 // create SoundContainer
101 rSt.WriteUInt32( ( EPP_Sound << 16 ) | 0xf ).WriteUInt32( GetSize( nId ) - 8 );
103 OUString aSoundName( ImplGetName() );
104 sal_Int32 i, nSoundNameLen = aSoundName.getLength();
105 if ( nSoundNameLen )
107 // name of sound ( instance 0 )
108 rSt.WriteUInt32( EPP_CString << 16 ).WriteUInt32( nSoundNameLen * 2 );
109 for ( i = 0; i < nSoundNameLen; ++i )
110 rSt.WriteUInt16( aSoundName[i] );
112 OUString aExtension( ImplGetExtension() );
113 sal_Int32 nExtensionLen = aExtension.getLength();
114 if ( nExtensionLen )
116 // extension of sound ( instance 1 )
117 rSt.WriteUInt32( ( EPP_CString << 16 ) | 16 ).WriteUInt32( nExtensionLen * 2 );
118 for ( i = 0; i < nExtensionLen; ++i )
119 rSt.WriteUInt16( aExtension[i] );
121 // id of sound ( instance 2 )
122 OUString aId( OUString::number(nId ) );
123 sal_Int32 nIdLen = aId.getLength();
124 rSt.WriteUInt32( ( EPP_CString << 16 ) | 32 ).WriteUInt32( nIdLen * 2 );
125 for ( i = 0; i < nIdLen; ++i )
126 rSt.WriteUInt16( aId[i] );
128 rSt.WriteUInt32( EPP_SoundData << 16 ).WriteUInt32( nFileSize );
129 sal_uInt32 nBytesLeft = nFileSize;
130 std::unique_ptr<SvStream> pSourceFile = ::utl::UcbStreamHelper::CreateStream( aSoundURL, StreamMode::READ );
131 if ( pSourceFile )
133 std::unique_ptr<sal_uInt8[]> pBuf( new sal_uInt8[ 0x10000 ] ); // 64 kB Buffer
134 while ( nBytesLeft )
136 sal_uInt32 nToDo = std::min<sal_uInt32>( nBytesLeft, 0x10000 );
137 pSourceFile->ReadBytes(pBuf.get(), nToDo);
138 rSt.WriteBytes(pBuf.get(), nToDo);
139 nBytesLeft -= nToDo;
143 catch( css::uno::Exception& )
149 sal_uInt32 ExSoundCollection::GetId(const OUString& rString)
151 sal_uInt32 nSoundId = 0;
152 if (!rString.isEmpty())
154 const sal_uInt32 nSoundCount = maEntries.size();
156 auto iter = std::find_if(maEntries.begin(), maEntries.end(),
157 [&rString](const ExSoundEntry& rEntry) { return rEntry.IsSameURL(rString); });
158 nSoundId = static_cast<sal_uInt32>(std::distance(maEntries.begin(), iter));
160 if ( nSoundId++ == nSoundCount )
162 ExSoundEntry aEntry( rString );
163 if ( aEntry.GetFileSize() )
164 maEntries.push_back(aEntry);
165 else
167 nSoundId = 0; // only insert sounds that are accessible
171 return nSoundId;
174 sal_uInt32 ExSoundCollection::GetSize() const
176 sal_uInt32 nSize = 0;
177 if (!maEntries.empty())
179 nSize += 8 + 12; // size of SoundCollectionContainerHeader + SoundCollAtom
180 sal_uInt32 i = 1;
181 for ( const auto& rEntry : maEntries )
183 nSize += rEntry.GetSize(i);
184 ++i;
187 return nSize;
190 void ExSoundCollection::Write( SvStream& rSt ) const
192 if (maEntries.empty())
193 return;
195 sal_uInt32 i = 1;
196 sal_uInt32 nSoundCount = maEntries.size();
198 // create SoundCollection Container
199 rSt.WriteUInt16( 0xf ).WriteUInt16( EPP_SoundCollection ).WriteUInt32( GetSize() - 8 );
201 // create SoundCollAtom ( reference to the next free SoundId );
202 rSt.WriteUInt32( EPP_SoundCollAtom << 16 ).WriteUInt32( 4 ).WriteUInt32( nSoundCount );
204 for ( const auto& rEntry : maEntries )
206 rEntry.Write(rSt,i);
207 ++i;
211 } // namespace ppt;
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */