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