bump product version to 5.0.4.1
[LibreOffice.git] / sfx2 / source / dialog / filtergrouping.cxx
blob236f971c5f69af026e0727e7adb642afc3e54a61
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 "filtergrouping.hxx"
21 #include <sfx2/fcontnr.hxx>
22 #include <sfx2/filedlghelper.hxx>
23 #include <sfx2/sfx.hrc>
24 #include <sfx2/docfac.hxx>
25 #include <sfx2/sfxresid.hxx>
26 #include <osl/thread.h>
27 #include <rtl/strbuf.hxx>
28 #include <com/sun/star/ui/dialogs/XFilterGroupManager.hpp>
29 #include <com/sun/star/beans/StringPair.hpp>
30 #include <com/sun/star/uno/Sequence.hxx>
31 #include <unotools/confignode.hxx>
32 #include <comphelper/processfactory.hxx>
33 #include <comphelper/sequenceashashmap.hxx>
34 #include <comphelper/string.hxx>
35 #include <tools/diagnose_ex.h>
37 #include <list>
38 #include <vector>
39 #include <map>
40 #include <algorithm>
43 namespace sfx2
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::ui::dialogs;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::beans;
51 using namespace ::utl;
54 /**
56 Some general words about what's going on here ....
58 <p>In our file open dialog, usually we display every filter we know. That's how it was before: every filter
59 lead to an own line in the filter list box, e.g. "StarWriter 5.0 Dokument" or "Microsoft Word 97".</p>
61 <p>But then the PM came. And everything changed ....</p>
63 <p>A basic idea are groups: Why simply listing all the single filters? Couldn't we draw nice separators
64 between the filters which logically belong together? I.e. all the filters which open a document in StarWriter:
65 couldn't we separate them from all the filters which open the document in StarCalc?<br/>
66 So spoke the PM, and engineering obeyed.</p>
68 <p>So we have groups. They're just a visual aspect: All the filters of a group are presented together, separated
69 by a line from other groups.</p>
71 <p>Let's be honest: How the concrete implementation of the file picker service separates the different groups
72 is a matter of this implementation. We only do this grouping and suggest it to the FilePicker service ...</p>
74 <p>Now for the second concept:<br/>
75 Thinking about it (and that's what the PM did), both "StarWriter 5.0 Dokument" and "Microsoft Word 97"
76 describe a text document. It's a text. It's of no interest for the user that one of the texts was saved in
77 MS' format, and one in our own format.<br/>
78 So in a first step, we want to have a filter entry "Text documents". This would cover both above-mentioned
79 filters, as well as any other filters for documents which are texts.</p>
81 <p>Such an entry as "Text documents" is - within the scope of this file - called "class" or "filter class".</p>
83 <p>In the file-open-dialog, such a class looks like an ordinary filter: it's simply a name in the filter
84 listbox. Selecting means that all the files matching one of the "sub-filters" are displayed (in the example above,
85 this would be "*.sdw", "*.doc" and so on).</p>
87 <p>Now there are two types of filter classes: global ones and local ones. "Text documents" is a global class. As
88 well as "Spreadsheets". Or "Web pages".<br/>
89 Let's have a look at a local class: The filters "MS Word 95" and "MS WinWord 6.0" together form the class
90 "Microsoft Word 6.0 / 95" (don't ask for the reasons. At least not me. Ask the PM). There are a lot of such
91 local classes ...</p>
93 <p>The difference between global and local classes is as follows: Global classes are presented in an own group.
94 There is one dedicated group at the top of the list, containing all the global groups - no local groups and no
95 single filters.</p>
97 <p>Ehm - it was a lie. Not really at the top. Before this group, there is this single "All files" entry. It forms
98 it's own group. But this is uninteresting here.</p>
100 <p>Local classes must consist of filters which - without the classification - would all belong to the same group.
101 Then, they're combined to one entry (in the example above: "Microsoft Word 6.0 / 95"), and this entry is inserted
102 into the file picker filter list, instead of the single filters which form the class.</p>
104 <p>This is an interesting difference between local and global classes: Filters which are part of a global class
105 are listed in there own group, too. Filters in local classes aren't listed a second time - neither directly (as
106 the filter itself) nor indirectly (as part of another local group).</p>
108 <p>The only exception are filters which are part of a global class <em>and</em> a local class. This is allowed.
109 Being cotained in two local classes isn't.</p>
111 <p>So that's all what you need to know: Understand the concept of "filter classes" (a filter class combines
112 different filters and acts as if it's a filter itself) and the concept of groups (a group just describes a
113 logical correlation of filters and usually is represented to the user by drawing group separators in the filter
114 list).</p>
116 <p>If you got it, go try understanding this file :).</p>
123 typedef StringPair FilterDescriptor; // a single filter or a filter class (display name and filter mask)
124 typedef ::std::list< FilterDescriptor > FilterGroup; // a list of single filter entries
125 typedef ::std::list< FilterGroup > GroupedFilterList; // a list of all filters, already grouped
127 /// the logical name of a filter
128 typedef OUString FilterName;
130 // a struct which holds references from a logical filter name to a filter group entry
131 // used for quick lookup of classes (means class entries - entries representing a class)
132 // which a given filter may belong to
133 typedef ::std::map< OUString, FilterGroup::iterator > FilterGroupEntryReferrer;
135 /// a descriptor for a filter class (which in the final dialog is represented by one filter entry)
136 typedef struct _tagFilterClass
138 OUString sDisplayName; // the display name
139 Sequence< FilterName > aSubFilters; // the (logical) names of the filter which belong to the class
140 } FilterClass;
142 typedef ::std::list< FilterClass > FilterClassList;
143 typedef ::std::map< OUString, FilterClassList::iterator > FilterClassReferrer;
145 typedef ::std::vector< OUString > StringArray;
148 // = reading of configuration data
152 void lcl_ReadFilterClass( const OConfigurationNode& _rClassesNode, const OUString& _rLogicalClassName,
153 FilterClass& /* [out] */ _rClass )
155 static const char sDisplaNameNodeName[] = "DisplayName";
156 static const char sSubFiltersNodeName[] = "Filters";
158 // the description node for the current class
159 OConfigurationNode aClassDesc = _rClassesNode.openNode( _rLogicalClassName );
161 // the values
162 aClassDesc.getNodeValue( sDisplaNameNodeName ) >>= _rClass.sDisplayName;
163 aClassDesc.getNodeValue( sSubFiltersNodeName ) >>= _rClass.aSubFilters;
167 struct CreateEmptyClassRememberPos : public ::std::unary_function< FilterName, void >
169 protected:
170 FilterClassList& m_rClassList;
171 FilterClassReferrer& m_rClassesReferrer;
173 public:
174 CreateEmptyClassRememberPos( FilterClassList& _rClassList, FilterClassReferrer& _rClassesReferrer )
175 :m_rClassList ( _rClassList )
176 ,m_rClassesReferrer ( _rClassesReferrer )
180 // operate on a single class name
181 void operator() ( const FilterName& _rLogicalFilterName )
183 // insert a new (empty) class
184 m_rClassList.push_back( FilterClass() );
185 // get the position of this new entry
186 FilterClassList::iterator aInsertPos = m_rClassList.end();
187 --aInsertPos;
188 // remember this position
189 m_rClassesReferrer.insert( FilterClassReferrer::value_type( _rLogicalFilterName, aInsertPos ) );
194 struct ReadGlobalFilter : public ::std::unary_function< FilterName, void >
196 protected:
197 OConfigurationNode m_aClassesNode;
198 FilterClassReferrer& m_aClassReferrer;
200 public:
201 ReadGlobalFilter( const OConfigurationNode& _rClassesNode, FilterClassReferrer& _rClassesReferrer )
202 :m_aClassesNode ( _rClassesNode )
203 ,m_aClassReferrer ( _rClassesReferrer )
207 // operate on a single logical name
208 void operator() ( const FilterName& _rName )
210 FilterClassReferrer::iterator aClassRef = m_aClassReferrer.find( _rName );
211 if ( m_aClassReferrer.end() == aClassRef )
213 // we do not know this global class
214 OSL_FAIL( "ReadGlobalFilter::operator(): unknown filter name!" );
215 // TODO: perhaps we should be more tolerant - at the moment, the filter is dropped
216 // We could silently push_back it to the container ....
218 else
220 // read the data of this class into the node referred to by aClassRef
221 lcl_ReadFilterClass( m_aClassesNode, _rName, *aClassRef->second );
227 void lcl_ReadGlobalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rGlobalClasses, StringArray& _rGlobalClassNames )
229 _rGlobalClasses.clear();
230 _rGlobalClassNames.clear();
233 // get the list describing the order of all global classes
234 Sequence< OUString > aGlobalClasses;
235 _rFilterClassification.getNodeValue( "GlobalFilters/Order" ) >>= aGlobalClasses;
237 const OUString* pNames = aGlobalClasses.getConstArray();
238 const OUString* pNamesEnd = pNames + aGlobalClasses.getLength();
240 // copy the logical names
241 _rGlobalClassNames.resize( aGlobalClasses.getLength() );
242 ::std::copy( pNames, pNamesEnd, _rGlobalClassNames.begin() );
244 // Global classes are presented in an own group, so their order matters (while the order of the
245 // "local classes" doesn't).
246 // That's why we can't simply add the global classes to _rGlobalClasses using the order in which they
247 // are returned from the configuration - it is completely undefined, and we need a _defined_ order.
248 FilterClassReferrer aClassReferrer;
249 ::std::for_each(
250 pNames,
251 pNamesEnd,
252 CreateEmptyClassRememberPos( _rGlobalClasses, aClassReferrer )
254 // now _rGlobalClasses contains a dummy entry for each global class,
255 // while aClassReferrer maps from the logical name of the class to the position within _rGlobalClasses where
256 // it's dummy entry resides
259 // go for all the single class entries
260 OConfigurationNode aFilterClassesNode =
261 _rFilterClassification.openNode( "GlobalFilters/Classes" );
262 Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
263 ::std::for_each(
264 aFilterClasses.getConstArray(),
265 aFilterClasses.getConstArray() + aFilterClasses.getLength(),
266 ReadGlobalFilter( aFilterClassesNode, aClassReferrer )
271 struct ReadLocalFilter : public ::std::unary_function< FilterName, void >
273 protected:
274 OConfigurationNode m_aClassesNode;
275 FilterClassList& m_rClasses;
277 public:
278 ReadLocalFilter( const OConfigurationNode& _rClassesNode, FilterClassList& _rClasses )
279 :m_aClassesNode ( _rClassesNode )
280 ,m_rClasses ( _rClasses )
284 // operate on a single logical name
285 void operator() ( const FilterName& _rName )
287 // read the data for this class
288 FilterClass aClass;
289 lcl_ReadFilterClass( m_aClassesNode, _rName, aClass );
291 // insert the class descriptor
292 m_rClasses.push_back( aClass );
297 void lcl_ReadLocalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rLocalClasses )
299 _rLocalClasses.clear();
301 // the node for the local classes
302 OConfigurationNode aFilterClassesNode =
303 _rFilterClassification.openNode( "LocalFilters/Classes" );
304 Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
306 ::std::for_each(
307 aFilterClasses.getConstArray(),
308 aFilterClasses.getConstArray() + aFilterClasses.getLength(),
309 ReadLocalFilter( aFilterClassesNode, _rLocalClasses )
314 void lcl_ReadClassification( FilterClassList& _rGlobalClasses, StringArray& _rGlobalClassNames, FilterClassList& _rLocalClasses )
317 // open our config node
318 OConfigurationTreeRoot aFilterClassification = OConfigurationTreeRoot::createWithComponentContext(
319 ::comphelper::getProcessComponentContext(),
320 "org.openoffice.Office.UI/FilterClassification",
322 OConfigurationTreeRoot::CM_READONLY
326 // go for the global classes
327 lcl_ReadGlobalFilters( aFilterClassification, _rGlobalClasses, _rGlobalClassNames );
330 // fo for the local classes
331 lcl_ReadLocalFilters( aFilterClassification, _rLocalClasses );
336 // = grouping and classifying
340 // a struct which adds helps remembering a reference to a class entry
341 struct ReferToFilterEntry : public ::std::unary_function< FilterName, void >
343 protected:
344 FilterGroupEntryReferrer& m_rEntryReferrer;
345 FilterGroup::iterator m_aClassPos;
347 public:
348 ReferToFilterEntry( FilterGroupEntryReferrer& _rEntryReferrer, const FilterGroup::iterator& _rClassPos )
349 :m_rEntryReferrer( _rEntryReferrer )
350 ,m_aClassPos( _rClassPos )
354 // operate on a single filter name
355 void operator() ( const FilterName& _rName )
357 ::std::pair< FilterGroupEntryReferrer::iterator, bool > aInsertRes =
358 m_rEntryReferrer.insert( FilterGroupEntryReferrer::value_type( _rName, m_aClassPos ) );
359 SAL_WARN_IF(
360 !aInsertRes.second, "sfx.dialog",
361 "already have an element for " << _rName);
366 struct FillClassGroup : public ::std::unary_function< FilterClass, void >
368 protected:
369 FilterGroup& m_rClassGroup;
370 FilterGroupEntryReferrer& m_rClassReferrer;
372 public:
373 FillClassGroup( FilterGroup& _rClassGroup, FilterGroupEntryReferrer& _rClassReferrer )
374 :m_rClassGroup ( _rClassGroup )
375 ,m_rClassReferrer ( _rClassReferrer )
379 // operate on a single class
380 void operator() ( const FilterClass& _rClass )
382 // create an empty filter descriptor for the class
383 FilterDescriptor aClassEntry;
384 // set its name (which is all we know by now)
385 aClassEntry.First = _rClass.sDisplayName;
387 // add it to the group
388 m_rClassGroup.push_back( aClassEntry );
389 // the position of the newly added class
390 FilterGroup::iterator aClassEntryPos = m_rClassGroup.end();
391 --aClassEntryPos;
393 // and for all the sub filters of the class, remember the class
394 // (respectively the position of the class it the group)
395 ::std::for_each(
396 _rClass.aSubFilters.getConstArray(),
397 _rClass.aSubFilters.getConstArray() + _rClass.aSubFilters.getLength(),
398 ReferToFilterEntry( m_rClassReferrer, aClassEntryPos )
404 static const sal_Unicode s_cWildcardSeparator( ';' );
406 static OUString getSeparatorString()
408 return OUString(";");
412 struct CheckAppendSingleWildcard : public ::std::unary_function< OUString, void >
414 OUString& _rToBeExtended;
416 CheckAppendSingleWildcard( OUString& _rBase ) : _rToBeExtended( _rBase ) { }
418 void operator() ( const OUString& _rWC )
420 // check for double wildcards
421 sal_Int32 nExistentPos = _rToBeExtended.indexOf( _rWC );
422 if ( -1 < nExistentPos )
423 { // found this wildcard (already part of _rToBeExtended)
424 const sal_Unicode* pBuffer = _rToBeExtended.getStr();
425 if ( ( 0 == nExistentPos )
426 || ( s_cWildcardSeparator == pBuffer[ nExistentPos - 1 ] )
428 { // the wildcard really starts at this position (it starts at pos 0 or the previous character is a separator
429 sal_Int32 nExistentWCEnd = nExistentPos + _rWC.getLength();
430 if ( ( _rToBeExtended.getLength() == nExistentWCEnd )
431 || ( s_cWildcardSeparator == pBuffer[ nExistentWCEnd ] )
433 { // it's really the complete wildcard we found
434 // (not something like _rWC being "*.t" and _rToBeExtended containing "*.txt")
435 // -> outta here
436 return;
441 if ( !_rToBeExtended.isEmpty() )
442 _rToBeExtended += getSeparatorString();
443 _rToBeExtended += _rWC;
448 // a helper struct which adds a fixed (Sfx-)filter to a filter group entry given by iterator
449 struct AppendWildcardToDescriptor : public ::std::unary_function< FilterGroupEntryReferrer::value_type, void >
451 protected:
452 ::std::vector< OUString > aWildCards;
454 public:
455 AppendWildcardToDescriptor( const OUString& _rWildCard );
457 // operate on a single class entry
458 void operator() ( const FilterGroupEntryReferrer::value_type& _rClassReference )
460 // simply add our wildcards
461 ::std::for_each(
462 aWildCards.begin(),
463 aWildCards.end(),
464 CheckAppendSingleWildcard( _rClassReference.second->Second )
470 AppendWildcardToDescriptor::AppendWildcardToDescriptor( const OUString& _rWildCard )
472 DBG_ASSERT( !_rWildCard.isEmpty(),
473 "AppendWildcardToDescriptor::AppendWildcardToDescriptor: invalid wildcard!" );
474 DBG_ASSERT( _rWildCard[0] != s_cWildcardSeparator,
475 "AppendWildcardToDescriptor::AppendWildcardToDescriptor: wildcard already separated!" );
477 aWildCards.reserve( comphelper::string::getTokenCount(_rWildCard, s_cWildcardSeparator) );
479 const sal_Unicode* pTokenLoop = _rWildCard.getStr();
480 const sal_Unicode* pTokenLoopEnd = pTokenLoop + _rWildCard.getLength();
481 const sal_Unicode* pTokenStart = pTokenLoop;
482 for ( ; pTokenLoop != pTokenLoopEnd; ++pTokenLoop )
484 if ( ( s_cWildcardSeparator == *pTokenLoop ) && ( pTokenLoop > pTokenStart ) )
485 { // found a new token separator (and a non-empty token)
486 aWildCards.push_back( OUString( pTokenStart, pTokenLoop - pTokenStart ) );
488 // search the start of the next token
489 while ( ( pTokenStart != pTokenLoopEnd ) && ( *pTokenStart != s_cWildcardSeparator ) )
490 ++pTokenStart;
492 if ( pTokenStart == pTokenLoopEnd )
493 // reached the end
494 break;
496 ++pTokenStart;
497 pTokenLoop = pTokenStart;
500 if ( pTokenLoop > pTokenStart )
501 // the last one ....
502 aWildCards.push_back( OUString( pTokenStart, pTokenLoop - pTokenStart ) );
506 void lcl_InitGlobalClasses( GroupedFilterList& _rAllFilters, const FilterClassList& _rGlobalClasses, FilterGroupEntryReferrer& _rGlobalClassesRef )
508 // we need an extra group in our "all filters" container
509 _rAllFilters.push_front( FilterGroup() );
510 FilterGroup& rGlobalFilters = _rAllFilters.front();
511 // it's important to work on the reference: we want to access the members of this filter group
512 // by an iterator (FilterGroup::const_iterator)
513 // the referrer for the global classes
515 // initialize the group
516 ::std::for_each(
517 _rGlobalClasses.begin(),
518 _rGlobalClasses.end(),
519 FillClassGroup( rGlobalFilters, _rGlobalClassesRef )
521 // now we have:
522 // in rGlobalFilters: a list of FilterDescriptor's, where each's discriptor's display name is set to the name of a class
523 // in aGlobalClassesRef: a mapping from logical filter names to positions within rGlobalFilters
524 // this way, if we encounter an arbitrary filter, we can easily (and efficient) check if it belongs to a global class
525 // and modify the descriptor for this class accordingly
529 typedef ::std::vector< ::std::pair< FilterGroupEntryReferrer::mapped_type, FilterGroup::iterator > >
530 MapGroupEntry2GroupEntry;
531 // this is not really a map - it's just called this way because it is used as a map
533 struct FindGroupEntry : public ::std::unary_function< MapGroupEntry2GroupEntry::value_type, sal_Bool >
535 FilterGroupEntryReferrer::mapped_type aLookingFor;
536 FindGroupEntry( FilterGroupEntryReferrer::mapped_type _rLookingFor ) : aLookingFor( _rLookingFor ) { }
538 bool operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
540 return _rMapEntry.first == aLookingFor;
544 struct CopyGroupEntryContent : public ::std::unary_function< MapGroupEntry2GroupEntry::value_type, void >
546 void operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
548 #ifdef DBG_UTIL
549 FilterDescriptor aHaveALook = *_rMapEntry.first;
550 #endif
551 *_rMapEntry.second = *_rMapEntry.first;
556 struct CopyNonEmptyFilter : public ::std::unary_function< FilterDescriptor, void >
558 FilterGroup& rTarget;
559 CopyNonEmptyFilter( FilterGroup& _rTarget ) :rTarget( _rTarget ) { }
561 void operator() ( const FilterDescriptor& _rFilter )
563 if ( !_rFilter.Second.isEmpty() )
564 rTarget.push_back( _rFilter );
569 void lcl_GroupAndClassify( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rAllFilters )
571 _rAllFilters.clear();
574 // read the classification of filters
575 FilterClassList aGlobalClasses, aLocalClasses;
576 StringArray aGlobalClassNames;
577 lcl_ReadClassification( aGlobalClasses, aGlobalClassNames, aLocalClasses );
580 // for the global filter classes
581 FilterGroupEntryReferrer aGlobalClassesRef;
582 lcl_InitGlobalClasses( _rAllFilters, aGlobalClasses, aGlobalClassesRef );
584 // insert as much placeholders (FilterGroup's) into _rAllFilter for groups as we have global classes
585 // (this assumes that both numbers are the same, which, speaking strictly, must not hold - but it does, as we know ...)
586 sal_Int32 nGlobalClasses = aGlobalClasses.size();
587 while ( nGlobalClasses-- )
588 _rAllFilters.push_back( FilterGroup() );
591 // for the local classes:
592 // if n filters belong to a local class, they do not appear in their respective group explicitly, instead
593 // and entry for the class is added to the group and the extensions of the filters are collected under
594 // this entry
595 FilterGroupEntryReferrer aLocalClassesRef;
596 FilterGroup aCollectedLocals;
597 ::std::for_each(
598 aLocalClasses.begin(),
599 aLocalClasses.end(),
600 FillClassGroup( aCollectedLocals, aLocalClassesRef )
602 // to map from the position within aCollectedLocals to positions within the real groups
603 // (where they finally belong to)
604 MapGroupEntry2GroupEntry aLocalFinalPositions;
607 // now add the filters
608 // the group which we currently work with
609 GroupedFilterList::iterator aCurrentGroup = _rAllFilters.end(); // no current group
610 // the filter container of the current group - if this changes between two filters, a new group is reached
611 OUString aCurrentServiceName;
613 OUString sFilterWildcard;
614 OUString sFilterName;
615 // loop through all the filters
616 for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
618 sFilterName = pFilter->GetFilterName();
619 sFilterWildcard = pFilter->GetWildcard().getGlob();
620 AppendWildcardToDescriptor aExtendWildcard( sFilterWildcard );
622 DBG_ASSERT( !sFilterWildcard.isEmpty(), "sfx2::lcl_GroupAndClassify: invalid wildcard of this filter!" );
625 // check for a change in the group
626 OUString aServiceName = pFilter->GetServiceName();
627 if ( aServiceName != aCurrentServiceName )
628 { // we reached a new group
630 OUString sDocServName = aServiceName;
632 // look for the place in _rAllFilters where this ne group belongs - this is determined
633 // by the order of classes in aGlobalClassNames
634 GroupedFilterList::iterator aGroupPos = _rAllFilters.begin();
635 DBG_ASSERT( aGroupPos != _rAllFilters.end(),
636 "sfx2::lcl_GroupAndClassify: invalid all-filters array here!" );
637 // the loop below will work on invalid objects else ...
638 ++aGroupPos;
639 StringArray::iterator aGlobalIter = aGlobalClassNames.begin();
640 while ( ( aGroupPos != _rAllFilters.end() )
641 && ( aGlobalIter != aGlobalClassNames.end() )
642 && ( *aGlobalIter != sDocServName )
645 ++aGlobalIter;
646 ++aGroupPos;
648 if ( aGroupPos != _rAllFilters.end() )
649 // we found a global class name which matchies the doc service name -> fill the filters of this
650 // group in the respective prepared group
651 aCurrentGroup = aGroupPos;
652 else
653 // insert a new entry in our overall-list
654 aCurrentGroup = _rAllFilters.insert( _rAllFilters.end(), FilterGroup() );
656 // remember the container to properly detect the next group
657 aCurrentServiceName = aServiceName;
660 assert(aCurrentGroup != _rAllFilters.end()); //invalid current group!
661 if (aCurrentGroup == _rAllFilters.end())
662 aCurrentGroup = _rAllFilters.begin();
665 // check if the filter is part of a global group
666 ::std::pair< FilterGroupEntryReferrer::iterator, FilterGroupEntryReferrer::iterator >
667 aBelongsTo = aGlobalClassesRef.equal_range( sFilterName );
668 // add the filter to the entries for these classes
669 // (if they exist - if not, the range is empty and the for_each is a no-op)
670 ::std::for_each(
671 aBelongsTo.first,
672 aBelongsTo.second,
673 aExtendWildcard
677 // add the filter to it's group
679 // for this, check if the filter is part of a local filter
680 FilterGroupEntryReferrer::iterator aBelongsToLocal = aLocalClassesRef.find( sFilterName );
681 if ( aLocalClassesRef.end() != aBelongsToLocal )
683 // okay, there is a local class which the filter belongs to
684 // -> append the wildcard
685 aExtendWildcard( *aBelongsToLocal );
687 MapGroupEntry2GroupEntry::iterator aThisGroupFinalPos =
688 ::std::find_if( aLocalFinalPositions.begin(), aLocalFinalPositions.end(), FindGroupEntry( aBelongsToLocal->second ) );
690 if ( aLocalFinalPositions.end() == aThisGroupFinalPos )
691 { // the position within aCollectedLocals has not been mapped to a final position
692 // within the "real" group (aCollectedLocals is only temporary)
693 // -> do this now (as we just encountered the first filter belonging to this local class
694 // add a new entry which is the "real" group entry
695 aCurrentGroup->push_back( FilterDescriptor( aBelongsToLocal->second->First, OUString() ) );
696 // the position where we inserted the entry
697 FilterGroup::iterator aInsertPos = aCurrentGroup->end();
698 --aInsertPos;
699 // remember this pos
700 aLocalFinalPositions.push_back( MapGroupEntry2GroupEntry::value_type( aBelongsToLocal->second, aInsertPos ) );
703 else
704 aCurrentGroup->push_back( FilterDescriptor( pFilter->GetUIName(), sFilterWildcard ) );
707 // now just complete the infos for the local groups:
708 // During the above loop, they have been collected in aCollectedLocals, but this is only temporary
709 // They have to be copied into their final positions (which are stored in aLocalFinalPositions)
710 ::std::for_each(
711 aLocalFinalPositions.begin(),
712 aLocalFinalPositions.end(),
713 CopyGroupEntryContent()
716 // and remove local groups which do not apply - e.g. have no entries due to the limited content of the
717 // current SfxFilterMatcherIter
719 FilterGroup& rGlobalFilters = _rAllFilters.front();
720 FilterGroup aNonEmptyGlobalFilters;
721 ::std::for_each(
722 rGlobalFilters.begin(),
723 rGlobalFilters.end(),
724 CopyNonEmptyFilter( aNonEmptyGlobalFilters )
726 rGlobalFilters.swap( aNonEmptyGlobalFilters );
730 struct AppendFilter : public ::std::unary_function< FilterDescriptor, void >
732 protected:
733 Reference< XFilterManager > m_xFilterManager;
734 FileDialogHelper_Impl* m_pFileDlgImpl;
735 bool m_bAddExtension;
737 public:
738 AppendFilter( const Reference< XFilterManager >& _rxFilterManager,
739 FileDialogHelper_Impl* _pImpl, bool _bAddExtension ) :
741 m_xFilterManager( _rxFilterManager ),
742 m_pFileDlgImpl ( _pImpl ),
743 m_bAddExtension ( _bAddExtension )
746 DBG_ASSERT( m_xFilterManager.is(), "AppendFilter::AppendFilter: invalid filter manager!" );
747 DBG_ASSERT( m_pFileDlgImpl, "AppendFilter::AppendFilter: invalid filedlg impl!" );
750 // operate on a single filter
751 void operator() ( const FilterDescriptor& _rFilterEntry )
753 OUString sDisplayText = m_bAddExtension
754 ? addExtension( _rFilterEntry.First, _rFilterEntry.Second, true, *m_pFileDlgImpl )
755 : _rFilterEntry.First;
756 m_xFilterManager->appendFilter( sDisplayText, _rFilterEntry.Second );
761 // = handling for the "all files" entry
765 bool lcl_hasAllFilesFilter( TSortedFilterList& _rFilterMatcher, OUString& /* [out] */ _rAllFilterName )
767 bool bHasAll = false;
768 _rAllFilterName = SfxResId( STR_SFX_FILTERNAME_ALL ).toString();
771 // check if there's already a filter <ALL>
772 for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter && !bHasAll; pFilter = _rFilterMatcher.Next() )
774 if ( pFilter->GetUIName() == _rAllFilterName )
775 bHasAll = true;
777 return bHasAll;
781 void lcl_EnsureAllFilesEntry( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rFilters )
784 OUString sAllFilterName;
785 if ( !lcl_hasAllFilesFilter( _rFilterMatcher, sAllFilterName ) )
787 // get the first group of filters (by definition, this group contains the global classes)
788 DBG_ASSERT( !_rFilters.empty(), "lcl_EnsureAllFilesEntry: invalid filter list!" );
789 if ( !_rFilters.empty() )
791 FilterGroup& rGlobalClasses = *_rFilters.begin();
792 rGlobalClasses.push_front( FilterDescriptor( sAllFilterName, FILEDIALOG_FILTER_ALL ) );
798 // = filling an XFilterManager
802 struct AppendFilterGroup : public ::std::unary_function< FilterGroup, void >
804 protected:
805 Reference< XFilterManager > m_xFilterManager;
806 Reference< XFilterGroupManager > m_xFilterGroupManager;
807 FileDialogHelper_Impl* m_pFileDlgImpl;
809 public:
810 AppendFilterGroup( const Reference< XFilterManager >& _rxFilterManager, FileDialogHelper_Impl* _pImpl )
811 :m_xFilterManager ( _rxFilterManager )
812 ,m_xFilterGroupManager ( _rxFilterManager, UNO_QUERY )
813 ,m_pFileDlgImpl ( _pImpl )
815 DBG_ASSERT( m_xFilterManager.is(), "AppendFilterGroup::AppendFilterGroup: invalid filter manager!" );
816 DBG_ASSERT( m_pFileDlgImpl, "AppendFilterGroup::AppendFilterGroup: invalid filedlg impl!" );
819 void appendGroup( const FilterGroup& _rGroup, bool _bAddExtension )
823 if ( m_xFilterGroupManager.is() )
824 { // the file dialog implementation supports visual grouping of filters
825 // create a representation of the group which is understandable by the XFilterGroupManager
826 if ( _rGroup.size() )
828 Sequence< StringPair > aFilters( _rGroup.size() );
829 ::std::copy(
830 _rGroup.begin(),
831 _rGroup.end(),
832 aFilters.getArray()
834 if ( _bAddExtension )
836 StringPair* pFilters = aFilters.getArray();
837 StringPair* pEnd = pFilters + aFilters.getLength();
838 for ( ; pFilters != pEnd; ++pFilters )
839 pFilters->First = addExtension( pFilters->First, pFilters->Second, true, *m_pFileDlgImpl );
841 m_xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
844 else
846 ::std::for_each(
847 _rGroup.begin(),
848 _rGroup.end(),
849 AppendFilter( m_xFilterManager, m_pFileDlgImpl, _bAddExtension ) );
852 catch( const Exception& )
854 DBG_UNHANDLED_EXCEPTION();
858 // operate on a single filter group
859 void operator() ( const FilterGroup& _rGroup )
861 appendGroup( _rGroup, true );
866 TSortedFilterList::TSortedFilterList(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration >& xFilterList)
867 : m_nIterator(0)
869 if (!xFilterList.is())
870 return;
872 m_lFilters.clear();
873 while(xFilterList->hasMoreElements())
875 ::comphelper::SequenceAsHashMap lFilterProps (xFilterList->nextElement());
876 OUString sFilterName = lFilterProps.getUnpackedValueOrDefault(
877 OUString("Name"),
878 OUString());
879 if (!sFilterName.isEmpty())
880 m_lFilters.push_back(sFilterName);
885 const SfxFilter* TSortedFilterList::First()
887 m_nIterator = 0;
888 return impl_getFilter(m_nIterator);
892 const SfxFilter* TSortedFilterList::Next()
894 ++m_nIterator;
895 return impl_getFilter(m_nIterator);
899 const SfxFilter* TSortedFilterList::impl_getFilter(sal_Int32 nIndex)
901 if (nIndex<0 || nIndex>=(sal_Int32)m_lFilters.size())
902 return 0;
903 const OUString& sFilterName = m_lFilters[nIndex];
904 if (sFilterName.isEmpty())
905 return 0;
906 return SfxFilter::GetFilterByName(sFilterName);
910 void appendFiltersForSave( TSortedFilterList& _rFilterMatcher,
911 const Reference< XFilterManager >& _rxFilterManager,
912 OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl,
913 const OUString& _rFactory )
915 DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForSave: invalid manager!" );
916 if ( !_rxFilterManager.is() )
917 return;
919 OUString sUIName;
920 OUString sExtension;
922 // retrieve the default filter for this application module.
923 // It must be set as first of the generated filter list.
924 const SfxFilter* pDefaultFilter = SfxFilterContainer::GetDefaultFilter_Impl(_rFactory);
925 // Only use one extension (#i32434#)
926 // (and always the first if there are more than one)
927 sExtension = pDefaultFilter->GetWildcard().getGlob().getToken(0, ';');
928 sUIName = addExtension( pDefaultFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
931 _rxFilterManager->appendFilter( sUIName, sExtension );
932 if ( _rFirstNonEmpty.isEmpty() )
933 _rFirstNonEmpty = sUIName;
935 catch( const IllegalArgumentException& )
937 SAL_WARN( "sfx.dialog", "Could not append DefaultFilter" << sUIName );
940 for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
942 if (pFilter->GetName() == pDefaultFilter->GetName())
943 continue;
945 // Only use one extension (#i32434#)
946 // (and always the first if there are more than one)
947 sExtension = pFilter->GetWildcard().getGlob().getToken(0, ';');
948 sUIName = addExtension( pFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
951 _rxFilterManager->appendFilter( sUIName, sExtension );
952 if ( _rFirstNonEmpty.isEmpty() )
953 _rFirstNonEmpty = sUIName;
955 catch( const IllegalArgumentException& )
957 SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
962 struct ExportFilter
964 ExportFilter( const OUString& _aUIName, const OUString& _aWildcard ) :
965 aUIName( _aUIName ), aWildcard( _aWildcard ) {}
967 OUString aUIName;
968 OUString aWildcard;
972 void appendExportFilters( TSortedFilterList& _rFilterMatcher,
973 const Reference< XFilterManager >& _rxFilterManager,
974 OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
976 DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendExportFilters: invalid manager!" );
977 if ( !_rxFilterManager.is() )
978 return;
980 sal_Int32 nHTMLIndex = -1;
981 sal_Int32 nXHTMLIndex = -1;
982 sal_Int32 nPDFIndex = -1;
983 sal_Int32 nFlashIndex = -1;
984 OUString sUIName;
985 OUString sExtensions;
986 std::vector< ExportFilter > aImportantFilterGroup;
987 std::vector< ExportFilter > aFilterGroup;
988 Reference< XFilterGroupManager > xFilterGroupManager( _rxFilterManager, UNO_QUERY );
989 OUString sTypeName;
990 const OUString sWriterHTMLType( "generic_HTML" );
991 const OUString sGraphicHTMLType( "graphic_HTML" );
992 const OUString sXHTMLType( "XHTML_File" );
993 const OUString sPDFType( "pdf_Portable_Document_Format" );
994 const OUString sFlashType( "graphic_SWF" );
996 for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
998 sTypeName = pFilter->GetTypeName();
999 sUIName = pFilter->GetUIName();
1000 sExtensions = pFilter->GetWildcard().getGlob();
1001 ExportFilter aExportFilter( sUIName, sExtensions );
1003 if ( nHTMLIndex == -1 &&
1004 ( sTypeName.equals( sWriterHTMLType ) || sTypeName.equals( sGraphicHTMLType ) ) )
1006 aImportantFilterGroup.insert( aImportantFilterGroup.begin(), aExportFilter );
1007 nHTMLIndex = 0;
1009 else if ( nXHTMLIndex == -1 && sTypeName.equals( sXHTMLType ) )
1011 std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
1012 if ( nHTMLIndex == -1 )
1013 aImportantFilterGroup.insert( aIter, aExportFilter );
1014 else
1015 aImportantFilterGroup.insert( ++aIter, aExportFilter );
1016 nXHTMLIndex = 0;
1018 else if ( nPDFIndex == -1 && sTypeName.equals( sPDFType ) )
1020 std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
1021 if ( nHTMLIndex != -1 )
1022 ++aIter;
1023 if ( nXHTMLIndex != -1 )
1024 ++aIter;
1025 aImportantFilterGroup.insert( aIter, aExportFilter );
1026 nPDFIndex = 0;
1028 else if ( nFlashIndex == -1 && sTypeName.equals( sFlashType ) )
1030 std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
1031 if ( nHTMLIndex != -1 )
1032 ++aIter;
1033 if ( nXHTMLIndex != -1 )
1034 ++aIter;
1035 if ( nPDFIndex != -1 )
1036 ++aIter;
1037 aImportantFilterGroup.insert( aIter, aExportFilter );
1038 nFlashIndex = 0;
1040 else
1041 aFilterGroup.push_back( aExportFilter );
1044 if ( xFilterGroupManager.is() )
1046 // Add both html/pdf filter as a filter group to get a separator between both groups
1047 if ( !aImportantFilterGroup.empty() )
1049 Sequence< StringPair > aFilters( aImportantFilterGroup.size() );
1050 for ( sal_Int32 i = 0; i < (sal_Int32)aImportantFilterGroup.size(); i++ )
1052 aFilters[i].First = addExtension( aImportantFilterGroup[i].aUIName,
1053 aImportantFilterGroup[i].aWildcard,
1054 false, _rFileDlgImpl );
1055 aFilters[i].Second = aImportantFilterGroup[i].aWildcard;
1060 xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
1062 catch( const IllegalArgumentException& )
1067 if ( !aFilterGroup.empty() )
1069 Sequence< StringPair > aFilters( aFilterGroup.size() );
1070 for ( sal_Int32 i = 0; i < (sal_Int32)aFilterGroup.size(); i++ )
1072 aFilters[i].First = addExtension( aFilterGroup[i].aUIName,
1073 aFilterGroup[i].aWildcard,
1074 false, _rFileDlgImpl );
1075 aFilters[i].Second = aFilterGroup[i].aWildcard;
1080 xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
1082 catch( const IllegalArgumentException& )
1087 else
1089 // Fallback solution just add both filter groups as single filters
1090 sal_Int32 n;
1092 for ( n = 0; n < (sal_Int32)aImportantFilterGroup.size(); n++ )
1096 OUString aUIName = addExtension( aImportantFilterGroup[n].aUIName,
1097 aImportantFilterGroup[n].aWildcard,
1098 false, _rFileDlgImpl );
1099 _rxFilterManager->appendFilter( aUIName, aImportantFilterGroup[n].aWildcard );
1100 if ( _rFirstNonEmpty.isEmpty() )
1101 _rFirstNonEmpty = sUIName;
1104 catch( const IllegalArgumentException& )
1106 SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
1110 for ( n = 0; n < (sal_Int32)aFilterGroup.size(); n++ )
1114 OUString aUIName = addExtension( aFilterGroup[n].aUIName,
1115 aFilterGroup[n].aWildcard,
1116 false, _rFileDlgImpl );
1117 _rxFilterManager->appendFilter( aUIName, aFilterGroup[n].aWildcard );
1118 if ( _rFirstNonEmpty.isEmpty() )
1119 _rFirstNonEmpty = sUIName;
1122 catch( const IllegalArgumentException& )
1124 SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
1131 void appendFiltersForOpen( TSortedFilterList& _rFilterMatcher,
1132 const Reference< XFilterManager >& _rxFilterManager,
1133 OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
1135 DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForOpen: invalid manager!" );
1136 if ( !_rxFilterManager.is() )
1137 return;
1140 // group and classify the filters
1141 GroupedFilterList aAllFilters;
1142 lcl_GroupAndClassify( _rFilterMatcher, aAllFilters );
1145 // ensure that we have the one "all files" entry
1146 lcl_EnsureAllFilesEntry( _rFilterMatcher, aAllFilters );
1149 // the first non-empty string - which we assume is the first overall entry
1150 if ( !aAllFilters.empty() )
1152 const FilterGroup& rFirstGroup = *aAllFilters.begin(); // should be the global classes
1153 if ( !rFirstGroup.empty() )
1154 _rFirstNonEmpty = rFirstGroup.begin()->First;
1155 // append first group, without extension
1156 AppendFilterGroup aGroup( _rxFilterManager, &_rFileDlgImpl );
1157 aGroup.appendGroup( rFirstGroup, false );
1161 // append the filters to the manager
1162 if ( !aAllFilters.empty() )
1164 ::std::list< FilterGroup >::iterator pIter = aAllFilters.begin();
1165 ++pIter;
1166 ::std::for_each(
1167 pIter, // first filter group was handled separately, see above
1168 aAllFilters.end(),
1169 AppendFilterGroup( _rxFilterManager, &_rFileDlgImpl ) );
1173 OUString addExtension( const OUString& _rDisplayText,
1174 const OUString& _rExtension,
1175 bool _bForOpen, FileDialogHelper_Impl& _rFileDlgImpl )
1177 static const char sAllFilter[] = "(*.*)";
1178 static const char sOpenBracket[] = " (";
1179 static const char sCloseBracket[] = ")";
1180 OUString sRet = _rDisplayText;
1182 if ( sRet.indexOf( sAllFilter ) == -1 )
1184 OUString sExt = _rExtension;
1185 if ( !_bForOpen )
1187 // show '*' in extensions only when opening a document
1188 sExt = comphelper::string::remove(sExt, '*');
1190 sRet += sOpenBracket;
1191 sRet += sExt;
1192 sRet += sCloseBracket;
1194 _rFileDlgImpl.addFilterPair( _rDisplayText, sRet );
1195 return sRet;
1199 } // namespace sfx2
1203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */