fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / sd / source / filter / eppt / pptexsoundcollection.cxx
blobefa58fdbc02b6f5942f5f7bb15be56f701094be7
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( OUString( "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 String aExtension( aTmp.GetExtension() );
60 if ( aExtension.Len() )
61 aExtension.Insert( (sal_Unicode)'.', 0 );
62 return aExtension;
65 sal_Bool ExSoundEntry::IsSameURL(const OUString& rURL) const
67 return ( rURL == aSoundURL );
70 sal_uInt32 ExSoundEntry::GetSize( sal_uInt32 nId ) const
72 OUString aName( ImplGetName() );
73 OUString aExtension( ImplGetExtension() );
75 sal_uInt32 nSize = 8; // SoundContainer Header
76 if ( !aName.isEmpty() ) // String Atom ( instance 0 - name of sound )
77 nSize += aName.getLength() * 2 + 8;
78 if ( !aExtension.isEmpty() ) // String Atom ( instance 1 - extension of sound )
79 nSize += aExtension.getLength() * 2 + 8;
81 OUString aId( OUString::number(nId) ); // String Atom ( instance 2 - reference id )
82 nSize += 2 * aId.getLength() + 8;
84 nSize += nFileSize + 8; // SoundData Atom
86 return nSize;
89 void ExSoundEntry::Write( SvStream& rSt, sal_uInt32 nId ) const
91 try
93 ::ucbhelper::Content aCnt( aSoundURL,
94 ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
95 comphelper::getProcessComponentContext() );
97 // create SoundContainer
98 rSt << (sal_uInt32)( ( EPP_Sound << 16 ) | 0xf ) << (sal_uInt32)( GetSize( nId ) - 8 );
100 OUString aSoundName( ImplGetName() );
101 sal_Int32 i, nSoundNameLen = aSoundName.getLength();
102 if ( nSoundNameLen )
104 // name of sound ( instance 0 )
105 rSt << (sal_uInt32)( EPP_CString << 16 ) << (sal_uInt32)( nSoundNameLen * 2 );
106 for ( i = 0; i < nSoundNameLen; ++i )
107 rSt << aSoundName[i];
109 OUString aExtension( ImplGetExtension() );
110 sal_Int32 nExtensionLen = aExtension.getLength();
111 if ( nExtensionLen )
113 // extension of sound ( instance 1 )
114 rSt << (sal_uInt32)( ( EPP_CString << 16 ) | 16 ) << (sal_uInt32)( nExtensionLen * 2 );
115 for ( i = 0; i < nExtensionLen; ++i )
116 rSt << aExtension[i];
118 // id of sound ( instance 2 )
119 OUString aId( OUString::number(nId ) );
120 sal_Int32 nIdLen = aId.getLength();
121 rSt << (sal_uInt32)( ( EPP_CString << 16 ) | 32 ) << (sal_uInt32)( nIdLen * 2 );
122 for ( i = 0; i < nIdLen; ++i )
123 rSt << aId[i];
125 rSt << (sal_uInt32)( EPP_SoundData << 16 ) << (sal_uInt32)( nFileSize );
126 sal_uInt32 nBytesLeft = nFileSize;
127 SvStream* pSourceFile = ::utl::UcbStreamHelper::CreateStream( aSoundURL, STREAM_READ );
128 if ( pSourceFile )
130 sal_uInt8* pBuf = new sal_uInt8[ 0x10000 ]; // 64 kB Buffer
131 while ( nBytesLeft )
133 sal_uInt32 nToDo = ( nBytesLeft > 0x10000 ) ? 0x10000 : nBytesLeft;
134 pSourceFile->Read( pBuf, nToDo );
135 rSt.Write( pBuf, nToDo );
136 nBytesLeft -= nToDo;
138 delete pSourceFile;
139 delete[] pBuf;
142 catch( ::com::sun::star::uno::Exception& )
148 sal_uInt32 ExSoundCollection::GetId(const OUString& rString)
150 sal_uInt32 nSoundId = 0;
151 if (!rString.isEmpty())
153 const sal_uInt32 nSoundCount = maEntries.size();
154 boost::ptr_vector<ExSoundEntry>::const_iterator iter;
156 for (iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++nSoundId)
158 if (iter->IsSameURL(rString))
159 break;
162 if ( nSoundId++ == nSoundCount )
164 ExSoundEntry* pEntry = new ExSoundEntry( rString );
165 if ( pEntry->GetFileSize() )
166 maEntries.push_back(pEntry);
167 else
169 nSoundId = 0; // only insert sounds that are accessible
170 delete pEntry;
174 return nSoundId;
177 sal_uInt32 ExSoundCollection::GetSize() const
179 sal_uInt32 nSize = 0;
180 if (!maEntries.empty())
182 nSize += 8 + 12; // size of SoundCollectionContainerHeader + SoundCollAtom
183 boost::ptr_vector<ExSoundEntry>::const_iterator iter;
184 sal_uInt32 i = 1;
185 for ( iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++i)
186 nSize += iter->GetSize(i);
188 return nSize;
191 void ExSoundCollection::Write( SvStream& rSt ) const
193 if (!maEntries.empty())
195 sal_uInt32 i = 1;
196 sal_uInt32 nSoundCount = maEntries.size();
198 // create SoundCollection Container
199 rSt << (sal_uInt16)0xf << (sal_uInt16)EPP_SoundCollection << (sal_uInt32)( GetSize() - 8 );
201 // create SoundCollAtom ( reference to the next free SoundId );
202 rSt << (sal_uInt32)( EPP_SoundCollAtom << 16 ) << (sal_uInt32)4 << nSoundCount;
204 boost::ptr_vector<ExSoundEntry>::const_iterator iter;
205 for ( iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++i)
206 iter->Write(rSt,i);
211 } // namespace ppt;
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */