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