xref: /core/sw/source/uibase/app/docsh2.cxx (revision be6aa7ca0a3d9d444956c0e19a262a2ef91bdd5a)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <config_features.h>
21 
22 #include <osl/diagnose.h>
23 #include <com/sun/star/drawing/ModuleDispatcher.hpp>
24 #include <com/sun/star/frame/DispatchHelper.hpp>
25 #include <ooo/vba/word/XDocument.hpp>
26 #include <comphelper/fileformat.h>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/propertyvalue.hxx>
29 #include <comphelper/string.hxx>
30 
31 #include <sal/log.hxx>
32 #include <edtwin.hxx>
33 #include <tools/urlobj.hxx>
34 #include <unotools/tempfile.hxx>
35 #include <unotools/configmgr.hxx>
36 #include <vcl/errinf.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/weld.hxx>
39 #include <svl/eitem.hxx>
40 #include <svl/macitem.hxx>
41 #include <unotools/pathoptions.hxx>
42 #include <vcl/transfer.hxx>
43 #include <sfx2/dinfdlg.hxx>
44 #include <sfx2/request.hxx>
45 #include <sfx2/dispatch.hxx>
46 #include <sfx2/new.hxx>
47 #include <sfx2/notebookbar/SfxNotebookBar.hxx>
48 #include <sfx2/filedlghelper.hxx>
49 #include <sfx2/printer.hxx>
50 #include <sfx2/evntconf.hxx>
51 #include <sfx2/docfile.hxx>
52 #include <sfx2/docfilt.hxx>
53 #include <svx/dialogs.hrc>
54 #include <svx/drawitem.hxx>
55 #include <editeng/svxacorr.hxx>
56 #include <svx/fmshell.hxx>
57 #include <sfx2/linkmgr.hxx>
58 #include <sfx2/classificationhelper.hxx>
59 #include <sfx2/watermarkitem.hxx>
60 
61 #include <svx/ofaitem.hxx>
62 #include <SwSmartTagMgr.hxx>
63 #include <sfx2/app.hxx>
64 #include <basic/sbstar.hxx>
65 #include <basic/basmgr.hxx>
66 #include <comphelper/classids.hxx>
67 #include <fmtcol.hxx>
68 #include <istype.hxx>
69 #include <view.hxx>
70 #include <docsh.hxx>
71 #include <docary.hxx>
72 #include <wrtsh.hxx>
73 #include <rootfrm.hxx>
74 #include <fldbas.hxx>
75 #include <viewopt.hxx>
76 #include <globdoc.hxx>
77 #include <fldwrap.hxx>
78 #include <redlndlg.hxx>
79 #include <doc.hxx>
80 #include <IDocumentUndoRedo.hxx>
81 #include <IDocumentSettingAccess.hxx>
82 #include <IDocumentDeviceAccess.hxx>
83 #include <IDocumentLinksAdministration.hxx>
84 #include <IDocumentFieldsAccess.hxx>
85 #include <IDocumentStatistics.hxx>
86 #include <IDocumentLayoutAccess.hxx>
87 #include <IDocumentRedlineAccess.hxx>
88 #include <IDocumentState.hxx>
89 #include <shellio.hxx>
90 #include <pview.hxx>
91 #include <srcview.hxx>
92 #include <wdocsh.hxx>
93 #include <unotxdoc.hxx>
94 #include <acmplwrd.hxx>
95 #include <swmodule.hxx>
96 #include <unobaseclass.hxx>
97 #include <swwait.hxx>
98 #include <swcli.hxx>
99 
100 #include <cmdid.h>
101 #include <helpids.h>
102 #include <strings.hrc>
103 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
104 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
105 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
106 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
107 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
108 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
109 #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
110 #include <com/sun/star/script/vba/VBAEventId.hpp>
111 #include <editeng/acorrcfg.hxx>
112 #include <officecfg/Office/Security.hxx>
113 #include <officecfg/Office/Common.hxx>
114 
115 #include <sfx2/fcontnr.hxx>
116 #include <svx/ClassificationDialog.hxx>
117 #include <svtools/embedhlp.hxx>
118 
119 #include <swabstdlg.hxx>
120 #include <watermarkdialog.hxx>
121 
122 #include <ndtxt.hxx>
123 #include <iodetect.hxx>
124 
125 #include <vcl/tabs.hrc>
126 
127 #include <memory>
128 
129 using namespace ::com::sun::star::ui::dialogs;
130 using namespace ::com::sun::star::uno;
131 using namespace ::com::sun::star;
132 using namespace ::sfx2;
133 
134 // create DocInfo (virtual)
CreateDocumentInfoDialog(weld::Window * pParent,const SfxItemSet & rSet)135 std::shared_ptr<SfxDocumentInfoDialog> SwDocShell::CreateDocumentInfoDialog(weld::Window* pParent, const SfxItemSet &rSet)
136 {
137     std::shared_ptr<SfxDocumentInfoDialog> xDlg = std::make_shared<SfxDocumentInfoDialog>(pParent, rSet);
138     //only with statistics, when this document is being shown, not
139     //from within the Doc-Manager
140     SwDocShell* pDocSh = static_cast<SwDocShell*>( SfxObjectShell::Current());
141     if( pDocSh == this )
142     {
143         //Not for SourceView.
144         SfxViewShell *pVSh = SfxViewShell::Current();
145         if ( pVSh && dynamic_cast< const SwSrcView *>( pVSh ) ==  nullptr )
146         {
147             SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
148             xDlg->AddFontTabPage();
149             xDlg->AddTabPage(u"writerstats"_ustr, TabResId(RID_TAB_STATISTICS.aLabel),
150                              pFact->GetTabPageCreatorFunc(RID_SW_TP_DOC_STAT),
151                              RID_L + RID_TAB_STATISTICS.sIconName);
152         }
153     }
154     return xDlg;
155 }
156 
ToggleLayoutMode(SwView * pView)157 void SwDocShell::ToggleLayoutMode(SwView* pView)
158 {
159     assert(pView && "SwDocShell::ToggleLayoutMode, pView is null.");
160 
161     const SwViewOption& rViewOptions = *pView->GetWrtShell().GetViewOptions();
162 
163     //TODO: Should HideWhitespace flag be saved in the document settings?
164     GetDoc()->getIDocumentSettingAccess().set(DocumentSettingId::BROWSE_MODE, rViewOptions.getBrowseMode());
165     UpdateFontList();  // Why is this necessary here?
166 
167     pView->GetViewFrame().GetBindings().Invalidate(FN_SHADOWCURSOR);
168     if( !GetDoc()->getIDocumentDeviceAccess().getPrinter( false ) )
169         pView->SetPrinter( GetDoc()->getIDocumentDeviceAccess().getPrinter( false ), SfxPrinterChangeFlags::PRINTER | SfxPrinterChangeFlags::JOBSETUP );
170     GetDoc()->CheckDefaultPageFormat();
171     SfxViewFrame *pTmpFrame = SfxViewFrame::GetFirst(this, false);
172     while (pTmpFrame)
173     {
174         if( pTmpFrame != &pView->GetViewFrame() )
175         {
176             pTmpFrame->DoClose();
177             pTmpFrame = SfxViewFrame::GetFirst(this, false);
178         }
179         else
180             pTmpFrame = SfxViewFrame::GetNext(*pTmpFrame, this, false);
181     }
182 
183     pView->GetWrtShell().InvalidateLayout(true);
184 
185     pView->RecheckBrowseMode();
186 
187     pView->SetNewWindowAllowed(!rViewOptions.getBrowseMode());
188 }
189 
190 // update text fields on document properties changes
DoFlushDocInfo()191 void SwDocShell::DoFlushDocInfo()
192 {
193     if (!m_xDoc)
194         return;
195 
196     bool bUnlockView(true);
197     if (m_pWrtShell)
198     {
199         bUnlockView = !m_pWrtShell->IsViewLocked();
200         m_pWrtShell->LockView( true );    // lock visible section
201         m_pWrtShell->StartAllAction();
202     }
203 
204     m_xDoc->getIDocumentStatistics().DocInfoChgd(IsEnableSetModified());
205 
206     if (m_pWrtShell)
207     {
208         m_pWrtShell->EndAllAction();
209         if (bUnlockView)
210         {
211             m_pWrtShell->LockView( false );
212         }
213     }
214 }
215 
lcl_processCompatibleSfxHint(const uno::Reference<script::vba::XVBAEventProcessor> & xVbaEvents,const SfxHint & rHint)216 static void lcl_processCompatibleSfxHint( const uno::Reference< script::vba::XVBAEventProcessor >& xVbaEvents, const SfxHint& rHint )
217 {
218     using namespace com::sun::star::script::vba::VBAEventId;
219     if (rHint.GetId() != SfxHintId::ThisIsAnSfxEventHint)
220         return;
221 
222     uno::Sequence< uno::Any > aArgs;
223     switch (static_cast<const SfxEventHint&>(rHint).GetEventId())
224     {
225         case SfxEventHintId::CreateDoc:
226             xVbaEvents->processVbaEvent(AUTO_NEW, aArgs);
227             xVbaEvents->processVbaEvent(DOCUMENT_NEW, aArgs);
228         break;
229         case SfxEventHintId::OpenDoc:
230             xVbaEvents->processVbaEvent(AUTO_OPEN, aArgs);
231             xVbaEvents->processVbaEvent(DOCUMENT_OPEN, aArgs);
232         break;
233         default: break;
234     }
235 }
236 
237 // Notification on DocInfo changes
Notify(SfxBroadcaster &,const SfxHint & rHint)238 void SwDocShell::Notify( SfxBroadcaster&, const SfxHint& rHint )
239 {
240     if (!m_xDoc)
241     {
242         return ;
243     }
244 
245     uno::Reference< script::vba::XVBAEventProcessor > const xVbaEvents =
246         m_xDoc->GetVbaEventProcessor();
247     if( xVbaEvents.is() )
248         lcl_processCompatibleSfxHint( xVbaEvents, rHint );
249 
250     if (rHint.GetId() == SfxHintId::ThisIsAnSfxEventHint)
251     {
252         switch (static_cast<const SfxEventHint&>(rHint).GetEventId())
253         {
254             case SfxEventHintId::ActivateDoc:
255             case SfxEventHintId::CreateDoc:
256             case SfxEventHintId::OpenDoc:
257             {
258                 uno::Sequence< css::uno::Any > aArgs;
259                 SwModule::get()->CallAutomationApplicationEventSinks(u"DocumentChange"_ustr, aArgs);
260                 break;
261             }
262             default:
263                 break;
264         }
265 
266         switch (static_cast<const SfxEventHint&>(rHint).GetEventId())
267         {
268             case SfxEventHintId::CreateDoc:
269                 {
270                     uno::Any aDocument;
271                     aDocument <<= mxAutomationDocumentObject;
272                     uno::Sequence< uno::Any > aArgs{ aDocument };
273                     SwModule::get()->CallAutomationApplicationEventSinks( u"NewDocument"_ustr, aArgs );
274                 }
275                 break;
276             case SfxEventHintId::OpenDoc:
277                 {
278                     uno::Any aDocument;
279                     aDocument <<= mxAutomationDocumentObject;
280                     uno::Sequence< uno::Any > aArgs{ aDocument };
281                     SwModule::get()->CallAutomationApplicationEventSinks( u"DocumentOpen"_ustr, aArgs );
282                 }
283                 break;
284             default:
285                 break;
286         }
287     }
288 
289     sal_uInt16 nAction = 0;
290     if (rHint.GetId() == SfxHintId::ThisIsAnSfxEventHint &&
291         static_cast<const SfxEventHint&>(rHint).GetEventId() == SfxEventHintId::LoadFinished)
292     {
293         // #i38126# - own action id
294         nAction = 3;
295     }
296     else
297     {
298         // switch for more actions
299         if( rHint.GetId() == SfxHintId::TitleChanged)
300         {
301             if( GetMedium() )
302                 nAction = 2;
303         }
304     }
305 
306     if( !nAction )
307         return;
308 
309     bool bUnlockView = true; //initializing prevents warning
310     if (m_pWrtShell)
311     {
312         bUnlockView = !m_pWrtShell->IsViewLocked();
313         m_pWrtShell->LockView( true );    //lock visible section
314         m_pWrtShell->StartAllAction();
315     }
316     switch( nAction )
317     {
318     case 2:
319         m_xDoc->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Filename )->UpdateFields();
320         break;
321     // #i38126# - own action for event LOADFINISHED
322     // in order to avoid a modified document.
323     // #i41679# - Also for the instance of <SwDoc>
324     // it has to be assured, that it's not modified.
325     // Perform the same as for action id 1, but disable <SetModified>.
326     case 3:
327         {
328             const bool bResetModified = IsEnableSetModified();
329             if ( bResetModified )
330                 EnableSetModified( false );
331             // #i41679#
332             const bool bIsDocModified = m_xDoc->getIDocumentState().IsModified();
333             // TODO: is the ResetModified() below because of only the direct call from DocInfoChgd, or does UpdateFields() set it too?
334 
335             m_xDoc->getIDocumentStatistics().DocInfoChgd(false);
336 
337             // #i41679#
338             if ( !bIsDocModified )
339                 m_xDoc->getIDocumentState().ResetModified();
340             if ( bResetModified )
341                 EnableSetModified();
342         }
343         break;
344     }
345 
346     if (m_pWrtShell)
347     {
348         m_pWrtShell->EndAllAction();
349         if( bUnlockView )
350             m_pWrtShell->LockView( false );
351     }
352 }
353 
354 // Notification Close Doc
PrepareClose(bool bUI)355 bool SwDocShell::PrepareClose( bool bUI )
356 {
357     bool bRet = SfxObjectShell::PrepareClose( bUI );
358 
359     // If we are going to close it at this point, let potential DocumentBeforeClose event handlers
360     // in Automation clients veto it.
361     if (bRet && m_xDoc && IsInPrepareClose())
362     {
363         uno::Any aDocument;
364         aDocument <<= mxAutomationDocumentObject;
365 
366         uno::Sequence<uno::Any> aArgs{ // Arg 0: Document
367                                        aDocument,
368                                        // Arg 1: Cancel
369                                        uno::Any(false)
370         };
371 
372         SwModule::get()->CallAutomationApplicationEventSinks(u"DocumentBeforeClose"_ustr, aArgs);
373 
374         // If the Cancel argument was set to True by an event handler, return false.
375         bool bCancel(false);
376         aArgs[1] >>= bCancel;
377         if (bCancel)
378             bRet = false;
379     }
380 
381     if( bRet )
382         EndListening( *this );
383 
384     if (m_xDoc && IsInPrepareClose())
385     {
386         uno::Reference< script::vba::XVBAEventProcessor > const xVbaEvents =
387             m_xDoc->GetVbaEventProcessor();
388         if( xVbaEvents.is() )
389         {
390             using namespace com::sun::star::script::vba::VBAEventId;
391             uno::Sequence< uno::Any > aNoArgs;
392             xVbaEvents->processVbaEvent(AUTO_CLOSE, aNoArgs);
393             xVbaEvents->processVbaEvent(DOCUMENT_CLOSE, aNoArgs);
394         }
395     }
396     return bRet;
397 }
398 
Execute(SfxRequest & rReq)399 void SwDocShell::Execute(SfxRequest& rReq)
400 {
401     const SfxItemSet* pArgs = rReq.GetArgs();
402     const SfxPoolItem* pItem;
403     sal_uInt16 nWhich = rReq.GetSlot();
404     bool bDone = false;
405     switch ( nWhich )
406     {
407         case SID_AUTO_CORRECT_DLG:
408         {
409             SvxSwAutoFormatFlags* pAFlags = &SvxAutoCorrCfg::Get().GetAutoCorrect()->GetSwFlags();
410             SwAutoCompleteWord& rACW = SwDoc::GetAutoCompleteWords();
411 
412             bool bOldLocked = rACW.IsLockWordLstLocked(),
413                  bOldAutoCmpltCollectWords = pAFlags->bAutoCmpltCollectWords;
414 
415             rACW.SetLockWordLstLocked( true );
416 
417             editeng::SortedAutoCompleteStrings aTmpLst( rACW.GetWordList().createNonOwningCopy() );
418             pAFlags->m_pAutoCompleteList = &aTmpLst;
419 
420             SfxApplication* pApp = SfxGetpApp();
421             SfxRequest aAppReq(SID_AUTO_CORRECT_DLG, SfxCallMode::SYNCHRON, pApp->GetPool());
422             SfxBoolItem aSwOptions( SID_AUTO_CORRECT_DLG, true );
423             aAppReq.AppendItem(aSwOptions);
424 
425             pAFlags->pSmartTagMgr = &SwSmartTagMgr::Get();
426 
427             SfxItemSetFixed<SID_AUTO_CORRECT_DLG, SID_AUTO_CORRECT_DLG, SID_OPEN_SMARTTAGOPTIONS, SID_OPEN_SMARTTAGOPTIONS> aSet( pApp->GetPool() );
428             aSet.Put( aSwOptions );
429 
430             const SfxBoolItem* pOpenSmartTagOptionsItem = nullptr;
431             if( pArgs && (pOpenSmartTagOptionsItem = pArgs->GetItemIfSet( SID_OPEN_SMARTTAGOPTIONS, false )) )
432                 aSet.Put( *pOpenSmartTagOptionsItem );
433 
434             SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
435             VclPtr<SfxAbstractTabDialog> pDlg = pFact->CreateAutoCorrTabDialog(GetView()->GetFrameWeld(), &aSet);
436             pDlg->Execute();
437             pDlg.disposeAndClear();
438 
439 
440             rACW.SetLockWordLstLocked( bOldLocked );
441 
442             SwEditShell::SetAutoFormatFlags( pAFlags );
443             rACW.SetMinWordLen( pAFlags->nAutoCmpltWordLen );
444             rACW.SetMaxCount( pAFlags->nAutoCmpltListLen );
445             if (pAFlags->m_pAutoCompleteList)  // any changes?
446             {
447                 rACW.CheckChangedList( aTmpLst );
448                 // clear the temp WordList pointer
449                 pAFlags->m_pAutoCompleteList = nullptr;
450             }
451 
452             if( !bOldAutoCmpltCollectWords && bOldAutoCmpltCollectWords !=
453                 pAFlags->bAutoCmpltCollectWords )
454             {
455                 // call on all Docs the idle formatter to start
456                 // the collection of Words
457                 for( SwDocShell *pDocSh = static_cast<SwDocShell*>(SfxObjectShell::GetFirst(checkSfxObjectShell<SwDocShell>));
458                      pDocSh;
459                      pDocSh = static_cast<SwDocShell*>(SfxObjectShell::GetNext( *pDocSh, checkSfxObjectShell<SwDocShell> )) )
460                 {
461                     SwDoc* pTmp = pDocSh->GetDoc();
462                     if ( pTmp->getIDocumentLayoutAccess().GetCurrentViewShell() )
463                         pTmp->InvalidateAutoCompleteFlag();
464                 }
465             }
466         }
467         break;
468 
469         case SID_PRINTPREVIEW:
470             {
471                 bool bSet = false;
472                 bool bFound = false, bOnly = true;
473                 SfxViewFrame *pTmpFrame = SfxViewFrame::GetFirst(this);
474                 SfxViewShell* pViewShell = SfxViewShell::Current();
475                 SwView* pCurrView = dynamic_cast< SwView *> ( pViewShell );
476                 bool bCurrent = isType<SwPagePreview>( pViewShell );
477 
478                 while( pTmpFrame )    // search Preview
479                 {
480                     if( isType<SwView>( pTmpFrame->GetViewShell()) )
481                         bOnly = false;
482                     else if( isType<SwPagePreview>( pTmpFrame->GetViewShell()))
483                     {
484                         pTmpFrame->GetFrame().Appear();
485                         bFound = true;
486                     }
487                     if( bFound && !bOnly )
488                         break;
489                     pTmpFrame = SfxViewFrame::GetNext(*pTmpFrame, this);
490                 }
491 
492                 if( pArgs && SfxItemState::SET ==
493                     pArgs->GetItemState( SID_PRINTPREVIEW, false, &pItem ))
494                     bSet = static_cast<const SfxBoolItem*>(pItem)->GetValue();
495                 else
496                     bSet = !bCurrent;
497 
498                 sal_uInt16 nSlotId = 0;
499                 if( bSet && !bFound )   // Nothing found, so create new Preview
500                         nSlotId = SID_VIEWSHELL1;
501                 else if( bFound && !bSet )
502                     nSlotId = bOnly ? SID_VIEWSHELL0 : SID_VIEWSHELL1;
503 
504                 if( nSlotId )
505                 {
506                     // PagePreview in the WebDocShell
507                     // is found under Id VIEWSHELL2.
508                     if( dynamic_cast< const SwWebDocShell *>( this ) !=  nullptr && SID_VIEWSHELL1 == nSlotId )
509                         nSlotId = SID_VIEWSHELL2;
510 
511                     if( pCurrView && pCurrView->GetDocShell() == this )
512                         pTmpFrame = &pCurrView->GetViewFrame();
513                     else
514                         pTmpFrame = SfxViewFrame::GetFirst( this );
515 
516                     if (pTmpFrame)
517                         pTmpFrame->GetDispatcher()->Execute( nSlotId, SfxCallMode::ASYNCHRON );
518                 }
519 
520                 rReq.SetReturnValue(SfxBoolItem(SID_PRINTPREVIEW, bSet ));
521             }
522             break;
523         case SID_TEMPLATE_LOAD:
524             {
525                 OUString aFileName;
526                 static bool bText = true;
527                 static bool bFrame = false;
528                 static bool bPage =  false;
529                 static bool bNum =   false;
530                 static bool bMerge = false;
531 
532                 SfxTemplateFlags nFlags = bFrame ? SfxTemplateFlags::LOAD_FRAME_STYLES : SfxTemplateFlags::NONE;
533                 if(bPage)
534                     nFlags |= SfxTemplateFlags::LOAD_PAGE_STYLES;
535                 if(bNum)
536                     nFlags |= SfxTemplateFlags::LOAD_NUM_STYLES;
537                 if(nFlags == SfxTemplateFlags::NONE || bText)
538                     nFlags |= SfxTemplateFlags::LOAD_TEXT_STYLES;
539                 if(bMerge)
540                     nFlags |= SfxTemplateFlags::MERGE_STYLES;
541 
542                 if ( pArgs )
543                 {
544                     const SfxStringItem* pTemplateItem = rReq.GetArg<SfxStringItem>(SID_TEMPLATE_NAME);
545                     if ( pTemplateItem )
546                     {
547                         aFileName = pTemplateItem->GetValue();
548                         const SfxInt32Item* pFlagsItem = rReq.GetArg<SfxInt32Item>(SID_TEMPLATE_LOAD);
549                         if ( pFlagsItem )
550                             nFlags = static_cast<SfxTemplateFlags>(o3tl::narrowing<sal_uInt16>(pFlagsItem->GetValue()));
551                     }
552                 }
553 
554                 if ( aFileName.isEmpty() )
555                 {
556                     weld::Window* pDialogParent = rReq.GetFrameWeld();
557                     SAL_WARN_IF(!pDialogParent, "sw.ui", "missing parameter for DialogParent");
558                     SfxNewFileDialog aNewFileDlg(pDialogParent, SfxNewFileDialogMode::LoadTemplate);
559                     aNewFileDlg.SetTemplateFlags(nFlags);
560 
561                     sal_uInt16 nRet = aNewFileDlg.run();
562                     if(RET_TEMPLATE_LOAD == nRet)
563                     {
564                         FileDialogHelper aDlgHelper(TemplateDescription::FILEOPEN_SIMPLE,
565                                                     FileDialogFlags::NONE, pDialogParent);
566                         aDlgHelper.SetContext(FileDialogHelper::WriterLoadTemplate);
567                         uno::Reference < XFilePicker3 > xFP = aDlgHelper.GetFilePicker();
568 
569                         SfxObjectFactory &rFact = GetFactory();
570                         SfxFilterMatcher aMatcher( rFact.GetFactoryName() );
571                         SfxFilterMatcherIter aIter( aMatcher );
572                         std::shared_ptr<const SfxFilter> pFlt = aIter.First();
573                         while( pFlt )
574                         {
575                             // --> OD #i117339#
576                             if( pFlt->IsAllowedAsTemplate() &&
577                                 ( pFlt->GetUserData() == "CXML" ||
578                                   pFlt->GetUserData() == "CXMLV" ) )
579                             {
580                                 const OUString sWild = pFlt->GetWildcard().getGlob();
581                                 xFP->appendFilter( pFlt->GetUIName(), sWild );
582                             }
583                             pFlt = aIter.Next();
584                         }
585                         bool bWeb = dynamic_cast< SwWebDocShell *>( this ) !=  nullptr;
586                         std::shared_ptr<const SfxFilter> pOwnFlt =
587                                 SwDocShell::Factory().GetFilterContainer()->
588                                 GetFilter4FilterName(u"writer8"_ustr);
589 
590                         // make sure the default file format is also available
591                         if(bWeb)
592                         {
593                             const OUString sWild = pOwnFlt->GetWildcard().getGlob();
594                             xFP->appendFilter( pOwnFlt->GetUIName(), sWild );
595                         }
596 
597                         bool bError = false;
598                         // catch exception if wrong filter is selected - should not happen anymore
599                         try
600                         {
601                             xFP->setCurrentFilter( pOwnFlt->GetUIName() );
602                         }
603                         catch (const uno::Exception&)
604                         {
605                             bError = true;
606                         }
607 
608                         if( !bError && ERRCODE_NONE == aDlgHelper.Execute() )
609                         {
610                             aFileName = xFP->getSelectedFiles().getConstArray()[0];
611                         }
612                     }
613                     else if( RET_OK == nRet)
614                     {
615                         aFileName = aNewFileDlg.GetTemplateFileName();
616                     }
617 
618                     nFlags = aNewFileDlg.GetTemplateFlags();
619                     rReq.AppendItem( SfxStringItem( SID_TEMPLATE_NAME, aFileName ) );
620                     rReq.AppendItem( SfxInt32Item( SID_TEMPLATE_LOAD, static_cast<tools::Long>(nFlags) ) );
621                 }
622 
623                 if( !aFileName.isEmpty() )
624                 {
625                     SwgReaderOption aOpt;
626                     bText  = bool(nFlags & SfxTemplateFlags::LOAD_TEXT_STYLES );
627                     aOpt.SetTextFormats(bText);
628                     bFrame = bool(nFlags & SfxTemplateFlags::LOAD_FRAME_STYLES);
629                     aOpt.SetFrameFormats(bFrame);
630                     bPage  = bool(nFlags & SfxTemplateFlags::LOAD_PAGE_STYLES );
631                     aOpt.SetPageDescs(bPage);
632                     bNum   = bool(nFlags & SfxTemplateFlags::LOAD_NUM_STYLES  );
633                     aOpt.SetNumRules(bNum);
634                     //different meaning between SFX_MERGE_STYLES and aOpt.SetMerge!
635                     bMerge = bool(nFlags & SfxTemplateFlags::MERGE_STYLES);
636                     aOpt.SetMerge( !bMerge );
637 
638                     SetError(LoadStylesFromFile(aFileName, aOpt, false));
639                     if ( !GetErrorIgnoreWarning() )
640                         rReq.Done();
641                 }
642             }
643             break;
644             case SID_SOURCEVIEW:
645             {
646                 SfxViewShell* pViewShell = GetView()
647                                             ? static_cast<SfxViewShell*>(GetView())
648                                             : SfxViewShell::Current();
649                 SfxViewFrame& rViewFrame = pViewShell->GetViewFrame();
650                 SwSrcView* pSrcView = dynamic_cast< SwSrcView *>( pViewShell );
651                 if(!pSrcView)
652                 {
653                     // 3 possible state:
654                     // 1 - file unsaved -> save as HTML
655                     // 2 - file modified and HTML filter active -> save
656                     // 3 - file saved in non-HTML -> QueryBox to save as HTML
657                     std::shared_ptr<const SfxFilter> pHtmlFlt =
658                                     SwIoSystem::GetFilterOfFormat(
659                                         u"HTML",
660                                         SwWebDocShell::Factory().GetFilterContainer() );
661                     bool bLocalHasName = HasName();
662                     if(bLocalHasName)
663                     {
664                         //check for filter type
665                         std::shared_ptr<const SfxFilter> pFlt = GetMedium()->GetFilter();
666                         if(!pFlt || pFlt->GetUserData() != pHtmlFlt->GetUserData())
667                         {
668                             std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(rViewFrame.GetFrameWeld(), u"modules/swriter/ui/saveashtmldialog.ui"_ustr));
669                             std::unique_ptr<weld::MessageDialog> xQuery(xBuilder->weld_message_dialog(u"SaveAsHTMLDialog"_ustr));
670                             if (RET_YES == xQuery->run())
671                                 bLocalHasName = false;
672                             else
673                                 break;
674                         }
675                     }
676                     if(!bLocalHasName)
677                     {
678                         FileDialogHelper aDlgHelper(TemplateDescription::FILESAVE_AUTOEXTENSION,
679                                                     FileDialogFlags::NONE,
680                                                     GetView()->GetFrameWeld());
681                         aDlgHelper.SetContext(FileDialogHelper::WriterSaveHTML);
682                         aDlgHelper.AddFilter( pHtmlFlt->GetFilterName(), pHtmlFlt->GetDefaultExtension() );
683                         aDlgHelper.SetCurrentFilter( pHtmlFlt->GetFilterName() );
684                         if( ERRCODE_NONE != aDlgHelper.Execute())
685                         {
686                             break;
687                         }
688                         OUString sPath = aDlgHelper.GetPath();
689                         SfxStringItem aName(SID_FILE_NAME, sPath);
690                         SfxStringItem aFilter(SID_FILTER_NAME, pHtmlFlt->GetName());
691                         const SfxPoolItemHolder aResult(
692                             rViewFrame.GetDispatcher()->ExecuteList(
693                             SID_SAVEASDOC, SfxCallMode::SYNCHRON,
694                             { &aName, &aFilter }));
695                         const SfxBoolItem* pBool(static_cast<const SfxBoolItem*>(aResult.getItem()));
696                         if(!pBool || !pBool->GetValue())
697                             break;
698                     }
699                 }
700 
701                 assert(dynamic_cast<SwWebDocShell*>(this) && "SourceView only in WebDocShell");
702 
703                 // the SourceView is not the 1 for SwWebDocShell
704                 sal_uInt16 nSlot = SID_VIEWSHELL1;
705                 bool bSetModified = false;
706                 VclPtr<SfxPrinter> pSavePrinter;
707                 if( nullptr != pSrcView)
708                 {
709                     SfxPrinter* pTemp = GetDoc()->getIDocumentDeviceAccess().getPrinter( false );
710                     if(pTemp)
711                         pSavePrinter = VclPtr<SfxPrinter>::Create(*pTemp);
712                     bSetModified = IsModified() || pSrcView->IsModified();
713                     if(pSrcView->IsModified()||pSrcView->HasSourceSaved())
714                     {
715                         utl::TempFileNamed aTempFile;
716                         aTempFile.EnableKillingFile();
717                         pSrcView->SaveContent(aTempFile.GetURL());
718                         bDone = true;
719                         SvxMacro aMac(OUString(), OUString(), STARBASIC);
720                         SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::OPENDOC ), aMac, this);
721                         SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::PREPARECLOSEDOC ), aMac, this);
722                         SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::ACTIVATEDOC ),     aMac, this);
723                         SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::DEACTIVATEDOC ), aMac, this);
724                         ReloadFromHtml(aTempFile.GetURL(), pSrcView);
725                         nSlot = 0;
726                     }
727                     else
728                     {
729                         nSlot = SID_VIEWSHELL0;
730                     }
731                 }
732                 if (nSlot)
733                     rViewFrame.GetDispatcher()->Execute(nSlot, SfxCallMode::SYNCHRON);
734                 if(bSetModified)
735                     GetDoc()->getIDocumentState().SetModified();
736                 if(pSavePrinter)
737                 {
738                     GetDoc()->getIDocumentDeviceAccess().setPrinter( pSavePrinter, true, true);
739                     //pSavePrinter must not be deleted again
740                 }
741                 rViewFrame.GetBindings().SetState(SfxBoolItem(SID_SOURCEVIEW, false)); // not SID_VIEWSHELL2
742                 rViewFrame.GetBindings().Invalidate( SID_NEWWINDOW );
743                 rViewFrame.GetBindings().Invalidate( SID_BROWSER_MODE );
744                 rViewFrame.GetBindings().Invalidate( FN_PRINT_LAYOUT );
745             }
746             break;
747             case SID_GET_COLORLIST:
748             {
749                 const SvxColorListItem* pColItem = GetItem(SID_COLOR_TABLE);
750                 const XColorListRef& pList = pColItem->GetColorList();
751                 rReq.SetReturnValue(OfaXColorListItem(SID_GET_COLORLIST, pList));
752             }
753             break;
754         case FN_ABSTRACT_STARIMPRESS:
755         case FN_ABSTRACT_NEWDOC:
756         {
757             SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
758             VclPtr<AbstractSwInsertAbstractDlg> pDlg(pFact->CreateSwInsertAbstractDlg(GetView()->GetFrameWeld()));
759             pDlg->StartExecuteAsync(
760                 [this, pDlg, nWhich] (sal_Int32 nResult)->void
761                 {
762                     if (nResult == RET_OK)
763                     {
764                         sal_uInt8 nLevel = pDlg->GetLevel();
765                         sal_uInt8 nPara = pDlg->GetPara();
766                         SwDoc* pSmryDoc = new SwDoc();
767                         SfxObjectShellLock xDocSh(new SwDocShell(*pSmryDoc, SfxObjectCreateMode::STANDARD));
768                         xDocSh->DoInitNew();
769 
770                         bool bImpress = FN_ABSTRACT_STARIMPRESS == nWhich;
771                         m_xDoc->Summary(*pSmryDoc, nLevel, nPara, bImpress);
772                         if( bImpress )
773                         {
774                             WriterRef xWrt;
775                             // mba: looks as if relative URLs don't make sense here
776                             ::GetRTFWriter(std::u16string_view(), OUString(), xWrt);
777                             SvMemoryStream *pStrm = new SvMemoryStream();
778                             pStrm->SetBufferSize( 16348 );
779                             SwWriter aWrt( *pStrm, *pSmryDoc );
780                             ErrCodeMsg eErr = aWrt.Write( xWrt );
781                             if( !eErr.IgnoreWarning() )
782                             {
783                                 const uno::Reference< uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext();
784                                 uno::Reference< frame::XDispatchProvider > xProv = drawing::ModuleDispatcher::create( xContext );
785 
786                                 uno::Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create(xContext) );
787                                 pStrm->Seek( STREAM_SEEK_TO_END );
788                                 pStrm->WriteChar( '\0' );
789                                 pStrm->Seek( STREAM_SEEK_TO_BEGIN );
790 
791                                 uno::Sequence< sal_Int8 > aSeq( pStrm->TellEnd() );
792                                 pStrm->ReadBytes( aSeq.getArray(), aSeq.getLength() );
793 
794                                 uno::Sequence< beans::PropertyValue > aArgs{
795                                     comphelper::makePropertyValue(u"RtfOutline"_ustr, aSeq)
796                                 };
797                                 xHelper->executeDispatch( xProv, u"SendOutlineToImpress"_ustr, OUString(), 0, aArgs );
798                             }
799                             else
800                                 ErrorHandler::HandleError( eErr );
801                         }
802                         else
803                         {
804                             // Create new document
805                             SfxViewFrame *pFrame = SfxViewFrame::LoadDocument( *xDocSh, SFX_INTERFACE_NONE );
806                             SwView      *pCurrView = static_cast<SwView*>( pFrame->GetViewShell());
807 
808                             // Set document's title
809                             OUString aTmp = SwResId(STR_ABSTRACT_TITLE) + GetTitle();
810                             xDocSh->SetTitle( aTmp );
811                             pCurrView->GetWrtShell().SetNewDoc();
812                             pFrame->Show();
813                             pSmryDoc->getIDocumentState().SetModified();
814                         }
815                     }
816                     pDlg->disposeOnce();
817                 }
818             );
819         }
820         break;
821         case FN_OUTLINE_TO_CLIPBOARD:
822         case FN_OUTLINE_TO_IMPRESS:
823             {
824                 bool bEnable = IsEnableSetModified();
825                 EnableSetModified( false );
826                 WriterRef xWrt;
827                 // mba: looks as if relative URLs don't make sense here
828                 ::GetRTFWriter( u"O", OUString(), xWrt );
829                 std::unique_ptr<SvMemoryStream> pStrm (new SvMemoryStream());
830                 pStrm->SetBufferSize( 16348 );
831                 SwWriter aWrt( *pStrm, *GetDoc() );
832                 ErrCodeMsg eErr = aWrt.Write( xWrt );
833                 EnableSetModified( bEnable );
834                 if( !eErr.IgnoreWarning() )
835                 {
836                     pStrm->Seek( STREAM_SEEK_TO_END );
837                     pStrm->WriteChar( '\0' );
838                     pStrm->Seek( STREAM_SEEK_TO_BEGIN );
839                     if ( nWhich == FN_OUTLINE_TO_IMPRESS )
840                     {
841                         const uno::Reference< uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext();
842                         uno::Reference< frame::XDispatchProvider > xProv = drawing::ModuleDispatcher::create( xContext );
843 
844                         uno::Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create(xContext) );
845                         pStrm->Seek( STREAM_SEEK_TO_END );
846                         pStrm->WriteChar( '\0' );
847                         pStrm->Seek( STREAM_SEEK_TO_BEGIN );
848 
849                         uno::Sequence< sal_Int8 > aSeq( pStrm->TellEnd() );
850                         pStrm->ReadBytes( aSeq.getArray(), aSeq.getLength() );
851 
852                         uno::Sequence< beans::PropertyValue > aArgs{
853                             comphelper::makePropertyValue(u"RtfOutline"_ustr, aSeq)
854                         };
855                         xHelper->executeDispatch( xProv, u"SendOutlineToImpress"_ustr, OUString(), 0, aArgs );
856                     }
857                     else
858                     {
859                         rtl::Reference<TransferDataContainer> pClipCntnr = new TransferDataContainer;
860 
861                         pClipCntnr->CopyAnyData( SotClipboardFormatId::RTF, static_cast<char const *>(
862                                     pStrm->GetData()), pStrm->GetEndOfData() );
863                         pClipCntnr->CopyToClipboard(
864                             GetView()? &GetView()->GetEditWin() : nullptr );
865                     }
866                 }
867                 else
868                     ErrorHandler::HandleError( eErr );
869             }
870             break;
871             case SID_SPELLCHECKER_CHANGED:
872             {
873                 //! false, true, true is on the save side but a probably overdone
874                 SwModule::CheckSpellChanges(false, true, true, false );
875                 Broadcast(SfxHint(SfxHintId::LanguageChanged));
876             }
877             break;
878 
879         case SID_MAIL_PREPAREEXPORT:
880         {
881             const SfxBoolItem* pNoUpdate = pArgs ?
882                 pArgs->GetItem<SfxBoolItem>(FN_NOUPDATE, false) :
883                 nullptr;
884 
885             //pWrtShell is not set in page preview
886             if (m_pWrtShell)
887                 m_pWrtShell->StartAllAction();
888 
889             if (!pNoUpdate || !pNoUpdate->GetValue())
890             {
891                 m_xDoc->getIDocumentFieldsAccess().UpdateFields( false );
892                 m_xDoc->getIDocumentLinksAdministration().EmbedAllLinks();
893             }
894 
895             m_IsRemovedInvisibleContent
896                 = officecfg::Office::Security::HiddenContent::RemoveHiddenContent::get();
897             if (m_IsRemovedInvisibleContent)
898                 m_xDoc->RemoveInvisibleContent();
899             if (m_pWrtShell)
900                 m_pWrtShell->EndAllAction();
901         }
902         break;
903 
904         case SID_MAIL_EXPORT_FINISHED:
905         {
906                 if (m_pWrtShell)
907                     m_pWrtShell->StartAllAction();
908                 //try to undo the removal of invisible content
909                 if (m_IsRemovedInvisibleContent)
910                     m_xDoc->RestoreInvisibleContent();
911                 if (m_pWrtShell)
912                     m_pWrtShell->EndAllAction();
913         }
914         break;
915         case FN_NEW_HTML_DOC:
916         case FN_NEW_GLOBAL_DOC:
917             {
918                 bDone = false;
919                 bool bCreateHtml = FN_NEW_HTML_DOC == nWhich;
920 
921                 bool bCreateByOutlineLevel = false;
922                 sal_Int32  nTemplateOutlineLevel = 0;
923 
924                 OUString aFileName, aTemplateName;
925                 if( pArgs && SfxItemState::SET == pArgs->GetItemState( nWhich, false, &pItem ) )
926                 {
927                     aFileName = static_cast<const SfxStringItem*>(pItem)->GetValue();
928                     const SfxStringItem* pTemplItem = SfxItemSet::GetItem<SfxStringItem>(pArgs, SID_TEMPLATE_NAME, false);
929                     if ( pTemplItem )
930                         aTemplateName = pTemplItem->GetValue();
931                 }
932                 if ( aFileName.isEmpty() )
933                 {
934                     bool bError = false;
935 
936                     FileDialogHelper aDlgHelper(TemplateDescription::FILESAVE_AUTOEXTENSION_TEMPLATE, FileDialogFlags::NONE,
937                                                 GetView()->GetFrameWeld());
938                     aDlgHelper.SetContext(FileDialogHelper::WriterNewHTMLGlobalDoc);
939 
940                     const sal_Int16 nControlIds[] = {
941                         CommonFilePickerElementIds::PUSHBUTTON_OK,
942                         CommonFilePickerElementIds::PUSHBUTTON_CANCEL,
943                         CommonFilePickerElementIds::LISTBOX_FILTER,
944                         CommonFilePickerElementIds::CONTROL_FILEVIEW,
945                         CommonFilePickerElementIds::EDIT_FILEURL,
946                         ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION,
947                         ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
948                         0
949                     };
950 
951                     if (bCreateHtml)
952                     {
953                         const char* aHTMLHelpIds[] =
954                         {
955                              HID_SEND_HTML_CTRL_PUSHBUTTON_OK,
956                              HID_SEND_HTML_CTRL_PUSHBUTTON_CANCEL,
957                              HID_SEND_HTML_CTRL_LISTBOX_FILTER,
958                              HID_SEND_HTML_CTRL_CONTROL_FILEVIEW,
959                              HID_SEND_HTML_CTRL_EDIT_FILEURL,
960                              HID_SEND_HTML_CTRL_CHECKBOX_AUTOEXTENSION,
961                              HID_SEND_HTML_CTRL_LISTBOX_TEMPLATE,
962                              ""
963                         };
964                         aDlgHelper.SetControlHelpIds( nControlIds, aHTMLHelpIds );
965                     }
966                     else
967                     {
968                         const char* aMasterHelpIds[] =
969                         {
970                              HID_SEND_MASTER_CTRL_PUSHBUTTON_OK,
971                              HID_SEND_MASTER_CTRL_PUSHBUTTON_CANCEL,
972                              HID_SEND_MASTER_CTRL_LISTBOX_FILTER,
973                              HID_SEND_MASTER_CTRL_CONTROL_FILEVIEW,
974                              HID_SEND_MASTER_CTRL_EDIT_FILEURL,
975                              HID_SEND_MASTER_CTRL_CHECKBOX_AUTOEXTENSION,
976                              HID_SEND_MASTER_CTRL_LISTBOX_TEMPLATE,
977                              ""
978                         };
979                         aDlgHelper.SetControlHelpIds( nControlIds, aMasterHelpIds );
980                     }
981                     uno::Reference < XFilePicker3 > xFP = aDlgHelper.GetFilePicker();
982 
983                     std::shared_ptr<const SfxFilter> pFlt;
984                     TranslateId pStrId;
985 
986                     if( bCreateHtml )
987                     {
988                         // for HTML there is only one filter!!
989                         pFlt = SwIoSystem::GetFilterOfFormat(
990                                 u"HTML",
991                                 SwWebDocShell::Factory().GetFilterContainer() );
992                         pStrId = STR_LOAD_HTML_DOC;
993                     }
994                     else
995                     {
996                         // for Global-documents we now only offer the current one.
997                         pFlt = SwGlobalDocShell::Factory().GetFilterContainer()->
998                                     GetFilter4Extension( u"odm"_ustr  );
999                         pStrId = STR_LOAD_GLOBAL_DOC;
1000                     }
1001 
1002                     if( pFlt )
1003                     {
1004                         const OUString sWild = pFlt->GetWildcard().getGlob();
1005                         xFP->appendFilter( pFlt->GetUIName(), sWild );
1006                         try
1007                         {
1008                             xFP->setCurrentFilter( pFlt->GetUIName() ) ;
1009                         }
1010                         catch (const uno::Exception&)
1011                         {
1012                             bError = true;
1013                         }
1014                     }
1015                     if(!bError)
1016                     {
1017                         uno::Reference<XFilePickerControlAccess> xCtrlAcc(xFP, UNO_QUERY);
1018 
1019                         bool    bOutline[MAXLEVEL] = {false};
1020                         const SwOutlineNodes& rOutlNds = m_xDoc->GetNodes().GetOutLineNds();
1021                         for( size_t n = 0; n < rOutlNds.size(); ++n )
1022                         {
1023                             const int nLevel = rOutlNds[n]->GetTextNode()->GetAttrOutlineLevel();
1024                             if( nLevel > 0 && ! bOutline[nLevel-1] )
1025                             {
1026                                 bOutline[nLevel-1] = true;
1027                             }
1028                         }
1029 
1030                         const sal_uInt16 nStyleCount = m_xDoc->GetTextFormatColls()->size();
1031                         Sequence<OUString> aListBoxEntries( MAXLEVEL + nStyleCount);
1032                         OUString* pEntries = aListBoxEntries.getArray();
1033                         sal_Int32   nIdx = 0 ;
1034 
1035                         OUString    sOutline( SwResId(STR_FDLG_OUTLINE_LEVEL) );
1036                         for( sal_uInt16 i = 0; i < MAXLEVEL; ++i )
1037                         {
1038                             if( bOutline[i] )
1039                                 pEntries[nIdx++] = sOutline + OUString::number( i+1 );
1040                         }
1041 
1042                         OUString    sStyle( SwResId(STR_FDLG_STYLE) );
1043                         for(sal_uInt16 i = 0; i < nStyleCount; ++i)
1044                         {
1045                             SwTextFormatColl &rTextColl = *(*m_xDoc->GetTextFormatColls())[ i ];
1046                             if( !rTextColl.IsDefault() && rTextColl.IsAtDocNodeSet() )
1047                             {
1048                                 pEntries[nIdx++] = sStyle + rTextColl.GetName().toString();
1049                             }
1050                         }
1051 
1052                         aListBoxEntries.realloc(nIdx);
1053                         sal_Int16 nSelect = 0;
1054 
1055                         try
1056                         {
1057                             Any aTemplates(&aListBoxEntries, cppu::UnoType<decltype(aListBoxEntries)>::get());
1058 
1059                             xCtrlAcc->setValue( ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1060                                 ListboxControlActions::ADD_ITEMS , aTemplates );
1061                             Any aSelectPos(&nSelect, cppu::UnoType<decltype(nSelect)>::get());
1062                             xCtrlAcc->setValue( ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1063                                 ListboxControlActions::SET_SELECT_ITEM, aSelectPos );
1064                             xCtrlAcc->setLabel( ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1065                                                     SwResId( STR_FDLG_TEMPLATE_NAME ));
1066                         }
1067                         catch (const Exception&)
1068                         {
1069                             OSL_FAIL("control access failed");
1070                         }
1071 
1072                         xFP->setTitle(SwResId(pStrId));
1073                         SvtPathOptions aPathOpt;
1074                         xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
1075                         if( ERRCODE_NONE == aDlgHelper.Execute())
1076                         {
1077                             aFileName = xFP->getSelectedFiles().getConstArray()[0];
1078                             Any aTemplateValue = xCtrlAcc->getValue(
1079                                 ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1080                                 ListboxControlActions::GET_SELECTED_ITEM );
1081                             OUString sTmpl;
1082                             aTemplateValue >>= sTmpl;
1083 
1084                             OUString aStyle(SwResId(STR_FDLG_STYLE));
1085                             OUString aOutline(SwResId(STR_FDLG_OUTLINE_LEVEL));
1086 
1087                             if ( sTmpl.startsWith(aStyle) )
1088                             {
1089                                 aTemplateName = sTmpl.copy( aStyle.getLength() );   //get string behind "Style: "
1090                             }
1091                             else if ( sTmpl.startsWith(aOutline) )
1092                             {
1093                                 nTemplateOutlineLevel = o3tl::toInt32(sTmpl.subView(aOutline.getLength())); //get string behind "Outline: Level  ";
1094                                 bCreateByOutlineLevel = true;
1095                             }
1096 
1097                             if ( !aFileName.isEmpty() )
1098                             {
1099                                 rReq.AppendItem( SfxStringItem( nWhich, aFileName ) );
1100                                 if( !aTemplateName.isEmpty() )
1101                                     rReq.AppendItem( SfxStringItem( SID_TEMPLATE_NAME, aTemplateName ) );
1102                             }
1103                         }
1104                     }
1105                 }
1106 
1107                 if( !aFileName.isEmpty() )
1108                 {
1109                     if( PrepareClose( false ) )
1110                     {
1111                         SwWait aWait( *this, true );
1112 
1113                         if ( bCreateByOutlineLevel )
1114                         {
1115                             bDone = bCreateHtml
1116                                 ? m_xDoc->GenerateHTMLDoc( aFileName, nTemplateOutlineLevel )
1117                                 : m_xDoc->GenerateGlobalDoc( aFileName, nTemplateOutlineLevel );
1118                         }
1119                         else
1120                         {
1121                             const SwTextFormatColl* pSplitColl = nullptr;
1122                             if ( !aTemplateName.isEmpty() )
1123                                 pSplitColl = m_xDoc->FindTextFormatCollByName(UIName(aTemplateName));
1124                             bDone = bCreateHtml
1125                                 ? m_xDoc->GenerateHTMLDoc( aFileName, pSplitColl )
1126                                 : m_xDoc->GenerateGlobalDoc( aFileName, pSplitColl );
1127                         }
1128                         if( bDone )
1129                         {
1130                             SfxStringItem aName( SID_FILE_NAME, aFileName );
1131                             SfxStringItem aReferer(SID_REFERER, OUString());
1132                             SfxViewShell* pViewShell = SfxViewShell::GetFirst();
1133                             while(pViewShell)
1134                             {
1135                                 //search for the view that created the call
1136                                 if(pViewShell->GetObjectShell() == this && pViewShell->GetDispatcher())
1137                                 {
1138                                     SfxFrameItem aFrameItem(SID_DOCFRAME, &pViewShell->GetViewFrame());
1139                                     SfxDispatcher* pDispatch = pViewShell->GetDispatcher();
1140                                     pDispatch->ExecuteList(SID_OPENDOC,
1141                                         SfxCallMode::ASYNCHRON,
1142                                         { &aName, &aReferer, &aFrameItem });
1143                                     break;
1144                                 }
1145                                 pViewShell = SfxViewShell::GetNext(*pViewShell);
1146                             }
1147                         }
1148                     }
1149                     if( !bDone && !rReq.IsAPI() )
1150                     {
1151                         std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(nullptr,
1152                                                                       VclMessageType::Info, VclButtonsType::Ok,
1153                                                                       SwResId(STR_CANTCREATE)));
1154                         xInfoBox->run();
1155                     }
1156                 }
1157             }
1158             rReq.SetReturnValue(SfxBoolItem( nWhich, bDone ));
1159             if (bDone)
1160                 rReq.Done();
1161             else
1162                 rReq.Ignore();
1163             break;
1164 
1165         case SID_ATTR_YEAR2000:
1166             if ( pArgs && SfxItemState::SET == pArgs->GetItemState( nWhich , false, &pItem ))
1167             {
1168                 assert(dynamic_cast< const SfxUInt16Item *>( pItem ) && "wrong Item");
1169                 sal_uInt16 nYear2K = static_cast<const SfxUInt16Item*>(pItem)->GetValue();
1170                 // iterate over Views and put the State to FormShells
1171 
1172                 SfxViewFrame* pVFrame = SfxViewFrame::GetFirst( this );
1173                 SfxViewShell* pViewShell = pVFrame ? pVFrame->GetViewShell() : nullptr;
1174                 SwView* pCurrView = dynamic_cast< SwView* >( pViewShell );
1175                 while(pCurrView)
1176                 {
1177                     FmFormShell* pFormShell = pCurrView->GetFormShell();
1178                     if(pFormShell)
1179                         pFormShell->SetY2KState(nYear2K);
1180                     pVFrame = SfxViewFrame::GetNext( *pVFrame, this );
1181                     pViewShell = pVFrame ? pVFrame->GetViewShell() : nullptr;
1182                     pCurrView = dynamic_cast<SwView*>( pViewShell );
1183                 }
1184                 m_xDoc->GetNumberFormatter()->SetYear2000(nYear2K);
1185             }
1186         break;
1187         case FN_OPEN_FILE:
1188         {
1189             SfxViewShell* pViewShell = GetView();
1190             if (!pViewShell)
1191                 pViewShell = SfxViewShell::Current();
1192 
1193             if (!pViewShell)
1194                 // Ok.  I did my best.
1195                 break;
1196 
1197             if (SfxDispatcher* pDispatch = pViewShell->GetDispatcher())
1198             {
1199                 SfxStringItem aApp(SID_DOC_SERVICE, u"com.sun.star.text.TextDocument"_ustr);
1200                 SfxStringItem aTarget(SID_TARGETNAME, u"_blank"_ustr);
1201                 pDispatch->ExecuteList(SID_OPENDOC, SfxCallMode::API|SfxCallMode::SYNCHRON, { &aApp, &aTarget });
1202             }
1203         }
1204         break;
1205         case SID_CLASSIFICATION_APPLY:
1206         {
1207             if (pArgs && pArgs->GetItemState(nWhich, false, &pItem) == SfxItemState::SET)
1208             {
1209                 SwWrtShell* pSh = GetWrtShell();
1210                 const OUString& rValue = static_cast<const SfxStringItem*>(pItem)->GetValue();
1211                 auto eType = SfxClassificationPolicyType::IntellectualProperty;
1212                 if (const SfxStringItem* pTypeNameItem = pArgs->GetItemIfSet(SID_TYPE_NAME, false))
1213                 {
1214                     const OUString& rType = pTypeNameItem->GetValue();
1215                     eType = SfxClassificationHelper::stringToPolicyType(rType);
1216                 }
1217                 pSh->SetClassification(rValue, eType);
1218             }
1219             else
1220                 SAL_WARN("sw.ui", "missing parameter for SID_CLASSIFICATION_APPLY");
1221         }
1222         break;
1223         case SID_CLASSIFICATION_DIALOG:
1224             if (SfxObjectShell* pObjSh = SfxObjectShell::Current())
1225             {
1226                 auto xDialog = std::make_shared<svx::ClassificationDialog>(GetView()->GetFrameWeld(), pObjSh->getDocProperties(), false);
1227 
1228                 SwWrtShell* pShell = GetWrtShell();
1229                 std::vector<svx::ClassificationResult> aInput = pShell->CollectAdvancedClassification();
1230                 xDialog->setupValues(std::move(aInput));
1231 
1232                 weld::DialogController::runAsync(xDialog, [xDialog, pShell](sal_Int32 nResult){
1233                     if (RET_OK == nResult)
1234                         pShell->ApplyAdvancedClassification(xDialog->getResult());
1235                 });
1236             }
1237             break;
1238         case SID_PARAGRAPH_SIGN_CLASSIFY_DLG:
1239             if (SfxObjectShell* pObjSh = SfxObjectShell::Current())
1240             {
1241                 SwWrtShell* pShell = GetWrtShell();
1242                 auto xDialog = std::make_shared<svx::ClassificationDialog>(GetView()->GetFrameWeld(), pObjSh->getDocProperties(), true, [pShell]()
1243                 {
1244                     pShell->SignParagraph();
1245                 });
1246 
1247                 std::vector<svx::ClassificationResult> aInput = pShell->CollectParagraphClassification();
1248                 xDialog->setupValues(std::move(aInput));
1249 
1250                 weld::DialogController::runAsync(xDialog, [xDialog, pShell](sal_Int32 nResult){
1251                     if (RET_OK == nResult)
1252                         pShell->ApplyParagraphClassification(xDialog->getResult());
1253                 });
1254             }
1255             break;
1256         case SID_WATERMARK:
1257         {
1258             SwWrtShell* pSh = GetWrtShell();
1259             if ( pSh )
1260             {
1261                 if (pArgs && pArgs->GetItemState( SID_WATERMARK, false, &pItem ) == SfxItemState::SET)
1262                 {
1263                     SfxWatermarkItem aItem;
1264                     aItem.SetText( static_cast<const SfxStringItem*>( pItem )->GetValue() );
1265 
1266                     if ( const SfxStringItem* pFontItem = pArgs->GetItemIfSet( SID_WATERMARK_FONT, false ) )
1267                         aItem.SetFont( pFontItem->GetValue() );
1268                     if ( const SfxInt16Item* pAngleItem = pArgs->GetItemIfSet( SID_WATERMARK_ANGLE, false ) )
1269                         aItem.SetAngle( pAngleItem->GetValue() );
1270                     if ( const SfxInt16Item* pTransItem = pArgs->GetItemIfSet( SID_WATERMARK_TRANSPARENCY, false ) )
1271                         aItem.SetTransparency( pTransItem->GetValue() );
1272                     if ( const SfxUInt32Item* pColorItem = pArgs->GetItemIfSet( SID_WATERMARK_COLOR, false ) )
1273                         aItem.SetColor( Color(ColorTransparency, pColorItem->GetValue()) );
1274 
1275                     pSh->SetWatermark( aItem );
1276                 }
1277                 else
1278                 {
1279                     SfxViewShell* pViewShell = GetView() ? GetView() : SfxViewShell::Current();
1280                     SfxBindings& rBindings( pViewShell->GetViewFrame().GetBindings() );
1281                     auto xDlg = std::make_shared<SwWatermarkDialog>(pViewShell->GetViewFrame().GetFrameWeld(),
1282                                                                                   rBindings);
1283                     weld::DialogController::runAsync(xDlg, [](sal_Int32 /*nResult*/){});
1284                 }
1285             }
1286         }
1287         break;
1288         case SID_NOTEBOOKBAR:
1289         {
1290             const SfxStringItem* pFile = rReq.GetArg<SfxStringItem>( SID_NOTEBOOKBAR );
1291             SfxViewShell* pViewShell = GetView()? GetView(): SfxViewShell::Current();
1292             SfxBindings& rBindings( pViewShell->GetViewFrame().GetBindings() );
1293 
1294             if ( SfxNotebookBar::IsActive() )
1295                 sfx2::SfxNotebookBar::ExecMethod( rBindings, pFile ? pFile->GetValue() : u""_ustr );
1296             else
1297             {
1298                 sfx2::SfxNotebookBar::CloseMethod( rBindings );
1299             }
1300         }
1301         break;
1302         case FN_REDLINE_ACCEPT_ALL:
1303         case FN_REDLINE_REJECT_ALL:
1304         case FN_REDLINE_REINSTATE_ALL:
1305         {
1306             IDocumentRedlineAccess& rRedlineAccess = GetDoc()->getIDocumentRedlineAccess();
1307             SwWrtShell *pWrtShell = dynamic_cast<SwWrtShell*>(GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell());
1308 
1309             if (rRedlineAccess.GetRedlineTable().empty())
1310             {
1311                 break;
1312             }
1313 
1314             // tables with tracked deletion need Show Changes
1315             bool bHideChanges = pWrtShell && pWrtShell->GetLayout() &&
1316                                 pWrtShell->GetLayout()->IsHideRedlines();
1317             bool bChangedHideChanges = false;
1318             if ( bHideChanges )
1319             {
1320                 SwTableNode* pOldTableNd = nullptr;
1321                 const SwRedlineTable& aRedlineTable = rRedlineAccess.GetRedlineTable();
1322                 for (SwRedlineTable::size_type n = 0; n < aRedlineTable.size(); ++n)
1323                 {
1324                     const SwRangeRedline* pRedline = aRedlineTable[n];
1325                     if ( pRedline->GetType() == RedlineType::Delete )
1326                     {
1327                         SwTableNode* pTableNd =
1328                             pRedline->GetPoint()->GetNode().FindTableNode();
1329                         if ( pTableNd && pTableNd !=
1330                                 pOldTableNd && pTableNd->GetTable().HasDeletedRowOrCell() )
1331                         {
1332                             SfxBoolItem aShow(FN_REDLINE_SHOW, true);
1333                             SfxViewShell* pViewShell = GetView()
1334                                     ? GetView()
1335                                     : SfxViewShell::Current();
1336                             pViewShell->GetViewFrame().GetDispatcher()->ExecuteList(
1337                                     FN_REDLINE_SHOW, SfxCallMode::SYNCHRON|SfxCallMode::RECORD,
1338                                     { &aShow });
1339                             bChangedHideChanges = true;
1340                             break;
1341                         }
1342                         pOldTableNd = pTableNd;
1343                     }
1344                 }
1345             }
1346 
1347             if (pWrtShell)
1348             {
1349                 pWrtShell->StartAllAction();
1350             }
1351 
1352             if (nWhich == FN_REDLINE_REINSTATE_ALL)
1353             {
1354                 pWrtShell->SelAll();
1355                 pWrtShell->ReinstateRedlinesInSelection();
1356             }
1357             else
1358             {
1359                 rRedlineAccess.AcceptAllRedline(nWhich == FN_REDLINE_ACCEPT_ALL);
1360             }
1361 
1362             if (pWrtShell)
1363             {
1364                 pWrtShell->EndAllAction();
1365             }
1366 
1367             if ( bChangedHideChanges )
1368             {
1369                 SfxBoolItem aShow(FN_REDLINE_SHOW, false);
1370                 SfxViewShell* pViewShell = GetView()? GetView(): SfxViewShell::Current();
1371                 pViewShell->GetViewFrame().GetDispatcher()->ExecuteList(
1372                         FN_REDLINE_SHOW, SfxCallMode::SYNCHRON|SfxCallMode::RECORD, { &aShow });
1373             }
1374 
1375             Broadcast(SfxHint(SfxHintId::RedlineChanged));
1376             rReq.Done();
1377         }
1378         break;
1379 
1380         default: OSL_FAIL("wrong Dispatcher");
1381     }
1382 }
1383 
1384 #if defined(_WIN32)
DdeGetData(const OUString & rItem,const OUString & rMimeType,uno::Any & rValue)1385 bool SwDocShell::DdeGetData( const OUString& rItem, const OUString& rMimeType,
1386                              uno::Any & rValue )
1387 {
1388     return m_xDoc->getIDocumentLinksAdministration().GetData( rItem, rMimeType, rValue );
1389 }
1390 
DdeSetData(const OUString & rItem,const OUString &,const uno::Any &)1391 bool SwDocShell::DdeSetData( const OUString& rItem, const OUString& /*rMimeType*/,
1392                              const uno::Any & /*rValue*/ )
1393 {
1394     m_xDoc->getIDocumentLinksAdministration().SetData( rItem );
1395     return false;
1396 }
1397 
1398 #endif
1399 
DdeCreateLinkSource(const OUString & rItem)1400 ::sfx2::SvLinkSource* SwDocShell::DdeCreateLinkSource( const OUString& rItem )
1401 {
1402     if(officecfg::Office::Common::Security::Scripting::DisableActiveContent::get())
1403         return nullptr;
1404     return m_xDoc->getIDocumentLinksAdministration().CreateLinkSource( rItem );
1405 }
1406 
ReconnectDdeLink(SfxObjectShell & rServer)1407 void SwDocShell::ReconnectDdeLink(SfxObjectShell& rServer)
1408 {
1409     if (m_xDoc)
1410     {
1411         ::sfx2::LinkManager& rLinkManager = m_xDoc->getIDocumentLinksAdministration().GetLinkManager();
1412         rLinkManager.ReconnectDdeLink(rServer);
1413     }
1414 }
1415 
FillClass(SvGlobalName * pClassName,SotClipboardFormatId * pClipFormat,OUString * pLongUserName,sal_Int32 nVersion,bool bTemplate) const1416 void SwDocShell::FillClass( SvGlobalName * pClassName,
1417                                    SotClipboardFormatId * pClipFormat,
1418                                    OUString * pLongUserName,
1419                                    sal_Int32 nVersion,
1420                                    bool bTemplate /* = false */) const
1421 {
1422     if (nVersion == SOFFICE_FILEFORMAT_60)
1423     {
1424         *pClassName     = SvGlobalName( SO3_SW_CLASSID_60 );
1425         *pClipFormat    = SotClipboardFormatId::STARWRITER_60;
1426         *pLongUserName = SwResId(STR_WRITER_DOCUMENT_FULLTYPE);
1427     }
1428     else if (nVersion == SOFFICE_FILEFORMAT_8)
1429     {
1430         *pClassName     = SvGlobalName( SO3_SW_CLASSID_60 );
1431         *pClipFormat    = bTemplate ? SotClipboardFormatId::STARWRITER_8_TEMPLATE : SotClipboardFormatId::STARWRITER_8;
1432         *pLongUserName = SwResId(STR_WRITER_DOCUMENT_FULLTYPE);
1433     }
1434 // #FIXME check with new Event handling
1435 #if 0
1436     uno::Reference< document::XVbaEventsHelper > xVbaEventsHelper = m_xDoc->GetVbaEventsHelper();
1437     if( xVbaEventsHelper.is() )
1438         lcl_processCompatibleSfxHint( xVbaEventsHelper, rHint );
1439 #endif
1440 }
1441 
SetModified(bool bSet)1442 void SwDocShell::SetModified( bool bSet )
1443 {
1444     if (comphelper::IsFuzzing())
1445         return;
1446     SfxObjectShell::SetModified( bSet );
1447     if( !IsEnableSetModified())
1448         return;
1449 
1450     if (!m_xDoc->getIDocumentState().IsInCallModified())
1451     {
1452         EnableSetModified( false );
1453         if( bSet )
1454         {
1455             bool const bOld = m_xDoc->getIDocumentState().IsModified();
1456             m_xDoc->getIDocumentState().SetModified();
1457             if( !bOld )
1458             {
1459                 m_xDoc->GetIDocumentUndoRedo().SetUndoNoResetModified();
1460             }
1461         }
1462         else
1463             m_xDoc->getIDocumentState().ResetModified();
1464 
1465         EnableSetModified();
1466     }
1467 
1468     UpdateChildWindows();
1469     Broadcast(SfxHint(SfxHintId::DocChanged));
1470 }
1471 
UpdateChildWindows()1472 void SwDocShell::UpdateChildWindows()
1473 {
1474     // if necessary newly initialize Fielddlg (i.e. for TYP_SETVAR)
1475     if(!GetView())
1476         return;
1477     SfxViewFrame& rVFrame = GetView()->GetViewFrame();
1478     SwFieldDlgWrapper *pWrp = static_cast<SwFieldDlgWrapper*>(rVFrame.
1479             GetChildWindow( SwFieldDlgWrapper::GetChildWindowId() ));
1480     if( pWrp )
1481         pWrp->ReInitDlg();
1482 
1483     // if necessary newly initialize RedlineDlg
1484     SwRedlineAcceptChild *pRed = static_cast<SwRedlineAcceptChild*>(rVFrame.
1485             GetChildWindow( SwRedlineAcceptChild::GetChildWindowId() ));
1486     if( pRed )
1487         pRed->ReInitDlg();
1488 }
1489 
1490 namespace {
1491 
1492 // #i48748#
1493 class SwReloadFromHtmlReader : public SwReader
1494 {
1495     public:
SwReloadFromHtmlReader(SfxMedium & _rTmpMedium,const OUString & _rFilename,SwDoc * _pDoc)1496         SwReloadFromHtmlReader( SfxMedium& _rTmpMedium,
1497                                 const OUString& _rFilename,
1498                                 SwDoc* _pDoc )
1499             : SwReader( _rTmpMedium, _rFilename, _pDoc )
1500         {
1501             SetBaseURL( _rFilename );
1502         }
1503 };
1504 
1505 }
1506 
ReloadFromHtml(const OUString & rStreamName,SwSrcView * pSrcView)1507 void SwDocShell::ReloadFromHtml( const OUString& rStreamName, SwSrcView* pSrcView )
1508 {
1509     bool bModified = IsModified();
1510 
1511     // The HTTP-Header fields have to be removed, otherwise
1512     // there are some from Meta-Tags duplicated or triplicated afterwards.
1513     ClearHeaderAttributesForSourceViewHack();
1514 
1515 #if HAVE_FEATURE_SCRIPTING
1516     // The Document-Basic also bites the dust ...
1517     // A EnterBasicCall is not needed here, because nothing is called and
1518     // there can't be any Dok-Basic, that has not yet been loaded inside
1519     // of an HTML document.
1520     //#59620# HasBasic() shows, that there already is a BasicManager at the DocShell.
1521     //          That was always generated in HTML-Import, when there are
1522     //          Macros in the source code.
1523     if( officecfg::Office::Common::Filter::HTML::Export::Basic::get() && HasBasic())
1524     {
1525         BasicManager *pBasicMan = GetBasicManager();
1526         if( pBasicMan && (pBasicMan != SfxApplication::GetBasicManager()) )
1527         {
1528             sal_uInt16 nLibCount = pBasicMan->GetLibCount();
1529             while( nLibCount )
1530             {
1531                 StarBASIC *pBasic = pBasicMan->GetLib( --nLibCount );
1532                 if( pBasic )
1533                 {
1534                     // Notify the IDE
1535                     SfxUnoAnyItem aShellItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, Any( GetModel() ) );
1536                     OUString aLibName( pBasic->GetName() );
1537                     SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
1538                     pSrcView->GetViewFrame().GetDispatcher()->ExecuteList(
1539                                             SID_BASICIDE_LIBREMOVED,
1540                                             SfxCallMode::SYNCHRON,
1541                                             { &aShellItem, &aLibNameItem });
1542 
1543                     // Only the modules are deleted from the standard-lib
1544                     if( nLibCount )
1545                         pBasicMan->RemoveLib( nLibCount, true );
1546                     else
1547                         pBasic->Clear();
1548                 }
1549             }
1550 
1551             OSL_ENSURE( pBasicMan->GetLibCount() <= 1,
1552                     "Deleting Basics didn't work" );
1553         }
1554     }
1555 #endif
1556     bool bWasBrowseMode = m_xDoc->getIDocumentSettingAccess().get(DocumentSettingId::BROWSE_MODE);
1557     RemoveLink();
1558 
1559     // now also the UNO-Model has to be informed about the new Doc #51535#
1560     rtl::Reference<SwXTextDocument> xDoc(GetBaseModel());
1561     xDoc->InitNewDoc();
1562 
1563     AddLink();
1564     //#116402# update font list when new document is created
1565     UpdateFontList();
1566     m_xDoc->getIDocumentSettingAccess().set(DocumentSettingId::BROWSE_MODE, bWasBrowseMode);
1567     pSrcView->SetPool(&GetPool());
1568 
1569     const OUString& rMedname = GetMedium()->GetName();
1570 
1571     // The HTML template still has to be set
1572     SetHTMLTemplate( *GetDoc() );   //Styles from HTML.vor
1573 
1574     if (SfxViewShell* pViewShell = GetView() ? static_cast<SfxViewShell*>(GetView())
1575                                              : SfxViewShell::Current())
1576     {
1577         SfxViewFrame& rViewFrame = pViewShell->GetViewFrame();
1578         rViewFrame.GetDispatcher()->Execute( SID_VIEWSHELL0, SfxCallMode::SYNCHRON );
1579     }
1580 
1581     SubInitNew();
1582 
1583     SfxMedium aMed( rStreamName, StreamMode::READ );
1584     // #i48748# - use class <SwReloadFromHtmlReader>, because
1585     // the base URL has to be set to the filename of the document <rMedname>
1586     // and not to the base URL of the temporary file <aMed> in order to get
1587     // the URLs of the linked graphics correctly resolved.
1588     SwReloadFromHtmlReader aReader( aMed, rMedname, m_xDoc.get() );
1589 
1590     aReader.Read( *ReadHTML );
1591 
1592     const SwView* pCurrView = GetView();
1593     //in print layout the first page(s) may have been formatted as a mix of browse
1594     //and print layout
1595     if(!bWasBrowseMode && pCurrView)
1596     {
1597         SwWrtShell& rWrtSh = pCurrView->GetWrtShell();
1598         if( rWrtSh.GetLayout())
1599             rWrtSh.InvalidateLayout( true );
1600     }
1601 
1602     // Take HTTP-Header-Attributes over into the DocInfo again.
1603     // The Base-URL doesn't matter here because TLX uses the one from the document
1604     // for absolutization.
1605     SetHeaderAttributesForSourceViewHack();
1606 
1607     if(bModified && !IsReadOnly())
1608         SetModified();
1609     else
1610         m_xDoc->getIDocumentState().ResetModified();
1611 }
1612 
LoadStylesFromFile(const OUString & rURL,SwgReaderOption & rOpt,bool bUnoCall)1613 ErrCodeMsg SwDocShell::LoadStylesFromFile(const OUString& rURL, SwgReaderOption& rOpt, bool bUnoCall)
1614 {
1615     ErrCodeMsg nErr = ERRCODE_NONE;
1616 
1617     // Set filter:
1618     SfxFilterMatcher aMatcher( SwDocShell::Factory().GetFactoryName() );
1619 
1620     // search for filter in WebDocShell, too
1621     SfxMedium aMed( rURL, StreamMode::STD_READ );
1622     if (rURL == "private:stream")
1623         aMed.setStreamToLoadFrom(rOpt.GetInputStream(), true);
1624     std::shared_ptr<const SfxFilter> pFlt;
1625     aMatcher.DetectFilter( aMed, pFlt );
1626     if(!pFlt)
1627     {
1628         SfxFilterMatcher aWebMatcher( SwWebDocShell::Factory().GetFactoryName() );
1629         aWebMatcher.DetectFilter( aMed, pFlt );
1630     }
1631     // --> OD #i117339# - trigger import only for own formats
1632     bool bImport( false );
1633     if ( aMed.IsStorage() )
1634     {
1635         // As <SfxMedium.GetFilter().IsOwnFormat() resp. IsOwnTemplateFormat()
1636         // does not work correct (e.g., MS Word 2007 XML Template),
1637         // use workaround provided by MAV.
1638         uno::Reference< embed::XStorage > xStorage = aMed.GetStorage();
1639         if ( xStorage.is() )
1640         {
1641             // use <try-catch> on retrieving <MediaType> in order to check,
1642             // if the storage is one of our own ones.
1643             try
1644             {
1645                 uno::Reference< beans::XPropertySet > xProps( xStorage, uno::UNO_QUERY_THROW );
1646                 xProps->getPropertyValue( u"MediaType"_ustr );
1647                 bImport = true;
1648             }
1649             catch (const uno::Exception&)
1650             {
1651                 bImport = false;
1652             }
1653         }
1654     }
1655     if ( bImport )
1656     {
1657         Reader* pRead =  ReadXML;
1658         SwReaderPtr pReader;
1659         std::optional<SwPaM> pPam;
1660         // the SW3IO - Reader need the pam/wrtshell, because only then he
1661         // insert the styles!
1662         if( bUnoCall )
1663         {
1664             SwNodeIndex aIdx( m_xDoc->GetNodes().GetEndOfContent(), -1 );
1665             pPam.emplace( aIdx );
1666             pReader.reset(new SwReader( aMed, rURL, *pPam ));
1667         }
1668         else
1669         {
1670             pReader.reset(new SwReader( aMed, rURL, *m_pWrtShell->GetCursor() ));
1671         }
1672 
1673         pRead->GetReaderOpt().SetTextFormats( rOpt.IsTextFormats() );
1674         pRead->GetReaderOpt().SetFrameFormats( rOpt.IsFrameFormats() );
1675         pRead->GetReaderOpt().SetPageDescs( rOpt.IsPageDescs() );
1676         pRead->GetReaderOpt().SetNumRules( rOpt.IsNumRules() );
1677         pRead->GetReaderOpt().SetMerge( rOpt.IsMerge() );
1678 
1679         if( bUnoCall )
1680         {
1681             UnoActionContext aAction( m_xDoc.get() );
1682             nErr = pReader->Read( *pRead );
1683         }
1684         else
1685         {
1686             m_pWrtShell->StartAllAction();
1687             nErr = pReader->Read( *pRead );
1688             m_pWrtShell->EndAllAction();
1689         }
1690     }
1691 
1692     return nErr;
1693 }
1694 
1695 // Get a client for an embedded object if possible.
GetIPClient(const::svt::EmbeddedObjectRef & xObjRef)1696 SfxInPlaceClient* SwDocShell::GetIPClient( const ::svt::EmbeddedObjectRef& xObjRef )
1697 {
1698     SfxInPlaceClient* pResult = nullptr;
1699 
1700     SwWrtShell* pShell = GetWrtShell();
1701     if ( pShell )
1702     {
1703         pResult = pShell->GetView().FindIPClient( xObjRef.GetObject(), &pShell->GetView().GetEditWin() );
1704         if ( !pResult )
1705             pResult = new SwOleClient( &pShell->GetView(), &pShell->GetView().GetEditWin(), xObjRef );
1706     }
1707 
1708     return pResult;
1709 }
1710 
SwFindDocShell(SfxObjectShellRef & xDocSh,SfxObjectShellLock & xLockRef,std::u16string_view rFileName,const OUString & rPasswd,const OUString & rFilter,sal_Int16 nVersion,SwDocShell * pDestSh,const uno::Reference<task::XInteractionHandler> & xIHandler)1711 int SwFindDocShell( SfxObjectShellRef& xDocSh,
1712                     SfxObjectShellLock& xLockRef,
1713                     std::u16string_view rFileName,
1714                     const OUString& rPasswd,
1715                     const OUString& rFilter,
1716                     sal_Int16 nVersion,
1717                     SwDocShell* pDestSh,
1718                     const uno::Reference<task::XInteractionHandler>& xIHandler )
1719 {
1720     if ( rFileName.empty() )
1721         return 0;
1722 
1723     // 1. Does the file already exist in the list of all Documents?
1724     INetURLObject aTmpObj( rFileName );
1725     aTmpObj.SetMark( u"" );
1726 
1727     // Iterate over the DocShell and get the ones with the name
1728 
1729     SfxObjectShell* pShell = pDestSh;
1730     bool bFirst = nullptr != pShell;
1731 
1732     if( !bFirst )
1733         // No DocShell passed, starting with the first from the DocShell list
1734         pShell = SfxObjectShell::GetFirst( checkSfxObjectShell<SwDocShell> );
1735 
1736     while( pShell )
1737     {
1738         // We want this one
1739         SfxMedium* pMed = pShell->GetMedium();
1740         if( pMed && pMed->GetURLObject() == aTmpObj )
1741         {
1742             const SfxPoolItem* pItem;
1743             if( ( SfxItemState::SET == pMed->GetItemSet().GetItemState(
1744                                             SID_VERSION, false, &pItem ) )
1745                     ? (nVersion == static_cast<const SfxInt16Item*>(pItem)->GetValue())
1746                     : !nVersion )
1747             {
1748                 // Found, thus return
1749                 xDocSh = pShell;
1750                 return 1;
1751             }
1752         }
1753 
1754         if( bFirst )
1755         {
1756             bFirst = false;
1757             pShell = SfxObjectShell::GetFirst( checkSfxObjectShell<SwDocShell> );
1758         }
1759         else
1760             pShell = SfxObjectShell::GetNext( *pShell, checkSfxObjectShell<SwDocShell> );
1761     }
1762 
1763     // 2. Open the file ourselves
1764     std::unique_ptr<SfxMedium> xMed(new SfxMedium( aTmpObj.GetMainURL(
1765                              INetURLObject::DecodeMechanism::NONE ), StreamMode::READ ));
1766     if (xIHandler)
1767         xMed->GetItemSet().Put(SfxUnoAnyItem(SID_INTERACTIONHANDLER, uno::Any(xIHandler)));
1768     if( INetProtocol::File == aTmpObj.GetProtocol() )
1769         xMed->Download(); // Touch the medium (download it)
1770 
1771     std::shared_ptr<const SfxFilter> pSfxFlt;
1772     if (!xMed->GetErrorIgnoreWarning())
1773     {
1774         SfxFilterMatcher aMatcher( rFilter == "writerglobal8"
1775             ? SwGlobalDocShell::Factory().GetFactoryName()
1776             : SwDocShell::Factory().GetFactoryName() );
1777 
1778         // No Filter, so search for it. Else test if the one passed is a valid one
1779         if( !rFilter.isEmpty() )
1780         {
1781             pSfxFlt = aMatcher.GetFilter4FilterName( rFilter );
1782         }
1783 
1784         if( nVersion )
1785             xMed->GetItemSet().Put( SfxInt16Item( SID_VERSION, nVersion ));
1786 
1787         if( !rPasswd.isEmpty() )
1788             xMed->GetItemSet().Put( SfxStringItem( SID_PASSWORD, rPasswd ));
1789 
1790         if( !pSfxFlt )
1791             aMatcher.DetectFilter( *xMed, pSfxFlt );
1792 
1793         if( pSfxFlt )
1794         {
1795             // We cannot do anything without a Filter
1796             xMed->SetFilter( pSfxFlt );
1797 
1798             // If the new shell is created, SfxObjectShellLock should be used to let it be closed later for sure
1799             xLockRef = new SwDocShell(SfxObjectCreateMode::INTERNAL);
1800             xDocSh = static_cast<SfxObjectShell*>(xLockRef);
1801             if (xDocSh->DoLoad(xMed.release()))
1802             {
1803                 return 2;
1804             }
1805         }
1806     }
1807 
1808     return 0;
1809 }
1810 
1811 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1812