/src/libreoffice/avmedia/source/framework/mediaitem.cxx
Line | Count | Source |
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 <avmedia/mediaitem.hxx> |
21 | | |
22 | | #include <com/sun/star/uno/Sequence.hxx> |
23 | | |
24 | | #include <com/sun/star/beans/XPropertySet.hpp> |
25 | | #include <com/sun/star/embed/ElementModes.hpp> |
26 | | #include <com/sun/star/embed/XTransactedObject.hpp> |
27 | | #include <com/sun/star/frame/XModel.hpp> |
28 | | #include <com/sun/star/document/XStorageBasedDocument.hpp> |
29 | | #include <com/sun/star/media/ZoomLevel.hpp> |
30 | | #include <com/sun/star/ucb/XCommandEnvironment.hpp> |
31 | | #include <com/sun/star/uri/UriReferenceFactory.hpp> |
32 | | #include <com/sun/star/uri/XUriReference.hpp> |
33 | | #include <com/sun/star/uri/XUriReferenceFactory.hpp> |
34 | | #include <com/sun/star/text/GraphicCrop.hpp> |
35 | | |
36 | | #include <sal/log.hxx> |
37 | | |
38 | | #include <ucbhelper/content.hxx> |
39 | | |
40 | | #include <comphelper/mediamimetype.hxx> |
41 | | #include <comphelper/processfactory.hxx> |
42 | | #include <comphelper/storagehelper.hxx> |
43 | | #include <osl/file.hxx> |
44 | | #include <comphelper/diagnose_ex.hxx> |
45 | | #include <vcl/graph.hxx> |
46 | | |
47 | | using namespace ::com::sun::star; |
48 | | |
49 | | namespace avmedia |
50 | | { |
51 | | |
52 | 0 | SfxPoolItem* MediaItem::CreateDefault() { return new MediaItem; } |
53 | | |
54 | | struct MediaItem::Impl |
55 | | { |
56 | | OUString m_URL; |
57 | | OUString m_TempFileURL; |
58 | | OUString m_FallbackURL; |
59 | | OUString m_Referer; |
60 | | OUString m_sMimeType; |
61 | | AVMediaSetMask m_nMaskSet; |
62 | | MediaState m_eState; |
63 | | double m_fTime; |
64 | | double m_fDuration; |
65 | | sal_Int16 m_nVolumeDB; |
66 | | bool m_bLoop; |
67 | | bool m_bMute; |
68 | | css::media::ZoomLevel m_eZoom; |
69 | | Graphic m_aGraphic; |
70 | | text::GraphicCrop m_aCrop; |
71 | | |
72 | | explicit Impl(AVMediaSetMask nMaskSet) |
73 | 0 | : m_nMaskSet( nMaskSet ) |
74 | 0 | , m_eState( MediaState::Stop ) |
75 | 0 | , m_fTime( 0.0 ) |
76 | 0 | , m_fDuration( 0.0 ) |
77 | 0 | , m_nVolumeDB( 0 ) |
78 | 0 | , m_bLoop( false ) |
79 | 0 | , m_bMute( false ) |
80 | 0 | , m_eZoom( css::media::ZoomLevel_NOT_AVAILABLE ) |
81 | 0 | { |
82 | 0 | } |
83 | | }; |
84 | | |
85 | | |
86 | | MediaItem::MediaItem( sal_uInt16 i_nWhich, AVMediaSetMask nMaskSet ) |
87 | 0 | : SfxPoolItem( i_nWhich ) |
88 | 0 | , m_pImpl( new Impl(nMaskSet) ) |
89 | 0 | { |
90 | 0 | } |
91 | | |
92 | | |
93 | | MediaItem::MediaItem( const MediaItem& rItem ) |
94 | 0 | : SfxPoolItem( rItem ) |
95 | 0 | , m_pImpl( new Impl(*rItem.m_pImpl) ) |
96 | 0 | { |
97 | 0 | } |
98 | | |
99 | | |
100 | | MediaItem::~MediaItem() |
101 | 0 | { |
102 | 0 | } |
103 | | |
104 | | |
105 | | bool MediaItem::operator==( const SfxPoolItem& rItem ) const |
106 | 0 | { |
107 | 0 | assert( SfxPoolItem::operator==(rItem)); |
108 | 0 | MediaItem const& rOther(static_cast< const MediaItem& >(rItem)); |
109 | 0 | return m_pImpl->m_nMaskSet == rOther.m_pImpl->m_nMaskSet |
110 | 0 | && m_pImpl->m_URL == rOther.m_pImpl->m_URL |
111 | 0 | && m_pImpl->m_FallbackURL == rOther.m_pImpl->m_FallbackURL |
112 | 0 | && m_pImpl->m_Referer == rOther.m_pImpl->m_Referer |
113 | 0 | && m_pImpl->m_sMimeType == rOther.m_pImpl->m_sMimeType |
114 | 0 | && m_pImpl->m_aGraphic == rOther.m_pImpl->m_aGraphic |
115 | 0 | && m_pImpl->m_aCrop == rOther.m_pImpl->m_aCrop |
116 | 0 | && m_pImpl->m_eState == rOther.m_pImpl->m_eState |
117 | 0 | && m_pImpl->m_fDuration == rOther.m_pImpl->m_fDuration |
118 | 0 | && m_pImpl->m_fTime == rOther.m_pImpl->m_fTime |
119 | 0 | && m_pImpl->m_nVolumeDB == rOther.m_pImpl->m_nVolumeDB |
120 | 0 | && m_pImpl->m_bLoop == rOther.m_pImpl->m_bLoop |
121 | 0 | && m_pImpl->m_bMute == rOther.m_pImpl->m_bMute |
122 | 0 | && m_pImpl->m_eZoom == rOther.m_pImpl->m_eZoom; |
123 | 0 | } |
124 | | |
125 | | MediaItem* MediaItem::Clone( SfxItemPool* ) const |
126 | 0 | { |
127 | 0 | return new MediaItem( *this ); |
128 | 0 | } |
129 | | |
130 | | bool MediaItem::GetPresentation( SfxItemPresentation, |
131 | | MapUnit, |
132 | | MapUnit, |
133 | | OUString& rText, |
134 | | const IntlWrapper& ) const |
135 | 0 | { |
136 | 0 | rText.clear(); |
137 | 0 | return false; |
138 | 0 | } |
139 | | |
140 | | bool MediaItem::QueryValue( css::uno::Any& rVal, sal_uInt8 ) const |
141 | 0 | { |
142 | 0 | uno::Sequence< uno::Any > aSeq{ uno::Any(m_pImpl->m_URL), |
143 | 0 | uno::Any(static_cast<sal_uInt32>(m_pImpl->m_nMaskSet)), |
144 | 0 | uno::Any(static_cast< sal_Int32 >( m_pImpl->m_eState )), |
145 | 0 | uno::Any(m_pImpl->m_fTime), |
146 | 0 | uno::Any(m_pImpl->m_fDuration), |
147 | 0 | uno::Any(m_pImpl->m_nVolumeDB), |
148 | 0 | uno::Any(m_pImpl->m_bLoop), |
149 | 0 | uno::Any(m_pImpl->m_bMute), |
150 | 0 | uno::Any(m_pImpl->m_eZoom), |
151 | 0 | uno::Any(m_pImpl->m_sMimeType) }; |
152 | |
|
153 | 0 | rVal <<= aSeq; |
154 | |
|
155 | 0 | return true; |
156 | 0 | } |
157 | | |
158 | | |
159 | | bool MediaItem::PutValue( const css::uno::Any& rVal, sal_uInt8 ) |
160 | 0 | { |
161 | 0 | uno::Sequence< uno::Any > aSeq; |
162 | 0 | bool bRet = false; |
163 | |
|
164 | 0 | if( ( rVal >>= aSeq ) && ( aSeq.getLength() == 10 ) ) |
165 | 0 | { |
166 | 0 | sal_Int32 nInt32 = 0; |
167 | |
|
168 | 0 | aSeq[ 0 ] >>= m_pImpl->m_URL; |
169 | 0 | aSeq[ 1 ] >>= nInt32; |
170 | 0 | m_pImpl->m_nMaskSet = static_cast<AVMediaSetMask>(nInt32); |
171 | 0 | aSeq[ 2 ] >>= nInt32; |
172 | 0 | m_pImpl->m_eState = static_cast< MediaState >( nInt32 ); |
173 | 0 | aSeq[ 3 ] >>= m_pImpl->m_fTime; |
174 | 0 | aSeq[ 4 ] >>= m_pImpl->m_fDuration; |
175 | 0 | aSeq[ 5 ] >>= m_pImpl->m_nVolumeDB; |
176 | 0 | aSeq[ 6 ] >>= m_pImpl->m_bLoop; |
177 | 0 | aSeq[ 7 ] >>= m_pImpl->m_bMute; |
178 | 0 | aSeq[ 8 ] >>= m_pImpl->m_eZoom; |
179 | 0 | aSeq[ 9 ] >>= m_pImpl->m_sMimeType; |
180 | |
|
181 | 0 | bRet = true; |
182 | 0 | } |
183 | |
|
184 | 0 | return bRet; |
185 | 0 | } |
186 | | |
187 | | bool MediaItem::merge(const MediaItem& rMediaItem) |
188 | 0 | { |
189 | 0 | bool bChanged = false; |
190 | |
|
191 | 0 | const AVMediaSetMask nMaskSet = rMediaItem.getMaskSet(); |
192 | |
|
193 | 0 | if( AVMediaSetMask::URL & nMaskSet ) |
194 | 0 | { |
195 | 0 | bChanged = m_pImpl->m_FallbackURL == rMediaItem.getFallbackURL(); |
196 | 0 | m_pImpl->m_FallbackURL = rMediaItem.getFallbackURL(); |
197 | 0 | bChanged |= setURL(rMediaItem.getURL(), rMediaItem.getTempURL(), rMediaItem.getReferer()); |
198 | 0 | } |
199 | |
|
200 | 0 | if( AVMediaSetMask::MIME_TYPE & nMaskSet ) |
201 | 0 | bChanged |= setMimeType(rMediaItem.getMimeType()); |
202 | |
|
203 | 0 | if (nMaskSet & AVMediaSetMask::GRAPHIC) |
204 | 0 | bChanged |= setGraphic(rMediaItem.getGraphic()); |
205 | |
|
206 | 0 | if (nMaskSet & AVMediaSetMask::CROP) |
207 | 0 | bChanged |= setCrop(rMediaItem.getCrop()); |
208 | |
|
209 | 0 | if( AVMediaSetMask::STATE & nMaskSet ) |
210 | 0 | bChanged |= setState( rMediaItem.getState() ); |
211 | |
|
212 | 0 | if( AVMediaSetMask::DURATION & nMaskSet ) |
213 | 0 | bChanged |= setDuration(rMediaItem.getDuration()); |
214 | |
|
215 | 0 | if( AVMediaSetMask::TIME & nMaskSet ) |
216 | 0 | bChanged |= setTime(rMediaItem.getTime()); |
217 | |
|
218 | 0 | if( AVMediaSetMask::LOOP & nMaskSet ) |
219 | 0 | bChanged |= setLoop(rMediaItem.isLoop()); |
220 | |
|
221 | 0 | if( AVMediaSetMask::MUTE & nMaskSet ) |
222 | 0 | bChanged |= setMute(rMediaItem.isMute()); |
223 | |
|
224 | 0 | if( AVMediaSetMask::VOLUMEDB & nMaskSet ) |
225 | 0 | bChanged |= setVolumeDB(rMediaItem.getVolumeDB()); |
226 | |
|
227 | 0 | if( AVMediaSetMask::ZOOM & nMaskSet ) |
228 | 0 | bChanged |= setZoom(rMediaItem.getZoom()); |
229 | |
|
230 | 0 | return bChanged; |
231 | 0 | } |
232 | | |
233 | | AVMediaSetMask MediaItem::getMaskSet() const |
234 | 0 | { |
235 | 0 | return m_pImpl->m_nMaskSet; |
236 | 0 | } |
237 | | |
238 | | bool MediaItem::setURL(const OUString& rURL, const OUString& rTempURL, const OUString& rReferer) |
239 | 0 | { |
240 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::URL; |
241 | 0 | bool bChanged = rURL != m_pImpl->m_URL || rTempURL != m_pImpl->m_TempFileURL || rReferer != m_pImpl->m_Referer; |
242 | 0 | if (bChanged) |
243 | 0 | { |
244 | 0 | m_pImpl->m_URL = rURL; |
245 | 0 | m_pImpl->m_TempFileURL = rTempURL; |
246 | 0 | m_pImpl->m_Referer = rReferer; |
247 | 0 | OUString sMimeType(::comphelper::GuessMediaMimeType(GetFilename(rURL))); |
248 | 0 | if (!sMimeType.isEmpty()) |
249 | 0 | setMimeType(sMimeType); |
250 | 0 | } |
251 | 0 | return bChanged; |
252 | 0 | } |
253 | | |
254 | | const OUString& MediaItem::getURL() const |
255 | 0 | { |
256 | 0 | return m_pImpl->m_URL; |
257 | 0 | } |
258 | | |
259 | | bool MediaItem::setFallbackURL(const OUString& rURL) |
260 | 0 | { |
261 | 0 | bool bChanged = rURL != m_pImpl->m_FallbackURL; |
262 | 0 | if (bChanged) |
263 | 0 | m_pImpl->m_FallbackURL = rURL; |
264 | 0 | return bChanged; |
265 | 0 | } |
266 | | const OUString& MediaItem::getFallbackURL() const |
267 | 0 | { |
268 | 0 | return m_pImpl->m_FallbackURL; |
269 | 0 | } |
270 | | |
271 | | const OUString& MediaItem::getTempURL() const |
272 | 0 | { |
273 | 0 | return m_pImpl->m_TempFileURL; |
274 | 0 | } |
275 | | |
276 | | const OUString& MediaItem::getReferer() const |
277 | 0 | { |
278 | 0 | return m_pImpl->m_Referer; |
279 | 0 | } |
280 | | |
281 | | bool MediaItem::setMimeType(const OUString& rMimeType) |
282 | 0 | { |
283 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::MIME_TYPE; |
284 | 0 | bool bChanged = rMimeType != m_pImpl->m_sMimeType; |
285 | 0 | if (bChanged) |
286 | 0 | m_pImpl->m_sMimeType = rMimeType; |
287 | 0 | return bChanged; |
288 | 0 | } |
289 | | |
290 | | const OUString & MediaItem::getMimeType() const |
291 | 0 | { |
292 | 0 | return !m_pImpl->m_sMimeType.isEmpty() ? m_pImpl->m_sMimeType : AVMEDIA_MIMETYPE_COMMON; |
293 | 0 | } |
294 | | |
295 | | bool MediaItem::setGraphic(const Graphic& rGraphic) |
296 | 0 | { |
297 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::GRAPHIC; |
298 | 0 | bool bChanged = rGraphic != m_pImpl->m_aGraphic; |
299 | 0 | if (bChanged) |
300 | 0 | m_pImpl->m_aGraphic = rGraphic; |
301 | 0 | return bChanged; |
302 | 0 | } |
303 | | |
304 | 0 | const Graphic & MediaItem::getGraphic() const { return m_pImpl->m_aGraphic; } |
305 | | |
306 | | bool MediaItem::setCrop(const text::GraphicCrop& rCrop) |
307 | 0 | { |
308 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::CROP; |
309 | 0 | bool bChanged = rCrop != m_pImpl->m_aCrop; |
310 | 0 | if (bChanged) |
311 | 0 | m_pImpl->m_aCrop = rCrop; |
312 | 0 | return bChanged; |
313 | 0 | } |
314 | | |
315 | 0 | const text::GraphicCrop& MediaItem::getCrop() const { return m_pImpl->m_aCrop; } |
316 | | |
317 | | bool MediaItem::setState(MediaState eState) |
318 | 0 | { |
319 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::STATE; |
320 | 0 | bool bChanged = eState != m_pImpl->m_eState; |
321 | 0 | if (bChanged) |
322 | 0 | m_pImpl->m_eState = eState; |
323 | 0 | return bChanged; |
324 | 0 | } |
325 | | |
326 | | MediaState MediaItem::getState() const |
327 | 0 | { |
328 | 0 | return m_pImpl->m_eState; |
329 | 0 | } |
330 | | |
331 | | bool MediaItem::setDuration(double fDuration) |
332 | 0 | { |
333 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::DURATION; |
334 | 0 | bool bChanged = fDuration != m_pImpl->m_fDuration; |
335 | 0 | if (bChanged) |
336 | 0 | m_pImpl->m_fDuration = fDuration; |
337 | 0 | return bChanged; |
338 | 0 | } |
339 | | |
340 | | double MediaItem::getDuration() const |
341 | 0 | { |
342 | 0 | return m_pImpl->m_fDuration; |
343 | 0 | } |
344 | | |
345 | | bool MediaItem::setTime(double fTime) |
346 | 0 | { |
347 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::TIME; |
348 | 0 | bool bChanged = fTime != m_pImpl->m_fTime; |
349 | 0 | if (bChanged) |
350 | 0 | m_pImpl->m_fTime = fTime; |
351 | 0 | return bChanged; |
352 | 0 | } |
353 | | |
354 | | double MediaItem::getTime() const |
355 | 0 | { |
356 | 0 | return m_pImpl->m_fTime; |
357 | 0 | } |
358 | | |
359 | | bool MediaItem::setLoop(bool bLoop) |
360 | 0 | { |
361 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::LOOP; |
362 | 0 | bool bChanged = bLoop != m_pImpl->m_bLoop; |
363 | 0 | if (bChanged) |
364 | 0 | m_pImpl->m_bLoop = bLoop; |
365 | 0 | return bChanged; |
366 | 0 | } |
367 | | |
368 | | bool MediaItem::isLoop() const |
369 | 0 | { |
370 | 0 | return m_pImpl->m_bLoop; |
371 | 0 | } |
372 | | |
373 | | bool MediaItem::setMute(bool bMute) |
374 | 0 | { |
375 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::MUTE; |
376 | 0 | bool bChanged = bMute != m_pImpl->m_bMute; |
377 | 0 | if (bChanged) |
378 | 0 | m_pImpl->m_bMute = bMute; |
379 | 0 | return bChanged; |
380 | 0 | } |
381 | | |
382 | | bool MediaItem::isMute() const |
383 | 0 | { |
384 | 0 | return m_pImpl->m_bMute; |
385 | 0 | } |
386 | | |
387 | | bool MediaItem::setVolumeDB(sal_Int16 nDB) |
388 | 0 | { |
389 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::VOLUMEDB; |
390 | 0 | bool bChanged = nDB != m_pImpl->m_nVolumeDB; |
391 | 0 | if (bChanged) |
392 | 0 | m_pImpl->m_nVolumeDB = nDB; |
393 | 0 | return bChanged; |
394 | 0 | } |
395 | | |
396 | | sal_Int16 MediaItem::getVolumeDB() const |
397 | 0 | { |
398 | 0 | return m_pImpl->m_nVolumeDB; |
399 | 0 | } |
400 | | |
401 | | bool MediaItem::setZoom(css::media::ZoomLevel eZoom) |
402 | 0 | { |
403 | 0 | m_pImpl->m_nMaskSet |= AVMediaSetMask::ZOOM; |
404 | 0 | bool bChanged = eZoom != m_pImpl->m_eZoom; |
405 | 0 | if (bChanged) |
406 | 0 | m_pImpl->m_eZoom = eZoom; |
407 | 0 | return bChanged; |
408 | 0 | } |
409 | | |
410 | | css::media::ZoomLevel MediaItem::getZoom() const |
411 | 0 | { |
412 | 0 | return m_pImpl->m_eZoom; |
413 | 0 | } |
414 | | |
415 | | OUString GetFilename(OUString const& rSourceURL) |
416 | 0 | { |
417 | 0 | uno::Reference<uri::XUriReferenceFactory> const xUriFactory( |
418 | 0 | uri::UriReferenceFactory::create( |
419 | 0 | comphelper::getProcessComponentContext())); |
420 | 0 | uno::Reference<uri::XUriReference> const xSourceURI( |
421 | 0 | xUriFactory->parse(rSourceURL), uno::UNO_SET_THROW); |
422 | |
|
423 | 0 | OUString filename; |
424 | 0 | { |
425 | 0 | sal_Int32 const nSegments(xSourceURI->getPathSegmentCount()); |
426 | 0 | if (0 < nSegments) |
427 | 0 | { |
428 | 0 | filename = xSourceURI->getPathSegment(nSegments - 1); |
429 | 0 | } |
430 | 0 | } |
431 | 0 | if (!::comphelper::OStorageHelper::IsValidZipEntryFileName( |
432 | 0 | filename, false) || !filename.getLength()) |
433 | 0 | { |
434 | 0 | filename = "media"; |
435 | 0 | } |
436 | 0 | return filename; |
437 | 0 | } |
438 | | |
439 | | |
440 | | uno::Reference<io::XStream> |
441 | | CreateStream(uno::Reference<embed::XStorage> const& xStorage, |
442 | | OUString const& rFilename) |
443 | 0 | { |
444 | 0 | OUString filename(rFilename); |
445 | |
|
446 | 0 | if (xStorage->hasByName(filename)) |
447 | 0 | { |
448 | 0 | std::u16string_view basename; |
449 | 0 | std::u16string_view suffix; |
450 | 0 | sal_Int32 const nIndex(rFilename.lastIndexOf('.')); |
451 | 0 | if (0 < nIndex) |
452 | 0 | { |
453 | 0 | basename = rFilename.subView(0, nIndex); |
454 | 0 | suffix = rFilename.subView(nIndex); |
455 | 0 | } |
456 | 0 | sal_Int32 count(0); // sigh... try to generate non-existent name |
457 | 0 | do |
458 | 0 | { |
459 | 0 | ++count; |
460 | 0 | filename = basename + OUString::number(count) + suffix; |
461 | 0 | } |
462 | 0 | while (xStorage->hasByName(filename)); |
463 | 0 | } |
464 | |
|
465 | 0 | uno::Reference<io::XStream> const xStream( |
466 | 0 | xStorage->openStreamElement(filename, |
467 | 0 | embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE), |
468 | 0 | uno::UNO_SET_THROW); |
469 | 0 | uno::Reference< beans::XPropertySet > const xStreamProps(xStream, |
470 | 0 | uno::UNO_QUERY); |
471 | 0 | if (xStreamProps.is()) { // this is NOT supported in FileSystemStorage |
472 | 0 | OUString const guessed(::comphelper::GuessMediaMimeType(filename)); |
473 | 0 | xStreamProps->setPropertyValue(u"MediaType"_ustr, |
474 | 0 | uno::Any(guessed.isEmpty() ? AVMEDIA_MIMETYPE_COMMON : guessed)); |
475 | 0 | xStreamProps->setPropertyValue( // turn off compression |
476 | 0 | u"Compressed"_ustr, uno::Any(false)); |
477 | 0 | } |
478 | 0 | return xStream; |
479 | 0 | } |
480 | | |
481 | | |
482 | | bool EmbedMedia(uno::Reference<frame::XModel> const& xModel, |
483 | | OUString const& rSourceURL, OUString & o_rEmbeddedURL, uno::Reference<io::XInputStream> const& xInputStream) |
484 | 0 | { |
485 | 0 | try |
486 | 0 | { |
487 | 0 | uno::Reference<document::XStorageBasedDocument> const xSBD(xModel, |
488 | 0 | uno::UNO_QUERY_THROW); |
489 | 0 | uno::Reference<embed::XStorage> const xStorage( |
490 | 0 | xSBD->getDocumentStorage(), uno::UNO_SET_THROW); |
491 | |
|
492 | 0 | static constexpr OUString media(u"Media"_ustr); |
493 | 0 | uno::Reference<embed::XStorage> const xSubStorage( |
494 | 0 | xStorage->openStorageElement(media, embed::ElementModes::WRITE)); |
495 | |
|
496 | 0 | OUString filename(GetFilename(rSourceURL)); |
497 | |
|
498 | 0 | uno::Reference<io::XStream> const xStream( |
499 | 0 | CreateStream(xSubStorage, filename), uno::UNO_SET_THROW); |
500 | 0 | uno::Reference<io::XOutputStream> const xOutStream( |
501 | 0 | xStream->getOutputStream(), uno::UNO_SET_THROW); |
502 | |
|
503 | 0 | if (xInputStream.is()) |
504 | 0 | { |
505 | | // Throw Exception if failed. |
506 | 0 | ::comphelper::OStorageHelper::CopyInputToOutput(xInputStream, xOutStream); |
507 | 0 | } |
508 | 0 | else |
509 | 0 | { |
510 | 0 | ::ucbhelper::Content sourceContent(rSourceURL, |
511 | 0 | uno::Reference<ucb::XCommandEnvironment>(), |
512 | 0 | comphelper::getProcessComponentContext()); |
513 | |
|
514 | 0 | if (!sourceContent.openStream(xOutStream)) // copy file to storage |
515 | 0 | { |
516 | 0 | SAL_INFO("avmedia", "openStream to storage failed"); |
517 | 0 | return false; |
518 | 0 | } |
519 | 0 | } |
520 | | |
521 | 0 | uno::Reference<embed::XTransactedObject> const xSubTransaction( |
522 | 0 | xSubStorage, uno::UNO_QUERY); |
523 | 0 | if (xSubTransaction.is()) { |
524 | 0 | xSubTransaction->commit(); |
525 | 0 | } |
526 | 0 | uno::Reference<embed::XTransactedObject> const xTransaction( |
527 | 0 | xStorage, uno::UNO_QUERY); |
528 | 0 | if (xTransaction.is()) { |
529 | 0 | xTransaction->commit(); |
530 | 0 | } |
531 | |
|
532 | 0 | o_rEmbeddedURL = "vnd.sun.star.Package:" + media + "/" + filename; |
533 | 0 | return true; |
534 | 0 | } |
535 | 0 | catch (uno::Exception const&) |
536 | 0 | { |
537 | 0 | SAL_WARN("avmedia", |
538 | 0 | "Exception while trying to embed media"); |
539 | 0 | } |
540 | 0 | return false; |
541 | 0 | } |
542 | | |
543 | | bool CreateMediaTempFile(uno::Reference<io::XInputStream> const& xInStream, |
544 | | OUString& o_rTempFileURL, std::u16string_view rDesiredExtension) |
545 | 0 | { |
546 | 0 | OUString tempFileURL; |
547 | 0 | ::osl::FileBase::RC const err = |
548 | 0 | ::osl::FileBase::createTempFile(nullptr, nullptr, & tempFileURL); |
549 | 0 | if (::osl::FileBase::E_None != err) |
550 | 0 | { |
551 | 0 | SAL_WARN("avmedia", "cannot create temp file"); |
552 | 0 | return false; |
553 | 0 | } |
554 | | |
555 | 0 | if (!rDesiredExtension.empty()) |
556 | 0 | { |
557 | 0 | OUString newTempFileURL = tempFileURL + rDesiredExtension; |
558 | 0 | if (osl::File::move(tempFileURL, newTempFileURL) != osl::FileBase::E_None) |
559 | 0 | { |
560 | 0 | SAL_WARN("avmedia", "Could not rename file '" << tempFileURL << "' to '" << newTempFileURL << "'"); |
561 | 0 | return false; |
562 | 0 | } |
563 | 0 | tempFileURL = newTempFileURL; |
564 | 0 | } |
565 | | |
566 | 0 | try |
567 | 0 | { |
568 | 0 | ::ucbhelper::Content tempContent(tempFileURL, |
569 | 0 | uno::Reference<ucb::XCommandEnvironment>(), |
570 | 0 | comphelper::getProcessComponentContext()); |
571 | 0 | tempContent.writeStream(xInStream, true); // copy stream to file |
572 | 0 | } |
573 | 0 | catch (uno::Exception const&) |
574 | 0 | { |
575 | 0 | TOOLS_WARN_EXCEPTION("avmedia", ""); |
576 | 0 | return false; |
577 | 0 | } |
578 | 0 | o_rTempFileURL = tempFileURL; |
579 | 0 | return true; |
580 | 0 | } |
581 | | |
582 | | MediaTempFile::~MediaTempFile() |
583 | 0 | { |
584 | 0 | ::osl::File::remove(m_TempFileURL); |
585 | 0 | } |
586 | | |
587 | | } // namespace avmedia |
588 | | |
589 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |