bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / ui / dlg / filedlg.cxx
blob2c16005c715e16a6763b43dc163b5f4773882324
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 <tools/debug.hxx>
21 #include <cppuhelper/implbase1.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <com/sun/star/lang/XInitialization.hpp>
24 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
25 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
26 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
27 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
28 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
29 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
30 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
31 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
32 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
33 #include <vcl/msgbox.hxx>
34 #include <sal/types.h>
35 #include <osl/thread.hxx>
36 #include <osl/mutex.hxx>
37 #include <vcl/svapp.hxx>
38 #include <sfx2/filedlghelper.hxx>
39 #include <avmedia/mediawindow.hxx>
40 #include "filedlg.hxx"
41 #include "sdresid.hxx"
42 #include "strings.hrc"
43 #include <vcl/graphicfilter.hxx>
46 // --------------------------------------------------------------------
47 // ----------- SdFileDialog_Imp ---------------------------
48 // --------------------------------------------------------------------
49 class SdFileDialog_Imp : public sfx2::FileDialogHelper
51 private:
52 #if defined __SUNPRO_CC
53 using sfx2::FileDialogHelper::Execute;
54 #endif
56 friend class SdOpenSoundFileDialog;
58 css::uno::Reference< css::ui::dialogs::XFilePickerControlAccess > mxControlAccess;
60 css::uno::Reference< css::media::XPlayer > mxPlayer;
61 sal_uLong mnPlaySoundEvent;
62 sal_Bool mbUsableSelection;
63 sal_Bool mbLabelPlaying;
65 void CheckSelectionState();
67 DECL_LINK( PlayMusicHdl, void * );
69 Timer maUpdateTimer;
71 DECL_LINK( IsMusicStoppedHdl, void * );
73 public:
74 SdFileDialog_Imp( const short nDialogType, sal_Bool bUsableSelection );
75 ~SdFileDialog_Imp();
77 ErrCode Execute();
79 // overwritten from FileDialogHelper, to receive user feedback
80 virtual void SAL_CALL ControlStateChanged( const css::ui::dialogs::FilePickerEvent& aEvent );
83 // ------------------------------------------------------------------------
84 void SAL_CALL SdFileDialog_Imp::ControlStateChanged( const css::ui::dialogs::FilePickerEvent& aEvent )
86 SolarMutexGuard aGuard;
88 switch( aEvent.ElementId )
90 case css::ui::dialogs::CommonFilePickerElementIds::LISTBOX_FILTER:
91 CheckSelectionState();
92 break;
94 case css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
95 if( mxControlAccess.is() )
97 if( mnPlaySoundEvent )
98 Application::RemoveUserEvent( mnPlaySoundEvent );
100 mnPlaySoundEvent = Application::PostUserEvent( LINK( this, SdFileDialog_Imp, PlayMusicHdl ) );
102 break;
106 // ------------------------------------------------------------------------
107 IMPL_LINK_NOARG(SdFileDialog_Imp, PlayMusicHdl)
109 maUpdateTimer.Stop();
110 mnPlaySoundEvent = 0;
112 if (mxPlayer.is())
114 if (mxPlayer->isPlaying())
115 mxPlayer->stop();
116 mxPlayer.clear();
119 if( mbLabelPlaying )
123 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
124 String( SdResId( STR_PLAY ) ) );
126 mbLabelPlaying = sal_False;
128 catch(const css::lang::IllegalArgumentException&)
130 #ifdef DBG_UTIL
131 OSL_FAIL( "Cannot access play button" );
132 #endif
135 else
137 OUString aUrl( GetPath() );
138 if ( !aUrl.isEmpty() )
142 mxPlayer.set( avmedia::MediaWindow::createPlayer( aUrl ), css::uno::UNO_QUERY_THROW );
143 mxPlayer->start();
144 maUpdateTimer.SetTimeout( 100 );
145 maUpdateTimer.Start();
147 catch (const css::uno::Exception&)
149 mxPlayer.clear();
152 if (mxPlayer.is())
156 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
157 String( SdResId( STR_STOP ) ) );
159 mbLabelPlaying = sal_True;
161 catch (const css::lang::IllegalArgumentException&)
163 #ifdef DBG_UTIL
164 OSL_FAIL( "Cannot access play button" );
165 #endif
171 return 0;
174 // ------------------------------------------------------------------------
175 IMPL_LINK_NOARG(SdFileDialog_Imp, IsMusicStoppedHdl)
177 SolarMutexGuard aGuard;
179 if (
180 mxPlayer.is() && mxPlayer->isPlaying() &&
181 mxPlayer->getMediaTime() < mxPlayer->getDuration()
184 maUpdateTimer.Start();
185 return 0L;
189 if( mxControlAccess.is() )
193 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
194 String( SdResId( STR_PLAY ) ) );
195 mbLabelPlaying = sal_False;
197 catch (const css::lang::IllegalArgumentException&)
199 #ifdef DBG_UTIL
200 OSL_FAIL( "Cannot access play button" );
201 #endif
205 return( 0L );
208 // check whether to disable the "selection" checkbox
209 void SdFileDialog_Imp::CheckSelectionState()
211 if( mbUsableSelection && mxControlAccess.is() )
213 String aCurrFilter( GetCurrentFilter() );
217 if( !aCurrFilter.Len() || ( aCurrFilter == String( SdResId( STR_EXPORT_HTML_NAME ) ) ) )
218 mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, sal_False );
219 else
220 mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, sal_True );
222 catch (const css::lang::IllegalArgumentException&)
224 #ifdef DBG_UTIL
225 OSL_FAIL( "Cannot access \"selection\" checkbox" );
226 #endif
231 //-----------------------------------------------------------------------------
232 SdFileDialog_Imp::SdFileDialog_Imp( const short nDialogType,
233 sal_Bool bUsableSelection ) :
234 FileDialogHelper( nDialogType, 0 ),
235 mnPlaySoundEvent( 0 ),
236 mbUsableSelection( bUsableSelection ),
237 mbLabelPlaying(sal_False)
239 maUpdateTimer.SetTimeoutHdl(LINK(this, SdFileDialog_Imp, IsMusicStoppedHdl));
241 css::uno::Reference < ::com::sun::star::ui::dialogs::XFilePicker > xFileDlg = GetFilePicker();
243 // get the control access
244 mxControlAccess = css::uno::Reference< css::ui::dialogs::XFilePickerControlAccess > ( xFileDlg, css::uno::UNO_QUERY );
246 if( mxControlAccess.is() )
248 if( nDialogType ==
249 css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY )
253 mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
254 String( SdResId( STR_PLAY ) ) );
256 catch (const css::lang::IllegalArgumentException&)
258 #ifdef DBG_UTIL
259 OSL_FAIL( "Cannot set play button label" );
260 #endif
263 else if( mbUsableSelection != sal_True )
267 mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, sal_False );
269 catch (const css::lang::IllegalArgumentException&)
271 #ifdef DBG_UTIL
272 OSL_FAIL( "Cannot disable selection checkbox" );
273 #endif
280 // ------------------------------------------------------------------------
281 SdFileDialog_Imp::~SdFileDialog_Imp()
283 if( mnPlaySoundEvent )
284 Application::RemoveUserEvent( mnPlaySoundEvent );
287 // ------------------------------------------------------------------------
288 ErrCode SdFileDialog_Imp::Execute()
290 // make sure selection checkbox is disabled if
291 // HTML is current filter!
292 CheckSelectionState();
293 return FileDialogHelper::Execute();
296 // --------------------------------------------------------------------
297 // ----------- SdOpenSoundFileDialog -----------------------
298 // --------------------------------------------------------------------
300 // these are simple forwarders
301 SdOpenSoundFileDialog::SdOpenSoundFileDialog() :
302 mpImpl(
303 new SdFileDialog_Imp(
304 css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY, sal_False ) )
306 String aDescr;
307 aDescr = String(SdResId(STR_ALL_FILES));
308 mpImpl->AddFilter( aDescr, OUString("*.*"));
310 // setup filter
311 #if defined UNX
312 aDescr = String(SdResId(STR_AU_FILE));
313 mpImpl->AddFilter( aDescr, OUString("*.au;*.snd" ));
314 aDescr = String(SdResId(STR_VOC_FILE));
315 mpImpl->AddFilter( aDescr, OUString("*.voc" ));
316 aDescr = String(SdResId(STR_WAV_FILE));
317 mpImpl->AddFilter( aDescr, OUString("*.wav" ));
318 aDescr = String(SdResId(STR_AIFF_FILE));
319 mpImpl->AddFilter( aDescr, OUString("*.aiff" ));
320 aDescr = String(SdResId(STR_SVX_FILE));
321 mpImpl->AddFilter( aDescr, OUString("*.svx" ));
322 #else
323 aDescr = String(SdResId(STR_WAV_FILE));
324 mpImpl->AddFilter( aDescr, OUString("*.wav;*.mp3;*.ogg" ));
325 aDescr = String(SdResId(STR_MIDI_FILE));
326 mpImpl->AddFilter( aDescr, OUString("*.mid" ));
327 #endif
330 // ------------------------------------------------------------------------
331 SdOpenSoundFileDialog::~SdOpenSoundFileDialog()
335 // ------------------------------------------------------------------------
336 ErrCode SdOpenSoundFileDialog::Execute()
338 return mpImpl->Execute();
341 // ------------------------------------------------------------------------
342 String SdOpenSoundFileDialog::GetPath() const
344 return mpImpl->GetPath();
347 // ------------------------------------------------------------------------
348 void SdOpenSoundFileDialog::SetPath( const String& rPath )
350 mpImpl->SetDisplayDirectory( rPath );
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */