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/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 <unotools/pathoptions.hxx>
30 #include <sfx2/filedlghelper.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 #include <memory>
37 #include <sal/log.hxx>
38 
39 #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
40 
41 using namespace ::com::sun::star;
42 
43 namespace avmedia {
44 
MediaWindow(vcl::Window * parent,bool bInternalMediaControl)45 MediaWindow::MediaWindow( vcl::Window* parent, bool bInternalMediaControl ) :
46     mpImpl( VclPtr<priv::MediaWindowImpl>::Create( parent, this, bInternalMediaControl ) )
47 {
48     mpImpl->Show();
49 }
50 
51 
~MediaWindow()52 MediaWindow::~MediaWindow()
53 {
54     mpImpl.disposeAndClear();
55 }
56 
57 
setURL(const OUString & rURL,const OUString & rReferer)58 void MediaWindow::setURL( const OUString& rURL, const OUString& rReferer )
59 {
60     mpImpl->setURL( rURL, OUString(), rReferer );
61 }
62 
63 
getURL() const64 const OUString& MediaWindow::getURL() const
65 {
66     return mpImpl->getURL();
67 }
68 
69 
isValid() const70 bool MediaWindow::isValid() const
71 {
72     return mpImpl->isValid();
73 }
74 
75 
MouseMove(const MouseEvent &)76 void MediaWindow::MouseMove( const MouseEvent& )
77 {
78 }
79 
80 
MouseButtonDown(const MouseEvent &)81 void MediaWindow::MouseButtonDown( const MouseEvent& )
82 {
83 }
84 
85 
MouseButtonUp(const MouseEvent &)86 void MediaWindow::MouseButtonUp( const MouseEvent& )
87 {
88 }
89 
90 
KeyInput(const KeyEvent &)91 void MediaWindow::KeyInput( const KeyEvent& )
92 {
93 }
94 
95 
KeyUp(const KeyEvent &)96 void MediaWindow::KeyUp( const KeyEvent& )
97 {
98 }
99 
Command(const CommandEvent &)100 void MediaWindow::Command( const CommandEvent& )
101 {
102 }
103 
104 
AcceptDrop(const AcceptDropEvent &)105 sal_Int8 MediaWindow::AcceptDrop( const AcceptDropEvent& )
106 {
107     return 0;
108 }
109 
110 
ExecuteDrop(const ExecuteDropEvent &)111 sal_Int8 MediaWindow::ExecuteDrop( const ExecuteDropEvent& )
112 {
113     return 0;
114 }
115 
116 
StartDrag(sal_Int8,const Point &)117 void MediaWindow::StartDrag( sal_Int8, const Point& )
118 {
119 }
120 
121 
getPreferredSize() const122 Size MediaWindow::getPreferredSize() const
123 {
124     return mpImpl->getPreferredSize();
125 }
126 
127 
setPosSize(const tools::Rectangle & rNewRect)128 void MediaWindow::setPosSize( const tools::Rectangle& rNewRect )
129 {
130     mpImpl->setPosSize( rNewRect );
131 }
132 
133 
setPointer(PointerStyle nPointer)134 void MediaWindow::setPointer( PointerStyle nPointer )
135 {
136     mpImpl->setPointer( nPointer );
137 }
138 
139 
start()140 bool MediaWindow::start()
141 {
142     return mpImpl->start();
143 }
144 
145 
updateMediaItem(MediaItem & rItem) const146 void MediaWindow::updateMediaItem( MediaItem& rItem ) const
147 {
148     mpImpl->updateMediaItem( rItem );
149 }
150 
151 
executeMediaItem(const MediaItem & rItem)152 void MediaWindow::executeMediaItem( const MediaItem& rItem )
153 {
154     mpImpl->executeMediaItem( rItem );
155 }
156 
157 
show()158 void MediaWindow::show()
159 {
160     mpImpl->Show();
161 }
162 
163 
hide()164 void MediaWindow::hide()
165 {
166     mpImpl->Hide();
167 }
168 
169 
getWindow() const170 vcl::Window* MediaWindow::getWindow() const
171 {
172     return mpImpl.get();
173 }
174 
175 
getMediaFilters()176 FilterNameVector MediaWindow::getMediaFilters()
177 {
178     return {{"Advanced Audio Coding", "aac"},
179             {"AIF Audio", "aif;aiff"},
180             {"Advanced Systems Format", "asf;wma;wmv"},
181             {"AU Audio", "au"},
182             {"AC3 Audio", "ac3"},
183             {"AVI", "avi"},
184             {"CD Audio", "cda"},
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"}};
204 }
205 
206 
executeMediaURLDialog(weld::Window * pParent,OUString & rURL,bool * const o_pbLink)207 bool MediaWindow::executeMediaURLDialog(weld::Window* pParent, OUString& rURL, bool *const o_pbLink)
208 {
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;
217 
218     aDlg.SetTitle( AvmResId( o_pbLink != nullptr
219                 ? AVMEDIA_STR_INSERTMEDIA_DLG : AVMEDIA_STR_OPENMEDIA_DLG ) );
220 
221     for( FilterNameVector::size_type i = 0; i < aFilters.size(); ++i )
222     {
223         for( sal_Int32 nIndex = 0; nIndex >= 0; )
224         {
225             if( !aAllTypes.isEmpty() )
226                 aAllTypes.append(aSeparator);
227 
228             aAllTypes.append(aWildcard).append(aFilters[ i ].second.getToken( 0, ';', nIndex ));
229         }
230     }
231 
232     // add filter for all media types
233     aDlg.AddFilter( AvmResId( AVMEDIA_STR_ALL_MEDIAFILES ), aAllTypes.makeStringAndClear() );
234 
235     for( FilterNameVector::size_type i = 0; i < aFilters.size(); ++i )
236     {
237         OUStringBuffer aTypes;
238 
239         for( sal_Int32 nIndex = 0; nIndex >= 0; )
240         {
241             if( !aTypes.isEmpty() )
242                 aTypes.append(aSeparator);
243 
244             aTypes.append(aWildcard).append(aFilters[ i ].second.getToken( 0, ';', nIndex ));
245         }
246 
247         // add single filters
248         aDlg.AddFilter( aFilters[ i ].first, aTypes.makeStringAndClear() );
249     }
250 
251     // add filter for all types
252     aDlg.AddFilter( AvmResId( AVMEDIA_STR_ALL_FILES ), "*.*" );
253 
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)
258     {
259         // for video link should be the default
260         xCtrlAcc->setValue(
261                 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0,
262                 uno::Any(true) );
263         // disabled for now: TODO: preview?
264         xCtrlAcc->enableControl(
265                 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_PREVIEW,
266                 false);
267     }
268 
269     if( aDlg.Execute() == ERRCODE_NONE )
270     {
271         const INetURLObject aURL( aDlg.GetPath() );
272         rURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous );
273 
274         if (o_pbLink != nullptr)
275         {
276             uno::Any const any = xCtrlAcc->getValue(
277                 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0);
278             if (!(any >>= *o_pbLink))
279             {
280                 SAL_WARN("avmedia", "invalid link property");
281                 *o_pbLink = true;
282             }
283         }
284     }
285     else if( !rURL.isEmpty() )
286         rURL.clear();
287 
288     return !rURL.isEmpty();
289 }
290 
executeFormatErrorBox(weld::Window * pParent)291 void MediaWindow::executeFormatErrorBox(weld::Window* pParent)
292 {
293     std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent,
294                                               VclMessageType::Warning, VclButtonsType::Ok, AvmResId(AVMEDIA_STR_ERR_URL)));
295     xBox->run();
296 }
297 
isMediaURL(const OUString & rURL,const OUString & rReferer,bool bDeep,Size * pPreferredSizePixel)298 bool MediaWindow::isMediaURL( const OUString& rURL, const OUString& rReferer, bool bDeep, Size* pPreferredSizePixel )
299 {
300     const INetURLObject aURL( rURL );
301 
302     if( aURL.GetProtocol() != INetProtocol::NotValid )
303     {
304         if( bDeep || pPreferredSizePixel )
305         {
306             try
307             {
308                 uno::Reference< media::XPlayer > xPlayer( priv::MediaWindowImpl::createPlayer(
309                                                             aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ),
310                                                             rReferer, nullptr ) );
311 
312                 if( xPlayer.is() )
313                 {
314                     if( pPreferredSizePixel )
315                     {
316                         const awt::Size aAwtSize( xPlayer->getPreferredPlayerWindowSize() );
317 
318                         pPreferredSizePixel->setWidth( aAwtSize.Width );
319                         pPreferredSizePixel->setHeight( aAwtSize.Height );
320                     }
321 
322                     return true;
323                 }
324             }
325             catch( ... )
326             {
327             }
328         }
329         else
330         {
331             FilterNameVector        aFilters = getMediaFilters();
332             const OUString          aExt( aURL.getExtension() );
333 
334             for( FilterNameVector::size_type i = 0; i < aFilters.size(); ++i )
335             {
336                 for( sal_Int32 nIndex = 0; nIndex >= 0; )
337                 {
338                     if( aExt.equalsIgnoreAsciiCase( aFilters[ i ].second.getToken( 0, ';', nIndex ) ) )
339                         return true;
340                 }
341             }
342         }
343     }
344 
345     return false;
346 }
347 
348 
createPlayer(const OUString & rURL,const OUString & rReferer,const OUString * pMimeType)349 uno::Reference< media::XPlayer > MediaWindow::createPlayer( const OUString& rURL, const OUString& rReferer, const OUString* pMimeType )
350 {
351     return priv::MediaWindowImpl::createPlayer( rURL, rReferer, pMimeType );
352 }
353 
354 
grabFrame(const OUString & rURL,const OUString & rReferer,const OUString & sMimeType)355 uno::Reference< graphic::XGraphic > MediaWindow::grabFrame( const OUString& rURL,
356                                                             const OUString& rReferer,
357                                                             const OUString& sMimeType )
358 {
359     uno::Reference< media::XPlayer >    xPlayer( createPlayer( rURL, rReferer, &sMimeType ) );
360     uno::Reference< graphic::XGraphic > xRet;
361     std::unique_ptr< Graphic > xGraphic;
362 
363     if( xPlayer.is() )
364     {
365         uno::Reference< media::XFrameGrabber > xGrabber( xPlayer->createFrameGrabber() );
366 
367         if( xGrabber.is() )
368         {
369             double fMediaTime = AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME;
370 
371             if( fMediaTime >= xPlayer->getDuration() )
372                 fMediaTime = ( xPlayer->getDuration() * 0.5 );
373 
374             xRet = xGrabber->grabFrame( fMediaTime );
375         }
376 
377         if( !xRet.is() )
378         {
379             awt::Size aPrefSize( xPlayer->getPreferredPlayerWindowSize() );
380 
381             if( !aPrefSize.Width && !aPrefSize.Height )
382             {
383                 const BitmapEx aBmpEx(AVMEDIA_BMP_AUDIOLOGO);
384                 xGraphic.reset( new Graphic( aBmpEx ) );
385             }
386         }
387     }
388 
389     if (!xRet.is() && !xGraphic)
390     {
391         const BitmapEx aBmpEx(AVMEDIA_BMP_EMPTYLOGO);
392         xGraphic.reset( new Graphic( aBmpEx ) );
393     }
394 
395     if (xGraphic)
396         xRet = xGraphic->GetXGraphic();
397 
398     return xRet;
399 }
400 
401 
402 } // namespace avmedia
403 
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
405