fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / avmedia / source / viewer / mediawindow.cxx
blob310d5640a72f953c1c9bfb6509da845e56d3cf45
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <stdio.h>
22 #include <avmedia/mediawindow.hxx>
23 #include "mediawindow_impl.hxx"
24 #include "mediamisc.hxx"
25 #include "mediawindow.hrc"
26 #include <tools/urlobj.hxx>
27 #include <vcl/msgbox.hxx>
28 #include <unotools/pathoptions.hxx>
29 #include <sfx2/filedlghelper.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/media/XManager.hpp>
33 #include "com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp"
34 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
35 #include "com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp"
37 #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
39 using namespace ::com::sun::star;
41 namespace avmedia {
43 // ---------------
44 // - MediaWindow -
45 // ---------------
47 MediaWindow::MediaWindow( Window* parent, bool bInternalMediaControl ) :
48 mpImpl( new priv::MediaWindowImpl( parent, this, bInternalMediaControl ) )
50 mpImpl->Show();
53 // -------------------------------------------------------------------------
55 MediaWindow::~MediaWindow()
57 mpImpl->cleanUp();
58 delete mpImpl;
59 mpImpl = NULL;
62 // -------------------------------------------------------------------------
64 void MediaWindow::setURL( const OUString& rURL )
66 if( mpImpl )
67 mpImpl->setURL( rURL, OUString() );
70 // -------------------------------------------------------------------------
72 const OUString& MediaWindow::getURL() const
74 return mpImpl->getURL();
77 // -------------------------------------------------------------------------
79 bool MediaWindow::isValid() const
81 return( mpImpl != NULL && mpImpl->isValid() );
84 // -------------------------------------------------------------------------
86 void MediaWindow::MouseMove( const MouseEvent& /* rMEvt */ )
90 // ---------------------------------------------------------------------
92 void MediaWindow::MouseButtonDown( const MouseEvent& /* rMEvt */ )
96 // ---------------------------------------------------------------------
98 void MediaWindow::MouseButtonUp( const MouseEvent& /* rMEvt */ )
102 // -------------------------------------------------------------------------
104 void MediaWindow::KeyInput( const KeyEvent& /* rKEvt */ )
108 // -------------------------------------------------------------------------
110 void MediaWindow::KeyUp( const KeyEvent& /* rKEvt */ )
114 // -------------------------------------------------------------------------
116 void MediaWindow::Command( const CommandEvent& /* rCEvt */ )
120 // -------------------------------------------------------------------------
122 sal_Int8 MediaWindow::AcceptDrop( const AcceptDropEvent& /* rEvt */ )
124 return 0;
127 // -------------------------------------------------------------------------
129 sal_Int8 MediaWindow::ExecuteDrop( const ExecuteDropEvent& /* rEvt */ )
131 return 0;
134 // -------------------------------------------------------------------------
136 void MediaWindow::StartDrag( sal_Int8 /* nAction */, const Point& /* rPosPixel */ )
140 // -------------------------------------------------------------------------
142 Size MediaWindow::getPreferredSize() const
144 return mpImpl->getPreferredSize();
147 // -------------------------------------------------------------------------
149 void MediaWindow::setPosSize( const Rectangle& rNewRect )
151 if( mpImpl )
153 mpImpl->setPosSize( rNewRect );
157 // -------------------------------------------------------------------------
159 void MediaWindow::setPointer( const Pointer& rPointer )
161 if( mpImpl )
162 mpImpl->setPointer( rPointer );
165 // -------------------------------------------------------------------------
167 bool MediaWindow::start()
169 return( mpImpl != NULL && mpImpl->start() );
172 // -------------------------------------------------------------------------
174 void MediaWindow::updateMediaItem( MediaItem& rItem ) const
176 if( mpImpl )
177 mpImpl->updateMediaItem( rItem );
180 // -------------------------------------------------------------------------
182 void MediaWindow::executeMediaItem( const MediaItem& rItem )
184 if( mpImpl )
185 mpImpl->executeMediaItem( rItem );
188 // -------------------------------------------------------------------------
190 void MediaWindow::show()
192 if( mpImpl )
193 mpImpl->Show();
196 // -------------------------------------------------------------------------
198 void MediaWindow::hide()
200 if( mpImpl )
201 mpImpl->Hide();
204 // -------------------------------------------------------------------------
206 Window* MediaWindow::getWindow() const
208 return mpImpl;
211 // -------------------------------------------------------------------------
213 void MediaWindow::getMediaFilters( FilterNameVector& rFilterNameVector )
215 static const char* pFilters[] = { "Advanced Audio Coding", "aac",
216 "AIF Audio", "aif;aiff",
217 "AU Audio", "au",
218 "AVI", "avi",
219 "CD Audio", "cda",
220 "FLAC Audio", "flac",
221 "Flash Video", "flv",
222 "Matroska Media", "mkv",
223 "MIDI Audio", "mid;midi",
224 "MPEG Audio", "mp2;mp3;mpa",
225 "MPEG Video", "mpg;mpeg;mpv;mp4",
226 "Ogg Audio", "ogg;oga;",
227 "Ogg Video", "ogv;ogx;",
228 "RMI MIDI Audio", "rmi",
229 "SND (SouND) Audio", "snd",
230 "Quicktime Video", "mov",
231 "Vivo Video", "viv",
232 "WAVE Audio", "wav",
233 "WebM Video", "webm",
234 "Windows Media Audio", "wma",
235 "Windows Media Video", "wmv"};
237 for( size_t i = 0; i < SAL_N_ELEMENTS(pFilters); i += 2 )
239 rFilterNameVector.push_back( ::std::make_pair< OUString, OUString >(
240 OUString::createFromAscii(pFilters[i]),
241 OUString::createFromAscii(pFilters[i+1]) ) );
245 // -------------------------------------------------------------------------
247 bool MediaWindow::executeMediaURLDialog(Window* /* pParent */,
248 OUString& rURL, bool *const o_pbLink)
250 ::sfx2::FileDialogHelper aDlg( (o_pbLink)
251 ? ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW
252 : ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
253 static const OUString aWildcard( "*." );
254 FilterNameVector aFilters;
255 const OUString aSeparator( ";" );
256 OUString aAllTypes;
258 aDlg.SetTitle( AVMEDIA_RESID( (o_pbLink)
259 ? AVMEDIA_STR_INSERTMEDIA_DLG : AVMEDIA_STR_OPENMEDIA_DLG ) );
261 getMediaFilters( aFilters );
263 unsigned int i;
264 for( i = 0; i < aFilters.size(); ++i )
266 for( sal_Int32 nIndex = 0; nIndex >= 0; )
268 if( !aAllTypes.isEmpty() )
269 aAllTypes += aSeparator;
271 ( aAllTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
275 // add filter for all media types
276 aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_MEDIAFILES ), aAllTypes );
278 for( i = 0; i < aFilters.size(); ++i )
280 OUString aTypes;
282 for( sal_Int32 nIndex = 0; nIndex >= 0; )
284 if( !aTypes.isEmpty() )
285 aTypes += aSeparator;
287 ( aTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
290 // add single filters
291 aDlg.AddFilter( aFilters[ i ].first, aTypes );
294 // add filter for all types
295 aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_FILES ), OUString( "*.*" ) );
297 uno::Reference<ui::dialogs::XFilePicker> const xFP(aDlg.GetFilePicker());
298 uno::Reference<ui::dialogs::XFilePickerControlAccess> const xCtrlAcc(xFP,
299 uno::UNO_QUERY_THROW);
300 if (o_pbLink)
302 // for video link should be the default
303 xCtrlAcc->setValue(
304 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0,
305 uno::makeAny(sal_True) );
306 // disabled for now: TODO: preview?
307 xCtrlAcc->enableControl(
308 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_PREVIEW,
309 sal_False);
312 if( aDlg.Execute() == ERRCODE_NONE )
314 const INetURLObject aURL( aDlg.GetPath() );
315 rURL = aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
317 if (o_pbLink)
319 uno::Any const any = xCtrlAcc->getValue(
320 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0);
321 if (!(any >>= *o_pbLink))
323 SAL_WARN("avmedia", "invalid link property");
324 *o_pbLink = true;
328 else if( !rURL.isEmpty() )
329 rURL = OUString();
331 return( !rURL.isEmpty() );
334 // -------------------------------------------------------------------------
336 void MediaWindow::executeFormatErrorBox( Window* pParent )
338 ErrorBox aErrBox( pParent, AVMEDIA_RESID( AVMEDIA_ERR_URL ) );
340 aErrBox.Execute();
343 // -------------------------------------------------------------------------
345 bool MediaWindow::isMediaURL( const OUString& rURL, bool bDeep, Size* pPreferredSizePixel )
347 const INetURLObject aURL( rURL );
348 bool bRet = false;
350 if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
352 if( bDeep || pPreferredSizePixel )
356 uno::Reference< media::XPlayer > xPlayer( priv::MediaWindowImpl::createPlayer(
357 aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) ) );
359 if( xPlayer.is() )
361 bRet = true;
363 if( pPreferredSizePixel )
365 const awt::Size aAwtSize( xPlayer->getPreferredPlayerWindowSize() );
367 pPreferredSizePixel->Width() = aAwtSize.Width;
368 pPreferredSizePixel->Height() = aAwtSize.Height;
372 catch( ... )
376 else
378 FilterNameVector aFilters;
379 const OUString aExt( aURL.getExtension() );
381 getMediaFilters( aFilters );
383 unsigned int i;
384 for( i = 0; ( i < aFilters.size() ) && !bRet; ++i )
386 for( sal_Int32 nIndex = 0; nIndex >= 0 && !bRet; )
388 if( aExt.equalsIgnoreAsciiCase( aFilters[ i ].second.getToken( 0, ';', nIndex ) ) )
389 bRet = true;
395 return bRet;
398 // -------------------------------------------------------------------------
400 uno::Reference< media::XPlayer > MediaWindow::createPlayer( const OUString& rURL )
402 return priv::MediaWindowImpl::createPlayer( rURL );
405 // -------------------------------------------------------------------------
407 uno::Reference< graphic::XGraphic > MediaWindow::grabFrame( const OUString& rURL,
408 bool bAllowToCreateReplacementGraphic,
409 double fMediaTime )
411 uno::Reference< media::XPlayer > xPlayer( createPlayer( rURL ) );
412 uno::Reference< graphic::XGraphic > xRet;
413 ::std::auto_ptr< Graphic > apGraphic;
415 if( xPlayer.is() )
417 uno::Reference< media::XFrameGrabber > xGrabber( xPlayer->createFrameGrabber() );
419 if( xGrabber.is() )
421 if( AVMEDIA_FRAMEGRABBER_DEFAULTFRAME == fMediaTime )
422 fMediaTime = AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME;
424 if( fMediaTime >= xPlayer->getDuration() )
425 fMediaTime = ( xPlayer->getDuration() * 0.5 );
427 xRet = xGrabber->grabFrame( fMediaTime );
430 if( !xRet.is() && bAllowToCreateReplacementGraphic )
432 awt::Size aPrefSize( xPlayer->getPreferredPlayerWindowSize() );
434 if( !aPrefSize.Width && !aPrefSize.Height )
436 const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_AUDIOLOGO ) );
437 apGraphic.reset( new Graphic( aBmpEx ) );
442 if( !xRet.is() && !apGraphic.get() && bAllowToCreateReplacementGraphic )
444 const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_EMPTYLOGO ) );
445 apGraphic.reset( new Graphic( aBmpEx ) );
448 if( apGraphic.get() )
449 xRet = apGraphic->GetXGraphic();
451 return xRet;
454 } // namespace avemdia
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */