Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "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>
36 :
37 : #include <list>
38 : #include <vector>
39 : #include <map>
40 : #include <algorithm>
41 :
42 :
43 : namespace sfx2
44 : {
45 :
46 :
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;
52 :
53 :
54 : /**
55 :
56 : Some general words about what's going on here ....
57 :
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>
60 :
61 : <p>But then the PM came. And everything changed ....</p>
62 :
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>
67 :
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>
70 :
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>
73 :
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>
80 :
81 : <p>Such an entry as "Text documents" is - within the scope of this file - called "class" or "filter class".</p>
82 :
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>
86 :
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>
92 :
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>
96 :
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>
99 :
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>
103 :
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>
107 :
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>
110 :
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>
115 :
116 : <p>If you got it, go try understanding this file :).</p>
117 :
118 : */
119 :
120 :
121 :
122 :
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
126 :
127 : /// the logical name of a filter
128 : typedef OUString FilterName;
129 :
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;
134 :
135 : /// a descriptor for a filter class (which in the final dialog is represented by one filter entry)
136 0 : typedef struct _tagFilterClass
137 : {
138 : OUString sDisplayName; // the display name
139 : Sequence< FilterName > aSubFilters; // the (logical) names of the filter which belong to the class
140 : } FilterClass;
141 :
142 : typedef ::std::list< FilterClass > FilterClassList;
143 : typedef ::std::map< OUString, FilterClassList::iterator > FilterClassReferrer;
144 :
145 : typedef ::std::vector< OUString > StringArray;
146 :
147 :
148 : // = reading of configuration data
149 :
150 :
151 :
152 0 : void lcl_ReadFilterClass( const OConfigurationNode& _rClassesNode, const OUString& _rLogicalClassName,
153 : FilterClass& /* [out] */ _rClass )
154 : {
155 : static const char sDisplaNameNodeName[] = "DisplayName";
156 : static const char sSubFiltersNodeName[] = "Filters";
157 :
158 : // the description node for the current class
159 0 : OConfigurationNode aClassDesc = _rClassesNode.openNode( _rLogicalClassName );
160 :
161 : // the values
162 0 : aClassDesc.getNodeValue( sDisplaNameNodeName ) >>= _rClass.sDisplayName;
163 0 : aClassDesc.getNodeValue( sSubFiltersNodeName ) >>= _rClass.aSubFilters;
164 0 : }
165 :
166 :
167 : struct CreateEmptyClassRememberPos : public ::std::unary_function< FilterName, void >
168 : {
169 : protected:
170 : FilterClassList& m_rClassList;
171 : FilterClassReferrer& m_rClassesReferrer;
172 :
173 : public:
174 0 : CreateEmptyClassRememberPos( FilterClassList& _rClassList, FilterClassReferrer& _rClassesReferrer )
175 : :m_rClassList ( _rClassList )
176 0 : ,m_rClassesReferrer ( _rClassesReferrer )
177 : {
178 0 : }
179 :
180 : // operate on a single class name
181 0 : void operator() ( const FilterName& _rLogicalFilterName )
182 : {
183 : // insert a new (empty) class
184 0 : m_rClassList.push_back( FilterClass() );
185 : // get the position of this new entry
186 0 : FilterClassList::iterator aInsertPos = m_rClassList.end();
187 0 : --aInsertPos;
188 : // remember this position
189 0 : m_rClassesReferrer.insert( FilterClassReferrer::value_type( _rLogicalFilterName, aInsertPos ) );
190 0 : }
191 : };
192 :
193 :
194 0 : struct ReadGlobalFilter : public ::std::unary_function< FilterName, void >
195 : {
196 : protected:
197 : OConfigurationNode m_aClassesNode;
198 : FilterClassReferrer& m_aClassReferrer;
199 :
200 : public:
201 0 : ReadGlobalFilter( const OConfigurationNode& _rClassesNode, FilterClassReferrer& _rClassesReferrer )
202 : :m_aClassesNode ( _rClassesNode )
203 0 : ,m_aClassReferrer ( _rClassesReferrer )
204 : {
205 0 : }
206 :
207 : // operate on a single logical name
208 0 : void operator() ( const FilterName& _rName )
209 : {
210 0 : FilterClassReferrer::iterator aClassRef = m_aClassReferrer.find( _rName );
211 0 : if ( m_aClassReferrer.end() == aClassRef )
212 : {
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 ....
217 : }
218 : else
219 : {
220 : // read the data of this class into the node referred to by aClassRef
221 0 : lcl_ReadFilterClass( m_aClassesNode, _rName, *aClassRef->second );
222 : }
223 0 : }
224 : };
225 :
226 :
227 0 : void lcl_ReadGlobalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rGlobalClasses, StringArray& _rGlobalClassNames )
228 : {
229 0 : _rGlobalClasses.clear();
230 0 : _rGlobalClassNames.clear();
231 :
232 :
233 : // get the list describing the order of all global classes
234 0 : Sequence< OUString > aGlobalClasses;
235 0 : _rFilterClassification.getNodeValue( "GlobalFilters/Order" ) >>= aGlobalClasses;
236 :
237 0 : const OUString* pNames = aGlobalClasses.getConstArray();
238 0 : const OUString* pNamesEnd = pNames + aGlobalClasses.getLength();
239 :
240 : // copy the logical names
241 0 : _rGlobalClassNames.resize( aGlobalClasses.getLength() );
242 0 : ::std::copy( pNames, pNamesEnd, _rGlobalClassNames.begin() );
243 :
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 0 : FilterClassReferrer aClassReferrer;
249 : ::std::for_each(
250 : pNames,
251 : pNamesEnd,
252 : CreateEmptyClassRememberPos( _rGlobalClasses, aClassReferrer )
253 0 : );
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
257 :
258 :
259 : // go for all the single class entries
260 : OConfigurationNode aFilterClassesNode =
261 0 : _rFilterClassification.openNode( "GlobalFilters/Classes" );
262 0 : Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
263 : ::std::for_each(
264 : aFilterClasses.getConstArray(),
265 0 : aFilterClasses.getConstArray() + aFilterClasses.getLength(),
266 : ReadGlobalFilter( aFilterClassesNode, aClassReferrer )
267 0 : );
268 0 : }
269 :
270 :
271 0 : struct ReadLocalFilter : public ::std::unary_function< FilterName, void >
272 : {
273 : protected:
274 : OConfigurationNode m_aClassesNode;
275 : FilterClassList& m_rClasses;
276 :
277 : public:
278 0 : ReadLocalFilter( const OConfigurationNode& _rClassesNode, FilterClassList& _rClasses )
279 : :m_aClassesNode ( _rClassesNode )
280 0 : ,m_rClasses ( _rClasses )
281 : {
282 0 : }
283 :
284 : // operate on a single logical name
285 0 : void operator() ( const FilterName& _rName )
286 : {
287 : // read the data for this class
288 0 : FilterClass aClass;
289 0 : lcl_ReadFilterClass( m_aClassesNode, _rName, aClass );
290 :
291 : // insert the class descriptor
292 0 : m_rClasses.push_back( aClass );
293 0 : }
294 : };
295 :
296 :
297 0 : void lcl_ReadLocalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rLocalClasses )
298 : {
299 0 : _rLocalClasses.clear();
300 :
301 : // the node for the local classes
302 : OConfigurationNode aFilterClassesNode =
303 0 : _rFilterClassification.openNode( "LocalFilters/Classes" );
304 0 : Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
305 :
306 : ::std::for_each(
307 : aFilterClasses.getConstArray(),
308 0 : aFilterClasses.getConstArray() + aFilterClasses.getLength(),
309 : ReadLocalFilter( aFilterClassesNode, _rLocalClasses )
310 0 : );
311 0 : }
312 :
313 :
314 0 : void lcl_ReadClassification( FilterClassList& _rGlobalClasses, StringArray& _rGlobalClassNames, FilterClassList& _rLocalClasses )
315 : {
316 :
317 : // open our config node
318 : OConfigurationTreeRoot aFilterClassification = OConfigurationTreeRoot::createWithComponentContext(
319 : ::comphelper::getProcessComponentContext(),
320 : "org.openoffice.Office.UI/FilterClassification",
321 : -1,
322 : OConfigurationTreeRoot::CM_READONLY
323 0 : );
324 :
325 :
326 : // go for the global classes
327 0 : lcl_ReadGlobalFilters( aFilterClassification, _rGlobalClasses, _rGlobalClassNames );
328 :
329 :
330 : // fo for the local classes
331 0 : lcl_ReadLocalFilters( aFilterClassification, _rLocalClasses );
332 :
333 0 : }
334 :
335 :
336 : // = grouping and classifying
337 :
338 :
339 :
340 : // a struct which adds helps remembering a reference to a class entry
341 : struct ReferToFilterEntry : public ::std::unary_function< FilterName, void >
342 : {
343 : protected:
344 : FilterGroupEntryReferrer& m_rEntryReferrer;
345 : FilterGroup::iterator m_aClassPos;
346 :
347 : public:
348 0 : ReferToFilterEntry( FilterGroupEntryReferrer& _rEntryReferrer, const FilterGroup::iterator& _rClassPos )
349 : :m_rEntryReferrer( _rEntryReferrer )
350 0 : ,m_aClassPos( _rClassPos )
351 : {
352 0 : }
353 :
354 : // operate on a single filter name
355 0 : void operator() ( const FilterName& _rName )
356 : {
357 : ::std::pair< FilterGroupEntryReferrer::iterator, bool > aInsertRes =
358 0 : 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);
362 0 : }
363 : };
364 :
365 :
366 : struct FillClassGroup : public ::std::unary_function< FilterClass, void >
367 : {
368 : protected:
369 : FilterGroup& m_rClassGroup;
370 : FilterGroupEntryReferrer& m_rClassReferrer;
371 :
372 : public:
373 0 : FillClassGroup( FilterGroup& _rClassGroup, FilterGroupEntryReferrer& _rClassReferrer )
374 : :m_rClassGroup ( _rClassGroup )
375 0 : ,m_rClassReferrer ( _rClassReferrer )
376 : {
377 0 : }
378 :
379 : // operate on a single class
380 0 : void operator() ( const FilterClass& _rClass )
381 : {
382 : // create an empty filter descriptor for the class
383 0 : FilterDescriptor aClassEntry;
384 : // set its name (which is all we know by now)
385 0 : aClassEntry.First = _rClass.sDisplayName;
386 :
387 : // add it to the group
388 0 : m_rClassGroup.push_back( aClassEntry );
389 : // the position of the newly added class
390 0 : FilterGroup::iterator aClassEntryPos = m_rClassGroup.end();
391 0 : --aClassEntryPos;
392 :
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 0 : _rClass.aSubFilters.getConstArray() + _rClass.aSubFilters.getLength(),
398 : ReferToFilterEntry( m_rClassReferrer, aClassEntryPos )
399 0 : );
400 0 : }
401 : };
402 :
403 :
404 : static const sal_Unicode s_cWildcardSeparator( ';' );
405 :
406 0 : static OUString getSeparatorString()
407 : {
408 0 : return OUString(";");
409 : }
410 :
411 :
412 : struct CheckAppendSingleWildcard : public ::std::unary_function< OUString, void >
413 : {
414 : OUString& _rToBeExtended;
415 :
416 0 : CheckAppendSingleWildcard( OUString& _rBase ) : _rToBeExtended( _rBase ) { }
417 :
418 0 : void operator() ( const OUString& _rWC )
419 : {
420 : // check for double wildcards
421 0 : sal_Int32 nExistentPos = _rToBeExtended.indexOf( _rWC );
422 0 : if ( -1 < nExistentPos )
423 : { // found this wildcard (already part of _rToBeExtended)
424 0 : const sal_Unicode* pBuffer = _rToBeExtended.getStr();
425 0 : if ( ( 0 == nExistentPos )
426 0 : || ( s_cWildcardSeparator == pBuffer[ nExistentPos - 1 ] )
427 : )
428 : { // the wildcard really starts at this position (it starts at pos 0 or the previous character is a separator
429 0 : sal_Int32 nExistentWCEnd = nExistentPos + _rWC.getLength();
430 0 : if ( ( _rToBeExtended.getLength() == nExistentWCEnd )
431 0 : || ( s_cWildcardSeparator == pBuffer[ nExistentWCEnd ] )
432 : )
433 : { // it's really the complete wildcard we found
434 : // (not something like _rWC being "*.t" and _rToBeExtended containing "*.txt")
435 : // -> outta here
436 0 : return;
437 : }
438 : }
439 : }
440 :
441 0 : if ( !_rToBeExtended.isEmpty() )
442 0 : _rToBeExtended += getSeparatorString();
443 0 : _rToBeExtended += _rWC;
444 : }
445 : };
446 :
447 :
448 : // a helper struct which adds a fixed (Sfx-)filter to a filter group entry given by iterator
449 0 : struct AppendWildcardToDescriptor : public ::std::unary_function< FilterGroupEntryReferrer::value_type, void >
450 : {
451 : protected:
452 : ::std::vector< OUString > aWildCards;
453 :
454 : public:
455 : AppendWildcardToDescriptor( const OUString& _rWildCard );
456 :
457 : // operate on a single class entry
458 0 : void operator() ( const FilterGroupEntryReferrer::value_type& _rClassReference )
459 : {
460 : // simply add our wildcards
461 : ::std::for_each(
462 : aWildCards.begin(),
463 : aWildCards.end(),
464 0 : CheckAppendSingleWildcard( _rClassReference.second->Second )
465 0 : );
466 0 : }
467 : };
468 :
469 :
470 0 : AppendWildcardToDescriptor::AppendWildcardToDescriptor( const OUString& _rWildCard )
471 : {
472 : DBG_ASSERT( !_rWildCard.isEmpty(),
473 : "AppendWildcardToDescriptor::AppendWildcardToDescriptor: invalid wildcard!" );
474 : DBG_ASSERT( _rWildCard[0] != s_cWildcardSeparator,
475 : "AppendWildcardToDescriptor::AppendWildcardToDescriptor: wildcard already separated!" );
476 :
477 0 : aWildCards.reserve( comphelper::string::getTokenCount(_rWildCard, s_cWildcardSeparator) );
478 :
479 0 : const sal_Unicode* pTokenLoop = _rWildCard.getStr();
480 0 : const sal_Unicode* pTokenLoopEnd = pTokenLoop + _rWildCard.getLength();
481 0 : const sal_Unicode* pTokenStart = pTokenLoop;
482 0 : for ( ; pTokenLoop != pTokenLoopEnd; ++pTokenLoop )
483 : {
484 0 : if ( ( s_cWildcardSeparator == *pTokenLoop ) && ( pTokenLoop > pTokenStart ) )
485 : { // found a new token separator (and a non-empty token)
486 0 : aWildCards.push_back( OUString( pTokenStart, pTokenLoop - pTokenStart ) );
487 :
488 : // search the start of the next token
489 0 : while ( ( pTokenStart != pTokenLoopEnd ) && ( *pTokenStart != s_cWildcardSeparator ) )
490 0 : ++pTokenStart;
491 :
492 0 : if ( pTokenStart == pTokenLoopEnd )
493 : // reached the end
494 0 : break;
495 :
496 0 : ++pTokenStart;
497 0 : pTokenLoop = pTokenStart;
498 : }
499 : }
500 0 : if ( pTokenLoop > pTokenStart )
501 : // the last one ....
502 0 : aWildCards.push_back( OUString( pTokenStart, pTokenLoop - pTokenStart ) );
503 0 : }
504 :
505 :
506 0 : void lcl_InitGlobalClasses( GroupedFilterList& _rAllFilters, const FilterClassList& _rGlobalClasses, FilterGroupEntryReferrer& _rGlobalClassesRef )
507 : {
508 : // we need an extra group in our "all filters" container
509 0 : _rAllFilters.push_front( FilterGroup() );
510 0 : 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
514 :
515 : // initialize the group
516 : ::std::for_each(
517 : _rGlobalClasses.begin(),
518 : _rGlobalClasses.end(),
519 : FillClassGroup( rGlobalFilters, _rGlobalClassesRef )
520 0 : );
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
526 0 : }
527 :
528 :
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
532 :
533 : struct FindGroupEntry : public ::std::unary_function< MapGroupEntry2GroupEntry::value_type, sal_Bool >
534 : {
535 : FilterGroupEntryReferrer::mapped_type aLookingFor;
536 0 : FindGroupEntry( FilterGroupEntryReferrer::mapped_type _rLookingFor ) : aLookingFor( _rLookingFor ) { }
537 :
538 0 : bool operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
539 : {
540 0 : return _rMapEntry.first == aLookingFor;
541 : }
542 : };
543 :
544 : struct CopyGroupEntryContent : public ::std::unary_function< MapGroupEntry2GroupEntry::value_type, void >
545 : {
546 0 : void operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
547 : {
548 : #ifdef DBG_UTIL
549 : FilterDescriptor aHaveALook = *_rMapEntry.first;
550 : #endif
551 0 : *_rMapEntry.second = *_rMapEntry.first;
552 0 : }
553 : };
554 :
555 :
556 : struct CopyNonEmptyFilter : public ::std::unary_function< FilterDescriptor, void >
557 : {
558 : FilterGroup& rTarget;
559 0 : CopyNonEmptyFilter( FilterGroup& _rTarget ) :rTarget( _rTarget ) { }
560 :
561 0 : void operator() ( const FilterDescriptor& _rFilter )
562 : {
563 0 : if ( !_rFilter.Second.isEmpty() )
564 0 : rTarget.push_back( _rFilter );
565 0 : }
566 : };
567 :
568 :
569 0 : void lcl_GroupAndClassify( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rAllFilters )
570 : {
571 0 : _rAllFilters.clear();
572 :
573 :
574 : // read the classification of filters
575 0 : FilterClassList aGlobalClasses, aLocalClasses;
576 0 : StringArray aGlobalClassNames;
577 0 : lcl_ReadClassification( aGlobalClasses, aGlobalClassNames, aLocalClasses );
578 :
579 :
580 : // for the global filter classes
581 0 : FilterGroupEntryReferrer aGlobalClassesRef;
582 0 : lcl_InitGlobalClasses( _rAllFilters, aGlobalClasses, aGlobalClassesRef );
583 :
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 0 : sal_Int32 nGlobalClasses = aGlobalClasses.size();
587 0 : while ( nGlobalClasses-- )
588 0 : _rAllFilters.push_back( FilterGroup() );
589 :
590 :
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 0 : FilterGroupEntryReferrer aLocalClassesRef;
596 0 : FilterGroup aCollectedLocals;
597 : ::std::for_each(
598 : aLocalClasses.begin(),
599 : aLocalClasses.end(),
600 : FillClassGroup( aCollectedLocals, aLocalClassesRef )
601 0 : );
602 : // to map from the position within aCollectedLocals to positions within the real groups
603 : // (where they finally belong to)
604 0 : MapGroupEntry2GroupEntry aLocalFinalPositions;
605 :
606 :
607 : // now add the filters
608 : // the group which we currently work with
609 0 : 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 0 : OUString aCurrentServiceName;
612 :
613 0 : OUString sFilterWildcard;
614 0 : OUString sFilterName;
615 : // loop through all the filters
616 0 : for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
617 : {
618 0 : sFilterName = pFilter->GetFilterName();
619 0 : sFilterWildcard = pFilter->GetWildcard().getGlob();
620 0 : AppendWildcardToDescriptor aExtendWildcard( sFilterWildcard );
621 :
622 : DBG_ASSERT( !sFilterWildcard.isEmpty(), "sfx2::lcl_GroupAndClassify: invalid wildcard of this filter!" );
623 :
624 :
625 : // check for a change in the group
626 0 : OUString aServiceName = pFilter->GetServiceName();
627 0 : if ( aServiceName != aCurrentServiceName )
628 : { // we reached a new group
629 :
630 0 : OUString sDocServName = aServiceName;
631 :
632 : // look for the place in _rAllFilters where this ne group belongs - this is determined
633 : // by the order of classes in aGlobalClassNames
634 0 : 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 0 : ++aGroupPos;
639 0 : StringArray::iterator aGlobalIter = aGlobalClassNames.begin();
640 0 : while ( ( aGroupPos != _rAllFilters.end() )
641 0 : && ( aGlobalIter != aGlobalClassNames.end() )
642 0 : && ( *aGlobalIter != sDocServName )
643 : )
644 : {
645 0 : ++aGlobalIter;
646 0 : ++aGroupPos;
647 : }
648 0 : 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 0 : aCurrentGroup = aGroupPos;
652 : else
653 : // insert a new entry in our overall-list
654 0 : aCurrentGroup = _rAllFilters.insert( _rAllFilters.end(), FilterGroup() );
655 :
656 : // remember the container to properly detect the next group
657 0 : aCurrentServiceName = aServiceName;
658 : }
659 :
660 : assert(aCurrentGroup != _rAllFilters.end()); //invalid current group!
661 0 : if (aCurrentGroup == _rAllFilters.end())
662 0 : aCurrentGroup = _rAllFilters.begin();
663 :
664 :
665 : // check if the filter is part of a global group
666 : ::std::pair< FilterGroupEntryReferrer::iterator, FilterGroupEntryReferrer::iterator >
667 0 : 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
674 0 : );
675 :
676 :
677 : // add the filter to it's group
678 :
679 : // for this, check if the filter is part of a local filter
680 0 : FilterGroupEntryReferrer::iterator aBelongsToLocal = aLocalClassesRef.find( sFilterName );
681 0 : if ( aLocalClassesRef.end() != aBelongsToLocal )
682 : {
683 : // okay, there is a local class which the filter belongs to
684 : // -> append the wildcard
685 0 : aExtendWildcard( *aBelongsToLocal );
686 :
687 : MapGroupEntry2GroupEntry::iterator aThisGroupFinalPos =
688 0 : ::std::find_if( aLocalFinalPositions.begin(), aLocalFinalPositions.end(), FindGroupEntry( aBelongsToLocal->second ) );
689 :
690 0 : 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 0 : aCurrentGroup->push_back( FilterDescriptor( aBelongsToLocal->second->First, OUString() ) );
696 : // the position where we inserted the entry
697 0 : FilterGroup::iterator aInsertPos = aCurrentGroup->end();
698 0 : --aInsertPos;
699 : // remember this pos
700 0 : aLocalFinalPositions.push_back( MapGroupEntry2GroupEntry::value_type( aBelongsToLocal->second, aInsertPos ) );
701 : }
702 : }
703 : else
704 0 : aCurrentGroup->push_back( FilterDescriptor( pFilter->GetUIName(), sFilterWildcard ) );
705 0 : }
706 :
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()
714 0 : );
715 :
716 : // and remove local groups which do not apply - e.g. have no entries due to the limited content of the
717 : // current SfxFilterMatcherIter
718 :
719 0 : FilterGroup& rGlobalFilters = _rAllFilters.front();
720 0 : FilterGroup aNonEmptyGlobalFilters;
721 : ::std::for_each(
722 : rGlobalFilters.begin(),
723 : rGlobalFilters.end(),
724 : CopyNonEmptyFilter( aNonEmptyGlobalFilters )
725 0 : );
726 0 : rGlobalFilters.swap( aNonEmptyGlobalFilters );
727 0 : }
728 :
729 :
730 0 : struct AppendFilter : public ::std::unary_function< FilterDescriptor, void >
731 : {
732 : protected:
733 : Reference< XFilterManager > m_xFilterManager;
734 : FileDialogHelper_Impl* m_pFileDlgImpl;
735 : bool m_bAddExtension;
736 :
737 : public:
738 0 : AppendFilter( const Reference< XFilterManager >& _rxFilterManager,
739 : FileDialogHelper_Impl* _pImpl, bool _bAddExtension ) :
740 :
741 : m_xFilterManager( _rxFilterManager ),
742 : m_pFileDlgImpl ( _pImpl ),
743 0 : m_bAddExtension ( _bAddExtension )
744 :
745 : {
746 : DBG_ASSERT( m_xFilterManager.is(), "AppendFilter::AppendFilter: invalid filter manager!" );
747 : DBG_ASSERT( m_pFileDlgImpl, "AppendFilter::AppendFilter: invalid filedlg impl!" );
748 0 : }
749 :
750 : // operate on a single filter
751 0 : void operator() ( const FilterDescriptor& _rFilterEntry )
752 : {
753 : OUString sDisplayText = m_bAddExtension
754 : ? addExtension( _rFilterEntry.First, _rFilterEntry.Second, true, *m_pFileDlgImpl )
755 0 : : _rFilterEntry.First;
756 0 : m_xFilterManager->appendFilter( sDisplayText, _rFilterEntry.Second );
757 0 : }
758 : };
759 :
760 :
761 : // = handling for the "all files" entry
762 :
763 :
764 :
765 0 : bool lcl_hasAllFilesFilter( TSortedFilterList& _rFilterMatcher, OUString& /* [out] */ _rAllFilterName )
766 : {
767 0 : bool bHasAll = false;
768 0 : _rAllFilterName = SfxResId( STR_SFX_FILTERNAME_ALL ).toString();
769 :
770 :
771 : // check if there's already a filter <ALL>
772 0 : for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter && !bHasAll; pFilter = _rFilterMatcher.Next() )
773 : {
774 0 : if ( pFilter->GetUIName() == _rAllFilterName )
775 0 : bHasAll = true;
776 : }
777 0 : return bHasAll;
778 : }
779 :
780 :
781 0 : void lcl_EnsureAllFilesEntry( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rFilters )
782 : {
783 :
784 0 : OUString sAllFilterName;
785 0 : if ( !lcl_hasAllFilesFilter( _rFilterMatcher, sAllFilterName ) )
786 : {
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 0 : if ( !_rFilters.empty() )
790 : {
791 0 : FilterGroup& rGlobalClasses = *_rFilters.begin();
792 0 : rGlobalClasses.push_front( FilterDescriptor( sAllFilterName, FILEDIALOG_FILTER_ALL ) );
793 : }
794 0 : }
795 0 : }
796 :
797 :
798 : // = filling an XFilterManager
799 :
800 :
801 :
802 0 : struct AppendFilterGroup : public ::std::unary_function< FilterGroup, void >
803 : {
804 : protected:
805 : Reference< XFilterManager > m_xFilterManager;
806 : Reference< XFilterGroupManager > m_xFilterGroupManager;
807 : FileDialogHelper_Impl* m_pFileDlgImpl;
808 :
809 : public:
810 0 : AppendFilterGroup( const Reference< XFilterManager >& _rxFilterManager, FileDialogHelper_Impl* _pImpl )
811 : :m_xFilterManager ( _rxFilterManager )
812 : ,m_xFilterGroupManager ( _rxFilterManager, UNO_QUERY )
813 0 : ,m_pFileDlgImpl ( _pImpl )
814 : {
815 : DBG_ASSERT( m_xFilterManager.is(), "AppendFilterGroup::AppendFilterGroup: invalid filter manager!" );
816 : DBG_ASSERT( m_pFileDlgImpl, "AppendFilterGroup::AppendFilterGroup: invalid filedlg impl!" );
817 0 : }
818 :
819 0 : void appendGroup( const FilterGroup& _rGroup, bool _bAddExtension )
820 : {
821 : try
822 : {
823 0 : 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 0 : if ( _rGroup.size() )
827 : {
828 0 : Sequence< StringPair > aFilters( _rGroup.size() );
829 : ::std::copy(
830 : _rGroup.begin(),
831 : _rGroup.end(),
832 : aFilters.getArray()
833 0 : );
834 0 : if ( _bAddExtension )
835 : {
836 0 : StringPair* pFilters = aFilters.getArray();
837 0 : StringPair* pEnd = pFilters + aFilters.getLength();
838 0 : for ( ; pFilters != pEnd; ++pFilters )
839 0 : pFilters->First = addExtension( pFilters->First, pFilters->Second, true, *m_pFileDlgImpl );
840 : }
841 0 : m_xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
842 : }
843 : }
844 : else
845 : {
846 : ::std::for_each(
847 : _rGroup.begin(),
848 : _rGroup.end(),
849 0 : AppendFilter( m_xFilterManager, m_pFileDlgImpl, _bAddExtension ) );
850 : }
851 : }
852 0 : catch( const Exception& )
853 : {
854 : DBG_UNHANDLED_EXCEPTION();
855 : }
856 0 : }
857 :
858 : // operate on a single filter group
859 0 : void operator() ( const FilterGroup& _rGroup )
860 : {
861 0 : appendGroup( _rGroup, true );
862 0 : }
863 : };
864 :
865 :
866 0 : TSortedFilterList::TSortedFilterList(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration >& xFilterList)
867 0 : : m_nIterator(0)
868 : {
869 0 : if (!xFilterList.is())
870 0 : return;
871 :
872 0 : m_lFilters.clear();
873 0 : while(xFilterList->hasMoreElements())
874 : {
875 0 : ::comphelper::SequenceAsHashMap lFilterProps (xFilterList->nextElement());
876 : OUString sFilterName = lFilterProps.getUnpackedValueOrDefault(
877 : OUString("Name"),
878 0 : OUString());
879 0 : if (!sFilterName.isEmpty())
880 0 : m_lFilters.push_back(sFilterName);
881 0 : }
882 : }
883 :
884 :
885 0 : const SfxFilter* TSortedFilterList::First()
886 : {
887 0 : m_nIterator = 0;
888 0 : return impl_getFilter(m_nIterator);
889 : }
890 :
891 :
892 0 : const SfxFilter* TSortedFilterList::Next()
893 : {
894 0 : ++m_nIterator;
895 0 : return impl_getFilter(m_nIterator);
896 : }
897 :
898 :
899 0 : const SfxFilter* TSortedFilterList::impl_getFilter(sal_Int32 nIndex)
900 : {
901 0 : if (nIndex<0 || nIndex>=(sal_Int32)m_lFilters.size())
902 0 : return 0;
903 0 : const OUString& sFilterName = m_lFilters[nIndex];
904 0 : if (sFilterName.isEmpty())
905 0 : return 0;
906 0 : return SfxFilter::GetFilterByName(sFilterName);
907 : }
908 :
909 :
910 0 : void appendFiltersForSave( TSortedFilterList& _rFilterMatcher,
911 : const Reference< XFilterManager >& _rxFilterManager,
912 : OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl,
913 : const OUString& _rFactory )
914 : {
915 : DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForSave: invalid manager!" );
916 0 : if ( !_rxFilterManager.is() )
917 0 : return;
918 :
919 0 : OUString sUIName;
920 0 : OUString sExtension;
921 :
922 : // retrieve the default filter for this application module.
923 : // It must be set as first of the generated filter list.
924 0 : 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 0 : sExtension = pDefaultFilter->GetWildcard().getGlob().getToken(0, ';');
928 0 : sUIName = addExtension( pDefaultFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
929 : try
930 : {
931 0 : _rxFilterManager->appendFilter( sUIName, sExtension );
932 0 : if ( _rFirstNonEmpty.isEmpty() )
933 0 : _rFirstNonEmpty = sUIName;
934 : }
935 0 : catch( const IllegalArgumentException& )
936 : {
937 : SAL_WARN( "sfx.dialog", "Could not append DefaultFilter" << sUIName );
938 : }
939 :
940 0 : for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
941 : {
942 0 : if (pFilter->GetName() == pDefaultFilter->GetName())
943 0 : continue;
944 :
945 : // Only use one extension (#i32434#)
946 : // (and always the first if there are more than one)
947 0 : sExtension = pFilter->GetWildcard().getGlob().getToken(0, ';');
948 0 : sUIName = addExtension( pFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
949 : try
950 : {
951 0 : _rxFilterManager->appendFilter( sUIName, sExtension );
952 0 : if ( _rFirstNonEmpty.isEmpty() )
953 0 : _rFirstNonEmpty = sUIName;
954 : }
955 0 : catch( const IllegalArgumentException& )
956 : {
957 : SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
958 : }
959 0 : }
960 : }
961 :
962 0 : struct ExportFilter
963 : {
964 0 : ExportFilter( const OUString& _aUIName, const OUString& _aWildcard ) :
965 0 : aUIName( _aUIName ), aWildcard( _aWildcard ) {}
966 :
967 : OUString aUIName;
968 : OUString aWildcard;
969 : };
970 :
971 :
972 0 : void appendExportFilters( TSortedFilterList& _rFilterMatcher,
973 : const Reference< XFilterManager >& _rxFilterManager,
974 : OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
975 : {
976 : DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendExportFilters: invalid manager!" );
977 0 : if ( !_rxFilterManager.is() )
978 0 : return;
979 :
980 0 : sal_Int32 nHTMLIndex = -1;
981 0 : sal_Int32 nXHTMLIndex = -1;
982 0 : sal_Int32 nPDFIndex = -1;
983 0 : sal_Int32 nFlashIndex = -1;
984 0 : OUString sUIName;
985 0 : OUString sExtensions;
986 0 : std::vector< ExportFilter > aImportantFilterGroup;
987 0 : std::vector< ExportFilter > aFilterGroup;
988 0 : Reference< XFilterGroupManager > xFilterGroupManager( _rxFilterManager, UNO_QUERY );
989 0 : OUString sTypeName;
990 0 : const OUString sWriterHTMLType( "generic_HTML" );
991 0 : const OUString sGraphicHTMLType( "graphic_HTML" );
992 0 : const OUString sXHTMLType( "XHTML_File" );
993 0 : const OUString sPDFType( "pdf_Portable_Document_Format" );
994 0 : const OUString sFlashType( "graphic_SWF" );
995 :
996 0 : for ( const SfxFilter* pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
997 : {
998 0 : sTypeName = pFilter->GetTypeName();
999 0 : sUIName = pFilter->GetUIName();
1000 0 : sExtensions = pFilter->GetWildcard().getGlob();
1001 0 : ExportFilter aExportFilter( sUIName, sExtensions );
1002 :
1003 0 : if ( nHTMLIndex == -1 &&
1004 0 : ( sTypeName.equals( sWriterHTMLType ) || sTypeName.equals( sGraphicHTMLType ) ) )
1005 : {
1006 0 : aImportantFilterGroup.insert( aImportantFilterGroup.begin(), aExportFilter );
1007 0 : nHTMLIndex = 0;
1008 : }
1009 0 : else if ( nXHTMLIndex == -1 && sTypeName.equals( sXHTMLType ) )
1010 : {
1011 0 : std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
1012 0 : if ( nHTMLIndex == -1 )
1013 0 : aImportantFilterGroup.insert( aIter, aExportFilter );
1014 : else
1015 0 : aImportantFilterGroup.insert( ++aIter, aExportFilter );
1016 0 : nXHTMLIndex = 0;
1017 : }
1018 0 : else if ( nPDFIndex == -1 && sTypeName.equals( sPDFType ) )
1019 : {
1020 0 : std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
1021 0 : if ( nHTMLIndex != -1 )
1022 0 : ++aIter;
1023 0 : if ( nXHTMLIndex != -1 )
1024 0 : ++aIter;
1025 0 : aImportantFilterGroup.insert( aIter, aExportFilter );
1026 0 : nPDFIndex = 0;
1027 : }
1028 0 : else if ( nFlashIndex == -1 && sTypeName.equals( sFlashType ) )
1029 : {
1030 0 : std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
1031 0 : if ( nHTMLIndex != -1 )
1032 0 : ++aIter;
1033 0 : if ( nXHTMLIndex != -1 )
1034 0 : ++aIter;
1035 0 : if ( nPDFIndex != -1 )
1036 0 : ++aIter;
1037 0 : aImportantFilterGroup.insert( aIter, aExportFilter );
1038 0 : nFlashIndex = 0;
1039 : }
1040 : else
1041 0 : aFilterGroup.push_back( aExportFilter );
1042 0 : }
1043 :
1044 0 : if ( xFilterGroupManager.is() )
1045 : {
1046 : // Add both html/pdf filter as a filter group to get a separator between both groups
1047 0 : if ( !aImportantFilterGroup.empty() )
1048 : {
1049 0 : Sequence< StringPair > aFilters( aImportantFilterGroup.size() );
1050 0 : for ( sal_Int32 i = 0; i < (sal_Int32)aImportantFilterGroup.size(); i++ )
1051 : {
1052 0 : aFilters[i].First = addExtension( aImportantFilterGroup[i].aUIName,
1053 0 : aImportantFilterGroup[i].aWildcard,
1054 0 : false, _rFileDlgImpl );
1055 0 : aFilters[i].Second = aImportantFilterGroup[i].aWildcard;
1056 : }
1057 :
1058 : try
1059 : {
1060 0 : xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
1061 : }
1062 0 : catch( const IllegalArgumentException& )
1063 : {
1064 0 : }
1065 : }
1066 :
1067 0 : if ( !aFilterGroup.empty() )
1068 : {
1069 0 : Sequence< StringPair > aFilters( aFilterGroup.size() );
1070 0 : for ( sal_Int32 i = 0; i < (sal_Int32)aFilterGroup.size(); i++ )
1071 : {
1072 0 : aFilters[i].First = addExtension( aFilterGroup[i].aUIName,
1073 0 : aFilterGroup[i].aWildcard,
1074 0 : false, _rFileDlgImpl );
1075 0 : aFilters[i].Second = aFilterGroup[i].aWildcard;
1076 : }
1077 :
1078 : try
1079 : {
1080 0 : xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
1081 : }
1082 0 : catch( const IllegalArgumentException& )
1083 : {
1084 0 : }
1085 : }
1086 : }
1087 : else
1088 : {
1089 : // Fallback solution just add both filter groups as single filters
1090 : sal_Int32 n;
1091 :
1092 0 : for ( n = 0; n < (sal_Int32)aImportantFilterGroup.size(); n++ )
1093 : {
1094 : try
1095 : {
1096 0 : OUString aUIName = addExtension( aImportantFilterGroup[n].aUIName,
1097 0 : aImportantFilterGroup[n].aWildcard,
1098 0 : false, _rFileDlgImpl );
1099 0 : _rxFilterManager->appendFilter( aUIName, aImportantFilterGroup[n].aWildcard );
1100 0 : if ( _rFirstNonEmpty.isEmpty() )
1101 0 : _rFirstNonEmpty = sUIName;
1102 :
1103 : }
1104 0 : catch( const IllegalArgumentException& )
1105 : {
1106 : SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
1107 : }
1108 : }
1109 :
1110 0 : for ( n = 0; n < (sal_Int32)aFilterGroup.size(); n++ )
1111 : {
1112 : try
1113 : {
1114 0 : OUString aUIName = addExtension( aFilterGroup[n].aUIName,
1115 0 : aFilterGroup[n].aWildcard,
1116 0 : false, _rFileDlgImpl );
1117 0 : _rxFilterManager->appendFilter( aUIName, aFilterGroup[n].aWildcard );
1118 0 : if ( _rFirstNonEmpty.isEmpty() )
1119 0 : _rFirstNonEmpty = sUIName;
1120 :
1121 : }
1122 0 : catch( const IllegalArgumentException& )
1123 : {
1124 : SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
1125 : }
1126 : }
1127 0 : }
1128 : }
1129 :
1130 :
1131 0 : void appendFiltersForOpen( TSortedFilterList& _rFilterMatcher,
1132 : const Reference< XFilterManager >& _rxFilterManager,
1133 : OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
1134 : {
1135 : DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForOpen: invalid manager!" );
1136 0 : if ( !_rxFilterManager.is() )
1137 0 : return;
1138 :
1139 :
1140 : // group and classify the filters
1141 0 : GroupedFilterList aAllFilters;
1142 0 : lcl_GroupAndClassify( _rFilterMatcher, aAllFilters );
1143 :
1144 :
1145 : // ensure that we have the one "all files" entry
1146 0 : lcl_EnsureAllFilesEntry( _rFilterMatcher, aAllFilters );
1147 :
1148 :
1149 : // the first non-empty string - which we assume is the first overall entry
1150 0 : if ( !aAllFilters.empty() )
1151 : {
1152 0 : const FilterGroup& rFirstGroup = *aAllFilters.begin(); // should be the global classes
1153 0 : if ( !rFirstGroup.empty() )
1154 0 : _rFirstNonEmpty = rFirstGroup.begin()->First;
1155 : // append first group, without extension
1156 0 : AppendFilterGroup aGroup( _rxFilterManager, &_rFileDlgImpl );
1157 0 : aGroup.appendGroup( rFirstGroup, false );
1158 : }
1159 :
1160 :
1161 : // append the filters to the manager
1162 0 : if ( !aAllFilters.empty() )
1163 : {
1164 0 : ::std::list< FilterGroup >::iterator pIter = aAllFilters.begin();
1165 0 : ++pIter;
1166 : ::std::for_each(
1167 : pIter, // first filter group was handled separately, see above
1168 : aAllFilters.end(),
1169 0 : AppendFilterGroup( _rxFilterManager, &_rFileDlgImpl ) );
1170 0 : }
1171 : }
1172 :
1173 0 : OUString addExtension( const OUString& _rDisplayText,
1174 : const OUString& _rExtension,
1175 : bool _bForOpen, FileDialogHelper_Impl& _rFileDlgImpl )
1176 : {
1177 : static const char sAllFilter[] = "(*.*)";
1178 : static const char sOpenBracket[] = " (";
1179 : static const char sCloseBracket[] = ")";
1180 0 : OUString sRet = _rDisplayText;
1181 :
1182 0 : if ( sRet.indexOf( sAllFilter ) == -1 )
1183 : {
1184 0 : OUString sExt = _rExtension;
1185 0 : if ( !_bForOpen )
1186 : {
1187 : // show '*' in extensions only when opening a document
1188 0 : sExt = comphelper::string::remove(sExt, '*');
1189 : }
1190 0 : sRet += sOpenBracket;
1191 0 : sRet += sExt;
1192 0 : sRet += sCloseBracket;
1193 : }
1194 0 : _rFileDlgImpl.addFilterPair( _rDisplayText, sRet );
1195 0 : return sRet;
1196 : }
1197 :
1198 :
1199 648 : } // namespace sfx2
1200 :
1201 :
1202 :
1203 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|