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 :
21 : #include "OfficeFilePicker.hxx"
22 : #include "iodlg.hxx"
23 :
24 : #ifndef _LIST_
25 : #include <list>
26 : #endif
27 : #ifndef _FUNCTIONAL_
28 : #include <functional>
29 : #endif
30 : #ifndef _ALGORITHM_
31 : #include <algorithm>
32 : #endif
33 : #include <tools/urlobj.hxx>
34 : #include <com/sun/star/uno/Any.hxx>
35 : #include <com/sun/star/ui/dialogs/FilePickerEvent.hpp>
36 : #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
37 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
38 : #include <com/sun/star/beans/PropertyValue.hpp>
39 : #include <com/sun/star/beans/XPropertySet.hpp>
40 : #include <com/sun/star/awt/XWindow.hpp>
41 : #include <com/sun/star/beans/StringPair.hpp>
42 : #include <com/sun/star/uno/Sequence.hxx>
43 : #include <com/sun/star/beans/NamedValue.hpp>
44 : #include <unotools/ucbhelper.hxx>
45 : #include <unotools/pathoptions.hxx>
46 : #include <comphelper/sequence.hxx>
47 : #include <cppuhelper/typeprovider.hxx>
48 : #include "osl/mutex.hxx"
49 : #include "vcl/svapp.hxx"
50 :
51 : // using ----------------------------------------------------------------
52 :
53 : using namespace ::com::sun::star::container;
54 : using namespace ::com::sun::star::lang;
55 : using namespace ::com::sun::star::ui::dialogs;
56 : using namespace ::com::sun::star::uno;
57 : using namespace ::com::sun::star::beans;
58 : using namespace ::com::sun::star::awt;
59 : using namespace ::utl;
60 :
61 : //=====================================================================
62 :
63 : //=====================================================================
64 :
65 0 : struct FilterEntry
66 : {
67 : protected:
68 : OUString m_sTitle;
69 : OUString m_sFilter;
70 :
71 : UnoFilterList m_aSubFilters;
72 :
73 : public:
74 0 : FilterEntry( const OUString& _rTitle, const OUString& _rFilter )
75 : :m_sTitle( _rTitle )
76 0 : ,m_sFilter( _rFilter )
77 : {
78 0 : }
79 :
80 : FilterEntry( const OUString& _rTitle, const UnoFilterList& _rSubFilters );
81 :
82 0 : OUString getTitle() const { return m_sTitle; }
83 0 : OUString getFilter() const { return m_sFilter; }
84 :
85 : /// determines if the filter has sub filter (i.e., the filter is a filter group in real)
86 : sal_Bool hasSubFilters( ) const;
87 :
88 : /** retrieves the filters belonging to the entry
89 : @return
90 : the number of sub filters
91 : */
92 : sal_Int32 getSubFilters( UnoFilterList& _rSubFilterList );
93 :
94 : // helpers for iterating the sub filters
95 0 : const UnoFilterEntry* beginSubFilters() const { return m_aSubFilters.getConstArray(); }
96 0 : const UnoFilterEntry* endSubFilters() const { return m_aSubFilters.getConstArray() + m_aSubFilters.getLength(); }
97 : };
98 :
99 : //=====================================================================
100 :
101 : //---------------------------------------------------------------------
102 0 : FilterEntry::FilterEntry( const OUString& _rTitle, const UnoFilterList& _rSubFilters )
103 : :m_sTitle( _rTitle )
104 0 : ,m_aSubFilters( _rSubFilters )
105 : {
106 0 : }
107 :
108 : //---------------------------------------------------------------------
109 0 : sal_Bool FilterEntry::hasSubFilters( ) const
110 : {
111 0 : return ( 0 < m_aSubFilters.getLength() );
112 : }
113 :
114 : //---------------------------------------------------------------------
115 0 : sal_Int32 FilterEntry::getSubFilters( UnoFilterList& _rSubFilterList )
116 : {
117 0 : _rSubFilterList = m_aSubFilters;
118 0 : return m_aSubFilters.getLength();
119 : }
120 :
121 : // struct ElementEntry_Impl ----------------------------------------------
122 :
123 0 : struct ElementEntry_Impl
124 : {
125 : sal_Int16 m_nElementID;
126 : sal_Int16 m_nControlAction;
127 : Any m_aValue;
128 : OUString m_aLabel;
129 : sal_Bool m_bEnabled : 1;
130 :
131 : sal_Bool m_bHasValue : 1;
132 : sal_Bool m_bHasLabel : 1;
133 : sal_Bool m_bHasEnabled : 1;
134 :
135 : ElementEntry_Impl( sal_Int16 nId );
136 :
137 0 : void setValue( const Any& rVal ) { m_aValue = rVal; m_bHasValue = sal_True; }
138 0 : void setAction( sal_Int16 nAction ) { m_nControlAction = nAction; }
139 0 : void setLabel( const OUString& rVal ) { m_aLabel = rVal; m_bHasLabel = sal_True; }
140 0 : void setEnabled( sal_Bool bEnabled ) { m_bEnabled = bEnabled; m_bHasEnabled = sal_True; }
141 : };
142 :
143 0 : ElementEntry_Impl::ElementEntry_Impl( sal_Int16 nId )
144 : : m_nElementID( nId )
145 : , m_nControlAction( 0 )
146 : , m_bEnabled( sal_False )
147 : , m_bHasValue( sal_False )
148 : , m_bHasLabel( sal_False )
149 0 : , m_bHasEnabled( sal_False )
150 0 : {}
151 :
152 : //------------------------------------------------------------------------------------
153 0 : void SvtFilePicker::prepareExecute()
154 : {
155 : // set the default directory
156 : // --**-- doesn't match the spec yet
157 0 : if ( !m_aDisplayDirectory.isEmpty() || !m_aDefaultName.isEmpty() )
158 : {
159 0 : if ( !m_aDisplayDirectory.isEmpty() )
160 : {
161 :
162 0 : INetURLObject aPath( m_aDisplayDirectory );
163 0 : if ( !m_aDefaultName.isEmpty() )
164 : {
165 0 : aPath.insertName( m_aDefaultName );
166 0 : getDialog()->SetHasFilename( true );
167 : }
168 0 : getDialog()->SetPath( aPath.GetMainURL( INetURLObject::NO_DECODE ) );
169 : }
170 0 : else if ( !m_aDefaultName.isEmpty() )
171 : {
172 0 : getDialog()->SetPath( m_aDefaultName );
173 0 : getDialog()->SetHasFilename( true );
174 : }
175 : }
176 : else
177 : {
178 : // Default-Standard-Dir setzen
179 0 : INetURLObject aStdDirObj( SvtPathOptions().GetWorkPath() );
180 0 : getDialog()->SetPath( aStdDirObj.GetMainURL( INetURLObject::NO_DECODE ) );
181 : }
182 :
183 : // set the control values and set the control labels, too
184 0 : if ( m_pElemList && !m_pElemList->empty() )
185 : {
186 0 : ::svt::OControlAccess aAccess( getDialog(), getDialog()->GetView() );
187 :
188 0 : ElementList::iterator aListIter;
189 0 : for ( aListIter = m_pElemList->begin();
190 0 : aListIter != m_pElemList->end(); ++aListIter )
191 : {
192 0 : ElementEntry_Impl& rEntry = *aListIter;
193 0 : if ( rEntry.m_bHasValue )
194 0 : aAccess.setValue( rEntry.m_nElementID, rEntry.m_nControlAction, rEntry.m_aValue );
195 0 : if ( rEntry.m_bHasLabel )
196 0 : aAccess.setLabel( rEntry.m_nElementID, rEntry.m_aLabel );
197 0 : if ( rEntry.m_bHasEnabled )
198 0 : aAccess.enableControl( rEntry.m_nElementID, rEntry.m_bEnabled );
199 : }
200 :
201 0 : getDialog()->updateListboxLabelSizes();
202 : }
203 :
204 0 : if ( m_pFilterList && !m_pFilterList->empty() )
205 : {
206 0 : for ( FilterList::iterator aListIter = m_pFilterList->begin();
207 0 : aListIter != m_pFilterList->end();
208 : ++aListIter
209 : )
210 : {
211 0 : if ( aListIter->hasSubFilters() )
212 : { // it's a filter group
213 0 : UnoFilterList aSubFilters;
214 0 : aListIter->getSubFilters( aSubFilters );
215 :
216 0 : getDialog()->AddFilterGroup( aListIter->getTitle(), aSubFilters );
217 : }
218 : else
219 : // it's a single filter
220 0 : getDialog()->AddFilter( aListIter->getTitle(), aListIter->getFilter() );
221 : }
222 : }
223 :
224 : // set the default filter
225 0 : if ( !m_aCurrentFilter.isEmpty() )
226 0 : getDialog()->SetCurFilter( m_aCurrentFilter );
227 :
228 0 : }
229 :
230 : //-----------------------------------------------------------------------------
231 0 : IMPL_LINK( SvtFilePicker, DialogClosedHdl, Dialog*, pDlg )
232 : {
233 0 : if ( m_xDlgClosedListener.is() )
234 : {
235 0 : sal_Int16 nRet = static_cast< sal_Int16 >( pDlg->GetResult() );
236 0 : ::com::sun::star::ui::dialogs::DialogClosedEvent aEvent( *this, nRet );
237 0 : m_xDlgClosedListener->dialogClosed( aEvent );
238 0 : m_xDlgClosedListener.clear();
239 : }
240 0 : return 0;
241 : }
242 :
243 : //------------------------------------------------------------------------------------
244 : // SvtFilePicker
245 : //------------------------------------------------------------------------------------
246 :
247 : //------------------------------------------------------------------------------------
248 0 : WinBits SvtFilePicker::getWinBits( WinBits& rExtraBits )
249 : {
250 : // set the winbits for creating the filedialog
251 0 : WinBits nBits = 0L;
252 0 : rExtraBits = 0L;
253 :
254 : // set the standard bits according to the service name
255 0 : if ( m_nServiceType == TemplateDescription::FILEOPEN_SIMPLE )
256 : {
257 0 : nBits = WB_OPEN;
258 : }
259 0 : else if ( m_nServiceType == TemplateDescription::FILESAVE_SIMPLE )
260 : {
261 0 : nBits = WB_SAVEAS;
262 : }
263 0 : else if ( m_nServiceType == TemplateDescription::FILESAVE_AUTOEXTENSION )
264 : {
265 0 : nBits = WB_SAVEAS;
266 0 : rExtraBits = SFX_EXTRA_AUTOEXTENSION;
267 : }
268 0 : else if ( m_nServiceType == TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD )
269 : {
270 0 : nBits = WB_SAVEAS | SFXWB_PASSWORD;
271 0 : rExtraBits = SFX_EXTRA_AUTOEXTENSION;
272 : }
273 0 : else if ( m_nServiceType == TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS )
274 : {
275 0 : nBits = WB_SAVEAS | SFXWB_PASSWORD;
276 0 : rExtraBits = SFX_EXTRA_AUTOEXTENSION | SFX_EXTRA_FILTEROPTIONS;
277 : }
278 0 : else if ( m_nServiceType == TemplateDescription::FILESAVE_AUTOEXTENSION_TEMPLATE )
279 : {
280 0 : nBits = WB_SAVEAS;
281 0 : rExtraBits = SFX_EXTRA_AUTOEXTENSION | SFX_EXTRA_TEMPLATES;
282 : }
283 0 : else if ( m_nServiceType == TemplateDescription::FILESAVE_AUTOEXTENSION_SELECTION )
284 : {
285 0 : nBits = WB_SAVEAS;
286 0 : rExtraBits = SFX_EXTRA_AUTOEXTENSION | SFX_EXTRA_SELECTION;
287 : }
288 :
289 0 : else if ( m_nServiceType == TemplateDescription::FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE )
290 : {
291 0 : nBits = WB_OPEN;
292 0 : rExtraBits = SFX_EXTRA_INSERTASLINK | SFX_EXTRA_SHOWPREVIEW | SFX_EXTRA_IMAGE_TEMPLATE;
293 : }
294 0 : else if ( m_nServiceType == TemplateDescription::FILEOPEN_PLAY )
295 : {
296 0 : nBits = WB_OPEN;
297 0 : rExtraBits = SFX_EXTRA_PLAYBUTTON;
298 : }
299 0 : else if ( m_nServiceType == TemplateDescription::FILEOPEN_READONLY_VERSION )
300 : {
301 0 : nBits = WB_OPEN | SFXWB_READONLY;
302 0 : rExtraBits = SFX_EXTRA_SHOWVERSIONS;
303 : }
304 0 : else if ( m_nServiceType == TemplateDescription::FILEOPEN_LINK_PREVIEW )
305 : {
306 0 : nBits = WB_OPEN;
307 0 : rExtraBits = SFX_EXTRA_INSERTASLINK | SFX_EXTRA_SHOWPREVIEW;
308 : }
309 0 : if ( m_bMultiSelection && ( ( nBits & WB_OPEN ) == WB_OPEN ) )
310 0 : nBits |= SFXWB_MULTISELECTION;
311 :
312 0 : return nBits;
313 : }
314 :
315 : //------------------------------------------------------------------------------------
316 0 : void SvtFilePicker::notify( sal_Int16 _nEventId, sal_Int16 _nControlId )
317 : {
318 0 : if ( !m_xListener.is() )
319 0 : return;
320 :
321 0 : FilePickerEvent aEvent( *this, _nControlId );
322 :
323 0 : switch ( _nEventId )
324 : {
325 : case FILE_SELECTION_CHANGED:
326 0 : m_xListener->fileSelectionChanged( aEvent );
327 0 : break;
328 : case DIRECTORY_CHANGED:
329 0 : m_xListener->directoryChanged( aEvent );
330 0 : break;
331 : case HELP_REQUESTED:
332 0 : m_xListener->helpRequested( aEvent );
333 0 : break;
334 : case CTRL_STATE_CHANGED:
335 0 : m_xListener->controlStateChanged( aEvent );
336 0 : break;
337 : case DIALOG_SIZE_CHANGED:
338 0 : m_xListener->dialogSizeChanged();
339 0 : break;
340 : default:
341 : SAL_WARN( "fpicker.office", "SvtFilePicker::notify(): Unknown event id!" );
342 0 : break;
343 0 : }
344 : }
345 :
346 : //------------------------------------------------------------------------------------
347 : namespace {
348 : //................................................................................
349 : struct FilterTitleMatch : public ::std::unary_function< FilterEntry, bool >
350 : {
351 : protected:
352 : const OUString& rTitle;
353 :
354 : public:
355 0 : FilterTitleMatch( const OUString& _rTitle ) : rTitle( _rTitle ) { }
356 :
357 : //............................................................................
358 0 : bool operator () ( const FilterEntry& _rEntry )
359 : {
360 : sal_Bool bMatch;
361 0 : if ( !_rEntry.hasSubFilters() )
362 : // a real filter
363 0 : bMatch = ( _rEntry.getTitle() == rTitle );
364 : else
365 : // a filter group -> search the sub filters
366 : bMatch =
367 0 : _rEntry.endSubFilters() != ::std::find_if(
368 : _rEntry.beginSubFilters(),
369 : _rEntry.endSubFilters(),
370 : *this
371 0 : );
372 :
373 0 : return bMatch ? true : false;
374 : }
375 0 : bool operator () ( const UnoFilterEntry& _rEntry )
376 : {
377 0 : return _rEntry.First == rTitle ? true : false;
378 : }
379 : };
380 : }
381 :
382 : //------------------------------------------------------------------------------------
383 0 : sal_Bool SvtFilePicker::FilterNameExists( const OUString& rTitle )
384 : {
385 0 : sal_Bool bRet = sal_False;
386 :
387 0 : if ( m_pFilterList )
388 : bRet =
389 0 : m_pFilterList->end() != ::std::find_if(
390 : m_pFilterList->begin(),
391 : m_pFilterList->end(),
392 : FilterTitleMatch( rTitle )
393 0 : );
394 :
395 0 : return bRet;
396 : }
397 :
398 : //------------------------------------------------------------------------------------
399 0 : sal_Bool SvtFilePicker::FilterNameExists( const UnoFilterList& _rGroupedFilters )
400 : {
401 0 : sal_Bool bRet = sal_False;
402 :
403 0 : if ( m_pFilterList )
404 : {
405 0 : const UnoFilterEntry* pStart = _rGroupedFilters.getConstArray();
406 0 : const UnoFilterEntry* pEnd = pStart + _rGroupedFilters.getLength();
407 0 : for ( ; pStart != pEnd; ++pStart )
408 0 : if ( m_pFilterList->end() != ::std::find_if( m_pFilterList->begin(), m_pFilterList->end(), FilterTitleMatch( pStart->First ) ) )
409 0 : break;
410 :
411 0 : bRet = pStart != pEnd;
412 : }
413 :
414 0 : return bRet;
415 : }
416 :
417 : //------------------------------------------------------------------------------------
418 0 : void SvtFilePicker::ensureFilterList( const OUString& _rInitialCurrentFilter )
419 : {
420 0 : if ( !m_pFilterList )
421 : {
422 0 : m_pFilterList = new FilterList;
423 :
424 : // set the first filter to the current filter
425 0 : if ( m_aCurrentFilter.isEmpty() )
426 0 : m_aCurrentFilter = _rInitialCurrentFilter;
427 : }
428 0 : }
429 :
430 : //------------------------------------------------------------------------------------
431 : // class SvtFilePicker
432 : //------------------------------------------------------------------------------------
433 0 : SvtFilePicker::SvtFilePicker( const Reference < XMultiServiceFactory >& xFactory )
434 : :OCommonPicker( xFactory )
435 : ,m_pFilterList ( NULL )
436 : ,m_pElemList ( NULL )
437 : ,m_bMultiSelection ( sal_False )
438 0 : ,m_nServiceType ( TemplateDescription::FILEOPEN_SIMPLE )
439 : {
440 0 : }
441 :
442 0 : SvtFilePicker::~SvtFilePicker()
443 : {
444 0 : if ( m_pFilterList && !m_pFilterList->empty() )
445 0 : m_pFilterList->erase( m_pFilterList->begin(), m_pFilterList->end() );
446 0 : delete m_pFilterList;
447 :
448 0 : if ( m_pElemList && !m_pElemList->empty() )
449 0 : m_pElemList->erase( m_pElemList->begin(), m_pElemList->end() );
450 0 : delete m_pElemList;
451 0 : }
452 :
453 : //------------------------------------------------------------------------------------
454 0 : sal_Int16 SvtFilePicker::implExecutePicker( )
455 : {
456 0 : getDialog()->SetFileCallback( this );
457 :
458 0 : prepareExecute();
459 :
460 0 : getDialog()->EnableAutocompletion( sal_True );
461 : // now we are ready to execute the dialog
462 0 : sal_Int16 nRet = getDialog()->Execute();
463 :
464 : // the execution of the dialog yields, so it is possible the at this point the window or the dialog is closed
465 0 : if ( getDialog() )
466 0 : getDialog()->SetFileCallback( NULL );
467 :
468 0 : return nRet;
469 : }
470 :
471 : //------------------------------------------------------------------------------------
472 0 : SvtFileDialog* SvtFilePicker::implCreateDialog( Window* _pParent )
473 : {
474 : WinBits nExtraBits;
475 0 : WinBits nBits = getWinBits( nExtraBits );
476 :
477 0 : SvtFileDialog* dialog = new SvtFileDialog( _pParent, nBits, nExtraBits );
478 :
479 : // Set StandardDir if present
480 0 : if ( !m_aStandardDir.isEmpty())
481 : {
482 0 : String sStandardDir = String( m_aStandardDir );
483 0 : dialog->SetStandardDir( sStandardDir );
484 0 : dialog->SetBlackList( m_aBlackList );
485 : }
486 :
487 0 : return dialog;
488 : }
489 :
490 : //------------------------------------------------------------------------------------
491 : // disambiguate XInterface
492 : //------------------------------------------------------------------------------------
493 0 : IMPLEMENT_FORWARD_XINTERFACE2( SvtFilePicker, OCommonPicker, SvtFilePicker_Base )
494 :
495 : //------------------------------------------------------------------------------------
496 : // disambiguate XTypeProvider
497 : //------------------------------------------------------------------------------------
498 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( SvtFilePicker, OCommonPicker, SvtFilePicker_Base )
499 :
500 : //------------------------------------------------------------------------------------
501 : // XExecutableDialog functions
502 : //------------------------------------------------------------------------------------
503 :
504 : //------------------------------------------------------------------------------------
505 0 : void SAL_CALL SvtFilePicker::setTitle( const OUString& _rTitle ) throw (RuntimeException)
506 : {
507 0 : OCommonPicker::setTitle( _rTitle );
508 0 : }
509 :
510 : //------------------------------------------------------------------------------------
511 0 : sal_Int16 SAL_CALL SvtFilePicker::execute( ) throw (RuntimeException)
512 : {
513 0 : return OCommonPicker::execute();
514 : }
515 :
516 : //------------------------------------------------------------------------------------
517 : // XAsynchronousExecutableDialog functions
518 : //------------------------------------------------------------------------------------
519 :
520 : //------------------------------------------------------------------------------------
521 0 : void SAL_CALL SvtFilePicker::setDialogTitle( const OUString& _rTitle ) throw (RuntimeException)
522 : {
523 0 : setTitle( _rTitle );
524 0 : }
525 :
526 : //------------------------------------------------------------------------------------
527 0 : void SAL_CALL SvtFilePicker::startExecuteModal( const Reference< ::com::sun::star::ui::dialogs::XDialogClosedListener >& xListener ) throw (RuntimeException)
528 : {
529 0 : m_xDlgClosedListener = xListener;
530 0 : prepareDialog();
531 0 : prepareExecute();
532 0 : getDialog()->EnableAutocompletion( sal_True );
533 0 : getDialog()->StartExecuteModal( LINK( this, SvtFilePicker, DialogClosedHdl ) );
534 0 : }
535 :
536 : //------------------------------------------------------------------------------------
537 : // XFilePicker functions
538 : //------------------------------------------------------------------------------------
539 :
540 0 : void SAL_CALL SvtFilePicker::setMultiSelectionMode( sal_Bool bMode ) throw( RuntimeException )
541 : {
542 0 : checkAlive();
543 :
544 0 : SolarMutexGuard aGuard;
545 0 : m_bMultiSelection = bMode;
546 0 : }
547 :
548 0 : void SAL_CALL SvtFilePicker::setDefaultName( const OUString& aName ) throw( RuntimeException )
549 : {
550 0 : checkAlive();
551 :
552 0 : SolarMutexGuard aGuard;
553 0 : m_aDefaultName = aName;
554 0 : }
555 :
556 0 : void SAL_CALL SvtFilePicker::setDisplayDirectory( const OUString& aDirectory )
557 : throw( IllegalArgumentException, RuntimeException )
558 : {
559 0 : checkAlive();
560 :
561 0 : SolarMutexGuard aGuard;
562 0 : m_aDisplayDirectory = aDirectory;
563 0 : }
564 :
565 0 : OUString SAL_CALL SvtFilePicker::getDisplayDirectory() throw( RuntimeException )
566 : {
567 0 : checkAlive();
568 :
569 0 : SolarMutexGuard aGuard;
570 0 : if ( getDialog() )
571 : {
572 0 : OUString aPath = getDialog()->GetPath();
573 :
574 : // #97148# ----
575 0 : if( m_aOldHideDirectory == aPath )
576 0 : return m_aOldDisplayDirectory;
577 0 : m_aOldHideDirectory = aPath;
578 :
579 : // #102204# -----
580 0 : if( !getDialog()->ContentIsFolder( aPath ) )
581 : {
582 0 : INetURLObject aFolder( aPath );
583 0 : aFolder.CutLastName();
584 0 : aPath = aFolder.GetMainURL( INetURLObject::NO_DECODE );
585 : }
586 0 : m_aOldDisplayDirectory = aPath;
587 0 : return aPath;
588 : }
589 : else
590 0 : return m_aDisplayDirectory;
591 : }
592 :
593 0 : Sequence< OUString > SAL_CALL SvtFilePicker::getFiles() throw( RuntimeException )
594 : {
595 0 : checkAlive();
596 :
597 0 : SolarMutexGuard aGuard;
598 0 : if ( ! getDialog() )
599 : {
600 0 : Sequence< OUString > aEmpty;
601 0 : return aEmpty;
602 : }
603 :
604 : // if there is more than one path we have to return the path to the
605 : // files first and then the list of the selected entries
606 :
607 0 : std::vector<OUString> aPathList(getDialog()->GetPathList());
608 0 : size_t nCount = aPathList.size();
609 0 : size_t nTotal = nCount > 1 ? nCount+1: nCount;
610 :
611 0 : Sequence< OUString > aPath( nTotal );
612 :
613 0 : if ( nCount == 1 )
614 0 : aPath[0] = OUString(aPathList[0]);
615 0 : else if ( nCount > 1 )
616 : {
617 0 : INetURLObject aObj(aPathList[0]);
618 0 : aObj.removeSegment();
619 0 : aPath[0] = aObj.GetMainURL( INetURLObject::NO_DECODE );
620 :
621 0 : for(size_t i = 0; i < aPathList.size(); ++i)
622 : {
623 0 : aObj.SetURL(aPathList[i]);
624 0 : aPath[i + 1] = aObj.getName();
625 0 : }
626 : }
627 :
628 0 : return aPath;
629 : }
630 :
631 : //------------------------------------------------------------------------------------
632 : // XFilePickerControlAccess functions
633 : //------------------------------------------------------------------------------------
634 :
635 0 : void SAL_CALL SvtFilePicker::setValue( sal_Int16 nElementID,
636 : sal_Int16 nControlAction,
637 : const Any& rValue )
638 : throw( RuntimeException )
639 : {
640 0 : checkAlive();
641 :
642 0 : SolarMutexGuard aGuard;
643 0 : if ( getDialog() )
644 : {
645 0 : ::svt::OControlAccess aAccess( getDialog(), getDialog()->GetView() );
646 0 : aAccess.setValue( nElementID, nControlAction, rValue );
647 : }
648 : else
649 : {
650 0 : if ( !m_pElemList )
651 0 : m_pElemList = new ElementList;
652 :
653 0 : sal_Bool bFound = sal_False;
654 0 : ElementList::iterator aListIter;
655 :
656 0 : for ( aListIter = m_pElemList->begin();
657 0 : aListIter != m_pElemList->end(); ++aListIter )
658 : {
659 0 : ElementEntry_Impl& rEntry = *aListIter;
660 0 : if ( ( rEntry.m_nElementID == nElementID ) &&
661 0 : ( !rEntry.m_bHasValue || ( rEntry.m_nControlAction == nControlAction ) ) )
662 : {
663 0 : rEntry.setAction( nControlAction );
664 0 : rEntry.setValue( rValue );
665 0 : bFound = sal_True;
666 : }
667 : }
668 :
669 0 : if ( !bFound )
670 : {
671 0 : ElementEntry_Impl aNew( nElementID );
672 0 : aNew.setAction( nControlAction );
673 0 : aNew.setValue( rValue );
674 0 : m_pElemList->insert( m_pElemList->end(), aNew );
675 : }
676 0 : }
677 0 : }
678 :
679 : //------------------------------------------------------------------------------------
680 :
681 0 : Any SAL_CALL SvtFilePicker::getValue( sal_Int16 nElementID, sal_Int16 nControlAction )
682 : throw( RuntimeException )
683 : {
684 0 : checkAlive();
685 :
686 0 : SolarMutexGuard aGuard;
687 0 : Any aAny;
688 :
689 : // execute() called?
690 0 : if ( getDialog() )
691 : {
692 0 : ::svt::OControlAccess aAccess( getDialog(), getDialog()->GetView() );
693 0 : aAny = aAccess.getValue( nElementID, nControlAction );
694 : }
695 0 : else if ( m_pElemList && !m_pElemList->empty() )
696 : {
697 0 : ElementList::iterator aListIter;
698 0 : for ( aListIter = m_pElemList->begin();
699 0 : aListIter != m_pElemList->end(); ++aListIter )
700 : {
701 0 : ElementEntry_Impl& rEntry = *aListIter;
702 0 : if ( ( rEntry.m_nElementID == nElementID ) &&
703 0 : ( rEntry.m_bHasValue ) &&
704 0 : ( rEntry.m_nControlAction == nControlAction ) )
705 : {
706 0 : aAny = rEntry.m_aValue;
707 0 : break;
708 : }
709 : }
710 : }
711 :
712 0 : return aAny;
713 : }
714 :
715 :
716 : //------------------------------------------------------------------------------------
717 0 : void SAL_CALL SvtFilePicker::setLabel( sal_Int16 nLabelID, const OUString& rValue )
718 : throw ( RuntimeException )
719 : {
720 0 : checkAlive();
721 :
722 0 : SolarMutexGuard aGuard;
723 0 : if ( getDialog() )
724 : {
725 0 : ::svt::OControlAccess aAccess( getDialog(), getDialog()->GetView() );
726 0 : aAccess.setLabel( nLabelID, rValue );
727 : }
728 : else
729 : {
730 0 : if ( !m_pElemList )
731 0 : m_pElemList = new ElementList;
732 :
733 0 : sal_Bool bFound = sal_False;
734 0 : ElementList::iterator aListIter;
735 :
736 0 : for ( aListIter = m_pElemList->begin();
737 0 : aListIter != m_pElemList->end(); ++aListIter )
738 : {
739 0 : ElementEntry_Impl& rEntry = *aListIter;
740 0 : if ( rEntry.m_nElementID == nLabelID )
741 : {
742 0 : rEntry.setLabel( rValue );
743 0 : bFound = sal_True;
744 : }
745 : }
746 :
747 0 : if ( !bFound )
748 : {
749 0 : ElementEntry_Impl aNew( nLabelID );
750 0 : aNew.setLabel( rValue );
751 0 : m_pElemList->insert( m_pElemList->end(), aNew );
752 : }
753 0 : }
754 0 : }
755 :
756 : //------------------------------------------------------------------------------------
757 0 : OUString SAL_CALL SvtFilePicker::getLabel( sal_Int16 nLabelID )
758 : throw ( RuntimeException )
759 : {
760 0 : checkAlive();
761 :
762 0 : SolarMutexGuard aGuard;
763 0 : OUString aLabel;
764 :
765 0 : if ( getDialog() )
766 : {
767 0 : ::svt::OControlAccess aAccess( getDialog(), getDialog()->GetView() );
768 0 : aLabel = aAccess.getLabel( nLabelID );
769 : }
770 0 : else if ( m_pElemList && !m_pElemList->empty() )
771 : {
772 0 : ElementList::iterator aListIter;
773 0 : for ( aListIter = m_pElemList->begin();
774 0 : aListIter != m_pElemList->end(); ++aListIter )
775 : {
776 0 : ElementEntry_Impl& rEntry = *aListIter;
777 0 : if ( rEntry.m_nElementID == nLabelID )
778 : {
779 0 : if ( rEntry.m_bHasLabel )
780 0 : aLabel = rEntry.m_aLabel;
781 0 : break;
782 : }
783 : }
784 : }
785 :
786 0 : return aLabel;
787 : }
788 :
789 : //------------------------------------------------------------------------------------
790 0 : void SAL_CALL SvtFilePicker::enableControl( sal_Int16 nElementID, sal_Bool bEnable )
791 : throw( RuntimeException )
792 : {
793 0 : checkAlive();
794 :
795 0 : SolarMutexGuard aGuard;
796 0 : if ( getDialog() )
797 : {
798 0 : ::svt::OControlAccess aAccess( getDialog(), getDialog()->GetView() );
799 0 : aAccess.enableControl( nElementID, bEnable );
800 : }
801 : else
802 : {
803 0 : if ( !m_pElemList )
804 0 : m_pElemList = new ElementList;
805 :
806 0 : sal_Bool bFound = sal_False;
807 0 : ElementList::iterator aListIter;
808 :
809 0 : for ( aListIter = m_pElemList->begin();
810 0 : aListIter != m_pElemList->end(); ++aListIter )
811 : {
812 0 : ElementEntry_Impl& rEntry = *aListIter;
813 0 : if ( rEntry.m_nElementID == nElementID )
814 : {
815 0 : rEntry.setEnabled( bEnable );
816 0 : bFound = sal_True;
817 : }
818 : }
819 :
820 0 : if ( !bFound )
821 : {
822 0 : ElementEntry_Impl aNew( nElementID );
823 0 : aNew.setEnabled( bEnable );
824 0 : m_pElemList->insert( m_pElemList->end(), aNew );
825 : }
826 0 : }
827 0 : }
828 :
829 : //------------------------------------------------------------------------------------
830 : // XFilePickerNotifier functions
831 : //------------------------------------------------------------------------------------
832 :
833 0 : void SAL_CALL SvtFilePicker::addFilePickerListener( const Reference< XFilePickerListener >& xListener ) throw ( RuntimeException )
834 : {
835 0 : checkAlive();
836 :
837 0 : SolarMutexGuard aGuard;
838 0 : m_xListener = xListener;
839 0 : }
840 :
841 : //------------------------------------------------------------------------------------
842 0 : void SAL_CALL SvtFilePicker::removeFilePickerListener( const Reference< XFilePickerListener >& ) throw ( RuntimeException )
843 : {
844 0 : checkAlive();
845 :
846 0 : SolarMutexGuard aGuard;
847 0 : m_xListener.clear();
848 0 : }
849 :
850 : //------------------------------------------------------------------------------------
851 : // XFilePreview functions
852 : //------------------------------------------------------------------------------------
853 :
854 0 : Sequence< sal_Int16 > SAL_CALL SvtFilePicker::getSupportedImageFormats()
855 : throw ( RuntimeException )
856 : {
857 0 : checkAlive();
858 :
859 0 : SolarMutexGuard aGuard;
860 0 : Sequence< sal_Int16 > aFormats( 1 );
861 :
862 0 : aFormats[0] = FilePreviewImageFormats::BITMAP;
863 :
864 0 : return aFormats;
865 : }
866 :
867 : //------------------------------------------------------------------------------------
868 0 : sal_Int32 SAL_CALL SvtFilePicker::getTargetColorDepth() throw ( RuntimeException )
869 : {
870 0 : checkAlive();
871 :
872 0 : SolarMutexGuard aGuard;
873 0 : sal_Int32 nDepth = 0;
874 :
875 0 : if ( getDialog() )
876 0 : nDepth = getDialog()->getTargetColorDepth();
877 :
878 0 : return nDepth;
879 : }
880 :
881 : //------------------------------------------------------------------------------------
882 0 : sal_Int32 SAL_CALL SvtFilePicker::getAvailableWidth() throw ( RuntimeException )
883 : {
884 0 : checkAlive();
885 :
886 0 : SolarMutexGuard aGuard;
887 0 : sal_Int32 nWidth = 0;
888 :
889 0 : if ( getDialog() )
890 0 : nWidth = getDialog()->getAvailableWidth();
891 :
892 0 : return nWidth;
893 : }
894 :
895 : //------------------------------------------------------------------------------------
896 0 : sal_Int32 SAL_CALL SvtFilePicker::getAvailableHeight() throw ( RuntimeException )
897 : {
898 0 : checkAlive();
899 :
900 0 : SolarMutexGuard aGuard;
901 0 : sal_Int32 nHeigth = 0;
902 :
903 0 : if ( getDialog() )
904 0 : nHeigth = getDialog()->getAvailableHeight();
905 :
906 0 : return nHeigth;
907 : }
908 :
909 : //------------------------------------------------------------------------------------
910 0 : void SAL_CALL SvtFilePicker::setImage( sal_Int16 aImageFormat, const Any& rImage )
911 : throw ( IllegalArgumentException, RuntimeException )
912 : {
913 0 : checkAlive();
914 :
915 0 : SolarMutexGuard aGuard;
916 0 : if ( getDialog() )
917 0 : getDialog()->setImage( aImageFormat, rImage );
918 0 : }
919 :
920 : //------------------------------------------------------------------------------------
921 0 : sal_Bool SAL_CALL SvtFilePicker::setShowState( sal_Bool bShowState )
922 : throw ( RuntimeException )
923 : {
924 0 : checkAlive();
925 :
926 0 : SolarMutexGuard aGuard;
927 0 : sal_Bool bRet = sal_False;
928 :
929 0 : if ( getDialog() )
930 0 : bRet = getDialog()->setShowState( bShowState );
931 :
932 0 : return bRet;
933 : }
934 :
935 : //------------------------------------------------------------------------------------
936 0 : sal_Bool SAL_CALL SvtFilePicker::getShowState() throw ( RuntimeException )
937 : {
938 0 : checkAlive();
939 :
940 0 : SolarMutexGuard aGuard;
941 0 : sal_Bool bRet = sal_False;
942 :
943 0 : if ( getDialog() )
944 0 : bRet = getDialog()->getShowState();
945 :
946 0 : return bRet;
947 : }
948 :
949 : //------------------------------------------------------------------------------------
950 : // XFilterGroupManager functions
951 : //------------------------------------------------------------------------------------
952 :
953 0 : void SAL_CALL SvtFilePicker::appendFilterGroup( const OUString& sGroupTitle,
954 : const Sequence< StringPair >& aFilters )
955 : throw ( IllegalArgumentException, RuntimeException )
956 : {
957 0 : checkAlive();
958 :
959 0 : SolarMutexGuard aGuard;
960 :
961 : // check the names
962 0 : if ( FilterNameExists( aFilters ) )
963 : throw IllegalArgumentException(
964 : OUString("filter name exists"),
965 0 : static_cast< OWeakObject * >(this), 1);
966 :
967 : // ensure that we have a filter list
968 0 : OUString sInitialCurrentFilter;
969 0 : if ( aFilters.getLength() )
970 0 : sInitialCurrentFilter = aFilters[0].First;
971 0 : ensureFilterList( sInitialCurrentFilter );
972 :
973 : // append the filter
974 0 : m_pFilterList->insert( m_pFilterList->end(), FilterEntry( sGroupTitle, aFilters ) );
975 0 : }
976 :
977 : //------------------------------------------------------------------------------------
978 : // XFilterManager functions
979 : //------------------------------------------------------------------------------------
980 :
981 0 : void SAL_CALL SvtFilePicker::appendFilter( const OUString& aTitle,
982 : const OUString& aFilter )
983 : throw( IllegalArgumentException, RuntimeException )
984 : {
985 0 : checkAlive();
986 :
987 0 : SolarMutexGuard aGuard;
988 : // check the name
989 0 : if ( FilterNameExists( aTitle ) )
990 : // TODO: a more precise exception message
991 0 : throw IllegalArgumentException();
992 :
993 : // ensure that we have a filter list
994 0 : ensureFilterList( aTitle );
995 :
996 : // append the filter
997 0 : m_pFilterList->insert( m_pFilterList->end(), FilterEntry( aTitle, aFilter ) );
998 0 : }
999 :
1000 : //------------------------------------------------------------------------------------
1001 0 : void SAL_CALL SvtFilePicker::setCurrentFilter( const OUString& aTitle )
1002 : throw( IllegalArgumentException, RuntimeException )
1003 : {
1004 0 : checkAlive();
1005 :
1006 0 : SolarMutexGuard aGuard;
1007 0 : if ( ! FilterNameExists( aTitle ) )
1008 0 : throw IllegalArgumentException();
1009 :
1010 0 : m_aCurrentFilter = aTitle;
1011 :
1012 0 : if ( getDialog() )
1013 0 : getDialog()->SetCurFilter( aTitle );
1014 0 : }
1015 :
1016 : //------------------------------------------------------------------------------------
1017 0 : OUString SAL_CALL SvtFilePicker::getCurrentFilter()
1018 : throw( RuntimeException )
1019 : {
1020 0 : checkAlive();
1021 :
1022 0 : SolarMutexGuard aGuard;
1023 0 : OUString aFilter = getDialog() ? OUString( getDialog()->GetCurFilter() ) :
1024 0 : OUString( m_aCurrentFilter );
1025 0 : return aFilter;
1026 : }
1027 :
1028 :
1029 : //------------------------------------------------------------------------------------
1030 : // XInitialization functions
1031 : //------------------------------------------------------------------------------------
1032 :
1033 0 : void SAL_CALL SvtFilePicker::initialize( const Sequence< Any >& _rArguments )
1034 : throw ( Exception, RuntimeException )
1035 : {
1036 0 : checkAlive();
1037 :
1038 0 : Sequence< Any > aArguments( _rArguments.getLength());
1039 :
1040 0 : m_nServiceType = TemplateDescription::FILEOPEN_SIMPLE;
1041 :
1042 0 : if ( _rArguments.getLength() >= 1 )
1043 : {
1044 : // compatibility: one argument, type sal_Int16 , specifies the service type
1045 0 : int index = 0;
1046 :
1047 0 : if (_rArguments[0] >>= m_nServiceType)
1048 : {
1049 : // skip the first entry if it was the ServiceType, because it's not needed in OCommonPicker::initialize and it's not a NamedValue
1050 0 : NamedValue emptyNamedValue;
1051 0 : aArguments[0] <<= emptyNamedValue;
1052 0 : index = 1;
1053 :
1054 : }
1055 0 : for ( int i = index; i < _rArguments.getLength(); i++)
1056 : {
1057 0 : NamedValue namedValue;
1058 0 : aArguments[i] <<= _rArguments[i];
1059 :
1060 0 : if (aArguments[i] >>= namedValue )
1061 : {
1062 :
1063 0 : if ( namedValue.Name == "StandardDir" )
1064 : {
1065 0 : OUString sStandardDir;
1066 :
1067 0 : namedValue.Value >>= sStandardDir;
1068 :
1069 : // Set the directory for the "back to the default dir" button
1070 0 : if ( !sStandardDir.isEmpty() )
1071 : {
1072 0 : m_aStandardDir = sStandardDir;
1073 0 : }
1074 : }
1075 0 : else if ( namedValue.Name == "BlackList" )
1076 : {
1077 0 : namedValue.Value >>= m_aBlackList;
1078 : }
1079 : }
1080 0 : }
1081 : }
1082 :
1083 : // let the base class analyze the sequence (will call into implHandleInitializationArgument)
1084 0 : OCommonPicker::initialize( aArguments );
1085 0 : }
1086 :
1087 : //-------------------------------------------------------------------------
1088 0 : sal_Bool SvtFilePicker::implHandleInitializationArgument( const OUString& _rName, const Any& _rValue ) SAL_THROW( ( Exception, RuntimeException ) )
1089 : {
1090 0 : if ( _rName == "TemplateDescription" )
1091 : {
1092 0 : m_nServiceType = TemplateDescription::FILEOPEN_SIMPLE;
1093 0 : OSL_VERIFY( _rValue >>= m_nServiceType );
1094 0 : return sal_True;
1095 : }
1096 0 : if ( _rName == "StandardDir" )
1097 : {
1098 0 : OSL_VERIFY( _rValue >>= m_aStandardDir );
1099 0 : return sal_True;
1100 : }
1101 :
1102 0 : if ( _rName == "BlackList" )
1103 : {
1104 0 : OSL_VERIFY( _rValue >>= m_aBlackList );
1105 0 : return sal_True;
1106 : }
1107 :
1108 :
1109 :
1110 0 : return OCommonPicker::implHandleInitializationArgument( _rName, _rValue );
1111 : }
1112 :
1113 : //------------------------------------------------------------------------------------
1114 : // XServiceInfo
1115 : //------------------------------------------------------------------------------------
1116 :
1117 : /* XServiceInfo */
1118 0 : OUString SAL_CALL SvtFilePicker::getImplementationName() throw( RuntimeException )
1119 : {
1120 0 : return impl_getStaticImplementationName();
1121 : }
1122 :
1123 : /* XServiceInfo */
1124 0 : sal_Bool SAL_CALL SvtFilePicker::supportsService( const OUString& sServiceName ) throw( RuntimeException )
1125 : {
1126 0 : Sequence< OUString > seqServiceNames = getSupportedServiceNames();
1127 0 : const OUString* pArray = seqServiceNames.getConstArray();
1128 0 : for ( sal_Int32 i = 0; i < seqServiceNames.getLength(); i++ )
1129 : {
1130 0 : if ( sServiceName == pArray[i] )
1131 : {
1132 0 : return sal_True ;
1133 : }
1134 : }
1135 0 : return sal_False ;
1136 : }
1137 :
1138 : /* XServiceInfo */
1139 0 : Sequence< OUString > SAL_CALL SvtFilePicker::getSupportedServiceNames() throw( RuntimeException )
1140 : {
1141 0 : return impl_getStaticSupportedServiceNames();
1142 : }
1143 :
1144 : /* Helper for XServiceInfo */
1145 0 : Sequence< OUString > SvtFilePicker::impl_getStaticSupportedServiceNames()
1146 : {
1147 0 : Sequence< OUString > seqServiceNames( 1 );
1148 0 : OUString* pArray = seqServiceNames.getArray();
1149 0 : pArray[0] = OUString( "com.sun.star.ui.dialogs.OfficeFilePicker" );
1150 0 : return seqServiceNames ;
1151 : }
1152 :
1153 : /* Helper for XServiceInfo */
1154 0 : OUString SvtFilePicker::impl_getStaticImplementationName()
1155 : {
1156 0 : return OUString( "com.sun.star.svtools.OfficeFilePicker" );
1157 : }
1158 :
1159 : /* Helper for registry */
1160 0 : Reference< XInterface > SAL_CALL SvtFilePicker::impl_createInstance(
1161 : const Reference< XComponentContext >& rxContext) throw( Exception )
1162 : {
1163 0 : Reference< XMultiServiceFactory > xServiceManager (rxContext->getServiceManager(), UNO_QUERY_THROW);
1164 0 : return Reference< XInterface >( *new SvtFilePicker( xServiceManager ) );
1165 0 : }
1166 :
1167 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|