bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / dlg / filedlg.cxx
blob9c8d072e61ea4dd99c29f63e54bc7854ec23c957
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 <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
42 private:
43 friend class SdOpenSoundFileDialog;
45 css::uno::Reference< css::ui::dialogs::XFilePickerControlAccess > mxControlAccess;
47 css::uno::Reference< css::media::XPlayer > mxPlayer;
48 ImplSVEvent * mnPlaySoundEvent;
49 bool mbLabelPlaying;
50 Idle maUpdateIdle;
52 DECL_LINK( PlayMusicHdl, void *, void );
53 DECL_LINK( IsMusicStoppedHdl, Timer *, void );
55 public:
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 ) );
77 break;
81 IMPL_LINK_NOARG(SdFileDialog_Imp, PlayMusicHdl, void*, void)
83 maUpdateIdle.Stop();
84 mnPlaySoundEvent = nullptr;
86 if (mxPlayer.is())
88 if (mxPlayer->isPlaying())
89 mxPlayer->stop();
90 mxPlayer.clear();
93 if( mbLabelPlaying )
95 try
97 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
98 SdResId( STR_PLAY ) );
99 mbLabelPlaying = false;
101 catch(const css::lang::IllegalArgumentException&)
103 #ifdef DBG_UTIL
104 OSL_FAIL( "Cannot access play button" );
105 #endif
108 else
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 );
117 mxPlayer->start();
118 maUpdateIdle.Start();
120 catch (const css::uno::Exception&)
122 mxPlayer.clear();
124 #endif
125 if (mxPlayer.is())
129 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
130 SdResId( STR_STOP ) );
131 mbLabelPlaying = true;
133 catch (const css::lang::IllegalArgumentException&)
135 #ifdef DBG_UTIL
136 OSL_FAIL( "Cannot access play button" );
137 #endif
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();
152 return;
155 if( !mxControlAccess.is() )
156 return;
160 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
161 SdResId( STR_PLAY ) );
162 mbLabelPlaying = false;
164 catch (const css::lang::IllegalArgumentException&)
166 #ifdef DBG_UTIL
167 OSL_FAIL( "Cannot access play button" );
168 #endif
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() )
186 return;
190 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
191 SdResId( STR_PLAY ) );
193 catch (const css::lang::IllegalArgumentException&)
195 #ifdef DBG_UTIL
196 OSL_FAIL( "Cannot set play button label" );
197 #endif
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, "*.*");
216 // setup filter
217 #if defined UNX
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");
228 #else
229 aDescr = SdResId(STR_WAV_FILE);
230 mpImpl->AddFilter( aDescr, "*.wav;*.mp3;*.ogg" );
231 aDescr = SdResId(STR_MIDI_FILE);
232 mpImpl->AddFilter( aDescr, "*.mid" );
233 #endif
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);
254 batch->commit();
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: */