LibreOffice Module sw (master)  1
glshell.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <com/sun/star/frame/XTitle.hpp>
21 #include <svl/eitem.hxx>
22 #include <svl/stritem.hxx>
23 #include <sfx2/printer.hxx>
24 #include <sfx2/request.hxx>
25 #include <svl/macitem.hxx>
26 #include <gloshdl.hxx>
27 
28 #include <editeng/acorrcfg.hxx>
29 #include <sfx2/objface.hxx>
30 #include <sfx2/viewfrm.hxx>
31 #include <wrtsh.hxx>
32 #include <view.hxx>
33 #include <glshell.hxx>
34 #include <doc.hxx>
35 #include <IDocumentUndoRedo.hxx>
37 #include <IDocumentState.hxx>
38 #include <glosdoc.hxx>
39 #include <shellio.hxx>
40 #include <initui.hxx>
41 #include <cmdid.h>
42 #include <strings.hrc>
43 
44 #define ShellClass_SwWebGlosDocShell
45 #define ShellClass_SwGlosDocShell
46 
47 #include <sfx2/msg.hxx>
48 #include <swslots.hxx>
49 #include <memory>
50 #include <utility>
51 
52 using namespace ::com::sun::star;
53 
55 
56 void SwGlosDocShell::InitInterface_Impl()
57 {
58 }
59 
61 
62 void SwWebGlosDocShell::InitInterface_Impl()
63 {
64 }
65 
66 
67 static void lcl_Execute( SwDocShell& rSh, SfxRequest& rReq )
68 {
69  if ( rReq.GetSlot() == SID_SAVEDOC )
70  {
71  if( !rSh.HasName() )
72  {
73  rReq.SetReturnValue( SfxBoolItem( 0, rSh.Save() ) );
74  }
75  else
76  {
77  const SfxBoolItem* pRes = static_cast< const SfxBoolItem* >(
78  rSh.ExecuteSlot( rReq,
79  rSh.SfxObjectShell::GetInterface() ));
80  if( pRes->GetValue() )
82  }
83  }
84 }
85 
86 static void lcl_GetState( SwDocShell& rSh, SfxItemSet& rSet )
87 {
88  if( SfxItemState::DEFAULT >= rSet.GetItemState( SID_SAVEDOC, false ))
89  {
90  if( !rSh.GetDoc()->getIDocumentState().IsModified() )
91  rSet.DisableItem( SID_SAVEDOC );
92  else
93  rSet.Put( SfxStringItem( SID_SAVEDOC, SwResId(STR_SAVE_GLOSSARY)));
94  }
95 }
96 
97 static bool lcl_Save( SwWrtShell& rSh, const OUString& rGroupName,
98  const OUString& rShortNm, const OUString& rLongNm )
99 {
100  const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
101  std::unique_ptr<SwTextBlocks> pBlock(::GetGlossaries()->GetGroupDoc( rGroupName ));
102 
103  SvxMacro aStart { OUString(), OUString() };
104  SvxMacro aEnd { OUString(), OUString() };
105  SwGlossaryHdl* pGlosHdl;
106 
107  pGlosHdl = rSh.GetView().GetGlosHdl();
108  pGlosHdl->GetMacros( rShortNm, aStart, aEnd, pBlock.get() );
109 
110  sal_uInt16 nRet = rSh.SaveGlossaryDoc( *pBlock, rLongNm, rShortNm,
111  rCfg.IsSaveRelFile(),
112  pBlock->IsOnlyTextBlock( rShortNm ) );
113 
114  if(aStart.HasMacro() || aEnd.HasMacro() )
115  {
116  SvxMacro* pStart = aStart.HasMacro() ? &aStart : nullptr;
117  SvxMacro* pEnd = aEnd.HasMacro() ? &aEnd : nullptr;
118  pGlosHdl->SetMacros( rShortNm, pStart, pEnd, pBlock.get() );
119  }
120 
121  rSh.EnterStdMode();
122  if( USHRT_MAX != nRet )
123  rSh.ResetModified();
124  return nRet != USHRT_MAX;
125 }
126 
128  : SwDocShell( bNewShow
130 {
131 }
132 
134 {
135 }
136 
138 {
139  ::lcl_Execute( *this, rReq );
140 }
141 
143 {
144  ::lcl_GetState( *this, rSet );
145 }
146 
148 {
149  // In case of an API object which holds this document, it is possible that the WrtShell is already
150  // dead. For instance, if the doc is modified via this API object, and then, upon office shutdown,
151  // the document's view is closed (by the SFX framework) _before_ the API object is release and
152  // tries to save the doc, again.
153  // 96380 - 2002-03-03 - fs@openoffice.org
154  if ( GetWrtShell() )
156  else
157  {
158  SetModified( false );
159  return false;
160  }
161 }
162 
164  : SwWebDocShell()
165 {
166 }
167 
169 {
170 }
171 
173 {
174  ::lcl_Execute( *this, rReq );
175 }
176 
178 {
179  ::lcl_GetState( *this, rSet );
180 }
181 
183 {
184  // same comment as in SwGlosDocShell::Save - see there
185  if ( GetWrtShell() )
187  else
188  {
189  SetModified( false );
190  return false;
191  }
192 }
193 
194 SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString& rShortName, bool bShow )
195 {
196  SwDocShellRef xDocSh;
197 
198  std::unique_ptr<SwTextBlocks> pGroup = GetGroupDoc( rGroup );
199  if (pGroup && pGroup->GetCount())
200  {
201  // query which view is registered. In WebWriter there is no normal view
202  SfxInterfaceId nViewId = nullptr != SwView::Factory() ? SFX_INTERFACE_SFXDOCSH : SfxInterfaceId(6);
203  const OUString sLongName = pGroup->GetLongName(pGroup->GetIndex( rShortName ));
204 
205  if( SfxInterfaceId(6) == nViewId )
206  {
207  SwWebGlosDocShell* pDocSh = new SwWebGlosDocShell();
208  xDocSh = pDocSh;
209  pDocSh->DoInitNew();
210  pDocSh->SetLongName( sLongName );
211  pDocSh->SetShortName( rShortName);
212  pDocSh->SetGroupName( rGroup );
213  }
214  else
215  {
216  SwGlosDocShell* pDocSh = new SwGlosDocShell(bShow);
217  xDocSh = pDocSh;
218  pDocSh->DoInitNew();
219  pDocSh->SetLongName( sLongName );
220  pDocSh->SetShortName( rShortName );
221  pDocSh->SetGroupName( rGroup );
222  }
223 
224  // set document title
225  SfxViewFrame* pFrame = bShow ? SfxViewFrame::LoadDocument( *xDocSh, nViewId ) : SfxViewFrame::LoadHiddenDocument( *xDocSh, nViewId );
226  const OUString aDocTitle(SwResId( STR_GLOSSARY ) + " " + sLongName);
227 
228  bool const bDoesUndo =
229  xDocSh->GetDoc()->GetIDocumentUndoRedo().DoesUndo();
230  xDocSh->GetDoc()->GetIDocumentUndoRedo().DoUndo( false );
231 
232  xDocSh->GetWrtShell()->InsertGlossary( *pGroup, rShortName );
233  if( !xDocSh->GetDoc()->getIDocumentDeviceAccess().getPrinter( false ) )
234  {
235  // we create a default SfxPrinter.
236  // ItemSet is deleted by Sfx!
237  auto pSet = std::make_unique<SfxItemSet>(
238  xDocSh->GetDoc()->GetAttrPool(),
239  svl::Items<
240  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
241  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
242  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
243  VclPtr<SfxPrinter> pPrinter = VclPtr<SfxPrinter>::Create( std::move(pSet) );
244 
245  // and append it to the document.
246  xDocSh->GetDoc()->getIDocumentDeviceAccess().setPrinter( pPrinter, true, true );
247  }
248 
249  xDocSh->SetTitle( aDocTitle );
250  try
251  {
252  // set the UI-title
253  uno::Reference< frame::XTitle > xTitle( xDocSh->GetModel(), uno::UNO_QUERY_THROW );
254  xTitle->setTitle( aDocTitle );
255  }
256  catch (const uno::Exception&)
257  {
258  }
259 
260  xDocSh->GetDoc()->GetIDocumentUndoRedo().DoUndo( bDoesUndo );
261  xDocSh->GetDoc()->getIDocumentState().ResetModified();
262  if ( bShow )
263  pFrame->GetFrame().Appear();
264  }
265  return xDocSh;
266 }
267 
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SfxPoolItem * ExecuteSlot(SfxRequest &rReq, const SfxInterface *pIF=nullptr)
SFX_IMPL_SUPERCLASS_INTERFACE(DrawDocShell, SfxObjectShell)
bool GetValue() const
void InsertGlossary(SwTextBlocks &rGlossary, const OUString &)
Glossary from glossary document in current document.
Definition: edglss.cxx:35
OUString aGroupName
Definition: glshell.hxx:27
void Appear()
virtual void SetModified(bool=true) override
Definition: docsh2.cxx:1377
static SvxAutoCorrCfg & Get()
OUString aGroupName
Definition: glshell.hxx:57
IDocumentDeviceAccess const & getIDocumentDeviceAccess() const
Definition: doc.cxx:237
static SfxViewFrame * LoadDocument(SfxObjectShell const &i_rDoc, SfxInterfaceId i_nViewId)
SwDocShellRef EditGroupDoc(const OUString &rGrpName, const OUString &rShortName, bool bShow=true)
Definition: glshell.cxx:194
virtual bool Save() override
global IO.
Definition: glshell.cxx:182
std::unique_ptr< SwTextBlocks > GetGroupDoc(const OUString &rName, bool bCreate=false)
Definition: glosdoc.cxx:157
void EnterStdMode()
Definition: select.cxx:550
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
css::uno::Reference< css::frame::XModel > GetModel() const
#define FN_PARAM_ADDPRINTER
Definition: cmdid.h:750
static void lcl_GetState(SwDocShell &rSh, SfxItemSet &rSet)
Definition: glshell.cxx:86
void SetLongName(const OUString &rLongName)
Definition: glshell.hxx:45
static bool lcl_Save(SwWrtShell &rSh, const OUString &rGroupName, const OUString &rShortNm, const OUString &rLongNm)
Definition: glshell.cxx:97
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
bool HasMacro() const
void SetShortName(const OUString &rShortName)
Definition: glshell.hxx:47
const SwView & GetView() const
Definition: wrtsh.hxx:428
void GetState(SfxItemSet &)
Definition: glshell.cxx:177
SfxFrame & GetFrame() const
void SetMacros(const OUString &rName, const SvxMacro *pStart, const SvxMacro *pEnd, SwTextBlocks *pGlossary=nullptr)
Definition: gloshdl.cxx:535
void Execute(SfxRequest &)
Definition: glshell.cxx:172
virtual void DoUndo(bool const bDoUndo)=0
Enable/Disable Undo.
void Execute(SfxRequest &)
Definition: glshell.cxx:137
SW_DLLPUBLIC SwGlossaries * GetGlossaries()
Definition: initui.cxx:160
SwGlosDocShell(bool bNewShow)
Definition: glshell.cxx:127
virtual bool DoesUndo() const =0
Is Undo enabled?
SwDoc * GetDoc()
returns Doc. But be careful!
Definition: docsh.hxx:203
OUString aShortName
Definition: glshell.hxx:56
OUString aLongName
Definition: glshell.hxx:55
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
SfxObjectCreateMode
virtual bool IsModified() const =0
Changes of document?
void GetState(SfxItemSet &)
Definition: glshell.cxx:142
virtual ~SwGlosDocShell() override
Definition: glshell.cxx:133
void SetTitle(const OUString &rTitle)
void SetShortName(const OUString &rShortName)
Definition: glshell.hxx:77
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:393
SwWrtShell * GetWrtShell()
Access to the SwWrtShell belonging to SwView.
Definition: docsh.hxx:224
OUString aLongName
Definition: glshell.hxx:25
bool HasName() const
virtual ~SwWebGlosDocShell() override
Definition: glshell.cxx:168
OUString SwResId(const char *pId)
Definition: swmodule.cxx:178
bool IsSaveRelFile() const
void SetGroupName(const OUString &rGroupName)
Definition: glshell.hxx:49
void SetGroupName(const OUString &rGroupName)
Definition: glshell.hxx:79
virtual SfxPrinter * getPrinter(bool bCreate) const =0
Return the printer set at the document.
virtual void setPrinter(SfxPrinter *pP, bool bDeleteOld, bool bCallPrtDataChanged)=0
Set the printer at the document.
constexpr auto SFX_INTERFACE_SFXDOCSH
void SetReturnValue(const SfxPoolItem &)
sal_uInt16 SaveGlossaryDoc(SwTextBlocks &rGlossary, const OUString &rName, const OUString &rShortName, bool bSaveRelFile, bool bOnlyText)
Save complete content of doc as glossary.
Definition: edglss.cxx:71
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
sal_uInt16 GetSlot() const
void DisableItem(sal_uInt16 nWhich)
static SfxViewFrame * LoadHiddenDocument(SfxObjectShell const &i_rDoc, SfxInterfaceId i_nViewId)
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:253
void ResetModified()
Definition: edws.cxx:74
bool DoInitNew(SfxMedium *pMedium=nullptr)
static VclPtr< reference_type > Create(Arg &&...arg)
virtual bool Save() override
global IO.
Definition: glshell.cxx:147
SwGlossaryHdl * GetGlosHdl()
Definition: view.cxx:1604
void GetMacros(const OUString &rShortName, SvxMacro &rStart, SvxMacro &rEnd, SwTextBlocks *pGlossary=nullptr)
Definition: gloshdl.cxx:556
static void lcl_Execute(SwDocShell &rSh, SfxRequest &rReq)
Definition: glshell.cxx:67
OUString aShortName
Definition: glshell.hxx:26
virtual bool Save() override
global IO.
Definition: docsh.cxx:266
void SetLongName(const OUString &rLongName)
Definition: glshell.hxx:75
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1313
virtual void ResetModified()=0