1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: filedlg.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
33 #include <tools/debug.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
38 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
40 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
41 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
42 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
43 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
44 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
45 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
46 #include <vcl/msgbox.hxx>
47 #include <sal/types.h>
48 #include <tools/urlobj.hxx>
49 #include <vos/thread.hxx>
50 #include <vos/mutex.hxx>
51 #include <vcl/svapp.hxx>
52 #include <sfx2/filedlghelper.hxx>
54 #include <svx/impgrf.hxx>
56 #include <avmedia/mediawindow.hxx>
57 #include "filedlg.hxx"
58 #include "sdresid.hxx"
59 #include "strings.hrc"
63 //-----------------------------------------------------------------------------
65 namespace css
= ::com::sun::star
;
68 // --------------------------------------------------------------------
69 // ----------- SdFileDialog_Imp ---------------------------
70 // --------------------------------------------------------------------
71 class SdFileDialog_Imp
: public sfx2::FileDialogHelper
74 #if defined __SUNPRO_CC
75 using sfx2::FileDialogHelper::Execute
;
78 friend class SdExportFileDialog
;
79 friend class SdOpenSoundFileDialog
;
81 css::uno::Reference
< css::ui::dialogs::XFilePickerControlAccess
> mxControlAccess
;
83 css::uno::Reference
< css::media::XPlayer
> mxPlayer
;
84 ULONG mnPlaySoundEvent
;
85 BOOL mbUsableSelection
;
88 void CheckSelectionState();
90 DECL_LINK( PlayMusicHdl
, void * );
94 DECL_LINK( IsMusicStoppedHdl
, void * );
97 SdFileDialog_Imp( const short nDialogType
, sal_Bool bUsableSelection
);
102 // overwritten from FileDialogHelper, to receive user feedback
103 virtual void SAL_CALL
ControlStateChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
);
105 sal_Bool
SelectionBoxState() const;
108 // ------------------------------------------------------------------------
109 void SAL_CALL
SdFileDialog_Imp::ControlStateChanged( const css::ui::dialogs::FilePickerEvent
& aEvent
)
111 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
113 switch( aEvent
.ElementId
)
115 case css::ui::dialogs::CommonFilePickerElementIds::LISTBOX_FILTER
:
116 CheckSelectionState();
119 case css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
:
120 if( mxControlAccess
.is() )
122 if( mnPlaySoundEvent
)
123 Application::RemoveUserEvent( mnPlaySoundEvent
);
125 mnPlaySoundEvent
= Application::PostUserEvent( LINK( this, SdFileDialog_Imp
, PlayMusicHdl
) );
131 // ------------------------------------------------------------------------
132 IMPL_LINK( SdFileDialog_Imp
, PlayMusicHdl
, void *, EMPTYARG
)
134 maUpdateTimer
.Stop();
135 mnPlaySoundEvent
= 0;
139 if (mxPlayer
->isPlaying())
148 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
149 String( SdResId( STR_PLAY
) ) );
151 mbLabelPlaying
= FALSE
;
153 catch( css::lang::IllegalArgumentException
)
156 DBG_ERROR( "Cannot access play button" );
162 rtl::OUString
aUrl( GetPath() );
163 if ( aUrl
.getLength() )
167 mxPlayer
.set( avmedia::MediaWindow::createPlayer( aUrl
), css::uno::UNO_QUERY_THROW
);
169 maUpdateTimer
.SetTimeout( 100 );
170 maUpdateTimer
.Start();
172 catch( css::uno::Exception
& e
)
182 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
183 String( SdResId( STR_STOP
) ) );
185 mbLabelPlaying
= TRUE
;
187 catch( css::lang::IllegalArgumentException
)
190 DBG_ERROR( "Cannot access play button" );
200 // ------------------------------------------------------------------------
201 IMPL_LINK( SdFileDialog_Imp
, IsMusicStoppedHdl
, void *, EMPTYARG
)
203 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
206 mxPlayer
.is() && mxPlayer
->isPlaying() &&
207 mxPlayer
->getMediaTime() < mxPlayer
->getDuration()
210 maUpdateTimer
.Start();
215 if( mxControlAccess
.is() )
219 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
220 String( SdResId( STR_PLAY
) ) );
221 mbLabelPlaying
= FALSE
;
223 catch( css::lang::IllegalArgumentException
)
226 DBG_ERROR( "Cannot access play button" );
234 // check whether to disable the "selection" checkbox
235 void SdFileDialog_Imp::CheckSelectionState()
237 if( mbUsableSelection
&& mxControlAccess
.is() )
239 String
aCurrFilter( GetCurrentFilter() );
243 if( !aCurrFilter
.Len() || ( aCurrFilter
== String( SdResId( STR_EXPORT_HTML_NAME
) ) ) )
244 mxControlAccess
->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, FALSE
);
246 mxControlAccess
->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, TRUE
);
248 catch( css::lang::IllegalArgumentException
)
251 DBG_ERROR( "Cannot access \"selection\" checkbox" );
257 //-----------------------------------------------------------------------------
258 SdFileDialog_Imp::SdFileDialog_Imp( const short nDialogType
,
259 sal_Bool bUsableSelection
) :
260 FileDialogHelper( nDialogType
, 0 ),
261 mnPlaySoundEvent( 0 ),
262 mbUsableSelection( bUsableSelection
),
263 mbLabelPlaying(FALSE
)
265 maUpdateTimer
.SetTimeoutHdl(LINK(this, SdFileDialog_Imp
, IsMusicStoppedHdl
));
267 css::uno::Reference
< ::com::sun::star::ui::dialogs::XFilePicker
> xFileDlg
= GetFilePicker();
269 // get the control access
270 mxControlAccess
= css::uno::Reference
< css::ui::dialogs::XFilePickerControlAccess
> ( xFileDlg
, css::uno::UNO_QUERY
);
272 if( mxControlAccess
.is() )
275 css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY
)
279 mxControlAccess
->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY
,
280 String( SdResId( STR_PLAY
) ) );
282 catch( css::lang::IllegalArgumentException
)
285 DBG_ERROR( "Cannot set play button label" );
289 else if( mbUsableSelection
!= sal_True
)
293 mxControlAccess
->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, FALSE
);
295 catch( css::lang::IllegalArgumentException
)
298 DBG_ERROR( "Cannot disable selection checkbox" );
306 // ------------------------------------------------------------------------
307 SdFileDialog_Imp::~SdFileDialog_Imp()
309 if( mnPlaySoundEvent
)
310 Application::RemoveUserEvent( mnPlaySoundEvent
);
313 // ------------------------------------------------------------------------
314 ErrCode
SdFileDialog_Imp::Execute()
316 // make sure selection checkbox is disabled if
317 // HTML is current filter!
318 CheckSelectionState();
319 return FileDialogHelper::Execute();
322 // ------------------------------------------------------------------------
323 sal_Bool
SdFileDialog_Imp::SelectionBoxState() const
325 if ( !mbUsableSelection
|| !mxControlAccess
.is() )
331 mxControlAccess
->getValue( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, 0 ) >>= bState
;
333 catch( css::lang::IllegalArgumentException
)
336 DBG_ERROR( "Cannot access \"selection\" checkbox" );
344 // --------------------------------------------------------------------
345 // ----------- SdExportFileDialog ---------------------------
346 // --------------------------------------------------------------------
348 // these are simple forwarders
349 SdExportFileDialog::SdExportFileDialog(BOOL bHaveCheckbox
) :
350 mpImpl( new SdFileDialog_Imp( css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_SELECTION
,
354 const String
aHTMLFilter( SdResId( STR_EXPORT_HTML_NAME
) );
355 GraphicFilter
* pFilter
= GetGrfFilter();
356 const USHORT nFilterCount
= pFilter
->GetExportFormatCount();
359 mpImpl
->AddFilter( aHTMLFilter
, String( SdResId( STR_EXPORT_HTML_FILTER
) ) );
361 // add other graphic filters
362 for ( USHORT i
= 0; i
< nFilterCount
; i
++ )
364 mpImpl
->AddFilter( pFilter
->GetExportFormatName( i
),
365 pFilter
->GetExportWildcard( i
) );
369 mpImpl
->SetTitle( String( SdResId( STR_EXPORT_DIALOG_TITLE
) ) );
372 // ------------------------------------------------------------------------
373 SdExportFileDialog::~SdExportFileDialog()
377 // ------------------------------------------------------------------------
378 ErrCode
SdExportFileDialog::Execute()
380 return mpImpl
->Execute();
383 String
SdExportFileDialog::GetPath() const
385 return mpImpl
->GetPath();
388 // ------------------------------------------------------------------------
389 void SdExportFileDialog::SetPath( const String
& rPath
)
391 mpImpl
->SetDisplayDirectory( rPath
);
394 // ------------------------------------------------------------------------
395 String
SdExportFileDialog::ReqCurrentFilter() const
397 return mpImpl
->GetCurrentFilter();
400 // ------------------------------------------------------------------------
401 BOOL
SdExportFileDialog::IsExportSelection() const
403 return mpImpl
->SelectionBoxState();
407 // --------------------------------------------------------------------
408 // ----------- SdOpenSoundFileDialog -----------------------
409 // --------------------------------------------------------------------
411 // these are simple forwarders
412 SdOpenSoundFileDialog::SdOpenSoundFileDialog() :
414 new SdFileDialog_Imp(
415 css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY
, sal_False
) )
418 aDescr
= String(SdResId(STR_ALL_FILES
));
419 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.*" ) ) );
423 aDescr
= String(SdResId(STR_AU_FILE
));
424 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.au;*.snd" ) ) );
425 aDescr
= String(SdResId(STR_VOC_FILE
));
426 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.voc" ) ) );
427 aDescr
= String(SdResId(STR_WAV_FILE
));
428 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.wav" ) ) );
429 aDescr
= String(SdResId(STR_AIFF_FILE
));
430 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.aiff" ) ) );
431 aDescr
= String(SdResId(STR_SVX_FILE
));
432 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.svx" ) ) );
434 aDescr
= String(SdResId(STR_WAV_FILE
));
435 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.wav;*.mp3;*.ogg" ) ) );
436 aDescr
= String(SdResId(STR_MIDI_FILE
));
437 mpImpl
->AddFilter( aDescr
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.mid" ) ) );
441 // ------------------------------------------------------------------------
442 SdOpenSoundFileDialog::~SdOpenSoundFileDialog()
446 // ------------------------------------------------------------------------
447 ErrCode
SdOpenSoundFileDialog::Execute()
449 return mpImpl
->Execute();
452 // ------------------------------------------------------------------------
453 String
SdOpenSoundFileDialog::GetPath() const
455 return mpImpl
->GetPath();
458 // ------------------------------------------------------------------------
459 void SdOpenSoundFileDialog::SetPath( const String
& rPath
)
461 mpImpl
->SetDisplayDirectory( rPath
);