nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / core / edit / edtox.cxx
blob46ab7e7bcbf82e4a2095200489b1c35372cb8475
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 <com/sun/star/util/SearchAlgorithms2.hpp>
21 #include <com/sun/star/util/SearchFlags.hpp>
22 #include <i18nlangtag/languagetag.hxx>
23 #include <i18nutil/transliteration.hxx>
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>
30 #include <sfx2/docfile.hxx>
32 #include <swtypes.hxx>
33 #include <editsh.hxx>
34 #include <doc.hxx>
35 #include <IDocumentContentOperations.hxx>
36 #include <IDocumentUndoRedo.hxx>
37 #include <pam.hxx>
38 #include <swundo.hxx>
39 #include <tox.hxx>
40 #include <doctxm.hxx>
41 #include <docary.hxx>
42 #include <mdiexp.hxx>
43 #include <strings.hrc>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::i18n;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::util;
50 // Add/delete listing markers to a document
52 void SwEditShell::Insert(const SwTOXMark& rMark)
54 bool bInsAtPos = rMark.IsAlternativeText();
55 StartAllAction();
56 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
58 const SwPosition *pStt = rPaM.Start(),
59 *pEnd = rPaM.End();
60 if( bInsAtPos )
62 SwPaM aTmp( *pStt );
63 GetDoc()->getIDocumentContentOperations().InsertPoolItem( aTmp, rMark );
65 else if( *pEnd != *pStt )
67 GetDoc()->getIDocumentContentOperations().InsertPoolItem(
68 rPaM, rMark, SetAttrMode::DONTEXPAND );
72 EndAllAction();
75 void SwEditShell::DeleteTOXMark( SwTOXMark const * pMark )
77 CurrShell aCurr( this );
78 StartAllAction();
80 mxDoc->DeleteTOXMark( pMark );
82 EndAllAction();
85 /// Collect all listing markers
86 void SwEditShell::GetCurTOXMarks(SwTOXMarks& rMarks) const
88 SwDoc::GetCurTOXMark( *GetCursor()->Start(), rMarks );
91 bool SwEditShell::IsTOXBaseReadonly(const SwTOXBase& rTOXBase)
93 OSL_ENSURE( dynamic_cast<const SwTOXBaseSection*>( &rTOXBase) != nullptr, "no TOXBaseSection!" );
94 const SwTOXBaseSection& rTOXSect = static_cast<const SwTOXBaseSection&>(rTOXBase);
95 return rTOXSect.IsProtect();
98 void SwEditShell::SetTOXBaseReadonly(const SwTOXBase& rTOXBase, bool bReadonly)
100 OSL_ENSURE( dynamic_cast<const SwTOXBaseSection*>( &rTOXBase) != nullptr, "no TOXBaseSection!" );
101 const SwTOXBaseSection& rTOXSect = static_cast<const SwTOXBaseSection&>(rTOXBase);
102 const_cast<SwTOXBase&>(rTOXBase).SetProtected(bReadonly);
103 OSL_ENSURE( rTOXSect.SwSection::GetType() == SectionType::ToxContent, "not a TOXContentSection" );
105 SwSectionData aSectionData(rTOXSect);
106 aSectionData.SetProtectFlag(bReadonly);
107 UpdateSection( GetSectionFormatPos( *rTOXSect.GetFormat() ), aSectionData );
110 const SwTOXBase* SwEditShell::GetDefaultTOXBase( TOXTypes eTyp, bool bCreate )
112 return GetDoc()->GetDefaultTOXBase( eTyp, bCreate );
115 void SwEditShell::SetDefaultTOXBase(const SwTOXBase& rBase)
117 GetDoc()->SetDefaultTOXBase(rBase);
120 /// Insert listing and create content
121 void SwEditShell::InsertTableOf( const SwTOXBase& rTOX, const SfxItemSet* pSet )
123 CurrShell aCurr( this );
124 StartAllAction();
126 SwDocShell* pDocSh = GetDoc()->GetDocShell();
127 ::StartProgress( STR_STATSTR_TOX_INSERT, 0, 0, pDocSh );
129 // Insert listing
130 const SwTOXBaseSection* pTOX = mxDoc->InsertTableOf(
131 *GetCursor()->GetPoint(), rTOX, pSet, true, GetLayout() );
132 OSL_ENSURE(pTOX, "No current TOX");
134 // start formatting
135 CalcLayout();
137 // insert page numbering
138 const_cast<SwTOXBaseSection*>(pTOX)->UpdatePageNum();
140 pTOX->SetPosAtStartEnd( *GetCursor()->GetPoint() );
142 // Fix for empty listing
143 InvalidateWindows( maVisArea );
144 ::EndProgress( pDocSh );
145 EndAllAction();
148 /// update tables of content
149 void SwEditShell::UpdateTableOf(const SwTOXBase& rTOX, const SfxItemSet* pSet)
151 assert(dynamic_cast<const SwTOXBaseSection*>(&rTOX) && "no TOXBaseSection!");
152 SwTOXBaseSection& rTOXSect = static_cast<SwTOXBaseSection&>(const_cast<SwTOXBase&>(rTOX));
153 if (!rTOXSect.GetFormat()->GetSectionNode())
154 return;
156 SwDoc* pMyDoc = GetDoc();
157 SwDocShell* pDocSh = pMyDoc->GetDocShell();
159 bool bInIndex = &rTOX == GetCurTOX();
160 CurrShell aCurr( this );
161 StartAllAction();
163 ::StartProgress( STR_STATSTR_TOX_UPDATE, 0, 0, pDocSh );
165 pMyDoc->GetIDocumentUndoRedo().StartUndo(SwUndoId::TOXCHANGE, nullptr);
167 // create listing stub
168 rTOXSect.Update(pSet, GetLayout());
170 // correct Cursor
171 if( bInIndex )
172 rTOXSect.SetPosAtStartEnd(*GetCursor()->GetPoint());
174 // start formatting
175 CalcLayout();
177 // insert page numbering
178 rTOXSect.UpdatePageNum();
180 pMyDoc->GetIDocumentUndoRedo().EndUndo(SwUndoId::TOXCHANGE, nullptr);
182 ::EndProgress( pDocSh );
183 EndAllAction();
186 /// Get current listing before or at the Cursor
187 const SwTOXBase* SwEditShell::GetCurTOX() const
189 return SwDoc::GetCurTOX( *GetCursor()->GetPoint() );
192 bool SwEditShell::DeleteTOX( const SwTOXBase& rTOXBase, bool bDelNodes )
194 return GetDoc()->DeleteTOX( rTOXBase, bDelNodes );
197 // manage types of listings
199 const SwTOXType* SwEditShell::GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
201 return mxDoc->GetTOXType(eTyp, nId);
204 // manage keys for the alphabetical index
206 void SwEditShell::GetTOIKeys( SwTOIKeyType eTyp, std::vector<OUString>& rArr ) const
208 GetDoc()->GetTOIKeys( eTyp, rArr, *GetLayout() );
211 sal_uInt16 SwEditShell::GetTOXCount() const
213 const SwSectionFormats& rFormats = GetDoc()->GetSections();
214 sal_uInt16 nRet = 0;
215 for( auto n = rFormats.size(); n; )
217 const SwSection* pSect = rFormats[ --n ]->GetSection();
218 if( SectionType::ToxContent == pSect->GetType() &&
219 pSect->GetFormat()->GetSectionNode() )
220 ++nRet;
222 return nRet;
225 const SwTOXBase* SwEditShell::GetTOX( sal_uInt16 nPos ) const
227 const SwSectionFormats& rFormats = GetDoc()->GetSections();
228 sal_uInt16 nCnt {0};
229 for( const SwSectionFormat *pFormat : rFormats )
231 const SwSection* pSect = pFormat->GetSection();
232 if( SectionType::ToxContent == pSect->GetType() &&
233 pSect->GetFormat()->GetSectionNode() &&
234 nCnt++ == nPos )
236 OSL_ENSURE( dynamic_cast<const SwTOXBaseSection*>( pSect) != nullptr, "no TOXBaseSection!" );
237 return static_cast<const SwTOXBaseSection*>(pSect);
240 return nullptr;
243 /** Update of all listings after reading-in a file */
244 void SwEditShell::SetUpdateTOX( bool bFlag )
246 GetDoc()->SetUpdateTOX( bFlag );
249 bool SwEditShell::IsUpdateTOX() const
251 return GetDoc()->IsUpdateTOX();
254 OUString const & SwEditShell::GetTOIAutoMarkURL() const
256 return GetDoc()->GetTOIAutoMarkURL();
259 void SwEditShell::SetTOIAutoMarkURL(const OUString& rSet)
261 GetDoc()->SetTOIAutoMarkURL(rSet);
264 void SwEditShell::ApplyAutoMark()
266 StartAllAction();
267 bool bDoesUndo = DoesUndo();
268 DoUndo(false);
269 //1. remove all automatic generated index entries if AutoMarkURL has a
270 // length and the file exists
271 //2. load file
272 //3. select all occurrences of the searched words
273 //4. apply index entries
275 OUString sAutoMarkURL(GetDoc()->GetTOIAutoMarkURL());
276 if( !sAutoMarkURL.isEmpty() && FStatHelper::IsDocument( sAutoMarkURL ))
278 //1.
279 const SwTOXType* pTOXType = GetTOXType(TOX_INDEX, 0);
281 SwTOXMarks aMarks;
282 pTOXType->CollectTextMarks(aMarks);
283 for( SwTOXMark* pMark : aMarks )
285 if(pMark->IsAutoGenerated() && pMark->GetTextTOXMark())
286 // mba: test iteration; objects are deleted in iteration
287 DeleteTOXMark(pMark);
290 //2.
291 SfxMedium aMedium( sAutoMarkURL, StreamMode::STD_READ );
292 SvStream& rStrm = *aMedium.GetInStream();
293 Push();
294 rtl_TextEncoding eChrSet = ::osl_getThreadTextEncoding();
296 // SearchOptions to be used in loop below
297 sal_Int32 const nLEV_Other = 2; // -> changedChars;
298 sal_Int32 const nLEV_Longer = 3; //! -> deletedChars;
299 sal_Int32 const nLEV_Shorter = 1; //! -> insertedChars;
301 i18nutil::SearchOptions2 aSearchOpt(
302 SearchAlgorithms_ABSOLUTE,
303 SearchFlags::LEV_RELAXED,
304 "", "",
305 SvtSysLocale().GetLanguageTag().getLocale(),
306 nLEV_Other, nLEV_Longer, nLEV_Shorter,
307 TransliterationFlags::NONE,
308 SearchAlgorithms2::ABSOLUTE,
309 '\\' );
311 while (rStrm.good())
313 OString aRdLine;
314 rStrm.ReadLine( aRdLine );
316 // # -> comment
317 // ; -> delimiter between entries ->
318 // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
319 // Leading and trailing blanks are ignored
320 if( !aRdLine.isEmpty() && '#' != aRdLine[0] )
322 OUString sLine(OStringToOUString(aRdLine, eChrSet));
324 sal_Int32 nTokenPos = 0;
325 OUString sToSelect( sLine.getToken(0, ';', nTokenPos ) );
326 if( !sToSelect.isEmpty() )
328 OUString sAlternative = sLine.getToken(0, ';', nTokenPos);
329 OUString sPrimary = sLine.getToken(0, ';', nTokenPos);
330 OUString sSecondary = sLine.getToken(0, ';', nTokenPos);
331 OUString sCase = sLine.getToken(0, ';', nTokenPos);
332 OUString sWordOnly = sLine.getToken(0, ';', nTokenPos);
334 //3.
335 bool bCaseSensitive = !sCase.isEmpty() && sCase != "0";
336 bool bWordOnly = !sWordOnly.isEmpty() && sWordOnly != "0";
338 if (!bCaseSensitive)
340 aSearchOpt.transliterateFlags |=
341 TransliterationFlags::IGNORE_CASE;
343 else
345 aSearchOpt.transliterateFlags &=
346 ~TransliterationFlags::IGNORE_CASE;
348 if ( bWordOnly)
349 aSearchOpt.searchFlag |= SearchFlags::NORM_WORD_ONLY;
350 else
351 aSearchOpt.searchFlag &= ~SearchFlags::NORM_WORD_ONLY;
353 aSearchOpt.searchString = sToSelect;
355 KillPams();
356 bool bCancel;
358 // todo/mba: assuming that notes shouldn't be searched
359 sal_uLong nRet = Find_Text(aSearchOpt, false/*bSearchInNotes*/, SwDocPositions::Start, SwDocPositions::End, bCancel,
360 FindRanges::InSelAll );
362 if(nRet)
364 SwTOXMark* pTmpMark = new SwTOXMark(pTOXType);
365 if( !sPrimary.isEmpty() )
367 pTmpMark->SetPrimaryKey( sPrimary );
368 if( !sSecondary.isEmpty() )
369 pTmpMark->SetSecondaryKey( sSecondary );
371 if( !sAlternative.isEmpty() )
372 pTmpMark->SetAlternativeText(sAlternative);
373 pTmpMark->SetMainEntry(false);
374 pTmpMark->SetAutoGenerated(true);
375 //4.
376 SwEditShell::Insert(*pTmpMark);
381 KillPams();
382 Pop(PopMode::DeleteCurrent);
384 DoUndo(bDoesUndo);
385 EndAllAction();
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */