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 <config_features.h>
22 #include <com/sun/star/media/XPlayer.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
25 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
26 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
27 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
28 #include <com/sun/star/ui/dialogs/FilePickerEvent.hpp>
29 #include <vcl/idle.hxx>
30 #include <osl/diagnose.h>
31 #include <vcl/svapp.hxx>
32 #include <sfx2/filedlghelper.hxx>
33 #include <avmedia/mediawindow.hxx>
34 #include <filedlg.hxx>
35 #include <sdresid.hxx>
36 #include <strings.hrc>
38 // ----------- SdFileDialog_Imp ---------------------------
40 class SdFileDialog_Imp
: public sfx2::FileDialogHelper
43 friend class SdOpenSoundFileDialog
;
45 css::uno::Reference
< css::ui::dialogs::XFilePickerControlAccess
> mxControlAccess
;
47 css::uno::Reference
< css::media::XPlayer
> mxPlayer
;
48 ImplSVEvent
* mnPlaySoundEvent
;
52 DECL_LINK( PlayMusicHdl
, void *, void );
53 DECL_LINK( IsMusicStoppedHdl
, Timer
*, void );
56 explicit SdFileDialog_Imp(weld::Window
*pParent
);
57 virtual ~SdFileDialog_Imp() override
;
59 // overwritten from FileDialogHelper, to receive user feedback
60 virtual void ControlStateChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
) override
;
63 void SdFileDialog_Imp::ControlStateChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
65 SolarMutexGuard aGuard
;
67 switch( aEvent
.ElementId
)
69 case css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
:
70 if( mxControlAccess
.is() )
72 if( mnPlaySoundEvent
)
73 Application::RemoveUserEvent( mnPlaySoundEvent
);
75 mnPlaySoundEvent
= Application::PostUserEvent( LINK( this, SdFileDialog_Imp
, PlayMusicHdl
) );
81 IMPL_LINK_NOARG(SdFileDialog_Imp
, PlayMusicHdl
, void*, void)
84 mnPlaySoundEvent
= nullptr;
88 if (mxPlayer
->isPlaying())
93 #if HAVE_FEATURE_AVMEDIA
98 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
99 SdResId( STR_PLAY
) );
100 mbLabelPlaying
= false;
102 catch(const css::lang::IllegalArgumentException
&)
105 OSL_FAIL( "Cannot access play button" );
111 OUString
aUrl( GetPath() );
112 if ( !aUrl
.isEmpty() )
116 mxPlayer
.set( avmedia::MediaWindow::createPlayer( aUrl
, u
""_ustr
), css::uno::UNO_SET_THROW
);
118 maUpdateIdle
.Start();
120 catch (const css::uno::Exception
&)
129 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
130 SdResId( STR_STOP
) );
131 mbLabelPlaying
= true;
133 catch (const css::lang::IllegalArgumentException
&)
136 OSL_FAIL( "Cannot access play button" );
145 IMPL_LINK_NOARG(SdFileDialog_Imp
, IsMusicStoppedHdl
, Timer
*, void)
147 SolarMutexGuard aGuard
;
149 if (mxPlayer
.is() && mxPlayer
->isPlaying() &&
150 mxPlayer
->getMediaTime() < mxPlayer
->getDuration())
152 maUpdateIdle
.Start();
156 if( !mxControlAccess
.is() )
161 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
162 SdResId( STR_PLAY
) );
163 mbLabelPlaying
= false;
165 catch (const css::lang::IllegalArgumentException
&)
168 OSL_FAIL( "Cannot access play button" );
173 SdFileDialog_Imp::SdFileDialog_Imp(weld::Window
* pParent
)
174 : FileDialogHelper(css::ui::dialogs::TemplateDescription::FILEOPEN_LINK_PLAY
, FileDialogFlags::NONE
, pParent
)
175 , mnPlaySoundEvent(nullptr)
176 , mbLabelPlaying(false)
177 , maUpdateIdle( "SdFileDialog_Imp maUpdateIdle" )
179 maUpdateIdle
.SetInvokeHandler(LINK(this, SdFileDialog_Imp
, IsMusicStoppedHdl
));
181 css::uno::Reference
< css::ui::dialogs::XFilePicker3
> xFileDlg
= GetFilePicker();
183 // get the control access
184 mxControlAccess
.set( xFileDlg
, css::uno::UNO_QUERY
);
186 if( !mxControlAccess
.is() )
191 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
192 SdResId( STR_PLAY
) );
194 catch (const css::lang::IllegalArgumentException
&)
197 OSL_FAIL( "Cannot set play button label" );
202 SdFileDialog_Imp::~SdFileDialog_Imp()
204 if( mnPlaySoundEvent
)
205 Application::RemoveUserEvent( mnPlaySoundEvent
);
208 // ----------- SdOpenSoundFileDialog -----------------------
210 // these are simple forwarders
211 SdOpenSoundFileDialog::SdOpenSoundFileDialog(weld::Window
*pParent
)
212 : mpImpl(new SdFileDialog_Imp(pParent
))
214 OUString aDescr
= SdResId(STR_ALL_FILES
);
215 mpImpl
->AddFilter( aDescr
, u
"*.*"_ustr
);
216 mpImpl
->SetContext(sfx2::FileDialogHelper::DrawImpressOpenSound
);
220 aDescr
= SdResId(STR_AU_FILE
);
221 mpImpl
->AddFilter( aDescr
, u
"*.au;*.snd"_ustr
);
222 aDescr
= SdResId(STR_VOC_FILE
);
223 mpImpl
->AddFilter( aDescr
, u
"*.voc"_ustr
);
224 aDescr
= SdResId(STR_WAV_FILE
);
225 mpImpl
->AddFilter( aDescr
, u
"*.wav"_ustr
);
226 aDescr
= SdResId(STR_AIFF_FILE
);
227 mpImpl
->AddFilter( aDescr
, u
"*.aiff"_ustr
);
228 aDescr
= SdResId(STR_SVX_FILE
);
229 mpImpl
->AddFilter( aDescr
, u
"*.svx"_ustr
);
231 aDescr
= SdResId(STR_WAV_FILE
);
232 mpImpl
->AddFilter( aDescr
, "*.wav;*.mp3;*.ogg" );
233 aDescr
= SdResId(STR_MIDI_FILE
);
234 mpImpl
->AddFilter( aDescr
, "*.mid" );
238 SdOpenSoundFileDialog::~SdOpenSoundFileDialog()
242 ErrCode
SdOpenSoundFileDialog::Execute()
244 return mpImpl
->Execute();
247 OUString
SdOpenSoundFileDialog::GetPath() const
249 return mpImpl
->GetPath();
252 void SdOpenSoundFileDialog::SetPath( const OUString
& rPath
)
254 mpImpl
->SetDisplayDirectory( rPath
);
257 // WIP, please don't remove, dear Clang plugins
258 bool SdOpenSoundFileDialog::IsInsertAsLinkSelected() const
260 bool bInsertAsLinkSelected
= false;
261 css::uno::Reference
<css::ui::dialogs::XFilePicker3
> const xFilePicker(mpImpl
->GetFilePicker());
262 css::uno::Reference
<css::ui::dialogs::XFilePickerControlAccess
> const xControlAccess(xFilePicker
, css::uno::UNO_QUERY_THROW
);
263 xControlAccess
->getValue(css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0) >>= bInsertAsLinkSelected
;
264 return bInsertAsLinkSelected
;
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */