LCOV - code coverage report
Current view: top level - avmedia/source/framework - mediaitem.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 56 205 27.3 %
Date: 2014-04-11 Functions: 20 42 47.6 %
Legend: Lines: hit not hit

          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 <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/document/XStorageBasedDocument.hpp>
      28             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      29             : #include <com/sun/star/uri/UriReferenceFactory.hpp>
      30             : #include <com/sun/star/uri/XUriReference.hpp>
      31             : #include <com/sun/star/uri/XUriReferenceFactory.hpp>
      32             : 
      33             : #include <rtl/ustrbuf.hxx>
      34             : 
      35             : #include <ucbhelper/content.hxx>
      36             : 
      37             : #include <comphelper/processfactory.hxx>
      38             : #include <comphelper/storagehelper.hxx>
      39             : #include "mediamisc.hxx"
      40             : 
      41             : using namespace ::com::sun::star;
      42             : 
      43             : namespace avmedia
      44             : {
      45             : 
      46             : // - MediaItem -
      47         174 : TYPEINIT1_AUTOFACTORY( MediaItem, ::SfxPoolItem );
      48             : 
      49          61 : struct MediaItem::Impl
      50             : {
      51             :     OUString                m_URL;
      52             :     OUString                m_TempFileURL;
      53             :     OUString                m_Referer;
      54             :     OUString                m_sMimeType;
      55             :     sal_uInt32              m_nMaskSet;
      56             :     MediaState              m_eState;
      57             :     double                  m_fTime;
      58             :     double                  m_fDuration;
      59             :     sal_Int16               m_nVolumeDB;
      60             :     bool                    m_bLoop;
      61             :     bool                    m_bMute;
      62             :     ::com::sun::star::media::ZoomLevel m_eZoom;
      63             : 
      64          49 :     Impl(sal_uInt32 const nMaskSet)
      65             :         : m_nMaskSet( nMaskSet )
      66             :         , m_eState( MEDIASTATE_STOP )
      67             :         , m_fTime( 0.0 )
      68             :         , m_fDuration( 0.0 )
      69             :         , m_nVolumeDB( 0 )
      70             :         , m_bLoop( false )
      71             :         , m_bMute( false )
      72          49 :         , m_eZoom( ::com::sun::star::media::ZoomLevel_NOT_AVAILABLE )
      73             :     {
      74          49 :     }
      75          14 :     Impl(Impl const& rOther)
      76             :         : m_URL( rOther.m_URL )
      77             :         , m_TempFileURL( rOther.m_TempFileURL )
      78             :         , m_Referer( rOther.m_Referer )
      79             :         , m_sMimeType( rOther.m_sMimeType )
      80             :         , m_nMaskSet( rOther.m_nMaskSet )
      81             :         , m_eState( rOther.m_eState )
      82             :         , m_fTime( rOther.m_fTime )
      83             :         , m_fDuration( rOther.m_fDuration )
      84             :         , m_nVolumeDB( rOther.m_nVolumeDB )
      85             :         , m_bLoop( rOther.m_bLoop )
      86             :         , m_bMute( rOther.m_bMute )
      87          14 :         , m_eZoom( rOther.m_eZoom )
      88             :     {
      89          14 :     }
      90             : };
      91             : 
      92          49 : MediaItem::MediaItem( sal_uInt16 const i_nWhich, sal_uInt32 const nMaskSet )
      93             :     : SfxPoolItem( i_nWhich )
      94          49 :     , m_pImpl( new Impl(nMaskSet) )
      95             : {
      96          49 : }
      97             : 
      98          14 : MediaItem::MediaItem( const MediaItem& rItem )
      99             :     : SfxPoolItem( rItem )
     100          14 :     , m_pImpl( new Impl(*rItem.m_pImpl) )
     101             : {
     102          14 : }
     103             : 
     104          61 : MediaItem::~MediaItem()
     105             : {
     106          61 : }
     107             : 
     108           0 : bool MediaItem::operator==( const SfxPoolItem& rItem ) const
     109             : {
     110             :     assert( SfxPoolItem::operator==(rItem));
     111           0 :     MediaItem const& rOther(static_cast< const MediaItem& >(rItem));
     112           0 :     return m_pImpl->m_nMaskSet == rOther.m_pImpl->m_nMaskSet
     113           0 :         && m_pImpl->m_URL == rOther.m_pImpl->m_URL
     114           0 :         && m_pImpl->m_Referer == rOther.m_pImpl->m_Referer
     115           0 :         && m_pImpl->m_sMimeType == rOther.m_pImpl->m_sMimeType
     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             : }
     124             : 
     125           0 : SfxPoolItem* MediaItem::Clone( SfxItemPool* ) const
     126             : {
     127           0 :     return new MediaItem( *this );
     128             : }
     129             : 
     130           0 : SfxItemPresentation MediaItem::GetPresentation( SfxItemPresentation,
     131             :                                                   SfxMapUnit,
     132             :                                                   SfxMapUnit,
     133             :                                                   OUString& rText,
     134             :                                                   const IntlWrapper * ) const
     135             : {
     136           0 :     rText = OUString();
     137           0 :     return SFX_ITEM_PRESENTATION_NONE;
     138             : }
     139             : 
     140           0 : bool MediaItem::QueryValue( com::sun::star::uno::Any& rVal, sal_uInt8 ) const
     141             : {
     142           0 :     uno::Sequence< uno::Any > aSeq( 10 );
     143             : 
     144           0 :     aSeq[ 0 ] <<= m_pImpl->m_URL;
     145           0 :     aSeq[ 1 ] <<= m_pImpl->m_nMaskSet;
     146           0 :     aSeq[ 2 ] <<= static_cast< sal_Int32 >( m_pImpl->m_eState );
     147           0 :     aSeq[ 3 ] <<= m_pImpl->m_fTime;
     148           0 :     aSeq[ 4 ] <<= m_pImpl->m_fDuration;
     149           0 :     aSeq[ 5 ] <<= m_pImpl->m_nVolumeDB;
     150           0 :     aSeq[ 6 ] <<= m_pImpl->m_bLoop;
     151           0 :     aSeq[ 7 ] <<= m_pImpl->m_bMute;
     152           0 :     aSeq[ 8 ] <<= m_pImpl->m_eZoom;
     153           0 :     aSeq[ 9 ] <<= m_pImpl->m_sMimeType;
     154             : 
     155           0 :     rVal <<= aSeq;
     156             : 
     157           0 :     return true;
     158             : }
     159             : 
     160           0 : bool MediaItem::PutValue( const com::sun::star::uno::Any& rVal, sal_uInt8 )
     161             : {
     162           0 :     uno::Sequence< uno::Any >   aSeq;
     163           0 :     bool                        bRet = false;
     164             : 
     165           0 :     if( ( rVal >>= aSeq ) && ( aSeq.getLength() == 10 ) )
     166             :     {
     167           0 :         sal_Int32 nInt32 = 0;
     168             : 
     169           0 :         aSeq[ 0 ] >>= m_pImpl->m_URL;
     170           0 :         aSeq[ 1 ] >>= m_pImpl->m_nMaskSet;
     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             :     }
     183             : 
     184           0 :     return bRet;
     185             : }
     186             : 
     187           0 : void MediaItem::merge( const MediaItem& rMediaItem )
     188             : {
     189           0 :     const sal_uInt32 nMaskSet = rMediaItem.getMaskSet();
     190             : 
     191           0 :     if( AVMEDIA_SETMASK_URL & nMaskSet )
     192           0 :         setURL( rMediaItem.getURL(), rMediaItem.getTempURL(), rMediaItem.getReferer() );
     193             : 
     194           0 :     if( AVMEDIA_SETMASK_MIME_TYPE & nMaskSet )
     195           0 :         setMimeType( rMediaItem.getMimeType() );
     196             : 
     197           0 :     if( AVMEDIA_SETMASK_STATE & nMaskSet )
     198           0 :         setState( rMediaItem.getState() );
     199             : 
     200           0 :     if( AVMEDIA_SETMASK_DURATION & nMaskSet )
     201           0 :         setDuration( rMediaItem.getDuration() );
     202             : 
     203           0 :     if( AVMEDIA_SETMASK_TIME & nMaskSet )
     204           0 :         setTime( rMediaItem.getTime() );
     205             : 
     206           0 :     if( AVMEDIA_SETMASK_LOOP & nMaskSet )
     207           0 :         setLoop( rMediaItem.isLoop() );
     208             : 
     209           0 :     if( AVMEDIA_SETMASK_MUTE & nMaskSet )
     210           0 :         setMute( rMediaItem.isMute() );
     211             : 
     212           0 :     if( AVMEDIA_SETMASK_VOLUMEDB & nMaskSet )
     213           0 :         setVolumeDB( rMediaItem.getVolumeDB() );
     214             : 
     215           0 :     if( AVMEDIA_SETMASK_ZOOM & nMaskSet )
     216           0 :         setZoom( rMediaItem.getZoom() );
     217           0 : }
     218             : 
     219          44 : sal_uInt32 MediaItem::getMaskSet() const
     220             : {
     221          44 :     return m_pImpl->m_nMaskSet;
     222             : }
     223             : 
     224          10 : void MediaItem::setURL( const OUString& rURL, const OUString& rTempURL, const OUString& rReferer )
     225             : {
     226          10 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_URL;
     227          10 :     m_pImpl->m_URL = rURL;
     228          10 :     m_pImpl->m_TempFileURL = rTempURL;
     229          10 :     m_pImpl->m_Referer = rReferer;
     230          10 : }
     231             : 
     232          19 : const OUString& MediaItem::getURL() const
     233             : {
     234          19 :     return m_pImpl->m_URL;
     235             : }
     236             : 
     237           0 : const OUString& MediaItem::getTempURL() const
     238             : {
     239           0 :     return m_pImpl->m_TempFileURL;
     240             : }
     241             : 
     242           0 : const OUString& MediaItem::getReferer() const
     243             : {
     244           0 :     return m_pImpl->m_Referer;
     245             : }
     246             : 
     247          10 : void MediaItem::setMimeType( const OUString& rMimeType )
     248             : {
     249          10 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_MIME_TYPE;
     250          10 :     m_pImpl->m_sMimeType = rMimeType;
     251          10 : }
     252             : 
     253           9 : OUString MediaItem::getMimeType() const
     254             : {
     255           9 :     return !m_pImpl->m_sMimeType.isEmpty() ? m_pImpl->m_sMimeType : AVMEDIA_MIMETYPE_COMMON;
     256             : }
     257             : 
     258           0 : void MediaItem::setState( MediaState eState )
     259             : {
     260           0 :     m_pImpl->m_eState = eState;
     261           0 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_STATE;
     262           0 : }
     263             : 
     264           0 : MediaState MediaItem::getState() const
     265             : {
     266           0 :     return m_pImpl->m_eState;
     267             : }
     268             : 
     269           0 : void MediaItem::setDuration( double fDuration )
     270             : {
     271           0 :     m_pImpl->m_fDuration = fDuration;
     272           0 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_DURATION;
     273           0 : }
     274             : 
     275           0 : double MediaItem::getDuration() const
     276             : {
     277           0 :     return m_pImpl->m_fDuration;
     278             : }
     279             : 
     280           0 : void MediaItem::setTime( double fTime )
     281             : {
     282           0 :     m_pImpl->m_fTime = fTime;
     283           0 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_TIME;
     284           0 : }
     285             : 
     286           0 : double MediaItem::getTime() const
     287             : {
     288           0 :     return m_pImpl->m_fTime;
     289             : }
     290             : 
     291          20 : void MediaItem::setLoop( bool bLoop )
     292             : {
     293          20 :     m_pImpl->m_bLoop = bLoop;
     294          20 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_LOOP;
     295          20 : }
     296             : 
     297          12 : bool MediaItem::isLoop() const
     298             : {
     299          12 :     return m_pImpl->m_bLoop;
     300             : }
     301             : 
     302          20 : void MediaItem::setMute( bool bMute )
     303             : {
     304          20 :     m_pImpl->m_bMute = bMute;
     305          20 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_MUTE;
     306          20 : }
     307             : 
     308          12 : bool MediaItem::isMute() const
     309             : {
     310          12 :     return m_pImpl->m_bMute;
     311             : }
     312             : 
     313          20 : void MediaItem::setVolumeDB( sal_Int16 nDB )
     314             : {
     315          20 :     m_pImpl->m_nVolumeDB = nDB;
     316          20 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_VOLUMEDB;
     317          20 : }
     318             : 
     319          12 : sal_Int16 MediaItem::getVolumeDB() const
     320             : {
     321          12 :     return m_pImpl->m_nVolumeDB;
     322             : }
     323             : 
     324           8 : void MediaItem::setZoom( ::com::sun::star::media::ZoomLevel eZoom )
     325             : {
     326           8 :     m_pImpl->m_eZoom = eZoom;
     327           8 :     m_pImpl->m_nMaskSet |= AVMEDIA_SETMASK_ZOOM;
     328           8 : }
     329             : 
     330           6 : ::com::sun::star::media::ZoomLevel MediaItem::getZoom() const
     331             : {
     332           6 :     return m_pImpl->m_eZoom;
     333             : }
     334             : 
     335           0 : OUString GetFilename(OUString const& rSourceURL)
     336             : {
     337             :     uno::Reference<uri::XUriReferenceFactory> const xUriFactory(
     338             :         uri::UriReferenceFactory::create(
     339           0 :             comphelper::getProcessComponentContext()));
     340             :     uno::Reference<uri::XUriReference> const xSourceURI(
     341           0 :         xUriFactory->parse(rSourceURL), uno::UNO_SET_THROW);
     342             : 
     343           0 :     OUString filename;
     344             :     {
     345           0 :         sal_Int32 const nSegments(xSourceURI->getPathSegmentCount());
     346           0 :         if (0 < nSegments)
     347             :         {
     348           0 :             filename = xSourceURI->getPathSegment(nSegments - 1);
     349             :         }
     350             :     }
     351           0 :     if (!::comphelper::OStorageHelper::IsValidZipEntryFileName(
     352           0 :                 filename, false) || !filename.getLength())
     353             :     {
     354           0 :         filename = "media";
     355             :     }
     356           0 :     return filename;
     357             : }
     358             : 
     359             : uno::Reference<io::XStream>
     360           0 : CreateStream(uno::Reference<embed::XStorage> const& xStorage,
     361             :         OUString const& rFilename)
     362             : {
     363           0 :     OUString filename(rFilename);
     364             : 
     365           0 :     if (xStorage->hasByName(filename))
     366             :     {
     367           0 :         OUString basename;
     368           0 :         OUString suffix;
     369           0 :         sal_Int32 const nIndex(rFilename.lastIndexOf('.'));
     370           0 :         if (0 < nIndex)
     371             :         {
     372           0 :             basename = rFilename.copy(0, nIndex);
     373           0 :             suffix = rFilename.copy(nIndex);
     374             :         }
     375           0 :         sal_Int32 count(0); // sigh... try to generate non-existent name
     376           0 :         do
     377             :         {
     378           0 :             ++count;
     379           0 :             filename = basename + OUString::number(count) + suffix;
     380             :         }
     381           0 :         while (xStorage->hasByName(filename));
     382             :     }
     383             : 
     384             :     uno::Reference<io::XStream> const xStream(
     385           0 :         xStorage->openStreamElement(filename,
     386           0 :             embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE),
     387           0 :         uno::UNO_SET_THROW);
     388             :     uno::Reference< beans::XPropertySet > const xStreamProps(xStream,
     389           0 :         uno::UNO_QUERY);
     390           0 :     if (xStreamProps.is()) { // this is NOT supported in FileSystemStorage
     391           0 :         xStreamProps->setPropertyValue("MediaType", uno::makeAny(OUString(
     392             :             //FIXME how to detect real media type?
     393             :             //but currently xmloff has this one hardcoded anyway...
     394           0 :             "application/vnd.sun.star.media")));
     395           0 :         xStreamProps->setPropertyValue( // turn off compression
     396           0 :             "Compressed", uno::makeAny(sal_False));
     397             :     }
     398           0 :     return xStream;
     399             : }
     400             : 
     401           0 : bool EmbedMedia(uno::Reference<frame::XModel> const& xModel,
     402             :         OUString const& rSourceURL, OUString & o_rEmbeddedURL)
     403             : {
     404             :     try
     405             :     {
     406             :         ::ucbhelper::Content sourceContent(rSourceURL,
     407             :                 uno::Reference<ucb::XCommandEnvironment>(),
     408           0 :                 comphelper::getProcessComponentContext());
     409             : 
     410             :         uno::Reference<document::XStorageBasedDocument> const xSBD(xModel,
     411           0 :                 uno::UNO_QUERY_THROW);
     412             :         uno::Reference<embed::XStorage> const xStorage(
     413           0 :                 xSBD->getDocumentStorage(), uno::UNO_QUERY_THROW);
     414             : 
     415           0 :         OUString const media("Media");
     416             :         uno::Reference<embed::XStorage> const xSubStorage(
     417           0 :             xStorage->openStorageElement(media, embed::ElementModes::WRITE));
     418             : 
     419           0 :         OUString filename(GetFilename(rSourceURL));
     420             : 
     421             :         uno::Reference<io::XStream> const xStream(
     422           0 :             CreateStream(xSubStorage, filename), uno::UNO_SET_THROW);
     423             :         uno::Reference<io::XOutputStream> const xOutStream(
     424           0 :             xStream->getOutputStream(), uno::UNO_SET_THROW);
     425             : 
     426           0 :         if (!sourceContent.openStream(xOutStream)) // copy file to storage
     427             :         {
     428             :             SAL_INFO("avmedia", "openStream to storage failed");
     429           0 :             return false;
     430             :         }
     431             : 
     432             :         uno::Reference<embed::XTransactedObject> const xSubTransaction(
     433           0 :             xSubStorage, uno::UNO_QUERY);
     434           0 :         if (xSubTransaction.is()) {
     435           0 :             xSubTransaction->commit();
     436             :         }
     437             :         uno::Reference<embed::XTransactedObject> const xTransaction(
     438           0 :             xStorage, uno::UNO_QUERY);
     439           0 :         if (xTransaction.is()) {
     440           0 :             xTransaction->commit();
     441             :         }
     442             : 
     443           0 :         o_rEmbeddedURL = "vnd.sun.star.Package:" + media + "/" + filename;
     444           0 :         return true;
     445             :     }
     446           0 :     catch (uno::Exception const&)
     447             :     {
     448             :         SAL_WARN("avmedia",
     449             :                 "Exception while trying to embed media");
     450             :     }
     451           0 :     return false;
     452             : }
     453             : }
     454             : 
     455             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10