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 <strings.hrc>
25 #include <tools/urlobj.hxx>
26 #include <vcl/graph.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/weld.hxx>
29 #include <sfx2/filedlghelper.hxx>
30 #include <com/sun/star/awt/Size.hpp>
31 #include <com/sun/star/media/XPlayer.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/XFilePicker3.hpp>
35 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
37 #include <sal/log.hxx>
39 #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
41 using namespace ::com::sun::star
;
45 MediaWindow::MediaWindow( vcl::Window
* parent
, bool bInternalMediaControl
) :
46 mpImpl( VclPtr
<priv::MediaWindowImpl
>::Create( parent
, this, bInternalMediaControl
) )
52 MediaWindow::~MediaWindow()
54 mpImpl
.disposeAndClear();
58 void MediaWindow::setURL( const OUString
& rURL
, const OUString
& rReferer
)
60 mpImpl
->setURL( rURL
, OUString(), rReferer
);
64 const OUString
& MediaWindow::getURL() const
66 return mpImpl
->getURL();
70 bool MediaWindow::isValid() const
72 return mpImpl
->isValid();
76 void MediaWindow::MouseMove( const MouseEvent
& )
81 void MediaWindow::MouseButtonDown( const MouseEvent
& )
86 void MediaWindow::MouseButtonUp( const MouseEvent
& )
91 void MediaWindow::KeyInput( const KeyEvent
& )
96 void MediaWindow::KeyUp( const KeyEvent
& )
100 void MediaWindow::Command( const CommandEvent
& )
105 sal_Int8
MediaWindow::AcceptDrop( const AcceptDropEvent
& )
111 sal_Int8
MediaWindow::ExecuteDrop( const ExecuteDropEvent
& )
117 void MediaWindow::StartDrag( sal_Int8
, const Point
& )
122 Size
MediaWindow::getPreferredSize() const
124 return mpImpl
->getPreferredSize();
128 void MediaWindow::setPosSize( const tools::Rectangle
& rNewRect
)
130 mpImpl
->setPosSize( rNewRect
);
134 void MediaWindow::setPointer( PointerStyle nPointer
)
136 mpImpl
->setPointer( nPointer
);
140 bool MediaWindow::start()
142 return mpImpl
->start();
146 void MediaWindow::updateMediaItem( MediaItem
& rItem
) const
148 mpImpl
->updateMediaItem( rItem
);
152 void MediaWindow::executeMediaItem( const MediaItem
& rItem
)
154 mpImpl
->executeMediaItem( rItem
);
158 void MediaWindow::show()
164 void MediaWindow::hide()
170 vcl::Window
* MediaWindow::getWindow() const
176 FilterNameVector
MediaWindow::getMediaFilters()
178 return {{"Advanced Audio Coding", "aac"},
179 {"AIF Audio", "aif;aiff"},
180 {"Advanced Systems Format", "asf;wma;wmv"},
182 {"AC3 Audio", "ac3"},
185 {"Digital Video", "dv"},
186 {"FLAC Audio", "flac"},
187 {"Flash Video", "flv"},
188 {"Matroska Media", "mkv"},
189 {"MIDI Audio", "mid;midi"},
190 {"MPEG Audio", "mp2;mp3;mpa;m4a"},
191 {"MPEG Video", "mpg;mpeg;mpv;mp4;m4v"},
192 {"Ogg Audio", "ogg;oga;opus"},
193 {"Ogg Video", "ogv;ogx"},
194 {"Real Audio", "ra"},
195 {"Real Media", "rm"},
196 {"RMI MIDI Audio", "rmi"},
197 {"SND (SouND) Audio", "snd"},
198 {"Quicktime Video", "mov"},
199 {"Vivo Video", "viv"},
200 {"WAVE Audio", "wav"},
201 {"WebM Video", "webm"},
202 {"Windows Media Audio", "wma"},
203 {"Windows Media Video", "wmv"}};
207 bool MediaWindow::executeMediaURLDialog(weld::Window
* pParent
, OUString
& rURL
, bool *const o_pbLink
)
209 ::sfx2::FileDialogHelper
aDlg(o_pbLink
!= nullptr
210 ? ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW
211 : ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
,
212 FileDialogFlags::NONE
, pParent
);
213 static const char aWildcard
[] = "*.";
214 FilterNameVector aFilters
= getMediaFilters();
215 static const char aSeparator
[] = ";";
216 OUStringBuffer aAllTypes
;
218 aDlg
.SetTitle( AvmResId( o_pbLink
!= nullptr
219 ? AVMEDIA_STR_INSERTMEDIA_DLG
: AVMEDIA_STR_OPENMEDIA_DLG
) );
221 for( FilterNameVector::size_type i
= 0; i
< aFilters
.size(); ++i
)
223 for( sal_Int32 nIndex
= 0; nIndex
>= 0; )
225 if( !aAllTypes
.isEmpty() )
226 aAllTypes
.append(aSeparator
);
228 aAllTypes
.append(aWildcard
).append(aFilters
[ i
].second
.getToken( 0, ';', nIndex
));
232 // add filter for all media types
233 aDlg
.AddFilter( AvmResId( AVMEDIA_STR_ALL_MEDIAFILES
), aAllTypes
.makeStringAndClear() );
235 for( FilterNameVector::size_type i
= 0; i
< aFilters
.size(); ++i
)
237 OUStringBuffer aTypes
;
239 for( sal_Int32 nIndex
= 0; nIndex
>= 0; )
241 if( !aTypes
.isEmpty() )
242 aTypes
.append(aSeparator
);
244 aTypes
.append(aWildcard
).append(aFilters
[ i
].second
.getToken( 0, ';', nIndex
));
247 // add single filters
248 aDlg
.AddFilter( aFilters
[ i
].first
, aTypes
.makeStringAndClear() );
251 // add filter for all types
252 aDlg
.AddFilter( AvmResId( AVMEDIA_STR_ALL_FILES
), "*.*" );
254 uno::Reference
<ui::dialogs::XFilePicker3
> const xFP(aDlg
.GetFilePicker());
255 uno::Reference
<ui::dialogs::XFilePickerControlAccess
> const xCtrlAcc(xFP
,
256 uno::UNO_QUERY_THROW
);
257 if (o_pbLink
!= nullptr)
259 // for video link should be the default
261 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0,
263 // disabled for now: TODO: preview?
264 xCtrlAcc
->enableControl(
265 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_PREVIEW
,
269 if( aDlg
.Execute() == ERRCODE_NONE
)
271 const INetURLObject
aURL( aDlg
.GetPath() );
272 rURL
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
);
274 if (o_pbLink
!= nullptr)
276 uno::Any
const any
= xCtrlAcc
->getValue(
277 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0);
278 if (!(any
>>= *o_pbLink
))
280 SAL_WARN("avmedia", "invalid link property");
285 else if( !rURL
.isEmpty() )
288 return !rURL
.isEmpty();
291 void MediaWindow::executeFormatErrorBox(weld::Window
* pParent
)
293 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(pParent
,
294 VclMessageType::Warning
, VclButtonsType::Ok
, AvmResId(AVMEDIA_STR_ERR_URL
)));
298 bool MediaWindow::isMediaURL( const OUString
& rURL
, const OUString
& rReferer
, bool bDeep
, Size
* pPreferredSizePixel
)
300 const INetURLObject
aURL( rURL
);
302 if( aURL
.GetProtocol() != INetProtocol::NotValid
)
304 if( bDeep
|| pPreferredSizePixel
)
308 uno::Reference
< media::XPlayer
> xPlayer( priv::MediaWindowImpl::createPlayer(
309 aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
),
310 rReferer
, nullptr ) );
314 if( pPreferredSizePixel
)
316 const awt::Size
aAwtSize( xPlayer
->getPreferredPlayerWindowSize() );
318 pPreferredSizePixel
->setWidth( aAwtSize
.Width
);
319 pPreferredSizePixel
->setHeight( aAwtSize
.Height
);
331 FilterNameVector aFilters
= getMediaFilters();
332 const OUString
aExt( aURL
.getExtension() );
334 for( FilterNameVector::size_type i
= 0; i
< aFilters
.size(); ++i
)
336 for( sal_Int32 nIndex
= 0; nIndex
>= 0; )
338 if( aExt
.equalsIgnoreAsciiCase( aFilters
[ i
].second
.getToken( 0, ';', nIndex
) ) )
349 uno::Reference
< media::XPlayer
> MediaWindow::createPlayer( const OUString
& rURL
, const OUString
& rReferer
, const OUString
* pMimeType
)
351 return priv::MediaWindowImpl::createPlayer( rURL
, rReferer
, pMimeType
);
355 uno::Reference
< graphic::XGraphic
> MediaWindow::grabFrame( const OUString
& rURL
,
356 const OUString
& rReferer
,
357 const OUString
& sMimeType
)
359 uno::Reference
< media::XPlayer
> xPlayer( createPlayer( rURL
, rReferer
, &sMimeType
) );
360 uno::Reference
< graphic::XGraphic
> xRet
;
361 std::unique_ptr
< Graphic
> xGraphic
;
365 uno::Reference
< media::XFrameGrabber
> xGrabber( xPlayer
->createFrameGrabber() );
369 double fMediaTime
= AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME
;
371 if( fMediaTime
>= xPlayer
->getDuration() )
372 fMediaTime
= ( xPlayer
->getDuration() * 0.5 );
374 xRet
= xGrabber
->grabFrame( fMediaTime
);
379 awt::Size
aPrefSize( xPlayer
->getPreferredPlayerWindowSize() );
381 if( !aPrefSize
.Width
&& !aPrefSize
.Height
)
383 const BitmapEx
aBmpEx(AVMEDIA_BMP_AUDIOLOGO
);
384 xGraphic
.reset( new Graphic( aBmpEx
) );
389 if (!xRet
.is() && !xGraphic
)
391 const BitmapEx
aBmpEx(AVMEDIA_BMP_EMPTYLOGO
);
392 xGraphic
.reset( new Graphic( aBmpEx
) );
396 xRet
= xGraphic
->GetXGraphic();
402 } // namespace avmedia
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */