tdf#158198 sw: prevent xBookmark.getAnchor().setString("") from deleting
[LibreOffice.git] / sd / source / ui / dlg / filedlg.cxx
blob14a1125f570c11bc58e773a145bae311a6bd3f62
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/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
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 HAVE_FEATURE_AVMEDIA
94 if( mbLabelPlaying )
96 try
98 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
99 SdResId( STR_PLAY ) );
100 mbLabelPlaying = false;
102 catch(const css::lang::IllegalArgumentException&)
104 #ifdef DBG_UTIL
105 OSL_FAIL( "Cannot access play button" );
106 #endif
109 else
111 OUString aUrl( GetPath() );
112 if ( !aUrl.isEmpty() )
116 mxPlayer.set( avmedia::MediaWindow::createPlayer( aUrl, u""_ustr ), css::uno::UNO_SET_THROW );
117 mxPlayer->start();
118 maUpdateIdle.Start();
120 catch (const css::uno::Exception&)
122 mxPlayer.clear();
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
142 #endif
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();
153 return;
156 if( !mxControlAccess.is() )
157 return;
161 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
162 SdResId( STR_PLAY ) );
163 mbLabelPlaying = false;
165 catch (const css::lang::IllegalArgumentException&)
167 #ifdef DBG_UTIL
168 OSL_FAIL( "Cannot access play button" );
169 #endif
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() )
187 return;
191 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
192 SdResId( STR_PLAY ) );
194 catch (const css::lang::IllegalArgumentException&)
196 #ifdef DBG_UTIL
197 OSL_FAIL( "Cannot set play button label" );
198 #endif
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);
218 // setup filter
219 #if defined UNX
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);
230 #else
231 aDescr = SdResId(STR_WAV_FILE);
232 mpImpl->AddFilter( aDescr, "*.wav;*.mp3;*.ogg" );
233 aDescr = SdResId(STR_MIDI_FILE);
234 mpImpl->AddFilter( aDescr, "*.mid" );
235 #endif
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: */