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 <sal/config.h>
21 
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/frame/XTitle.hpp>
24 #include <svl/eitem.hxx>
25 #include <svl/stritem.hxx>
26 #include <sfx2/printer.hxx>
27 #include <sfx2/request.hxx>
28 #include <svl/macitem.hxx>
29 #include <gloshdl.hxx>
30 
31 #include <editeng/acorrcfg.hxx>
32 #include <sfx2/objface.hxx>
33 #include <sfx2/viewfrm.hxx>
34 #include <wrtsh.hxx>
35 #include <view.hxx>
36 #include <glshell.hxx>
37 #include <doc.hxx>
38 #include <IDocumentUndoRedo.hxx>
40 #include <IDocumentState.hxx>
41 #include <glosdoc.hxx>
42 #include <shellio.hxx>
43 #include <initui.hxx>
44 #include <cmdid.h>
45 #include <strings.hrc>
46 
47 #define ShellClass_SwWebGlosDocShell
48 #define ShellClass_SwGlosDocShell
49 
50 #include <sfx2/msg.hxx>
51 #include <swslots.hxx>
52 #include <memory>
53 #include <utility>
54 
55 using namespace ::com::sun::star;
56 
58 
59 void SwGlosDocShell::InitInterface_Impl()
60 {
61 }
62 
64 
65 void SwWebGlosDocShell::InitInterface_Impl()
66 {
67 }
68 
69 
70 static void lcl_Execute( SwDocShell& rSh, SfxRequest& rReq )
71 {
72  if ( rReq.GetSlot() != SID_SAVEDOC )
73  return;
74 
75  if( !rSh.HasName() )
76  {
77  rReq.SetReturnValue( SfxBoolItem( 0, rSh.Save() ) );
78  }
79  else
80  {
81  const SfxBoolItem* pRes = static_cast< const SfxBoolItem* >(
82  rSh.ExecuteSlot( rReq,
83  rSh.SfxObjectShell::GetInterface() ));
84  if( pRes->GetValue() )
86  }
87 }
88 
89 static void lcl_GetState( SwDocShell& rSh, SfxItemSet& rSet )
90 {
91  if( SfxItemState::DEFAULT >= rSet.GetItemState( SID_SAVEDOC, false ))
92  {
93  if( !rSh.GetDoc()->getIDocumentState().IsModified() )
94  rSet.DisableItem( SID_SAVEDOC );
95  else
96  rSet.Put( SfxStringItem( SID_SAVEDOC, SwResId(STR_SAVE_GLOSSARY)));
97  }
98 }
99 
100 static bool lcl_Save( SwWrtShell& rSh, const OUString& rGroupName,
101  const OUString& rShortNm, const OUString& rLongNm )
102 {
103  const SvxAutoCorrCfg& rCfg = SvxAutoCorrCfg::Get();
104  std::unique_ptr<SwTextBlocks> pBlock(::GetGlossaries()->GetGroupDoc( rGroupName ));
105 
106  SvxMacro aStart { OUString(), OUString() };
107  SvxMacro aEnd { OUString(), OUString() };
108  SwGlossaryHdl* pGlosHdl;
109 
110  pGlosHdl = rSh.GetView().GetGlosHdl();
111  pGlosHdl->GetMacros( rShortNm, aStart, aEnd, pBlock.get() );
112 
113  sal_uInt16 nRet = rSh.SaveGlossaryDoc( *pBlock, rLongNm, rShortNm,
114  rCfg.IsSaveRelFile(),
115  pBlock->IsOnlyTextBlock( rShortNm ) );
116 
117  if(aStart.HasMacro() || aEnd.HasMacro() )
118  {
119  SvxMacro* pStart = aStart.HasMacro() ? &aStart : nullptr;
120  SvxMacro* pEnd = aEnd.HasMacro() ? &aEnd : nullptr;
121  pGlosHdl->SetMacros( rShortNm, pStart, pEnd, pBlock.get() );
122  }
123 
124  rSh.EnterStdMode();
125  if( USHRT_MAX != nRet )
126  rSh.ResetModified();
127  return nRet != USHRT_MAX;
128 }
129 
131  : SwDocShell( bNewShow
133 {
134 }
135 
137 {
138 }
139 
141 {
142  ::lcl_Execute( *this, rReq );
143 }
144 
146 {
147  ::lcl_GetState( *this, rSet );
148 }
149 
151 {
152  // In case of an API object which holds this document, it is possible that the WrtShell is already
153  // dead. For instance, if the doc is modified via this API object, and then, upon office shutdown,
154  // the document's view is closed (by the SFX framework) _before_ the API object is release and
155  // tries to save the doc, again.
156  // 96380 - 2002-03-03 - fs@openoffice.org
157  if ( GetWrtShell() )
159  else
160  {
161  SetModified( false );
162  return false;
163  }
164 }
165 
167  : SwWebDocShell()
168 {
169 }
170 
172 {
173 }
174 
176 {
177  ::lcl_Execute( *this, rReq );
178 }
179 
181 {
182  ::lcl_GetState( *this, rSet );
183 }
184 
186 {
187  // same comment as in SwGlosDocShell::Save - see there
188  if ( GetWrtShell() )
190  else
191  {
192  SetModified( false );
193  return false;
194  }
195 }
196 
197 SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString& rShortName, bool bShow )
198 {
199  SwDocShellRef xDocSh;
200 
201  std::unique_ptr<SwTextBlocks> pGroup = GetGroupDoc( rGroup );
202  if (pGroup && pGroup->GetCount())
203  {
204  // query which view is registered. In WebWriter there is no normal view
205  SfxInterfaceId nViewId = nullptr != SwView::Factory() ? SFX_INTERFACE_SFXDOCSH : SfxInterfaceId(6);
206  const OUString sLongName = pGroup->GetLongName(pGroup->GetIndex( rShortName ));
207 
208  if( SfxInterfaceId(6) == nViewId )
209  {
210  SwWebGlosDocShell* pDocSh = new SwWebGlosDocShell();
211  xDocSh = pDocSh;
212  pDocSh->DoInitNew();
213  pDocSh->SetLongName( sLongName );
214  pDocSh->SetShortName( rShortName);
215  pDocSh->SetGroupName( rGroup );
216  }
217  else
218  {
219  SwGlosDocShell* pDocSh = new SwGlosDocShell(bShow);
220  xDocSh = pDocSh;
221  pDocSh->DoInitNew();
222  pDocSh->SetLongName( sLongName );
223  pDocSh->SetShortName( rShortName );
224  pDocSh->SetGroupName( rGroup );
225  }
226 
227  // set document title
228  SfxViewFrame* pFrame = bShow ? SfxViewFrame::LoadDocument( *xDocSh, nViewId ) : SfxViewFrame::LoadHiddenDocument( *xDocSh, nViewId );
229  const OUString aDocTitle(SwResId( STR_GLOSSARY ) + " " + sLongName);
230 
231  bool const bDoesUndo =
232  xDocSh->GetDoc()->GetIDocumentUndoRedo().DoesUndo();
233  xDocSh->GetDoc()->GetIDocumentUndoRedo().DoUndo( false );
234 
235  xDocSh->GetWrtShell()->InsertGlossary( *pGroup, rShortName );
236  if( !xDocSh->GetDoc()->getIDocumentDeviceAccess().getPrinter( false ) )
237  {
238  // we create a default SfxPrinter.
239  // ItemSet is deleted by Sfx!
240  auto pSet = std::make_unique<SfxItemSet>(
241  xDocSh->GetDoc()->GetAttrPool(),
242  svl::Items<
243  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
244  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
245  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
246  VclPtr<SfxPrinter> pPrinter = VclPtr<SfxPrinter>::Create( std::move(pSet) );
247 
248  // and append it to the document.
249  xDocSh->GetDoc()->getIDocumentDeviceAccess().setPrinter( pPrinter, true, true );
250  }
251 
252  xDocSh->SetTitle( aDocTitle );
253  try
254  {
255  // set the UI-title
256  uno::Reference< frame::XTitle > xTitle( xDocSh->GetModel(), uno::UNO_QUERY_THROW );
257  xTitle->setTitle( aDocTitle );
258  }
259  catch (const uno::Exception&)
260  {
261  }
262 
263  xDocSh->GetDoc()->GetIDocumentUndoRedo().DoUndo( bDoesUndo );
264  xDocSh->GetDoc()->getIDocumentState().ResetModified();
265  if ( bShow )
266  pFrame->GetFrame().Appear();
267  }
268  return xDocSh;
269 }
270 
271 /* 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:39
OUString aGroupName
Definition: glshell.hxx:27
void Appear()
virtual void SetModified(bool=true) override
Definition: docsh2.cxx:1378
static SvxAutoCorrCfg & Get()
OUString aGroupName
Definition: glshell.hxx:57
IDocumentDeviceAccess const & getIDocumentDeviceAccess() const
Definition: doc.cxx:238
static SfxViewFrame * LoadDocument(SfxObjectShell const &i_rDoc, SfxInterfaceId i_nViewId)
SwDocShellRef EditGroupDoc(const OUString &rGrpName, const OUString &rShortName, bool bShow=true)
Definition: glshell.cxx:197
virtual bool Save() override
global IO.
Definition: glshell.cxx:185
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:144
css::uno::Reference< css::frame::XModel > GetModel() const
#define FN_PARAM_ADDPRINTER
Definition: cmdid.h:760
static void lcl_GetState(SwDocShell &rSh, SfxItemSet &rSet)
Definition: glshell.cxx:89
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:100
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:180
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:175
virtual void DoUndo(bool const bDoUndo)=0
Enable/Disable Undo.
void Execute(SfxRequest &)
Definition: glshell.cxx:140
SW_DLLPUBLIC SwGlossaries * GetGlossaries()
Definition: initui.cxx:160
SwGlosDocShell(bool bNewShow)
Definition: glshell.cxx:130
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:145
virtual ~SwGlosDocShell() override
Definition: glshell.cxx:136
void SetTitle(const OUString &rTitle)
void SetShortName(const OUString &rShortName)
Definition: glshell.hxx:77
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:394
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:171
OUString SwResId(const char *pId)
Definition: swmodule.cxx:165
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:75
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:254
void ResetModified()
Definition: edws.cxx:75
bool DoInitNew(SfxMedium *pMedium=nullptr)
static VclPtr< reference_type > Create(Arg &&...arg)
virtual bool Save() override
global IO.
Definition: glshell.cxx:150
SwGlossaryHdl * GetGlosHdl()
Definition: view.cxx:1607
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:70
OUString aShortName
Definition: glshell.hxx:26
virtual bool Save() override
global IO.
Definition: docsh.cxx:265
void SetLongName(const OUString &rLongName)
Definition: glshell.hxx:75
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1315
virtual void ResetModified()=0