Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
27 :
28 : namespace ppt
29 : {
30 :
31 0 : ExSoundEntry::ExSoundEntry(const OUString& rString)
32 : : nFileSize(0)
33 0 : , aSoundURL(rString)
34 : {
35 : try
36 : {
37 : ::ucbhelper::Content aCnt( aSoundURL,
38 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
39 0 : comphelper::getProcessComponentContext() );
40 0 : sal_Int64 nVal = 0;
41 0 : ::cppu::convertPropertyValue( nVal, aCnt.getPropertyValue("Size") );
42 0 : nFileSize = (sal_uInt32)nVal;
43 : }
44 0 : catch( ::com::sun::star::uno::Exception& )
45 : {
46 :
47 : }
48 0 : };
49 :
50 0 : OUString ExSoundEntry::ImplGetName() const
51 : {
52 0 : INetURLObject aTmp( aSoundURL );
53 0 : return aTmp.GetName();
54 : }
55 :
56 0 : OUString ExSoundEntry::ImplGetExtension() const
57 : {
58 0 : INetURLObject aTmp( aSoundURL );
59 0 : OUString aExtension( aTmp.GetExtension() );
60 0 : if ( !aExtension.isEmpty() )
61 : {
62 0 : aExtension = "." + aExtension;
63 : }
64 0 : return aExtension;
65 : }
66 :
67 0 : bool ExSoundEntry::IsSameURL(const OUString& rURL) const
68 : {
69 0 : return ( rURL == aSoundURL );
70 : }
71 :
72 0 : sal_uInt32 ExSoundEntry::GetSize( sal_uInt32 nId ) const
73 : {
74 0 : OUString aName( ImplGetName() );
75 0 : OUString aExtension( ImplGetExtension() );
76 :
77 0 : sal_uInt32 nSize = 8; // SoundContainer Header
78 0 : if ( !aName.isEmpty() ) // String Atom ( instance 0 - name of sound )
79 0 : nSize += aName.getLength() * 2 + 8;
80 0 : if ( !aExtension.isEmpty() ) // String Atom ( instance 1 - extension of sound )
81 0 : nSize += aExtension.getLength() * 2 + 8;
82 :
83 0 : OUString aId( OUString::number(nId) ); // String Atom ( instance 2 - reference id )
84 0 : nSize += 2 * aId.getLength() + 8;
85 :
86 0 : nSize += nFileSize + 8; // SoundData Atom
87 :
88 0 : return nSize;
89 : }
90 :
91 0 : void ExSoundEntry::Write( SvStream& rSt, sal_uInt32 nId ) const
92 : {
93 : try
94 : {
95 : ::ucbhelper::Content aCnt( aSoundURL,
96 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
97 0 : comphelper::getProcessComponentContext() );
98 :
99 : // create SoundContainer
100 0 : rSt.WriteUInt32( ( EPP_Sound << 16 ) | 0xf ).WriteUInt32( GetSize( nId ) - 8 );
101 :
102 0 : OUString aSoundName( ImplGetName() );
103 0 : sal_Int32 i, nSoundNameLen = aSoundName.getLength();
104 0 : if ( nSoundNameLen )
105 : {
106 : // name of sound ( instance 0 )
107 0 : rSt.WriteUInt32( EPP_CString << 16 ).WriteUInt32( nSoundNameLen * 2 );
108 0 : for ( i = 0; i < nSoundNameLen; ++i )
109 0 : rSt.WriteUInt16( aSoundName[i] );
110 : }
111 0 : OUString aExtension( ImplGetExtension() );
112 0 : sal_Int32 nExtensionLen = aExtension.getLength();
113 0 : if ( nExtensionLen )
114 : {
115 : // extension of sound ( instance 1 )
116 0 : rSt.WriteUInt32( ( EPP_CString << 16 ) | 16 ).WriteUInt32( nExtensionLen * 2 );
117 0 : for ( i = 0; i < nExtensionLen; ++i )
118 0 : rSt.WriteUInt16( aExtension[i] );
119 : }
120 : // id of sound ( instance 2 )
121 0 : OUString aId( OUString::number(nId ) );
122 0 : sal_Int32 nIdLen = aId.getLength();
123 0 : rSt.WriteUInt32( ( EPP_CString << 16 ) | 32 ).WriteUInt32( nIdLen * 2 );
124 0 : for ( i = 0; i < nIdLen; ++i )
125 0 : rSt.WriteUInt16( aId[i] );
126 :
127 0 : rSt.WriteUInt32( EPP_SoundData << 16 ).WriteUInt32( nFileSize );
128 0 : sal_uInt32 nBytesLeft = nFileSize;
129 0 : SvStream* pSourceFile = ::utl::UcbStreamHelper::CreateStream( aSoundURL, StreamMode::READ );
130 0 : if ( pSourceFile )
131 : {
132 0 : sal_uInt8* pBuf = new sal_uInt8[ 0x10000 ]; // 64 kB Buffer
133 0 : while ( nBytesLeft )
134 : {
135 0 : sal_uInt32 nToDo = ( nBytesLeft > 0x10000 ) ? 0x10000 : nBytesLeft;
136 0 : pSourceFile->Read( pBuf, nToDo );
137 0 : rSt.Write( pBuf, nToDo );
138 0 : nBytesLeft -= nToDo;
139 : }
140 0 : delete pSourceFile;
141 0 : delete[] pBuf;
142 0 : }
143 : }
144 0 : catch( ::com::sun::star::uno::Exception& )
145 : {
146 :
147 : }
148 0 : }
149 :
150 5 : sal_uInt32 ExSoundCollection::GetId(const OUString& rString)
151 : {
152 5 : sal_uInt32 nSoundId = 0;
153 5 : if (!rString.isEmpty())
154 : {
155 0 : const sal_uInt32 nSoundCount = maEntries.size();
156 0 : boost::ptr_vector<ExSoundEntry>::const_iterator iter;
157 :
158 0 : for (iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++nSoundId)
159 : {
160 0 : if (iter->IsSameURL(rString))
161 0 : break;
162 : }
163 :
164 0 : if ( nSoundId++ == nSoundCount )
165 : {
166 0 : ExSoundEntry* pEntry = new ExSoundEntry( rString );
167 0 : if ( pEntry->GetFileSize() )
168 0 : maEntries.push_back(pEntry);
169 : else
170 : {
171 0 : nSoundId = 0; // only insert sounds that are accessible
172 0 : delete pEntry;
173 : }
174 : }
175 : }
176 5 : return nSoundId;
177 : }
178 :
179 3 : sal_uInt32 ExSoundCollection::GetSize() const
180 : {
181 3 : sal_uInt32 nSize = 0;
182 3 : if (!maEntries.empty())
183 : {
184 0 : nSize += 8 + 12; // size of SoundCollectionContainerHeader + SoundCollAtom
185 0 : boost::ptr_vector<ExSoundEntry>::const_iterator iter;
186 0 : sal_uInt32 i = 1;
187 0 : for ( iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++i)
188 0 : nSize += iter->GetSize(i);
189 : }
190 3 : return nSize;
191 : }
192 :
193 3 : void ExSoundCollection::Write( SvStream& rSt ) const
194 : {
195 3 : if (!maEntries.empty())
196 : {
197 0 : sal_uInt32 i = 1;
198 0 : sal_uInt32 nSoundCount = maEntries.size();
199 :
200 : // create SoundCollection Container
201 0 : rSt.WriteUInt16( 0xf ).WriteUInt16( EPP_SoundCollection ).WriteUInt32( GetSize() - 8 );
202 :
203 : // create SoundCollAtom ( reference to the next free SoundId );
204 0 : rSt.WriteUInt32( EPP_SoundCollAtom << 16 ).WriteUInt32( 4 ).WriteUInt32( nSoundCount );
205 :
206 0 : boost::ptr_vector<ExSoundEntry>::const_iterator iter;
207 0 : for ( iter = maEntries.begin(); iter != maEntries.end(); ++iter, ++i)
208 0 : iter->Write(rSt,i);
209 : }
210 3 : }
211 :
212 : } // namespace ppt;
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|