Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/doc/guisaveas.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
#include <config_gpgme.h>
20
#if HAVE_FEATURE_GPGME
21
#include <com/sun/star/xml/crypto/GPGSEInitializer.hpp>
22
#include <com/sun/star/xml/crypto/SEInitializer.hpp>
23
#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
24
#endif
25
#include <com/sun/star/security/DocumentDigitalSignatures.hpp>
26
#include <com/sun/star/security/XCertificate.hpp>
27
28
#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
29
#include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
30
#include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
31
#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
32
#include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
33
#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
34
#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
35
#include <com/sun/star/view/XSelectionSupplier.hpp>
36
#include <com/sun/star/beans/PropertyExistException.hpp>
37
#include <com/sun/star/beans/XPropertyAccess.hpp>
38
#include <com/sun/star/beans/XPropertySet.hpp>
39
#include <com/sun/star/beans/XPropertyContainer.hpp>
40
#include <com/sun/star/beans/PropertyAttribute.hpp>
41
#include <com/sun/star/document/XExporter.hpp>
42
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
43
#include <com/sun/star/document/XDocumentProperties.hpp>
44
#include <com/sun/star/task/ErrorCodeIOException.hpp>
45
#include <com/sun/star/task/InteractionHandler.hpp>
46
#include <com/sun/star/util/URLTransformer.hpp>
47
#include <com/sun/star/util/XURLTransformer.hpp>
48
#include <com/sun/star/frame/ModuleManager.hpp>
49
#include <com/sun/star/frame/XStorable.hpp>
50
#include <com/sun/star/frame/XStorable2.hpp>
51
#include <com/sun/star/frame/XDispatchProvider.hpp>
52
#include <com/sun/star/frame/XDispatch.hpp>
53
#include <com/sun/star/frame/XTitle.hpp>
54
#include <com/sun/star/util/XModifiable.hpp>
55
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
56
57
#include <com/sun/star/util/XCloneable.hpp>
58
59
#include <guisaveas.hxx>
60
61
#include <sal/log.hxx>
62
#include <svl/itemset.hxx>
63
#include <svl/eitem.hxx>
64
#include <tools/debug.hxx>
65
#include <comphelper/diagnose_ex.hxx>
66
#include <comphelper/documentconstants.hxx>
67
#include <comphelper/namedvaluecollection.hxx>
68
#include <tools/urlobj.hxx>
69
#include <tools/json_writer.hxx>
70
#include <comphelper/processfactory.hxx>
71
#include <comphelper/propertysequence.hxx>
72
#include <comphelper/propertyvalue.hxx>
73
#include <comphelper/sequenceashashmap.hxx>
74
#include <comphelper/mimeconfighelper.hxx>
75
#include <comphelper/lok.hxx>
76
#include <comphelper/xmlsechelper.hxx>
77
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
78
#include <utility>
79
#include <vcl/svapp.hxx>
80
#include <vcl/vclenum.hxx>
81
#include <vcl/weld/MessageDialog.hxx>
82
#include <vcl/weld/weld.hxx>
83
#include <o3tl/char16_t2wchar_t.hxx>
84
#include <unotools/tempfile.hxx>
85
#include <unotools/useroptions.hxx>
86
87
#include <sfx2/docfile.hxx>
88
#include <sfx2/objsh.hxx>
89
#include <sfx2/sfxsids.hrc>
90
#include <sfx2/strings.hrc>
91
#include <sfx2/sfxresid.hxx>
92
#include <sfx2/filedlghelper.hxx>
93
#include <sfx2/app.hxx>
94
#include <sfx2/sfxuno.hxx>
95
#include <sfx2/viewsh.hxx>
96
#include <sfx2/bindings.hxx>
97
98
#include <memory>
99
#include <string_view>
100
101
#include <officecfg/Office/Common.hxx>
102
103
#include <vcl/FilterConfigItem.hxx>
104
#include <com/sun/star/system/SystemShellExecute.hpp>
105
#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
106
107
#include <osl/file.hxx>
108
#include <svl/cryptosign.hxx>
109
110
#include <svtools/confirmationdlg.hxx>
111
112
#ifdef _WIN32
113
#include <Shlobj.h>
114
#ifdef GetTempPath
115
#undef GetTempPath
116
#endif
117
#endif
118
119
// flags that specify requested operation
120
0
#define EXPORT_REQUESTED            1
121
0
#define PDFEXPORT_REQUESTED         2
122
0
#define PDFDIRECTEXPORT_REQUESTED   4
123
0
#define WIDEEXPORT_REQUESTED        8
124
0
#define SAVE_REQUESTED              16
125
0
#define SAVEAS_REQUESTED            32
126
0
#define SAVEACOPY_REQUESTED         64
127
0
#define EPUBEXPORT_REQUESTED       128
128
0
#define EPUBDIRECTEXPORT_REQUESTED 256
129
0
#define SAVEASREMOTE_REQUESTED      -1
130
131
// possible statuses of save operation
132
0
#define STATUS_NO_ACTION            0
133
0
#define STATUS_SAVE                 1
134
0
#define STATUS_SAVEAS               2
135
0
#define STATUS_SAVEAS_STANDARDNAME  3
136
137
constexpr OUString aFilterNameString = u"FilterName"_ustr;
138
constexpr OUString aFilterOptionsString = u"FilterOptions"_ustr;
139
constexpr OUString aFilterDataString = u"FilterData"_ustr;
140
141
using namespace ::com::sun::star;
142
using namespace css::system;
143
144
namespace {
145
146
sal_uInt16 getSlotIDFromMode( sal_Int16 nStoreMode )
147
0
{
148
    // This is a temporary hardcoded solution must be removed when
149
    // dialogs do not need parameters in SidSet representation any more
150
151
0
    sal_uInt16 nResult = 0;
152
0
    if ( nStoreMode == EXPORT_REQUESTED || nStoreMode == ( EXPORT_REQUESTED | SAVEACOPY_REQUESTED | WIDEEXPORT_REQUESTED ) )
153
0
        nResult = SID_EXPORTDOC;
154
0
    else if ( nStoreMode == ( EXPORT_REQUESTED | PDFEXPORT_REQUESTED ) )
155
0
        nResult = SID_EXPORTDOCASPDF;
156
0
    else if ( nStoreMode == ( EXPORT_REQUESTED | EPUBEXPORT_REQUESTED ) )
157
0
        nResult = SID_EXPORTDOCASEPUB;
158
0
    else if ( nStoreMode == ( EXPORT_REQUESTED | PDFEXPORT_REQUESTED | PDFDIRECTEXPORT_REQUESTED ) )
159
0
        nResult = SID_DIRECTEXPORTDOCASPDF;
160
0
    else if ( nStoreMode == ( EXPORT_REQUESTED | EPUBEXPORT_REQUESTED | EPUBDIRECTEXPORT_REQUESTED ) )
161
0
        nResult = SID_DIRECTEXPORTDOCASEPUB;
162
0
    else if ( nStoreMode == SAVEAS_REQUESTED || nStoreMode == ( EXPORT_REQUESTED | WIDEEXPORT_REQUESTED ) )
163
0
        nResult = SID_SAVEASDOC;
164
0
    else if ( nStoreMode == SAVEASREMOTE_REQUESTED )
165
0
        nResult = SID_SAVEASREMOTE;
166
0
    else {
167
0
        SAL_WARN( "sfx.doc", "Unacceptable slot name is provided!" );
168
0
    }
169
170
0
    return nResult;
171
0
}
172
173
174
sal_Int16 getStoreModeFromSlotName( std::u16string_view aSlotName )
175
0
{
176
0
    sal_Int16 nResult = 0;
177
0
    if ( aSlotName == u"ExportTo" )
178
0
        nResult = EXPORT_REQUESTED;
179
0
    else if ( aSlotName == u"ExportToPDF" )
180
0
        nResult = EXPORT_REQUESTED | PDFEXPORT_REQUESTED;
181
0
    else if ( aSlotName == u"ExportDirectToPDF" )
182
0
        nResult = EXPORT_REQUESTED | PDFEXPORT_REQUESTED | PDFDIRECTEXPORT_REQUESTED;
183
0
    else if ( aSlotName == u"ExportToEPUB" )
184
0
        nResult = EXPORT_REQUESTED | EPUBEXPORT_REQUESTED;
185
0
    else if ( aSlotName == u"ExportDirectToEPUB" )
186
0
        nResult = EXPORT_REQUESTED | EPUBEXPORT_REQUESTED | EPUBDIRECTEXPORT_REQUESTED;
187
0
    else if ( aSlotName == u"Save" )
188
0
        nResult = SAVE_REQUESTED;
189
0
    else if ( aSlotName == u"SaveAs" )
190
0
        nResult = SAVEAS_REQUESTED;
191
0
    else if ( aSlotName == u"SaveAsRemote" )
192
0
        nResult = SAVEASREMOTE_REQUESTED;
193
0
    else
194
0
        throw task::ErrorCodeIOException(
195
0
            (OUString::Concat("getStoreModeFromSlotName(\"") + aSlotName
196
0
             + "): ERRCODE_IO_INVALIDPARAMETER"),
197
0
            uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_INVALIDPARAMETER) );
198
199
0
    return nResult;
200
0
}
201
202
203
SfxFilterFlags getMustFlags( sal_Int16 nStoreMode )
204
0
{
205
0
    return ( SfxFilterFlags::EXPORT
206
0
            | ( ( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) ) ? SfxFilterFlags::NONE : SfxFilterFlags::IMPORT ) );
207
0
}
208
209
210
SfxFilterFlags getDontFlags( sal_Int16 nStoreMode )
211
0
{
212
0
    return ( SfxFilterFlags::INTERNAL
213
0
            | SfxFilterFlags::NOTINFILEDLG
214
0
            | ( ( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) ) ? SfxFilterFlags::IMPORT : SfxFilterFlags::NONE ) );
215
0
}
216
217
218
219
220
class DocumentSettingsGuard
221
{
222
    uno::Reference< beans::XPropertySet > m_xDocumentSettings;
223
    bool m_bPreserveReadOnly;
224
    bool m_bReadOnlySupported;
225
226
    bool m_bRestoreSettings;
227
public:
228
    DocumentSettingsGuard( const uno::Reference< frame::XModel2 >& xModel, bool bReadOnly, bool bRestore )
229
0
    : m_bPreserveReadOnly( false )
230
0
    , m_bReadOnlySupported( false )
231
0
    , m_bRestoreSettings( bRestore )
232
0
    {
233
0
        try
234
0
        {
235
0
            uno::Reference< lang::XMultiServiceFactory > xDocSettingsSupplier( xModel, uno::UNO_QUERY );
236
0
            if (xDocSettingsSupplier)
237
0
            {
238
0
                m_xDocumentSettings.set(
239
0
                    xDocSettingsSupplier->createInstance( u"com.sun.star.document.Settings"_ustr ),
240
0
                    uno::UNO_QUERY );
241
0
                if (m_xDocumentSettings)
242
0
                {
243
0
                    OUString aLoadReadonlyString( u"LoadReadonly"_ustr );
244
0
                    m_xDocumentSettings->getPropertyValue( aLoadReadonlyString ) >>= m_bPreserveReadOnly;
245
0
                    m_xDocumentSettings->setPropertyValue( aLoadReadonlyString, uno::Any( bReadOnly ) );
246
0
                    m_bReadOnlySupported = true;
247
0
                }
248
0
            }
249
0
        }
250
0
        catch( const uno::Exception& )
251
0
        {}
252
253
0
        if ( bReadOnly && !m_bReadOnlySupported )
254
0
            throw uno::RuntimeException(); // the user could provide the data, so it must be stored
255
0
    }
256
257
    ~DocumentSettingsGuard()
258
0
    {
259
0
        if ( m_bRestoreSettings )
260
0
        {
261
0
            try
262
0
            {
263
0
                if ( m_bReadOnlySupported )
264
0
                    m_xDocumentSettings->setPropertyValue( u"LoadReadonly"_ustr, uno::Any( m_bPreserveReadOnly ) );
265
0
            }
266
0
            catch( const uno::Exception& )
267
0
            {
268
0
                TOOLS_WARN_EXCEPTION( "sfx.doc", "" );
269
0
            }
270
0
        }
271
0
    }
272
};
273
} // anonymous namespace
274
275
276
277
class ModelData_Impl
278
{
279
    SfxStoringHelper* m_pOwner;
280
    uno::Reference< frame::XModel2 > m_xModel;
281
    uno::Reference< frame::XStorable > m_xStorable;
282
    uno::Reference< frame::XStorable2 > m_xStorable2;
283
284
    OUString m_aModuleName;
285
    std::unique_ptr<::comphelper::SequenceAsHashMap> m_pDocumentPropsHM;
286
    std::unique_ptr<::comphelper::SequenceAsHashMap> m_pModulePropsHM;
287
288
    uno::Reference<beans::XPropertyAccess> m_xFilterProperties;
289
    uno::Reference<ui::dialogs::XAsynchronousExecutableDialog> m_xFilterDialog;
290
291
    ::comphelper::SequenceAsHashMap m_aMediaDescrHM;
292
293
    bool m_bRecommendReadOnly;
294
    bool m_bSignWithDefaultSignature;
295
296
    DECL_LINK(OptionsDialogClosedHdl, css::ui::dialogs::DialogClosedEvent*, void);
297
298
public:
299
    ModelData_Impl( SfxStoringHelper& aOwner,
300
                    uno::Reference< frame::XModel2 > xModel,
301
                    const uno::Sequence< beans::PropertyValue >& aMediaDescr );
302
303
    ~ModelData_Impl();
304
305
    void FreeDocumentProps();
306
307
    uno::Reference< frame::XModel2 > const & GetModel() const;
308
    uno::Reference< frame::XStorable > const & GetStorable();
309
    uno::Reference< frame::XStorable2 > const & GetStorable2();
310
311
0
    ::comphelper::SequenceAsHashMap& GetMediaDescr() { return m_aMediaDescrHM; }
312
313
0
    bool IsRecommendReadOnly() const { return m_bRecommendReadOnly; }
314
0
    bool IsSignWithDefaultSignature() const { return m_bSignWithDefaultSignature; }
315
316
    const ::comphelper::SequenceAsHashMap& GetDocProps();
317
318
    OUString const & GetModuleName();
319
    const ::comphelper::SequenceAsHashMap& GetModuleProps();
320
321
    void CheckInteractionHandler();
322
323
324
    OUString GetDocServiceName();
325
    uno::Sequence< beans::PropertyValue > GetDocServiceDefaultFilterCheckFlags( SfxFilterFlags nMust, SfxFilterFlags nDont );
326
    uno::Sequence< beans::PropertyValue > GetDocServiceAnyFilter( SfxFilterFlags nMust, SfxFilterFlags nDont );
327
    uno::Sequence< beans::PropertyValue > GetPreselectedFilter_Impl( sal_Int16 nStoreMode );
328
    uno::Sequence< beans::PropertyValue > GetDocServiceDefaultFilter();
329
330
    bool ExecuteFilterDialog_Impl( const OUString& aFilterName, bool bAsync );
331
332
    sal_Int8 CheckSaveAcceptable( sal_Int8 nCurStatus );
333
    sal_Int8 CheckStateForSave();
334
335
    sal_Int8 CheckFilter( const OUString& );
336
337
    bool CheckFilterOptionsDialogExistence();
338
339
    bool OutputFileDialog( sal_Int16 nStoreMode,
340
                                const ::comphelper::SequenceAsHashMap& aPreselectedFilterPropsHM,
341
                                bool bSetStandardName,
342
                                OUString& aSuggestedName,
343
                                bool bPreselectPassword,
344
                                OUString& aSuggestedDir,
345
                                sal_Int16 nDialog,
346
                                const css::uno::Sequence<OUString>& rDenyList,
347
                                SignatureState const nScriptingSignatureState
348
                                );
349
350
    bool ShowDocumentInfoDialog();
351
352
    static OUString GetRecommendedExtension( const OUString& aTypeName );
353
    OUString GetRecommendedName( const OUString& aSuggestedName,
354
                                        const OUString& aTypeName );
355
};
356
357
358
ModelData_Impl::ModelData_Impl( SfxStoringHelper& aOwner,
359
                                uno::Reference< frame::XModel2 > xModel,
360
                                const uno::Sequence< beans::PropertyValue >& aMediaDescr )
361
0
: m_pOwner( &aOwner )
362
0
, m_xModel(std::move( xModel ))
363
0
, m_aMediaDescrHM( aMediaDescr )
364
0
, m_bRecommendReadOnly( false )
365
0
, m_bSignWithDefaultSignature( false )
366
0
{
367
0
    CheckInteractionHandler();
368
0
}
369
370
371
ModelData_Impl::~ModelData_Impl()
372
0
{
373
0
    FreeDocumentProps();
374
0
    m_pDocumentPropsHM.reset();
375
0
    m_pModulePropsHM.reset();
376
0
    if (m_xFilterProperties)
377
0
        m_xFilterProperties.clear();
378
0
}
379
380
381
void ModelData_Impl::FreeDocumentProps()
382
0
{
383
0
    m_pDocumentPropsHM.reset();
384
0
}
385
386
387
uno::Reference< frame::XModel2 > const & ModelData_Impl::GetModel() const
388
0
{
389
0
    if ( !m_xModel.is() )
390
0
        throw uno::RuntimeException();
391
392
0
    return m_xModel;
393
0
}
394
395
uno::Reference< frame::XStorable > const & ModelData_Impl::GetStorable()
396
0
{
397
0
    if ( !m_xStorable.is() )
398
0
    {
399
0
        m_xStorable.set( m_xModel, uno::UNO_QUERY_THROW );
400
0
    }
401
402
0
    return m_xStorable;
403
0
}
404
405
406
uno::Reference< frame::XStorable2 > const & ModelData_Impl::GetStorable2()
407
0
{
408
0
    if ( !m_xStorable2.is() )
409
0
    {
410
0
        m_xStorable2.set( m_xModel, uno::UNO_QUERY_THROW );
411
0
    }
412
413
0
    return m_xStorable2;
414
0
}
415
416
417
const ::comphelper::SequenceAsHashMap& ModelData_Impl::GetDocProps()
418
0
{
419
0
    if ( !m_pDocumentPropsHM )
420
0
        m_pDocumentPropsHM.reset( new ::comphelper::SequenceAsHashMap( GetModel()->getArgs() ) );
421
422
0
    return *m_pDocumentPropsHM;
423
0
}
424
425
426
OUString const & ModelData_Impl::GetModuleName()
427
0
{
428
0
    if ( m_aModuleName.isEmpty() )
429
0
    {
430
0
        m_aModuleName = m_pOwner->GetModuleManager()->identify(
431
0
                                                uno::Reference< uno::XInterface >( m_xModel, uno::UNO_QUERY ) );
432
0
        if ( m_aModuleName.isEmpty() )
433
0
            throw uno::RuntimeException(); // TODO:
434
0
    }
435
0
    return m_aModuleName;
436
0
}
437
438
439
const ::comphelper::SequenceAsHashMap& ModelData_Impl::GetModuleProps()
440
0
{
441
0
    if ( !m_pModulePropsHM )
442
0
    {
443
0
        uno::Sequence< beans::PropertyValue > aModuleProps;
444
0
        m_pOwner->GetModuleManager()->getByName( GetModuleName() ) >>= aModuleProps;
445
0
        if ( !aModuleProps.hasElements() )
446
0
            throw uno::RuntimeException(); // TODO;
447
0
        m_pModulePropsHM.reset( new ::comphelper::SequenceAsHashMap( aModuleProps ) );
448
0
    }
449
450
0
    return *m_pModulePropsHM;
451
0
}
452
453
454
OUString ModelData_Impl::GetDocServiceName()
455
0
{
456
0
    return GetModuleProps().getUnpackedValueOrDefault(u"ooSetupFactoryDocumentService"_ustr, OUString());
457
0
}
458
459
460
void ModelData_Impl::CheckInteractionHandler()
461
0
{
462
0
    static constexpr OUString sInteractionHandler {u"InteractionHandler"_ustr};
463
0
    ::comphelper::SequenceAsHashMap::const_iterator aInteractIter =
464
0
            m_aMediaDescrHM.find( sInteractionHandler );
465
466
0
    if ( aInteractIter == m_aMediaDescrHM.end() )
467
0
    {
468
0
        try {
469
0
            m_aMediaDescrHM[ sInteractionHandler ]
470
0
                <<= task::InteractionHandler::createWithParent( comphelper::getProcessComponentContext(), nullptr);
471
0
        }
472
0
        catch( const uno::Exception& )
473
0
        {
474
0
        }
475
0
    }
476
0
    else
477
0
    {
478
0
        uno::Reference< task::XInteractionHandler > xInteract;
479
0
        DBG_ASSERT( ( aInteractIter->second >>= xInteract ) && xInteract.is(), "Broken interaction handler is provided!\n" );
480
0
    }
481
0
}
482
483
484
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetDocServiceDefaultFilter()
485
0
{
486
0
    uno::Sequence< beans::PropertyValue > aProps;
487
488
0
    const OUString aFilterName = GetModuleProps().getUnpackedValueOrDefault( u"ooSetupFactoryDefaultFilter"_ustr, OUString() );
489
490
0
    m_pOwner->GetFilterConfiguration()->getByName( aFilterName ) >>= aProps;
491
492
0
    return aProps;
493
0
}
494
495
496
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetDocServiceDefaultFilterCheckFlags( SfxFilterFlags nMust,
497
                                                                                            SfxFilterFlags nDont )
498
0
{
499
0
    uno::Sequence< beans::PropertyValue > aFilterProps;
500
0
    uno::Sequence< beans::PropertyValue > aProps = GetDocServiceDefaultFilter();
501
0
    if ( aProps.hasElements() )
502
0
    {
503
0
        ::comphelper::SequenceAsHashMap aFiltHM( aProps );
504
0
        SfxFilterFlags nFlags = static_cast<SfxFilterFlags>(aFiltHM.getUnpackedValueOrDefault(u"Flags"_ustr,
505
0
                                                        sal_Int32(0) ));
506
0
        if ( ( ( nFlags & nMust ) == nMust ) && !( nFlags & nDont ) )
507
0
            aFilterProps = std::move(aProps);
508
0
    }
509
510
0
    return aFilterProps;
511
0
}
512
513
514
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetDocServiceAnyFilter( SfxFilterFlags nMust, SfxFilterFlags nDont )
515
0
{
516
0
    uno::Sequence< beans::NamedValue > aSearchRequest { { u"DocumentService"_ustr, css::uno::Any(GetDocServiceName()) } };
517
518
0
    return ::comphelper::MimeConfigurationHelper::SearchForFilter( m_pOwner->GetFilterQuery(), aSearchRequest, nMust, nDont );
519
0
}
520
521
522
uno::Sequence< beans::PropertyValue > ModelData_Impl::GetPreselectedFilter_Impl( sal_Int16 nStoreMode )
523
0
{
524
0
    if ( nStoreMode == SAVEASREMOTE_REQUESTED )
525
0
        nStoreMode = SAVEAS_REQUESTED;
526
527
0
    uno::Sequence< beans::PropertyValue > aFilterProps;
528
529
0
    SfxFilterFlags nMust = getMustFlags( nStoreMode );
530
0
    SfxFilterFlags nDont = getDontFlags( nStoreMode );
531
532
0
    if ( ( nStoreMode != SAVEASREMOTE_REQUESTED ) && ( nStoreMode & PDFEXPORT_REQUESTED ) )
533
0
    {
534
        // Preselect PDF-Filter for EXPORT
535
0
        uno::Sequence< beans::NamedValue > aSearchRequest
536
0
        {
537
0
            { u"Type"_ustr, css::uno::Any(u"pdf_Portable_Document_Format"_ustr) },
538
0
            { u"DocumentService"_ustr, css::uno::Any(GetDocServiceName()) }
539
0
        };
540
541
0
        aFilterProps = ::comphelper::MimeConfigurationHelper::SearchForFilter( m_pOwner->GetFilterQuery(), aSearchRequest, nMust, nDont );
542
0
    }
543
0
    else if ( ( nStoreMode != SAVEASREMOTE_REQUESTED ) && ( nStoreMode & EPUBEXPORT_REQUESTED ) )
544
0
    {
545
        // Preselect EPUB filter for export.
546
0
        uno::Sequence<beans::NamedValue> aSearchRequest
547
0
        {
548
0
            { u"Type"_ustr, css::uno::Any(u"writer_EPUB_Document"_ustr) },
549
0
            { u"DocumentService"_ustr, css::uno::Any(GetDocServiceName()) }
550
0
        };
551
552
0
        aFilterProps = ::comphelper::MimeConfigurationHelper::SearchForFilter( m_pOwner->GetFilterQuery(), aSearchRequest, nMust, nDont );
553
0
    }
554
0
    else
555
0
    {
556
0
        aFilterProps = GetDocServiceDefaultFilterCheckFlags( nMust, nDont );
557
558
0
        if ( !aFilterProps.hasElements() )
559
0
        {
560
            // the default filter was not found, use just the first acceptable one
561
0
            aFilterProps = GetDocServiceAnyFilter( nMust, nDont );
562
0
        }
563
0
    }
564
565
0
    return aFilterProps;
566
0
}
567
568
569
bool ModelData_Impl::ExecuteFilterDialog_Impl( const OUString& aFilterName, bool bIsAsync )
570
0
{
571
0
    bool bDialogUsed = false;
572
573
0
    try {
574
0
        uno::Sequence < beans::PropertyValue > aProps;
575
0
        uno::Any aAny = m_pOwner->GetFilterConfiguration()->getByName( aFilterName );
576
0
        if ( aAny >>= aProps )
577
0
        {
578
0
            auto pProp = std::find_if(std::cbegin(aProps), std::cend(aProps),
579
0
                [](const beans::PropertyValue& rProp) { return rProp.Name == "UIComponent"; });
580
0
            if (pProp != std::cend(aProps))
581
0
            {
582
0
                OUString aServiceName;
583
0
                pProp->Value >>= aServiceName;
584
0
                if( !aServiceName.isEmpty() )
585
0
                {
586
0
                    uno::Sequence<uno::Any> aDialogArgs(comphelper::InitAnyPropertySequence(
587
0
                    {
588
0
                        {"ParentWindow", uno::Any(SfxStoringHelper::GetModelXWindow(m_xModel))},
589
0
                    }));
590
591
0
                    uno::Reference< beans::XPropertyAccess > xFilterProperties;
592
0
                    uno::Reference< ui::dialogs::XExecutableDialog > xFilterDialog;
593
0
                    uno::Reference< ui::dialogs::XAsynchronousExecutableDialog > xAsyncFilterDialog;
594
0
                    uno::Reference< document::XExporter > xExporter;
595
596
0
                    if ( bIsAsync )
597
0
                    {
598
0
                        xAsyncFilterDialog = uno::Reference< ui::dialogs::XAsynchronousExecutableDialog >(
599
0
                            comphelper::getProcessServiceFactory()->createInstanceWithArguments( aServiceName, aDialogArgs ), uno::UNO_QUERY );
600
0
                        OSL_ENSURE(xAsyncFilterDialog.is(), "ModelData_Impl::ExecuteFilterDialog_Impl: Dialog is not async!");
601
0
                        xFilterProperties = uno::Reference< beans::XPropertyAccess >( xAsyncFilterDialog, uno::UNO_QUERY );
602
0
                        xExporter = uno::Reference< document::XExporter >( xAsyncFilterDialog, uno::UNO_QUERY );
603
0
                    }
604
0
                    else
605
0
                    {
606
0
                        xFilterDialog = uno::Reference< ui::dialogs::XExecutableDialog >(
607
0
                            comphelper::getProcessServiceFactory()->createInstanceWithArguments( aServiceName, aDialogArgs ), uno::UNO_QUERY );
608
0
                        xFilterProperties = uno::Reference< beans::XPropertyAccess >( xFilterDialog, uno::UNO_QUERY );
609
0
                        xExporter = uno::Reference< document::XExporter >( xFilterDialog, uno::UNO_QUERY );
610
0
                    }
611
612
0
                    if ( xFilterProperties.is() && ( xFilterDialog.is() || xAsyncFilterDialog.is() ) )
613
0
                    {
614
0
                        bDialogUsed = true;
615
616
0
                        if( xExporter.is() )
617
0
                            xExporter->setSourceDocument( GetModel() );
618
619
0
                        uno::Sequence< beans::PropertyValue > aPropsForDialog;
620
0
                        GetMediaDescr() >> aPropsForDialog;
621
0
                        xFilterProperties->setPropertyValues( aPropsForDialog );
622
623
0
                        if ( bIsAsync )
624
0
                        {
625
0
                            m_xFilterProperties = std::move(xFilterProperties);
626
0
                            m_xFilterDialog = std::move(xAsyncFilterDialog);
627
628
0
                            auto aDialogClosedListener = rtl::Reference(new svt::DialogClosedListener());
629
0
                            aDialogClosedListener->SetDialogClosedLink( LINK( this, ModelData_Impl, OptionsDialogClosedHdl ) );
630
631
0
                            m_xFilterDialog->startExecuteModal( aDialogClosedListener );
632
0
                        }
633
0
                        else
634
0
                        {
635
0
                            if( !xFilterDialog->execute() )
636
0
                            {
637
0
                                throw task::ErrorCodeIOException(
638
0
                                    (u"ModelData_Impl::ExecuteFilterDialog_Impl:"
639
0
                                    " ERRCODE_IO_ABORT"_ustr),
640
0
                                    uno::Reference< uno::XInterface >(),
641
0
                                    sal_uInt32(ERRCODE_IO_ABORT));
642
0
                            }
643
644
0
                            const uno::Sequence< beans::PropertyValue > aPropsFromDialog =
645
0
                                                                        xFilterProperties->getPropertyValues();
646
0
                            for ( const auto& rProp : aPropsFromDialog )
647
0
                                GetMediaDescr()[rProp.Name] = rProp.Value;
648
0
                        }
649
0
                    }
650
0
                }
651
0
            }
652
0
        }
653
0
    }
654
0
    catch( const container::NoSuchElementException& e )
655
0
    {
656
        // the filter name is unknown
657
0
        throw task::ErrorCodeIOException(
658
0
            ("ModelData_Impl::ExecuteFilterDialog_Impl: NoSuchElementException"
659
0
             " \"" + e.Message + "\": ERRCODE_IO_ABORT"),
660
0
            uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_INVALIDPARAMETER));
661
0
    }
662
0
    catch( const task::ErrorCodeIOException& )
663
0
    {
664
0
        throw;
665
0
    }
666
0
    catch( const uno::Exception& )
667
0
    {
668
0
        TOOLS_WARN_EXCEPTION("sfx.doc", "ignoring");
669
0
    }
670
671
0
    return bDialogUsed;
672
0
}
673
674
void SfxStoringHelper::CallFinishGUIStoreModel()
675
0
{
676
0
    ::comphelper::SequenceAsHashMap::const_iterator aFileNameIter = m_xModelData->GetMediaDescr().find( u"URL"_ustr );
677
0
    uno::Sequence< beans::PropertyValue > aFilterProps = m_xModelData->GetPreselectedFilter_Impl( m_nStoreMode );
678
0
    const OUString aFilterFromMediaDescr = m_xModelData->GetMediaDescr().getUnpackedValueOrDefault( aFilterNameString, OUString() );
679
0
    const OUString aOldFilterName = m_xModelData->GetDocProps().getUnpackedValueOrDefault( aFilterNameString, OUString() );
680
0
    ::comphelper::SequenceAsHashMap aFilterPropsHM( aFilterProps );
681
0
    OUString aFilterName = aFilterPropsHM.getUnpackedValueOrDefault( u"Name"_ustr, OUString() );
682
683
0
    SfxStoringHelper::FinishGUIStoreModel(aFileNameIter, *m_xModelData, m_bRemote, m_nStoreMode, aFilterProps,
684
0
                                          m_bSetStandardName, m_bPreselectPassword, m_bDialogUsed,
685
0
                                          aFilterFromMediaDescr, aOldFilterName, m_aArgsSequence,
686
0
                                          aFilterName, m_nScriptingSignatureState);
687
688
0
    if (SfxViewShell::Current())
689
0
        SfxViewShell::Current()->SetStoringHelper(nullptr);
690
0
}
691
692
IMPL_LINK( ModelData_Impl, OptionsDialogClosedHdl, css::ui::dialogs::DialogClosedEvent*, pEvt, void )
693
0
{
694
0
    SfxViewShell* pNotifier = comphelper::LibreOfficeKit::isActive() ? SfxViewShell::Current() : nullptr;
695
696
0
    if (pEvt->DialogResult == RET_OK && m_xFilterProperties)
697
0
    {
698
0
        if (pNotifier)
699
0
            pNotifier->libreOfficeKitViewCallback( LOK_CALLBACK_EXPORT_FILE, "PENDING"_ostr );
700
701
0
        const uno::Sequence< beans::PropertyValue > aPropsFromDialog = m_xFilterProperties->getPropertyValues();
702
0
        for ( const auto& rProp : aPropsFromDialog )
703
0
            GetMediaDescr()[rProp.Name] = rProp.Value;
704
705
0
        m_pOwner->CallFinishGUIStoreModel();
706
0
    }
707
0
    else if (pNotifier)
708
0
    {
709
0
        pNotifier->libreOfficeKitViewCallback( LOK_CALLBACK_EXPORT_FILE, "ABORT"_ostr );
710
0
    }
711
0
}
712
713
sal_Int8 ModelData_Impl::CheckSaveAcceptable( sal_Int8 nCurStatus )
714
0
{
715
0
    sal_Int8 nResult = nCurStatus;
716
717
0
    if ( nResult != STATUS_NO_ACTION && GetStorable()->hasLocation() )
718
0
    {
719
        // the saving is acceptable
720
        // in case the configuration entry is not set or set to false
721
        // or in case of version creation
722
0
        if ( officecfg::Office::Common::Save::Document::AlwaysSaveAs::get()
723
0
          && GetMediaDescr().find( u"VersionComment"_ustr ) == GetMediaDescr().end() )
724
0
        {
725
            // notify the user that SaveAs is going to be done
726
0
            std::unique_ptr<weld::MessageDialog> xMessageBox(Application::CreateMessageDialog(SfxStoringHelper::GetModelWindow(m_xModel),
727
0
                                                             VclMessageType::Question, VclButtonsType::OkCancel, SfxResId(STR_NEW_FILENAME_SAVE)));
728
0
            if (xMessageBox->run() == RET_OK)
729
0
                nResult = STATUS_SAVEAS;
730
0
            else
731
0
                nResult = STATUS_NO_ACTION;
732
0
        }
733
0
    }
734
735
0
    return nResult;
736
0
}
737
738
739
sal_Int8 ModelData_Impl::CheckStateForSave()
740
0
{
741
    // if the document is readonly or a new one a SaveAs operation must be used
742
0
    if ( !GetStorable()->hasLocation() || GetStorable()->isReadonly() )
743
0
        return STATUS_SAVEAS;
744
745
    // check acceptable entries for media descriptor
746
0
    ::comphelper::SequenceAsHashMap aAcceptedArgs;
747
748
0
    static constexpr OUString aVersionCommentString(u"VersionComment"_ustr);
749
0
    static constexpr OUString aAuthorString(u"Author"_ustr);
750
0
    static constexpr OUString aDontTerminateEdit(u"DontTerminateEdit"_ustr);
751
0
    static constexpr OUString aInteractionHandlerString(u"InteractionHandler"_ustr);
752
0
    static constexpr OUString aStatusIndicatorString(u"StatusIndicator"_ustr);
753
0
    static constexpr OUString aFailOnWarningString(u"FailOnWarning"_ustr);
754
0
    static constexpr OUString aNoFileSync(u"NoFileSync"_ustr);
755
756
0
    if ( GetMediaDescr().find( aVersionCommentString ) != GetMediaDescr().end() )
757
0
        aAcceptedArgs[ aVersionCommentString ] = GetMediaDescr()[ aVersionCommentString ];
758
0
    if ( GetMediaDescr().find( aAuthorString ) != GetMediaDescr().end() )
759
0
        aAcceptedArgs[ aAuthorString ] = GetMediaDescr()[ aAuthorString ];
760
0
    if ( GetMediaDescr().find( aDontTerminateEdit ) != GetMediaDescr().end() )
761
0
        aAcceptedArgs[ aDontTerminateEdit ] = GetMediaDescr()[ aDontTerminateEdit ];
762
0
    if ( GetMediaDescr().find( aInteractionHandlerString ) != GetMediaDescr().end() )
763
0
        aAcceptedArgs[ aInteractionHandlerString ] = GetMediaDescr()[ aInteractionHandlerString ];
764
0
    if ( GetMediaDescr().find( aStatusIndicatorString ) != GetMediaDescr().end() )
765
0
        aAcceptedArgs[ aStatusIndicatorString ] = GetMediaDescr()[ aStatusIndicatorString ];
766
0
    if ( GetMediaDescr().find( aFailOnWarningString ) != GetMediaDescr().end() )
767
0
        aAcceptedArgs[ aFailOnWarningString ] = GetMediaDescr()[ aFailOnWarningString ];
768
0
    if (GetMediaDescr().find(aNoFileSync) != GetMediaDescr().end())
769
0
        aAcceptedArgs[aNoFileSync] = GetMediaDescr()[aNoFileSync];
770
771
    // remove unacceptable entry if there is any
772
0
    DBG_ASSERT( GetMediaDescr().size() == aAcceptedArgs.size(),
773
0
                "Unacceptable parameters are provided in Save request!\n" );
774
0
    if ( GetMediaDescr().size() != aAcceptedArgs.size() )
775
0
        GetMediaDescr() = std::move(aAcceptedArgs);
776
777
    // check that the old filter is acceptable
778
0
    return CheckFilter( GetDocProps().getUnpackedValueOrDefault(aFilterNameString, OUString()) );
779
0
}
780
781
sal_Int8 ModelData_Impl::CheckFilter( const OUString& aFilterName )
782
0
{
783
0
    ::comphelper::SequenceAsHashMap aFiltPropsHM;
784
0
    SfxFilterFlags nFiltFlags = SfxFilterFlags::NONE;
785
0
    if ( !aFilterName.isEmpty() )
786
0
    {
787
        // get properties of filter
788
0
        uno::Sequence< beans::PropertyValue > aFilterProps;
789
0
        m_pOwner->GetFilterConfiguration()->getByName( aFilterName ) >>= aFilterProps;
790
791
0
        aFiltPropsHM = ::comphelper::SequenceAsHashMap( aFilterProps );
792
0
        nFiltFlags = static_cast<SfxFilterFlags>(aFiltPropsHM.getUnpackedValueOrDefault(u"Flags"_ustr, sal_Int32(0) ));
793
0
    }
794
795
    // only a temporary solution until default filter retrieving feature is implemented
796
    // then GetDocServiceDefaultFilter() must be used
797
0
    ::comphelper::SequenceAsHashMap aDefFiltPropsHM = GetDocServiceDefaultFilterCheckFlags( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT, SfxFilterFlags::NONE );
798
0
    SfxFilterFlags nDefFiltFlags = static_cast<SfxFilterFlags>(aDefFiltPropsHM.getUnpackedValueOrDefault(u"Flags"_ustr, sal_Int32(0) ));
799
800
0
    bool bAsk = false;
801
802
    // if the old filter is not acceptable
803
    // and there is no default filter or it is not acceptable for requested parameters then proceed with saveAs
804
0
    if ( ( aFiltPropsHM.empty() || !( nFiltFlags & SfxFilterFlags::EXPORT ) )
805
0
      && ( aDefFiltPropsHM.empty() || !( nDefFiltFlags & SfxFilterFlags::EXPORT ) || nDefFiltFlags & SfxFilterFlags::INTERNAL ) )
806
0
        return STATUS_SAVEAS;
807
808
    // so at this point there is either an acceptable old filter or default one
809
0
    if ( aFiltPropsHM.empty() || !( nFiltFlags & SfxFilterFlags::EXPORT ) )
810
0
    {
811
        // so the default filter must be acceptable
812
0
        return STATUS_SAVEAS_STANDARDNAME;
813
0
    }
814
0
    else if ( ( !( nFiltFlags & SfxFilterFlags::OWN ) || ( nFiltFlags & SfxFilterFlags::ALIEN ) )
815
0
           && !aDefFiltPropsHM.empty()
816
0
           && ( nDefFiltFlags & SfxFilterFlags::EXPORT ) && !( nDefFiltFlags & SfxFilterFlags::INTERNAL ))
817
0
    {
818
0
        bAsk = true;
819
0
    }
820
821
    // check if EncryptionData supports this output format
822
0
    {
823
0
        OUString aSupportedFilters;
824
0
        const ::comphelper::SequenceAsHashMap& rDocumentProperties = GetDocProps();
825
0
        const css::uno::Sequence<css::beans::NamedValue> aEncryptionData = rDocumentProperties.getUnpackedValueOrDefault(u"EncryptionData"_ustr, css::uno::Sequence<css::beans::NamedValue>());
826
0
        if (aEncryptionData != css::uno::Sequence<css::beans::NamedValue>())
827
0
        {
828
0
            for (const css::beans::NamedValue& aNamedValue : aEncryptionData)
829
0
            {
830
0
                if (aNamedValue.Name == "SupportedFilters")
831
0
                {
832
0
                    aNamedValue.Value >>= aSupportedFilters;
833
0
                }
834
0
            }
835
0
        }
836
837
        // if 'SupportedFilters' is empty assume that all filters are supported.
838
0
        if (!aSupportedFilters.isEmpty())
839
0
        {
840
0
            const OUString aSelectedFilter = aFiltPropsHM.getUnpackedValueOrDefault(u"UIName"_ustr, OUString());
841
842
0
            aSupportedFilters = ";" + aSupportedFilters + ";";
843
0
            const OUString aSearchToken = ";" + aSelectedFilter + ";";
844
0
            bAsk = (aSupportedFilters.indexOf(aSearchToken) < 0);
845
0
        }
846
0
    }
847
848
0
    if (bAsk)
849
0
    {
850
        // the default filter is acceptable and the old filter is alien one
851
        // so ask to make a saveAs operation
852
0
        const OUString aUIName = aFiltPropsHM.getUnpackedValueOrDefault(u"UIName"_ustr, OUString() );
853
0
        const OUString aType = aFiltPropsHM.getUnpackedValueOrDefault( u"Type"_ustr, OUString() );
854
0
        const OUString aExtension = GetRecommendedExtension(aType);
855
0
        const OUString aPreusedFilterName = GetDocProps().getUnpackedValueOrDefault(u"PreusedFilterName"_ustr, OUString() );
856
0
        const OUString aDefUIName = aDefFiltPropsHM.getUnpackedValueOrDefault(u"UIName"_ustr, OUString() );
857
0
        const OUString aDefType = aDefFiltPropsHM.getUnpackedValueOrDefault( u"Type"_ustr, OUString() );
858
0
        const OUString aDefExtension = GetRecommendedExtension( aDefType );
859
860
0
        if ( aPreusedFilterName != aFilterName && aUIName != aDefUIName )
861
0
        {
862
0
            if ( !SfxStoringHelper::WarnUnacceptableFormat( GetModel(), aUIName,aExtension, aDefExtension,
863
0
                                                            static_cast<bool>( nDefFiltFlags & SfxFilterFlags::ALIEN ) ) )
864
0
                return STATUS_SAVEAS_STANDARDNAME;
865
0
        }
866
0
    }
867
868
0
    return STATUS_SAVE;
869
0
}
870
871
872
bool ModelData_Impl::CheckFilterOptionsDialogExistence()
873
0
{
874
0
    uno::Sequence< beans::NamedValue > aSearchRequest { { u"DocumentService"_ustr, css::uno::Any(GetDocServiceName()) } };
875
876
0
    uno::Reference< container::XEnumeration > xFilterEnum =
877
0
                                    m_pOwner->GetFilterQuery()->createSubSetEnumerationByProperties( aSearchRequest );
878
879
0
    while ( xFilterEnum->hasMoreElements() )
880
0
    {
881
0
        uno::Sequence< beans::PropertyValue > aProps;
882
0
        if ( xFilterEnum->nextElement() >>= aProps )
883
0
        {
884
0
            ::comphelper::SequenceAsHashMap aPropsHM( aProps );
885
0
            if ( !aPropsHM.getUnpackedValueOrDefault(u"UIComponent"_ustr, OUString()).isEmpty() )
886
0
                return true;
887
0
        }
888
0
    }
889
890
0
    return false;
891
0
}
892
893
894
bool ModelData_Impl::OutputFileDialog( sal_Int16 nStoreMode,
895
                                            const ::comphelper::SequenceAsHashMap& aPreselectedFilterPropsHM,
896
                                            bool bSetStandardName,
897
                                            OUString& aSuggestedName,
898
                                            bool bPreselectPassword,
899
                                            OUString& aSuggestedDir,
900
                                            sal_Int16 nDialog,
901
                                            const css::uno::Sequence<OUString>& rDenyList,
902
                                            SignatureState const nScriptingSignatureState)
903
0
{
904
0
    if ( nStoreMode == SAVEASREMOTE_REQUESTED )
905
0
        nStoreMode = SAVEAS_REQUESTED;
906
907
0
    bool bUseFilterOptions = false;
908
909
0
    ::comphelper::SequenceAsHashMap::const_iterator aOverwriteIter =
910
0
                GetMediaDescr().find( u"Overwrite"_ustr );
911
912
    // the file name must be specified if overwrite option is set
913
0
    if ( aOverwriteIter != GetMediaDescr().end() )
914
0
           throw task::ErrorCodeIOException(
915
0
               u"ModelData_Impl::OutputFileDialog: ERRCODE_IO_INVALIDPARAMETER"_ustr,
916
0
               uno::Reference< uno::XInterface >(),
917
0
               sal_uInt32(ERRCODE_IO_INVALIDPARAMETER));
918
919
    // no target file name is specified
920
    // we need to show the file dialog
921
922
    // check if we have a filter which allows for filter options, so we need a corresponding checkbox in the dialog
923
0
    bool bAllowOptions = false;
924
925
    // in case of Export, filter options dialog is used if available
926
0
    if( !( nStoreMode & EXPORT_REQUESTED ) || ( nStoreMode & WIDEEXPORT_REQUESTED ) )
927
0
        bAllowOptions = CheckFilterOptionsDialogExistence();
928
929
    // get the filename by dialog ...
930
    // create the file dialog
931
0
    sal_Int16  aDialogMode = bAllowOptions
932
0
        ? css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS
933
0
        : css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD;
934
0
    FileDialogFlags aDialogFlags = FileDialogFlags::NONE;
935
936
0
    if( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) )
937
0
    {
938
0
        if ( (nStoreMode & PDFEXPORT_REQUESTED) || (nStoreMode & EPUBEXPORT_REQUESTED) )
939
0
            aDialogMode = css::ui::dialogs::TemplateDescription::
940
0
                FILESAVE_AUTOEXTENSION;
941
0
        else
942
0
            aDialogMode = css::ui::dialogs::TemplateDescription::
943
0
                FILESAVE_AUTOEXTENSION_SELECTION;
944
0
        aDialogFlags = FileDialogFlags::Export;
945
0
    }
946
947
0
    if( ( nStoreMode & EXPORT_REQUESTED ) && ( nStoreMode & SAVEACOPY_REQUESTED ) && ( nStoreMode & WIDEEXPORT_REQUESTED ) )
948
0
    {
949
0
        aDialogFlags = FileDialogFlags::SaveACopy;
950
0
    }
951
952
0
    std::unique_ptr<sfx2::FileDialogHelper> pFileDlg;
953
954
0
    const OUString aDocServiceName {GetDocServiceName()};
955
0
    DBG_ASSERT( !aDocServiceName.isEmpty(), "No document service for this module set!" );
956
957
0
    SfxFilterFlags nMust = getMustFlags( nStoreMode );
958
0
    SfxFilterFlags nDont = getDontFlags( nStoreMode );
959
0
    weld::Window* pFrameWin = SfxStoringHelper::GetModelWindow(m_xModel);
960
0
    OUString sPreselectedDir
961
0
        = GetDocProps().getUnpackedValueOrDefault("ExportDirectory", OUString());
962
963
    // Fall back to the document base URL - but only if the document is not based on a template.
964
    // Otherwise the template's directory would be used, which is not what we want.
965
0
    if (SfxViewShell* pViewShell = SfxViewShell::Current())
966
0
    {
967
0
        SfxObjectShell* pDocShell = pViewShell->GetObjectShell();
968
0
        if (pDocShell && !pDocShell->IsBasedOnTemplate())
969
0
        {
970
0
            if (sPreselectedDir.isEmpty())
971
0
                sPreselectedDir = GetDocProps().getUnpackedValueOrDefault("DocumentBaseURL", OUString());
972
0
            if (sPreselectedDir.isEmpty() && GetStorable()->hasLocation())
973
0
                sPreselectedDir = GetStorable()->getLocation();
974
0
        }
975
0
    }
976
977
0
    INetURLObject aObj(sPreselectedDir);
978
0
    aObj.removeSegment(); // remove file name from URL
979
0
    sPreselectedDir = aObj.GetMainURL(INetURLObject::DecodeMechanism::NONE);
980
0
    if ( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) )
981
0
    {
982
0
        if ( ( nStoreMode & PDFEXPORT_REQUESTED ) && !aPreselectedFilterPropsHM.empty() )
983
0
        {
984
            // this is a PDF export
985
            // the filter options has been shown already
986
0
            const OUString aFilterUIName = aPreselectedFilterPropsHM.getUnpackedValueOrDefault( u"UIName"_ustr, OUString() );
987
0
            pFileDlg.reset(new sfx2::FileDialogHelper( aDialogMode, aDialogFlags, aFilterUIName, u"pdf", sPreselectedDir, rDenyList, pFrameWin ));
988
0
            pFileDlg->SetCurrentFilter( aFilterUIName );
989
0
        }
990
0
        else if ((nStoreMode & EPUBEXPORT_REQUESTED) && !aPreselectedFilterPropsHM.empty())
991
0
        {
992
            // This is an EPUB export, the filter options has been shown already.
993
0
            const OUString aFilterUIName = aPreselectedFilterPropsHM.getUnpackedValueOrDefault( u"UIName"_ustr, OUString() );
994
0
            pFileDlg.reset(new sfx2::FileDialogHelper(aDialogMode, aDialogFlags, aFilterUIName, u"epub", sPreselectedDir, rDenyList, pFrameWin));
995
0
            pFileDlg->SetCurrentFilter(aFilterUIName);
996
0
        }
997
0
        else
998
0
        {
999
            // This is the normal dialog
1000
0
            pFileDlg.reset(new sfx2::FileDialogHelper( aDialogMode, aDialogFlags, aDocServiceName, nDialog, nMust, nDont, sPreselectedDir, rDenyList, pFrameWin ));
1001
0
        }
1002
1003
0
        sfx2::FileDialogHelper::Context eCtxt = sfx2::FileDialogHelper::UnknownContext;
1004
0
        if ( aDocServiceName == "com.sun.star.drawing.DrawingDocument" )
1005
0
            eCtxt = sfx2::FileDialogHelper::DrawExport;
1006
0
        else if ( aDocServiceName == "com.sun.star.presentation.PresentationDocument" )
1007
0
            eCtxt = sfx2::FileDialogHelper::ImpressExport;
1008
0
        else if ( aDocServiceName == "com.sun.star.text.TextDocument" )
1009
0
            eCtxt = sfx2::FileDialogHelper::WriterExport;
1010
0
        else if ( aDocServiceName == "com.sun.star.sheet.SpreadsheetDocument" )
1011
0
            eCtxt = sfx2::FileDialogHelper::CalcExport;
1012
0
        if ( eCtxt != sfx2::FileDialogHelper::UnknownContext )
1013
0
               pFileDlg->SetContext( eCtxt );
1014
1015
0
        pFileDlg->CreateMatcher( aDocServiceName );
1016
1017
0
        uno::Reference< ui::dialogs::XFilePicker3 > xFilePicker = pFileDlg->GetFilePicker();
1018
0
        uno::Reference< ui::dialogs::XFilePickerControlAccess > xControlAccess( xFilePicker, uno::UNO_QUERY );
1019
1020
0
        if ( xControlAccess.is() )
1021
0
        {
1022
0
            xControlAccess->setLabel( ui::dialogs::CommonFilePickerElementIds::PUSHBUTTON_OK, SfxResId(STR_EXPORTBUTTON) );
1023
0
            xControlAccess->setLabel( ui::dialogs::CommonFilePickerElementIds::LISTBOX_FILTER_LABEL, SfxResId(STR_LABEL_FILEFORMAT) );
1024
0
        }
1025
0
    }
1026
0
    else
1027
0
    {
1028
        // This is the normal save as dialog
1029
0
        pFileDlg.reset(new sfx2::FileDialogHelper( aDialogMode, aDialogFlags, aDocServiceName, nDialog,
1030
0
            nMust, nDont, sPreselectedDir, rDenyList, pFrameWin ));
1031
0
        pFileDlg->CreateMatcher( aDocServiceName );
1032
1033
0
        sfx2::FileDialogHelper::Context eCtxt = sfx2::FileDialogHelper::UnknownContext;
1034
0
        if ( aDocServiceName == "com.sun.star.drawing.DrawingDocument" )
1035
0
            eCtxt = sfx2::FileDialogHelper::DrawSaveAs;
1036
0
        else if ( aDocServiceName == "com.sun.star.presentation.PresentationDocument" )
1037
0
            eCtxt = sfx2::FileDialogHelper::ImpressSaveAs;
1038
0
        else if ( aDocServiceName == "com.sun.star.text.TextDocument" )
1039
0
            eCtxt = sfx2::FileDialogHelper::WriterSaveAs;
1040
0
        else if ( aDocServiceName == "com.sun.star.sheet.SpreadsheetDocument" )
1041
0
            eCtxt = sfx2::FileDialogHelper::CalcSaveAs;
1042
1043
0
        if ( eCtxt != sfx2::FileDialogHelper::UnknownContext )
1044
0
               pFileDlg->SetContext( eCtxt );
1045
0
    }
1046
1047
0
    OUString aAdjustToType;
1048
1049
0
    const OUString sFilterNameString(aFilterNameString);
1050
1051
0
    if ( ( nStoreMode & EXPORT_REQUESTED ) && !( nStoreMode & WIDEEXPORT_REQUESTED ) )
1052
0
    {
1053
        // it is export, set the preselected filter
1054
0
        pFileDlg->SetCurrentFilter( aPreselectedFilterPropsHM.getUnpackedValueOrDefault( u"UIName"_ustr, OUString() ) );
1055
0
        aAdjustToType = aPreselectedFilterPropsHM.getUnpackedValueOrDefault( u"Type"_ustr, OUString() );
1056
0
    }
1057
    // it is no export, bSetStandardName == true means that user agreed to store document in the default (default default ;-)) format
1058
0
    else if ( bSetStandardName || GetStorable()->hasLocation() )
1059
0
    {
1060
0
        uno::Sequence< beans::PropertyValue > aOldFilterProps;
1061
0
        const OUString aOldFilterName = GetDocProps().getUnpackedValueOrDefault( sFilterNameString, OUString() );
1062
1063
0
        if ( !aOldFilterName.isEmpty() )
1064
0
            m_pOwner->GetFilterConfiguration()->getByName( aOldFilterName ) >>= aOldFilterProps;
1065
1066
0
        ::comphelper::SequenceAsHashMap aOldFiltPropsHM( aOldFilterProps );
1067
0
        SfxFilterFlags nOldFiltFlags = static_cast<SfxFilterFlags>(aOldFiltPropsHM.getUnpackedValueOrDefault(u"Flags"_ustr, sal_Int32(0) ));
1068
1069
0
        if ( bSetStandardName || ( nOldFiltFlags & nMust ) != nMust || bool(nOldFiltFlags & nDont) )
1070
0
        {
1071
            // the suggested type will be changed, the extension should be adjusted
1072
0
            aAdjustToType = aPreselectedFilterPropsHM.getUnpackedValueOrDefault( u"Type"_ustr, OUString() );
1073
0
            pFileDlg->SetCurrentFilter( aPreselectedFilterPropsHM.getUnpackedValueOrDefault( u"UIName"_ustr, OUString() ) );
1074
0
        }
1075
0
        else
1076
0
        {
1077
0
            pFileDlg->SetCurrentFilter( aOldFiltPropsHM.getUnpackedValueOrDefault(
1078
0
                                                        u"UIName"_ustr,
1079
0
                                                        OUString() ) );
1080
0
        }
1081
0
    }
1082
1083
0
    if ( !aSuggestedDir.isEmpty() )
1084
0
        pFileDlg->SetDisplayFolder( aSuggestedDir );
1085
0
    const OUString aRecommendedName {GetRecommendedName( aSuggestedName, aAdjustToType )};
1086
0
    if ( !aRecommendedName.isEmpty() )
1087
0
        pFileDlg->SetFileName( aRecommendedName );
1088
1089
0
    uno::Reference < view::XSelectionSupplier > xSel( GetModel()->getCurrentController(), uno::UNO_QUERY );
1090
0
    if ( xSel.is() && xSel->getSelection().hasValue() )
1091
0
        GetMediaDescr()[u"SelectionOnly"_ustr] <<= true;
1092
1093
    // This is a temporary hardcoded solution must be removed when
1094
    // dialogs do not need parameters in SidSet representation any more
1095
0
    sal_uInt16 nSlotID = getSlotIDFromMode( nStoreMode );
1096
0
    if ( !nSlotID )
1097
0
        throw lang::IllegalArgumentException(); // TODO:
1098
1099
    // generate SidSet from MediaDescriptor and provide it into FileDialog
1100
    // than merge changed SidSet back
1101
0
    std::optional<SfxAllItemSet> pDialogParams( SfxGetpApp()->GetPool() );
1102
0
    TransformParameters( nSlotID,
1103
0
                         GetMediaDescr().getAsConstPropertyValueList(),
1104
0
                         *pDialogParams );
1105
1106
0
    if ( bPreselectPassword && !pDialogParams->HasItem( SID_ENCRYPTIONDATA ) )
1107
0
    {
1108
        // the file dialog preselects the password checkbox if the provided mediadescriptor has encryption data entry
1109
        // after dialog execution the password interaction flag will be either removed or not
1110
0
        pDialogParams->Put( SfxBoolItem( SID_PASSWORDINTERACTION, true ) );
1111
0
    }
1112
1113
    // aFilterName is a pure output parameter, pDialogParams is an in/out parameter
1114
0
    OUString aFilterName;
1115
    // in LOK case we don't show File Picker so it will fail, but execute to do other preparations
1116
0
    if (pFileDlg->Execute(pDialogParams, aFilterName, nScriptingSignatureState) != ERRCODE_NONE
1117
0
        && !comphelper::LibreOfficeKit::isActive() )
1118
0
    {
1119
0
        throw task::ErrorCodeIOException(
1120
0
            u"ModelData_Impl::OutputFileDialog: ERRCODE_IO_ABORT"_ustr,
1121
0
            uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_ABORT));
1122
0
    }
1123
0
    else if (comphelper::LibreOfficeKit::isActive())
1124
0
    {
1125
0
        aFilterName = aPreselectedFilterPropsHM.getUnpackedValueOrDefault( u"Name"_ustr, OUString() );
1126
0
    }
1127
1128
    // the following two arguments can not be converted in MediaDescriptor,
1129
    // so they should be removed from the ItemSet after retrieving
1130
0
    const SfxBoolItem* pRecommendReadOnly = pDialogParams->GetItem(SID_RECOMMENDREADONLY, false);
1131
0
    m_bRecommendReadOnly = ( pRecommendReadOnly && pRecommendReadOnly->GetValue() );
1132
0
    pDialogParams->ClearItem( SID_RECOMMENDREADONLY );
1133
1134
0
    const SfxBoolItem* pSignWithDefaultKey = pDialogParams->GetItem(SID_GPGSIGN, false);
1135
0
    m_bSignWithDefaultSignature = (pSignWithDefaultKey && pSignWithDefaultKey->GetValue());
1136
0
    pDialogParams->ClearItem( SID_GPGSIGN );
1137
1138
0
    GetMediaDescr() = TransformItems(nSlotID, *pDialogParams);
1139
1140
    // get the path from the dialog
1141
0
    INetURLObject aURL( pFileDlg->GetPath() );
1142
1143
0
    if (comphelper::LibreOfficeKit::isActive())
1144
0
    {
1145
#ifdef IOS
1146
        // The iOS app (and maybe the Android app) have fails to set the URL to
1147
        // save to so we need to set it to a temporary file.
1148
        // Note: the iOS app is responsible for deleting the temporary file.
1149
        if (nStoreMode & EXPORT_REQUESTED && aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE).isEmpty())
1150
        {
1151
            // Mirror the "export/docbasename.pdf" path format to match the
1152
            // format used in the "downloadas" message handler in the iOS app's
1153
            // -[DocumentViewController userContentController:didReceiveScriptMessage]
1154
            // selector.
1155
            // Important note: temporary files created here must be in their
1156
            // own subdirectory since the iOS app's UIDocumentPickerDelegate
1157
            // will try to delete both the temporary file and its parent
1158
            // directory.
1159
            OUString aFullName = u"export/" + aRecommendedName;
1160
            OUString aBaseName;
1161
            OUString aExtension;
1162
            sal_Int32 nPos = aFullName.lastIndexOf( '.' );
1163
            if ( nPos >= 0 )
1164
            {
1165
                aBaseName = aFullName.copy(0, nPos);
1166
                aExtension = aFullName.copy(nPos, aFullName.getLength() - nPos);
1167
            }
1168
            aURL = INetURLObject(::utl::CreateTempURL( aBaseName, false, aExtension, nullptr, true));
1169
1170
            // Remove any stale files left from a previous export
1171
            OUString fileURL = aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
1172
            if (!fileURL.isEmpty())
1173
                osl::File::remove(fileURL);
1174
        }
1175
        else
1176
        {
1177
#endif
1178
            // keep name with extension
1179
0
            aSuggestedName = aRecommendedName;
1180
0
            OUString aExtension;
1181
0
            if (size_t nPos = aSuggestedName.lastIndexOf('.') + 1)
1182
0
                aExtension = aSuggestedName.copy(nPos, aSuggestedName.getLength() - nPos);
1183
0
            aURL.SetExtension(aExtension);
1184
#ifdef IOS
1185
        }
1186
#endif
1187
0
    }
1188
0
    else
1189
0
    {
1190
        // the path should be provided outside since it might be used for further calls to the dialog
1191
0
        aSuggestedName = aURL.GetLastName(INetURLObject::DecodeMechanism::WithCharset);
1192
0
    }
1193
0
    aSuggestedDir = pFileDlg->GetDisplayDirectory();
1194
1195
    // old filter options should be cleared in case different filter is used
1196
1197
0
    const OUString aFilterFromMediaDescr = GetMediaDescr().getUnpackedValueOrDefault( sFilterNameString, OUString() );
1198
0
    const OUString aOldFilterName = GetDocProps().getUnpackedValueOrDefault( sFilterNameString, OUString() );
1199
1200
0
    if ( aFilterName == aFilterFromMediaDescr )
1201
0
    {
1202
        // preserve current settings if any
1203
        // if there no current settings and the name is the same
1204
        // as old filter name use old filter settings
1205
1206
0
        if ( aFilterFromMediaDescr == aOldFilterName )
1207
0
        {
1208
0
            ::comphelper::SequenceAsHashMap::const_iterator aIter =
1209
0
                                        GetDocProps().find( aFilterOptionsString );
1210
0
            if ( aIter != GetDocProps().end()
1211
0
              && GetMediaDescr().find( aFilterOptionsString ) == GetMediaDescr().end() )
1212
0
                GetMediaDescr()[aIter->first] = aIter->second;
1213
1214
0
            aIter = GetDocProps().find( aFilterDataString );
1215
0
            if ( aIter != GetDocProps().end()
1216
0
              && GetMediaDescr().find( aFilterDataString ) == GetMediaDescr().end() )
1217
0
                GetMediaDescr()[aIter->first] = aIter->second;
1218
0
        }
1219
0
    }
1220
0
    else
1221
0
    {
1222
0
        GetMediaDescr().erase( aFilterDataString );
1223
0
        GetMediaDescr().erase( aFilterOptionsString );
1224
1225
0
        if ( aFilterName == aOldFilterName )
1226
0
        {
1227
            // merge filter option of the document filter
1228
1229
0
            ::comphelper::SequenceAsHashMap::const_iterator aIter =
1230
0
                                GetDocProps().find( aFilterOptionsString );
1231
0
            if ( aIter != GetDocProps().end() )
1232
0
                GetMediaDescr()[aIter->first] = aIter->second;
1233
1234
0
            aIter = GetDocProps().find( aFilterDataString );
1235
0
            if ( aIter != GetDocProps().end() )
1236
0
                GetMediaDescr()[aIter->first] = aIter->second;
1237
0
        }
1238
0
    }
1239
1240
0
    uno::Reference< ui::dialogs::XFilePickerControlAccess > xExtFileDlg( pFileDlg->GetFilePicker(), uno::UNO_QUERY );
1241
0
    if ( xExtFileDlg.is() )
1242
0
    {
1243
0
        if ( SfxStoringHelper::CheckFilterOptionsAppearance( m_pOwner->GetFilterConfiguration(), aFilterName ) )
1244
0
            bUseFilterOptions = true;
1245
1246
0
        if ( ( !( nStoreMode & EXPORT_REQUESTED ) || ( nStoreMode & WIDEEXPORT_REQUESTED ) ) && bUseFilterOptions )
1247
0
        {
1248
0
            try
1249
0
            {
1250
                // for exporters: always show dialog if format uses options
1251
                // for save: show dialog if format uses options and no options given or if forced by user
1252
0
                uno::Any aVal =
1253
0
                        xExtFileDlg->getValue( ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS, 0 );
1254
1255
0
                aVal >>= bUseFilterOptions;
1256
0
                if ( !bUseFilterOptions )
1257
0
                    bUseFilterOptions =
1258
0
                      ( GetMediaDescr().find( aFilterDataString ) == GetMediaDescr().end()
1259
0
                      && GetMediaDescr().find( aFilterOptionsString ) == GetMediaDescr().end() );
1260
0
            }
1261
0
            catch( const lang::IllegalArgumentException& )
1262
0
            {}
1263
0
        }
1264
0
    }
1265
1266
    // merge in results of the dialog execution
1267
0
    GetMediaDescr()[u"URL"_ustr] <<= aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1268
0
    GetMediaDescr()[sFilterNameString] <<= aFilterName;
1269
1270
    // for Export - keep a runtime var for each document where the document was last exported to
1271
0
    if (GetStorable()->hasLocation() && (nStoreMode & EXPORT_REQUESTED))
1272
0
    {
1273
0
        uno::Sequence< beans::PropertyValue > descriptor{
1274
0
            beans::PropertyValue(u"ExportDirectory"_ustr,
1275
0
                -1, uno::Any(aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE )), beans::PropertyState_DIRECT_VALUE),
1276
0
        };
1277
0
        GetModel()->setArgs(descriptor);
1278
0
    }
1279
1280
0
    return bUseFilterOptions;
1281
0
}
1282
1283
1284
bool ModelData_Impl::ShowDocumentInfoDialog()
1285
0
{
1286
0
    bool bDialogUsed = false;
1287
1288
0
    try {
1289
0
        uno::Reference< frame::XController > xController = GetModel()->getCurrentController();
1290
0
        if ( xController.is() )
1291
0
        {
1292
0
            uno::Reference< frame::XDispatchProvider > xFrameDispatch( xController->getFrame(), uno::UNO_QUERY );
1293
0
            if ( xFrameDispatch.is() )
1294
0
            {
1295
0
                util::URL aURL;
1296
0
                aURL.Complete = ".uno:SetDocumentProperties";
1297
1298
0
                uno::Reference < util::XURLTransformer > xTransformer( util::URLTransformer::create( comphelper::getProcessComponentContext() ) );
1299
0
                if ( xTransformer->parseStrict( aURL ) )
1300
0
                {
1301
0
                    uno::Reference< frame::XDispatch > xDispatch = xFrameDispatch->queryDispatch(
1302
0
                                                                                aURL,
1303
0
                                                                                u"_self"_ustr,
1304
0
                                                                                0 );
1305
0
                    if ( xDispatch.is() )
1306
0
                    {
1307
                        // tdf#119206 use (abuse?) a SynchronMode of true,
1308
                        // which will become SfxRequest::IsSynchronCall of true
1309
                        // in SfxObjectShell::ExecFile_Impl to request that we
1310
                        // do not want the properties dialog to be run async
1311
0
                        uno::Sequence< beans::PropertyValue > aProperties{
1312
0
                            comphelper::makePropertyValue(u"SynchronMode"_ustr, true)
1313
0
                        };
1314
0
                        xDispatch->dispatch(aURL, aProperties);
1315
0
                        bDialogUsed = true;
1316
0
                    }
1317
0
                }
1318
0
            }
1319
0
        }
1320
0
    }
1321
0
    catch ( const uno::Exception& )
1322
0
    {
1323
0
    }
1324
1325
0
    return bDialogUsed;
1326
0
}
1327
1328
1329
OUString ModelData_Impl::GetRecommendedExtension( const OUString& aTypeName )
1330
0
{
1331
0
    if ( aTypeName.isEmpty() )
1332
0
       return OUString();
1333
1334
0
    uno::Reference< container::XNameAccess > xTypeDetection(
1335
0
       comphelper::getProcessServiceFactory()->createInstance(u"com.sun.star.document.TypeDetection"_ustr),
1336
0
       uno::UNO_QUERY );
1337
0
    if ( xTypeDetection.is() )
1338
0
    {
1339
0
       uno::Sequence< beans::PropertyValue > aTypeNameProps;
1340
0
       if ( ( xTypeDetection->getByName( aTypeName ) >>= aTypeNameProps ) && aTypeNameProps.hasElements() )
1341
0
       {
1342
0
           ::comphelper::SequenceAsHashMap aTypeNamePropsHM( aTypeNameProps );
1343
0
           uno::Sequence< OUString > aExtensions = aTypeNamePropsHM.getUnpackedValueOrDefault(
1344
0
                                           u"Extensions"_ustr,
1345
0
                                           ::uno::Sequence< OUString >() );
1346
0
           if ( aExtensions.hasElements() )
1347
0
               return aExtensions[0];
1348
0
       }
1349
0
    }
1350
1351
0
    return OUString();
1352
0
}
1353
1354
OUString ModelData_Impl::GetRecommendedName( const OUString& aSuggestedName, const OUString& aTypeName )
1355
0
{
1356
    // the last used name might be provided by aSuggestedName from the old selection, or from the MediaDescriptor
1357
0
    if ( !aSuggestedName.isEmpty() )
1358
0
        return aSuggestedName;
1359
1360
0
    OUString aRecommendedName{ INetURLObject(GetStorable()->getLocation())
1361
0
                                   .GetLastName(INetURLObject::DecodeMechanism::WithCharset) };
1362
0
    if ( aRecommendedName.isEmpty() )
1363
0
    {
1364
0
        try {
1365
0
            uno::Reference< frame::XTitle > xTitle( GetModel(), uno::UNO_QUERY_THROW );
1366
0
            aRecommendedName = xTitle->getTitle();
1367
0
        } catch( const uno::Exception& ) {}
1368
0
    }
1369
1370
0
    if ( !aRecommendedName.isEmpty() && !aTypeName.isEmpty() )
1371
0
    {
1372
        // adjust the extension to the type
1373
0
        uno::Reference< container::XNameAccess > xTypeDetection(
1374
0
            comphelper::getProcessServiceFactory()->createInstance(u"com.sun.star.document.TypeDetection"_ustr),
1375
0
            uno::UNO_QUERY );
1376
0
        if ( xTypeDetection.is() )
1377
0
        {
1378
0
            INetURLObject aObj( rtl::Concat2View("c:/" + aRecommendedName), INetProtocol::File,
1379
0
                    INetURLObject::EncodeMechanism::All, RTL_TEXTENCODING_UTF8, FSysStyle::Dos );
1380
1381
0
            const OUString aExtension = GetRecommendedExtension( aTypeName );
1382
0
            if ( !aExtension.isEmpty() )
1383
0
                aObj.SetExtension( aExtension );
1384
1385
0
            aRecommendedName = aObj.GetLastName(INetURLObject::DecodeMechanism::WithCharset);
1386
0
        }
1387
0
    }
1388
1389
0
    return aRecommendedName;
1390
0
}
1391
1392
SfxStoringHelper::SfxStoringHelper()
1393
0
    : m_bRemote(false)
1394
0
    , m_bPreselectPassword(false)
1395
0
    , m_bDialogUsed(false)
1396
0
    , m_bSetStandardName(false)
1397
0
    , m_nStoreMode(0)
1398
0
{
1399
0
}
1400
1401
uno::Reference< container::XNameAccess > const & SfxStoringHelper::GetFilterConfiguration()
1402
0
{
1403
0
    if ( !m_xFilterCFG.is() )
1404
0
    {
1405
0
        m_xFilterCFG.set( comphelper::getProcessServiceFactory()->createInstance(u"com.sun.star.document.FilterFactory"_ustr),
1406
0
                          uno::UNO_QUERY_THROW );
1407
0
    }
1408
1409
0
    return m_xFilterCFG;
1410
0
}
1411
1412
uno::Reference< container::XContainerQuery > const & SfxStoringHelper::GetFilterQuery()
1413
0
{
1414
0
    if ( !m_xFilterQuery.is() )
1415
0
    {
1416
0
        m_xFilterQuery.set( GetFilterConfiguration(), uno::UNO_QUERY_THROW );
1417
0
    }
1418
1419
0
    return m_xFilterQuery;
1420
0
}
1421
1422
uno::Reference< css::frame::XModuleManager2 > const & SfxStoringHelper::GetModuleManager()
1423
0
{
1424
0
    if ( !m_xModuleManager.is() )
1425
0
    {
1426
0
        m_xModuleManager = frame::ModuleManager::create(
1427
0
            comphelper::getProcessComponentContext() );
1428
0
    }
1429
1430
0
    return m_xModuleManager;
1431
0
}
1432
1433
bool SfxStoringHelper::GUIStoreModel( const uno::Reference< frame::XModel2 >& xModel,
1434
                                            std::u16string_view aSlotName,
1435
                                            uno::Sequence< beans::PropertyValue >& aArgsSequence,
1436
                                            bool bPreselectPassword,
1437
                                            SignatureState const nDocumentSignatureState,
1438
                                            SignatureState const nScriptingSignatureState,
1439
                                            bool bIsAsync)
1440
0
{
1441
0
    m_xModelData = std::make_shared<ModelData_Impl>( *this, xModel, aArgsSequence );
1442
0
    m_aArgsSequence = aArgsSequence;
1443
0
    ModelData_Impl& aModelData = *m_xModelData;
1444
1445
0
    m_bDialogUsed = false;
1446
1447
0
    m_bSetStandardName = false; // can be set only for SaveAs
1448
0
    m_bPreselectPassword = bPreselectPassword;
1449
0
    m_nScriptingSignatureState = nScriptingSignatureState;
1450
1451
    // parse the slot name
1452
0
    m_bRemote = false;
1453
0
    m_nStoreMode = getStoreModeFromSlotName( aSlotName );
1454
1455
0
    if ( m_nStoreMode == SAVEASREMOTE_REQUESTED )
1456
0
    {
1457
0
        m_nStoreMode = SAVEAS_REQUESTED;
1458
0
        m_bRemote = true;
1459
0
    }
1460
1461
0
    sal_Int8 nStatusSave = STATUS_NO_ACTION;
1462
1463
0
    ::comphelper::SequenceAsHashMap::const_iterator aSaveACopyIter =
1464
0
                        aModelData.GetMediaDescr().find( u"SaveACopy"_ustr );
1465
0
    if ( aSaveACopyIter != aModelData.GetMediaDescr().end() )
1466
0
    {
1467
0
        bool bSaveACopy = false;
1468
0
        aSaveACopyIter->second >>= bSaveACopy;
1469
0
        if ( bSaveACopy )
1470
0
            m_nStoreMode = EXPORT_REQUESTED | SAVEACOPY_REQUESTED | WIDEEXPORT_REQUESTED;
1471
0
    }
1472
    // handle the special cases
1473
0
    if ( m_nStoreMode & SAVEAS_REQUESTED )
1474
0
    {
1475
0
        ::comphelper::SequenceAsHashMap::const_iterator aSaveToIter =
1476
0
                        aModelData.GetMediaDescr().find( u"SaveTo"_ustr );
1477
0
        if ( aSaveToIter != aModelData.GetMediaDescr().end() )
1478
0
        {
1479
0
            bool bWideExport = false;
1480
0
            aSaveToIter->second >>= bWideExport;
1481
0
            if ( bWideExport )
1482
0
                m_nStoreMode = EXPORT_REQUESTED | WIDEEXPORT_REQUESTED;
1483
0
        }
1484
1485
        // if saving is not acceptable the warning must be shown even in case of SaveAs operation
1486
0
        if ( ( m_nStoreMode & SAVEAS_REQUESTED ) && aModelData.CheckSaveAcceptable( STATUS_SAVEAS ) == STATUS_NO_ACTION )
1487
0
            throw task::ErrorCodeIOException(
1488
0
                u"SfxStoringHelper::GUIStoreModel: ERRCODE_IO_ABORT"_ustr,
1489
0
                uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_ABORT));
1490
0
    }
1491
0
    else if ( m_nStoreMode & SAVE_REQUESTED )
1492
0
    {
1493
        // if saving is not acceptable by the configuration the warning must be shown
1494
0
        nStatusSave = aModelData.CheckSaveAcceptable( STATUS_SAVE );
1495
1496
0
        if ( nStatusSave == STATUS_NO_ACTION )
1497
0
            throw task::ErrorCodeIOException(
1498
0
                u"SfxStoringHelper::GUIStoreModel: ERRCODE_IO_ABORT"_ustr,
1499
0
                uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_ABORT));
1500
0
        else if ( nStatusSave == STATUS_SAVE )
1501
0
        {
1502
            // check whether it is possible to use save operation
1503
0
            nStatusSave = aModelData.CheckStateForSave();
1504
0
        }
1505
1506
0
        if ( nStatusSave == STATUS_NO_ACTION )
1507
0
        {
1508
0
            throw task::ErrorCodeIOException(
1509
0
                u"SfxStoringHelper::GUIStoreModel: ERRCODE_IO_ABORT"_ustr,
1510
0
                uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_ABORT));
1511
0
        }
1512
0
        else if ( nStatusSave != STATUS_SAVE )
1513
0
        {
1514
            // this should be a usual SaveAs operation
1515
0
            m_nStoreMode = SAVEAS_REQUESTED;
1516
0
            if ( nStatusSave == STATUS_SAVEAS_STANDARDNAME )
1517
0
                m_bSetStandardName = true;
1518
0
        }
1519
0
    }
1520
1521
0
    if (!comphelper::LibreOfficeKit::isActive() && !( m_nStoreMode & EXPORT_REQUESTED ) && SfxViewShell::Current() )
1522
0
    {
1523
0
        SfxObjectShell* pDocShell = SfxViewShell::Current()->GetObjectShell();
1524
1525
        // if it is no export, warn user that the signature will be removed
1526
0
        if (  !pDocShell->IsRememberingSignature()
1527
0
           && (SignatureState::OK == nDocumentSignatureState
1528
0
           || SignatureState::INVALID == nDocumentSignatureState
1529
0
           || SignatureState::NOTVALIDATED == nDocumentSignatureState
1530
0
           || SignatureState::PARTIAL_OK == nDocumentSignatureState) )
1531
0
        {
1532
0
            std::unique_ptr<weld::MessageDialog> xMessageBox(Application::CreateMessageDialog(SfxStoringHelper::GetModelWindow(xModel),
1533
0
                                                             VclMessageType::Question, VclButtonsType::YesNo, SfxResId(RID_SVXSTR_XMLSEC_QUERY_LOSINGSIGNATURE)));
1534
0
            if (xMessageBox->run() != RET_YES)
1535
0
            {
1536
                // the user has decided not to store the document
1537
0
                throw task::ErrorCodeIOException(
1538
0
                    u"SfxStoringHelper::GUIStoreModel: ERRCODE_IO_ABORT (Preserve Signature)"_ustr,
1539
0
                    uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_ABORT));
1540
0
            }
1541
0
        }
1542
0
    }
1543
1544
0
    if ( m_nStoreMode & SAVE_REQUESTED && nStatusSave == STATUS_SAVE )
1545
0
    {
1546
        // Document properties can contain streams that should be freed before storing
1547
0
        aModelData.FreeDocumentProps();
1548
1549
0
        if ( aModelData.GetStorable2().is() )
1550
0
        {
1551
0
            try
1552
0
            {
1553
0
                aModelData.GetStorable2()->storeSelf( aModelData.GetMediaDescr().getAsConstPropertyValueList() );
1554
0
            }
1555
0
            catch (const lang::IllegalArgumentException&)
1556
0
            {
1557
0
                TOOLS_WARN_EXCEPTION("sfx.doc", "Ignoring parameters! ModelData considers this illegal");
1558
0
                aModelData.GetStorable()->store();
1559
0
            }
1560
0
        }
1561
0
        else
1562
0
        {
1563
0
            OSL_FAIL( "XStorable2 is not supported by the model!" );
1564
0
            aModelData.GetStorable()->store();
1565
0
        }
1566
1567
0
        return false;
1568
0
    }
1569
1570
    // preselect a filter for the storing process
1571
0
    uno::Sequence< beans::PropertyValue > aFilterProps = aModelData.GetPreselectedFilter_Impl( m_nStoreMode );
1572
1573
0
    DBG_ASSERT( aFilterProps.hasElements(), "No filter for storing!\n" );
1574
0
    if ( !aFilterProps.hasElements() )
1575
0
        throw task::ErrorCodeIOException(
1576
0
            u"SfxStoringHelper::GUIStoreModel: ERRCODE_IO_INVALIDPARAMETER"_ustr,
1577
0
            uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_INVALIDPARAMETER));
1578
1579
0
    ::comphelper::SequenceAsHashMap aFilterPropsHM( aFilterProps );
1580
0
    OUString aFilterName = aFilterPropsHM.getUnpackedValueOrDefault( u"Name"_ustr, OUString() );
1581
1582
0
    const OUString aFilterFromMediaDescr = aModelData.GetMediaDescr().getUnpackedValueOrDefault( aFilterNameString, OUString() );
1583
0
    const OUString aOldFilterName = aModelData.GetDocProps().getUnpackedValueOrDefault( aFilterNameString, OUString() );
1584
1585
0
    ::comphelper::SequenceAsHashMap::const_iterator aFileNameIter = aModelData.GetMediaDescr().find( u"URL"_ustr );
1586
1587
0
    bool bPDFOptions = (m_nStoreMode & PDFEXPORT_REQUESTED) && !(m_nStoreMode & PDFDIRECTEXPORT_REQUESTED);
1588
0
    bool bEPUBOptions = (m_nStoreMode & EPUBEXPORT_REQUESTED) && !(m_nStoreMode & EPUBDIRECTEXPORT_REQUESTED);
1589
0
    if ( ( m_nStoreMode & EXPORT_REQUESTED ) && (bPDFOptions || bEPUBOptions) )
1590
0
    {
1591
        // this is PDF or EPUB export, the filter options dialog should be shown before the export
1592
0
        aModelData.GetMediaDescr()[aFilterNameString] <<= aFilterName;
1593
0
        if ( aModelData.GetMediaDescr().find( u"FilterFlags"_ustr ) == aModelData.GetMediaDescr().end()
1594
0
          && aModelData.GetMediaDescr().find( aFilterOptionsString ) == aModelData.GetMediaDescr().end()
1595
0
          && aModelData.GetMediaDescr().find( aFilterDataString ) == aModelData.GetMediaDescr().end() )
1596
0
        {
1597
            // execute filter options dialog since no options are set in the media descriptor
1598
0
            if ( aModelData.ExecuteFilterDialog_Impl( aFilterName, bIsAsync ) )
1599
0
                m_bDialogUsed = true;
1600
0
        }
1601
0
    }
1602
1603
0
    if (bIsAsync)
1604
0
        return false;
1605
1606
0
    return SfxStoringHelper::FinishGUIStoreModel(aFileNameIter, aModelData, m_bRemote, m_nStoreMode, aFilterProps,
1607
0
                                                 m_bSetStandardName, m_bPreselectPassword, m_bDialogUsed,
1608
0
                                                 aFilterFromMediaDescr, aOldFilterName, aArgsSequence,
1609
0
                                                 aFilterName, nScriptingSignatureState);
1610
0
}
1611
1612
bool SfxStoringHelper::FinishGUIStoreModel(::comphelper::SequenceAsHashMap::const_iterator& aFileNameIter,
1613
                                          ModelData_Impl& aModelData, bool bRemote, sal_Int16 nStoreMode,
1614
                                          const uno::Sequence< beans::PropertyValue >& aFilterProps,
1615
                                          bool bSetStandardName, bool bPreselectPassword, bool bDialogUsed,
1616
                                          std::u16string_view aFilterFromMediaDescr,
1617
                                          std::u16string_view aOldFilterName,
1618
                                          uno::Sequence< beans::PropertyValue >& aArgsSequence,
1619
                                          OUString aFilterName,
1620
                                          SignatureState const nScriptingSignatureState)
1621
0
{
1622
0
    const OUString sFilterNameString(aFilterNameString);
1623
0
    const OUString sFilterOptionsString(aFilterOptionsString);
1624
0
    const OUString sFilterDataString(aFilterDataString);
1625
0
    bool bUseFilterOptions = false;
1626
0
    INetURLObject aURL;
1627
1628
0
    if ( aFileNameIter == aModelData.GetMediaDescr().end() )
1629
0
    {
1630
0
        sal_Int16 nDialog = SFX2_IMPL_DIALOG_CONFIG;
1631
1632
0
        if( bRemote )
1633
0
        {
1634
0
            nDialog = SFX2_IMPL_DIALOG_REMOTE;
1635
0
        }
1636
0
        else
1637
0
        {
1638
0
            ::comphelper::SequenceAsHashMap::const_iterator aDlgIter =
1639
0
                aModelData.GetMediaDescr().find( u"UseSystemDialog"_ustr );
1640
0
            if ( aDlgIter != aModelData.GetMediaDescr().end() )
1641
0
            {
1642
0
                bool bUseSystemDialog = true;
1643
0
                if ( aDlgIter->second >>= bUseSystemDialog )
1644
0
                {
1645
0
                    if ( bUseSystemDialog )
1646
0
                        nDialog = SFX2_IMPL_DIALOG_SYSTEM;
1647
0
                    else
1648
0
                        nDialog = SFX2_IMPL_DIALOG_OOO;
1649
0
                }
1650
0
            }
1651
0
        }
1652
1653
        // The Dispatch supports parameter FolderName that overwrites SuggestedSaveAsDir
1654
0
        OUString aSuggestedDir = aModelData.GetMediaDescr().getUnpackedValueOrDefault(u"FolderName"_ustr, OUString() );
1655
0
        if ( aSuggestedDir.isEmpty() )
1656
0
        {
1657
0
            aSuggestedDir = aModelData.GetMediaDescr().getUnpackedValueOrDefault(u"SuggestedSaveAsDir"_ustr, OUString() );
1658
0
            if ( aSuggestedDir.isEmpty() )
1659
0
                aSuggestedDir = aModelData.GetDocProps().getUnpackedValueOrDefault(u"SuggestedSaveAsDir"_ustr, OUString() );
1660
0
        }
1661
1662
0
        OUString aSuggestedName = aModelData.GetMediaDescr().getUnpackedValueOrDefault(u"SuggestedSaveAsName"_ustr, OUString() );
1663
0
        if ( aSuggestedName.isEmpty() )
1664
0
            aSuggestedName = aModelData.GetDocProps().getUnpackedValueOrDefault(u"SuggestedSaveAsName"_ustr, OUString() );
1665
1666
0
        css::uno::Sequence< OUString >  aDenyList;
1667
1668
0
        ::comphelper::SequenceAsHashMap::const_iterator aDenyListIter =
1669
0
            aModelData.GetMediaDescr().find( u"DenyList"_ustr );
1670
0
        if ( aDenyListIter != aModelData.GetMediaDescr().end() )
1671
0
            aDenyListIter->second >>= aDenyList;
1672
1673
0
        for (;;)
1674
0
        {
1675
            // in case the dialog is opened a second time the folder should be the same as previously navigated to by the user, not what was handed over by initial parameters
1676
0
            bUseFilterOptions = aModelData.OutputFileDialog( nStoreMode, aFilterProps, bSetStandardName, aSuggestedName, bPreselectPassword, aSuggestedDir, nDialog, aDenyList, nScriptingSignatureState );
1677
0
            if ( nStoreMode == SAVEAS_REQUESTED )
1678
0
            {
1679
                // in case of saving check filter for possible alien warning
1680
0
                const OUString aSelFilterName = aModelData.GetMediaDescr().getUnpackedValueOrDefault( sFilterNameString, OUString() );
1681
0
                sal_Int8 nStatusFilterSave = aModelData.CheckFilter( aSelFilterName );
1682
0
                if ( nStatusFilterSave == STATUS_SAVEAS_STANDARDNAME )
1683
0
                {
1684
                    // switch to best filter
1685
0
                    bSetStandardName = true;
1686
0
                }
1687
0
                else if ( nStatusFilterSave == STATUS_SAVE )
1688
0
                {
1689
                    // user confirmed alien filter or "good" filter is used
1690
0
                    break;
1691
0
                }
1692
0
            }
1693
0
            else
1694
0
                break;
1695
0
        }
1696
1697
0
        bDialogUsed = true;
1698
0
        aFileNameIter = aModelData.GetMediaDescr().find( u"URL"_ustr );
1699
0
    }
1700
0
    else
1701
0
    {
1702
        // the target file name is provided so check if new filter options
1703
        // are provided or old options can be used
1704
0
        if ( aFilterFromMediaDescr == aOldFilterName )
1705
0
        {
1706
0
            ::comphelper::SequenceAsHashMap::const_iterator aIter =
1707
0
                                            aModelData.GetDocProps().find( sFilterOptionsString );
1708
0
            if ( aIter != aModelData.GetDocProps().end()
1709
0
              && aModelData.GetMediaDescr().find( sFilterOptionsString ) == aModelData.GetMediaDescr().end() )
1710
0
                aModelData.GetMediaDescr()[aIter->first] = aIter->second;
1711
1712
0
            aIter = aModelData.GetDocProps().find( sFilterDataString );
1713
0
            if ( aIter != aModelData.GetDocProps().end()
1714
0
              && aModelData.GetMediaDescr().find( sFilterDataString ) == aModelData.GetMediaDescr().end() )
1715
0
                aModelData.GetMediaDescr()[aIter->first] = aIter->second;
1716
0
        }
1717
0
    }
1718
1719
0
    if ( aFileNameIter != aModelData.GetMediaDescr().end() )
1720
0
    {
1721
0
        OUString aFileName;
1722
0
        aFileNameIter->second >>= aFileName;
1723
0
        if (comphelper::LibreOfficeKit::isActive())
1724
0
        {
1725
            // In the LOK case, we didn't actually display any dialog yet, so invoke a callback if
1726
            // that's set.
1727
0
            OUString aNewURI;
1728
0
            if (comphelper::LibreOfficeKit::fileSaveDialog(aFileName, aNewURI))
1729
0
            {
1730
0
                if (aNewURI.isEmpty())
1731
0
                    return false;
1732
0
                aFileName = aNewURI;
1733
0
            }
1734
0
        }
1735
1736
0
        aURL.SetURL( aFileName );
1737
0
        DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "Illegal URL!" );
1738
1739
0
        ::comphelper::SequenceAsHashMap::const_iterator aIter =
1740
0
                                aModelData.GetMediaDescr().find( sFilterNameString );
1741
1742
0
        if ( aIter != aModelData.GetMediaDescr().end() )
1743
0
            aIter->second >>= aFilterName;
1744
0
        else
1745
0
            aModelData.GetMediaDescr()[sFilterNameString] <<= aFilterName;
1746
1747
0
        DBG_ASSERT( !aFilterName.isEmpty(), "Illegal filter!" );
1748
0
    }
1749
0
    else
1750
0
    {
1751
0
        SAL_WARN( "sfx.doc", "This code must be unreachable!" );
1752
0
        throw task::ErrorCodeIOException(
1753
0
            u"SfxStoringHelper::GUIStoreModel: ERRCODE_IO_INVALIDPARAMETER"_ustr,
1754
0
            uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_IO_INVALIDPARAMETER));
1755
0
    }
1756
1757
0
    ::comphelper::SequenceAsHashMap::const_iterator aIter =
1758
0
                            aModelData.GetMediaDescr().find( u"FilterFlags"_ustr );
1759
0
    bool bFilterFlagsSet = ( aIter != aModelData.GetMediaDescr().end() );
1760
1761
    // check if the filter Dialog has not been called before
1762
0
    if( !( nStoreMode & PDFEXPORT_REQUESTED ) && !( nStoreMode & EPUBEXPORT_REQUESTED ) && !bFilterFlagsSet
1763
0
        && ( ( nStoreMode & EXPORT_REQUESTED ) || bUseFilterOptions ) )
1764
0
    {
1765
        // execute filter options dialog
1766
0
        if ( aModelData.ExecuteFilterDialog_Impl( aFilterName, false ) )
1767
0
        {
1768
0
            bDialogUsed = true;
1769
            // check if the file is a pdf or not and change the storing mode at convenience
1770
0
            if (aFilterName.endsWith("pdf_Export"))
1771
0
                nStoreMode = EXPORT_REQUESTED | PDFEXPORT_REQUESTED;
1772
0
        }
1773
0
    }
1774
1775
    // so the arguments will not change any more and can be stored to the main location
1776
0
    aArgsSequence = aModelData.GetMediaDescr().getAsConstPropertyValueList();
1777
1778
    // store the document and handle it's docinfo
1779
    // Restore when exporting (which is also done when LoKit is enable, see below).
1780
0
    const bool bRestore
1781
0
        = (nStoreMode & EXPORT_REQUESTED)
1782
0
          || ((nStoreMode & SAVEAS_REQUESTED) && comphelper::LibreOfficeKit::isActive());
1783
0
    DocumentSettingsGuard aSettingsGuard(aModelData.GetModel(), aModelData.IsRecommendReadOnly(),
1784
0
                                         bRestore);
1785
1786
    // Treat attempted PDF export like a print: update document print statistics
1787
0
    if ((nStoreMode & PDFEXPORT_REQUESTED) && SfxViewShell::Current())
1788
0
    {
1789
0
        SfxObjectShell* pDocShell = SfxViewShell::Current()->GetObjectShell();
1790
0
        const bool bWasEnableSetModified = pDocShell && pDocShell->IsEnableSetModified();
1791
0
        bool bResetESM = false;
1792
1793
0
        if (bWasEnableSetModified
1794
0
            && !officecfg::Office::Common::Print::PrintingModifiesDocument::get())
1795
0
        {
1796
0
            pDocShell->EnableSetModified(false); // don't let export mark document as modified
1797
0
            bResetESM = true;
1798
0
        }
1799
1800
0
        uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
1801
0
            aModelData.GetModel(), uno::UNO_QUERY_THROW);
1802
0
        uno::Reference<document::XDocumentProperties> xDocProps(xDPS->getDocumentProperties());
1803
0
        xDocProps->setPrintDate(DateTime(DateTime::SYSTEM).GetUNODateTime());
1804
1805
0
        OUString sPrintedBy(SfxResId(STR_SFX_FILTERNAME_PDF));
1806
0
        if (pDocShell && pDocShell->IsUseUserData())
1807
0
        {
1808
0
            const OUString sFullName = SvtUserOptions().GetFullName();
1809
0
            if (!sFullName.isEmpty())
1810
0
                sPrintedBy += ": " + sFullName;
1811
0
        }
1812
0
        xDocProps->setPrintedBy(sPrintedBy);
1813
1814
0
        if (bResetESM)
1815
0
            pDocShell->EnableSetModified(true);
1816
0
    }
1817
1818
0
    OSL_ENSURE( aModelData.GetMediaDescr().find( u"Password"_ustr ) == aModelData.GetMediaDescr().end(), "The Password property of MediaDescriptor should not be used here!" );
1819
1820
    // Fetch the current view early, saving may spin the main loop, which may change the current
1821
    // view.
1822
0
    SfxViewShell* pViewShell = SfxViewShell::Current();
1823
1824
0
    if ( officecfg::Office::Common::Save::Document::EditProperty::get()
1825
0
      && ( !aModelData.GetStorable()->hasLocation()
1826
0
          || INetURLObject( aModelData.GetStorable()->getLocation() ) != aURL ) )
1827
0
    {
1828
        // this is definitely not a Save operation
1829
        // so the document info can be updated
1830
1831
        // on export document info must be preserved
1832
0
        uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
1833
0
            aModelData.GetModel(), uno::UNO_QUERY_THROW);
1834
0
        uno::Reference<util::XCloneable> xCloneable(
1835
0
            xDPS->getDocumentProperties(), uno::UNO_QUERY_THROW);
1836
0
        uno::Reference<document::XDocumentProperties> xOldDocProps(
1837
0
            xCloneable->createClone(), uno::UNO_QUERY_THROW);
1838
1839
        // use dispatch API to show document info dialog
1840
0
        if ( aModelData.ShowDocumentInfoDialog() )
1841
0
            bDialogUsed = true;
1842
0
        else
1843
0
        {
1844
0
            OSL_FAIL( "Can't execute document info dialog!" );
1845
0
        }
1846
1847
0
        try {
1848
            // Document properties can contain streams that should be freed before storing
1849
0
            aModelData.FreeDocumentProps();
1850
0
            if ( nStoreMode & EXPORT_REQUESTED )
1851
0
                aModelData.GetStorable()->storeToURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aArgsSequence );
1852
0
            else
1853
0
                aModelData.GetStorable()->storeAsURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aArgsSequence );
1854
0
        }
1855
0
        catch( const uno::Exception& )
1856
0
        {
1857
0
            if ( nStoreMode & EXPORT_REQUESTED )
1858
0
            {
1859
0
                SetDocInfoState(aModelData.GetModel(), xOldDocProps);
1860
0
            }
1861
0
            throw;
1862
0
        }
1863
1864
0
        if ( nStoreMode & EXPORT_REQUESTED )
1865
0
        {
1866
0
            SetDocInfoState(aModelData.GetModel(), xOldDocProps);
1867
0
        }
1868
0
    }
1869
0
    else
1870
0
    {
1871
        // Document properties can contain streams that should be freed before storing
1872
0
        aModelData.FreeDocumentProps();
1873
1874
        // this is actually a save operation with different parameters
1875
        // so storeTo or storeAs without DocInfo operations are used
1876
#ifdef IOS
1877
        try
1878
        {
1879
#endif
1880
            // SaveAs in LoKit is unhelpful. It saves the document to a new path, which
1881
            // breaks the link with the storage, so new modifications can't be uploaded.
1882
0
            if ((nStoreMode & EXPORT_REQUESTED)
1883
0
                || ((nStoreMode & SAVEAS_REQUESTED) && comphelper::LibreOfficeKit::isActive()))
1884
0
                aModelData.GetStorable()->storeToURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aArgsSequence );
1885
0
            else
1886
0
                aModelData.GetStorable()->storeAsURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aArgsSequence );
1887
#ifdef IOS
1888
        }
1889
        catch( const uno::Exception& )
1890
        {
1891
            // When using the iOS app (and maybe the Android app), the app
1892
            // will remain blocked if we rethrow an exception.
1893
        }
1894
#endif
1895
0
    }
1896
1897
0
    if (aModelData.IsSignWithDefaultSignature())
1898
0
    {
1899
0
        auto SignWithDefaultSignature = [&]()
1900
0
        {
1901
#if HAVE_FEATURE_GPGME
1902
            auto aSigningKey = SvtUserOptions().GetSigningKey();
1903
            if (aSigningKey.isEmpty())
1904
                return;
1905
1906
            std::vector<uno::Reference<xml::crypto::XXMLSecurityContext>> xSecurityContexts{
1907
                xml::crypto::GPGSEInitializer::create(comphelper::getProcessComponentContext())
1908
                    ->createSecurityContext({}),
1909
                xml::crypto::SEInitializer::create(comphelper::getProcessComponentContext())
1910
                    ->createSecurityContext({}),
1911
            };
1912
1913
            for (const auto& xSecurityContext : xSecurityContexts)
1914
            {
1915
                if (xSecurityContext.is())
1916
                {
1917
                    css::uno::Reference<css::security::XCertificate> xCert
1918
                        = comphelper::xmlsec::FindCertInContext(xSecurityContext, aSigningKey);
1919
1920
                    if (xCert.is() && SfxViewShell::Current())
1921
                    {
1922
                        SfxObjectShell* pDocShell = SfxViewShell::Current()->GetObjectShell();
1923
                        svl::crypto::SigningContext aSigningContext;
1924
                        aSigningContext.m_xCertificate = std::move(xCert);
1925
                        bool bSigned = pDocShell->SignDocumentContentUsingCertificate(aSigningContext);
1926
                        if (bSigned && pDocShell->HasValidSignatures())
1927
                        {
1928
                            std::unique_ptr<weld::MessageDialog> xBox(
1929
                                Application::CreateMessageDialog(
1930
                                    SfxStoringHelper::GetModelWindow(aModelData.GetModel()),
1931
                                    VclMessageType::Question, VclButtonsType::YesNo,
1932
                                    SfxResId(STR_QUERY_REMEMBERSIGNATURE)));
1933
                            pDocShell->SetRememberCurrentSignature(xBox->run() == RET_YES);
1934
                        }
1935
                        return;
1936
                    }
1937
                }
1938
            }
1939
1940
            // couldn't find the specified default signing certificate!
1941
            // alert the user the document won't be signed
1942
            std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
1943
                SfxStoringHelper::GetModelWindow(aModelData.GetModel()),
1944
                VclMessageType::Error, VclButtonsType::Ok,
1945
                SfxResId(STR_ERROR_NOMATCHINGDEFUALTCERT)));
1946
            xBox->run();
1947
            return;
1948
#else
1949
0
            (void)aModelData;
1950
0
#endif
1951
0
        };
1952
0
        SignWithDefaultSignature();
1953
0
    }
1954
1955
    // Launch PDF viewer
1956
0
    if ( nStoreMode & PDFEXPORT_REQUESTED && !comphelper::LibreOfficeKit::isActive() )
1957
0
    {
1958
0
        FilterConfigItem aItem(u"Office.Common/Filter/PDF/Export/");
1959
0
        bool aViewPDF = aItem.ReadBool( u"ViewPDFAfterExport"_ustr, false );
1960
1961
0
        if ( aViewPDF )
1962
0
        {
1963
0
            uno::Reference<XSystemShellExecute> xSystemShellExecute(SystemShellExecute::create( ::comphelper::getProcessComponentContext() ) );
1964
0
            xSystemShellExecute->execute( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), u""_ustr, SystemShellExecuteFlags::URIS_ONLY );
1965
0
        }
1966
0
    }
1967
1968
0
    if ( comphelper::LibreOfficeKit::isActive() )
1969
0
    {
1970
0
        if ( pViewShell )
1971
0
        {
1972
0
            OUString sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1973
0
            pViewShell->libreOfficeKitViewCallback( LOK_CALLBACK_EXPORT_FILE, sURL.toUtf8() );
1974
0
        }
1975
0
    }
1976
1977
0
    return bDialogUsed;
1978
0
}
1979
1980
1981
// static
1982
bool SfxStoringHelper::CheckFilterOptionsAppearance(
1983
                                                    const uno::Reference< container::XNameAccess >& xFilterCFG,
1984
                                                    const OUString& aFilterName )
1985
0
{
1986
0
    bool bUseFilterOptions = false;
1987
1988
0
    DBG_ASSERT( xFilterCFG.is(), "No filter configuration!\n" );
1989
0
    if( xFilterCFG.is() )
1990
0
    {
1991
0
        try {
1992
0
            uno::Sequence < beans::PropertyValue > aProps;
1993
0
            uno::Any aAny = xFilterCFG->getByName( aFilterName );
1994
0
            if ( aAny >>= aProps )
1995
0
            {
1996
0
                ::comphelper::SequenceAsHashMap aPropsHM( aProps );
1997
0
                if( !aPropsHM.getUnpackedValueOrDefault( u"UIComponent"_ustr, OUString() ).isEmpty() )
1998
0
                    bUseFilterOptions = true;
1999
0
            }
2000
0
        }
2001
0
        catch( const uno::Exception& )
2002
0
        {
2003
0
        }
2004
0
    }
2005
2006
0
    return bUseFilterOptions;
2007
0
}
2008
2009
2010
// static
2011
void SfxStoringHelper::SetDocInfoState(
2012
        const uno::Reference< frame::XModel2 >& xModel,
2013
        const uno::Reference< document::XDocumentProperties>& i_xOldDocProps )
2014
0
{
2015
0
    uno::Reference<document::XDocumentPropertiesSupplier> const
2016
0
        xModelDocPropsSupplier(xModel, uno::UNO_QUERY_THROW);
2017
0
    uno::Reference<document::XDocumentProperties> const xDocPropsToFill =
2018
0
        xModelDocPropsSupplier->getDocumentProperties();
2019
0
    uno::Reference< beans::XPropertySet > const xPropSet(
2020
0
            i_xOldDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
2021
2022
0
    uno::Reference< util::XModifiable > xModifiable( xModel, uno::UNO_QUERY );
2023
0
    if ( !xModifiable.is() )
2024
0
        throw uno::RuntimeException();
2025
2026
0
    bool bIsModified = xModifiable->isModified();
2027
2028
0
    try
2029
0
    {
2030
0
        uno::Reference< beans::XPropertySet > const xSet(
2031
0
                xDocPropsToFill->getUserDefinedProperties(), uno::UNO_QUERY);
2032
0
        uno::Reference< beans::XPropertyContainer > xContainer( xSet, uno::UNO_QUERY );
2033
0
        uno::Reference< beans::XPropertySetInfo > xSetInfo = xSet->getPropertySetInfo();
2034
0
        const uno::Sequence< beans::Property > lProps = xSetInfo->getProperties();
2035
0
        for (const beans::Property& rProp : lProps)
2036
0
        {
2037
0
            uno::Any aValue = xPropSet->getPropertyValue( rProp.Name );
2038
0
            if ( rProp.Attributes & css::beans::PropertyAttribute::REMOVABLE )
2039
0
            {
2040
0
                try
2041
0
                {
2042
                    // QUESTION: DefaultValue?!
2043
0
                    xContainer->addProperty( rProp.Name, rProp.Attributes, aValue );
2044
0
                }
2045
0
                catch (beans::PropertyExistException const&) {}
2046
0
                try
2047
0
                {
2048
                    // it is possible that the propertysets from XML and binary files differ; we shouldn't break then
2049
0
                    xSet->setPropertyValue( rProp.Name, aValue );
2050
0
                }
2051
0
                catch ( const uno::Exception& ) {}
2052
0
            }
2053
0
        }
2054
2055
        // sigh... have to set these manually I'm afraid... wonder why
2056
        // SfxObjectShell doesn't handle this internally, should be easier
2057
0
        xDocPropsToFill->setAuthor(i_xOldDocProps->getAuthor());
2058
0
        xDocPropsToFill->setGenerator(i_xOldDocProps->getGenerator());
2059
0
        xDocPropsToFill->setCreationDate(i_xOldDocProps->getCreationDate());
2060
0
        xDocPropsToFill->setTitle(i_xOldDocProps->getTitle());
2061
0
        xDocPropsToFill->setSubject(i_xOldDocProps->getSubject());
2062
0
        xDocPropsToFill->setDescription(i_xOldDocProps->getDescription());
2063
0
        xDocPropsToFill->setKeywords(i_xOldDocProps->getKeywords());
2064
0
        xDocPropsToFill->setModifiedBy(i_xOldDocProps->getModifiedBy());
2065
0
        xDocPropsToFill->setModificationDate(i_xOldDocProps->getModificationDate());
2066
0
        xDocPropsToFill->setPrintedBy(i_xOldDocProps->getPrintedBy());
2067
0
        xDocPropsToFill->setPrintDate(i_xOldDocProps->getPrintDate());
2068
0
        xDocPropsToFill->setAutoloadURL(i_xOldDocProps->getAutoloadURL());
2069
0
        xDocPropsToFill->setAutoloadSecs(i_xOldDocProps->getAutoloadSecs());
2070
0
        xDocPropsToFill->setDefaultTarget(i_xOldDocProps->getDefaultTarget());
2071
0
        xDocPropsToFill->setEditingCycles(i_xOldDocProps->getEditingCycles());
2072
0
        xDocPropsToFill->setEditingDuration(i_xOldDocProps->getEditingDuration());
2073
        // other attributes e.g. DocumentStatistics are not editable from dialog
2074
0
    }
2075
0
    catch (const uno::Exception&)
2076
0
    {
2077
0
        TOOLS_INFO_EXCEPTION("sfx.doc", "SetDocInfoState");
2078
0
    }
2079
2080
    // set the modified flag back if required
2081
0
    if ( bIsModified != bool(xModifiable->isModified()) )
2082
0
        xModifiable->setModified( bIsModified );
2083
0
}
2084
2085
2086
// static
2087
bool SfxStoringHelper::WarnUnacceptableFormat( const uno::Reference< frame::XModel2 >& xModel,
2088
                                                    std::u16string_view aOldUIName,
2089
                                                    std::u16string_view aExtension,
2090
                                                    const OUString& aDefExtension,
2091
                                                    bool bDefIsAlien )
2092
0
{
2093
0
    weld::Window* pWin = SfxStoringHelper::GetModelWindow(xModel);
2094
2095
0
    OUString sInfoText = SfxResId(STR_QUERY_ALIENFORMAT_TEXT);
2096
0
    sInfoText = sInfoText.replaceAll("%FORMATNAME", aOldUIName);
2097
0
    sInfoText = sInfoText.replaceAll("%EXTENSION", aExtension);
2098
2099
0
    OUString sExtension = u"ODF"_ustr;
2100
0
    OUString sQuestion = "";
2101
0
    if (bDefIsAlien) {
2102
0
        sExtension = aDefExtension.toAsciiUpperCase();
2103
0
    }
2104
0
    else
2105
0
    {
2106
0
        sQuestion = SfxResId(STR_QUERY_ALIENFORMAT_QUESTION);
2107
0
        sQuestion = sQuestion.replaceAll("%EXTENSION", aDefExtension);
2108
0
    }
2109
2110
0
    const OUString sTitle = SfxResId(STR_QUERY_ALIENFORMAT_TTITLE);
2111
0
    const OUString sYesLabel
2112
0
        = SfxResId(STR_QUERY_ALIENFORMAT_YES).replaceAll("%FORMATNAME", aOldUIName);
2113
0
    const OUString sNoLabel
2114
0
        = SfxResId(STR_QUERY_ALIENFORMAT_NO).replaceAll("%DEFAULTEXTENSION", sExtension);
2115
0
    return ConfirmationDlg::Query<officecfg::Office::Common::Save::Document::WarnAlienFormat>(
2116
0
        pWin, sTitle, sInfoText, sQuestion, sYesLabel, sNoLabel);
2117
0
}
2118
2119
uno::Reference<awt::XWindow> SfxStoringHelper::GetModelXWindow(const uno::Reference<frame::XModel2>& xModel)
2120
0
{
2121
0
    try {
2122
0
        if ( xModel.is() )
2123
0
        {
2124
0
            uno::Reference< frame::XController > xController = xModel->getCurrentController();
2125
0
            if ( xController.is() )
2126
0
            {
2127
0
                uno::Reference< frame::XFrame > xFrame = xController->getFrame();
2128
0
                if ( xFrame.is() )
2129
0
                {
2130
0
                    return xFrame->getContainerWindow();
2131
0
                }
2132
0
            }
2133
0
        }
2134
0
    }
2135
0
    catch ( const uno::Exception& )
2136
0
    {
2137
0
    }
2138
2139
0
    return uno::Reference<awt::XWindow>();
2140
0
}
2141
2142
weld::Window* SfxStoringHelper::GetModelWindow( const uno::Reference< frame::XModel2 >& xModel )
2143
0
{
2144
0
    weld::Window* pWin = nullptr;
2145
2146
0
    try
2147
0
    {
2148
0
        pWin = Application::GetFrameWeld(GetModelXWindow(xModel));
2149
0
    }
2150
0
    catch (const uno::Exception&)
2151
0
    {
2152
0
    }
2153
2154
0
    return pWin;
2155
0
}
2156
2157
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */