LibreOffice Module sw (master)  1
edtox.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/util/SearchAlgorithms2.hpp>
21 #include <com/sun/star/util/SearchFlags.hpp>
24 #include <i18nutil/searchopt.hxx>
25 #include <svl/fstathelper.hxx>
26 #include <osl/diagnose.h>
27 #include <osl/thread.h>
28 #include <unotools/syslocale.hxx>
29 
30 #include <sfx2/docfile.hxx>
31 
32 #include <swtypes.hxx>
33 #include <rootfrm.hxx>
34 #include <editsh.hxx>
35 #include <doc.hxx>
37 #include <IDocumentUndoRedo.hxx>
38 #include <pam.hxx>
39 #include <swundo.hxx>
40 #include <tox.hxx>
41 #include <doctxm.hxx>
42 #include <docary.hxx>
43 #include <mdiexp.hxx>
44 #include <strings.hrc>
45 #include <iodetect.hxx>
46 
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::i18n;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::util;
51 
52 // Add/delete listing markers to a document
53 
54 void SwEditShell::Insert(const SwTOXMark& rMark)
55 {
56  bool bInsAtPos = rMark.IsAlternativeText();
58  for(SwPaM& rPaM : GetCursor()->GetRingContainer())
59  {
60  const SwPosition *pStt = rPaM.Start(),
61  *pEnd = rPaM.End();
62  if( bInsAtPos )
63  {
64  SwPaM aTmp( *pStt );
66  }
67  else if( *pEnd != *pStt )
68  {
70  rPaM, rMark, SetAttrMode::DONTEXPAND );
71  }
72 
73  }
74  EndAllAction();
75 }
76 
78 {
79  CurrShell aCurr( this );
81 
82  mxDoc->DeleteTOXMark( pMark );
83 
84  EndAllAction();
85 }
86 
89 {
90  SwDoc::GetCurTOXMark( *GetCursor()->Start(), rMarks );
91 }
92 
94 {
95  assert( dynamic_cast<const SwTOXBaseSection*>( &rTOXBase) && "no TOXBaseSection!" );
96  const SwTOXBaseSection& rTOXSect = static_cast<const SwTOXBaseSection&>(rTOXBase);
97  return rTOXSect.IsProtect();
98 }
99 
100 void SwEditShell::SetTOXBaseReadonly(const SwTOXBase& rTOXBase, bool bReadonly)
101 {
102  assert( dynamic_cast<const SwTOXBaseSection*>( &rTOXBase) && "no TOXBaseSection!" );
103  const SwTOXBaseSection& rTOXSect = static_cast<const SwTOXBaseSection&>(rTOXBase);
104  const_cast<SwTOXBase&>(rTOXBase).SetProtected(bReadonly);
105  OSL_ENSURE( rTOXSect.SwSection::GetType() == SectionType::ToxContent, "not a TOXContentSection" );
106 
107  SwSectionData aSectionData(rTOXSect);
108  aSectionData.SetProtectFlag(bReadonly);
109  UpdateSection( GetSectionFormatPos( *rTOXSect.GetFormat() ), aSectionData );
110 }
111 
113 {
114  return GetDoc()->GetDefaultTOXBase( eTyp, bCreate );
115 }
116 
118 {
119  GetDoc()->SetDefaultTOXBase(rBase);
120 }
121 
123 void SwEditShell::InsertTableOf( const SwTOXBase& rTOX, const SfxItemSet* pSet )
124 {
125  CurrShell aCurr( this );
126  StartAllAction();
127 
128  SwDocShell* pDocSh = GetDoc()->GetDocShell();
129  ::StartProgress( STR_STATSTR_TOX_INSERT, 0, 0, pDocSh );
130 
131  // Insert listing
132  const SwTOXBaseSection* pTOX = mxDoc->InsertTableOf(
133  *GetCursor()->GetPoint(), rTOX, pSet, true, GetLayout() );
134  OSL_ENSURE(pTOX, "No current TOX");
135 
136  // start formatting
137  CalcLayout();
138 
139  // insert page numbering
140  const_cast<SwTOXBaseSection*>(pTOX)->UpdatePageNum();
141 
142  pTOX->SetPosAtStartEnd( *GetCursor()->GetPoint() );
143 
144  // Fix for empty listing
146  ::EndProgress( pDocSh );
147  EndAllAction();
148 }
149 
151 void SwEditShell::UpdateTableOf(const SwTOXBase& rTOX, const SfxItemSet* pSet)
152 {
153  assert(dynamic_cast<const SwTOXBaseSection*>(&rTOX) && "no TOXBaseSection!");
154  SwTOXBaseSection& rTOXSect = static_cast<SwTOXBaseSection&>(const_cast<SwTOXBase&>(rTOX));
155  if (!rTOXSect.GetFormat()->GetSectionNode())
156  return;
157 
158  SwDoc* pMyDoc = GetDoc();
159  SwDocShell* pDocSh = pMyDoc->GetDocShell();
160 
161  bool bInIndex = &rTOX == GetCurTOX();
162  CurrShell aCurr( this );
163  StartAllAction();
164 
165  ::StartProgress( STR_STATSTR_TOX_UPDATE, 0, 0, pDocSh );
166 
168 
169  // create listing stub
170  rTOXSect.Update(pSet, GetLayout());
171 
172  // correct Cursor
173  if( bInIndex )
174  rTOXSect.SetPosAtStartEnd(*GetCursor()->GetPoint());
175 
176  // start formatting
177  // tdf#139426 ...but allow suppression of AssertFlyPages
179  CalcLayout();
181 
182  // insert page numbering
183  rTOXSect.UpdatePageNum();
184 
186 
187  ::EndProgress( pDocSh );
188  EndAllAction();
189 }
190 
193 {
194  return SwDoc::GetCurTOX( *GetCursor()->GetPoint() );
195 }
196 
197 bool SwEditShell::DeleteTOX( const SwTOXBase& rTOXBase, bool bDelNodes )
198 {
199  return GetDoc()->DeleteTOX( rTOXBase, bDelNodes );
200 }
201 
202 // manage types of listings
203 
204 const SwTOXType* SwEditShell::GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
205 {
206  return mxDoc->GetTOXType(eTyp, nId);
207 }
208 
209 // manage keys for the alphabetical index
210 
211 void SwEditShell::GetTOIKeys( SwTOIKeyType eTyp, std::vector<OUString>& rArr ) const
212 {
213  GetDoc()->GetTOIKeys( eTyp, rArr, *GetLayout() );
214 }
215 
216 sal_uInt16 SwEditShell::GetTOXCount() const
217 {
218  const SwSectionFormats& rFormats = GetDoc()->GetSections();
219  sal_uInt16 nRet = 0;
220  for( auto n = rFormats.size(); n; )
221  {
222  const SwSection* pSect = rFormats[ --n ]->GetSection();
223  if( SectionType::ToxContent == pSect->GetType() &&
224  pSect->GetFormat()->GetSectionNode() )
225  ++nRet;
226  }
227  return nRet;
228 }
229 
230 const SwTOXBase* SwEditShell::GetTOX( sal_uInt16 nPos ) const
231 {
232  const SwSectionFormats& rFormats = GetDoc()->GetSections();
233  sal_uInt16 nCnt {0};
234  for( const SwSectionFormat *pFormat : rFormats )
235  {
236  const SwSection* pSect = pFormat->GetSection();
237  if( SectionType::ToxContent == pSect->GetType() &&
238  pSect->GetFormat()->GetSectionNode() &&
239  nCnt++ == nPos )
240  {
241  assert( dynamic_cast<const SwTOXBaseSection*>( pSect) && "no TOXBaseSection!" );
242  return static_cast<const SwTOXBaseSection*>(pSect);
243  }
244  }
245  return nullptr;
246 }
247 
249 void SwEditShell::SetUpdateTOX( bool bFlag )
250 {
251  GetDoc()->SetUpdateTOX( bFlag );
252 }
253 
255 {
256  return GetDoc()->IsUpdateTOX();
257 }
258 
259 OUString const & SwEditShell::GetTOIAutoMarkURL() const
260 {
261  return GetDoc()->GetTOIAutoMarkURL();
262 }
263 
264 void SwEditShell::SetTOIAutoMarkURL(const OUString& rSet)
265 {
266  GetDoc()->SetTOIAutoMarkURL(rSet);
267 }
268 
270 {
271  StartAllAction();
272  bool bDoesUndo = DoesUndo();
273  DoUndo(false);
274  //1. remove all automatic generated index entries if AutoMarkURL has a
275  // length and the file exists
276  //2. load file
277  //3. select all occurrences of the searched words
278  //4. apply index entries
279 
280  OUString sAutoMarkURL(GetDoc()->GetTOIAutoMarkURL());
281  if( !sAutoMarkURL.isEmpty() && FStatHelper::IsDocument( sAutoMarkURL ))
282  {
283  //1.
284  const SwTOXType* pTOXType = GetTOXType(TOX_INDEX, 0);
285 
286  SwTOXMarks aMarks;
287  pTOXType->CollectTextMarks(aMarks);
288  for( SwTOXMark* pMark : aMarks )
289  {
290  if(pMark->IsAutoGenerated() && pMark->GetTextTOXMark())
291  // mba: test iteration; objects are deleted in iteration
292  DeleteTOXMark(pMark);
293  }
294 
295  //2.
296  SfxMedium aMedium( sAutoMarkURL, StreamMode::STD_READ );
297  SvStream& rStrm = *aMedium.GetInStream();
298  Push();
299  // tdf#106899 - import tox concordance file using the appropriate character set
300  rtl_TextEncoding eChrSet = SwIoSystem::GetTextEncoding(rStrm);
301  if (eChrSet == RTL_TEXTENCODING_DONTKNOW)
302  eChrSet = ::osl_getThreadTextEncoding();
303 
304  // SearchOptions to be used in loop below
305  sal_Int32 const nLEV_Other = 2; // -> changedChars;
306  sal_Int32 const nLEV_Longer = 3;
307  sal_Int32 const nLEV_Shorter = 1;
308 
309  i18nutil::SearchOptions2 aSearchOpt(
310  SearchAlgorithms_ABSOLUTE,
311  SearchFlags::LEV_RELAXED,
312  "", "",
313  SvtSysLocale().GetLanguageTag().getLocale(),
314  nLEV_Other, nLEV_Longer, nLEV_Shorter,
315  TransliterationFlags::NONE,
316  SearchAlgorithms2::ABSOLUTE,
317  '\\' );
318 
319  while (rStrm.good())
320  {
321  OString aRdLine;
322  rStrm.ReadLine( aRdLine );
323 
324  // # -> comment
325  // ; -> delimiter between entries ->
326  // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
327  // Leading and trailing blanks are ignored
328  if( !aRdLine.isEmpty() && '#' != aRdLine[0] )
329  {
330  OUString sLine(OStringToOUString(aRdLine, eChrSet));
331 
332  sal_Int32 nTokenPos = 0;
333  OUString sToSelect( sLine.getToken(0, ';', nTokenPos ) );
334  if( !sToSelect.isEmpty() )
335  {
336  OUString sAlternative = sLine.getToken(0, ';', nTokenPos);
337  OUString sPrimary = sLine.getToken(0, ';', nTokenPos);
338  OUString sSecondary = sLine.getToken(0, ';', nTokenPos);
339  OUString sCase = sLine.getToken(0, ';', nTokenPos);
340  OUString sWordOnly = sLine.getToken(0, ';', nTokenPos);
341 
342  //3.
343  bool bCaseSensitive = !sCase.isEmpty() && sCase != "0";
344  bool bWordOnly = !sWordOnly.isEmpty() && sWordOnly != "0";
345 
346  if (!bCaseSensitive)
347  {
348  aSearchOpt.transliterateFlags |=
349  TransliterationFlags::IGNORE_CASE;
350  }
351  else
352  {
353  aSearchOpt.transliterateFlags &=
354  ~TransliterationFlags::IGNORE_CASE;
355  }
356  if ( bWordOnly)
357  aSearchOpt.searchFlag |= SearchFlags::NORM_WORD_ONLY;
358  else
359  aSearchOpt.searchFlag &= ~SearchFlags::NORM_WORD_ONLY;
360 
361  aSearchOpt.searchString = sToSelect;
362 
363  KillPams();
364  bool bCancel;
365 
366  // todo/mba: assuming that notes shouldn't be searched
367  sal_uLong nRet = Find_Text(aSearchOpt, false/*bSearchInNotes*/, SwDocPositions::Start, SwDocPositions::End, bCancel,
369 
370  if(nRet)
371  {
372  SwTOXMark* pTmpMark = new SwTOXMark(pTOXType);
373  if( !sPrimary.isEmpty() )
374  {
375  pTmpMark->SetPrimaryKey( sPrimary );
376  if( !sSecondary.isEmpty() )
377  pTmpMark->SetSecondaryKey( sSecondary );
378  }
379  if( !sAlternative.isEmpty() )
380  pTmpMark->SetAlternativeText(sAlternative);
381  pTmpMark->SetMainEntry(false);
382  pTmpMark->SetAutoGenerated(true);
383  //4.
384  SwEditShell::Insert(*pTmpMark);
385  }
386  }
387  }
388  }
389  KillPams();
391  }
392  DoUndo(bDoesUndo);
393  EndAllAction();
394 }
395 
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
TOXTypes
Definition: toxe.hxx:39
SVL_DLLPUBLIC bool IsDocument(const OUString &rURL)
void KillPams()
Definition: crsrsh.cxx:1022
bool IsAlternativeText() const
Definition: tox.hxx:577
sal_uLong Find_Text(const i18nutil::SearchOptions2 &rSearchOpt, bool bSearchInNotes, SwDocPositions eStart, SwDocPositions eEnd, bool &bCancel, FindRanges eRng, bool bReplace=false)
Definition: crsrsh.cxx:3490
size_t GetSectionFormatPos(const SwSectionFormat &) const
Definition: edsect.cxx:136
static SW_DLLPUBLIC rtl_TextEncoding GetTextEncoding(SvStream &)
Definition: iodetect.cxx:241
Marks a position in the document model.
Definition: pam.hxx:36
const OUString & GetTOIAutoMarkURL() const
Definition: doc.hxx:950
SwDocShell * GetDocShell()
Definition: doc.hxx:1351
void InsertTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
Insert content table. Renew if required.
Definition: edtox.cxx:123
static SwTOXBase * GetCurTOX(const SwPosition &rPos)
Get current table of contents.
Definition: doctxm.cxx:448
void UpdateTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
update tables of content
Definition: edtox.cxx:151
sal_uIntPtr sal_uLong
std::vector< SwTOXMark * > SwTOXMarks
Definition: tox.hxx:44
void CollectTextMarks(SwTOXMarks &rMarks) const
Definition: tox.hxx:182
sal_Int64 n
virtual SwUndoId EndUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Closes undo block.
Definition: doc.hxx:187
void SetTOIAutoMarkURL(const OUString &rSet)
Definition: edtox.cxx:264
bool IsUpdateTOX() const
Definition: doc.hxx:948
void EndProgress(SwDocShell const *pDocShell)
Definition: mainwn.cxx:92
SwSectionFormat * GetFormat()
Definition: section.hxx:336
OUString const & GetTOIAutoMarkURL() const
AutoMark file.
Definition: edtox.cxx:259
void DoUndo(bool bOn=true)
Undo.
Definition: edws.cxx:200
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:144
virtual bool InsertPoolItem(const SwPaM &rRg, const SfxPoolItem &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr, SwTextAttr **ppNewTextAttr=nullptr)=0
Insert an attribute.
void StartProgress(TranslateId pMessResId, tools::Long nStartValue, tools::Long nEndValue, SwDocShell *pDocShell)
Definition: mainwn.cxx:52
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:315
void EndAllAction()
Definition: edws.cxx:97
static bool IsTOXBaseReadonly(const SwTOXBase &rTOXBase)
Definition: edtox.cxx:93
Array of Undo-history.
Definition: docary.hxx:191
void UpdateSection(size_t const nSect, SwSectionData &, SfxItemSet const *const =nullptr)
Definition: edsect.cxx:156
rtl::Reference< SwDoc > mxDoc
The document; never 0.
Definition: viewsh.hxx:171
void DeleteTOXMark(SwTOXMark const *pMark)
Definition: edtox.cxx:77
SwSectionNode * GetSectionNode()
Definition: section.cxx:922
bool ReadLine(OString &rStr, sal_Int32 nMaxBytesToRead=0xFFFE)
SwTOIKeyType
Definition: toxe.hxx:30
const SwTOXBase * GetTOX(sal_uInt16 nPos) const
Definition: edtox.cxx:230
void SetTOIAutoMarkURL(const OUString &rSet)
Definition: doc.hxx:951
SwRect maVisArea
The modern version of VisArea.
Definition: viewsh.hxx:169
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Delete table of contents.
Definition: doctxm.cxx:524
SwDoc * GetDoc() const
Definition: viewsh.hxx:282
const SwTOXBase * GetCurTOX() const
Get current listing before or at the Cursor.
Definition: edtox.cxx:192
void SetAlternativeText(const OUString &rAlt)
Definition: tox.hxx:580
TransliterationFlags
bool IsUpdateTOX() const
Definition: edtox.cxx:254
void UpdatePageNum()
Calculate PageNumber and insert after formatting.
Definition: doctxm.cxx:1664
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:137
void GetTOIKeys(SwTOIKeyType eTyp, std::vector< OUString > &rArr) const
Key for managing index.
Definition: edtox.cxx:211
virtual SwUndoId StartUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Opens undo block.
const SwTOXBase * GetDefaultTOXBase(TOXTypes eTyp, bool bCreate=false)
Definition: edtox.cxx:112
void SetPrimaryKey(const OUString &rStr)
Definition: tox.hxx:596
bool IsProtect() const
Definition: section.cxx:325
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2248
TransliterationFlags transliterateFlags
bool SetPosAtStartEnd(SwPosition &rPos) const
Definition: doctxm.cxx:835
void SetUpdateTOX(bool bFlag)
Definition: doc.hxx:947
All (only in non-body and selections).
sal_uInt16 GetTOXCount() const
Definition: edtox.cxx:216
size_t size() const
Definition: docary.hxx:87
void GetTOIKeys(SwTOIKeyType eTyp, std::vector< OUString > &rArr, SwRootFrame const &rLayout) const
Definition: doctxm.cxx:86
void SetTableUpdateInProgress(bool bValue)
Definition: rootfrm.hxx:281
ring_container GetRingContainer()
void InvalidateWindows(const SwRect &rRect)
Definition: viewsh.cxx:556
void SetAutoGenerated(bool bSet)
Definition: tox.hxx:151
void SetDefaultTOXBase(const SwTOXBase &rBase)
Definition: edtox.cxx:117
void SetTOXBaseReadonly(const SwTOXBase &rTOXBase, bool bReadonly)
Definition: edtox.cxx:100
const LanguageTag & getLocale()
SvStream * GetInStream()
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:195
void Update(const SfxItemSet *pAttr=nullptr, SwRootFrame const *pLayout=nullptr, const bool _bNewTOX=false)
Collect table of contents content.
Definition: doctxm.cxx:850
static sal_uInt16 GetCurTOXMark(const SwPosition &rPos, SwTOXMarks &)
Get current table of contents Mark.
Definition: doctxm.cxx:117
void SetUpdateTOX(bool bFlag)
After reading file update all content tables.
Definition: edtox.cxx:249
void SetSecondaryKey(const OUString &rStr)
Definition: tox.hxx:602
const SwTOXType * GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
Definition: edtox.cxx:204
virtual void CalcLayout() override
To enable set up of StartActions and EndActions.
Definition: edws.cxx:108
void GetCurTOXMarks(SwTOXMarks &rMarks) const
Get all marks at current SPoint.
Definition: edtox.cxx:88
bool good() const
void Insert(sal_Unicode, bool bOnlyCurrCursor=false)
Edit (all selected ranges).
Definition: editsh.cxx:62
void ApplyAutoMark()
Definition: edtox.cxx:269
SectionType GetType() const
Definition: section.hxx:169
bool Pop(PopMode)
delete cursor
Definition: crsrsh.cxx:2270
bool DoesUndo() const
Definition: edws.cxx:203
void SetDefaultTOXBase(const SwTOXBase &rBase)
Definition: doctxm.cxx:503
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void SvStream & rStrm
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2172
SwSectionFormats & GetSections()
Definition: doc.hxx:1337
void SetMainEntry(bool bSet)
Definition: tox.hxx:154
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Definition: edtox.cxx:197
const SwTOXBase * GetDefaultTOXBase(TOXTypes eTyp, bool bCreate)
Definition: doctxm.cxx:477