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/XFilePickerControlAccess.hpp>
27 #include <com/sun/star/ui/dialogs/FilePickerEvent.hpp>
28 #include <vcl/idle.hxx>
29 #include <osl/diagnose.h>
30 #include <vcl/svapp.hxx>
31 #include <sfx2/filedlghelper.hxx>
32 #include <avmedia/mediawindow.hxx>
33 #include <filedlg.hxx>
34 #include <sdresid.hxx>
35 #include <strings.hrc>
36 #include <officecfg/Office/Impress.hxx>
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())
97 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
98 SdResId( STR_PLAY
) );
99 mbLabelPlaying
= false;
101 catch(const css::lang::IllegalArgumentException
&)
104 OSL_FAIL( "Cannot access play button" );
110 OUString
aUrl( GetPath() );
111 if ( !aUrl
.isEmpty() )
113 #if HAVE_FEATURE_AVMEDIA
116 mxPlayer
.set( avmedia::MediaWindow::createPlayer( aUrl
, "" ), 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" );
144 IMPL_LINK_NOARG(SdFileDialog_Imp
, IsMusicStoppedHdl
, Timer
*, void)
146 SolarMutexGuard aGuard
;
148 if (mxPlayer
.is() && mxPlayer
->isPlaying() &&
149 mxPlayer
->getMediaTime() < mxPlayer
->getDuration())
151 maUpdateIdle
.Start();
155 if( !mxControlAccess
.is() )
160 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
161 SdResId( STR_PLAY
) );
162 mbLabelPlaying
= false;
164 catch (const css::lang::IllegalArgumentException
&)
167 OSL_FAIL( "Cannot access play button" );
172 SdFileDialog_Imp::SdFileDialog_Imp(weld::Window
* pParent
)
173 : FileDialogHelper(css::ui::dialogs::TemplateDescription::FILEOPEN_LINK_PLAY
, FileDialogFlags::NONE
, pParent
)
174 , mnPlaySoundEvent(nullptr)
175 , mbLabelPlaying(false)
177 maUpdateIdle
.SetInvokeHandler(LINK(this, SdFileDialog_Imp
, IsMusicStoppedHdl
));
178 maUpdateIdle
.SetDebugName( "SdFileDialog_Imp maUpdateIdle" );
180 css::uno::Reference
< css::ui::dialogs::XFilePicker3
> xFileDlg
= GetFilePicker();
182 // get the control access
183 mxControlAccess
.set( xFileDlg
, css::uno::UNO_QUERY
);
185 if( !mxControlAccess
.is() )
190 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
191 SdResId( STR_PLAY
) );
193 catch (const css::lang::IllegalArgumentException
&)
196 OSL_FAIL( "Cannot set play button label" );
201 SdFileDialog_Imp::~SdFileDialog_Imp()
203 if( mnPlaySoundEvent
)
204 Application::RemoveUserEvent( mnPlaySoundEvent
);
207 // ----------- SdOpenSoundFileDialog -----------------------
209 // these are simple forwarders
210 SdOpenSoundFileDialog::SdOpenSoundFileDialog(weld::Window
*pParent
)
211 : mpImpl(new SdFileDialog_Imp(pParent
))
213 OUString aDescr
= SdResId(STR_ALL_FILES
);
214 mpImpl
->AddFilter( aDescr
, "*.*");
218 aDescr
= SdResId(STR_AU_FILE
);
219 mpImpl
->AddFilter( aDescr
, "*.au;*.snd");
220 aDescr
= SdResId(STR_VOC_FILE
);
221 mpImpl
->AddFilter( aDescr
, "*.voc");
222 aDescr
= SdResId(STR_WAV_FILE
);
223 mpImpl
->AddFilter( aDescr
, "*.wav");
224 aDescr
= SdResId(STR_AIFF_FILE
);
225 mpImpl
->AddFilter( aDescr
, "*.aiff");
226 aDescr
= SdResId(STR_SVX_FILE
);
227 mpImpl
->AddFilter( aDescr
, "*.svx");
229 aDescr
= SdResId(STR_WAV_FILE
);
230 mpImpl
->AddFilter( aDescr
, "*.wav;*.mp3;*.ogg" );
231 aDescr
= SdResId(STR_MIDI_FILE
);
232 mpImpl
->AddFilter( aDescr
, "*.mid" );
235 // Restore last selected path
236 mpImpl
->SetDisplayDirectory(officecfg::Office::Impress::Sound::Path::get());
239 SdOpenSoundFileDialog::~SdOpenSoundFileDialog()
243 ErrCode
SdOpenSoundFileDialog::Execute()
245 return mpImpl
->Execute();
248 OUString
SdOpenSoundFileDialog::GetPath() const
250 // Save last selected path
251 std::shared_ptr
<comphelper::ConfigurationChanges
> batch(
252 comphelper::ConfigurationChanges::create());
253 officecfg::Office::Impress::Sound::Path::set(mpImpl
->GetPath(), batch
);
256 return mpImpl
->GetPath();
259 void SdOpenSoundFileDialog::SetPath( const OUString
& rPath
)
261 mpImpl
->SetDisplayDirectory( rPath
);
264 // WIP, please don't remove, dear Clang plugins
265 bool SdOpenSoundFileDialog::IsInsertAsLinkSelected()
267 bool bInsertAsLinkSelected
= false;
268 css::uno::Reference
<css::ui::dialogs::XFilePicker3
> const xFilePicker(mpImpl
->GetFilePicker());
269 css::uno::Reference
<css::ui::dialogs::XFilePickerControlAccess
> const xControlAccess(xFilePicker
, css::uno::UNO_QUERY_THROW
);
270 xControlAccess
->getValue(css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK
, 0) >>= bInsertAsLinkSelected
;
271 return bInsertAsLinkSelected
;
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */