1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <rootfrm.hxx>
36 #include <IDocumentContentOperations.hxx>
37 #include <IDocumentUndoRedo.hxx>
44 #include <strings.hrc>
45 #include <iodetect.hxx>
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::lang
;
49 using namespace ::com::sun::star::util
;
51 // Add/delete listing markers to a document
53 void SwEditShell::Insert(const SwTOXMark
& rMark
)
55 bool bInsAtPos
= rMark
.IsAlternativeText();
57 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
59 auto [pStart
, pEnd
] = rPaM
.StartEnd(); // SwPosition*
62 SwPaM
aTmp( *pStart
);
63 GetDoc()->getIDocumentContentOperations().InsertPoolItem( aTmp
, rMark
);
65 else if( *pEnd
!= *pStart
)
67 GetDoc()->getIDocumentContentOperations().InsertPoolItem(
68 rPaM
, rMark
, SetAttrMode::DONTEXPAND
);
75 void SwEditShell::DeleteTOXMark( SwTOXMark
const * pMark
)
77 CurrShell
aCurr( this );
80 mxDoc
->DeleteTOXMark( pMark
);
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 assert( dynamic_cast<const SwTOXBaseSection
*>( &rTOXBase
) && "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 assert( dynamic_cast<const SwTOXBaseSection
*>( &rTOXBase
) && "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 );
126 SwDocShell
* pDocSh
= GetDoc()->GetDocShell();
127 ::StartProgress( STR_STATSTR_TOX_INSERT
, 0, 0, pDocSh
);
130 const SwTOXBaseSection
* pTOX
= mxDoc
->InsertTableOf(
131 *GetCursor()->GetPoint(), rTOX
, pSet
, true, GetLayout() );
132 assert(pTOX
&& "No current TOX");
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
);
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())
156 SwDoc
* pMyDoc
= GetDoc();
157 SwDocShell
* pDocSh
= pMyDoc
->GetDocShell();
159 bool bInIndex
= &rTOX
== GetCurTOX();
160 CurrShell
aCurr( this );
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());
172 rTOXSect
.SetPosAtStartEnd(*GetCursor()->GetPoint());
175 // tdf#139426 ...but allow suppression of AssertFlyPages
176 GetLayout()->SetTableUpdateInProgress(true);
178 GetLayout()->SetTableUpdateInProgress(false);
180 // insert page numbering
181 rTOXSect
.UpdatePageNum();
183 pMyDoc
->GetIDocumentUndoRedo().EndUndo(SwUndoId::TOXCHANGE
, nullptr);
185 ::EndProgress( pDocSh
);
189 /// Get current listing before or at the Cursor
190 const SwTOXBase
* SwEditShell::GetCurTOX() const
192 return SwDoc::GetCurTOX( *GetCursor()->GetPoint() );
195 bool SwEditShell::DeleteTOX( const SwTOXBase
& rTOXBase
, bool bDelNodes
)
197 return GetDoc()->DeleteTOX( rTOXBase
, bDelNodes
);
200 // manage types of listings
202 const SwTOXType
* SwEditShell::GetTOXType(TOXTypes eTyp
, sal_uInt16 nId
) const
204 return mxDoc
->GetTOXType(eTyp
, nId
);
207 // manage keys for the alphabetical index
209 void SwEditShell::GetTOIKeys( SwTOIKeyType eTyp
, std::vector
<OUString
>& rArr
) const
211 GetDoc()->GetTOIKeys( eTyp
, rArr
, *GetLayout() );
214 sal_uInt16
SwEditShell::GetTOXCount() const
216 const SwSectionFormats
& rFormats
= GetDoc()->GetSections();
218 for( auto n
= rFormats
.size(); n
; )
220 const SwSection
* pSect
= rFormats
[ --n
]->GetSection();
221 if( SectionType::ToxContent
== pSect
->GetType() &&
222 pSect
->GetFormat()->GetSectionNode() )
228 const SwTOXBase
* SwEditShell::GetTOX( sal_uInt16 nPos
) const
230 const SwSectionFormats
& rFormats
= GetDoc()->GetSections();
232 for( const SwSectionFormat
*pFormat
: rFormats
)
234 const SwSection
* pSect
= pFormat
->GetSection();
235 if( SectionType::ToxContent
== pSect
->GetType() &&
236 pSect
->GetFormat()->GetSectionNode() &&
239 assert( dynamic_cast<const SwTOXBaseSection
*>( pSect
) && "no TOXBaseSection!" );
240 return static_cast<const SwTOXBaseSection
*>(pSect
);
246 /** Update of all listings after reading-in a file */
247 void SwEditShell::SetUpdateTOX( bool bFlag
)
249 GetDoc()->SetUpdateTOX( bFlag
);
252 bool SwEditShell::IsUpdateTOX() const
254 return GetDoc()->IsUpdateTOX();
257 OUString
const & SwEditShell::GetTOIAutoMarkURL() const
259 return GetDoc()->GetTOIAutoMarkURL();
262 void SwEditShell::SetTOIAutoMarkURL(const OUString
& rSet
)
264 GetDoc()->SetTOIAutoMarkURL(rSet
);
267 void SwEditShell::ApplyAutoMark()
270 bool bDoesUndo
= DoesUndo();
272 //1. remove all automatic generated index entries if AutoMarkURL has a
273 // length and the file exists
275 //3. select all occurrences of the searched words
276 //4. apply index entries
278 OUString
sAutoMarkURL(GetDoc()->GetTOIAutoMarkURL());
279 if( !sAutoMarkURL
.isEmpty() && FStatHelper::IsDocument( sAutoMarkURL
))
282 const SwTOXType
* pTOXType
= GetTOXType(TOX_INDEX
, 0);
285 pTOXType
->CollectTextMarks(aMarks
);
286 for( SwTOXMark
* pMark
: aMarks
)
288 if(pMark
->IsAutoGenerated() && pMark
->GetTextTOXMark())
289 // mba: test iteration; objects are deleted in iteration
290 DeleteTOXMark(pMark
);
294 SfxMedium
aMedium( sAutoMarkURL
, StreamMode::STD_READ
);
295 SvStream
& rStrm
= *aMedium
.GetInStream();
297 // tdf#106899 - import tox concordance file using the appropriate character set
298 rtl_TextEncoding eChrSet
= SwIoSystem::GetTextEncoding(rStrm
);
299 if (eChrSet
== RTL_TEXTENCODING_DONTKNOW
)
300 eChrSet
= ::osl_getThreadTextEncoding();
302 // SearchOptions to be used in loop below
303 sal_Int32
const nLEV_Other
= 2; // -> changedChars;
304 sal_Int32
const nLEV_Longer
= 3; //! -> deletedChars;
305 sal_Int32
const nLEV_Shorter
= 1; //! -> insertedChars;
307 i18nutil::SearchOptions2
aSearchOpt(
308 SearchFlags::LEV_RELAXED
,
310 SvtSysLocale().GetLanguageTag().getLocale(),
311 nLEV_Other
, nLEV_Longer
, nLEV_Shorter
,
312 TransliterationFlags::NONE
,
313 SearchAlgorithms2::ABSOLUTE
,
316 OStringBuffer aRdLine
;
319 rStrm
.ReadLine( aRdLine
);
322 // ; -> delimiter between entries ->
323 // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
324 // Leading and trailing blanks are ignored
325 if( !aRdLine
.isEmpty() && '#' != aRdLine
[0] )
327 OUString
sLine(OStringToOUString(aRdLine
, eChrSet
));
329 sal_Int32 nTokenPos
= 0;
330 OUString
sToSelect( sLine
.getToken(0, ';', nTokenPos
) );
331 if( !sToSelect
.isEmpty() )
333 OUString sAlternative
= sLine
.getToken(0, ';', nTokenPos
);
334 OUString sPrimary
= sLine
.getToken(0, ';', nTokenPos
);
335 OUString sSecondary
= sLine
.getToken(0, ';', nTokenPos
);
336 OUString sCase
= sLine
.getToken(0, ';', nTokenPos
);
337 OUString sWordOnly
= sLine
.getToken(0, ';', nTokenPos
);
340 bool bCaseSensitive
= !sCase
.isEmpty() && sCase
!= "0";
341 bool bWordOnly
= !sWordOnly
.isEmpty() && sWordOnly
!= "0";
345 aSearchOpt
.transliterateFlags
|=
346 TransliterationFlags::IGNORE_CASE
;
350 aSearchOpt
.transliterateFlags
&=
351 ~TransliterationFlags::IGNORE_CASE
;
354 aSearchOpt
.searchFlag
|= SearchFlags::NORM_WORD_ONLY
;
356 aSearchOpt
.searchFlag
&= ~SearchFlags::NORM_WORD_ONLY
;
358 aSearchOpt
.searchString
= sToSelect
;
363 // todo/mba: assuming that notes shouldn't be searched
364 sal_Int32 nRet
= Find_Text(aSearchOpt
, false/*bSearchInNotes*/, SwDocPositions::Start
, SwDocPositions::End
, bCancel
,
365 FindRanges::InSelAll
);
369 SwTOXMark
* pTmpMark
= new SwTOXMark(pTOXType
);
370 if( !sPrimary
.isEmpty() )
372 pTmpMark
->SetPrimaryKey( sPrimary
);
373 if( !sSecondary
.isEmpty() )
374 pTmpMark
->SetSecondaryKey( sSecondary
);
376 if( !sAlternative
.isEmpty() )
377 pTmpMark
->SetAlternativeText(sAlternative
);
378 pTmpMark
->SetMainEntry(false);
379 pTmpMark
->SetAutoGenerated(true);
381 SwEditShell::Insert(*pTmpMark
);
387 Pop(PopMode::DeleteCurrent
);
393 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */