summaryrefslogtreecommitdiff
path: root/sfx2/source/doc/guisaveas.cxx
blob: 921d56539d1a985a50dc3173c075641b2f3bc544 (plain)
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
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: guisaveas.cxx,v $
 * $Revision: 1.37 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sfx2.hxx"
#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/ui/dialogs/XFilePicker.hpp>
#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
#include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
#include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
#include <com/sun/star/view/XSelectionSupplier.hpp>
#include <com/sun/star/beans/XPropertyAccess.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XPropertyContainer.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/document/XExporter.hpp>
#include <com/sun/star/document/XDocumentInfoSupplier.hpp>
#include <com/sun/star/document/XDocumentInfo.hpp>
#include <com/sun/star/task/XInteractionHandler.hpp>
#include <com/sun/star/util/DateTime.hpp>
#include <com/sun/star/util/XURLTransformer.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/frame/XStorable2.hpp>
#include <com/sun/star/frame/XDispatchProvider.hpp>
#include <com/sun/star/frame/XDispatch.hpp>
#include <com/sun/star/frame/XTitle.hpp>
#include <com/sun/star/util/XModifyListener.hpp>
#include <com/sun/star/util/XModifiable.hpp>
#include <com/sun/star/util/XModifyBroadcaster.hpp>

#include <com/sun/star/util/XCloneable.hpp>
#include <com/sun/star/frame/XModuleManager.hpp>
#include <com/sun/star/io/IOException.hpp>

#include "guisaveas.hxx"

#include <svtools/pathoptions.hxx>
#include <svtools/pathoptions.hxx>
#include <svtools/itemset.hxx>
#include <svtools/adrparse.hxx>
#include <svtools/useroptions.hxx>
#include <svtools/saveopt.hxx>
#include <tools/debug.hxx>
#include <tools/urlobj.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/configurationhelper.hxx>
#include <vcl/msgbox.hxx>
#include <vcl/window.hxx>
#include <toolkit/awt/vclxwindow.hxx>

#include <sfx2/sfxsids.hrc>
#include <doc.hrc>
#include <sfxresid.hxx>
#include <sfx2/docfilt.hxx>
#include <sfx2/filedlghelper.hxx>
#include <sfx2/app.hxx>
#include <sfx2/objsh.hxx>
#include <sfx2/dinfdlg.hxx>
#include <sfxtypes.hxx>
#include "alienwarn.hxx"

#define DOCPROPSNUM 17

// flags that specify requested operation
#define EXPORT_REQUESTED 			1
#define PDFEXPORT_REQUESTED			2
#define PDFDIRECTEXPORT_REQUESTED	4
#define WIDEEXPORT_REQUESTED		8
#define SAVE_REQUESTED				16
#define SAVEAS_REQUESTED			32

// possible statuses of save operation
#define STATUS_NO_ACTION			0
#define STATUS_SAVE					1
#define STATUS_SAVEAS				2
#define STATUS_SAVEAS_STANDARDNAME	3

const ::rtl::OUString aFilterNameString = ::rtl::OUString::createFromAscii( "FilterName" );
const ::rtl::OUString aFilterOptionsString = ::rtl::OUString::createFromAscii( "FilterOptions" );
const ::rtl::OUString aFilterDataString    = ::rtl::OUString::createFromAscii( "FilterData" );
const ::rtl::OUString aFilterFlagsString   = ::rtl::OUString::createFromAscii( "FilterFlags" );

using namespace ::com::sun::star;

//-------------------------------------------------------------------------
static sal_uInt16 getSlotIDFromMode( sal_Int8 nStoreMode )
{
    // This is a temporary hardcoded solution must be removed when
    // dialogs do not need parameters in SidSet representation any more

    sal_uInt16 nResult = 0;
    if ( nStoreMode == EXPORT_REQUESTED )
        nResult = SID_EXPORTDOC;
    else if ( nStoreMode == ( EXPORT_REQUESTED | PDFEXPORT_REQUESTED ) )
        nResult = SID_EXPORTDOCASPDF;
    else if ( nStoreMode == ( EXPORT_REQUESTED | PDFEXPORT_REQUESTED | PDFDIRECTEXPORT_REQUESTED ) )
        nResult = SID_DIRECTEXPORTDOCASPDF;
    else if ( nStoreMode == SAVEAS_REQUESTED || nStoreMode == ( EXPORT_REQUESTED | WIDEEXPORT_REQUESTED ) )
        nResult = SID_SAVEASDOC;
    else {
        DBG_ASSERT( sal_False, "Unacceptable slot name is provided!\n" );
    }

    return nResult;
}

//-------------------------------------------------------------------------
static sal_uInt8 getStoreModeFromSlotName( const ::rtl::OUString& aSlotName )
{
    sal_uInt8 nResult = 0;
    if ( aSlotName.equalsAscii( "ExportTo" ) )
        nResult = EXPORT_REQUESTED;
    else if ( aSlotName.equalsAscii( "ExportToPDF" ) )
        nResult = EXPORT_REQUESTED | PDFEXPORT_REQUESTED;
    else if ( aSlotName.equalsAscii( "ExportDirectToPDF" ) )
        nResult = EXPORT_REQUESTED | PDFEXPORT_REQUESTED | PDFDIRECTEXPORT_REQUESTED;
    else if ( aSlotName.equalsAscii( "Save" ) )
        nResult = SAVE_REQUESTED;
    else if ( aSlotName.equalsAscii( "SaveAs" ) )
        nResult = SAVEAS_REQUESTED;
    else
        throw task::ErrorCodeIOException( ::rtl::OUString(),
                                            uno::Reference< uno::XInterface >(),
                                            ERRCODE_IO_INVALIDPARAMETER );

    return nResult;
}

//-------------------------------------------------------------------------
static sal_Int32 getMustFlags( sal_Int8 nStoreMode )
{
    return ( SFX_FILTER_EXPORT
            | ( ( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) ) ? 0 : SFX_FILTER_IMPORT ) );
}

//-------------------------------------------------------------------------
static sal_Int32 getDontFlags( sal_Int8 nStoreMode )
{
    return ( SFX_FILTER_INTERNAL
            | SFX_FILTER_NOTINFILEDLG
            | ( ( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) ) ? SFX_FILTER_IMPORT : 0 ) );
}

//=========================================================================
// class ModelData_Impl
//=========================================================================
class ModelData_Impl
{
    SfxStoringHelper* m_pOwner;
    uno::Reference< frame::XModel > m_xModel;
    uno::Reference< frame::XStorable > m_xStorable;
    uno::Reference< frame::XStorable2 > m_xStorable2;
    uno::Reference< util::XModifiable > m_xModifiable;

    ::rtl::OUString m_aModuleName;
    ::comphelper::SequenceAsHashMap* m_pDocumentPropsHM;
    ::comphelper::SequenceAsHashMap* m_pModulePropsHM;

    ::comphelper::SequenceAsHashMap m_aMediaDescrHM;

public:
    ModelData_Impl( SfxStoringHelper& aOwner,
                    const uno::Reference< frame::XModel >& xModel,
                    const uno::Sequence< beans::PropertyValue >& aMediaDescr );

    ~ModelData_Impl();

    void FreeDocumentProps();

    uno::Reference< frame::XModel > GetModel();
    uno::Reference< frame::XStorable > GetStorable();
    uno::Reference< frame::XStorable2 > GetStorable2();
    uno::Reference< util::XModifiable > GetModifiable();

    ::comphelper::SequenceAsHashMap& GetMediaDescr() { return m_aMediaDescrHM; }

    const ::comphelper::SequenceAsHashMap& GetDocProps();

    ::rtl::OUString GetModuleName();
    const ::comphelper::SequenceAsHashMap& GetModuleProps();

    void CheckInteractionHandler();


    ::rtl::OUString GetDocServiceName();
    uno::Sequence< beans::PropertyValue > GetDocServiceDefaultFilterCheckFlags( sal_Int32 nMust, sal_Int32 nDont );
    uno::Sequence< beans::PropertyValue > GetDocServiceAnyFilter( sal_Int32 nMust, sal_Int32 nDont );
    uno::Sequence< beans::PropertyValue > GetPreselectedFilter_Impl( sal_Int8 nStoreMode );
    uno::Sequence< beans::PropertyValue > GetDocServiceDefaultFilter();

    sal_Bool ExecuteFilterDialog_Impl( const ::rtl::OUString& aFilterName );

    sal_Int8 CheckSaveAcceptable( sal_Int8 nCurStatus );
    sal_Int8 CheckStateForSave();

    sal_Int8 CheckFilter( const ::rtl::OUString& );

    sal_Bool CheckFilterOptionsDialogExistence();

    sal_Bool OutputFileDialog( sal_Int8 nStoreMode,
                                const ::comphelper::SequenceAsHashMap& aPreselectedFilterPropsHM,
                                sal_Bool bSetStandardName,
                                ::rtl::OUString& aSuggestedName,
                                sal_Bool bPreselectPassword,
                                const ::rtl::OUString& aSuggestedDir,
                                sal_Int16 nDialog,
                                const ::rtl::OUString& rStandardDir,
                                const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rBlackList
                                );

    sal_Bool ShowDocumentInfoDialog();

    ::rtl::OUString GetReccomendedDir( const ::rtl::OUString& aSuggestedDir,
                                       const sfx2::FileDialogHelper::Context& aCtxt );
    ::rtl::OUString GetReccomendedName( const ::rtl::OUString& aSuggestedName,
                                        const ::rtl::OUString& aTypeName );

};

//-------------------------------------------------------------------------
ModelData_Impl::ModelData_Impl( SfxStoringHelper& aOwner,
                                const uno::Reference< frame::XModel >& xModel,
                                const uno::Sequence< beans::PropertyValue >& aMediaDescr )
: m_pOwner( &aOwner )
, m_xModel( xModel )
, m_pDocumentPropsHM( NULL )
, m_pModulePropsHM( NULL )
, m_aMediaDescrHM( aMediaDescr )
{
    CheckInteractionHandler();
}

//-------------------------------------------------------------------------
ModelData_Impl::~ModelData_Impl()
{
    FreeDocumentProps();
    if ( m_pDocumentPropsHM )
        delete m_pDocumentPropsHM;

    if ( m_pModulePropsHM )
        delete m_pModulePropsHM;
}

//-------------------------------------------------------------------------
void ModelData_Impl::FreeDocumentProps()
{
    if ( m_pDocumentPropsHM )
    {
        delete m_pDocumentPropsHM;
        m_pDocumentPropsHM = NULL;
    }
}

//-------------------------------------------------------------------------
uno::Reference< frame::XModel > ModelData_Impl::GetModel()
{
    if ( !m_xModel.is() )
        throw uno::RuntimeException();

    return m_xModel;
}

//-------------------------------------------------------------------------
uno::Reference< frame::XStorable > ModelData_Impl::GetStorable()
{
    if ( !m_xStorable.is() )
    {
        m_xStorable = uno::Reference< frame::XStorable >( m_xModel, uno::UNO_QUERY );
        if ( !m_xStorable.is() )
            throw uno::RuntimeException();
    }

    return m_xStorable;
}

//-------------------------------------------------------------------------
uno::Reference< frame::XStorable2 > ModelData_Impl::GetStorable2()
{
    if ( !m_xStorable2.is() )
    {
        m_xStorable2 = uno::Reference< frame::XStorable2 >( m_xModel, uno::UNO_QUERY );
        if ( !m_xStorable2.is() )
            throw uno::RuntimeException();
    }

    return m_xStorable2;
}

//-------------------------------------------------------------------------
uno::Reference< util::XModifiable > ModelData_Impl::GetModifiable()
{
    if ( !m_xModifiable.is() )
    {
        m_xModifiable = uno::Reference< util::XModifiable >( m_xModel, uno::UNO_QUERY );
        if ( !m_xModifiable.is() )
            throw uno::RuntimeException();
    }

    return m_xModifiable;
}

//-------------------------------------------------------------------------
const ::comphelper::SequenceAsHashMap& ModelData_Impl::GetDocProps()
{
    if ( !m_pDocumentPropsHM )
        m_pDocumentPropsHM = new ::comphelper::SequenceAsHashMap( GetModel()->getArgs() );

    return *m_pDocumentPropsHM;
}

//-------------------------------------------------------------------------
::rtl::OUString ModelData_Impl::GetModuleName()
{
    if ( !m_aModuleName.getLength() )
    {
        m_aModuleName = m_pOwner->GetModuleManager()->identify(
                                                uno::Reference< uno::XInterface >( m_xModel, uno::UNO_QUERY ) );
        if ( !m_aModuleName.getLength() )
            throw uno::RuntimeException(); // TODO:
    }
    return m_aModuleName;
}

//-------------------------------------------------------------------------
const ::comphelper::SequenceAsHashMap& ModelData_Impl::GetModuleProps()
{
    if ( !m_pModulePropsHM )
    {
        uno::Sequence< beans::PropertyValue > aModuleProps;
        m_pOwner->GetNamedModuleManager()->getByName( GetModuleName() ) >>= aModuleProps;
        if ( !aModuleProps.getLength() )
            throw uno::RuntimeException(); // TODO;
        m_pModulePropsHM = new ::comphelper::SequenceAsHashMap( aModuleProps );
    }

    return *m_pModulePropsHM;
}

//-------------------------------------------------------------------------
::rtl::OUString ModelData_Impl::GetDocServiceName()
{
    return GetModuleProps().getUnpackedValueOrDefault(::rtl::OUString::createFromAscii( "ooSetupFactoryDocumentService" ), ::rtl::OUString());
}

//-------------------------------------------------------------------------
void ModelData_Impl::CheckInteractionHandler()
{
    ::comphelper::SequenceAsHashMap::const_iterator aInteractIter =
            m_aMediaDescrHM.find( ::rtl::OUString::createFromAscii( "InteractionHandler" ) );

    if ( aInteractIter == m_aMediaDescrHM.end() )
    {
        try {
            m_aMediaDescrHM[ ::rtl::OUString::createFromAscii( "InteractionHandler" ) ]
                <<= uno::Reference< task::XInteractionHandler >(
                            m_pOwner->GetServiceFactory()->createInstance(
                                            DEFINE_CONST_UNICODE("com.sun.star.task.InteractionHandler") ),
                            uno::UNO_QUERY );
        }
        catch( uno::Exception& )
        {
        }
    }
    else
    {
        uno::Reference< task::XInteractionHandler > xInteract;
        DBG_ASSERT( ( aInteractIter->second >>= xInteract ) && xInteract.is(), "Broken interaction handler is provided!\n" );
    }
}

//-------------------------------------------------------------------------
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetDocServiceDefaultFilter()
{
    uno::Sequence< beans::PropertyValue > aProps;

    ::rtl::OUString aFilterName = GetModuleProps().getUnpackedValueOrDefault(
                                                                ::rtl::OUString::createFromAscii( "ooSetupFactoryDefaultFilter" ),
                                                                ::rtl::OUString() );

    m_pOwner->GetFilterConfiguration()->getByName( aFilterName ) >>= aProps;

    return aProps;
}

//-------------------------------------------------------------------------
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetDocServiceDefaultFilterCheckFlags( sal_Int32 nMust,
                                                                                                sal_Int32 nDont )
{
    uno::Sequence< beans::PropertyValue > aFilterProps;
    uno::Sequence< beans::PropertyValue > aProps = GetDocServiceDefaultFilter();
    if ( aProps.getLength() )
    {
        ::comphelper::SequenceAsHashMap aFiltHM( aProps );
        sal_Int32 nFlags = aFiltHM.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "Flags" ),
                                                        (sal_Int32)0 );
        if ( ( ( nFlags & nMust ) == nMust ) && !( nFlags & nDont ) )
            aFilterProps = aProps;
    }

    return aFilterProps;
}


//-------------------------------------------------------------------------
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetDocServiceAnyFilter( sal_Int32 nMust, sal_Int32 nDont )
{
    uno::Sequence< beans::NamedValue > aSearchRequest( 1 );
    aSearchRequest[0].Name = ::rtl::OUString::createFromAscii( "DocumentService" );
    aSearchRequest[0].Value <<= GetDocServiceName();

    return SfxStoringHelper::SearchForFilter( m_pOwner->GetFilterQuery(), aSearchRequest, nMust, nDont );
}

//-------------------------------------------------------------------------
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetPreselectedFilter_Impl( sal_Int8 nStoreMode )
{
    uno::Sequence< beans::PropertyValue > aFilterProps;

    sal_Int32 nMust = getMustFlags( nStoreMode );
    sal_Int32 nDont = getDontFlags( nStoreMode );

    if ( nStoreMode & PDFEXPORT_REQUESTED )
    {
        // Preselect PDF-Filter for EXPORT
        uno::Sequence< beans::NamedValue > aSearchRequest( 2 );
        aSearchRequest[0].Name = ::rtl::OUString::createFromAscii( "Type" );
        aSearchRequest[0].Value <<= ::rtl::OUString::createFromAscii( "pdf_Portable_Document_Format" );
        aSearchRequest[1].Name = ::rtl::OUString::createFromAscii( "DocumentService" );
        aSearchRequest[1].Value <<= GetDocServiceName();

        aFilterProps = SfxStoringHelper::SearchForFilter( m_pOwner->GetFilterQuery(), aSearchRequest, nMust, nDont );
    }
    else
    {
        aFilterProps = GetDocServiceDefaultFilterCheckFlags( nMust, nDont );

        if ( !aFilterProps.getLength() )
        {
            // the default filter was not faund, use just the first acceptable one
            aFilterProps = GetDocServiceAnyFilter( nMust, nDont );
        }
    }

    return aFilterProps;
}

//-------------------------------------------------------------------------
sal_Bool ModelData_Impl::ExecuteFilterDialog_Impl( const ::rtl::OUString& aFilterName )
{
    sal_Bool bDialogUsed = sal_False;

    try {
        uno::Sequence < beans::PropertyValue > aProps;
          uno::Any aAny = m_pOwner->GetFilterConfiguration()->getByName( aFilterName );
           if ( aAny >>= aProps )
           {
               sal_Int32 nPropertyCount = aProps.getLength();
               for( sal_Int32 nProperty=0; nProperty < nPropertyCount; ++nProperty )
                   if( aProps[nProperty].Name.equals( ::rtl::OUString::createFromAscii("UIComponent")) )
                   {
                    ::rtl::OUString aServiceName;
                       aProps[nProperty].Value >>= aServiceName;
                    if( aServiceName.getLength() )
                    {
                        uno::Reference< ui::dialogs::XExecutableDialog > xFilterDialog(
                                                    m_pOwner->GetServiceFactory()->createInstance( aServiceName ), uno::UNO_QUERY );
                        uno::Reference< beans::XPropertyAccess > xFilterProperties( xFilterDialog, uno::UNO_QUERY );

                        if( xFilterDialog.is() && xFilterProperties.is() )
                        {
                            bDialogUsed = sal_True;

                            uno::Reference< document::XExporter > xExporter( xFilterDialog, uno::UNO_QUERY );
                            if( xExporter.is() )
                                xExporter->setSourceDocument(
                                    uno::Reference< lang::XComponent >( GetModel(), uno::UNO_QUERY ) );

                            uno::Sequence< beans::PropertyValue > aPropsForDialog;
                            GetMediaDescr() >> aPropsForDialog;
                            xFilterProperties->setPropertyValues( aPropsForDialog );

                            if( xFilterDialog->execute() )
                            {
                                uno::Sequence< beans::PropertyValue > aPropsFromDialog =
                                                                            xFilterProperties->getPropertyValues();
                                for ( sal_Int32 nInd = 0; nInd < aPropsFromDialog.getLength(); nInd++ )
                                    GetMediaDescr()[aPropsFromDialog[nInd].Name] = aPropsFromDialog[nInd].Value;
                            }
                            else
                            {
                                throw task::ErrorCodeIOException( ::rtl::OUString(),
                                                                    uno::Reference< uno::XInterface >(),
                                                                    ERRCODE_IO_ABORT );
                            }
                        }
                    }

                    break;
                }
        }
    }
    catch( container::NoSuchElementException& )
    {
        // the filter name is unknown
        throw task::ErrorCodeIOException( ::rtl::OUString(),
                                            uno::Reference< uno::XInterface >(),
                                            ERRCODE_IO_INVALIDPARAMETER );
    }
    catch( task::ErrorCodeIOException& )
    {
        throw;
    }
    catch( uno::Exception& )
    {
    }

    return bDialogUsed;
}

//-------------------------------------------------------------------------
sal_Int8 ModelData_Impl::CheckSaveAcceptable( sal_Int8 nCurStatus )
{
    sal_Int8 nResult = nCurStatus;

    if ( nResult != STATUS_NO_ACTION && GetStorable()->hasLocation() )
    {
        // check whether save is acceptable by the configuration
        // it is done only for documents that have persistence already
        uno::Reference< uno::XInterface > xCommonConfig = ::comphelper::ConfigurationHelper::openConfig(
                            m_pOwner->GetServiceFactory(),
                            ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Common" ) ),
                            ::comphelper::ConfigurationHelper::E_STANDARD );
        if ( !xCommonConfig.is() )
            throw uno::RuntimeException(); // should the saving proceed as usual instead?

        try
        {
            sal_Bool bAlwaysSaveAs = sal_False;

            // the saving is acceptable
            // in case the configuration entry is not set or set to false
            // or in case of version creation
            ::rtl::OUString aVersionCommentString = ::rtl::OUString::createFromAscii( "VersionComment" );
            if ( ( ::comphelper::ConfigurationHelper::readRelativeKey(
                    xCommonConfig,
                    ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Save/Document/" ) ),
                    ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AlwaysSaveAs" ) ) ) >>= bAlwaysSaveAs )
              && bAlwaysSaveAs
            && GetMediaDescr().find( aVersionCommentString ) == GetMediaDescr().end() )
            {
                // notify the user that SaveAs is going to be done
                String aString( SfxResId( STR_NEW_FILENAME_SAVE ) );
                Window* pWin = SfxStoringHelper::GetModelWindow( m_xModel );
                QueryBox aMessageBox( pWin, WB_OK_CANCEL | WB_DEF_OK, aString );
                if ( aMessageBox.Execute() == RET_OK )
                    nResult = STATUS_SAVEAS;
                else
                    nResult = STATUS_NO_ACTION;
            }
        }
        catch( uno::Exception& )
        {
            // impossibility to get the configuration access means normal saving flow for now
        }
    }

    return nResult;
}

//-------------------------------------------------------------------------
sal_Int8 ModelData_Impl::CheckStateForSave()
{
    // check acceptable entries for media descriptor
    sal_Bool bVersInfoNeedsStore = sal_False;
    ::comphelper::SequenceAsHashMap aAcceptedArgs;

    ::rtl::OUString aVersionCommentString = ::rtl::OUString::createFromAscii( "VersionComment" );
    ::rtl::OUString aAuthorString = ::rtl::OUString::createFromAscii( "Author" );
    ::rtl::OUString aInteractionHandlerString = ::rtl::OUString::createFromAscii( "InteractionHandler" );
    ::rtl::OUString aStatusIndicatorString = ::rtl::OUString::createFromAscii( "StatusIndicator" );

    if ( GetMediaDescr().find( aVersionCommentString ) != GetMediaDescr().end() )
    {
        bVersInfoNeedsStore = sal_True;
        aAcceptedArgs[ aVersionCommentString ] = GetMediaDescr()[ aVersionCommentString ];
    }
    if ( GetMediaDescr().find( aAuthorString ) != GetMediaDescr().end() )
        aAcceptedArgs[ aAuthorString ] = GetMediaDescr()[ aAuthorString ];
    if ( GetMediaDescr().find( aInteractionHandlerString ) != GetMediaDescr().end() )
        aAcceptedArgs[ aInteractionHandlerString ] = GetMediaDescr()[ aInteractionHandlerString ];
    if ( GetMediaDescr().find( aStatusIndicatorString ) != GetMediaDescr().end() )
        aAcceptedArgs[ aStatusIndicatorString ] = GetMediaDescr()[ aStatusIndicatorString ];

    // remove unacceptable entry if there is any
    DBG_ASSERT( GetMediaDescr().size() == aAcceptedArgs.size(),
                "Unacceptable parameters are provided in Save request!\n" );
    if ( GetMediaDescr().size() != aAcceptedArgs.size() )
        GetMediaDescr() = aAcceptedArgs;

    // the document must be modified
    if ( !GetModifiable()->isModified() && !bVersInfoNeedsStore )
        return STATUS_NO_ACTION;

    // if the document is readonly or a new one a SaveAs operation must be used
    if ( !GetStorable()->hasLocation() || GetStorable()->isReadonly() )
        return STATUS_SAVEAS;

    // check that the old filter is acceptable
    ::rtl::OUString aOldFilterName = GetDocProps().getUnpackedValueOrDefault(
                                                    aFilterNameString,
                                                    ::rtl::OUString() );
    sal_Int8 nResult = CheckFilter( aOldFilterName );

    return nResult;
}

sal_Int8 ModelData_Impl::CheckFilter( const ::rtl::OUString& aFilterName )
{
    ::comphelper::SequenceAsHashMap aFiltPropsHM;
    sal_Int32 nFiltFlags = 0;
    if ( aFilterName.getLength() )
    {
        // get properties of filter
        uno::Sequence< beans::PropertyValue > aFilterProps;
        if ( aFilterName.getLength() )
            m_pOwner->GetFilterConfiguration()->getByName( aFilterName ) >>= aFilterProps;

        aFiltPropsHM = ::comphelper::SequenceAsHashMap( aFilterProps );
        nFiltFlags = aFiltPropsHM.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "Flags" ), (sal_Int32)0 );
    }

    // only a temporary solution until default filter retrieving feature is implemented
    // then GetDocServiceDefaultFilter() must be used
    ::comphelper::SequenceAsHashMap aDefFiltPropsHM = GetDocServiceDefaultFilterCheckFlags( 3, 0 );
    sal_Int32 nDefFiltFlags = aDefFiltPropsHM.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "Flags" ), (sal_Int32)0 );

    // if the old filter is not acceptable
    // and there is no default filter or it is not acceptable for requested parameters then proceed with saveAs
    if ( ( !aFiltPropsHM.size() || !( nFiltFlags & SFX_FILTER_EXPORT ) )
      && ( !aDefFiltPropsHM.size() || !( nDefFiltFlags & SFX_FILTER_EXPORT ) || nDefFiltFlags & SFX_FILTER_INTERNAL ) )
        return STATUS_SAVEAS;

    // so at this point there is either an acceptable old filter or default one
    if ( !aFiltPropsHM.size() || !( nFiltFlags & SFX_FILTER_EXPORT ) )
    {
        // so the default filter must be acceptable
        return STATUS_SAVEAS_STANDARDNAME;
    }
    else if ( ( !( nFiltFlags & SFX_FILTER_OWN ) || ( nFiltFlags & SFX_FILTER_ALIEN ) )
           && !( nFiltFlags & SFX_FILTER_SILENTEXPORT ) && aDefFiltPropsHM.size()
           && ( nDefFiltFlags & SFX_FILTER_EXPORT ) && !( nDefFiltFlags & SFX_FILTER_INTERNAL ))
    {
        // the default filter is acceptable and the old filter is alian one
        // so ask to make a saveAs operation
        ::rtl::OUString aUIName = aFiltPropsHM.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "UIName" ),
                                                                                ::rtl::OUString() );
        ::rtl::OUString aDefUIName = aDefFiltPropsHM.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "UIName" ),
                                                                                ::rtl::OUString() );
        ::rtl::OUString aPreusedFilterName = GetDocProps().getUnpackedValueOrDefault(
                                                    ::rtl::OUString::createFromAscii( "PreusedFilterName" ),
                                                    ::rtl::OUString() );
        if ( !aPreusedFilterName.equals( aFilterName ) && !aUIName.equals( aDefUIName ) )
        {
            if ( !SfxStoringHelper::WarnUnacceptableFormat( GetModel(), aUIName, aDefUIName, sal_True ) )
                return STATUS_SAVEAS_STANDARDNAME;
        }
    }

    return STATUS_SAVE;
}

//-------------------------------------------------------------------------
sal_Bool ModelData_Impl::CheckFilterOptionsDialogExistence()
{
    uno::Sequence< beans::NamedValue > aSearchRequest( 1 );
    aSearchRequest[0].Name = ::rtl::OUString::createFromAscii( "DocumentService" );
    aSearchRequest[0].Value <<= GetDocServiceName();

    uno::Reference< container::XEnumeration > xFilterEnum =
                                    m_pOwner->GetFilterQuery()->createSubSetEnumerationByProperties( aSearchRequest );

    while ( xFilterEnum->hasMoreElements() )
    {
        uno::Sequence< beans::PropertyValue > pProps;
        if ( xFilterEnum->nextElement() >>= pProps )
        {
            ::comphelper::SequenceAsHashMap aPropsHM( pProps );
            ::rtl::OUString aUIServName = aPropsHM.getUnpackedValueOrDefault(
                                            ::rtl::OUString::createFromAscii( "UIComponent" ),
                                            ::rtl::OUString() );
            if ( aUIServName.getLength() )
                return sal_True;
        }
    }

    return sal_False;
}

//-------------------------------------------------------------------------
sal_Bool ModelData_Impl::OutputFileDialog( sal_Int8 nStoreMode,
                                            const ::comphelper::SequenceAsHashMap& aPreselectedFilterPropsHM,
                                            sal_Bool bSetStandardName,
                                            ::rtl::OUString& aSuggestedName,
                                            sal_Bool bPreselectPassword,
                                            const ::rtl::OUString& aSuggestedDir,
                                            sal_Int16 nDialog,
                                            const ::rtl::OUString& rStandardDir,
                                            const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rBlackList)
{
    sal_Bool bUseFilterOptions = sal_False;

    ::comphelper::SequenceAsHashMap::const_iterator aOverwriteIter =
                GetMediaDescr().find( ::rtl::OUString::createFromAscii( "Overwrite" ) );

    // the file name must be specified if overwrite option is set
    if ( aOverwriteIter != GetMediaDescr().end() )
           throw task::ErrorCodeIOException( ::rtl::OUString(),
                                            uno::Reference< uno::XInterface >(),
                                            ERRCODE_IO_INVALIDPARAMETER );

    // no target file name is specified
    // we need to show the file dialog

    // check if we have a filter which allows for filter options, so we need a corresponding checkbox in the dialog
    sal_Bool bAllowOptions = sal_False;

    // in case of Export, filter options dialog is used if available
    if( !( nStoreMode & EXPORT_REQUESTED ) || ( nStoreMode & WIDEEXPORT_REQUESTED ) )
        bAllowOptions = CheckFilterOptionsDialogExistence();

    // get the filename by dialog ...
    // create the file dialog
    sal_Int16  aDialogMode = bAllowOptions
        ? (com::sun::star::ui::dialogs::TemplateDescription::
           FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS)
        : (com::sun::star::ui::dialogs::TemplateDescription::
           FILESAVE_AUTOEXTENSION_PASSWORD);
    sal_Int64 aDialogFlags = 0;

    if( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) )
    {
        if ( nStoreMode & PDFEXPORT_REQUESTED )
            aDialogMode = com::sun::star::ui::dialogs::TemplateDescription::
                FILESAVE_AUTOEXTENSION;
        else
            aDialogMode = com::sun::star::ui::dialogs::TemplateDescription::
                FILESAVE_AUTOEXTENSION_SELECTION;
        aDialogFlags = SFXWB_EXPORT;
    }

    sfx2::FileDialogHelper* pFileDlg = NULL;

    ::rtl::OUString aDocServiceName = GetDocServiceName();
    DBG_ASSERT( aDocServiceName.getLength(), "No document service for this module set!" );

    sal_Int32 nMust = getMustFlags( nStoreMode );
    sal_Int32 nDont = getDontFlags( nStoreMode );
    sfx2::FileDialogHelper::Context eCtxt = sfx2::FileDialogHelper::UNKNOWN_CONTEXT;

    if ( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) )
    {
        if ( ( nStoreMode & PDFEXPORT_REQUESTED ) && aPreselectedFilterPropsHM.size() )
        {
            // this is a PDF export
            // the filter options has been shown already
            ::rtl::OUString aFilterUIName = aPreselectedFilterPropsHM.getUnpackedValueOrDefault(
                                                        ::rtl::OUString::createFromAscii( "UIName" ),
                                                        ::rtl::OUString() );

            pFileDlg = new sfx2::FileDialogHelper( aDialogMode, aDialogFlags, aFilterUIName, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "pdf" ) ), rStandardDir, rBlackList );
            pFileDlg->SetCurrentFilter( aFilterUIName );
        }
        else
        {
            // This is the normal dialog
            pFileDlg = new sfx2::FileDialogHelper( aDialogMode, aDialogFlags, aDocServiceName, nDialog, nMust, nDont, rStandardDir, rBlackList );
        }

        if( aDocServiceName.equalsAscii( "com.sun.star.drawing.DrawingDocument" ) )
               eCtxt = sfx2::FileDialogHelper::SD_EXPORT;
        if( aDocServiceName.equalsAscii( "com.sun.star.presentation.PresentationDocument" ) )
               eCtxt = sfx2::FileDialogHelper::SI_EXPORT;
        if( aDocServiceName.equalsAscii( "com.sun.star.text.TextDocument" ) )
            eCtxt = sfx2::FileDialogHelper::SW_EXPORT;
        
        if ( eCtxt != sfx2::FileDialogHelper::UNKNOWN_CONTEXT )
               pFileDlg->SetContext( eCtxt );

        pFileDlg->CreateMatcher( aDocServiceName );

        uno::Reference< ui::dialogs::XFilePicker > xFilePicker = pFileDlg->GetFilePicker();
        uno::Reference< ui::dialogs::XFilePickerControlAccess > xControlAccess =
        uno::Reference< ui::dialogs::XFilePickerControlAccess >( xFilePicker, uno::UNO_QUERY );

        if ( xControlAccess.is() )
        {
            ::rtl::OUString aCtrlText = String( SfxResId( STR_EXPORTBUTTON ) );
            xControlAccess->setLabel( ui::dialogs::CommonFilePickerElementIds::PUSHBUTTON_OK, aCtrlText );
    
            aCtrlText = ::rtl::OUString( String( SfxResId( STR_LABEL_FILEFORMAT ) ) );
            xControlAccess->setLabel( ui::dialogs::CommonFilePickerElementIds::LISTBOX_FILTER_LABEL, aCtrlText );
        }
    }
    else
    {
        // This is the normal dialog
        pFileDlg = new sfx2::FileDialogHelper( aDialogMode, aDialogFlags, aDocServiceName, nDialog, nMust, nDont, rStandardDir, rBlackList );
        pFileDlg->CreateMatcher( aDocServiceName );
    }

    ::rtl::OUString aAdjustToType;

    // bSetStandardName == true means that user agreed to store document in the default (default default ;-)) format
    if ( !(( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED )) && 
        ( bSetStandardName || GetStorable()->hasLocation() ))
    {
        uno::Sequence< beans::PropertyValue > aOldFilterProps;
        ::rtl::OUString aOldFilterName = GetDocProps().getUnpackedValueOrDefault(
                                                        aFilterNameString,
                                                        ::rtl::OUString() );

        if ( aOldFilterName.getLength() )
            m_pOwner->GetFilterConfiguration()->getByName( aOldFilterName ) >>= aOldFilterProps;

        ::comphelper::SequenceAsHashMap aOldFiltPropsHM( aOldFilterProps );
        sal_Int32 nOldFiltFlags = aOldFiltPropsHM.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "Flags" ), (sal_Int32)0 );

        if ( bSetStandardName || ( nOldFiltFlags & nMust ) != nMust || nOldFiltFlags & nDont )
        {
            // the suggested type will be changed, the extension should be adjusted
            aAdjustToType = aPreselectedFilterPropsHM.getUnpackedValueOrDefault(
                                            ::rtl::OUString::createFromAscii( "Type" ),
                                            ::rtl::OUString() );

            ::rtl::OUString aFilterUIName = aPreselectedFilterPropsHM.getUnpackedValueOrDefault(
                                            ::rtl::OUString::createFromAscii( "UIName" ),
                                            ::rtl::OUString() );
            pFileDlg->SetCurrentFilter( aFilterUIName );
        }
        else
        {
            pFileDlg->SetCurrentFilter( aOldFiltPropsHM.getUnpackedValueOrDefault(
                                                        ::rtl::OUString::createFromAscii( "UIName" ),
                                                        ::rtl::OUString() ) );
        }
    }

    ::rtl::OUString aReccomendedDir = GetReccomendedDir( aSuggestedDir, eCtxt );
    if ( aReccomendedDir.getLength() )
        pFileDlg->SetDisplayDirectory( aReccomendedDir );
    ::rtl::OUString aReccomendedName = GetReccomendedName( aSuggestedName, aAdjustToType );
    if ( aReccomendedName.getLength() )
        pFileDlg->SetFileName( aReccomendedName );

    uno::Reference < view::XSelectionSupplier > xSel( GetModel()->getCurrentController(), uno::UNO_QUERY );
    if ( xSel.is() && xSel->getSelection().hasValue() )
        GetMediaDescr()[::rtl::OUString::createFromAscii( "SelectionOnly" )] <<= sal_True;

    // This is a temporary hardcoded solution must be removed when
    // dialogs do not need parameters in SidSet representation any more
    sal_uInt16 nSlotID = getSlotIDFromMode( nStoreMode );
    if ( !nSlotID )
        throw lang::IllegalArgumentException(); // TODO:

    // generate SidSet from MediaDescriptor and provide it into FileDialog
    // than merge changed SidSet back
    SfxAllItemSet aDialogParams( SFX_APP()->GetPool() );
    SfxItemSet* pDialogParams = &aDialogParams;
    TransformParameters( nSlotID,
                         GetMediaDescr().getAsConstPropertyValueList(),
                         aDialogParams,
                         NULL );

    const SfxPoolItem* pItem = NULL;
    if ( bPreselectPassword && aDialogParams.GetItemState( SID_PASSWORD, sal_True, &pItem ) != SFX_ITEM_SET )
    {
        // the file dialog preselects the password checkbox if the provided mediadescriptor has password entry
        // after dialog execution the password entry will be either removed or replaced with the password
        // entered by the user
        aDialogParams.Put( SfxStringItem( SID_PASSWORD, String() ) );
    }

    // aStringTypeFN is a pure output parameter, pDialogParams is an in/out parameter
    String aStringTypeFN;
    if ( pFileDlg->Execute( pDialogParams, aStringTypeFN ) != ERRCODE_NONE )
    {
        delete pFileDlg;
        throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), ERRCODE_IO_ABORT );
    }

    ::rtl::OUString aFilterName = aStringTypeFN;

    uno::Sequence< beans::PropertyValue > aPropsFromDialog;
    TransformItems( nSlotID, *pDialogParams, aPropsFromDialog, NULL );
    GetMediaDescr() << aPropsFromDialog;

    // get the path from the dialog
    INetURLObject aURL( pFileDlg->GetPath() );
    // the path should be provided outside since it might be used for further calls to the dialog
    aSuggestedName = aURL.GetName( INetURLObject::DECODE_WITH_CHARSET );

    // old filter options should be cleared in case different filter is used

    ::rtl::OUString aFilterFromMediaDescr = GetMediaDescr().getUnpackedValueOrDefault(
                                                    aFilterNameString,
                                                    ::rtl::OUString() );
    ::rtl::OUString aOldFilterName = GetDocProps().getUnpackedValueOrDefault(
                                                    aFilterNameString,
                                                    ::rtl::OUString() );
    if ( aFilterName.equals( aFilterFromMediaDescr ) )
    {
        // preserv current settings if any
        // if there no current settings and the name is the same
        // as old filter name use old filter settings

        if ( aFilterFromMediaDescr.equals( aOldFilterName ) )
        {
            ::comphelper::SequenceAsHashMap::const_iterator aIter =
                                        GetDocProps().find( aFilterOptionsString );
            if ( aIter != GetDocProps().end()
              && GetMediaDescr().find( aFilterOptionsString ) == GetMediaDescr().end() )
                GetMediaDescr()[aIter->first] = aIter->second;

            aIter = GetDocProps().find( aFilterDataString );
            if ( aIter != GetDocProps().end()
              && GetMediaDescr().find( aFilterDataString ) == GetMediaDescr().end() )
                GetMediaDescr()[aIter->first] = aIter->second;
        }
    }
    else
    {
        GetMediaDescr().erase( aFilterDataString );
        GetMediaDescr().erase( aFilterOptionsString );

        if ( aFilterName.equals( aOldFilterName ) )
        {
            // merge filter option of the document filter

            ::comphelper::SequenceAsHashMap::const_iterator aIter =
                                GetDocProps().find( aFilterOptionsString );
            if ( aIter != GetDocProps().end() )
                GetMediaDescr()[aIter->first] = aIter->second;

            aIter = GetDocProps().find( aFilterDataString );
            if ( aIter != GetDocProps().end() )
                GetMediaDescr()[aIter->first] = aIter->second;
        }
    }

    uno::Reference< ui::dialogs::XFilePickerControlAccess > xExtFileDlg( pFileDlg->GetFilePicker(), uno::UNO_QUERY );
    if ( xExtFileDlg.is() )
    {
        if ( SfxStoringHelper::CheckFilterOptionsAppearence( m_pOwner->GetFilterConfiguration(), aFilterName ) )
            bUseFilterOptions = sal_True;

        if ( ( !( nStoreMode & EXPORT_REQUESTED ) || ( nStoreMode & WIDEEXPORT_REQUESTED ) ) && bUseFilterOptions )
        {
            try
            {
                // for exporters: always show dialog if format uses options
                // for save: show dialog if format uses options and no options given or if forced by user
                uno::Any aVal =
                        xExtFileDlg->getValue( ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS, 0 );

                aVal >>= bUseFilterOptions;
                if ( !bUseFilterOptions )
                    bUseFilterOptions =
                      ( GetMediaDescr().find( aFilterDataString ) == GetMediaDescr().end()
                      && GetMediaDescr().find( aFilterOptionsString ) == GetMediaDescr().end() );
            }
            catch( lang::IllegalArgumentException& )
            {}
        }
    }

    delete pFileDlg;

    // merge in results of the dialog execution
    GetMediaDescr()[::rtl::OUString::createFromAscii( "URL" )] <<=
                                                ::rtl::OUString( aURL.GetMainURL( INetURLObject::NO_DECODE ));
    GetMediaDescr()[aFilterNameString] <<= aFilterName;

    return bUseFilterOptions;
}

//-------------------------------------------------------------------------
sal_Bool ModelData_Impl::ShowDocumentInfoDialog()
{
    sal_Bool bDialogUsed = sal_False;

    try {
        uno::Reference< frame::XController > xController = GetModel()->getCurrentController();
        if ( xController.is() )
        {
            uno::Reference< frame::XDispatchProvider > xFrameDispatch( xController->getFrame(), uno::UNO_QUERY );
            if ( xFrameDispatch.is() )
            {
                util::URL aURL;
                aURL.Complete = ::rtl::OUString::createFromAscii( ".uno:SetDocumentProperties" );

                uno::Reference< util::XURLTransformer > xTransformer(
                            m_pOwner->GetServiceFactory()->createInstance(
                                            DEFINE_CONST_UNICODE("com.sun.star.util.URLTransformer") ),
                            uno::UNO_QUERY );
                if ( xTransformer.is() && xTransformer->parseStrict( aURL ) )
                {
                    uno::Reference< frame::XDispatch > xDispatch = xFrameDispatch->queryDispatch(
                                                                                aURL,
                                                                                ::rtl::OUString::createFromAscii( "_self" ),
                                                                                0 );
                    if ( xDispatch.is() )
                    {
                        xDispatch->dispatch( aURL, uno::Sequence< beans::PropertyValue >() );
                        bDialogUsed = sal_True;
                    }
                }
            }
        }
    }
    catch ( uno::Exception& )
    {
    }

    return bDialogUsed;
}

//-------------------------------------------------------------------------
::rtl::OUString ModelData_Impl::GetReccomendedDir( const ::rtl::OUString& aSuggestedDir, const sfx2::FileDialogHelper::Context& aCtxt )
{
    ::rtl::OUString aReccomendedDir;

    if ( ( aSuggestedDir.getLength() || GetStorable()->hasLocation() )
      && !GetMediaDescr().getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "RepairPackage" ),
                                                                      sal_False ) )
    {
        INetURLObject aLocation;
        if ( aSuggestedDir.getLength() )
            aLocation = INetURLObject( aSuggestedDir );
        else
        {
            ::rtl::OUString aOldURL = GetStorable()->getLocation();
            if ( aOldURL.getLength() )
            {
                INetURLObject aTmp( aOldURL );
                if ( aTmp.removeSegment() )
                    aLocation = aTmp;
            }

            if ( aLocation.HasError() )
                aLocation = INetURLObject( SvtPathOptions().GetWorkPath() );
        }

        aLocation.setFinalSlash();
        if ( !aLocation.HasError() )
            aReccomendedDir = aLocation.GetMainURL( INetURLObject::NO_DECODE );
    }
    else
    {
        // pb: set graphic path if context == SD_EXPORT or SI_EXPORT else work path
        ::rtl::OUString aConfigSuggestion( ( aCtxt != sfx2::FileDialogHelper::UNKNOWN_CONTEXT ) ? SvtPathOptions().GetGraphicPath() : SvtPathOptions().GetWorkPath() );
        aReccomendedDir = INetURLObject( aConfigSuggestion ).GetMainURL( INetURLObject::NO_DECODE );
    }
    
    return aReccomendedDir;
}

//-------------------------------------------------------------------------
::rtl::OUString ModelData_Impl::GetReccomendedName( const ::rtl::OUString& aSuggestedName, const ::rtl::OUString& aTypeName )
{
    // the last used name might be provided by aSuggestedName from the old selection, or from the MediaDescriptor
    ::rtl::OUString aReccomendedName;

    if ( aSuggestedName.getLength() )
        aReccomendedName = aSuggestedName;
    else
    {
        aReccomendedName = INetURLObject( GetStorable()->getLocation() ).GetName( INetURLObject::DECODE_WITH_CHARSET );
        if ( !aReccomendedName.getLength() )
        {
            try {
                uno::Reference< frame::XTitle > xTitle( GetModel(), uno::UNO_QUERY_THROW );
                aReccomendedName = xTitle->getTitle();
            } catch( uno::Exception& ) {}
        }

        if ( aReccomendedName.getLength() && aTypeName.getLength() )
        {
            // adjust the extension to the type
            uno::Reference< container::XNameAccess > xTypeDetection = uno::Reference< container::XNameAccess >(
                m_pOwner->GetServiceFactory()->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.document.TypeDetection" ) ),
                uno::UNO_QUERY );
            if ( xTypeDetection.is() )
            {
                INetURLObject aObj( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "file:///c:/" ) ) + aReccomendedName );

                uno::Sequence< beans::PropertyValue > aTypeNameProps;
                if ( ( xTypeDetection->getByName( aTypeName ) >>= aTypeNameProps ) && aTypeNameProps.getLength() )
                {
                    ::comphelper::SequenceAsHashMap aTypeNamePropsHM( aTypeNameProps );
                    uno::Sequence< ::rtl::OUString > aExtensions = aTypeNamePropsHM.getUnpackedValueOrDefault(
                                                    ::rtl::OUString::createFromAscii( "Extensions" ),
                                                    ::uno::Sequence< ::rtl::OUString >() );
                    if ( aExtensions.getLength() )
                        aObj.SetExtension( aExtensions[0] );
                }

                aReccomendedName = aObj.GetName( INetURLObject::DECODE_WITH_CHARSET );
            }
        }
    }

    return aReccomendedName;
}


//=========================================================================
// class SfxStoringHelper
//=========================================================================
//-------------------------------------------------------------------------
SfxStoringHelper::SfxStoringHelper( const uno::Reference< lang::XMultiServiceFactory >& xFactory )
: m_xFactory( xFactory )
{
}

//-------------------------------------------------------------------------
uno::Reference< lang::XMultiServiceFactory > SfxStoringHelper::GetServiceFactory()
{
    if ( !m_xFactory.is() )
    {
        m_xFactory = ::comphelper::getProcessServiceFactory();
        if( !m_xFactory.is() )
            throw uno::RuntimeException(); // TODO:
    }

    return m_xFactory;
}

//-------------------------------------------------------------------------
uno::Reference< container::XNameAccess > SfxStoringHelper::GetFilterConfiguration()
{
    if ( !m_xFilterCFG.is() )
    {
        m_xFilterCFG = uno::Reference< container::XNameAccess >(
            GetServiceFactory()->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.document.FilterFactory" ) ),
            uno::UNO_QUERY );

        if ( !m_xFilterCFG.is() )
            throw uno::RuntimeException();
    }

    return m_xFilterCFG;
}

//-------------------------------------------------------------------------
uno::Reference< container::XContainerQuery > SfxStoringHelper::GetFilterQuery()
{
    if ( !m_xFilterQuery.is() )
    {
        m_xFilterQuery = uno::Reference< container::XContainerQuery >( GetFilterConfiguration(), uno::UNO_QUERY );
        if ( !m_xFilterQuery.is() )
            throw uno::RuntimeException();
    }

    return m_xFilterQuery;
}

//-------------------------------------------------------------------------
uno::Reference< ::com::sun::star::frame::XModuleManager > SfxStoringHelper::GetModuleManager()
{
    if ( !m_xModuleManager.is() )
    {
        m_xModuleManager = uno::Reference< ::com::sun::star::frame::XModuleManager >(
            GetServiceFactory()->createInstance(
                    ::rtl::OUString::createFromAscii( "com.sun.star.frame.ModuleManager" ) ),
            uno::UNO_QUERY );

        if ( !m_xModuleManager.is() )
            throw uno::RuntimeException();
    }

    return m_xModuleManager;
}

//-------------------------------------------------------------------------
uno::Reference< container::XNameAccess > SfxStoringHelper::GetNamedModuleManager()
{
    if ( !m_xNamedModManager.is() )
    {
        m_xNamedModManager = uno::Reference< container::XNameAccess >( GetModuleManager(), uno::UNO_QUERY );
        if ( !m_xNamedModManager.is() )
            throw uno::RuntimeException();
    }

    return m_xNamedModManager;
}

//-------------------------------------------------------------------------
sal_Bool SfxStoringHelper::GUIStoreModel( const uno::Reference< frame::XModel >& xModel,
                                            const ::rtl::OUString& aSlotName,
                                            uno::Sequence< beans::PropertyValue >& aArgsSequence,
                                            sal_Bool bPreselectPassword,
                                            ::rtl::OUString aSuggestedName )
{
    ModelData_Impl aModelData( *this, xModel, aArgsSequence );

    sal_Bool bDialogUsed = sal_False;

    INetURLObject aURL;

    sal_Bool bSetStandardName = sal_False; // can be set only for SaveAs

    // parse the slot name
    sal_Int8 nStoreMode = getStoreModeFromSlotName( aSlotName );

    // handle the special cases
    if ( nStoreMode & SAVEAS_REQUESTED )
    {
        ::comphelper::SequenceAsHashMap::const_iterator aSaveToIter =
                        aModelData.GetMediaDescr().find( ::rtl::OUString::createFromAscii( "SaveTo" ) );
        if ( aSaveToIter != aModelData.GetMediaDescr().end() )
        {
            sal_Bool bWideExport = sal_False;
            aSaveToIter->second >>= bWideExport;
            if ( bWideExport )
                nStoreMode = EXPORT_REQUESTED | WIDEEXPORT_REQUESTED;
        }

        // if saving is not acceptable the warning must be shown even in case of SaveAs operation
        if ( ( nStoreMode & SAVEAS_REQUESTED ) && aModelData.CheckSaveAcceptable( STATUS_SAVEAS ) == STATUS_NO_ACTION )
            throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), ERRCODE_IO_ABORT );
    }
    else if ( nStoreMode & SAVE_REQUESTED )
    {
        // if saving is not acceptable by the configuration the warning must be shown
        sal_Int8 nStatusSave = aModelData.CheckSaveAcceptable( STATUS_SAVE );

        if ( nStatusSave == STATUS_NO_ACTION )
            throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), ERRCODE_IO_ABORT );
        else if ( nStatusSave == STATUS_SAVE )
        {
            // check whether it is possible to use save operation
            nStatusSave = aModelData.CheckStateForSave();
        }

        if ( nStatusSave == STATUS_NO_ACTION )
        {
            throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), ERRCODE_IO_ABORT );
        }
        else if ( nStatusSave == STATUS_SAVE )
        {
            // Document properties can contain streams that should be freed before storing
            aModelData.FreeDocumentProps();

            if ( aModelData.GetStorable2().is() )
            {
                try
                {
                    aModelData.GetStorable2()->storeSelf( aModelData.GetMediaDescr().getAsConstPropertyValueList() );
                }
                catch( lang::IllegalArgumentException& )
                {
                    OSL_ENSURE( sal_False, "ModelData didn't handle illegal parameters, all the parameters are ignored!\n" );
                    aModelData.GetStorable()->store();
                }
            }
            else
            {
                OSL_ENSURE( sal_False, "XStorable2 is not supported by the model!\n" );
                aModelData.GetStorable()->store();
            }

            return sal_False;
        }
        else
        {
            // this should be a usual SaveAs operation
            nStoreMode = SAVEAS_REQUESTED;
            if ( nStatusSave == STATUS_SAVEAS_STANDARDNAME )
                bSetStandardName = sal_True;
        }
    }

    // preselect a filter for the storing process
    uno::Sequence< beans::PropertyValue > aFilterProps = aModelData.GetPreselectedFilter_Impl( nStoreMode );

    DBG_ASSERT( aFilterProps.getLength(), "No filter for storing!\n" );
    if ( !aFilterProps.getLength() )
        throw task::ErrorCodeIOException( ::rtl::OUString(),
                                            uno::Reference< uno::XInterface >(),
                                            ERRCODE_IO_INVALIDPARAMETER );

    ::comphelper::SequenceAsHashMap aFilterPropsHM( aFilterProps );
    ::rtl::OUString aFilterName = aFilterPropsHM.getUnpackedValueOrDefault(
                                                                    ::rtl::OUString::createFromAscii( "Name" ),
                                                                    ::rtl::OUString() );

    ::rtl::OUString aFilterFromMediaDescr = aModelData.GetMediaDescr().getUnpackedValueOrDefault(
                                                    aFilterNameString,
                                                    ::rtl::OUString() );
    ::rtl::OUString aOldFilterName = aModelData.GetDocProps().getUnpackedValueOrDefault(
                                                    aFilterNameString,
                                                    ::rtl::OUString() );

    sal_Bool bUseFilterOptions = sal_False;
    ::comphelper::SequenceAsHashMap::const_iterator aFileNameIter = aModelData.GetMediaDescr().find( ::rtl::OUString::createFromAscii( "URL" ) );

    if ( ( nStoreMode & EXPORT_REQUESTED ) && ( nStoreMode & PDFEXPORT_REQUESTED ) && !( nStoreMode & PDFDIRECTEXPORT_REQUESTED ) )
    {
        // this is PDF export, the filter options dialog should be shown before the export
        aModelData.GetMediaDescr()[aFilterNameString] <<= aFilterName;
        if ( aModelData.GetMediaDescr().find( aFilterFlagsString ) == aModelData.GetMediaDescr().end()
          && aModelData.GetMediaDescr().find( aFilterOptionsString ) == aModelData.GetMediaDescr().end()
          && aModelData.GetMediaDescr().find( aFilterDataString ) == aModelData.GetMediaDescr().end() )
        {
            // execute filter options dialog since no options are set in the media descriptor
            if ( aModelData.ExecuteFilterDialog_Impl( aFilterName ) )
                bDialogUsed = sal_True;
        }
    }

    if ( aFileNameIter == aModelData.GetMediaDescr().end() )
    {
        sal_Int16 nDialog = SFX2_IMPL_DIALOG_CONFIG;
        ::comphelper::SequenceAsHashMap::const_iterator aDlgIter =
            aModelData.GetMediaDescr().find( ::rtl::OUString::createFromAscii( "UseSystemDialog" ) );
        if ( aDlgIter != aModelData.GetMediaDescr().end() )
        {
            sal_Bool bUseSystemDialog = sal_True;
            if ( aDlgIter->second >>= bUseSystemDialog )
            {
                if ( bUseSystemDialog )
                    nDialog = SFX2_IMPL_DIALOG_SYSTEM;
                else
                    nDialog = SFX2_IMPL_DIALOG_OOO;
            }
        }

        // The Dispatch supports parameter FolderName that overwrites SuggestedSaveAsDir
        ::rtl::OUString aSuggestedDir = aModelData.GetMediaDescr().getUnpackedValueOrDefault( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FolderName" ) ), ::rtl::OUString() );
        if ( !aSuggestedDir.getLength() )
        {
            aSuggestedDir = aModelData.GetMediaDescr().getUnpackedValueOrDefault( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SuggestedSaveAsDir" ) ), ::rtl::OUString() );
            if ( !aSuggestedDir.getLength() )
                aSuggestedDir = aModelData.GetDocProps().getUnpackedValueOrDefault( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SuggestedSaveAsDir" ) ), ::rtl::OUString() );
        }

    aSuggestedName = aModelData.GetMediaDescr().getUnpackedValueOrDefault( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SuggestedSaveAsName" ) ), ::rtl::OUString() );
        if ( !aSuggestedName.getLength() )
            aSuggestedName = aModelData.GetDocProps().getUnpackedValueOrDefault( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SuggestedSaveAsName" ) ), ::rtl::OUString() );

        ::rtl::OUString sStandardDir;
        ::comphelper::SequenceAsHashMap::const_iterator aStdDirIter =
            aModelData.GetMediaDescr().find( ::rtl::OUString::createFromAscii( "StandardDir" ) );
        if ( aStdDirIter != aModelData.GetMediaDescr().end() )
            aStdDirIter->second >>= sStandardDir;

        ::com::sun::star::uno::Sequence< ::rtl::OUString >	aBlackList;

        ::comphelper::SequenceAsHashMap::const_iterator aBlackListIter =
            aModelData.GetMediaDescr().find( ::rtl::OUString::createFromAscii( "BlackList" ) );
        if ( aBlackListIter != aModelData.GetMediaDescr().end() )
            aBlackListIter->second >>= aBlackList;

        sal_Bool bExit = sal_False;
        while ( !bExit )
        {
            bUseFilterOptions = aModelData.OutputFileDialog( nStoreMode, aFilterProps, bSetStandardName, aSuggestedName, bPreselectPassword, aSuggestedDir, nDialog, sStandardDir, aBlackList );

            // in case the dialog is opend a second time the folder should be the same as before, not what was handed over by parameters
            aSuggestedDir = ::rtl::OUString();
            if ( nStoreMode == SAVEAS_REQUESTED )
            {
                // in case of saving check filter for possible alien warning
                ::rtl::OUString aSelFilterName = aModelData.GetMediaDescr().getUnpackedValueOrDefault(
                                                                                aFilterNameString,
                                                                                ::rtl::OUString() );
                sal_Int8 nStatusSave = aModelData.CheckFilter( aSelFilterName );
                if ( nStatusSave == STATUS_SAVEAS_STANDARDNAME )
                {
                    // switch to best filter
                    bSetStandardName = sal_True;
                }
                else if ( nStatusSave == STATUS_SAVE )
                {
                    // user confirmed alien filter or "good" filter is used
                    bExit = sal_True;
                }
            }
            else
                bExit = sal_True;
        }

        bDialogUsed = sal_True;
        aFileNameIter = aModelData.GetMediaDescr().find( ::rtl::OUString::createFromAscii( "URL" ) );
    }
    else
    {
        // the target file name is provided so check if new filter options
        // are provided or old options can be used
        if ( aFilterFromMediaDescr.equals( aOldFilterName ) )
        {
            ::comphelper::SequenceAsHashMap::const_iterator aIter =
                                            aModelData.GetDocProps().find( aFilterOptionsString );
            if ( aIter != aModelData.GetDocProps().end()
              && aModelData.GetMediaDescr().find( aFilterOptionsString ) == aModelData.GetMediaDescr().end() )
                aModelData.GetMediaDescr()[aIter->first] = aIter->second;

            aIter = aModelData.GetDocProps().find( aFilterDataString );
            if ( aIter != aModelData.GetDocProps().end()
              && aModelData.GetMediaDescr().find( aFilterDataString ) == aModelData.GetMediaDescr().end() )
                aModelData.GetMediaDescr()[aIter->first] = aIter->second;
        }
    }

    if ( aFileNameIter != aModelData.GetMediaDescr().end() )
    {
        ::rtl::OUString aFileName;
        aFileNameIter->second >>= aFileName;
        aURL.SetURL( aFileName );
        DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "Illegal URL!" );

        ::comphelper::SequenceAsHashMap::const_iterator aIter =
                                aModelData.GetMediaDescr().find( aFilterNameString );

        if ( aIter != aModelData.GetMediaDescr().end() )
            aIter->second >>= aFilterName;
        else
            aModelData.GetMediaDescr()[aFilterNameString] <<= aFilterName;

        DBG_ASSERT( aFilterName.getLength(), "Illegal filter!" );
    }
    else
    {
        DBG_ASSERT( sal_False, "This code must be unreachable!\n" );
        throw task::ErrorCodeIOException( ::rtl::OUString(),
                                            uno::Reference< uno::XInterface >(),
                                            ERRCODE_IO_INVALIDPARAMETER );
    }

    ::comphelper::SequenceAsHashMap::const_iterator aIter =
                            aModelData.GetMediaDescr().find( ::rtl::OUString::createFromAscii( "FilterFlags" ) );
    sal_Bool bFilterFlagsSet = ( aIter != aModelData.GetMediaDescr().end() );

    if( !( nStoreMode & PDFEXPORT_REQUESTED ) && !bFilterFlagsSet
        && ( ( nStoreMode & EXPORT_REQUESTED ) || bUseFilterOptions ) )
    {
        // execute filter options dialog
        if ( aModelData.ExecuteFilterDialog_Impl( aFilterName ) )
            bDialogUsed = sal_True;
    }

    // so the arguments will not change any more and can be stored to the main location
    aArgsSequence = aModelData.GetMediaDescr().getAsConstPropertyValueList();

    // store the document and handle it's docinfo
    SvtSaveOptions aOptions;

    if ( aOptions.IsDocInfoSave()
      && ( !aModelData.GetStorable()->hasLocation()
          || INetURLObject( aModelData.GetStorable()->getLocation() ) != aURL ) )
    {
        // this is defenitly not a Save operation
        // so the document info can be updated

        // on export document info must be preserved
        uno::Reference<document::XDocumentInfoSupplier> xDIS(
            aModelData.GetModel(), uno::UNO_QUERY_THROW);
        uno::Reference<util::XCloneable> xCloneable(
            xDIS->getDocumentInfo(), uno::UNO_QUERY_THROW);
        uno::Reference<document::XDocumentInfo> xOldDocInfo(
            xCloneable->createClone(), uno::UNO_QUERY_THROW);

        // use dispatch API to show document info dialog
        if ( aModelData.ShowDocumentInfoDialog() )
            bDialogUsed = sal_True;
        else
        {
            DBG_ERROR( "Can't execute document info dialog!\n" );
        }

        try {
            // Document properties can contain streams that should be freed before storing
            aModelData.FreeDocumentProps();
            if ( ( nStoreMode & EXPORT_REQUESTED ) )
                aModelData.GetStorable()->storeToURL( aURL.GetMainURL( INetURLObject::NO_DECODE ), aArgsSequence );
            else
                aModelData.GetStorable()->storeAsURL( aURL.GetMainURL( INetURLObject::NO_DECODE ), aArgsSequence );
        }
        catch( uno::Exception& )
        {
            if ( ( nStoreMode & EXPORT_REQUESTED ) )
                SetDocInfoState( aModelData.GetModel(), xOldDocInfo, sal_True );

            throw;
        }

        if ( ( nStoreMode & EXPORT_REQUESTED ) )
            SetDocInfoState( aModelData.GetModel(), xOldDocInfo, sal_True );
    }
    else
    {
        // Document properties can contain streams that should be freed before storing
        aModelData.FreeDocumentProps();

        // this is actually a save operation with different parameters
        // so storeTo or storeAs without DocInfo operations are used
        if ( ( nStoreMode & EXPORT_REQUESTED ) )
            aModelData.GetStorable()->storeToURL( aURL.GetMainURL( INetURLObject::NO_DECODE ), aArgsSequence );
        else
            aModelData.GetStorable()->storeAsURL( aURL.GetMainURL( INetURLObject::NO_DECODE ), aArgsSequence );
    }

    return bDialogUsed;
}

//-------------------------------------------------------------------------
// static
uno::Sequence< beans::PropertyValue > SfxStoringHelper::SearchForFilter(
                                                        const uno::Reference< container::XContainerQuery >& xFilterQuery,
                                                        const uno::Sequence< beans::NamedValue >& aSearchRequest,
                                                        sal_Int32 nMustFlags,
                                                        sal_Int32 nDontFlags )
{
    uno::Sequence< beans::PropertyValue > aFilterProps;
    uno::Reference< container::XEnumeration > xFilterEnum =
                                            xFilterQuery->createSubSetEnumerationByProperties( aSearchRequest );

    // use the first filter that is found
    if ( xFilterEnum.is() )
        while ( xFilterEnum->hasMoreElements() )
        {
            uno::Sequence< beans::PropertyValue > aProps;
            if ( xFilterEnum->nextElement() >>= aProps )
            {
                ::comphelper::SequenceAsHashMap aPropsHM( aProps );
                sal_Int32 nFlags = aPropsHM.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "Flags" ),
                                                                        (sal_Int32)0 );
                if ( ( ( nFlags & nMustFlags ) == nMustFlags ) && !( nFlags & nDontFlags ) )
                {
                    aFilterProps = aProps;
                    break;
                }
            }
        }

    return aFilterProps;
}

//-------------------------------------------------------------------------
// static
sal_Bool SfxStoringHelper::CheckFilterOptionsAppearence(
                                                    const uno::Reference< container::XNameAccess >& xFilterCFG,
                                                    const ::rtl::OUString& aFilterName )
{
    sal_Bool bUseFilterOptions = sal_False;

    DBG_ASSERT( xFilterCFG.is(), "No filter configuration!\n" );
    if( xFilterCFG.is() )
    {
        try {
               uno::Sequence < beans::PropertyValue > aProps;
            uno::Any aAny = xFilterCFG->getByName( aFilterName );
               if ( aAny >>= aProps )
               {
                ::comphelper::SequenceAsHashMap aPropsHM( aProps );
                   ::rtl::OUString aServiceName = aPropsHM.getUnpackedValueOrDefault(
                                                    ::rtl::OUString::createFromAscii( "UIComponent" ),
                                                    ::rtl::OUString() );
                if( aServiceName.getLength() )
                       bUseFilterOptions = sal_True;
            }
        }
        catch( uno::Exception& )
        {
        }
    }

    return bUseFilterOptions;
}

//-------------------------------------------------------------------------
// static
void SfxStoringHelper::SetDocInfoState(
        const uno::Reference< frame::XModel >& xModel,
        const uno::Reference< document::XDocumentInfo >& i_xOldDocInfo,
        sal_Bool bNoModify )
{
    uno::Reference< document::XDocumentInfoSupplier > xModelDocInfoSupplier( xModel, uno::UNO_QUERY );
    if ( !xModelDocInfoSupplier.is() )
        throw uno::RuntimeException(); // TODO:

    uno::Reference< document::XDocumentInfo > xDocInfoToFill = xModelDocInfoSupplier->getDocumentInfo();
    uno::Reference< beans::XPropertySet > xPropSet( i_xOldDocInfo,
        uno::UNO_QUERY_THROW );

    uno::Reference< util::XModifiable > xModifiable( xModel, uno::UNO_QUERY );
    if ( bNoModify && !xModifiable.is() )
        throw uno::RuntimeException();

    sal_Bool bIsModified = bNoModify && xModifiable->isModified();

    try
    {
        uno::Reference< beans::XPropertySet > xSet( xDocInfoToFill, uno::UNO_QUERY );
        uno::Reference< beans::XPropertyContainer > xContainer( xSet, uno::UNO_QUERY );
        uno::Reference< beans::XPropertySetInfo > xSetInfo = xSet->getPropertySetInfo();
        uno::Sequence< beans::Property > lProps = xSetInfo->getProperties();
        const beans::Property* pProps = lProps.getConstArray();
        sal_Int32 c = lProps.getLength();
        sal_Int32 i = 0;
        for (i=0; i<c; ++i)
        {
            uno::Any aValue = xPropSet->getPropertyValue( pProps[i].Name );
            if ( pProps[i].Attributes & ::com::sun::star::beans::PropertyAttribute::REMOVABLE )
                // QUESTION: DefaultValue?!
                xContainer->addProperty( pProps[i].Name, pProps[i].Attributes, aValue );
            try
            {
                // it is possible that the propertysets from XML and binary files differ; we shouldn't break then
                xSet->setPropertyValue( pProps[i].Name, aValue );
            }
            catch ( uno::Exception& ) {}
        }

        sal_Int16 nCount = i_xOldDocInfo->getUserFieldCount();
        sal_Int16 nSupportedCount = xDocInfoToFill->getUserFieldCount();
        for ( sal_Int16 nInd = 0; nInd < nCount && nInd < nSupportedCount; nInd++ )
        {
            ::rtl::OUString aPropName = i_xOldDocInfo->getUserFieldName( nInd );
            xDocInfoToFill->setUserFieldName( nInd, aPropName );
            ::rtl::OUString aPropVal = i_xOldDocInfo->getUserFieldValue( nInd );
            xDocInfoToFill->setUserFieldValue( nInd, aPropVal );
        }
    }
    catch ( uno::Exception& ) {}

    // set the modified flag back if required
    if ( bNoModify && bIsModified != xModifiable->isModified() )
        xModifiable->setModified( bIsModified );
}

//-------------------------------------------------------------------------
// static
sal_Bool SfxStoringHelper::WarnUnacceptableFormat( const uno::Reference< frame::XModel >& xModel,
                                                    ::rtl::OUString aOldUIName,
                                                    ::rtl::OUString /*aDefUIName*/,
                                                    sal_Bool /*bCanProceedFurther*/ )
{
    if ( !SvtSaveOptions().IsWarnAlienFormat() )
        return sal_True;

    Window* pWin = SfxStoringHelper::GetModelWindow( xModel );
    SfxAlienWarningDialog aDlg( pWin, aOldUIName );

    return aDlg.Execute() == RET_OK;
}

// static
void SfxStoringHelper::ExecuteFilterDialog( SfxStoringHelper& _rStorageHelper
                                            ,const ::rtl::OUString& _sFilterName
                                            ,const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& _xModel
                                            ,/*OUT*/::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArgsSequence)
{
    ModelData_Impl aModelData( _rStorageHelper, _xModel, _rArgsSequence );
    if ( aModelData.ExecuteFilterDialog_Impl( _sFilterName ) )
        _rArgsSequence = aModelData.GetMediaDescr().getAsConstPropertyValueList();
}

// static
Window* SfxStoringHelper::GetModelWindow( const uno::Reference< frame::XModel >& xModel )
{
    Window* pWin = 0;
    try {
        if ( xModel.is() )
        {
            uno::Reference< frame::XController > xController = xModel->getCurrentController();
            if ( xController.is() )
            {
                uno::Reference< frame::XFrame > xFrame = xController->getFrame();
                if ( xFrame.is() )
                {
                    uno::Reference< awt::XWindow > xWindow = xFrame->getContainerWindow();
                    if ( xWindow.is() )
                    {
                        VCLXWindow* pVCLWindow = VCLXWindow::GetImplementation( xWindow );
                        if ( pVCLWindow )
                            pWin = pVCLWindow->GetWindow();
                    }
                }
            }
        }
    }
    catch ( uno::Exception& )
    {
    }

    return pWin;
}