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>
35 #include <IDocumentContentOperations.hxx>
36 #include <IDocumentUndoRedo.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();
56 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
58 const SwPosition
*pStt
= rPaM
.Start(),
63 GetDoc()->getIDocumentContentOperations().InsertPoolItem( aTmp
, rMark
);
65 else if( *pEnd
!= *pStt
)
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 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 );
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 OSL_ENSURE(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());
177 // insert page numbering
178 rTOXSect
.UpdatePageNum();
180 pMyDoc
->GetIDocumentUndoRedo().EndUndo(SwUndoId::TOXCHANGE
, nullptr);
182 ::EndProgress( pDocSh
);
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();
215 for( auto n
= rFormats
.size(); n
; )
217 const SwSection
* pSect
= rFormats
[ --n
]->GetSection();
218 if( SectionType::ToxContent
== pSect
->GetType() &&
219 pSect
->GetFormat()->GetSectionNode() )
225 const SwTOXBase
* SwEditShell::GetTOX( sal_uInt16 nPos
) const
227 const SwSectionFormats
& rFormats
= GetDoc()->GetSections();
229 for( const SwSectionFormat
*pFormat
: rFormats
)
231 const SwSection
* pSect
= pFormat
->GetSection();
232 if( SectionType::ToxContent
== pSect
->GetType() &&
233 pSect
->GetFormat()->GetSectionNode() &&
236 OSL_ENSURE( dynamic_cast<const SwTOXBaseSection
*>( pSect
) != nullptr, "no TOXBaseSection!" );
237 return static_cast<const SwTOXBaseSection
*>(pSect
);
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()
267 bool bDoesUndo
= DoesUndo();
269 //1. remove all automatic generated index entries if AutoMarkURL has a
270 // length and the file exists
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
))
279 const SwTOXType
* pTOXType
= GetTOXType(TOX_INDEX
, 0);
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
);
291 SfxMedium
aMedium( sAutoMarkURL
, StreamMode::STD_READ
);
292 SvStream
& rStrm
= *aMedium
.GetInStream();
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
,
305 SvtSysLocale().GetLanguageTag().getLocale(),
306 nLEV_Other
, nLEV_Longer
, nLEV_Shorter
,
307 TransliterationFlags::NONE
,
308 SearchAlgorithms2::ABSOLUTE
,
314 rStrm
.ReadLine( aRdLine
);
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
);
335 bool bCaseSensitive
= !sCase
.isEmpty() && sCase
!= "0";
336 bool bWordOnly
= !sWordOnly
.isEmpty() && sWordOnly
!= "0";
340 aSearchOpt
.transliterateFlags
|=
341 TransliterationFlags::IGNORE_CASE
;
345 aSearchOpt
.transliterateFlags
&=
346 ~TransliterationFlags::IGNORE_CASE
;
349 aSearchOpt
.searchFlag
|= SearchFlags::NORM_WORD_ONLY
;
351 aSearchOpt
.searchFlag
&= ~SearchFlags::NORM_WORD_ONLY
;
353 aSearchOpt
.searchString
= sToSelect
;
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
);
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);
376 SwEditShell::Insert(*pTmpMark
);
382 Pop(PopMode::DeleteCurrent
);
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */