LibreOffice Module svx (master)  1
svdomedia.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column:100 -*- */
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 <config_features.h>
21 
22 #include <svx/svdomedia.hxx>
23 
24 #include <rtl/ustring.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <sal/log.hxx>
27 
28 #include <ucbhelper/content.hxx>
31 
32 #include <vcl/svapp.hxx>
33 
34 #include <svx/svdmodel.hxx>
35 #include <svx/dialmgr.hxx>
36 #include <svx/strings.hrc>
38 #include <avmedia/mediawindow.hxx>
39 #include <tools/diagnose_ex.h>
40 
41 using namespace ::com::sun::star;
42 
43 
45 {
47  // Note: the temp file is read only, until it is deleted!
48  // It may be shared between multiple documents in case of copy/paste,
49  // hence the shared_ptr.
50  std::shared_ptr< ::avmedia::MediaTempFile > m_pTempFile;
51  uno::Reference< graphic::XGraphic > m_xCachedSnapshot;
53 };
54 
56 : SdrRectObj(rSdrModel)
57  ,m_xImpl( new Impl )
58 {
59 }
60 
61 SdrMediaObj::SdrMediaObj(SdrModel& rSdrModel, SdrMediaObj const & rSource)
62 : SdrRectObj(rSdrModel, rSource)
63  ,m_xImpl( new Impl )
64 {
65  m_xImpl->m_pTempFile = rSource.m_xImpl->m_pTempFile; // before props
67  m_xImpl->m_xCachedSnapshot = rSource.m_xImpl->m_xCachedSnapshot;
68 }
69 
71  SdrModel& rSdrModel,
72  const tools::Rectangle& rRect)
73 : SdrRectObj(rSdrModel, rRect)
74  ,m_xImpl( new Impl )
75 {
76 }
77 
79 {
80 }
81 
83 {
84  return false;
85 }
86 
87 std::unique_ptr<sdr::contact::ViewContact> SdrMediaObj::CreateObjectSpecificViewContact()
88 {
89  return std::make_unique<sdr::contact::ViewContactOfSdrMediaObj>( *this );
90 }
91 
93 {
94  rInfo.bMoveAllowed = true;
95  rInfo.bResizeFreeAllowed = true;
96  rInfo.bResizePropAllowed = true;
97  rInfo.bRotateFreeAllowed = false;
98  rInfo.bRotate90Allowed = false;
99  rInfo.bMirrorFreeAllowed = false;
100  rInfo.bMirror45Allowed = false;
101  rInfo.bMirror90Allowed = false;
102  rInfo.bTransparenceAllowed = false;
103  rInfo.bShearAllowed = false;
104  rInfo.bEdgeRadiusAllowed = false;
105  rInfo.bNoOrthoDesired = false;
106  rInfo.bNoContortion = false;
107  rInfo.bCanConvToPath = false;
108  rInfo.bCanConvToPoly = false;
109  rInfo.bCanConvToContour = false;
110  rInfo.bCanConvToPathLineToArea = false;
111  rInfo.bCanConvToPolyLineToArea = false;
112 }
113 
115 {
116  return OBJ_MEDIA;
117 }
118 
120 {
121  OUString sName(SvxResId(STR_ObjNameSingulMEDIA));
122 
123  OUString aName(GetName());
124 
125  if (!aName.isEmpty())
126  sName += " '" + aName + "'";
127 
128  return sName;
129 }
130 
132 {
133  return SvxResId(STR_ObjNamePluralMEDIA);
134 }
135 
137 {
138  return new SdrMediaObj(rTargetModel, *this);
139 }
140 
141 uno::Reference< graphic::XGraphic > const & SdrMediaObj::getSnapshot() const
142 {
143 #if HAVE_FEATURE_AVMEDIA
144  if( !m_xImpl->m_xCachedSnapshot.is() )
145  {
146  OUString aRealURL = m_xImpl->m_MediaProperties.getTempURL();
147  if( aRealURL.isEmpty() )
148  aRealURL = m_xImpl->m_MediaProperties.getURL();
149  m_xImpl->m_xCachedSnapshot = avmedia::MediaWindow::grabFrame( aRealURL, m_xImpl->m_MediaProperties.getReferer(), m_xImpl->m_MediaProperties.getMimeType());
150  }
151 #endif
152  return m_xImpl->m_xCachedSnapshot;
153 }
154 
155 void SdrMediaObj::AdjustToMaxRect( const tools::Rectangle& rMaxRect, bool bShrinkOnly /* = false */ )
156 {
157  Size aSize( Application::GetDefaultDevice()->PixelToLogic(
158  static_cast< sdr::contact::ViewContactOfSdrMediaObj& >( GetViewContact() ).getPreferredSize(),
159  MapMode(MapUnit::Map100thMM)) );
160  Size aMaxSize( rMaxRect.GetSize() );
161 
162  if( aSize.IsEmpty() )
163  return;
164 
165  Point aPos( rMaxRect.TopLeft() );
166 
167  // if graphic is too large, fit it to the page
168  if ( (!bShrinkOnly ||
169  ( aSize.Height() > aMaxSize.Height() ) ||
170  ( aSize.Width() > aMaxSize.Width() ) )&&
171  aSize.Height() && aMaxSize.Height() )
172  {
173  float fGrfWH = static_cast<float>(aSize.Width()) /
174  static_cast<float>(aSize.Height());
175  float fWinWH = static_cast<float>(aMaxSize.Width()) /
176  static_cast<float>(aMaxSize.Height());
177 
178  // scale graphic to page size
179  if ( fGrfWH < fWinWH )
180  {
181  aSize.setWidth( static_cast<tools::Long>(aMaxSize.Height() * fGrfWH) );
182  aSize.setHeight( aMaxSize.Height() );
183  }
184  else if ( fGrfWH > 0.F )
185  {
186  aSize.setWidth( aMaxSize.Width() );
187  aSize.setHeight( static_cast<tools::Long>(aMaxSize.Width() / fGrfWH) );
188  }
189 
190  aPos = rMaxRect.Center();
191  }
192 
193  if( bShrinkOnly )
194  aPos = maRect.TopLeft();
195 
196  aPos.AdjustX( -(aSize.Width() / 2) );
197  aPos.AdjustY( -(aSize.Height() / 2) );
198  SetLogicRect( tools::Rectangle( aPos, aSize ) );
199 }
200 
201 void SdrMediaObj::setURL( const OUString& rURL, const OUString& rReferer, const OUString& rMimeType )
202 {
203  ::avmedia::MediaItem aURLItem;
204 #if HAVE_FEATURE_AVMEDIA
205  if( !rMimeType.isEmpty() )
206  m_xImpl->m_MediaProperties.setMimeType(rMimeType);
207  aURLItem.setURL( rURL, "", rReferer );
208 #else
209  (void) rMimeType;
210  (void) rURL;
211  (void) rReferer;
212 #endif
213  setMediaProperties( aURLItem );
214 }
215 
216 const OUString& SdrMediaObj::getURL() const
217 {
218 #if HAVE_FEATURE_AVMEDIA
219  return m_xImpl->m_MediaProperties.getURL();
220 #else
221 static OUString ret;
222  return ret;
223 #endif
224 }
225 
226 void SdrMediaObj::setMediaProperties( const ::avmedia::MediaItem& rState )
227 {
228  mediaPropertiesChanged( rState );
229  static_cast< sdr::contact::ViewContactOfSdrMediaObj& >( GetViewContact() ).executeMediaItem( getMediaProperties() );
230 }
231 
232 const ::avmedia::MediaItem& SdrMediaObj::getMediaProperties() const
233 {
234  return m_xImpl->m_MediaProperties;
235 }
236 
237 uno::Reference<io::XInputStream> SdrMediaObj::GetInputStream() const
238 {
239  if (!m_xImpl->m_pTempFile)
240  {
241  SAL_WARN("svx", "this is only intended for embedded media");
242  return nullptr;
243  }
244  ucbhelper::Content tempFile(m_xImpl->m_pTempFile->m_TempFileURL,
245  uno::Reference<ucb::XCommandEnvironment>(),
247  return tempFile.openStream();
248 }
249 
250 void SdrMediaObj::SetInputStream(uno::Reference<io::XInputStream> const& xStream)
251 {
252  if (m_xImpl->m_pTempFile || m_xImpl->m_LastFailedPkgURL.isEmpty())
253  {
254  SAL_WARN("svx", "this is only intended for embedded media");
255  return;
256  }
257 
258  OUString tempFileURL;
259  const bool bSuccess(
261  xStream,
262  tempFileURL,
263  u""));
264 
265  if (bSuccess)
266  {
267  m_xImpl->m_pTempFile = std::make_shared<::avmedia::MediaTempFile>(tempFileURL);
268 #if HAVE_FEATURE_AVMEDIA
269  m_xImpl->m_MediaProperties.setURL(
270  m_xImpl->m_LastFailedPkgURL, tempFileURL, "");
271 #endif
272  }
273  m_xImpl->m_LastFailedPkgURL.clear(); // once only
274 }
275 
277 #if HAVE_FEATURE_AVMEDIA
278 static bool lcl_HandlePackageURL(
279  OUString const & rURL,
280  const SdrModel& rModel,
281  OUString & o_rTempFileURL)
282 {
283  ::comphelper::LifecycleProxy sourceProxy;
284  uno::Reference<io::XInputStream> xInStream;
285  try {
286  xInStream = rModel.GetDocumentStream(rURL, sourceProxy);
287  }
288  catch (container::NoSuchElementException const&)
289  {
290  SAL_INFO("svx", "not found: '" << rURL << "'");
291  return false;
292  }
293  catch (uno::Exception const&)
294  {
295  TOOLS_WARN_EXCEPTION("svx", "");
296  return false;
297  }
298  if (!xInStream.is())
299  {
300  SAL_WARN("svx", "no stream?");
301  return false;
302  }
303  // Make sure the temporary copy has the same file name extension as the original media file
304  // (like .mp4). That seems to be important for some AVFoundation APIs. For random extension-less
305  // file names, they don't seem to even bother looking inside the file.
306  sal_Int32 nLastDot = rURL.lastIndexOf('.');
307  sal_Int32 nLastSlash = rURL.lastIndexOf('/');
308  OUString sDesiredExtension;
309  if (nLastDot > nLastSlash && nLastDot+1 < rURL.getLength())
310  sDesiredExtension = rURL.copy(nLastDot);
311  return ::avmedia::CreateMediaTempFile(xInStream, o_rTempFileURL, sDesiredExtension);
312 }
313 #endif
314 
315 void SdrMediaObj::mediaPropertiesChanged( const ::avmedia::MediaItem& rNewProperties )
316 {
317  bool bBroadcastChanged = false;
318 #if HAVE_FEATURE_AVMEDIA
319  const AVMediaSetMask nMaskSet = rNewProperties.getMaskSet();
320 
321  // use only a subset of MediaItem properties for own properties
322  if( AVMediaSetMask::MIME_TYPE & nMaskSet )
323  m_xImpl->m_MediaProperties.setMimeType( rNewProperties.getMimeType() );
324 
325  if (nMaskSet & AVMediaSetMask::GRAPHIC)
326  {
327  m_xImpl->m_MediaProperties.setGraphic(rNewProperties.getGraphic());
328  }
329 
330  if( ( AVMediaSetMask::URL & nMaskSet ) &&
331  ( rNewProperties.getURL() != getURL() ))
332  {
333  m_xImpl->m_xCachedSnapshot.clear();
334  OUString const& url(rNewProperties.getURL());
335  if (url.startsWithIgnoreAsciiCase("vnd.sun.star.Package:"))
336  {
337  if ( !m_xImpl->m_pTempFile
338  || (m_xImpl->m_pTempFile->m_TempFileURL !=
339  rNewProperties.getTempURL()))
340  {
341  OUString tempFileURL;
342  const bool bSuccess(
343  lcl_HandlePackageURL(
344  url,
346  tempFileURL));
347 
348  if (bSuccess)
349  {
350  m_xImpl->m_pTempFile =
351  std::make_shared<::avmedia::MediaTempFile>(tempFileURL);
352  m_xImpl->m_MediaProperties.setURL(url, tempFileURL, "");
353  }
354  else // this case is for Clone via operator=
355  {
356  m_xImpl->m_pTempFile.reset();
357  m_xImpl->m_MediaProperties.setURL("", "", "");
358  // UGLY: oox import also gets here, because unlike ODF
359  // getDocumentStorage() is not the imported file...
360  m_xImpl->m_LastFailedPkgURL = url;
361  }
362  }
363  else
364  {
365  m_xImpl->m_MediaProperties.setURL(url,
366  rNewProperties.getTempURL(), "");
367  }
368  }
369  else
370  {
371  m_xImpl->m_pTempFile.reset();
372  m_xImpl->m_MediaProperties.setURL(url, "", rNewProperties.getReferer());
373  }
374  bBroadcastChanged = true;
375  }
376 
377  if( AVMediaSetMask::LOOP & nMaskSet )
378  m_xImpl->m_MediaProperties.setLoop( rNewProperties.isLoop() );
379 
380  if( AVMediaSetMask::MUTE & nMaskSet )
381  m_xImpl->m_MediaProperties.setMute( rNewProperties.isMute() );
382 
383  if( AVMediaSetMask::VOLUMEDB & nMaskSet )
384  m_xImpl->m_MediaProperties.setVolumeDB( rNewProperties.getVolumeDB() );
385 
386  if( AVMediaSetMask::ZOOM & nMaskSet )
387  m_xImpl->m_MediaProperties.setZoom( rNewProperties.getZoom() );
388 #else
389  (void) rNewProperties;
390 #endif
391 
392  if( bBroadcastChanged )
393  {
394  SetChanged();
396  }
397 }
398 
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString m_LastFailedPkgURL
Definition: svdomedia.cxx:52
AVMediaSetMask
void setWidth(tools::Long nWidth)
virtual ~SdrMediaObj() override
Definition: svdomedia.cxx:78
virtual bool HasTextEdit() const override
Definition: svdomedia.cxx:82
std::shared_ptr< ::avmedia::MediaTempFile > m_pTempFile
Definition: svdomedia.cxx:50
SdrObjKind
Definition: svdobjkind.hxx:24
sdr::contact::ViewContact & GetViewContact() const
Definition: svdobj.cxx:251
uno::Reference< graphic::XGraphic > m_xCachedSnapshot
Definition: svdomedia.cxx:51
Rectangle objects (rectangle, circle, ...)
Definition: svdorect.hxx:38
static OutputDevice * GetDefaultDevice()
constexpr tools::Long Width() const
Provides information about various ZObject properties.
Definition: svdobj.hxx:195
virtual SdrMediaObj * CloneSdrObject(SdrModel &rTargetModel) const override
Definition: svdomedia.cxx:136
const ::avmedia::MediaItem & getMediaProperties() const
Definition: svdomedia.cxx:232
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
tools::Rectangle maRect
Definition: svdotext.hxx:170
std::unique_ptr< Impl > m_xImpl
Definition: svdomedia.hxx:79
virtual OUString TakeObjNamePlural() const override
Definition: svdomedia.cxx:131
#define TOOLS_WARN_EXCEPTION(area, stream)
void setURL(const OUString &rURL, const OUString &rReferer, const OUString &rMimeType=OUString())
Definition: svdomedia.cxx:201
bool IsEmpty() const
virtual void TakeObjInfo(SdrObjTransformInfoRec &rInfo) const override
Definition: svdomedia.cxx:92
OUString sName
void BroadcastObjectChange() const
Definition: svdobj.cxx:1008
const OUString & getURL() const
Definition: svdomedia.cxx:216
float u
SdrModel & getSdrModelFromSdrObject() const
Definition: svdobj.cxx:279
void mediaPropertiesChanged(const ::avmedia::MediaItem &rNewState)
copy a stream from XStorage to temp file
Definition: svdomedia.cxx:315
virtual OUString TakeObjNameSingul() const override
Definition: svdomedia.cxx:119
virtual void SetLogicRect(const tools::Rectangle &rRect)
Definition: svdobj.cxx:1700
OUString GetName() const
Definition: svdobj.cxx:802
constexpr Point Center() const
constexpr Point TopLeft() const
void SetInputStream(css::uno::Reference< css::io::XInputStream > const &)
Definition: svdomedia.cxx:250
css::uno::Reference< css::io::XInputStream > openStream()
constexpr Size GetSize() const
css::uno::Reference< css::graphic::XGraphic > const & getSnapshot() const
Definition: svdomedia.cxx:141
constexpr tools::Long Height() const
#define SAL_INFO(area, stream)
OUString aName
SdrMediaObj(SdrModel &rSdrModel)
Definition: svdomedia.cxx:55
Reference< XComponentContext > getProcessComponentContext()
void setMediaProperties(const ::avmedia::MediaItem &rState)
Definition: svdomedia.cxx:226
virtual void AdjustToMaxRect(const tools::Rectangle &rMaxRect, bool bShrinkOnly=false) override
Definition: svdomedia.cxx:155
::avmedia::MediaItem m_MediaProperties
Definition: svdomedia.cxx:46
void setHeight(tools::Long nHeight)
css::uno::Reference< css::io::XInputStream > GetInputStream() const
Definition: svdomedia.cxx:237
#define SAL_WARN(area, stream)
virtual std::unique_ptr< sdr::contact::ViewContact > CreateObjectSpecificViewContact() override
Definition: svdomedia.cxx:87
virtual void SetChanged()
Definition: svdobj.cxx:1032
static css::uno::Reference< css::graphic::XGraphic > grabFrame(const OUString &rURL, const OUString &rReferer, const OUString &sMimeType)
bool CreateMediaTempFile(uno::Reference< io::XInputStream > const &xInStream, OUString &o_rTempFileURL, std::u16string_view rDesiredExtension)
void setURL(const OUString &rURL, const OUString &rTempURL, const OUString &rReferer)
css::uno::Reference< css::io::XInputStream > GetDocumentStream(OUString const &rURL,::comphelper::LifecycleProxy const &rProxy) const
Definition: svdmodel.cxx:751
custom shape
Definition: svdobjkind.hxx:56
virtual SdrObjKind GetObjIdentifier() const override
Definition: svdomedia.cxx:114
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo