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