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 <stdio.h>
21 :
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"
36 :
37 : #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
38 :
39 : using namespace ::com::sun::star;
40 :
41 : namespace avmedia {
42 :
43 : // ---------------
44 : // - MediaWindow -
45 : // ---------------
46 :
47 0 : MediaWindow::MediaWindow( Window* parent, bool bInternalMediaControl ) :
48 0 : mpImpl( new priv::MediaWindowImpl( parent, this, bInternalMediaControl ) )
49 : {
50 0 : mpImpl->Show();
51 0 : }
52 :
53 : // -------------------------------------------------------------------------
54 :
55 0 : MediaWindow::~MediaWindow()
56 : {
57 0 : mpImpl->cleanUp();
58 0 : delete mpImpl;
59 0 : mpImpl = NULL;
60 0 : }
61 :
62 : // -------------------------------------------------------------------------
63 :
64 0 : void MediaWindow::setURL( const OUString& rURL )
65 : {
66 0 : if( mpImpl )
67 0 : mpImpl->setURL( rURL, OUString() );
68 0 : }
69 :
70 : // -------------------------------------------------------------------------
71 :
72 0 : const OUString& MediaWindow::getURL() const
73 : {
74 0 : return mpImpl->getURL();
75 : }
76 :
77 : // -------------------------------------------------------------------------
78 :
79 0 : bool MediaWindow::isValid() const
80 : {
81 0 : return( mpImpl != NULL && mpImpl->isValid() );
82 : }
83 :
84 : // -------------------------------------------------------------------------
85 :
86 0 : void MediaWindow::MouseMove( const MouseEvent& /* rMEvt */ )
87 : {
88 0 : }
89 :
90 : // ---------------------------------------------------------------------
91 :
92 0 : void MediaWindow::MouseButtonDown( const MouseEvent& /* rMEvt */ )
93 : {
94 0 : }
95 :
96 : // ---------------------------------------------------------------------
97 :
98 0 : void MediaWindow::MouseButtonUp( const MouseEvent& /* rMEvt */ )
99 : {
100 0 : }
101 :
102 : // -------------------------------------------------------------------------
103 :
104 0 : void MediaWindow::KeyInput( const KeyEvent& /* rKEvt */ )
105 : {
106 0 : }
107 :
108 : // -------------------------------------------------------------------------
109 :
110 0 : void MediaWindow::KeyUp( const KeyEvent& /* rKEvt */ )
111 : {
112 0 : }
113 :
114 : // -------------------------------------------------------------------------
115 :
116 0 : void MediaWindow::Command( const CommandEvent& /* rCEvt */ )
117 : {
118 0 : }
119 :
120 : // -------------------------------------------------------------------------
121 :
122 0 : sal_Int8 MediaWindow::AcceptDrop( const AcceptDropEvent& /* rEvt */ )
123 : {
124 0 : return 0;
125 : }
126 :
127 : // -------------------------------------------------------------------------
128 :
129 0 : sal_Int8 MediaWindow::ExecuteDrop( const ExecuteDropEvent& /* rEvt */ )
130 : {
131 0 : return 0;
132 : }
133 :
134 : // -------------------------------------------------------------------------
135 :
136 0 : void MediaWindow::StartDrag( sal_Int8 /* nAction */, const Point& /* rPosPixel */ )
137 : {
138 0 : }
139 :
140 : // -------------------------------------------------------------------------
141 :
142 0 : Size MediaWindow::getPreferredSize() const
143 : {
144 0 : return mpImpl->getPreferredSize();
145 : }
146 :
147 : // -------------------------------------------------------------------------
148 :
149 0 : void MediaWindow::setPosSize( const Rectangle& rNewRect )
150 : {
151 0 : if( mpImpl )
152 : {
153 0 : mpImpl->setPosSize( rNewRect );
154 : }
155 0 : }
156 :
157 : // -------------------------------------------------------------------------
158 :
159 0 : void MediaWindow::setPointer( const Pointer& rPointer )
160 : {
161 0 : if( mpImpl )
162 0 : mpImpl->setPointer( rPointer );
163 0 : }
164 :
165 : // -------------------------------------------------------------------------
166 :
167 0 : bool MediaWindow::start()
168 : {
169 0 : return( mpImpl != NULL && mpImpl->start() );
170 : }
171 :
172 : // -------------------------------------------------------------------------
173 :
174 0 : void MediaWindow::updateMediaItem( MediaItem& rItem ) const
175 : {
176 0 : if( mpImpl )
177 0 : mpImpl->updateMediaItem( rItem );
178 0 : }
179 :
180 : // -------------------------------------------------------------------------
181 :
182 0 : void MediaWindow::executeMediaItem( const MediaItem& rItem )
183 : {
184 0 : if( mpImpl )
185 0 : mpImpl->executeMediaItem( rItem );
186 0 : }
187 :
188 : // -------------------------------------------------------------------------
189 :
190 0 : void MediaWindow::show()
191 : {
192 0 : if( mpImpl )
193 0 : mpImpl->Show();
194 0 : }
195 :
196 : // -------------------------------------------------------------------------
197 :
198 0 : void MediaWindow::hide()
199 : {
200 0 : if( mpImpl )
201 0 : mpImpl->Hide();
202 0 : }
203 :
204 : // -------------------------------------------------------------------------
205 :
206 0 : Window* MediaWindow::getWindow() const
207 : {
208 0 : return mpImpl;
209 : }
210 :
211 : // -------------------------------------------------------------------------
212 :
213 35 : void MediaWindow::getMediaFilters( FilterNameVector& rFilterNameVector )
214 : {
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"};
236 :
237 770 : for( size_t i = 0; i < SAL_N_ELEMENTS(pFilters); i += 2 )
238 : {
239 : rFilterNameVector.push_back( ::std::make_pair< OUString, OUString >(
240 : OUString::createFromAscii(pFilters[i]),
241 735 : OUString::createFromAscii(pFilters[i+1]) ) );
242 : }
243 35 : }
244 :
245 : // -------------------------------------------------------------------------
246 :
247 0 : bool MediaWindow::executeMediaURLDialog(Window* /* pParent */,
248 : OUString& rURL, bool *const o_pbLink)
249 : {
250 : ::sfx2::FileDialogHelper aDlg( (o_pbLink)
251 : ? ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW
252 0 : : ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
253 0 : static const OUString aWildcard( "*." );
254 0 : FilterNameVector aFilters;
255 0 : const OUString aSeparator( ";" );
256 0 : OUString aAllTypes;
257 :
258 0 : aDlg.SetTitle( AVMEDIA_RESID( (o_pbLink)
259 0 : ? AVMEDIA_STR_INSERTMEDIA_DLG : AVMEDIA_STR_OPENMEDIA_DLG ) );
260 :
261 0 : getMediaFilters( aFilters );
262 :
263 : unsigned int i;
264 0 : for( i = 0; i < aFilters.size(); ++i )
265 : {
266 0 : for( sal_Int32 nIndex = 0; nIndex >= 0; )
267 : {
268 0 : if( !aAllTypes.isEmpty() )
269 0 : aAllTypes += aSeparator;
270 :
271 0 : ( aAllTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
272 : }
273 : }
274 :
275 : // add filter for all media types
276 0 : aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_MEDIAFILES ), aAllTypes );
277 :
278 0 : for( i = 0; i < aFilters.size(); ++i )
279 : {
280 0 : OUString aTypes;
281 :
282 0 : for( sal_Int32 nIndex = 0; nIndex >= 0; )
283 : {
284 0 : if( !aTypes.isEmpty() )
285 0 : aTypes += aSeparator;
286 :
287 0 : ( aTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
288 : }
289 :
290 : // add single filters
291 0 : aDlg.AddFilter( aFilters[ i ].first, aTypes );
292 0 : }
293 :
294 : // add filter for all types
295 0 : aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_FILES ), OUString( "*.*" ) );
296 :
297 0 : uno::Reference<ui::dialogs::XFilePicker> const xFP(aDlg.GetFilePicker());
298 : uno::Reference<ui::dialogs::XFilePickerControlAccess> const xCtrlAcc(xFP,
299 0 : uno::UNO_QUERY_THROW);
300 0 : if (o_pbLink)
301 : {
302 : // for video link should be the default
303 0 : xCtrlAcc->setValue(
304 : ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0,
305 0 : uno::makeAny(sal_True) );
306 : // disabled for now: TODO: preview?
307 0 : xCtrlAcc->enableControl(
308 : ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_PREVIEW,
309 0 : sal_False);
310 : }
311 :
312 0 : if( aDlg.Execute() == ERRCODE_NONE )
313 : {
314 0 : const INetURLObject aURL( aDlg.GetPath() );
315 0 : rURL = aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
316 :
317 0 : if (o_pbLink)
318 : {
319 0 : uno::Any const any = xCtrlAcc->getValue(
320 0 : ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0);
321 0 : if (!(any >>= *o_pbLink))
322 : {
323 : SAL_WARN("avmedia", "invalid link property");
324 0 : *o_pbLink = true;
325 0 : }
326 0 : }
327 : }
328 0 : else if( !rURL.isEmpty() )
329 0 : rURL = OUString();
330 :
331 0 : return( !rURL.isEmpty() );
332 : }
333 :
334 : // -------------------------------------------------------------------------
335 :
336 0 : void MediaWindow::executeFormatErrorBox( Window* pParent )
337 : {
338 0 : ErrorBox aErrBox( pParent, AVMEDIA_RESID( AVMEDIA_ERR_URL ) );
339 :
340 0 : aErrBox.Execute();
341 0 : }
342 :
343 : // -------------------------------------------------------------------------
344 :
345 35 : bool MediaWindow::isMediaURL( const OUString& rURL, bool bDeep, Size* pPreferredSizePixel )
346 : {
347 35 : const INetURLObject aURL( rURL );
348 35 : bool bRet = false;
349 :
350 35 : if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
351 : {
352 35 : if( bDeep || pPreferredSizePixel )
353 : {
354 : try
355 : {
356 : uno::Reference< media::XPlayer > xPlayer( priv::MediaWindowImpl::createPlayer(
357 0 : aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) ) );
358 :
359 0 : if( xPlayer.is() )
360 : {
361 0 : bRet = true;
362 :
363 0 : if( pPreferredSizePixel )
364 : {
365 0 : const awt::Size aAwtSize( xPlayer->getPreferredPlayerWindowSize() );
366 :
367 0 : pPreferredSizePixel->Width() = aAwtSize.Width;
368 0 : pPreferredSizePixel->Height() = aAwtSize.Height;
369 : }
370 0 : }
371 : }
372 0 : catch( ... )
373 : {
374 0 : }
375 : }
376 : else
377 : {
378 35 : FilterNameVector aFilters;
379 70 : const OUString aExt( aURL.getExtension() );
380 :
381 35 : getMediaFilters( aFilters );
382 :
383 : unsigned int i;
384 665 : for( i = 0; ( i < aFilters.size() ) && !bRet; ++i )
385 : {
386 2275 : for( sal_Int32 nIndex = 0; nIndex >= 0 && !bRet; )
387 : {
388 1015 : if( aExt.equalsIgnoreAsciiCase( aFilters[ i ].second.getToken( 0, ';', nIndex ) ) )
389 35 : bRet = true;
390 : }
391 35 : }
392 : }
393 : }
394 :
395 35 : return bRet;
396 : }
397 :
398 : // -------------------------------------------------------------------------
399 :
400 0 : uno::Reference< media::XPlayer > MediaWindow::createPlayer( const OUString& rURL )
401 : {
402 0 : return priv::MediaWindowImpl::createPlayer( rURL );
403 : }
404 :
405 : // -------------------------------------------------------------------------
406 :
407 0 : uno::Reference< graphic::XGraphic > MediaWindow::grabFrame( const OUString& rURL,
408 : bool bAllowToCreateReplacementGraphic,
409 : double fMediaTime )
410 : {
411 0 : uno::Reference< media::XPlayer > xPlayer( createPlayer( rURL ) );
412 0 : uno::Reference< graphic::XGraphic > xRet;
413 0 : ::std::auto_ptr< Graphic > apGraphic;
414 :
415 0 : if( xPlayer.is() )
416 : {
417 0 : uno::Reference< media::XFrameGrabber > xGrabber( xPlayer->createFrameGrabber() );
418 :
419 0 : if( xGrabber.is() )
420 : {
421 0 : if( AVMEDIA_FRAMEGRABBER_DEFAULTFRAME == fMediaTime )
422 0 : fMediaTime = AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME;
423 :
424 0 : if( fMediaTime >= xPlayer->getDuration() )
425 0 : fMediaTime = ( xPlayer->getDuration() * 0.5 );
426 :
427 0 : xRet = xGrabber->grabFrame( fMediaTime );
428 : }
429 :
430 0 : if( !xRet.is() && bAllowToCreateReplacementGraphic )
431 : {
432 0 : awt::Size aPrefSize( xPlayer->getPreferredPlayerWindowSize() );
433 :
434 0 : if( !aPrefSize.Width && !aPrefSize.Height )
435 : {
436 0 : const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_AUDIOLOGO ) );
437 0 : apGraphic.reset( new Graphic( aBmpEx ) );
438 : }
439 0 : }
440 : }
441 :
442 0 : if( !xRet.is() && !apGraphic.get() && bAllowToCreateReplacementGraphic )
443 : {
444 0 : const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_EMPTYLOGO ) );
445 0 : apGraphic.reset( new Graphic( aBmpEx ) );
446 : }
447 :
448 0 : if( apGraphic.get() )
449 0 : xRet = apGraphic->GetXGraphic();
450 :
451 0 : return xRet;
452 : }
453 :
454 258 : } // namespace avemdia
455 :
456 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|