LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / dialog / filtergrouping.cxx
blob195fa09dd9bc1696fc1a8f0387b2babedfa64f0f
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/strings.hrc>
24 #include <sfx2/docfilt.hxx>
25 #include <sfx2/sfxresid.hxx>
26 #include <sal/log.hxx>
27 #include <com/sun/star/ui/dialogs/XFilterGroupManager.hpp>
28 #include <com/sun/star/beans/StringPair.hpp>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <unotools/confignode.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <comphelper/sequenceashashmap.hxx>
33 #include <comphelper/sequence.hxx>
34 #include <comphelper/string.hxx>
35 #include <tools/diagnose_ex.h>
36 #include <tools/debug.hxx>
38 #include <list>
39 #include <vector>
40 #include <map>
41 #include <algorithm>
44 namespace sfx2
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::ui::dialogs;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::beans;
52 using namespace ::utl;
55 /**
57 Some general words about what's going on here...
59 <p>In our file open dialog, usually we display every filter we know. That's how it was before: every filter
60 lead to an own line in the filter list box, e.g. "StarWriter 5.0 Document" or "Microsoft Word 97".</p>
62 <p>But then the PM came. And everything changed...</p>
64 <p>A basic idea are groups: Why simply listing all the single filters? Couldn't we draw nice separators
65 between the filters which logically belong together? I.e. all the filters which open a document in StarWriter:
66 couldn't we separate them from all the filters which open the document in StarCalc?<br/>
67 So spoke the PM, and engineering obeyed.</p>
69 <p>So we have groups. They're just a visual aspect: All the filters of a group are presented together, separated
70 by a line from other groups.</p>
72 <p>Let's be honest: How the concrete implementation of the file picker service separates the different groups
73 is a matter of this implementation. We only do this grouping and suggest it to the FilePicker service ...</p>
75 <p>Now for the second concept:<br/>
76 Thinking about it (and that's what the PM did), both "StarWriter 5.0 Document" and "Microsoft Word 97"
77 describe a text document. It's a text. It's of no interest for the user that one of the texts was saved in
78 MS' format, and one in our own format.<br/>
79 So in a first step, we want to have a filter entry "Text documents". This would cover both above-mentioned
80 filters, as well as any other filters for documents which are texts.</p>
82 <p>Such an entry as "Text documents" is - within the scope of this file - called "class" or "filter class".</p>
84 <p>In the file-open-dialog, such a class looks like an ordinary filter: it's simply a name in the filter
85 listbox. Selecting means that all the files matching one of the "sub-filters" are displayed (in the example above,
86 this would be "*.sdw", "*.doc" and so on).</p>
88 <p>Now there are two types of filter classes: global ones and local ones. "Text documents" is a global class. As
89 well as "Spreadsheets". Or "Web pages".<br/>
90 Let's have a look at a local class: The filters "MS Word 95" and "MS WinWord 6.0" together form the class
91 "Microsoft Word 6.0 / 95" (don't ask for the reasons. At least not me. Ask the PM). There are a lot of such
92 local classes ...</p>
94 <p>The difference between global and local classes is as follows: Global classes are presented in an own group.
95 There is one dedicated group at the top of the list, containing all the global groups - no local groups and no
96 single filters.</p>
98 <p>Ehm - it was a lie. Not really at the top. Before this group, there is this single "All files" entry. It forms
99 its own group. But this is uninteresting here.</p>
101 <p>Local classes must consist of filters which - without the classification - would all belong to the same group.
102 Then, they're combined to one entry (in the example above: "Microsoft Word 6.0 / 95"), and this entry is inserted
103 into the file picker filter list, instead of the single filters which form the class.</p>
105 <p>This is an interesting difference between local and global classes: Filters which are part of a global class
106 are listed in their own group, too. Filters in local classes aren't listed a second time - neither directly (as
107 the filter itself) nor indirectly (as part of another local group).</p>
109 <p>The only exception are filters which are part of a global class <em>and</em> a local class. This is allowed.
110 Being contained in two local classes isn't.</p>
112 <p>So that's all what you need to know: Understand the concept of "filter classes" (a filter class combines
113 different filters and acts as if it's a filter itself) and the concept of groups (a group just describes a
114 logical correlation of filters and usually is represented to the user by drawing group separators in the filter
115 list).</p>
117 <p>If you got it, go try understanding this file :).</p>
122 typedef StringPair FilterDescriptor; // a single filter or a filter class (display name and filter mask)
123 typedef ::std::list< FilterDescriptor > FilterGroup; // a list of single filter entries
124 typedef ::std::list< FilterGroup > GroupedFilterList; // a list of all filters, already grouped
126 /// the logical name of a filter
127 typedef OUString FilterName;
129 // a struct which holds references from a logical filter name to a filter group entry
130 // used for quick lookup of classes (means class entries - entries representing a class)
131 // which a given filter may belong to
132 typedef ::std::map< OUString, FilterGroup::iterator > FilterGroupEntryReferrer;
134 namespace {
136 /// a descriptor for a filter class (which in the final dialog is represented by one filter entry)
137 struct FilterClass
139 OUString sDisplayName; // the display name
140 Sequence< FilterName > aSubFilters; // the (logical) names of the filter which belong to the class
145 typedef ::std::list< FilterClass > FilterClassList;
146 typedef ::std::map< OUString, FilterClassList::iterator > FilterClassReferrer;
149 // = reading of configuration data
152 static void lcl_ReadFilterClass( const OConfigurationNode& _rClassesNode, const OUString& _rLogicalClassName,
153 FilterClass& /* [out] */ _rClass )
155 // the description node for the current class
156 OConfigurationNode aClassDesc = _rClassesNode.openNode( _rLogicalClassName );
158 // the values
159 aClassDesc.getNodeValue( "DisplayName" ) >>= _rClass.sDisplayName;
160 aClassDesc.getNodeValue( "Filters" ) >>= _rClass.aSubFilters;
163 namespace {
165 struct CreateEmptyClassRememberPos
167 protected:
168 FilterClassList& m_rClassList;
169 FilterClassReferrer& m_rClassesReferrer;
171 public:
172 CreateEmptyClassRememberPos( FilterClassList& _rClassList, FilterClassReferrer& _rClassesReferrer )
173 :m_rClassList ( _rClassList )
174 ,m_rClassesReferrer ( _rClassesReferrer )
178 // operate on a single class name
179 void operator() ( const FilterName& _rLogicalFilterName )
181 // insert a new (empty) class
182 m_rClassList.emplace_back( );
183 // get the position of this new entry
184 FilterClassList::iterator aInsertPos = m_rClassList.end();
185 --aInsertPos;
186 // remember this position
187 m_rClassesReferrer.emplace( _rLogicalFilterName, aInsertPos );
192 struct ReadGlobalFilter
194 protected:
195 OConfigurationNode m_aClassesNode;
196 FilterClassReferrer& m_aClassReferrer;
198 public:
199 ReadGlobalFilter( const OConfigurationNode& _rClassesNode, FilterClassReferrer& _rClassesReferrer )
200 :m_aClassesNode ( _rClassesNode )
201 ,m_aClassReferrer ( _rClassesReferrer )
205 // operate on a single logical name
206 void operator() ( const FilterName& _rName )
208 FilterClassReferrer::iterator aClassRef = m_aClassReferrer.find( _rName );
209 if ( m_aClassReferrer.end() == aClassRef )
211 // we do not know this global class
212 OSL_FAIL( "ReadGlobalFilter::operator(): unknown filter name!" );
213 // TODO: perhaps we should be more tolerant - at the moment, the filter is dropped
214 // We could silently push_back it to the container...
216 else
218 // read the data of this class into the node referred to by aClassRef
219 lcl_ReadFilterClass( m_aClassesNode, _rName, *aClassRef->second );
226 static void lcl_ReadGlobalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rGlobalClasses, std::vector<OUString>& _rGlobalClassNames )
228 _rGlobalClasses.clear();
229 _rGlobalClassNames.clear();
231 // get the list describing the order of all global classes
232 Sequence< OUString > aGlobalClasses;
233 _rFilterClassification.getNodeValue( "GlobalFilters/Order" ) >>= aGlobalClasses;
235 // copy the logical names
236 comphelper::sequenceToContainer(_rGlobalClassNames, aGlobalClasses);
238 // Global classes are presented in an own group, so their order matters (while the order of the
239 // "local classes" doesn't).
240 // That's why we can't simply add the global classes to _rGlobalClasses using the order in which they
241 // are returned from the configuration - it is completely undefined, and we need a _defined_ order.
242 FilterClassReferrer aClassReferrer;
243 ::std::for_each(
244 std::cbegin(aGlobalClasses),
245 std::cend(aGlobalClasses),
246 CreateEmptyClassRememberPos( _rGlobalClasses, aClassReferrer )
248 // now _rGlobalClasses contains a dummy entry for each global class,
249 // while aClassReferrer maps from the logical name of the class to the position within _rGlobalClasses where
250 // it's dummy entry resides
253 // go for all the single class entries
254 OConfigurationNode aFilterClassesNode =
255 _rFilterClassification.openNode( "GlobalFilters/Classes" );
256 const Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
257 ::std::for_each(
258 aFilterClasses.begin(),
259 aFilterClasses.end(),
260 ReadGlobalFilter( aFilterClassesNode, aClassReferrer )
264 namespace {
266 struct ReadLocalFilter
268 protected:
269 OConfigurationNode m_aClassesNode;
270 FilterClassList& m_rClasses;
272 public:
273 ReadLocalFilter( const OConfigurationNode& _rClassesNode, FilterClassList& _rClasses )
274 :m_aClassesNode ( _rClassesNode )
275 ,m_rClasses ( _rClasses )
279 // operate on a single logical name
280 void operator() ( const FilterName& _rName )
282 // read the data for this class
283 FilterClass aClass;
284 lcl_ReadFilterClass( m_aClassesNode, _rName, aClass );
286 // insert the class descriptor
287 m_rClasses.push_back( aClass );
293 static void lcl_ReadLocalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rLocalClasses )
295 _rLocalClasses.clear();
297 // the node for the local classes
298 OConfigurationNode aFilterClassesNode =
299 _rFilterClassification.openNode( "LocalFilters/Classes" );
300 const Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
302 ::std::for_each(
303 aFilterClasses.begin(),
304 aFilterClasses.end(),
305 ReadLocalFilter( aFilterClassesNode, _rLocalClasses )
310 static void lcl_ReadClassification( FilterClassList& _rGlobalClasses, std::vector<OUString>& _rGlobalClassNames, FilterClassList& _rLocalClasses )
313 // open our config node
314 OConfigurationTreeRoot aFilterClassification = OConfigurationTreeRoot::createWithComponentContext(
315 ::comphelper::getProcessComponentContext(),
316 "org.openoffice.Office.UI/FilterClassification",
318 OConfigurationTreeRoot::CM_READONLY
322 // go for the global classes
323 lcl_ReadGlobalFilters( aFilterClassification, _rGlobalClasses, _rGlobalClassNames );
326 // go for the local classes
327 lcl_ReadLocalFilters( aFilterClassification, _rLocalClasses );
332 // = grouping and classifying
334 namespace {
336 // a struct which adds helps remembering a reference to a class entry
337 struct ReferToFilterEntry
339 protected:
340 FilterGroupEntryReferrer& m_rEntryReferrer;
341 FilterGroup::iterator m_aClassPos;
343 public:
344 ReferToFilterEntry( FilterGroupEntryReferrer& _rEntryReferrer, const FilterGroup::iterator& _rClassPos )
345 :m_rEntryReferrer( _rEntryReferrer )
346 ,m_aClassPos( _rClassPos )
350 // operate on a single filter name
351 void operator() ( const FilterName& _rName )
353 ::std::pair< FilterGroupEntryReferrer::iterator, bool > aInsertRes =
354 m_rEntryReferrer.emplace( _rName, m_aClassPos );
355 SAL_WARN_IF(
356 !aInsertRes.second, "sfx.dialog",
357 "already have an element for " << _rName);
362 struct FillClassGroup
364 protected:
365 FilterGroup& m_rClassGroup;
366 FilterGroupEntryReferrer& m_rClassReferrer;
368 public:
369 FillClassGroup( FilterGroup& _rClassGroup, FilterGroupEntryReferrer& _rClassReferrer )
370 :m_rClassGroup ( _rClassGroup )
371 ,m_rClassReferrer ( _rClassReferrer )
375 // operate on a single class
376 void operator() ( const FilterClass& _rClass )
378 // create an empty filter descriptor for the class
379 FilterDescriptor aClassEntry;
380 // set its name (which is all we know by now)
381 aClassEntry.First = _rClass.sDisplayName;
383 // add it to the group
384 m_rClassGroup.push_back( aClassEntry );
385 // the position of the newly added class
386 FilterGroup::iterator aClassEntryPos = m_rClassGroup.end();
387 --aClassEntryPos;
389 // and for all the sub filters of the class, remember the class
390 // (respectively the position of the class it the group)
391 ::std::for_each(
392 _rClass.aSubFilters.begin(),
393 _rClass.aSubFilters.end(),
394 ReferToFilterEntry( m_rClassReferrer, aClassEntryPos )
401 const sal_Unicode s_cWildcardSeparator( ';' );
403 static OUString getSeparatorString()
405 return ";";
408 namespace {
410 struct CheckAppendSingleWildcard
412 OUString& _rToBeExtended;
414 explicit CheckAppendSingleWildcard( OUString& _rBase ) : _rToBeExtended( _rBase ) { }
416 void operator() ( const OUString& _rWC )
418 // check for double wildcards
419 sal_Int32 nExistentPos = _rToBeExtended.indexOf( _rWC );
420 if ( -1 < nExistentPos )
421 { // found this wildcard (already part of _rToBeExtended)
422 if ( ( 0 == nExistentPos )
423 || ( s_cWildcardSeparator == _rToBeExtended[ nExistentPos - 1 ] )
425 { // the wildcard really starts at this position (it starts at pos 0 or the previous character is a separator
426 sal_Int32 nExistentWCEnd = nExistentPos + _rWC.getLength();
427 if ( ( _rToBeExtended.getLength() == nExistentWCEnd )
428 || ( s_cWildcardSeparator == _rToBeExtended[ nExistentWCEnd ] )
430 { // it's really the complete wildcard we found
431 // (not something like _rWC being "*.t" and _rToBeExtended containing "*.txt")
432 // -> outta here
433 return;
438 if ( !_rToBeExtended.isEmpty() )
439 _rToBeExtended += getSeparatorString();
440 _rToBeExtended += _rWC;
445 // a helper struct which adds a fixed (Sfx-)filter to a filter group entry given by iterator
446 struct AppendWildcardToDescriptor
448 protected:
449 ::std::vector< OUString > aWildCards;
451 public:
452 explicit AppendWildcardToDescriptor( const OUString& _rWildCard );
454 // operate on a single class entry
455 void operator() ( const FilterGroupEntryReferrer::value_type& _rClassReference )
457 // simply add our wildcards
458 ::std::for_each(
459 aWildCards.begin(),
460 aWildCards.end(),
461 CheckAppendSingleWildcard( _rClassReference.second->Second )
468 AppendWildcardToDescriptor::AppendWildcardToDescriptor( const OUString& _rWildCard )
470 DBG_ASSERT( !_rWildCard.isEmpty(),
471 "AppendWildcardToDescriptor::AppendWildcardToDescriptor: invalid wildcard!" );
472 DBG_ASSERT( _rWildCard.isEmpty() || _rWildCard[0] != s_cWildcardSeparator,
473 "AppendWildcardToDescriptor::AppendWildcardToDescriptor: wildcard already separated!" );
475 aWildCards.reserve( comphelper::string::getTokenCount(_rWildCard, s_cWildcardSeparator) );
477 const sal_Unicode* pTokenLoop = _rWildCard.getStr();
478 const sal_Unicode* pTokenLoopEnd = pTokenLoop + _rWildCard.getLength();
479 const sal_Unicode* pTokenStart = pTokenLoop;
480 for ( ; pTokenLoop != pTokenLoopEnd; ++pTokenLoop )
482 if ( ( s_cWildcardSeparator == *pTokenLoop ) && ( pTokenLoop > pTokenStart ) )
483 { // found a new token separator (and a non-empty token)
484 aWildCards.emplace_back( pTokenStart, pTokenLoop - pTokenStart );
486 // search the start of the next token
487 while ( ( pTokenStart != pTokenLoopEnd ) && ( *pTokenStart != s_cWildcardSeparator ) )
488 ++pTokenStart;
490 if ( pTokenStart == pTokenLoopEnd )
491 // reached the end
492 break;
494 ++pTokenStart;
495 pTokenLoop = pTokenStart;
498 if ( pTokenLoop > pTokenStart )
499 // the last one...
500 aWildCards.emplace_back( pTokenStart, pTokenLoop - pTokenStart );
504 static void lcl_InitGlobalClasses( GroupedFilterList& _rAllFilters, const FilterClassList& _rGlobalClasses, FilterGroupEntryReferrer& _rGlobalClassesRef )
506 // we need an extra group in our "all filters" container
507 _rAllFilters.push_front( FilterGroup() );
508 FilterGroup& rGlobalFilters = _rAllFilters.front();
509 // it's important to work on the reference: we want to access the members of this filter group
510 // by an iterator (FilterGroup::const_iterator)
511 // the referrer for the global classes
513 // initialize the group
514 ::std::for_each(
515 _rGlobalClasses.begin(),
516 _rGlobalClasses.end(),
517 FillClassGroup( rGlobalFilters, _rGlobalClassesRef )
519 // now we have:
520 // in rGlobalFilters: a list of FilterDescriptor's, where each's descriptor's display name is set to the name of a class
521 // in aGlobalClassesRef: a mapping from logical filter names to positions within rGlobalFilters
522 // this way, if we encounter an arbitrary filter, we can easily (and efficient) check if it belongs to a global class
523 // and modify the descriptor for this class accordingly
527 typedef ::std::vector< ::std::pair< FilterGroupEntryReferrer::mapped_type, FilterGroup::iterator > >
528 MapGroupEntry2GroupEntry;
529 // this is not really a map - it's just called this way because it is used as a map
531 namespace {
533 struct FindGroupEntry
535 FilterGroupEntryReferrer::mapped_type aLookingFor;
536 explicit FindGroupEntry( FilterGroupEntryReferrer::mapped_type const & _rLookingFor ) : aLookingFor( _rLookingFor ) { }
538 bool operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
540 return _rMapEntry.first == aLookingFor;
544 struct CopyGroupEntryContent
546 void operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
548 *_rMapEntry.second = *_rMapEntry.first;
553 struct CopyNonEmptyFilter
555 FilterGroup& rTarget;
556 explicit CopyNonEmptyFilter( FilterGroup& _rTarget ) :rTarget( _rTarget ) { }
558 void operator() ( const FilterDescriptor& _rFilter )
560 if ( !_rFilter.Second.isEmpty() )
561 rTarget.push_back( _rFilter );
567 static void lcl_GroupAndClassify( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rAllFilters )
569 _rAllFilters.clear();
572 // read the classification of filters
573 FilterClassList aGlobalClasses, aLocalClasses;
574 std::vector<OUString> aGlobalClassNames;
575 lcl_ReadClassification( aGlobalClasses, aGlobalClassNames, aLocalClasses );
578 // for the global filter classes
579 FilterGroupEntryReferrer aGlobalClassesRef;
580 lcl_InitGlobalClasses( _rAllFilters, aGlobalClasses, aGlobalClassesRef );
582 // insert as much placeholders (FilterGroup's) into _rAllFilter for groups as we have global classes
583 // (this assumes that both numbers are the same, which, speaking strictly, must not hold - but it does, as we know ...)
584 sal_Int32 nGlobalClasses = aGlobalClasses.size();
585 while ( nGlobalClasses-- )
586 _rAllFilters.emplace_back( );
589 // for the local classes:
590 // if n filters belong to a local class, they do not appear in their respective group explicitly, instead
591 // and entry for the class is added to the group and the extensions of the filters are collected under
592 // this entry
593 FilterGroupEntryReferrer aLocalClassesRef;
594 FilterGroup aCollectedLocals;
595 ::std::for_each(
596 aLocalClasses.begin(),
597 aLocalClasses.end(),
598 FillClassGroup( aCollectedLocals, aLocalClassesRef )
600 // to map from the position within aCollectedLocals to positions within the real groups
601 // (where they finally belong to)
602 MapGroupEntry2GroupEntry aLocalFinalPositions;
605 // now add the filters
606 // the group which we currently work with
607 GroupedFilterList::iterator aCurrentGroup = _rAllFilters.end(); // no current group
608 // the filter container of the current group - if this changes between two filters, a new group is reached
609 OUString aCurrentServiceName;
611 OUString sFilterWildcard;
612 OUString sFilterName;
613 // loop through all the filters
614 for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
616 sFilterName = pFilter->GetFilterName();
617 sFilterWildcard = pFilter->GetWildcard().getGlob();
618 AppendWildcardToDescriptor aExtendWildcard( sFilterWildcard );
620 DBG_ASSERT( !sFilterWildcard.isEmpty(), "sfx2::lcl_GroupAndClassify: invalid wildcard of this filter!" );
623 // check for a change in the group
624 OUString aServiceName = pFilter->GetServiceName();
625 if ( aServiceName != aCurrentServiceName )
626 { // we reached a new group
628 // look for the place in _rAllFilters where this ne group belongs - this is determined
629 // by the order of classes in aGlobalClassNames
630 GroupedFilterList::iterator aGroupPos = _rAllFilters.begin();
631 DBG_ASSERT( aGroupPos != _rAllFilters.end(),
632 "sfx2::lcl_GroupAndClassify: invalid all-filters array here!" );
633 // the loop below will work on invalid objects else ...
634 ++aGroupPos;
635 auto aGlobalIter = std::find(aGlobalClassNames.begin(), aGlobalClassNames.end(), aServiceName);
636 auto nGroupPosShift = std::min(
637 std::distance(aGlobalClassNames.begin(), aGlobalIter),
638 std::distance(aGroupPos, _rAllFilters.end()));
639 std::advance(aGroupPos, nGroupPosShift);
640 if ( aGroupPos != _rAllFilters.end() )
641 // we found a global class name which matches the doc service name -> fill the filters of this
642 // group in the respective prepared group
643 aCurrentGroup = aGroupPos;
644 else
645 // insert a new entry in our overall-list
646 aCurrentGroup = _rAllFilters.insert( _rAllFilters.end(), FilterGroup() );
648 // remember the container to properly detect the next group
649 aCurrentServiceName = aServiceName;
652 assert(aCurrentGroup != _rAllFilters.end()); //invalid current group!
653 if (aCurrentGroup == _rAllFilters.end())
654 aCurrentGroup = _rAllFilters.begin();
657 // check if the filter is part of a global group
658 ::std::pair< FilterGroupEntryReferrer::iterator, FilterGroupEntryReferrer::iterator >
659 aBelongsTo = aGlobalClassesRef.equal_range( sFilterName );
660 // add the filter to the entries for these classes
661 // (if they exist - if not, the range is empty and the for_each is a no-op)
662 ::std::for_each(
663 aBelongsTo.first,
664 aBelongsTo.second,
665 aExtendWildcard
669 // add the filter to its group
671 // for this, check if the filter is part of a local filter
672 FilterGroupEntryReferrer::iterator aBelongsToLocal = aLocalClassesRef.find( sFilterName );
673 if ( aLocalClassesRef.end() != aBelongsToLocal )
675 // okay, there is a local class which the filter belongs to
676 // -> append the wildcard
677 aExtendWildcard( *aBelongsToLocal );
679 if ( std::none_of( aLocalFinalPositions.begin(), aLocalFinalPositions.end(), FindGroupEntry( aBelongsToLocal->second ) ) )
680 { // the position within aCollectedLocals has not been mapped to a final position
681 // within the "real" group (aCollectedLocals is only temporary)
682 // -> do this now (as we just encountered the first filter belonging to this local class
683 // add a new entry which is the "real" group entry
684 aCurrentGroup->push_back( FilterDescriptor( aBelongsToLocal->second->First, OUString() ) );
685 // the position where we inserted the entry
686 FilterGroup::iterator aInsertPos = aCurrentGroup->end();
687 --aInsertPos;
688 // remember this pos
689 aLocalFinalPositions.emplace_back( aBelongsToLocal->second, aInsertPos );
692 else
693 aCurrentGroup->push_back( FilterDescriptor( pFilter->GetUIName(), sFilterWildcard ) );
696 // now just complete the infos for the local groups:
697 // During the above loop, they have been collected in aCollectedLocals, but this is only temporary
698 // They have to be copied into their final positions (which are stored in aLocalFinalPositions)
699 ::std::for_each(
700 aLocalFinalPositions.begin(),
701 aLocalFinalPositions.end(),
702 CopyGroupEntryContent()
705 // and remove local groups which do not apply - e.g. have no entries due to the limited content of the
706 // current SfxFilterMatcherIter
708 FilterGroup& rGlobalFilters = _rAllFilters.front();
709 FilterGroup aNonEmptyGlobalFilters;
710 ::std::for_each(
711 rGlobalFilters.begin(),
712 rGlobalFilters.end(),
713 CopyNonEmptyFilter( aNonEmptyGlobalFilters )
715 rGlobalFilters.swap( aNonEmptyGlobalFilters );
718 namespace {
720 struct AppendFilter
722 protected:
723 Reference< XFilterManager > m_xFilterManager;
724 FileDialogHelper_Impl* m_pFileDlgImpl;
725 bool m_bAddExtension;
727 public:
728 AppendFilter( const Reference< XFilterManager >& _rxFilterManager,
729 FileDialogHelper_Impl* _pImpl, bool _bAddExtension ) :
731 m_xFilterManager( _rxFilterManager ),
732 m_pFileDlgImpl ( _pImpl ),
733 m_bAddExtension ( _bAddExtension )
736 DBG_ASSERT( m_xFilterManager.is(), "AppendFilter::AppendFilter: invalid filter manager!" );
737 DBG_ASSERT( m_pFileDlgImpl, "AppendFilter::AppendFilter: invalid filedlg impl!" );
740 // operate on a single filter
741 void operator() ( const FilterDescriptor& _rFilterEntry )
743 OUString sDisplayText = m_bAddExtension
744 ? addExtension( _rFilterEntry.First, _rFilterEntry.Second, true, *m_pFileDlgImpl )
745 : _rFilterEntry.First;
746 m_xFilterManager->appendFilter( sDisplayText, _rFilterEntry.Second );
752 // = handling for the "all files" entry
755 static bool lcl_hasAllFilesFilter( TSortedFilterList& _rFilterMatcher, OUString& /* [out] */ _rAllFilterName )
757 bool bHasAll = false;
758 _rAllFilterName = SfxResId( STR_SFX_FILTERNAME_ALL );
761 // check if there's already a filter <ALL>
762 for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter && !bHasAll; pFilter = _rFilterMatcher.Next() )
764 if ( pFilter->GetUIName() == _rAllFilterName )
765 bHasAll = true;
767 return bHasAll;
771 static void lcl_EnsureAllFilesEntry( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rFilters )
774 OUString sAllFilterName;
775 if ( !lcl_hasAllFilesFilter( _rFilterMatcher, sAllFilterName ) )
777 // get the first group of filters (by definition, this group contains the global classes)
778 DBG_ASSERT( !_rFilters.empty(), "lcl_EnsureAllFilesEntry: invalid filter list!" );
779 if ( !_rFilters.empty() )
781 FilterGroup& rGlobalClasses = *_rFilters.begin();
782 rGlobalClasses.push_front( FilterDescriptor( sAllFilterName, FILEDIALOG_FILTER_ALL ) );
788 // = filling an XFilterManager
790 namespace {
792 struct AppendFilterGroup
794 protected:
795 Reference< XFilterManager > m_xFilterManager;
796 Reference< XFilterGroupManager > m_xFilterGroupManager;
797 FileDialogHelper_Impl* m_pFileDlgImpl;
799 public:
800 AppendFilterGroup( const Reference< XFilterManager >& _rxFilterManager, FileDialogHelper_Impl* _pImpl )
801 :m_xFilterManager ( _rxFilterManager )
802 ,m_xFilterGroupManager ( _rxFilterManager, UNO_QUERY )
803 ,m_pFileDlgImpl ( _pImpl )
805 DBG_ASSERT( m_xFilterManager.is(), "AppendFilterGroup::AppendFilterGroup: invalid filter manager!" );
806 DBG_ASSERT( m_pFileDlgImpl, "AppendFilterGroup::AppendFilterGroup: invalid filedlg impl!" );
809 void appendGroup( const FilterGroup& _rGroup, bool _bAddExtension )
813 if ( m_xFilterGroupManager.is() )
814 { // the file dialog implementation supports visual grouping of filters
815 // create a representation of the group which is understandable by the XFilterGroupManager
816 if ( !_rGroup.empty() )
818 Sequence< StringPair > aFilters( comphelper::containerToSequence(_rGroup) );
819 if ( _bAddExtension )
821 for ( StringPair & filter : asNonConstRange(aFilters) )
822 filter.First = addExtension( filter.First, filter.Second, true, *m_pFileDlgImpl );
824 m_xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
827 else
829 ::std::for_each(
830 _rGroup.begin(),
831 _rGroup.end(),
832 AppendFilter( m_xFilterManager, m_pFileDlgImpl, _bAddExtension ) );
835 catch( const Exception& )
837 DBG_UNHANDLED_EXCEPTION("sfx.dialog");
841 // operate on a single filter group
842 void operator() ( const FilterGroup& _rGroup )
844 appendGroup( _rGroup, true );
850 TSortedFilterList::TSortedFilterList(const css::uno::Reference< css::container::XEnumeration >& xFilterList)
851 : m_nIterator(0)
853 if (!xFilterList.is())
854 return;
856 m_lFilters.clear();
857 while(xFilterList->hasMoreElements())
859 ::comphelper::SequenceAsHashMap lFilterProps (xFilterList->nextElement());
860 OUString sFilterName = lFilterProps.getUnpackedValueOrDefault(
861 "Name",
862 OUString());
863 if (!sFilterName.isEmpty())
864 m_lFilters.push_back(sFilterName);
869 std::shared_ptr<const SfxFilter> TSortedFilterList::First()
871 m_nIterator = 0;
872 return impl_getFilter(m_nIterator);
876 std::shared_ptr<const SfxFilter> TSortedFilterList::Next()
878 ++m_nIterator;
879 return impl_getFilter(m_nIterator);
883 std::shared_ptr<const SfxFilter> TSortedFilterList::impl_getFilter(sal_Int32 nIndex)
885 if (nIndex<0 || nIndex>=static_cast<sal_Int32>(m_lFilters.size()))
886 return nullptr;
887 const OUString& sFilterName = m_lFilters[nIndex];
888 if (sFilterName.isEmpty())
889 return nullptr;
890 return SfxFilter::GetFilterByName(sFilterName);
894 void appendFiltersForSave( TSortedFilterList& _rFilterMatcher,
895 const Reference< XFilterManager >& _rxFilterManager,
896 OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl,
897 const OUString& _rFactory )
899 DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForSave: invalid manager!" );
900 if ( !_rxFilterManager.is() )
901 return;
903 OUString sUIName;
904 OUString sExtension;
906 // retrieve the default filter for this application module.
907 // It must be set as first of the generated filter list.
908 std::shared_ptr<const SfxFilter> pDefaultFilter = SfxFilterContainer::GetDefaultFilter_Impl(_rFactory);
909 // Only use one extension (#i32434#)
910 // (and always the first if there are more than one)
911 sExtension = pDefaultFilter->GetWildcard().getGlob().getToken(0, ';');
912 sUIName = addExtension( pDefaultFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
915 _rxFilterManager->appendFilter( sUIName, sExtension );
916 if ( _rFirstNonEmpty.isEmpty() )
917 _rFirstNonEmpty = sUIName;
919 catch( const IllegalArgumentException& )
921 SAL_WARN( "sfx.dialog", "Could not append DefaultFilter" << sUIName );
924 for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
926 if (pFilter->GetName() == pDefaultFilter->GetName())
927 continue;
929 // Only use one extension (#i32434#)
930 // (and always the first if there are more than one)
931 sExtension = pFilter->GetWildcard().getGlob().getToken(0, ';');
932 sUIName = addExtension( pFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
935 _rxFilterManager->appendFilter( sUIName, sExtension );
936 if ( _rFirstNonEmpty.isEmpty() )
937 _rFirstNonEmpty = sUIName;
939 catch( const IllegalArgumentException& )
941 SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
946 namespace {
948 struct ExportFilter
950 ExportFilter( const OUString& _aUIName, const OUString& _aWildcard ) :
951 aUIName( _aUIName ), aWildcard( _aWildcard ) {}
953 OUString aUIName;
954 OUString aWildcard;
959 void appendExportFilters( TSortedFilterList& _rFilterMatcher,
960 const Reference< XFilterManager >& _rxFilterManager,
961 OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
963 DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendExportFilters: invalid manager!" );
964 if ( !_rxFilterManager.is() )
965 return;
967 sal_Int32 nHTMLIndex = -1;
968 sal_Int32 nXHTMLIndex = -1;
969 sal_Int32 nPDFIndex = -1;
970 OUString sUIName;
971 OUString sExtensions;
972 std::vector< ExportFilter > aImportantFilterGroup;
973 std::vector< ExportFilter > aFilterGroup;
974 Reference< XFilterGroupManager > xFilterGroupManager( _rxFilterManager, UNO_QUERY );
975 OUString sTypeName;
977 for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
979 sTypeName = pFilter->GetTypeName();
980 sUIName = pFilter->GetUIName();
981 sExtensions = pFilter->GetWildcard().getGlob();
982 ExportFilter aExportFilter( sUIName, sExtensions );
984 if ( nHTMLIndex == -1 &&
985 ( sTypeName == "generic_HTML" || sTypeName == "graphic_HTML" ) )
987 aImportantFilterGroup.insert( aImportantFilterGroup.begin(), aExportFilter );
988 nHTMLIndex = 0;
990 else if ( nXHTMLIndex == -1 && sTypeName == "XHTML_File" )
992 std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
993 if ( nHTMLIndex == -1 )
994 aImportantFilterGroup.insert( aIter, aExportFilter );
995 else
996 aImportantFilterGroup.insert( ++aIter, aExportFilter );
997 nXHTMLIndex = 0;
999 else if ( nPDFIndex == -1 && sTypeName == "pdf_Portable_Document_Format" )
1001 std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
1002 if ( nHTMLIndex != -1 )
1003 ++aIter;
1004 if ( nXHTMLIndex != -1 )
1005 ++aIter;
1006 aImportantFilterGroup.insert( aIter, aExportFilter );
1007 nPDFIndex = 0;
1009 else
1010 aFilterGroup.push_back( aExportFilter );
1013 if ( xFilterGroupManager.is() )
1015 // Add both html/pdf filter as a filter group to get a separator between both groups
1016 if ( !aImportantFilterGroup.empty() )
1018 Sequence< StringPair > aFilters( aImportantFilterGroup.size() );
1019 auto pFilters = aFilters.getArray();
1020 for ( sal_Int32 i = 0; i < static_cast<sal_Int32>(aImportantFilterGroup.size()); i++ )
1022 pFilters[i].First = addExtension( aImportantFilterGroup[i].aUIName,
1023 aImportantFilterGroup[i].aWildcard,
1024 false, _rFileDlgImpl );
1025 pFilters[i].Second = aImportantFilterGroup[i].aWildcard;
1030 xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
1032 catch( const IllegalArgumentException& )
1037 if ( !aFilterGroup.empty() )
1039 Sequence< StringPair > aFilters( aFilterGroup.size() );
1040 auto pFilters = aFilters.getArray();
1041 for ( sal_Int32 i = 0; i < static_cast<sal_Int32>(aFilterGroup.size()); i++ )
1043 pFilters[i].First = addExtension( aFilterGroup[i].aUIName,
1044 aFilterGroup[i].aWildcard,
1045 false, _rFileDlgImpl );
1046 pFilters[i].Second = aFilterGroup[i].aWildcard;
1051 xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
1053 catch( const IllegalArgumentException& )
1058 else
1060 // Fallback solution just add both filter groups as single filters
1061 sal_Int32 n;
1063 for ( n = 0; n < static_cast<sal_Int32>(aImportantFilterGroup.size()); n++ )
1067 OUString aUIName = addExtension( aImportantFilterGroup[n].aUIName,
1068 aImportantFilterGroup[n].aWildcard,
1069 false, _rFileDlgImpl );
1070 _rxFilterManager->appendFilter( aUIName, aImportantFilterGroup[n].aWildcard );
1071 if ( _rFirstNonEmpty.isEmpty() )
1072 _rFirstNonEmpty = sUIName;
1075 catch( const IllegalArgumentException& )
1077 SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
1081 for ( n = 0; n < static_cast<sal_Int32>(aFilterGroup.size()); n++ )
1085 OUString aUIName = addExtension( aFilterGroup[n].aUIName,
1086 aFilterGroup[n].aWildcard,
1087 false, _rFileDlgImpl );
1088 _rxFilterManager->appendFilter( aUIName, aFilterGroup[n].aWildcard );
1089 if ( _rFirstNonEmpty.isEmpty() )
1090 _rFirstNonEmpty = sUIName;
1093 catch( const IllegalArgumentException& )
1095 SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
1102 void appendFiltersForOpen( TSortedFilterList& _rFilterMatcher,
1103 const Reference< XFilterManager >& _rxFilterManager,
1104 OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
1106 DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForOpen: invalid manager!" );
1107 if ( !_rxFilterManager.is() )
1108 return;
1111 // group and classify the filters
1112 GroupedFilterList aAllFilters;
1113 lcl_GroupAndClassify( _rFilterMatcher, aAllFilters );
1116 // ensure that we have the one "all files" entry
1117 lcl_EnsureAllFilesEntry( _rFilterMatcher, aAllFilters );
1120 // the first non-empty string - which we assume is the first overall entry
1121 if ( !aAllFilters.empty() )
1123 const FilterGroup& rFirstGroup = *aAllFilters.begin(); // should be the global classes
1124 if ( !rFirstGroup.empty() )
1125 _rFirstNonEmpty = rFirstGroup.begin()->First;
1126 // append first group, without extension
1127 AppendFilterGroup aGroup( _rxFilterManager, &_rFileDlgImpl );
1128 aGroup.appendGroup( rFirstGroup, false );
1132 // append the filters to the manager
1133 if ( !aAllFilters.empty() )
1135 ::std::list< FilterGroup >::iterator pIter = aAllFilters.begin();
1136 ++pIter;
1137 ::std::for_each(
1138 pIter, // first filter group was handled separately, see above
1139 aAllFilters.end(),
1140 AppendFilterGroup( _rxFilterManager, &_rFileDlgImpl ) );
1144 OUString addExtension( const OUString& _rDisplayText,
1145 const OUString& _rExtension,
1146 bool _bForOpen, FileDialogHelper_Impl& _rFileDlgImpl )
1148 OUString sRet = _rDisplayText;
1150 if ( sRet.indexOf( "(*.*)" ) == -1 )
1152 OUString sExt = _rExtension;
1153 if ( !_bForOpen )
1155 // show '*' in extensions only when opening a document
1156 sExt = sExt.replaceAll("*", "");
1158 sRet += " (" + sExt + ")";
1160 _rFileDlgImpl.addFilterPair( _rDisplayText, sRet );
1161 return sRet;
1165 } // namespace sfx2
1168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */