1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
31 ExSoundEntry::ExSoundEntry(const OUString
& rString
)
37 ::ucbhelper::Content
aCnt( aSoundURL
,
38 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>(),
39 comphelper::getProcessComponentContext() );
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 );
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
89 void ExSoundEntry::Write( SvStream
& rSt
, sal_uInt32 nId
) const
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();
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();
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
)
125 rSt
<< (sal_uInt32
)( EPP_SoundData
<< 16 ) << (sal_uInt32
)( nFileSize
);
126 sal_uInt32 nBytesLeft
= nFileSize
;
127 SvStream
* pSourceFile
= ::utl::UcbStreamHelper::CreateStream( aSoundURL
, STREAM_READ
);
130 sal_uInt8
* pBuf
= new sal_uInt8
[ 0x10000 ]; // 64 kB Buffer
133 sal_uInt32 nToDo
= ( nBytesLeft
> 0x10000 ) ? 0x10000 : nBytesLeft
;
134 pSourceFile
->Read( pBuf
, nToDo
);
135 rSt
.Write( pBuf
, nToDo
);
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
))
162 if ( nSoundId
++ == nSoundCount
)
164 ExSoundEntry
* pEntry
= new ExSoundEntry( rString
);
165 if ( pEntry
->GetFileSize() )
166 maEntries
.push_back(pEntry
);
169 nSoundId
= 0; // only insert sounds that are accessible
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
;
185 for ( iter
= maEntries
.begin(); iter
!= maEntries
.end(); ++iter
, ++i
)
186 nSize
+= iter
->GetSize(i
);
191 void ExSoundCollection::Write( SvStream
& rSt
) const
193 if (!maEntries
.empty())
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
)
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */