sw lok: add Accept/Reject All tracked changes, tdf#101977
[LibreOffice.git] / sw / source / uibase / app / docsh2.cxx
blobd288a67813008af4c3196702eb160c8d2aba8e85
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
22 #include <com/sun/star/drawing/ModuleDispatcher.hpp>
23 #include <com/sun/star/frame/DispatchHelper.hpp>
25 #include <comphelper/processfactory.hxx>
27 #include <edtwin.hxx>
28 #include <hintids.hxx>
29 #include <tools/urlobj.hxx>
30 #include <unotools/tempfile.hxx>
31 #include <unotools/configmgr.hxx>
32 #include <vcl/wrkwin.hxx>
33 #include <vcl/msgbox.hxx>
34 #include <svl/lckbitem.hxx>
35 #include <svl/eitem.hxx>
36 #include <svl/zforlist.hxx>
37 #include <svl/zformat.hxx>
38 #include <unotools/pathoptions.hxx>
39 #include <svtools/transfer.hxx>
40 #include <sfx2/sfxsids.hrc>
41 #include <sfx2/dinfdlg.hxx>
42 #include <sfx2/request.hxx>
43 #include <sfx2/dispatch.hxx>
44 #include <sfx2/new.hxx>
45 #include <sfx2/notebookbar/SfxNotebookBar.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <sfx2/printer.hxx>
48 #include <sfx2/evntconf.hxx>
49 #include <svtools/sfxecode.hxx>
50 #include <sfx2/docfile.hxx>
51 #include <sfx2/docfilt.hxx>
52 #include <svx/dialogs.hrc>
53 #include <svx/svxids.hrc>
54 #include <svx/drawitem.hxx>
55 #include <editeng/svxacorr.hxx>
56 #include <editeng/langitem.hxx>
57 #include <svx/fmshell.hxx>
58 #include <sfx2/linkmgr.hxx>
59 #include <sfx2/classificationhelper.hxx>
60 #include <sfx2/watermarkitem.hxx>
62 #include <svtools/htmlcfg.hxx>
63 #include <svx/ofaitem.hxx>
64 #include <SwSmartTagMgr.hxx>
65 #include <sfx2/app.hxx>
66 #include <basic/sbstar.hxx>
67 #include <basic/basmgr.hxx>
68 #include <sot/storage.hxx>
69 #include <comphelper/classids.hxx>
70 #include <fmtcol.hxx>
71 #include <swevent.hxx>
72 #include <view.hxx>
73 #include <docsh.hxx>
74 #include <docary.hxx>
75 #include <wrtsh.hxx>
76 #include <fldbas.hxx>
77 #include <viewopt.hxx>
78 #include <globdoc.hxx>
79 #include <fldwrap.hxx>
80 #include <redlndlg.hxx>
81 #include <docstyle.hxx>
82 #include <doc.hxx>
83 #include <IDocumentUndoRedo.hxx>
84 #include <IDocumentSettingAccess.hxx>
85 #include <IDocumentDeviceAccess.hxx>
86 #include <IDocumentLinksAdministration.hxx>
87 #include <IDocumentFieldsAccess.hxx>
88 #include <IDocumentStatistics.hxx>
89 #include <IDocumentLayoutAccess.hxx>
90 #include <IDocumentState.hxx>
91 #include <pagedesc.hxx>
92 #include <shellio.hxx>
93 #include <pview.hxx>
94 #include <srcview.hxx>
95 #include <poolfmt.hxx>
96 #include <usrpref.hxx>
97 #include <wdocsh.hxx>
98 #include <unotxdoc.hxx>
99 #include <acmplwrd.hxx>
100 #include <swmodule.hxx>
101 #include <unobaseclass.hxx>
102 #include <swwait.hxx>
103 #include <swcli.hxx>
105 #include <cmdid.h>
106 #include <globals.h>
107 #include <helpids.h>
108 #include <app.hrc>
109 #include <strings.hrc>
110 #include <globals.hrc>
111 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
112 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
113 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
114 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
115 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
116 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
117 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
118 #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
119 #include <com/sun/star/script/vba/VBAEventId.hpp>
120 #include <editeng/acorrcfg.hxx>
121 #include <SwStyleNameMapper.hxx>
122 #include <officecfg/Office/Security.hxx>
124 #include <sfx2/fcontnr.hxx>
125 #include <svx/ClassificationDialog.hxx>
127 #include <swabstdlg.hxx>
128 #include <watermarkdialog.hxx>
130 #include <ndtxt.hxx>
132 #include <memory>
134 using namespace ::com::sun::star::ui::dialogs;
135 using namespace ::com::sun::star::lang;
136 using namespace ::com::sun::star::uno;
137 using namespace ::com::sun::star;
138 using namespace ::sfx2;
140 // create DocInfo (virtual)
141 VclPtr<SfxDocumentInfoDialog> SwDocShell::CreateDocumentInfoDialog(const SfxItemSet &rSet)
143 VclPtr<SfxDocumentInfoDialog> pDlg = VclPtr<SfxDocumentInfoDialog>::Create(nullptr, rSet);
144 //only with statistics, when this document is being shown, not
145 //from within the Doc-Manager
146 SwDocShell* pDocSh = static_cast<SwDocShell*>( SfxObjectShell::Current());
147 if( pDocSh == this )
149 //Not for SourceView.
150 SfxViewShell *pVSh = SfxViewShell::Current();
151 if ( pVSh && dynamic_cast< const SwSrcView *>( pVSh ) == nullptr )
153 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
154 OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
155 pDlg->AddFontTabPage();
156 pDlg->AddTabPage(RID_SW_TP_DOC_STAT, SwResId(STR_DOC_STAT), pFact->GetTabPageCreatorFunc(RID_SW_TP_DOC_STAT), nullptr);
159 return pDlg;
162 void SwDocShell::ToggleLayoutMode(SwView* pView)
164 OSL_ENSURE( pView, "SwDocShell::ToggleLayoutMode, pView is null." );
166 const SwViewOption& rViewOptions = *pView->GetWrtShell().GetViewOptions();
168 //TODO: Should HideWhitespace flag be saved in the document settings?
169 GetDoc()->getIDocumentSettingAccess().set(DocumentSettingId::BROWSE_MODE, rViewOptions.getBrowseMode());
170 UpdateFontList(); // Why is this necessary here?
172 pView->GetViewFrame()->GetBindings().Invalidate(FN_SHADOWCURSOR);
173 if( !GetDoc()->getIDocumentDeviceAccess().getPrinter( false ) )
174 pView->SetPrinter( GetDoc()->getIDocumentDeviceAccess().getPrinter( false ), SfxPrinterChangeFlags::PRINTER | SfxPrinterChangeFlags::JOBSETUP );
175 GetDoc()->CheckDefaultPageFormat();
176 SfxViewFrame *pTmpFrame = SfxViewFrame::GetFirst(this, false);
177 while (pTmpFrame)
179 if( pTmpFrame != pView->GetViewFrame() )
181 pTmpFrame->DoClose();
182 pTmpFrame = SfxViewFrame::GetFirst(this, false);
184 else
185 pTmpFrame = SfxViewFrame::GetNext(*pTmpFrame, this, false);
188 pView->GetWrtShell().InvalidateLayout(true);
190 pView->RecheckBrowseMode();
192 pView->SetNewWindowAllowed(!rViewOptions.getBrowseMode());
195 // update text fields on document properties changes
196 void SwDocShell::DoFlushDocInfo()
198 if (!m_xDoc.get()) return;
200 bool bUnlockView(true);
201 if (m_pWrtShell)
203 bUnlockView = !m_pWrtShell->IsViewLocked();
204 m_pWrtShell->LockView( true ); // lock visible section
205 m_pWrtShell->StartAllAction();
208 m_xDoc->getIDocumentStatistics().DocInfoChgd(IsEnableSetModified());
210 if (m_pWrtShell)
212 m_pWrtShell->EndAllAction();
213 if (bUnlockView)
215 m_pWrtShell->LockView( false );
220 static void lcl_processCompatibleSfxHint( const uno::Reference< script::vba::XVBAEventProcessor >& xVbaEvents, const SfxHint& rHint )
222 using namespace com::sun::star::script::vba::VBAEventId;
223 if ( const SfxEventHint* pSfxEventHint = dynamic_cast<const SfxEventHint*>(&rHint) )
225 uno::Sequence< uno::Any > aArgs;
226 switch( pSfxEventHint->GetEventId() )
228 case SfxEventHintId::CreateDoc:
229 xVbaEvents->processVbaEvent( DOCUMENT_NEW, aArgs );
230 break;
231 case SfxEventHintId::OpenDoc:
232 xVbaEvents->processVbaEvent( DOCUMENT_OPEN, aArgs );
233 break;
234 default: break;
239 // Notification on DocInfo changes
240 void SwDocShell::Notify( SfxBroadcaster&, const SfxHint& rHint )
242 if (!m_xDoc.get())
244 return ;
247 uno::Reference< script::vba::XVBAEventProcessor > const xVbaEvents =
248 m_xDoc->GetVbaEventProcessor();
249 if( xVbaEvents.is() )
250 lcl_processCompatibleSfxHint( xVbaEvents, rHint );
252 sal_uInt16 nAction = 0;
253 auto pEventHint = dynamic_cast<const SfxEventHint*>(&rHint);
254 if( pEventHint && pEventHint->GetEventId() == SfxEventHintId::LoadFinished )
256 // #i38126# - own action id
257 nAction = 3;
259 else
261 // switch for more actions
262 if( rHint.GetId() == SfxHintId::TitleChanged)
264 if( GetMedium() )
265 nAction = 2;
269 if( nAction )
271 bool bUnlockView = true; //initializing prevents warning
272 if (m_pWrtShell)
274 bUnlockView = !m_pWrtShell->IsViewLocked();
275 m_pWrtShell->LockView( true ); //lock visible section
276 m_pWrtShell->StartAllAction();
278 switch( nAction )
280 case 2:
281 m_xDoc->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Filename )->UpdateFields();
282 break;
283 // #i38126# - own action for event LOADFINISHED
284 // in order to avoid a modified document.
285 // #i41679# - Also for the instance of <SwDoc>
286 // it has to be assured, that it's not modified.
287 // Perform the same as for action id 1, but disable <SetModified>.
288 case 3:
290 const bool bResetModified = IsEnableSetModified();
291 if ( bResetModified )
292 EnableSetModified( false );
293 // #i41679#
294 const bool bIsDocModified = m_xDoc->getIDocumentState().IsModified();
295 // TODO: is the ResetModified() below because of only the direct call from DocInfoChgd, or does UpdateFields() set it too?
297 m_xDoc->getIDocumentStatistics().DocInfoChgd(false);
299 // #i41679#
300 if ( !bIsDocModified )
301 m_xDoc->getIDocumentState().ResetModified();
302 if ( bResetModified )
303 EnableSetModified();
305 break;
308 if (m_pWrtShell)
310 m_pWrtShell->EndAllAction();
311 if( bUnlockView )
312 m_pWrtShell->LockView( false );
317 // Notification Close Doc
318 bool SwDocShell::PrepareClose( bool bUI )
320 bool bRet = SfxObjectShell::PrepareClose( bUI );
322 if( bRet )
323 EndListening( *this );
325 if (m_xDoc && IsInPrepareClose())
327 uno::Reference< script::vba::XVBAEventProcessor > const xVbaEvents =
328 m_xDoc->GetVbaEventProcessor();
329 if( xVbaEvents.is() )
331 using namespace com::sun::star::script::vba::VBAEventId;
332 uno::Sequence< uno::Any > aArgs;
333 xVbaEvents->processVbaEvent( DOCUMENT_CLOSE, aArgs );
336 return bRet;
339 void SwDocShell::Execute(SfxRequest& rReq)
341 const SfxItemSet* pArgs = rReq.GetArgs();
342 const SfxPoolItem* pItem;
343 sal_uInt16 nWhich = rReq.GetSlot();
344 bool bDone = false;
345 switch ( nWhich )
347 case SID_AUTO_CORRECT_DLG:
349 SvxSwAutoFormatFlags* pAFlags = &SvxAutoCorrCfg::Get().GetAutoCorrect()->GetSwFlags();
350 SwAutoCompleteWord& rACW = SwDoc::GetAutoCompleteWords();
352 bool bOldLocked = rACW.IsLockWordLstLocked(),
353 bOldAutoCmpltCollectWords = pAFlags->bAutoCmpltCollectWords;
355 rACW.SetLockWordLstLocked( true );
357 editeng::SortedAutoCompleteStrings aTmpLst( rACW.GetWordList() );
358 pAFlags->m_pAutoCompleteList = &aTmpLst;
360 SfxApplication* pApp = SfxGetpApp();
361 SfxRequest aAppReq(SID_AUTO_CORRECT_DLG, SfxCallMode::SYNCHRON, pApp->GetPool());
362 SfxBoolItem aSwOptions( SID_AUTO_CORRECT_DLG, true );
363 aAppReq.AppendItem(aSwOptions);
365 pAFlags->pSmartTagMgr = &SwSmartTagMgr::Get();
367 SfxItemSet aSet( pApp->GetPool(), svl::Items<SID_AUTO_CORRECT_DLG, SID_AUTO_CORRECT_DLG, SID_OPEN_SMARTTAGOPTIONS, SID_OPEN_SMARTTAGOPTIONS>{} );
368 aSet.Put( aSwOptions );
370 const SfxPoolItem* pOpenSmartTagOptionsItem = nullptr;
371 if( pArgs && SfxItemState::SET == pArgs->GetItemState( SID_OPEN_SMARTTAGOPTIONS, false, &pOpenSmartTagOptionsItem ) )
372 aSet.Put( *static_cast<const SfxBoolItem*>(pOpenSmartTagOptionsItem) );
374 SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
375 VclPtr<SfxAbstractTabDialog> pDlg = pFact->CreateAutoCorrTabDialog( &aSet );
376 pDlg->Execute();
377 pDlg.disposeAndClear();
380 rACW.SetLockWordLstLocked( bOldLocked );
382 SwEditShell::SetAutoFormatFlags( pAFlags );
383 rACW.SetMinWordLen( pAFlags->nAutoCmpltWordLen );
384 rACW.SetMaxCount( pAFlags->nAutoCmpltListLen );
385 if (pAFlags->m_pAutoCompleteList) // any changes?
387 rACW.CheckChangedList( aTmpLst );
388 // clear the temp WordList pointer
389 pAFlags->m_pAutoCompleteList = nullptr;
391 // remove all pointer we never delete the strings
392 aTmpLst.clear();
394 if( !bOldAutoCmpltCollectWords && bOldAutoCmpltCollectWords !=
395 pAFlags->bAutoCmpltCollectWords )
397 // call on all Docs the idle formatter to start
398 // the collection of Words
399 for( SwDocShell *pDocSh = static_cast<SwDocShell*>(SfxObjectShell::GetFirst(checkSfxObjectShell<SwDocShell>));
400 pDocSh;
401 pDocSh = static_cast<SwDocShell*>(SfxObjectShell::GetNext( *pDocSh, checkSfxObjectShell<SwDocShell> )) )
403 SwDoc* pTmp = pDocSh->GetDoc();
404 if ( pTmp->getIDocumentLayoutAccess().GetCurrentViewShell() )
405 pTmp->InvalidateAutoCompleteFlag();
409 break;
411 case SID_PRINTPREVIEW:
413 bool bSet = false;
414 bool bFound = false, bOnly = true;
415 SfxViewFrame *pTmpFrame = SfxViewFrame::GetFirst(this);
416 SfxViewShell* pViewShell = SfxViewShell::Current();
417 SwView* pCurrView = dynamic_cast< SwView *> ( pViewShell );
418 bool bCurrent = typeid(SwPagePreview) == typeid( pViewShell );
420 while( pTmpFrame ) // search Preview
422 if( typeid(SwView) == typeid( pTmpFrame->GetViewShell()) )
423 bOnly = false;
424 else if( typeid(SwPagePreview) == typeid( pTmpFrame->GetViewShell()))
426 pTmpFrame->GetFrame().Appear();
427 bFound = true;
429 if( bFound && !bOnly )
430 break;
431 pTmpFrame = SfxViewFrame::GetNext(*pTmpFrame, this);
434 if( pArgs && SfxItemState::SET ==
435 pArgs->GetItemState( SID_PRINTPREVIEW, false, &pItem ))
436 bSet = static_cast<const SfxBoolItem*>(pItem)->GetValue();
437 else
438 bSet = !bCurrent;
440 sal_uInt16 nSlotId = 0;
441 if( bSet && !bFound ) // Nothing found, so create new Preview
442 nSlotId = SID_VIEWSHELL1;
443 else if( bFound && !bSet )
444 nSlotId = bOnly ? SID_VIEWSHELL0 : SID_VIEWSHELL1;
446 if( nSlotId )
448 // PagePreview in the WebDocShell
449 // is found under Id VIEWSHELL2.
450 if( dynamic_cast< const SwWebDocShell *>( this ) != nullptr && SID_VIEWSHELL1 == nSlotId )
451 nSlotId = SID_VIEWSHELL2;
453 if( pCurrView && pCurrView->GetDocShell() == this )
454 pTmpFrame = pCurrView->GetViewFrame();
455 else
456 pTmpFrame = SfxViewFrame::GetFirst( this );
458 if (pTmpFrame)
459 pTmpFrame->GetDispatcher()->Execute( nSlotId, SfxCallMode::ASYNCHRON );
462 rReq.SetReturnValue(SfxBoolItem(SID_PRINTPREVIEW, bSet ));
464 break;
465 case SID_TEMPLATE_LOAD:
467 OUString aFileName;
468 static bool bText = true;
469 static bool bFrame = false;
470 static bool bPage = false;
471 static bool bNum = false;
472 static bool bMerge = false;
473 sal_uInt16 nRet = USHRT_MAX;
475 SfxTemplateFlags nFlags = bFrame ? SfxTemplateFlags::LOAD_FRAME_STYLES : SfxTemplateFlags::NONE;
476 if(bPage)
477 nFlags |= SfxTemplateFlags::LOAD_PAGE_STYLES;
478 if(bNum)
479 nFlags |= SfxTemplateFlags::LOAD_NUM_STYLES;
480 if(nFlags == SfxTemplateFlags::NONE || bText)
481 nFlags |= SfxTemplateFlags::LOAD_TEXT_STYLES;
482 if(bMerge)
483 nFlags |= SfxTemplateFlags::MERGE_STYLES;
485 if ( pArgs )
487 const SfxStringItem* pTemplateItem = rReq.GetArg<SfxStringItem>(SID_TEMPLATE_NAME);
488 if ( pTemplateItem )
490 aFileName = pTemplateItem->GetValue();
491 const SfxInt32Item* pFlagsItem = rReq.GetArg<SfxInt32Item>(SID_TEMPLATE_LOAD);
492 if ( pFlagsItem )
493 nFlags = static_cast<SfxTemplateFlags>((sal_uInt16) pFlagsItem->GetValue());
497 if ( aFileName.isEmpty() )
499 SvtPathOptions aPathOpt;
500 ScopedVclPtr<SfxNewFileDialog> pNewFileDlg(
501 VclPtr<SfxNewFileDialog>::Create(&GetView()->GetViewFrame()->GetWindow(), SfxNewFileDialogMode::LoadTemplate));
502 pNewFileDlg->SetTemplateFlags(nFlags);
504 nRet = pNewFileDlg->Execute();
505 if(RET_TEMPLATE_LOAD == nRet)
507 FileDialogHelper aDlgHelper(TemplateDescription::FILEOPEN_SIMPLE,
508 FileDialogFlags::NONE, &GetView()->GetViewFrame()->GetWindow());
509 uno::Reference < XFilePicker3 > xFP = aDlgHelper.GetFilePicker();
511 xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
513 SfxObjectFactory &rFact = GetFactory();
514 SfxFilterMatcher aMatcher( rFact.GetFactoryName() );
515 SfxFilterMatcherIter aIter( aMatcher );
516 uno::Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
517 std::shared_ptr<const SfxFilter> pFlt = aIter.First();
518 while( pFlt )
520 // --> OD #i117339#
521 if( pFlt && pFlt->IsAllowedAsTemplate() &&
522 ( pFlt->GetUserData() == "CXML" ||
523 pFlt->GetUserData() == "CXMLV" ) )
525 const OUString sWild = pFlt->GetWildcard().getGlob();
526 xFltMgr->appendFilter( pFlt->GetUIName(), sWild );
528 pFlt = aIter.Next();
530 bool bWeb = dynamic_cast< SwWebDocShell *>( this ) != nullptr;
531 std::shared_ptr<const SfxFilter> pOwnFlt =
532 SwDocShell::Factory().GetFilterContainer()->
533 GetFilter4FilterName("writer8");
535 // make sure the default file format is also available
536 if(bWeb)
538 const OUString sWild = pOwnFlt->GetWildcard().getGlob();
539 xFltMgr->appendFilter( pOwnFlt->GetUIName(), sWild );
542 bool bError = false;
543 // catch exception if wrong filter is selected - should not happen anymore
546 xFltMgr->setCurrentFilter( pOwnFlt->GetUIName() );
548 catch (const uno::Exception&)
550 bError = true;
553 if( !bError && ERRCODE_NONE == aDlgHelper.Execute() )
555 aFileName = xFP->getSelectedFiles().getConstArray()[0];
558 else if( RET_OK == nRet)
560 aFileName = pNewFileDlg->GetTemplateFileName();
563 nFlags = pNewFileDlg->GetTemplateFlags();
564 rReq.AppendItem( SfxStringItem( SID_TEMPLATE_NAME, aFileName ) );
565 rReq.AppendItem( SfxInt32Item( SID_TEMPLATE_LOAD, (long) nFlags ) );
568 if( !aFileName.isEmpty() )
570 SwgReaderOption aOpt;
571 aOpt.SetTextFormats( bText = bool(nFlags & SfxTemplateFlags::LOAD_TEXT_STYLES ));
572 aOpt.SetFrameFormats( bFrame = bool(nFlags & SfxTemplateFlags::LOAD_FRAME_STYLES));
573 aOpt.SetPageDescs( bPage = bool(nFlags & SfxTemplateFlags::LOAD_PAGE_STYLES ));
574 aOpt.SetNumRules( bNum = bool(nFlags & SfxTemplateFlags::LOAD_NUM_STYLES ));
575 //different meaning between SFX_MERGE_STYLES and aOpt.SetMerge!
576 bMerge = bool(nFlags & SfxTemplateFlags::MERGE_STYLES);
577 aOpt.SetMerge( !bMerge );
579 SetError(LoadStylesFromFile(aFileName, aOpt, false));
580 if ( !GetError() )
581 rReq.Done();
584 break;
585 case SID_SOURCEVIEW:
587 SfxViewShell* pViewShell = GetView()
588 ? static_cast<SfxViewShell*>(GetView())
589 : SfxViewShell::Current();
590 SfxViewFrame* pViewFrame = pViewShell->GetViewFrame();
591 SwSrcView* pSrcView = dynamic_cast< SwSrcView *>( pViewShell );
592 if(!pSrcView)
594 // 3 possible state:
595 // 1 - file unsaved -> save as HTML
596 // 2 - file modified and HTML filter active -> save
597 // 3 - file saved in non-HTML -> QueryBox to save as HTML
598 std::shared_ptr<const SfxFilter> pHtmlFlt =
599 SwIoSystem::GetFilterOfFormat(
600 "HTML",
601 SwWebDocShell::Factory().GetFilterContainer() );
602 bool bLocalHasName = HasName();
603 if(bLocalHasName)
605 //check for filter type
606 std::shared_ptr<const SfxFilter> pFlt = GetMedium()->GetFilter();
607 if(!pFlt || pFlt->GetUserData() != pHtmlFlt->GetUserData())
609 ScopedVclPtrInstance<MessageDialog> aQuery(&pViewFrame->GetWindow(),
610 "SaveAsHTMLDialog", "modules/swriter/ui/saveashtmldialog.ui");
612 if(RET_YES == aQuery->Execute())
613 bLocalHasName = false;
614 else
615 break;
618 if(!bLocalHasName)
620 FileDialogHelper aDlgHelper(TemplateDescription::FILESAVE_AUTOEXTENSION,
621 FileDialogFlags::NONE,
622 &GetView()->GetViewFrame()->GetWindow());
623 aDlgHelper.AddFilter( pHtmlFlt->GetFilterName(), pHtmlFlt->GetDefaultExtension() );
624 aDlgHelper.SetCurrentFilter( pHtmlFlt->GetFilterName() );
625 if( ERRCODE_NONE != aDlgHelper.Execute())
627 break;
629 OUString sPath = aDlgHelper.GetPath();
630 SfxStringItem aName(SID_FILE_NAME, sPath);
631 SfxStringItem aFilter(SID_FILTER_NAME, pHtmlFlt->GetName());
632 const SfxBoolItem* pBool = static_cast<const SfxBoolItem*>(
633 pViewFrame->GetDispatcher()->ExecuteList(
634 SID_SAVEASDOC, SfxCallMode::SYNCHRON,
635 { &aName, &aFilter }));
636 if(!pBool || !pBool->GetValue())
637 break;
641 OSL_ENSURE(dynamic_cast<SwWebDocShell*>(this),
642 "SourceView only in WebDocShell");
644 // the SourceView is not the 1 for SwWebDocShell
645 sal_uInt16 nSlot = SID_VIEWSHELL1;
646 bool bSetModified = false;
647 VclPtr<SfxPrinter> pSavePrinter;
648 if( nullptr != pSrcView)
650 SfxPrinter* pTemp = GetDoc()->getIDocumentDeviceAccess().getPrinter( false );
651 if(pTemp)
652 pSavePrinter = VclPtr<SfxPrinter>::Create(*pTemp);
653 bSetModified = IsModified() || pSrcView->IsModified();
654 if(pSrcView->IsModified()||pSrcView->HasSourceSaved())
656 utl::TempFile aTempFile;
657 aTempFile.EnableKillingFile();
658 pSrcView->SaveContent(aTempFile.GetURL());
659 bDone = true;
660 SvxMacro aMac(aEmptyOUStr, aEmptyOUStr, STARBASIC);
661 SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::OPENDOC ), aMac, this);
662 SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::PREPARECLOSEDOC ), aMac, this);
663 SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::ACTIVATEDOC ), aMac, this);
664 SfxEventConfiguration::ConfigureEvent(GlobalEventConfig::GetEventName( GlobalEventId::DEACTIVATEDOC ), aMac, this);
665 ReloadFromHtml(aTempFile.GetURL(), pSrcView);
666 nSlot = 0;
668 else
670 nSlot = SID_VIEWSHELL0;
673 if(nSlot)
674 pViewFrame->GetDispatcher()->Execute(nSlot, SfxCallMode::SYNCHRON);
675 if(bSetModified)
676 GetDoc()->getIDocumentState().SetModified();
677 if(pSavePrinter)
679 GetDoc()->getIDocumentDeviceAccess().setPrinter( pSavePrinter, true, true);
680 //pSavePrinter must not be deleted again
682 pViewFrame->GetBindings().SetState(SfxBoolItem(SID_SOURCEVIEW, nSlot == SID_VIEWSHELL2));
683 pViewFrame->GetBindings().Invalidate( SID_NEWWINDOW );
684 pViewFrame->GetBindings().Invalidate( SID_BROWSER_MODE );
685 pViewFrame->GetBindings().Invalidate( FN_PRINT_LAYOUT );
687 break;
688 case SID_GET_COLORLIST:
690 const SvxColorListItem* pColItem = static_cast<const SvxColorListItem*>(GetItem(SID_COLOR_TABLE));
691 XColorListRef pList = pColItem->GetColorList();
692 rReq.SetReturnValue(OfaRefItem<XColorList>(SID_GET_COLORLIST, pList));
694 break;
695 case FN_ABSTRACT_STARIMPRESS:
696 case FN_ABSTRACT_NEWDOC:
698 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
699 OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
701 ScopedVclPtr<AbstractSwInsertAbstractDlg> pDlg(pFact->CreateSwInsertAbstractDlg());
702 OSL_ENSURE(pDlg, "Dialog creation failed!");
703 if(RET_OK == pDlg->Execute())
705 sal_uInt8 nLevel = pDlg->GetLevel();
706 sal_uInt8 nPara = pDlg->GetPara();
707 SwDoc* pSmryDoc = new SwDoc();
708 SfxObjectShellLock xDocSh( new SwDocShell( pSmryDoc, SfxObjectCreateMode::STANDARD));
709 xDocSh->DoInitNew();
711 bool bImpress = FN_ABSTRACT_STARIMPRESS == nWhich;
712 m_xDoc->Summary( pSmryDoc, nLevel, nPara, bImpress );
713 if( bImpress )
715 WriterRef xWrt;
716 // mba: looks as if relative URLs don't make sense here
717 ::GetRTFWriter( aEmptyOUStr, OUString(), xWrt );
718 SvMemoryStream *pStrm = new SvMemoryStream();
719 pStrm->SetBufferSize( 16348 );
720 SwWriter aWrt( *pStrm, *pSmryDoc );
721 ErrCode eErr = aWrt.Write( xWrt );
722 if( !eErr.IgnoreWarning() )
724 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
725 uno::Reference< frame::XDispatchProvider > xProv = drawing::ModuleDispatcher::create( xContext );
727 uno::Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create(xContext) );
728 pStrm->Seek( STREAM_SEEK_TO_END );
729 pStrm->WriteChar( '\0' );
730 pStrm->Seek( STREAM_SEEK_TO_BEGIN );
732 // Transfer ownership of stream to a lockbytes object
733 SvLockBytes aLockBytes( pStrm, true );
734 SvLockBytesStat aStat;
735 if ( aLockBytes.Stat( &aStat, SVSTATFLAG_DEFAULT ) == ERRCODE_NONE )
737 sal_uInt32 nLen = aStat.nSize;
738 std::size_t nRead = 0;
739 uno::Sequence< sal_Int8 > aSeq( nLen );
740 aLockBytes.ReadAt( 0, aSeq.getArray(), nLen, &nRead );
742 uno::Sequence< beans::PropertyValue > aArgs(1);
743 aArgs[0].Name = "RtfOutline";
744 aArgs[0].Value <<= aSeq;
745 xHelper->executeDispatch( xProv, "SendOutlineToImpress", OUString(), 0, aArgs );
748 else
749 ErrorHandler::HandleError( eErr );
751 else
753 // Create new document
754 SfxViewFrame *pFrame = SfxViewFrame::LoadDocument( *xDocSh, SFX_INTERFACE_NONE );
755 SwView *pCurrView = static_cast<SwView*>( pFrame->GetViewShell());
757 // Set document's title
758 OUString aTmp( SwResId(STR_ABSTRACT_TITLE) );
759 aTmp += GetTitle();
760 xDocSh->SetTitle( aTmp );
761 pCurrView->GetWrtShell().SetNewDoc();
762 pFrame->Show();
763 pSmryDoc->getIDocumentState().SetModified();
768 break;
769 case FN_OUTLINE_TO_CLIPBOARD:
770 case FN_OUTLINE_TO_IMPRESS:
772 bool bEnable = IsEnableSetModified();
773 EnableSetModified( false );
774 WriterRef xWrt;
775 // mba: looks as if relative URLs don't make sense here
776 ::GetRTFWriter( OUString('O'), OUString(), xWrt );
777 SvMemoryStream *pStrm = new SvMemoryStream();
778 pStrm->SetBufferSize( 16348 );
779 SwWriter aWrt( *pStrm, *GetDoc() );
780 ErrCode eErr = aWrt.Write( xWrt );
781 EnableSetModified( bEnable );
782 if( !eErr.IgnoreWarning() )
784 pStrm->Seek( STREAM_SEEK_TO_END );
785 pStrm->WriteChar( '\0' );
786 pStrm->Seek( STREAM_SEEK_TO_BEGIN );
787 if ( nWhich == FN_OUTLINE_TO_IMPRESS )
789 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
790 uno::Reference< frame::XDispatchProvider > xProv = drawing::ModuleDispatcher::create( xContext );
792 uno::Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create(xContext) );
793 pStrm->Seek( STREAM_SEEK_TO_END );
794 pStrm->WriteChar( '\0' );
795 pStrm->Seek( STREAM_SEEK_TO_BEGIN );
797 // Transfer ownership of stream to a lockbytes object
798 SvLockBytes aLockBytes( pStrm, true );
799 SvLockBytesStat aStat;
800 if ( aLockBytes.Stat( &aStat, SVSTATFLAG_DEFAULT ) == ERRCODE_NONE )
802 sal_uInt32 nLen = aStat.nSize;
803 std::size_t nRead = 0;
804 uno::Sequence< sal_Int8 > aSeq( nLen );
805 aLockBytes.ReadAt( 0, aSeq.getArray(), nLen, &nRead );
807 uno::Sequence< beans::PropertyValue > aArgs(1);
808 aArgs[0].Name = "RtfOutline";
809 aArgs[0].Value <<= aSeq;
810 xHelper->executeDispatch( xProv, "SendOutlineToImpress", OUString(), 0, aArgs );
813 else
815 rtl::Reference<TransferDataContainer> pClipCntnr = new TransferDataContainer;
817 pClipCntnr->CopyAnyData( SotClipboardFormatId::RTF, static_cast<sal_Char const *>(
818 pStrm->GetData()), pStrm->GetEndOfData() );
819 pClipCntnr->CopyToClipboard(
820 GetView()? &GetView()->GetEditWin() : nullptr );
821 delete pStrm;
824 else
825 ErrorHandler::HandleError( eErr );
827 break;
828 case SID_SPELLCHECKER_CHANGED:
829 //! false, true, true is on the save side but a probably overdone
830 SwModule::CheckSpellChanges(false, true, true, false );
831 break;
833 case SID_MAIL_PREPAREEXPORT:
835 //pWrtShell is not set in page preview
836 if (m_pWrtShell)
837 m_pWrtShell->StartAllAction();
838 m_xDoc->getIDocumentFieldsAccess().UpdateFields( false );
839 m_xDoc->getIDocumentLinksAdministration().EmbedAllLinks();
840 m_IsRemovedInvisibleContent
841 = officecfg::Office::Security::HiddenContent::RemoveHiddenContent::get();
842 if (m_IsRemovedInvisibleContent)
843 m_xDoc->RemoveInvisibleContent();
844 if (m_pWrtShell)
845 m_pWrtShell->EndAllAction();
847 break;
849 case SID_MAIL_EXPORT_FINISHED:
851 if (m_pWrtShell)
852 m_pWrtShell->StartAllAction();
853 //try to undo the removal of invisible content
854 if (m_IsRemovedInvisibleContent)
855 m_xDoc->RestoreInvisibleContent();
856 if (m_pWrtShell)
857 m_pWrtShell->EndAllAction();
859 break;
860 case FN_NEW_HTML_DOC:
861 case FN_NEW_GLOBAL_DOC:
863 bDone = false;
864 bool bCreateHtml = FN_NEW_HTML_DOC == nWhich;
866 bool bCreateByOutlineLevel = false;
867 sal_Int32 nTemplateOutlineLevel = 0;
869 OUString aFileName, aTemplateName;
870 if( pArgs && SfxItemState::SET == pArgs->GetItemState( nWhich, false, &pItem ) )
872 aFileName = static_cast<const SfxStringItem*>(pItem)->GetValue();
873 const SfxStringItem* pTemplItem = SfxItemSet::GetItem<SfxStringItem>(pArgs, SID_TEMPLATE_NAME, false);
874 if ( pTemplItem )
875 aTemplateName = pTemplItem->GetValue();
877 if ( aFileName.isEmpty() )
879 bool bError = false;
881 FileDialogHelper aDlgHelper(TemplateDescription::FILESAVE_AUTOEXTENSION_TEMPLATE, FileDialogFlags::NONE,
882 &GetView()->GetViewFrame()->GetWindow());
884 const sal_Int16 nControlIds[] = {
885 CommonFilePickerElementIds::PUSHBUTTON_OK,
886 CommonFilePickerElementIds::PUSHBUTTON_CANCEL,
887 CommonFilePickerElementIds::LISTBOX_FILTER,
888 CommonFilePickerElementIds::CONTROL_FILEVIEW,
889 CommonFilePickerElementIds::EDIT_FILEURL,
890 ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION,
891 ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
895 if (bCreateHtml)
897 const char* aHTMLHelpIds[] =
899 HID_SEND_HTML_CTRL_PUSHBUTTON_OK,
900 HID_SEND_HTML_CTRL_PUSHBUTTON_CANCEL,
901 HID_SEND_HTML_CTRL_LISTBOX_FILTER,
902 HID_SEND_HTML_CTRL_CONTROL_FILEVIEW,
903 HID_SEND_HTML_CTRL_EDIT_FILEURL,
904 HID_SEND_HTML_CTRL_CHECKBOX_AUTOEXTENSION,
905 HID_SEND_HTML_CTRL_LISTBOX_TEMPLATE,
908 aDlgHelper.SetControlHelpIds( nControlIds, aHTMLHelpIds );
910 else
912 const char* aMasterHelpIds[] =
914 HID_SEND_MASTER_CTRL_PUSHBUTTON_OK,
915 HID_SEND_MASTER_CTRL_PUSHBUTTON_CANCEL,
916 HID_SEND_MASTER_CTRL_LISTBOX_FILTER,
917 HID_SEND_MASTER_CTRL_CONTROL_FILEVIEW,
918 HID_SEND_MASTER_CTRL_EDIT_FILEURL,
919 HID_SEND_MASTER_CTRL_CHECKBOX_AUTOEXTENSION,
920 HID_SEND_MASTER_CTRL_LISTBOX_TEMPLATE,
923 aDlgHelper.SetControlHelpIds( nControlIds, aMasterHelpIds );
925 uno::Reference < XFilePicker3 > xFP = aDlgHelper.GetFilePicker();
927 std::shared_ptr<const SfxFilter> pFlt;
928 const char* pStrId;
930 if( bCreateHtml )
932 // for HTML there is only one filter!!
933 pFlt = SwIoSystem::GetFilterOfFormat(
934 "HTML",
935 SwWebDocShell::Factory().GetFilterContainer() );
936 pStrId = STR_LOAD_HTML_DOC;
938 else
940 // for Global-documents we now only offer the current one.
941 pFlt = SwGlobalDocShell::Factory().GetFilterContainer()->
942 GetFilter4Extension( "odm" );
943 pStrId = STR_LOAD_GLOBAL_DOC;
946 if( pFlt )
948 uno::Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
949 const OUString sWild = pFlt->GetWildcard().getGlob();
950 xFltMgr->appendFilter( pFlt->GetUIName(), sWild );
953 xFltMgr->setCurrentFilter( pFlt->GetUIName() ) ;
955 catch (const uno::Exception&)
957 bError = true;
960 if(!bError)
962 uno::Reference<XFilePickerControlAccess> xCtrlAcc(xFP, UNO_QUERY);
964 bool bOutline[MAXLEVEL] = {false};
965 const SwOutlineNodes& rOutlNds = m_xDoc->GetNodes().GetOutLineNds();
966 if( !rOutlNds.empty() )
968 for( size_t n = 0; n < rOutlNds.size(); ++n )
970 const int nLevel = rOutlNds[n]->GetTextNode()->GetAttrOutlineLevel();
971 if( nLevel > 0 && ! bOutline[nLevel-1] )
973 bOutline[nLevel-1] = true;
978 const sal_uInt16 nStyleCount = m_xDoc->GetTextFormatColls()->size();
979 Sequence<OUString> aListBoxEntries( MAXLEVEL + nStyleCount);
980 OUString* pEntries = aListBoxEntries.getArray();
981 sal_Int32 nIdx = 0 ;
983 OUString sOutline( SwResId(STR_FDLG_OUTLINE_LEVEL) );
984 for( sal_uInt16 i = 0; i < MAXLEVEL; ++i )
986 if( bOutline[i] )
987 pEntries[nIdx++] = sOutline + OUString::number( i+1 );
990 OUString sStyle( SwResId(STR_FDLG_STYLE) );
991 for(sal_uInt16 i = 0; i < nStyleCount; ++i)
993 SwTextFormatColl &rTextColl = *(*m_xDoc->GetTextFormatColls())[ i ];
994 if( !rTextColl.IsDefault() && rTextColl.IsAtDocNodeSet() )
996 pEntries[nIdx++] = sStyle + rTextColl.GetName();
1000 aListBoxEntries.realloc(nIdx);
1001 sal_Int16 nSelect = 0;
1005 Any aTemplates(&aListBoxEntries, cppu::UnoType<decltype(aListBoxEntries)>::get());
1007 xCtrlAcc->setValue( ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1008 ListboxControlActions::ADD_ITEMS , aTemplates );
1009 Any aSelectPos(&nSelect, cppu::UnoType<decltype(nSelect)>::get());
1010 xCtrlAcc->setValue( ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1011 ListboxControlActions::SET_SELECT_ITEM, aSelectPos );
1012 xCtrlAcc->setLabel( ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1013 SwResId( STR_FDLG_TEMPLATE_NAME ));
1015 catch (const Exception&)
1017 OSL_FAIL("control access failed");
1020 xFP->setTitle(SwResId(pStrId));
1021 SvtPathOptions aPathOpt;
1022 xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
1023 if( ERRCODE_NONE == aDlgHelper.Execute())
1025 aFileName = xFP->getSelectedFiles().getConstArray()[0];
1026 Any aTemplateValue = xCtrlAcc->getValue(
1027 ExtendedFilePickerElementIds::LISTBOX_TEMPLATE,
1028 ListboxControlActions::GET_SELECTED_ITEM );
1029 OUString sTmpl;
1030 aTemplateValue >>= sTmpl;
1032 sal_Int32 nColonPos = sTmpl.indexOf( ':' );
1033 OUString sPrefix = sTmpl.copy( 0, nColonPos );
1034 if ( sPrefix == "Style" )
1036 aTemplateName = sTmpl.copy( 7 ); //get string behind "Style: "
1038 else if ( sPrefix == "Outline" )
1040 nTemplateOutlineLevel = ( sTmpl.copy( 15 )).toInt32(); //get string behind "Outline: Leve ";
1041 bCreateByOutlineLevel = true;
1044 if ( !aFileName.isEmpty() )
1046 rReq.AppendItem( SfxStringItem( nWhich, aFileName ) );
1047 if( !aTemplateName.isEmpty() )
1048 rReq.AppendItem( SfxStringItem( SID_TEMPLATE_NAME, aTemplateName ) );
1054 if( !aFileName.isEmpty() )
1056 if( PrepareClose( false ) )
1058 SwWait aWait( *this, true );
1060 if ( bCreateByOutlineLevel )
1062 bDone = bCreateHtml
1063 ? m_xDoc->GenerateHTMLDoc( aFileName, nTemplateOutlineLevel )
1064 : m_xDoc->GenerateGlobalDoc( aFileName, nTemplateOutlineLevel );
1066 else
1068 const SwTextFormatColl* pSplitColl = nullptr;
1069 if ( !aTemplateName.isEmpty() )
1070 pSplitColl = m_xDoc->FindTextFormatCollByName(aTemplateName);
1071 bDone = bCreateHtml
1072 ? m_xDoc->GenerateHTMLDoc( aFileName, pSplitColl )
1073 : m_xDoc->GenerateGlobalDoc( aFileName, pSplitColl );
1075 if( bDone )
1077 SfxStringItem aName( SID_FILE_NAME, aFileName );
1078 SfxStringItem aReferer( SID_REFERER, aEmptyOUStr );
1079 SfxViewShell* pViewShell = SfxViewShell::GetFirst();
1080 while(pViewShell)
1082 //search for the view that created the call
1083 if(pViewShell->GetObjectShell() == this && pViewShell->GetDispatcher())
1085 std::unique_ptr<SfxFrameItem> pFrameItem(new SfxFrameItem( SID_DOCFRAME,
1086 pViewShell->GetViewFrame() ));
1087 SfxDispatcher* pDispatch = pViewShell->GetDispatcher();
1088 pDispatch->ExecuteList(SID_OPENDOC,
1089 SfxCallMode::ASYNCHRON,
1090 { &aName, &aReferer, pFrameItem.get() });
1091 break;
1093 pViewShell = SfxViewShell::GetNext(*pViewShell);
1097 if( !bDone && !rReq.IsAPI() )
1099 ScopedVclPtrInstance<InfoBox>(nullptr, SwResId( STR_CANTCREATE))->Execute();
1103 rReq.SetReturnValue(SfxBoolItem( nWhich, bDone ));
1104 if (bDone)
1105 rReq.Done();
1106 else
1107 rReq.Ignore();
1108 break;
1110 case SID_ATTR_YEAR2000:
1111 if ( pArgs && SfxItemState::SET == pArgs->GetItemState( nWhich , false, &pItem ))
1113 OSL_ENSURE(dynamic_cast< const SfxUInt16Item *>( pItem ) != nullptr, "wrong Item");
1114 sal_uInt16 nYear2K = static_cast<const SfxUInt16Item*>(pItem)->GetValue();
1115 // iterate over Views and put the State to FormShells
1117 SfxViewFrame* pVFrame = SfxViewFrame::GetFirst( this );
1118 SfxViewShell* pViewShell = pVFrame ? pVFrame->GetViewShell() : nullptr;
1119 SwView* pCurrView = dynamic_cast< SwView* >( pViewShell );
1120 while(pCurrView)
1122 FmFormShell* pFormShell = pCurrView->GetFormShell();
1123 if(pFormShell)
1124 pFormShell->SetY2KState(nYear2K);
1125 pVFrame = SfxViewFrame::GetNext( *pVFrame, this );
1126 pViewShell = pVFrame ? pVFrame->GetViewShell() : nullptr;
1127 pCurrView = dynamic_cast<SwView*>( pViewShell );
1129 m_xDoc->GetNumberFormatter()->SetYear2000(nYear2K);
1131 break;
1132 case FN_OPEN_FILE:
1134 SfxViewShell* pViewShell = GetView();
1135 if (!pViewShell)
1136 pViewShell = SfxViewShell::Current();
1138 if (!pViewShell)
1139 // Ok. I did my best.
1140 break;
1142 SfxStringItem aApp(SID_DOC_SERVICE, OUString("com.sun.star.text.TextDocument"));
1143 SfxStringItem aTarget(SID_TARGETNAME, OUString("_blank"));
1144 pViewShell->GetDispatcher()->ExecuteList(SID_OPENDOC,
1145 SfxCallMode::API|SfxCallMode::SYNCHRON,
1146 { &aApp, &aTarget });
1148 break;
1149 case SID_CLASSIFICATION_APPLY:
1151 if (pArgs && pArgs->GetItemState(nWhich, false, &pItem) == SfxItemState::SET)
1153 SwWrtShell* pSh = GetWrtShell();
1154 const OUString& rValue = static_cast<const SfxStringItem*>(pItem)->GetValue();
1155 auto eType = SfxClassificationPolicyType::IntellectualProperty;
1156 if (pArgs->GetItemState(SID_TYPE_NAME, false, &pItem) == SfxItemState::SET)
1158 const OUString& rType = static_cast<const SfxStringItem*>(pItem)->GetValue();
1159 eType = SfxClassificationHelper::stringToPolicyType(rType);
1161 pSh->SetClassification(rValue, eType);
1163 else
1164 SAL_WARN("sw.ui", "missing parameter for SID_CLASSIFICATION_APPLY");
1166 break;
1167 case SID_CLASSIFICATION_DIALOG:
1169 ScopedVclPtr<svx::ClassificationDialog> pDialog(VclPtr<svx::ClassificationDialog>::Create(nullptr, false));
1171 SwWrtShell* pShell = GetWrtShell();
1172 std::vector<svx::ClassificationResult> aInput = pShell->CollectAdvancedClassification();
1173 pDialog->setupValues(aInput);
1175 if (RET_OK == pDialog->Execute())
1176 pShell->ApplyAdvancedClassification(pDialog->getResult());
1178 pDialog.disposeAndClear();
1180 break;
1181 case SID_PARAGRAPH_SIGN_CLASSIFY_DLG:
1183 SwWrtShell* pShell = GetWrtShell();
1184 ScopedVclPtr<svx::ClassificationDialog> pDialog(VclPtr<svx::ClassificationDialog>::Create(nullptr, true, [pShell]()
1186 pShell->SignParagraph();
1187 }));
1189 std::vector<svx::ClassificationResult> aInput = pShell->CollectParagraphClassification();
1190 pDialog->setupValues(aInput);
1192 if (RET_OK == pDialog->Execute())
1193 pShell->ApplyParagraphClassification(pDialog->getResult());
1195 pDialog.disposeAndClear();
1197 break;
1198 case SID_WATERMARK:
1200 SwWrtShell* pSh = GetWrtShell();
1201 if ( pSh )
1203 if (pArgs && pArgs->GetItemState( SID_WATERMARK, false, &pItem ) == SfxItemState::SET)
1205 SfxWatermarkItem aItem;
1206 aItem.SetText( static_cast<const SfxStringItem*>( pItem )->GetValue() );
1208 if ( pArgs->GetItemState( SID_WATERMARK_FONT, false, &pItem ) == SfxItemState::SET )
1209 aItem.SetFont( static_cast<const SfxStringItem*>( pItem )->GetValue() );
1210 if ( pArgs->GetItemState( SID_WATERMARK_ANGLE, false, &pItem ) == SfxItemState::SET )
1211 aItem.SetAngle( static_cast<const SfxInt16Item*>( pItem )->GetValue() );
1212 if ( pArgs->GetItemState( SID_WATERMARK_TRANSPARENCY, false, &pItem ) == SfxItemState::SET )
1213 aItem.SetTransparency( static_cast<const SfxInt16Item*>( pItem )->GetValue() );
1214 if ( pArgs->GetItemState( SID_WATERMARK_COLOR, false, &pItem ) == SfxItemState::SET )
1215 aItem.SetColor( static_cast<const SfxUInt32Item*>( pItem )->GetValue() );
1217 pSh->SetWatermark( aItem );
1219 else
1221 SfxViewShell* pViewShell = GetView()? GetView(): SfxViewShell::Current();
1222 SfxBindings& rBindings( pViewShell->GetViewFrame()->GetBindings() );
1223 ScopedVclPtr<SwWatermarkDialog> pDlg( VclPtr<SwWatermarkDialog>::Create( nullptr, rBindings ) );
1224 pDlg->Execute();
1225 pDlg.disposeAndClear();
1229 break;
1230 case SID_NOTEBOOKBAR:
1232 const SfxStringItem* pFile = rReq.GetArg<SfxStringItem>( SID_NOTEBOOKBAR );
1233 SfxViewShell* pViewShell = GetView()? GetView(): SfxViewShell::Current();
1234 SfxBindings& rBindings( pViewShell->GetViewFrame()->GetBindings() );
1236 if ( SfxNotebookBar::IsActive() )
1237 sfx2::SfxNotebookBar::ExecMethod( rBindings, pFile ? pFile->GetValue() : "" );
1238 else
1240 sfx2::SfxNotebookBar::CloseMethod( rBindings );
1243 break;
1244 case FN_REDLINE_ACCEPT_ALL:
1245 case FN_REDLINE_REJECT_ALL:
1247 IDocumentRedlineAccess& rRedlineAccess = GetDoc()->getIDocumentRedlineAccess();
1248 SwWrtShell *pWrtShell = dynamic_cast<SwWrtShell*>(GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell());
1250 if (rRedlineAccess.GetRedlineTable().empty())
1252 break;
1255 if (pWrtShell)
1257 pWrtShell->StartAllAction();
1260 rRedlineAccess.AcceptAllRedline(nWhich == FN_REDLINE_ACCEPT_ALL);
1262 if (pWrtShell)
1264 pWrtShell->EndAllAction();
1267 Broadcast(SfxHint(SfxHintId::RedlineChanged));
1268 rReq.Done();
1270 break;
1272 default: OSL_FAIL("wrong Dispatcher");
1276 #if defined(_WIN32)
1277 bool SwDocShell::DdeGetData( const OUString& rItem, const OUString& rMimeType,
1278 uno::Any & rValue )
1280 return m_xDoc->getIDocumentLinksAdministration().GetData( rItem, rMimeType, rValue );
1283 bool SwDocShell::DdeSetData( const OUString& rItem, const OUString& rMimeType,
1284 const uno::Any & rValue )
1286 return m_xDoc->getIDocumentLinksAdministration().SetData( rItem, rMimeType, rValue );
1289 #endif
1291 ::sfx2::SvLinkSource* SwDocShell::DdeCreateLinkSource( const OUString& rItem )
1293 return m_xDoc->getIDocumentLinksAdministration().CreateLinkSource( rItem );
1296 void SwDocShell::ReconnectDdeLink(SfxObjectShell& rServer)
1298 if (m_xDoc)
1300 ::sfx2::LinkManager& rLinkManager = m_xDoc->getIDocumentLinksAdministration().GetLinkManager();
1301 rLinkManager.ReconnectDdeLink(rServer);
1305 void SwDocShell::FillClass( SvGlobalName * pClassName,
1306 SotClipboardFormatId * pClipFormat,
1307 OUString * /*pAppName*/,
1308 OUString * pLongUserName,
1309 OUString * pUserName,
1310 sal_Int32 nVersion,
1311 bool bTemplate /* = false */) const
1313 if (nVersion == SOFFICE_FILEFORMAT_60)
1315 *pClassName = SvGlobalName( SO3_SW_CLASSID_60 );
1316 *pClipFormat = SotClipboardFormatId::STARWRITER_60;
1317 *pLongUserName = SwResId(STR_WRITER_DOCUMENT_FULLTYPE);
1319 else if (nVersion == SOFFICE_FILEFORMAT_8)
1321 *pClassName = SvGlobalName( SO3_SW_CLASSID_60 );
1322 *pClipFormat = bTemplate ? SotClipboardFormatId::STARWRITER_8_TEMPLATE : SotClipboardFormatId::STARWRITER_8;
1323 *pLongUserName = SwResId(STR_WRITER_DOCUMENT_FULLTYPE);
1325 // #FIXME check with new Event handling
1326 #if 0
1327 uno::Reference< document::XVbaEventsHelper > xVbaEventsHelper = m_xDoc->GetVbaEventsHelper();
1328 if( xVbaEventsHelper.is() )
1329 lcl_processCompatibleSfxHint( xVbaEventsHelper, rHint );
1330 #endif
1332 *pUserName = SwResId(STR_HUMAN_SWDOC_NAME);
1335 void SwDocShell::SetModified( bool bSet )
1337 if (utl::ConfigManager::IsAvoidConfig())
1338 return;
1339 SfxObjectShell::SetModified( bSet );
1340 if( IsEnableSetModified())
1342 if (!m_xDoc->getIDocumentState().IsInCallModified())
1344 EnableSetModified( false );
1345 if( bSet )
1347 bool const bOld = m_xDoc->getIDocumentState().IsModified();
1348 m_xDoc->getIDocumentState().SetModified();
1349 if( !bOld )
1351 m_xDoc->GetIDocumentUndoRedo().SetUndoNoResetModified();
1354 else
1355 m_xDoc->getIDocumentState().ResetModified();
1357 EnableSetModified();
1360 UpdateChildWindows();
1361 Broadcast(SfxHint(SfxHintId::DocChanged));
1365 void SwDocShell::UpdateChildWindows()
1367 // if necessary newly initialize Fielddlg (i.e. for TYP_SETVAR)
1368 if(!GetView())
1369 return;
1370 SfxViewFrame* pVFrame = GetView()->GetViewFrame();
1371 SwFieldDlgWrapper *pWrp = static_cast<SwFieldDlgWrapper*>(pVFrame->
1372 GetChildWindow( SwFieldDlgWrapper::GetChildWindowId() ));
1373 if( pWrp )
1374 pWrp->ReInitDlg( this );
1376 // if necessary newly initialize RedlineDlg
1377 SwRedlineAcceptChild *pRed = static_cast<SwRedlineAcceptChild*>(pVFrame->
1378 GetChildWindow( SwRedlineAcceptChild::GetChildWindowId() ));
1379 if( pRed )
1380 pRed->ReInitDlg( this );
1383 // #i48748#
1384 class SwReloadFromHtmlReader : public SwReader
1386 public:
1387 SwReloadFromHtmlReader( SfxMedium& _rTmpMedium,
1388 const OUString& _rFilename,
1389 SwDoc* _pDoc )
1390 : SwReader( _rTmpMedium, _rFilename, _pDoc )
1392 SetBaseURL( _rFilename );
1396 void SwDocShell::ReloadFromHtml( const OUString& rStreamName, SwSrcView* pSrcView )
1398 bool bModified = IsModified();
1400 // The HTTP-Header fields have to be removed, otherwise
1401 // there are some from Meta-Tags dublicated or triplicated afterwards.
1402 ClearHeaderAttributesForSourceViewHack();
1404 #if HAVE_FEATURE_SCRIPTING
1405 // The Document-Basic also bites the dust ...
1406 // A EnterBasicCall is not needed here, because nothing is called and
1407 // there can't be any Dok-Basic, that has not yet been loaded inside
1408 // of an HTML document.
1409 SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
1410 //#59620# HasBasic() shows, that there already is a BasicManager at the DocShell.
1411 // That was always generated in HTML-Import, when there are
1412 // Macros in the source code.
1413 if( rHtmlOptions.IsStarBasic() && HasBasic())
1415 BasicManager *pBasicMan = GetBasicManager();
1416 if( pBasicMan && (pBasicMan != SfxApplication::GetBasicManager()) )
1418 sal_uInt16 nLibCount = pBasicMan->GetLibCount();
1419 while( nLibCount )
1421 StarBASIC *pBasic = pBasicMan->GetLib( --nLibCount );
1422 if( pBasic )
1424 // Notify the IDE
1425 SfxUsrAnyItem aShellItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, makeAny( GetModel() ) );
1426 OUString aLibName( pBasic->GetName() );
1427 SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
1428 pSrcView->GetViewFrame()->GetDispatcher()->ExecuteList(
1429 SID_BASICIDE_LIBREMOVED,
1430 SfxCallMode::SYNCHRON,
1431 { &aShellItem, &aLibNameItem });
1433 // Only the modules are deleted from the standard-lib
1434 if( nLibCount )
1435 pBasicMan->RemoveLib( nLibCount, true );
1436 else
1437 pBasic->Clear();
1441 OSL_ENSURE( pBasicMan->GetLibCount() <= 1,
1442 "Deleting Basics didn't work" );
1445 #endif
1446 bool bWasBrowseMode = m_xDoc->getIDocumentSettingAccess().get(DocumentSettingId::BROWSE_MODE);
1447 RemoveLink();
1449 // now also the UNO-Model has to be informed about the new Doc #51535#
1450 uno::Reference<text::XTextDocument> xDoc(GetBaseModel(), uno::UNO_QUERY);
1451 text::XTextDocument* pxDoc = xDoc.get();
1452 static_cast<SwXTextDocument*>(pxDoc)->InitNewDoc();
1454 AddLink();
1455 //#116402# update font list when new document is created
1456 UpdateFontList();
1457 m_xDoc->getIDocumentSettingAccess().set(DocumentSettingId::BROWSE_MODE, bWasBrowseMode);
1458 pSrcView->SetPool(&GetPool());
1460 const OUString& rMedname = GetMedium()->GetName();
1462 // The HTML template still has to be set
1463 SetHTMLTemplate( *GetDoc() ); //Styles from HTML.vor
1465 SfxViewShell* pViewShell = GetView() ? static_cast<SfxViewShell*>(GetView())
1466 : SfxViewShell::Current();
1467 SfxViewFrame* pViewFrame = pViewShell->GetViewFrame();
1468 pViewFrame->GetDispatcher()->Execute( SID_VIEWSHELL0, SfxCallMode::SYNCHRON );
1470 SubInitNew();
1472 SfxMedium aMed( rStreamName, StreamMode::READ );
1473 // #i48748# - use class <SwReloadFromHtmlReader>, because
1474 // the base URL has to be set to the filename of the document <rMedname>
1475 // and not to the base URL of the temporary file <aMed> in order to get
1476 // the URLs of the linked graphics correctly resolved.
1477 SwReloadFromHtmlReader aReader( aMed, rMedname, m_xDoc.get() );
1479 aReader.Read( *ReadHTML );
1481 const SwView* pCurrView = GetView();
1482 //in print layout the first page(s) may have been formatted as a mix of browse
1483 //and print layout
1484 if(!bWasBrowseMode && pCurrView)
1486 SwWrtShell& rWrtSh = pCurrView->GetWrtShell();
1487 if( rWrtSh.GetLayout())
1488 rWrtSh.InvalidateLayout( true );
1491 // Take HTTP-Header-Attributes over into the DocInfo again.
1492 // The Base-URL doesn't matter here because TLX uses the one from the document
1493 // for absolutization.
1494 SetHeaderAttributesForSourceViewHack();
1496 if(bModified && !IsReadOnly())
1497 SetModified();
1498 else
1499 m_xDoc->getIDocumentState().ResetModified();
1502 ErrCode SwDocShell::LoadStylesFromFile( const OUString& rURL,
1503 SwgReaderOption const & rOpt, bool bUnoCall )
1505 ErrCode nErr = ERRCODE_NONE;
1507 // Set filter:
1508 SfxFilterMatcher aMatcher( SwDocShell::Factory().GetFactoryName() );
1510 // search for filter in WebDocShell, too
1511 SfxMedium aMed( rURL, StreamMode::STD_READ );
1512 std::shared_ptr<const SfxFilter> pFlt;
1513 aMatcher.DetectFilter( aMed, pFlt );
1514 if(!pFlt)
1516 SfxFilterMatcher aWebMatcher( SwWebDocShell::Factory().GetFactoryName() );
1517 aWebMatcher.DetectFilter( aMed, pFlt );
1519 // --> OD #i117339# - trigger import only for own formats
1520 bool bImport( false );
1522 if ( aMed.IsStorage() )
1524 // As <SfxMedium.GetFilter().IsOwnFormat() resp. IsOwnTemplateFormat()
1525 // does not work correct (e.g., MS Word 2007 XML Template),
1526 // use workaround provided by MAV.
1527 uno::Reference< embed::XStorage > xStorage = aMed.GetStorage();
1528 if ( xStorage.is() )
1530 // use <try-catch> on retrieving <MediaType> in order to check,
1531 // if the storage is one of our own ones.
1534 uno::Reference< beans::XPropertySet > xProps( xStorage, uno::UNO_QUERY_THROW );
1535 const OUString aMediaTypePropName( "MediaType" );
1536 xProps->getPropertyValue( aMediaTypePropName );
1537 bImport = true;
1539 catch (const uno::Exception&)
1541 bImport = false;
1546 if ( bImport )
1548 SwRead pRead = ReadXML;
1549 std::unique_ptr<SwReader> pReader;
1550 std::unique_ptr<SwPaM> pPam;
1551 // the SW3IO - Reader need the pam/wrtshell, because only then he
1552 // insert the styles!
1553 if( bUnoCall )
1555 SwNodeIndex aIdx( m_xDoc->GetNodes().GetEndOfContent(), -1 );
1556 pPam.reset(new SwPaM( aIdx ));
1557 pReader.reset(new SwReader( aMed, rURL, *pPam ));
1559 else
1561 pReader.reset(new SwReader( aMed, rURL, *m_pWrtShell->GetCursor() ));
1564 pRead->GetReaderOpt().SetTextFormats( rOpt.IsTextFormats() );
1565 pRead->GetReaderOpt().SetFrameFormats( rOpt.IsFrameFormats() );
1566 pRead->GetReaderOpt().SetPageDescs( rOpt.IsPageDescs() );
1567 pRead->GetReaderOpt().SetNumRules( rOpt.IsNumRules() );
1568 pRead->GetReaderOpt().SetMerge( rOpt.IsMerge() );
1570 if( bUnoCall )
1572 UnoActionContext aAction( m_xDoc.get() );
1573 nErr = pReader->Read( *pRead );
1575 else
1577 m_pWrtShell->StartAllAction();
1578 nErr = pReader->Read( *pRead );
1579 m_pWrtShell->EndAllAction();
1583 return nErr;
1586 // Get a client for an embedded object if possible.
1587 SfxInPlaceClient* SwDocShell::GetIPClient( const ::svt::EmbeddedObjectRef& xObjRef )
1589 SfxInPlaceClient* pResult = nullptr;
1591 SwWrtShell* pShell = GetWrtShell();
1592 if ( pShell )
1594 pResult = pShell->GetView().FindIPClient( xObjRef.GetObject(), &pShell->GetView().GetEditWin() );
1595 if ( !pResult )
1596 pResult = new SwOleClient( &pShell->GetView(), &pShell->GetView().GetEditWin(), xObjRef );
1599 return pResult;
1602 static bool lcl_MergePortions(SwNode *const& pNode, void *)
1604 if (pNode->IsTextNode())
1606 pNode->GetTextNode()->FileLoadedInitHints();
1608 return true;
1611 int SwFindDocShell( SfxObjectShellRef& xDocSh,
1612 SfxObjectShellLock& xLockRef,
1613 const OUString& rFileName,
1614 const OUString& rPasswd,
1615 const OUString& rFilter,
1616 sal_Int16 nVersion,
1617 SwDocShell* pDestSh )
1619 if ( rFileName.isEmpty() )
1620 return 0;
1622 // 1. Does the file already exist in the list of all Documents?
1623 INetURLObject aTmpObj( rFileName );
1624 aTmpObj.SetMark( OUString() );
1626 // Iterate over the DocShell and get the ones with the name
1628 SfxObjectShell* pShell = pDestSh;
1629 bool bFirst = nullptr != pShell;
1631 if( !bFirst )
1632 // No DocShell passed, starting with the first from the DocShell list
1633 pShell = SfxObjectShell::GetFirst( checkSfxObjectShell<SwDocShell> );
1635 while( pShell )
1637 // We want this one
1638 SfxMedium* pMed = pShell->GetMedium();
1639 if( pMed && pMed->GetURLObject() == aTmpObj )
1641 const SfxPoolItem* pItem;
1642 if( ( SfxItemState::SET == pMed->GetItemSet()->GetItemState(
1643 SID_VERSION, false, &pItem ) )
1644 ? (nVersion == static_cast<const SfxInt16Item*>(pItem)->GetValue())
1645 : !nVersion )
1647 // Found, thus return
1648 xDocSh = pShell;
1649 return 1;
1653 if( bFirst )
1655 bFirst = false;
1656 pShell = SfxObjectShell::GetFirst( checkSfxObjectShell<SwDocShell> );
1658 else
1659 pShell = SfxObjectShell::GetNext( *pShell, checkSfxObjectShell<SwDocShell> );
1662 // 2. Open the file ourselves
1663 std::unique_ptr<SfxMedium> xMed(new SfxMedium( aTmpObj.GetMainURL(
1664 INetURLObject::DecodeMechanism::NONE ), StreamMode::READ ));
1665 if( INetProtocol::File == aTmpObj.GetProtocol() )
1666 xMed->Download(); // Touch the medium (download it)
1668 std::shared_ptr<const SfxFilter> pSfxFlt;
1669 if (!xMed->GetError())
1671 SfxFilterMatcher aMatcher( rFilter == "writerglobal8"
1672 ? SwGlobalDocShell::Factory().GetFactoryName()
1673 : SwDocShell::Factory().GetFactoryName() );
1675 // No Filter, so search for it. Else test if the one passed is a valid one
1676 if( !rFilter.isEmpty() )
1678 pSfxFlt = aMatcher.GetFilter4FilterName( rFilter );
1681 if( nVersion )
1682 xMed->GetItemSet()->Put( SfxInt16Item( SID_VERSION, nVersion ));
1684 if( !rPasswd.isEmpty() )
1685 xMed->GetItemSet()->Put( SfxStringItem( SID_PASSWORD, rPasswd ));
1687 if( !pSfxFlt )
1688 aMatcher.DetectFilter( *xMed, pSfxFlt );
1690 if( pSfxFlt )
1692 // We cannot do anything without a Filter
1693 xMed->SetFilter( pSfxFlt );
1695 // If the new shell is created, SfxObjectShellLock should be used to let it be closed later for sure
1696 SwDocShell *const pNew(new SwDocShell(SfxObjectCreateMode::INTERNAL));
1697 xLockRef = pNew;
1698 xDocSh = static_cast<SfxObjectShell*>(xLockRef);
1699 if (xDocSh->DoLoad(xMed.release()))
1701 SwDoc const& rDoc(*pNew->GetDoc());
1702 const_cast<SwDoc&>(rDoc).GetNodes().ForEach(&lcl_MergePortions);
1703 return 2;
1708 return 0;
1711 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */