1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <avmedia/mediawindow.hxx>
21 #include "mediawindow_impl.hxx"
22 #include "mediamisc.hxx"
23 #include "bitmaps.hlst"
24 #include "mediawindow.hrc"
25 #include <tools/urlobj.hxx>
26 #include <vcl/layout.hxx>
27 #include <unotools/pathoptions.hxx>
28 #include <sfx2/filedlghelper.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/media/XManager.hpp>
32 #include "com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp"
33 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
34 #include "com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp"
37 #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
39 using namespace ::com::sun::star
;
43 MediaWindow::MediaWindow( vcl::Window
* parent
, bool bInternalMediaControl
) :
44 mpImpl( VclPtr
<priv::MediaWindowImpl
>::Create( parent
, this, bInternalMediaControl
) )
50 MediaWindow::~MediaWindow()
52 mpImpl
.disposeAndClear();
56 void MediaWindow::setURL( const OUString
& rURL
, const OUString
& rReferer
)
58 mpImpl
->setURL( rURL
, OUString(), rReferer
);
62 const OUString
& MediaWindow::getURL() const
64 return mpImpl
->getURL();
68 bool MediaWindow::isValid() const
70 return mpImpl
->isValid();
74 void MediaWindow::MouseMove( const MouseEvent
& )
79 void MediaWindow::MouseButtonDown( const MouseEvent
& )
84 void MediaWindow::MouseButtonUp( const MouseEvent
& )
89 void MediaWindow::KeyInput( const KeyEvent
& )
94 void MediaWindow::KeyUp( const KeyEvent
& )
98 void MediaWindow::Command( const CommandEvent
& )
103 sal_Int8
MediaWindow::AcceptDrop( const AcceptDropEvent
& )
109 sal_Int8
MediaWindow::ExecuteDrop( const ExecuteDropEvent
& )
115 void MediaWindow::StartDrag( sal_Int8
, const Point
& )
120 Size
MediaWindow::getPreferredSize() const
122 return mpImpl
->getPreferredSize();
126 void MediaWindow::setPosSize( const tools::Rectangle
& rNewRect
)
128 mpImpl
->setPosSize( rNewRect
);
132 void MediaWindow::setPointer( const Pointer
& rPointer
)
134 mpImpl
->setPointer( rPointer
);
138 bool MediaWindow::start()
140 return mpImpl
->start();
144 void MediaWindow::updateMediaItem( MediaItem
& rItem
) const
146 mpImpl
->updateMediaItem( rItem
);
150 void MediaWindow::executeMediaItem( const MediaItem
& rItem
)
152 mpImpl
->executeMediaItem( rItem
);
156 void MediaWindow::show()
162 void MediaWindow::hide()
168 vcl::Window
* MediaWindow::getWindow() const
174 void MediaWindow::getMediaFilters( FilterNameVector
& rFilterNameVector
)
176 static const char* pFilters
[] = { "Advanced Audio Coding", "aac",
177 "AIF Audio", "aif;aiff",
178 "Advanced Systems Format", "asf;wma;wmv",
183 "Digital Video", "dv",
184 "FLAC Audio", "flac",
185 "Flash Video", "flv",
186 "Matroska Media", "mkv",
187 "MIDI Audio", "mid;midi",
188 "MPEG Audio", "mp2;mp3;mpa;m4a",
189 "MPEG Video", "mpg;mpeg;mpv;mp4;m4v",
190 "Ogg Audio", "ogg;oga;opus",
191 "Ogg Video", "ogv;ogx",
194 "RMI MIDI Audio", "rmi",
195 "SND (SouND) Audio", "snd",
196 "Quicktime Video", "mov",
199 "WebM Video", "webm",
200 "Windows Media Audio", "wma",
201 "Windows Media Video", "wmv"};
203 for( size_t i
= 0; i
< SAL_N_ELEMENTS(pFilters
); i
+= 2 )
205 rFilterNameVector
.push_back( std::make_pair
< OUString
, OUString
>(
206 OUString::createFromAscii(pFilters
[i
]),
207 OUString::createFromAscii(pFilters
[i
+1]) ) );
212 bool MediaWindow::executeMediaURLDialog(OUString
& rURL
, bool *const o_pbLink
)
214 ::sfx2::FileDialogHelper
aDlg( o_pbLink
215 ? ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW
216 : ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
);
217 static const char aWildcard
[] = "*.";
218 FilterNameVector aFilters
;
219 static const char aSeparator
[] = ";";
222 aDlg
.SetTitle( AvmResId( (o_pbLink
)
223 ? AVMEDIA_STR_INSERTMEDIA_DLG
: AVMEDIA_STR_OPENMEDIA_DLG
) );
225 getMediaFilters( aFilters
);
227 for( FilterNameVector::size_type i
= 0; i
< aFilters
.size(); ++i
)
229 for( sal_Int32 nIndex
= 0; nIndex
>= 0; )
231 if( !aAllTypes
.isEmpty() )
232 aAllTypes
+= aSeparator
;
234 ( aAllTypes
+= aWildcard
) += aFilters
[ i
].second
.getToken( 0, ';', nIndex
);
238 // add filter for all media types
239 aDlg
.AddFilter( AvmResId( AVMEDIA_STR_ALL_MEDIAFILES
), aAllTypes
);
241 for( FilterNameVector::size_type i
= 0; i
< aFilters
.size(); ++i
)
245 for( sal_Int32 nIndex
= 0; nIndex
>= 0; )
247 if( !aTypes
.isEmpty() )
248 aTypes
+= aSeparator
;
250 ( aTypes
+= aWildcard
) += aFilters
[ i
].second
.getToken( 0, ';', nIndex
);
253 // add single filters
254 aDlg
.AddFilter( aFilters
[ i
].first
, aTypes
);
257 // add filter for all types
258 aDlg
.AddFilter( AvmResId( AVMEDIA_STR_ALL_FILES
), "*.*" );
260 uno::Reference
<ui::dialogs::XFilePicker2
> const xFP(aDlg
.GetFilePicker());
261 uno::Reference
<ui::dialogs::XFilePickerControlAccess
> const xCtrlAcc(xFP
,
262 uno::UNO_QUERY_THROW
);
265 // for video link should be the default
267 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0,
269 // disabled for now: TODO: preview?
270 xCtrlAcc
->enableControl(
271 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_PREVIEW
,
275 if( aDlg
.Execute() == ERRCODE_NONE
)
277 const INetURLObject
aURL( aDlg
.GetPath() );
278 rURL
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
);
282 uno::Any
const any
= xCtrlAcc
->getValue(
283 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0);
284 if (!(any
>>= *o_pbLink
))
286 SAL_WARN("avmedia", "invalid link property");
291 else if( !rURL
.isEmpty() )
294 return !rURL
.isEmpty();
298 void MediaWindow::executeFormatErrorBox( vcl::Window
* pParent
)
300 ScopedVclPtrInstance
< MessageDialog
> aErrBox( pParent
, AvmResId( AVMEDIA_STR_ERR_URL
) );
303 aErrBox
.disposeAndClear();
307 bool MediaWindow::isMediaURL( const OUString
& rURL
, const OUString
& rReferer
, bool bDeep
, Size
* pPreferredSizePixel
)
309 const INetURLObject
aURL( rURL
);
312 if( aURL
.GetProtocol() != INetProtocol::NotValid
)
314 if( bDeep
|| pPreferredSizePixel
)
318 uno::Reference
< media::XPlayer
> xPlayer( priv::MediaWindowImpl::createPlayer(
319 aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
),
320 rReferer
, nullptr ) );
326 if( pPreferredSizePixel
)
328 const awt::Size
aAwtSize( xPlayer
->getPreferredPlayerWindowSize() );
330 pPreferredSizePixel
->Width() = aAwtSize
.Width
;
331 pPreferredSizePixel
->Height() = aAwtSize
.Height
;
341 FilterNameVector aFilters
;
342 const OUString
aExt( aURL
.getExtension() );
344 getMediaFilters( aFilters
);
346 for( FilterNameVector::size_type i
= 0; ( i
< aFilters
.size() ) && !bRet
; ++i
)
348 for( sal_Int32 nIndex
= 0; nIndex
>= 0 && !bRet
; )
350 if( aExt
.equalsIgnoreAsciiCase( aFilters
[ i
].second
.getToken( 0, ';', nIndex
) ) )
361 uno::Reference
< media::XPlayer
> MediaWindow::createPlayer( const OUString
& rURL
, const OUString
& rReferer
, const OUString
* pMimeType
)
363 return priv::MediaWindowImpl::createPlayer( rURL
, rReferer
, pMimeType
);
367 uno::Reference
< graphic::XGraphic
> MediaWindow::grabFrame( const OUString
& rURL
,
368 const OUString
& rReferer
,
369 const OUString
& sMimeType
)
371 uno::Reference
< media::XPlayer
> xPlayer( createPlayer( rURL
, rReferer
, &sMimeType
) );
372 uno::Reference
< graphic::XGraphic
> xRet
;
373 std::unique_ptr
< Graphic
> xGraphic
;
377 uno::Reference
< media::XFrameGrabber
> xGrabber( xPlayer
->createFrameGrabber() );
381 double fMediaTime
= AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME
;
383 if( fMediaTime
>= xPlayer
->getDuration() )
384 fMediaTime
= ( xPlayer
->getDuration() * 0.5 );
386 xRet
= xGrabber
->grabFrame( fMediaTime
);
391 awt::Size
aPrefSize( xPlayer
->getPreferredPlayerWindowSize() );
393 if( !aPrefSize
.Width
&& !aPrefSize
.Height
)
395 const BitmapEx
aBmpEx(AVMEDIA_BMP_AUDIOLOGO
);
396 xGraphic
.reset( new Graphic( aBmpEx
) );
401 if( !xRet
.is() && !xGraphic
.get() )
403 const BitmapEx
aBmpEx(AVMEDIA_BMP_EMPTYLOGO
);
404 xGraphic
.reset( new Graphic( aBmpEx
) );
408 xRet
= xGraphic
->GetXGraphic();
414 } // namespace avmedia
416 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */