LibreOffice Module sd (master) 1
pptexsoundcollection.cxx
Go to the documentation of this file.
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 <memory>
22#include "epptdef.hxx"
23#include <tools/stream.hxx>
24#include <tools/urlobj.hxx>
25#include <ucbhelper/content.hxx>
28#include <utility>
29
30namespace ppt
31{
32
34 : nFileSize(0)
35 , aSoundURL(std::move(aString))
36{
37 try
38 {
40 css::uno::Reference< css::ucb::XCommandEnvironment >(),
42 sal_Int64 nVal = 0;
43 aCnt.getPropertyValue("Size") >>= nVal;
44 nFileSize = static_cast<sal_uInt32>(nVal);
45 }
46 catch( css::uno::Exception& )
47 {
48
49 }
50};
51
53{
55 return aTmp.GetLastName();
56}
57
59{
61 OUString aExtension(aTmp.GetFileExtension());
62 if ( !aExtension.isEmpty() )
63 {
64 aExtension = "." + aExtension;
65 }
66 return aExtension;
67}
68
69bool ExSoundEntry::IsSameURL(std::u16string_view rURL) const
70{
71 return ( rURL == aSoundURL );
72}
73
74sal_uInt32 ExSoundEntry::GetSize( sal_uInt32 nId ) const
75{
76 OUString aName( ImplGetName() );
77 OUString aExtension( ImplGetExtension() );
78
79 sal_uInt32 nSize = 8; // SoundContainer Header
80 if ( !aName.isEmpty() ) // String Atom ( instance 0 - name of sound )
81 nSize += aName.getLength() * 2 + 8;
82 if ( !aExtension.isEmpty() ) // String Atom ( instance 1 - extension of sound )
83 nSize += aExtension.getLength() * 2 + 8;
84
85 OUString aId( OUString::number(nId) ); // String Atom ( instance 2 - reference id )
86 nSize += 2 * aId.getLength() + 8;
87
88 nSize += nFileSize + 8; // SoundData Atom
89
90 return nSize;
91}
92
93void ExSoundEntry::Write( SvStream& rSt, sal_uInt32 nId ) const
94{
95 try
96 {
97 ::ucbhelper::Content aLoadContentIfExists( aSoundURL,
98 css::uno::Reference< css::ucb::XCommandEnvironment >(),
100
101 // create SoundContainer
102 rSt.WriteUInt32( ( EPP_Sound << 16 ) | 0xf ).WriteUInt32( GetSize( nId ) - 8 );
103
104 OUString aSoundName( ImplGetName() );
105 sal_Int32 i, nSoundNameLen = aSoundName.getLength();
106 if ( nSoundNameLen )
107 {
108 // name of sound ( instance 0 )
109 rSt.WriteUInt32( EPP_CString << 16 ).WriteUInt32( nSoundNameLen * 2 );
110 for ( i = 0; i < nSoundNameLen; ++i )
111 rSt.WriteUInt16( aSoundName[i] );
112 }
113 OUString aExtension( ImplGetExtension() );
114 sal_Int32 nExtensionLen = aExtension.getLength();
115 if ( nExtensionLen )
116 {
117 // extension of sound ( instance 1 )
118 rSt.WriteUInt32( ( EPP_CString << 16 ) | 16 ).WriteUInt32( nExtensionLen * 2 );
119 for ( i = 0; i < nExtensionLen; ++i )
120 rSt.WriteUInt16( aExtension[i] );
121 }
122 // id of sound ( instance 2 )
123 OUString aId( OUString::number(nId ) );
124 sal_Int32 nIdLen = aId.getLength();
125 rSt.WriteUInt32( ( EPP_CString << 16 ) | 32 ).WriteUInt32( nIdLen * 2 );
126 for ( i = 0; i < nIdLen; ++i )
127 rSt.WriteUInt16( aId[i] );
128
130 sal_uInt32 nBytesLeft = nFileSize;
131 std::unique_ptr<SvStream> pSourceFile = ::utl::UcbStreamHelper::CreateStream( aSoundURL, StreamMode::READ );
132 if ( pSourceFile )
133 {
134 std::unique_ptr<sal_uInt8[]> pBuf( new sal_uInt8[ 0x10000 ] ); // 64 kB Buffer
135 while ( nBytesLeft )
136 {
137 sal_uInt32 nToDo = std::min<sal_uInt32>( nBytesLeft, 0x10000 );
138 pSourceFile->ReadBytes(pBuf.get(), nToDo);
139 rSt.WriteBytes(pBuf.get(), nToDo);
140 nBytesLeft -= nToDo;
141 }
142 }
143 }
144 catch( css::uno::Exception& )
145 {
146
147 }
148}
149
150sal_uInt32 ExSoundCollection::GetId(const OUString& rString)
151{
152 sal_uInt32 nSoundId = 0;
153 if (!rString.isEmpty())
154 {
155 const sal_uInt32 nSoundCount = maEntries.size();
156
157 auto iter = std::find_if(maEntries.begin(), maEntries.end(),
158 [&rString](const ExSoundEntry& rEntry) { return rEntry.IsSameURL(rString); });
159 nSoundId = static_cast<sal_uInt32>(std::distance(maEntries.begin(), iter));
160
161 if ( nSoundId++ == nSoundCount )
162 {
163 ExSoundEntry aEntry( rString );
164 if ( aEntry.GetFileSize() )
165 maEntries.push_back(aEntry);
166 else
167 {
168 nSoundId = 0; // only insert sounds that are accessible
169 }
170 }
171 }
172 return nSoundId;
173}
174
176{
177 sal_uInt32 nSize = 0;
178 if (!maEntries.empty())
179 {
180 nSize += 8 + 12; // size of SoundCollectionContainerHeader + SoundCollAtom
181 sal_uInt32 i = 1;
182 for ( const auto& rEntry : maEntries )
183 {
184 nSize += rEntry.GetSize(i);
185 ++i;
186 }
187 }
188 return nSize;
189}
190
192{
193 if (maEntries.empty())
194 return;
195
196 sal_uInt32 i = 1;
197 sal_uInt32 nSoundCount = maEntries.size();
198
199 // create SoundCollection Container
201
202 // create SoundCollAtom ( reference to the next free SoundId );
203 rSt.WriteUInt32( EPP_SoundCollAtom << 16 ).WriteUInt32( 4 ).WriteUInt32( nSoundCount );
204
205 for ( const auto& rEntry : maEntries )
206 {
207 rEntry.Write(rSt,i);
208 ++i;
209 }
210}
211
212} // namespace ppt;
213
214/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetFileExtension() const
OUString GetLastName(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
std::size_t WriteBytes(const void *pData, std::size_t nSize)
SvStream & WriteUInt16(sal_uInt16 nUInt16)
SvStream & WriteUInt32(sal_uInt32 nUInt32)
sal_uInt32 GetId(const OUString &)
std::vector< ExSoundEntry > maEntries
void Write(SvStream &rSt) const
void Write(SvStream &rSt, sal_uInt32 nId) const
ExSoundEntry(OUString aSoundURL)
sal_uInt32 GetFileSize() const
sal_uInt32 GetSize(sal_uInt32 nId) const
OUString ImplGetName() const
bool IsSameURL(std::u16string_view rURL) const
OUString ImplGetExtension() const
css::uno::Any getPropertyValue(const OUString &rPropertyName)
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
#define EPP_SoundCollection
Definition: epptdef.hxx:50
#define EPP_CString
Definition: epptdef.hxx:79
#define EPP_SoundCollAtom
Definition: epptdef.hxx:51
#define EPP_Sound
Definition: epptdef.hxx:52
#define EPP_SoundData
Definition: epptdef.hxx:53
OUString aName
Reference< XComponentContext > getProcessComponentContext()
int i
sal_Int16 nId
unsigned char sal_uInt8