Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <cppuhelper/implbase1.hxx>
21 : #include <comphelper/processfactory.hxx>
22 : #include <com/sun/star/lang/XInitialization.hpp>
23 : #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
24 : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
25 : #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
26 : #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
27 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
28 : #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
29 : #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
30 : #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
31 : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
32 : #include <vcl/msgbox.hxx>
33 : #include <sal/types.h>
34 : #include <osl/thread.hxx>
35 : #include <osl/mutex.hxx>
36 : #include <vcl/svapp.hxx>
37 : #include <sfx2/filedlghelper.hxx>
38 : #include <avmedia/mediawindow.hxx>
39 : #include "filedlg.hxx"
40 : #include "sdresid.hxx"
41 : #include "strings.hrc"
42 : #include <vcl/graphicfilter.hxx>
43 :
44 : // ----------- SdFileDialog_Imp ---------------------------
45 :
46 : class SdFileDialog_Imp : public sfx2::FileDialogHelper
47 : {
48 : private:
49 : friend class SdOpenSoundFileDialog;
50 :
51 : css::uno::Reference< css::ui::dialogs::XFilePickerControlAccess > mxControlAccess;
52 :
53 : css::uno::Reference< css::media::XPlayer > mxPlayer;
54 : ImplSVEvent * mnPlaySoundEvent;
55 : bool mbUsableSelection;
56 : bool mbLabelPlaying;
57 :
58 : void CheckSelectionState();
59 :
60 : DECL_LINK( PlayMusicHdl, void * );
61 :
62 : Timer maUpdateTimer;
63 :
64 : DECL_LINK( IsMusicStoppedHdl, void * );
65 :
66 : public:
67 : SdFileDialog_Imp( const short nDialogType, bool bUsableSelection );
68 : virtual ~SdFileDialog_Imp();
69 :
70 : ErrCode Execute();
71 :
72 : // overwritten from FileDialogHelper, to receive user feedback
73 : virtual void SAL_CALL ControlStateChanged( const css::ui::dialogs::FilePickerEvent& aEvent ) SAL_OVERRIDE;
74 : };
75 :
76 0 : void SAL_CALL SdFileDialog_Imp::ControlStateChanged( const css::ui::dialogs::FilePickerEvent& aEvent )
77 : {
78 0 : SolarMutexGuard aGuard;
79 :
80 0 : switch( aEvent.ElementId )
81 : {
82 : case css::ui::dialogs::CommonFilePickerElementIds::LISTBOX_FILTER:
83 0 : CheckSelectionState();
84 0 : break;
85 :
86 : case css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
87 0 : if( mxControlAccess.is() )
88 : {
89 0 : if( mnPlaySoundEvent )
90 0 : Application::RemoveUserEvent( mnPlaySoundEvent );
91 :
92 0 : mnPlaySoundEvent = Application::PostUserEvent( LINK( this, SdFileDialog_Imp, PlayMusicHdl ) );
93 : }
94 0 : break;
95 0 : }
96 0 : }
97 :
98 0 : IMPL_LINK_NOARG(SdFileDialog_Imp, PlayMusicHdl)
99 : {
100 0 : maUpdateTimer.Stop();
101 0 : mnPlaySoundEvent = 0;
102 :
103 0 : if (mxPlayer.is())
104 : {
105 0 : if (mxPlayer->isPlaying())
106 0 : mxPlayer->stop();
107 0 : mxPlayer.clear();
108 : }
109 :
110 0 : if( mbLabelPlaying )
111 : {
112 : try
113 : {
114 0 : mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
115 0 : SD_RESSTR( STR_PLAY ) );
116 :
117 0 : mbLabelPlaying = false;
118 : }
119 0 : catch(const css::lang::IllegalArgumentException&)
120 : {
121 : #ifdef DBG_UTIL
122 : OSL_FAIL( "Cannot access play button" );
123 : #endif
124 : }
125 : }
126 : else
127 : {
128 0 : OUString aUrl( GetPath() );
129 0 : if ( !aUrl.isEmpty() )
130 : {
131 : try
132 : {
133 0 : mxPlayer.set( avmedia::MediaWindow::createPlayer( aUrl, "" ), css::uno::UNO_QUERY_THROW );
134 0 : mxPlayer->start();
135 0 : maUpdateTimer.SetTimeout( 100 );
136 0 : maUpdateTimer.Start();
137 : }
138 0 : catch (const css::uno::Exception&)
139 : {
140 0 : mxPlayer.clear();
141 : }
142 :
143 0 : if (mxPlayer.is())
144 : {
145 : try
146 : {
147 0 : mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
148 0 : SD_RESSTR( STR_STOP ) );
149 :
150 0 : mbLabelPlaying = true;
151 : }
152 0 : catch (const css::lang::IllegalArgumentException&)
153 : {
154 : #ifdef DBG_UTIL
155 : OSL_FAIL( "Cannot access play button" );
156 : #endif
157 : }
158 : }
159 0 : }
160 : }
161 :
162 0 : return 0;
163 : }
164 :
165 0 : IMPL_LINK_NOARG(SdFileDialog_Imp, IsMusicStoppedHdl)
166 : {
167 0 : SolarMutexGuard aGuard;
168 :
169 0 : if (
170 0 : mxPlayer.is() && mxPlayer->isPlaying() &&
171 0 : mxPlayer->getMediaTime() < mxPlayer->getDuration()
172 : )
173 : {
174 0 : maUpdateTimer.Start();
175 0 : return 0L;
176 : }
177 :
178 0 : if( mxControlAccess.is() )
179 : {
180 : try
181 : {
182 0 : mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
183 0 : SD_RESSTR( STR_PLAY ) );
184 0 : mbLabelPlaying = false;
185 : }
186 0 : catch (const css::lang::IllegalArgumentException&)
187 : {
188 : #ifdef DBG_UTIL
189 : OSL_FAIL( "Cannot access play button" );
190 : #endif
191 : }
192 : }
193 :
194 0 : return( 0L );
195 : }
196 :
197 : // check whether to disable the "selection" checkbox
198 0 : void SdFileDialog_Imp::CheckSelectionState()
199 : {
200 0 : if( mbUsableSelection && mxControlAccess.is() )
201 : {
202 0 : OUString aCurrFilter( GetCurrentFilter() );
203 :
204 : try
205 : {
206 0 : if( aCurrFilter.isEmpty() || ( aCurrFilter == SD_RESSTR( STR_EXPORT_HTML_NAME ) ) )
207 0 : mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, sal_False );
208 : else
209 0 : mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, sal_True );
210 : }
211 0 : catch (const css::lang::IllegalArgumentException&)
212 : {
213 : #ifdef DBG_UTIL
214 : OSL_FAIL( "Cannot access \"selection\" checkbox" );
215 : #endif
216 0 : }
217 : }
218 0 : }
219 :
220 0 : SdFileDialog_Imp::SdFileDialog_Imp( const short nDialogType,
221 : bool bUsableSelection ) :
222 : FileDialogHelper( nDialogType, 0 ),
223 : mnPlaySoundEvent( 0 ),
224 : mbUsableSelection( bUsableSelection ),
225 0 : mbLabelPlaying(false)
226 : {
227 0 : maUpdateTimer.SetTimeoutHdl(LINK(this, SdFileDialog_Imp, IsMusicStoppedHdl));
228 :
229 0 : css::uno::Reference < ::com::sun::star::ui::dialogs::XFilePicker > xFileDlg = GetFilePicker();
230 :
231 : // get the control access
232 0 : mxControlAccess = css::uno::Reference< css::ui::dialogs::XFilePickerControlAccess > ( xFileDlg, css::uno::UNO_QUERY );
233 :
234 0 : if( mxControlAccess.is() )
235 : {
236 0 : if( nDialogType ==
237 : css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY )
238 : {
239 : try
240 : {
241 0 : mxControlAccess->setLabel( css::ui::dialogs::ExtendedFilePickerElementIds::PUSHBUTTON_PLAY,
242 0 : SD_RESSTR( STR_PLAY ) );
243 : }
244 0 : catch (const css::lang::IllegalArgumentException&)
245 : {
246 : #ifdef DBG_UTIL
247 : OSL_FAIL( "Cannot set play button label" );
248 : #endif
249 : }
250 : }
251 0 : else if( mbUsableSelection != true )
252 : {
253 : try
254 : {
255 0 : mxControlAccess->enableControl( css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_SELECTION, sal_False );
256 : }
257 0 : catch (const css::lang::IllegalArgumentException&)
258 : {
259 : #ifdef DBG_UTIL
260 : OSL_FAIL( "Cannot disable selection checkbox" );
261 : #endif
262 : }
263 : }
264 0 : }
265 0 : }
266 :
267 0 : SdFileDialog_Imp::~SdFileDialog_Imp()
268 : {
269 0 : if( mnPlaySoundEvent )
270 0 : Application::RemoveUserEvent( mnPlaySoundEvent );
271 0 : }
272 :
273 0 : ErrCode SdFileDialog_Imp::Execute()
274 : {
275 : // make sure selection checkbox is disabled if
276 : // HTML is current filter!
277 0 : CheckSelectionState();
278 0 : return FileDialogHelper::Execute();
279 : }
280 :
281 : // ----------- SdOpenSoundFileDialog -----------------------
282 :
283 : // these are simple forwarders
284 0 : SdOpenSoundFileDialog::SdOpenSoundFileDialog() :
285 : mpImpl(
286 : new SdFileDialog_Imp(
287 0 : css::ui::dialogs::TemplateDescription::FILEOPEN_PLAY, false ) )
288 : {
289 0 : OUString aDescr;
290 0 : aDescr = SD_RESSTR(STR_ALL_FILES);
291 0 : mpImpl->AddFilter( aDescr, OUString("*.*"));
292 :
293 : // setup filter
294 : #if defined UNX
295 0 : aDescr = SD_RESSTR(STR_AU_FILE);
296 0 : mpImpl->AddFilter( aDescr, OUString("*.au;*.snd" ));
297 0 : aDescr = SD_RESSTR(STR_VOC_FILE);
298 0 : mpImpl->AddFilter( aDescr, OUString("*.voc" ));
299 0 : aDescr = SD_RESSTR(STR_WAV_FILE);
300 0 : mpImpl->AddFilter( aDescr, OUString("*.wav" ));
301 0 : aDescr = SD_RESSTR(STR_AIFF_FILE);
302 0 : mpImpl->AddFilter( aDescr, OUString("*.aiff" ));
303 0 : aDescr = SD_RESSTR(STR_SVX_FILE);
304 0 : mpImpl->AddFilter( aDescr, OUString("*.svx" ));
305 : #else
306 : aDescr = SD_RESSTR(STR_WAV_FILE);
307 : mpImpl->AddFilter( aDescr, OUString("*.wav;*.mp3;*.ogg" ));
308 : aDescr = SD_RESSTR(STR_MIDI_FILE);
309 : mpImpl->AddFilter( aDescr, OUString("*.mid" ));
310 : #endif
311 0 : }
312 :
313 0 : SdOpenSoundFileDialog::~SdOpenSoundFileDialog()
314 : {
315 0 : }
316 :
317 0 : ErrCode SdOpenSoundFileDialog::Execute()
318 : {
319 0 : return mpImpl->Execute();
320 : }
321 :
322 0 : OUString SdOpenSoundFileDialog::GetPath() const
323 : {
324 0 : return mpImpl->GetPath();
325 : }
326 :
327 0 : void SdOpenSoundFileDialog::SetPath( const OUString& rPath )
328 : {
329 0 : mpImpl->SetDisplayDirectory( rPath );
330 114 : }
331 :
332 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|