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