1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "filtergrouping.hxx"
#include <sfx2/fcontnr.hxx>
#include <sfx2/filedlghelper.hxx>
#include <sfx2/strings.hrc>
#include <sfx2/docfilt.hxx>
#include <sfx2/sfxresid.hxx>
#include <sal/log.hxx>
#include <com/sun/star/ui/dialogs/XFilterGroupManager.hpp>
#include <com/sun/star/beans/StringPair.hpp>
#include <com/sun/star/uno/Sequence.hxx>
#include <unotools/confignode.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <comphelper/sequence.hxx>
#include <comphelper/string.hxx>
#include <tools/diagnose_ex.h>
#include <tools/debug.hxx>
#include <list>
#include <vector>
#include <map>
#include <algorithm>
namespace sfx2
{
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::ui::dialogs;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::utl;
/**
Some general words about what's going on here...
<p>In our file open dialog, usually we display every filter we know. That's how it was before: every filter
lead to an own line in the filter list box, e.g. "StarWriter 5.0 Document" or "Microsoft Word 97".</p>
<p>But then the PM came. And everything changed...</p>
<p>A basic idea are groups: Why simply listing all the single filters? Couldn't we draw nice separators
between the filters which logically belong together? I.e. all the filters which open a document in StarWriter:
couldn't we separate them from all the filters which open the document in StarCalc?<br/>
So spoke the PM, and engineering obeyed.</p>
<p>So we have groups. They're just a visual aspect: All the filters of a group are presented together, separated
by a line from other groups.</p>
<p>Let's be honest: How the concrete implementation of the file picker service separates the different groups
is a matter of this implementation. We only do this grouping and suggest it to the FilePicker service ...</p>
<p>Now for the second concept:<br/>
Thinking about it (and that's what the PM did), both "StarWriter 5.0 Document" and "Microsoft Word 97"
describe a text document. It's a text. It's of no interest for the user that one of the texts was saved in
MS' format, and one in our own format.<br/>
So in a first step, we want to have a filter entry "Text documents". This would cover both above-mentioned
filters, as well as any other filters for documents which are texts.</p>
<p>Such an entry as "Text documents" is - within the scope of this file - called "class" or "filter class".</p>
<p>In the file-open-dialog, such a class looks like an ordinary filter: it's simply a name in the filter
listbox. Selecting means that all the files matching one of the "sub-filters" are displayed (in the example above,
this would be "*.sdw", "*.doc" and so on).</p>
<p>Now there are two types of filter classes: global ones and local ones. "Text documents" is a global class. As
well as "Spreadsheets". Or "Web pages".<br/>
Let's have a look at a local class: The filters "MS Word 95" and "MS WinWord 6.0" together form the class
"Microsoft Word 6.0 / 95" (don't ask for the reasons. At least not me. Ask the PM). There are a lot of such
local classes ...</p>
<p>The difference between global and local classes is as follows: Global classes are presented in an own group.
There is one dedicated group at the top of the list, containing all the global groups - no local groups and no
single filters.</p>
<p>Ehm - it was a lie. Not really at the top. Before this group, there is this single "All files" entry. It forms
its own group. But this is uninteresting here.</p>
<p>Local classes must consist of filters which - without the classification - would all belong to the same group.
Then, they're combined to one entry (in the example above: "Microsoft Word 6.0 / 95"), and this entry is inserted
into the file picker filter list, instead of the single filters which form the class.</p>
<p>This is an interesting difference between local and global classes: Filters which are part of a global class
are listed in their own group, too. Filters in local classes aren't listed a second time - neither directly (as
the filter itself) nor indirectly (as part of another local group).</p>
<p>The only exception are filters which are part of a global class <em>and</em> a local class. This is allowed.
Being contained in two local classes isn't.</p>
<p>So that's all what you need to know: Understand the concept of "filter classes" (a filter class combines
different filters and acts as if it's a filter itself) and the concept of groups (a group just describes a
logical correlation of filters and usually is represented to the user by drawing group separators in the filter
list).</p>
<p>If you got it, go try understanding this file :).</p>
*/
typedef StringPair FilterDescriptor; // a single filter or a filter class (display name and filter mask)
typedef ::std::list< FilterDescriptor > FilterGroup; // a list of single filter entries
typedef ::std::list< FilterGroup > GroupedFilterList; // a list of all filters, already grouped
/// the logical name of a filter
typedef OUString FilterName;
// a struct which holds references from a logical filter name to a filter group entry
// used for quick lookup of classes (means class entries - entries representing a class)
// which a given filter may belong to
typedef ::std::map< OUString, FilterGroup::iterator > FilterGroupEntryReferrer;
namespace {
/// a descriptor for a filter class (which in the final dialog is represented by one filter entry)
struct FilterClass
{
OUString sDisplayName; // the display name
Sequence< FilterName > aSubFilters; // the (logical) names of the filter which belong to the class
};
}
typedef ::std::list< FilterClass > FilterClassList;
typedef ::std::map< OUString, FilterClassList::iterator > FilterClassReferrer;
// = reading of configuration data
static void lcl_ReadFilterClass( const OConfigurationNode& _rClassesNode, const OUString& _rLogicalClassName,
FilterClass& /* [out] */ _rClass )<--- Parameter '_rClass' can be declared with const
{
// the description node for the current class
OConfigurationNode aClassDesc = _rClassesNode.openNode( _rLogicalClassName );
// the values
aClassDesc.getNodeValue( "DisplayName" ) >>= _rClass.sDisplayName;
aClassDesc.getNodeValue( "Filters" ) >>= _rClass.aSubFilters;
}
namespace {
struct CreateEmptyClassRememberPos
{
protected:
FilterClassList& m_rClassList;
FilterClassReferrer& m_rClassesReferrer;
public:
CreateEmptyClassRememberPos( FilterClassList& _rClassList, FilterClassReferrer& _rClassesReferrer )
:m_rClassList ( _rClassList )
,m_rClassesReferrer ( _rClassesReferrer )
{
}
// operate on a single class name
void operator() ( const FilterName& _rLogicalFilterName )
{
// insert a new (empty) class
m_rClassList.emplace_back( );
// get the position of this new entry
FilterClassList::iterator aInsertPos = m_rClassList.end();
--aInsertPos;
// remember this position
m_rClassesReferrer.emplace( _rLogicalFilterName, aInsertPos );
}
};
struct ReadGlobalFilter
{
protected:
OConfigurationNode m_aClassesNode;
FilterClassReferrer& m_aClassReferrer;
public:
ReadGlobalFilter( const OConfigurationNode& _rClassesNode, FilterClassReferrer& _rClassesReferrer )
:m_aClassesNode ( _rClassesNode )
,m_aClassReferrer ( _rClassesReferrer )
{
}
// operate on a single logical name
void operator() ( const FilterName& _rName )
{
FilterClassReferrer::iterator aClassRef = m_aClassReferrer.find( _rName );
if ( m_aClassReferrer.end() == aClassRef )
{
// we do not know this global class
OSL_FAIL( "ReadGlobalFilter::operator(): unknown filter name!" );
// TODO: perhaps we should be more tolerant - at the moment, the filter is dropped
// We could silently push_back it to the container...
}
else
{
// read the data of this class into the node referred to by aClassRef
lcl_ReadFilterClass( m_aClassesNode, _rName, *aClassRef->second );
}
}
};
}
static void lcl_ReadGlobalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rGlobalClasses, std::vector<OUString>& _rGlobalClassNames )
{
_rGlobalClasses.clear();
_rGlobalClassNames.clear();
// get the list describing the order of all global classes
Sequence< OUString > aGlobalClasses;
_rFilterClassification.getNodeValue( "GlobalFilters/Order" ) >>= aGlobalClasses;
// copy the logical names
comphelper::sequenceToContainer(_rGlobalClassNames, aGlobalClasses);
// Global classes are presented in an own group, so their order matters (while the order of the
// "local classes" doesn't).
// That's why we can't simply add the global classes to _rGlobalClasses using the order in which they
// are returned from the configuration - it is completely undefined, and we need a _defined_ order.
FilterClassReferrer aClassReferrer;
::std::for_each(
aGlobalClasses.begin(),
aGlobalClasses.end(),
CreateEmptyClassRememberPos( _rGlobalClasses, aClassReferrer )
);
// now _rGlobalClasses contains a dummy entry for each global class,
// while aClassReferrer maps from the logical name of the class to the position within _rGlobalClasses where
// it's dummy entry resides
// go for all the single class entries
OConfigurationNode aFilterClassesNode =
_rFilterClassification.openNode( "GlobalFilters/Classes" );
Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
::std::for_each(
aFilterClasses.begin(),
aFilterClasses.end(),
ReadGlobalFilter( aFilterClassesNode, aClassReferrer )
);
}
namespace {
struct ReadLocalFilter
{
protected:
OConfigurationNode m_aClassesNode;
FilterClassList& m_rClasses;
public:
ReadLocalFilter( const OConfigurationNode& _rClassesNode, FilterClassList& _rClasses )
:m_aClassesNode ( _rClassesNode )
,m_rClasses ( _rClasses )
{
}
// operate on a single logical name
void operator() ( const FilterName& _rName )
{
// read the data for this class
FilterClass aClass;
lcl_ReadFilterClass( m_aClassesNode, _rName, aClass );
// insert the class descriptor
m_rClasses.push_back( aClass );
}
};
}
static void lcl_ReadLocalFilters( const OConfigurationNode& _rFilterClassification, FilterClassList& _rLocalClasses )
{
_rLocalClasses.clear();
// the node for the local classes
OConfigurationNode aFilterClassesNode =
_rFilterClassification.openNode( "LocalFilters/Classes" );
Sequence< OUString > aFilterClasses = aFilterClassesNode.getNodeNames();
::std::for_each(
aFilterClasses.begin(),
aFilterClasses.end(),
ReadLocalFilter( aFilterClassesNode, _rLocalClasses )
);
}
static void lcl_ReadClassification( FilterClassList& _rGlobalClasses, std::vector<OUString>& _rGlobalClassNames, FilterClassList& _rLocalClasses )
{
// open our config node
OConfigurationTreeRoot aFilterClassification = OConfigurationTreeRoot::createWithComponentContext(
::comphelper::getProcessComponentContext(),
"org.openoffice.Office.UI/FilterClassification",
-1,
OConfigurationTreeRoot::CM_READONLY
);
// go for the global classes
lcl_ReadGlobalFilters( aFilterClassification, _rGlobalClasses, _rGlobalClassNames );
// go for the local classes
lcl_ReadLocalFilters( aFilterClassification, _rLocalClasses );
}
// = grouping and classifying
namespace {
// a struct which adds helps remembering a reference to a class entry
struct ReferToFilterEntry
{
protected:
FilterGroupEntryReferrer& m_rEntryReferrer;
FilterGroup::iterator m_aClassPos;
public:
ReferToFilterEntry( FilterGroupEntryReferrer& _rEntryReferrer, const FilterGroup::iterator& _rClassPos )
:m_rEntryReferrer( _rEntryReferrer )
,m_aClassPos( _rClassPos )
{
}
// operate on a single filter name
void operator() ( const FilterName& _rName )
{
::std::pair< FilterGroupEntryReferrer::iterator, bool > aInsertRes =
m_rEntryReferrer.emplace( _rName, m_aClassPos );
SAL_WARN_IF(
!aInsertRes.second, "sfx.dialog",
"already have an element for " << _rName);
}
};
struct FillClassGroup
{
protected:
FilterGroup& m_rClassGroup;
FilterGroupEntryReferrer& m_rClassReferrer;
public:
FillClassGroup( FilterGroup& _rClassGroup, FilterGroupEntryReferrer& _rClassReferrer )
:m_rClassGroup ( _rClassGroup )
,m_rClassReferrer ( _rClassReferrer )
{
}
// operate on a single class
void operator() ( const FilterClass& _rClass )
{
// create an empty filter descriptor for the class
FilterDescriptor aClassEntry;
// set its name (which is all we know by now)
aClassEntry.First = _rClass.sDisplayName;
// add it to the group
m_rClassGroup.push_back( aClassEntry );
// the position of the newly added class
FilterGroup::iterator aClassEntryPos = m_rClassGroup.end();
--aClassEntryPos;
// and for all the sub filters of the class, remember the class
// (respectively the position of the class it the group)
::std::for_each(
_rClass.aSubFilters.begin(),
_rClass.aSubFilters.end(),
ReferToFilterEntry( m_rClassReferrer, aClassEntryPos )
);
}
};
}
static const sal_Unicode s_cWildcardSeparator( ';' );
static OUString getSeparatorString()
{
return ";";
}
namespace {
struct CheckAppendSingleWildcard
{
OUString& _rToBeExtended;
explicit CheckAppendSingleWildcard( OUString& _rBase ) : _rToBeExtended( _rBase ) { }
void operator() ( const OUString& _rWC )
{
// check for double wildcards
sal_Int32 nExistentPos = _rToBeExtended.indexOf( _rWC );
if ( -1 < nExistentPos )
{ // found this wildcard (already part of _rToBeExtended)
if ( ( 0 == nExistentPos )
|| ( s_cWildcardSeparator == _rToBeExtended[ nExistentPos - 1 ] )
)
{ // the wildcard really starts at this position (it starts at pos 0 or the previous character is a separator
sal_Int32 nExistentWCEnd = nExistentPos + _rWC.getLength();
if ( ( _rToBeExtended.getLength() == nExistentWCEnd )
|| ( s_cWildcardSeparator == _rToBeExtended[ nExistentWCEnd ] )
)
{ // it's really the complete wildcard we found
// (not something like _rWC being "*.t" and _rToBeExtended containing "*.txt")
// -> outta here
return;
}
}
}
if ( !_rToBeExtended.isEmpty() )
_rToBeExtended += getSeparatorString();
_rToBeExtended += _rWC;
}
};
// a helper struct which adds a fixed (Sfx-)filter to a filter group entry given by iterator
struct AppendWildcardToDescriptor
{
protected:
::std::vector< OUString > aWildCards;
public:
explicit AppendWildcardToDescriptor( const OUString& _rWildCard );
// operate on a single class entry
void operator() ( const FilterGroupEntryReferrer::value_type& _rClassReference )
{
// simply add our wildcards
::std::for_each(
aWildCards.begin(),
aWildCards.end(),
CheckAppendSingleWildcard( _rClassReference.second->Second )
);
}
};
}
AppendWildcardToDescriptor::AppendWildcardToDescriptor( const OUString& _rWildCard )
{
DBG_ASSERT( !_rWildCard.isEmpty(),
"AppendWildcardToDescriptor::AppendWildcardToDescriptor: invalid wildcard!" );
DBG_ASSERT( _rWildCard.isEmpty() || _rWildCard[0] != s_cWildcardSeparator,
"AppendWildcardToDescriptor::AppendWildcardToDescriptor: wildcard already separated!" );
aWildCards.reserve( comphelper::string::getTokenCount(_rWildCard, s_cWildcardSeparator) );
const sal_Unicode* pTokenLoop = _rWildCard.getStr();
const sal_Unicode* pTokenLoopEnd = pTokenLoop + _rWildCard.getLength();
const sal_Unicode* pTokenStart = pTokenLoop;
for ( ; pTokenLoop != pTokenLoopEnd; ++pTokenLoop )
{
if ( ( s_cWildcardSeparator == *pTokenLoop ) && ( pTokenLoop > pTokenStart ) )
{ // found a new token separator (and a non-empty token)
aWildCards.emplace_back( pTokenStart, pTokenLoop - pTokenStart );
// search the start of the next token
while ( ( pTokenStart != pTokenLoopEnd ) && ( *pTokenStart != s_cWildcardSeparator ) )
++pTokenStart;
if ( pTokenStart == pTokenLoopEnd )
// reached the end
break;
++pTokenStart;
pTokenLoop = pTokenStart;
}
}
if ( pTokenLoop > pTokenStart )
// the last one...
aWildCards.emplace_back( pTokenStart, pTokenLoop - pTokenStart );
}
static void lcl_InitGlobalClasses( GroupedFilterList& _rAllFilters, const FilterClassList& _rGlobalClasses, FilterGroupEntryReferrer& _rGlobalClassesRef )
{
// we need an extra group in our "all filters" container
_rAllFilters.push_front( FilterGroup() );
FilterGroup& rGlobalFilters = _rAllFilters.front();
// it's important to work on the reference: we want to access the members of this filter group
// by an iterator (FilterGroup::const_iterator)
// the referrer for the global classes
// initialize the group
::std::for_each(
_rGlobalClasses.begin(),
_rGlobalClasses.end(),
FillClassGroup( rGlobalFilters, _rGlobalClassesRef )
);
// now we have:
// in rGlobalFilters: a list of FilterDescriptor's, where each's descriptor's display name is set to the name of a class
// in aGlobalClassesRef: a mapping from logical filter names to positions within rGlobalFilters
// this way, if we encounter an arbitrary filter, we can easily (and efficient) check if it belongs to a global class
// and modify the descriptor for this class accordingly
}
typedef ::std::vector< ::std::pair< FilterGroupEntryReferrer::mapped_type, FilterGroup::iterator > >
MapGroupEntry2GroupEntry;
// this is not really a map - it's just called this way because it is used as a map
namespace {
struct FindGroupEntry
{
FilterGroupEntryReferrer::mapped_type aLookingFor;
explicit FindGroupEntry( FilterGroupEntryReferrer::mapped_type const & _rLookingFor ) : aLookingFor( _rLookingFor ) { }
bool operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
{
return _rMapEntry.first == aLookingFor;
}
};
struct CopyGroupEntryContent
{
void operator() ( const MapGroupEntry2GroupEntry::value_type& _rMapEntry )
{
*_rMapEntry.second = *_rMapEntry.first;
}
};
struct CopyNonEmptyFilter
{
FilterGroup& rTarget;
explicit CopyNonEmptyFilter( FilterGroup& _rTarget ) :rTarget( _rTarget ) { }
void operator() ( const FilterDescriptor& _rFilter )
{
if ( !_rFilter.Second.isEmpty() )
rTarget.push_back( _rFilter );
}
};
}
static void lcl_GroupAndClassify( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rAllFilters )
{
_rAllFilters.clear();
// read the classification of filters
FilterClassList aGlobalClasses, aLocalClasses;
std::vector<OUString> aGlobalClassNames;
lcl_ReadClassification( aGlobalClasses, aGlobalClassNames, aLocalClasses );
// for the global filter classes
FilterGroupEntryReferrer aGlobalClassesRef;
lcl_InitGlobalClasses( _rAllFilters, aGlobalClasses, aGlobalClassesRef );
// insert as much placeholders (FilterGroup's) into _rAllFilter for groups as we have global classes
// (this assumes that both numbers are the same, which, speaking strictly, must not hold - but it does, as we know ...)
sal_Int32 nGlobalClasses = aGlobalClasses.size();
while ( nGlobalClasses-- )
_rAllFilters.emplace_back( );
// for the local classes:
// if n filters belong to a local class, they do not appear in their respective group explicitly, instead
// and entry for the class is added to the group and the extensions of the filters are collected under
// this entry
FilterGroupEntryReferrer aLocalClassesRef;
FilterGroup aCollectedLocals;
::std::for_each(
aLocalClasses.begin(),
aLocalClasses.end(),
FillClassGroup( aCollectedLocals, aLocalClassesRef )
);
// to map from the position within aCollectedLocals to positions within the real groups
// (where they finally belong to)
MapGroupEntry2GroupEntry aLocalFinalPositions;
// now add the filters
// the group which we currently work with
GroupedFilterList::iterator aCurrentGroup = _rAllFilters.end(); // no current group
// the filter container of the current group - if this changes between two filters, a new group is reached
OUString aCurrentServiceName;
OUString sFilterWildcard;
OUString sFilterName;
// loop through all the filters
for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
{
sFilterName = pFilter->GetFilterName();
sFilterWildcard = pFilter->GetWildcard().getGlob();
AppendWildcardToDescriptor aExtendWildcard( sFilterWildcard );
DBG_ASSERT( !sFilterWildcard.isEmpty(), "sfx2::lcl_GroupAndClassify: invalid wildcard of this filter!" );
// check for a change in the group
OUString aServiceName = pFilter->GetServiceName();
if ( aServiceName != aCurrentServiceName )
{ // we reached a new group
// look for the place in _rAllFilters where this ne group belongs - this is determined
// by the order of classes in aGlobalClassNames
GroupedFilterList::iterator aGroupPos = _rAllFilters.begin();
DBG_ASSERT( aGroupPos != _rAllFilters.end(),
"sfx2::lcl_GroupAndClassify: invalid all-filters array here!" );
// the loop below will work on invalid objects else ...
++aGroupPos;
auto aGlobalIter = std::find(aGlobalClassNames.begin(), aGlobalClassNames.end(), aServiceName);
auto nGroupPosShift = std::min(
std::distance(aGlobalClassNames.begin(), aGlobalIter),
std::distance(aGroupPos, _rAllFilters.end()));
std::advance(aGroupPos, nGroupPosShift);
if ( aGroupPos != _rAllFilters.end() )
// we found a global class name which matches the doc service name -> fill the filters of this
// group in the respective prepared group
aCurrentGroup = aGroupPos;
else
// insert a new entry in our overall-list
aCurrentGroup = _rAllFilters.insert( _rAllFilters.end(), FilterGroup() );
// remember the container to properly detect the next group
aCurrentServiceName = aServiceName;
}
assert(aCurrentGroup != _rAllFilters.end()); //invalid current group!
if (aCurrentGroup == _rAllFilters.end())
aCurrentGroup = _rAllFilters.begin();
// check if the filter is part of a global group
::std::pair< FilterGroupEntryReferrer::iterator, FilterGroupEntryReferrer::iterator >
aBelongsTo = aGlobalClassesRef.equal_range( sFilterName );
// add the filter to the entries for these classes
// (if they exist - if not, the range is empty and the for_each is a no-op)
::std::for_each(
aBelongsTo.first,
aBelongsTo.second,
aExtendWildcard
);
// add the filter to its group
// for this, check if the filter is part of a local filter
FilterGroupEntryReferrer::iterator aBelongsToLocal = aLocalClassesRef.find( sFilterName );
if ( aLocalClassesRef.end() != aBelongsToLocal )
{
// okay, there is a local class which the filter belongs to
// -> append the wildcard
aExtendWildcard( *aBelongsToLocal );
if ( std::none_of( aLocalFinalPositions.begin(), aLocalFinalPositions.end(), FindGroupEntry( aBelongsToLocal->second ) ) )
{ // the position within aCollectedLocals has not been mapped to a final position
// within the "real" group (aCollectedLocals is only temporary)
// -> do this now (as we just encountered the first filter belonging to this local class
// add a new entry which is the "real" group entry
aCurrentGroup->push_back( FilterDescriptor( aBelongsToLocal->second->First, OUString() ) );
// the position where we inserted the entry
FilterGroup::iterator aInsertPos = aCurrentGroup->end();
--aInsertPos;
// remember this pos
aLocalFinalPositions.emplace_back( aBelongsToLocal->second, aInsertPos );
}
}
else
aCurrentGroup->push_back( FilterDescriptor( pFilter->GetUIName(), sFilterWildcard ) );
}
// now just complete the infos for the local groups:
// During the above loop, they have been collected in aCollectedLocals, but this is only temporary
// They have to be copied into their final positions (which are stored in aLocalFinalPositions)
::std::for_each(
aLocalFinalPositions.begin(),
aLocalFinalPositions.end(),
CopyGroupEntryContent()
);
// and remove local groups which do not apply - e.g. have no entries due to the limited content of the
// current SfxFilterMatcherIter
FilterGroup& rGlobalFilters = _rAllFilters.front();
FilterGroup aNonEmptyGlobalFilters;
::std::for_each(
rGlobalFilters.begin(),
rGlobalFilters.end(),
CopyNonEmptyFilter( aNonEmptyGlobalFilters )
);
rGlobalFilters.swap( aNonEmptyGlobalFilters );
}
namespace {
struct AppendFilter
{
protected:
Reference< XFilterManager > m_xFilterManager;
FileDialogHelper_Impl* m_pFileDlgImpl;
bool m_bAddExtension;
public:
AppendFilter( const Reference< XFilterManager >& _rxFilterManager,
FileDialogHelper_Impl* _pImpl, bool _bAddExtension ) :
m_xFilterManager( _rxFilterManager ),
m_pFileDlgImpl ( _pImpl ),
m_bAddExtension ( _bAddExtension )
{
DBG_ASSERT( m_xFilterManager.is(), "AppendFilter::AppendFilter: invalid filter manager!" );
DBG_ASSERT( m_pFileDlgImpl, "AppendFilter::AppendFilter: invalid filedlg impl!" );
}
// operate on a single filter
void operator() ( const FilterDescriptor& _rFilterEntry )
{
OUString sDisplayText = m_bAddExtension
? addExtension( _rFilterEntry.First, _rFilterEntry.Second, true, *m_pFileDlgImpl )
: _rFilterEntry.First;
m_xFilterManager->appendFilter( sDisplayText, _rFilterEntry.Second );
}
};
}
// = handling for the "all files" entry
static bool lcl_hasAllFilesFilter( TSortedFilterList& _rFilterMatcher, OUString& /* [out] */ _rAllFilterName )
{
bool bHasAll = false;
_rAllFilterName = SfxResId( STR_SFX_FILTERNAME_ALL );
// check if there's already a filter <ALL>
for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter && !bHasAll; pFilter = _rFilterMatcher.Next() )
{
if ( pFilter->GetUIName() == _rAllFilterName )
bHasAll = true;
}
return bHasAll;
}
static void lcl_EnsureAllFilesEntry( TSortedFilterList& _rFilterMatcher, GroupedFilterList& _rFilters )
{
OUString sAllFilterName;
if ( !lcl_hasAllFilesFilter( _rFilterMatcher, sAllFilterName ) )
{
// get the first group of filters (by definition, this group contains the global classes)
DBG_ASSERT( !_rFilters.empty(), "lcl_EnsureAllFilesEntry: invalid filter list!" );
if ( !_rFilters.empty() )
{
FilterGroup& rGlobalClasses = *_rFilters.begin();
rGlobalClasses.push_front( FilterDescriptor( sAllFilterName, FILEDIALOG_FILTER_ALL ) );
}
}
}
// = filling an XFilterManager
namespace {
struct AppendFilterGroup
{
protected:
Reference< XFilterManager > m_xFilterManager;
Reference< XFilterGroupManager > m_xFilterGroupManager;
FileDialogHelper_Impl* m_pFileDlgImpl;
public:
AppendFilterGroup( const Reference< XFilterManager >& _rxFilterManager, FileDialogHelper_Impl* _pImpl )
:m_xFilterManager ( _rxFilterManager )
,m_xFilterGroupManager ( _rxFilterManager, UNO_QUERY )
,m_pFileDlgImpl ( _pImpl )
{
DBG_ASSERT( m_xFilterManager.is(), "AppendFilterGroup::AppendFilterGroup: invalid filter manager!" );
DBG_ASSERT( m_pFileDlgImpl, "AppendFilterGroup::AppendFilterGroup: invalid filedlg impl!" );
}
void appendGroup( const FilterGroup& _rGroup, bool _bAddExtension )
{
try
{
if ( m_xFilterGroupManager.is() )
{ // the file dialog implementation supports visual grouping of filters
// create a representation of the group which is understandable by the XFilterGroupManager
if ( !_rGroup.empty() )
{
Sequence< StringPair > aFilters( comphelper::containerToSequence(_rGroup) );
if ( _bAddExtension )
{
for ( StringPair & filter : aFilters )
filter.First = addExtension( filter.First, filter.Second, true, *m_pFileDlgImpl );
}
m_xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
}
}
else
{
::std::for_each(
_rGroup.begin(),
_rGroup.end(),
AppendFilter( m_xFilterManager, m_pFileDlgImpl, _bAddExtension ) );
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("sfx.dialog");
}
}
// operate on a single filter group
void operator() ( const FilterGroup& _rGroup )
{
appendGroup( _rGroup, true );
}
};
}
TSortedFilterList::TSortedFilterList(const css::uno::Reference< css::container::XEnumeration >& xFilterList)
: m_nIterator(0)
{
if (!xFilterList.is())
return;
m_lFilters.clear();
while(xFilterList->hasMoreElements())
{
::comphelper::SequenceAsHashMap lFilterProps (xFilterList->nextElement());
OUString sFilterName = lFilterProps.getUnpackedValueOrDefault(
"Name",
OUString());
if (!sFilterName.isEmpty())
m_lFilters.push_back(sFilterName);
}
}
std::shared_ptr<const SfxFilter> TSortedFilterList::First()
{
m_nIterator = 0;
return impl_getFilter(m_nIterator);
}
std::shared_ptr<const SfxFilter> TSortedFilterList::Next()
{
++m_nIterator;
return impl_getFilter(m_nIterator);
}
std::shared_ptr<const SfxFilter> TSortedFilterList::impl_getFilter(sal_Int32 nIndex)
{
if (nIndex<0 || nIndex>=static_cast<sal_Int32>(m_lFilters.size()))
return nullptr;
const OUString& sFilterName = m_lFilters[nIndex];
if (sFilterName.isEmpty())
return nullptr;
return SfxFilter::GetFilterByName(sFilterName);
}
void appendFiltersForSave( TSortedFilterList& _rFilterMatcher,<--- The function 'appendFiltersForSave' is never used.
const Reference< XFilterManager >& _rxFilterManager,
OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl,
const OUString& _rFactory )
{
DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForSave: invalid manager!" );
if ( !_rxFilterManager.is() )
return;
OUString sUIName;
OUString sExtension;
// retrieve the default filter for this application module.
// It must be set as first of the generated filter list.
std::shared_ptr<const SfxFilter> pDefaultFilter = SfxFilterContainer::GetDefaultFilter_Impl(_rFactory);
// Only use one extension (#i32434#)
// (and always the first if there are more than one)
sExtension = pDefaultFilter->GetWildcard().getGlob().getToken(0, ';');
sUIName = addExtension( pDefaultFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
try
{
_rxFilterManager->appendFilter( sUIName, sExtension );
if ( _rFirstNonEmpty.isEmpty() )
_rFirstNonEmpty = sUIName;
}
catch( const IllegalArgumentException& )
{
SAL_WARN( "sfx.dialog", "Could not append DefaultFilter" << sUIName );
}
for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
{
if (pFilter->GetName() == pDefaultFilter->GetName())
continue;
// Only use one extension (#i32434#)
// (and always the first if there are more than one)
sExtension = pFilter->GetWildcard().getGlob().getToken(0, ';');
sUIName = addExtension( pFilter->GetUIName(), sExtension, false, _rFileDlgImpl );
try
{
_rxFilterManager->appendFilter( sUIName, sExtension );
if ( _rFirstNonEmpty.isEmpty() )
_rFirstNonEmpty = sUIName;
}
catch( const IllegalArgumentException& )
{
SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
}
}
}
namespace {
struct ExportFilter
{
ExportFilter( const OUString& _aUIName, const OUString& _aWildcard ) :
aUIName( _aUIName ), aWildcard( _aWildcard ) {}
OUString aUIName;
OUString aWildcard;
};
}
void appendExportFilters( TSortedFilterList& _rFilterMatcher,<--- The function 'appendExportFilters' is never used.
const Reference< XFilterManager >& _rxFilterManager,
OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
{
DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendExportFilters: invalid manager!" );
if ( !_rxFilterManager.is() )
return;
sal_Int32 nHTMLIndex = -1;
sal_Int32 nXHTMLIndex = -1;
sal_Int32 nPDFIndex = -1;
OUString sUIName;
OUString sExtensions;
std::vector< ExportFilter > aImportantFilterGroup;
std::vector< ExportFilter > aFilterGroup;
Reference< XFilterGroupManager > xFilterGroupManager( _rxFilterManager, UNO_QUERY );
OUString sTypeName;
for ( std::shared_ptr<const SfxFilter> pFilter = _rFilterMatcher.First(); pFilter; pFilter = _rFilterMatcher.Next() )
{
sTypeName = pFilter->GetTypeName();
sUIName = pFilter->GetUIName();
sExtensions = pFilter->GetWildcard().getGlob();
ExportFilter aExportFilter( sUIName, sExtensions );
if ( nHTMLIndex == -1 &&
( sTypeName == "generic_HTML" || sTypeName == "graphic_HTML" ) )
{
aImportantFilterGroup.insert( aImportantFilterGroup.begin(), aExportFilter );
nHTMLIndex = 0;
}
else if ( nXHTMLIndex == -1 && sTypeName == "XHTML_File" )
{
std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
if ( nHTMLIndex == -1 )
aImportantFilterGroup.insert( aIter, aExportFilter );
else
aImportantFilterGroup.insert( ++aIter, aExportFilter );
nXHTMLIndex = 0;
}
else if ( nPDFIndex == -1 && sTypeName == "pdf_Portable_Document_Format" )
{
std::vector< ExportFilter >::iterator aIter = aImportantFilterGroup.begin();
if ( nHTMLIndex != -1 )
++aIter;
if ( nXHTMLIndex != -1 )
++aIter;
aImportantFilterGroup.insert( aIter, aExportFilter );
nPDFIndex = 0;
}
else
aFilterGroup.push_back( aExportFilter );
}
if ( xFilterGroupManager.is() )
{
// Add both html/pdf filter as a filter group to get a separator between both groups
if ( !aImportantFilterGroup.empty() )
{
Sequence< StringPair > aFilters( aImportantFilterGroup.size() );
for ( sal_Int32 i = 0; i < static_cast<sal_Int32>(aImportantFilterGroup.size()); i++ )
{
aFilters[i].First = addExtension( aImportantFilterGroup[i].aUIName,
aImportantFilterGroup[i].aWildcard,
false, _rFileDlgImpl );
aFilters[i].Second = aImportantFilterGroup[i].aWildcard;
}
try
{
xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
}
catch( const IllegalArgumentException& )
{
}
}
if ( !aFilterGroup.empty() )
{
Sequence< StringPair > aFilters( aFilterGroup.size() );
for ( sal_Int32 i = 0; i < static_cast<sal_Int32>(aFilterGroup.size()); i++ )
{
aFilters[i].First = addExtension( aFilterGroup[i].aUIName,
aFilterGroup[i].aWildcard,
false, _rFileDlgImpl );
aFilters[i].Second = aFilterGroup[i].aWildcard;
}
try
{
xFilterGroupManager->appendFilterGroup( OUString(), aFilters );
}
catch( const IllegalArgumentException& )
{
}
}
}
else
{
// Fallback solution just add both filter groups as single filters
sal_Int32 n;
for ( n = 0; n < static_cast<sal_Int32>(aImportantFilterGroup.size()); n++ )
{
try
{
OUString aUIName = addExtension( aImportantFilterGroup[n].aUIName,
aImportantFilterGroup[n].aWildcard,
false, _rFileDlgImpl );
_rxFilterManager->appendFilter( aUIName, aImportantFilterGroup[n].aWildcard );
if ( _rFirstNonEmpty.isEmpty() )
_rFirstNonEmpty = sUIName;
}
catch( const IllegalArgumentException& )
{
SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
}
}
for ( n = 0; n < static_cast<sal_Int32>(aFilterGroup.size()); n++ )
{
try
{
OUString aUIName = addExtension( aFilterGroup[n].aUIName,
aFilterGroup[n].aWildcard,
false, _rFileDlgImpl );
_rxFilterManager->appendFilter( aUIName, aFilterGroup[n].aWildcard );
if ( _rFirstNonEmpty.isEmpty() )
_rFirstNonEmpty = sUIName;
}
catch( const IllegalArgumentException& )
{
SAL_WARN( "sfx.dialog", "Could not append Filter" << sUIName );
}
}
}
}
void appendFiltersForOpen( TSortedFilterList& _rFilterMatcher,<--- The function 'appendFiltersForOpen' is never used.
const Reference< XFilterManager >& _rxFilterManager,
OUString& _rFirstNonEmpty, FileDialogHelper_Impl& _rFileDlgImpl )
{
DBG_ASSERT( _rxFilterManager.is(), "sfx2::appendFiltersForOpen: invalid manager!" );
if ( !_rxFilterManager.is() )
return;
// group and classify the filters
GroupedFilterList aAllFilters;
lcl_GroupAndClassify( _rFilterMatcher, aAllFilters );
// ensure that we have the one "all files" entry
lcl_EnsureAllFilesEntry( _rFilterMatcher, aAllFilters );
// the first non-empty string - which we assume is the first overall entry
if ( !aAllFilters.empty() )
{
const FilterGroup& rFirstGroup = *aAllFilters.begin(); // should be the global classes
if ( !rFirstGroup.empty() )
_rFirstNonEmpty = rFirstGroup.begin()->First;
// append first group, without extension
AppendFilterGroup aGroup( _rxFilterManager, &_rFileDlgImpl );
aGroup.appendGroup( rFirstGroup, false );
}
// append the filters to the manager
if ( !aAllFilters.empty() )
{
::std::list< FilterGroup >::iterator pIter = aAllFilters.begin();
++pIter;
::std::for_each(
pIter, // first filter group was handled separately, see above
aAllFilters.end(),
AppendFilterGroup( _rxFilterManager, &_rFileDlgImpl ) );
}
}
OUString addExtension( const OUString& _rDisplayText,
const OUString& _rExtension,
bool _bForOpen, FileDialogHelper_Impl& _rFileDlgImpl )
{
OUString sRet = _rDisplayText;
if ( sRet.indexOf( "(*.*)" ) == -1 )
{
OUString sExt = _rExtension;
if ( !_bForOpen )
{
// show '*' in extensions only when opening a document
sExt = sExt.replaceAll("*", "");
}
sRet += " (" + sExt + ")";
}
_rFileDlgImpl.addFilterPair( _rDisplayText, sRet );
return sRet;
}
} // namespace sfx2
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|