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 #ifndef EE_SVXACORR_HXX
21 #define EE_SVXACORR_HXX
23 #include <com/sun/star/embed/XStorage.hpp>
25 #include <o3tl/sorted_vector.hxx>
26 #include <tools/rtti.hxx>
27 #include <i18nlangtag/lang.h>
28 #include <tools/time.hxx>
29 #include <tools/date.hxx>
30 #include <tools/string.hxx>
31 #include <editeng/swafopt.hxx>
32 #include "editeng/editengdllapi.h"
36 #include <boost/unordered_map.hpp>
37 #include <boost/ptr_container/ptr_map.hpp>
47 struct CompareSvStringsISortDtor
49 bool operator()( String
* const& lhs
, String
* const& rhs
) const
51 return lhs
->CompareIgnoreCaseToAscii( *rhs
) == COMPARE_LESS
;
55 class SvStringsISortDtor
56 : public o3tl::sorted_vector
<String
*, CompareSvStringsISortDtor
>
59 ~SvStringsISortDtor() { DeleteAndDestroyAll(); }
63 const long CptlSttSntnc
= 0x00000001; // Capital letters at the beginning of a sentence
64 const long CptlSttWrd
= 0x00000002; // not two Capital letters at the beginning of a word
65 const long AddNonBrkSpace
= 0x00000004; // Add non breaking space before :;?!%
66 const long ChgOrdinalNumber
= 0x00000008; // Ordinal-Number 1st, 2nd,..
67 const long ChgToEnEmDash
= 0x00000010; // - -> Endash/Emdash
68 const long ChgWeightUnderl
= 0x00000020; // * -> Bold, _ -> Underscore
69 const long SetINetAttr
= 0x00000040; // Set INetAttribut
70 const long Autocorrect
= 0x00000080; // Call AutoCorrect
71 const long ChgQuotes
= 0x00000100; // replace double quotes
72 const long SaveWordCplSttLst
= 0x00000200; // Save Auto correction of Capital letter at beginning of sentence.
73 const long SaveWordWrdSttLst
= 0x00000400; // Save Auto correction of 2 Capital letter at beginning of word.
74 const long IgnoreDoubleSpace
= 0x00000800; // Ignore 2 Spaces
75 const long ChgSglQuotes
= 0x00001000; // Replace simple quotes
76 const long CorrectCapsLock
= 0x00002000; // Correct accidental use of cAPS LOCK key
78 const long ChgWordLstLoad
= 0x20000000; // Replacement list loaded
79 const long CplSttLstLoad
= 0x40000000; // Exception list for Capital letters Start loaded
80 const long WrdSttLstLoad
= 0x80000000; // Exception list for Word Start loaded
82 // TODO: handle unicodes > U+FFFF and check users of this class
84 // only a mapping class
85 class EDITENG_DLLPUBLIC SvxAutoCorrDoc
89 virtual ~SvxAutoCorrDoc();
91 virtual sal_Bool
Delete( xub_StrLen nStt
, xub_StrLen nEnd
) = 0;
92 virtual sal_Bool
Insert( xub_StrLen nPos
, const String
& rTxt
) = 0;
93 virtual sal_Bool
Replace( xub_StrLen nPos
, const String
& rTxt
) = 0;
94 virtual sal_Bool
ReplaceRange( xub_StrLen nPos
, xub_StrLen nLen
, const String
& rTxt
) = 0;
96 virtual sal_Bool
SetAttr( xub_StrLen nStt
, xub_StrLen nEnd
, sal_uInt16 nSlotId
,
99 virtual sal_Bool
SetINetAttr( xub_StrLen nStt
, xub_StrLen nEnd
, const String
& rURL
) = 0;
101 // Return the text of a previous paragraph. This must not be empty!
102 // If no paragraph exits or just an empty one, then return 0.
103 // The flag indicates:
104 // TRUE: before the normal insertion position (TRUE)
105 // FALSE: in which the corrected word was inserted.
106 // (Does not to have to be the same paragraph !!!!)
107 virtual const String
* GetPrevPara( sal_Bool bAtNormalPos
) = 0;
109 virtual sal_Bool
ChgAutoCorrWord( xub_StrLen
& rSttPos
, xub_StrLen nEndPos
,
110 SvxAutoCorrect
& rACorrect
,
111 const String
** ppPara
) = 0;
112 // Is called after the change of the signs by the functions
115 // As an option, the words can then be inserted into the exception lists.
116 virtual void SaveCpltSttWord( sal_uLong nFlag
, xub_StrLen nPos
,
117 const String
& rExceptWord
,
120 // which language at the position?
121 virtual LanguageType
GetLanguage( xub_StrLen nPos
, sal_Bool bPrevPara
= sal_False
) const;
125 class EDITENG_DLLPUBLIC SvxAutocorrWord
127 String sShort
, sLong
;
128 sal_Bool bIsTxtOnly
; // Is pure ASCII - Text
130 SvxAutocorrWord( const String
& rS
, const String
& rL
, sal_Bool bFlag
= sal_True
)
131 : sShort( rS
), sLong( rL
), bIsTxtOnly( bFlag
)
134 const String
& GetShort() const { return sShort
; }
135 const String
& GetLong() const { return sLong
; }
136 sal_Bool
IsTextOnly() const { return bIsTxtOnly
; }
139 struct CompareSvxAutocorrWordList
141 bool operator()( SvxAutocorrWord
* const& lhs
, SvxAutocorrWord
* const& rhs
) const;
144 typedef std::set
<SvxAutocorrWord
*, CompareSvxAutocorrWordList
> SvxAutocorrWordList_Set
;
145 typedef ::boost::unordered_map
< OUString
, SvxAutocorrWord
*,
146 OUStringHash
> SvxAutocorrWordList_Hash
;
148 class EDITENG_DLLPUBLIC SvxAutocorrWordList
150 // only one of these contains the data
151 mutable SvxAutocorrWordList_Set maSet
;
152 mutable SvxAutocorrWordList_Hash maHash
; // key is 'Short'
154 bool WordMatches(const SvxAutocorrWord
*pFnd
,
157 xub_StrLen nEndPos
) const;
159 // free any objects still in the set
160 ~SvxAutocorrWordList();
161 void DeleteAndDestroyAll();
162 bool Insert(SvxAutocorrWord
*pWord
);
163 SvxAutocorrWord
* FindAndRemove(SvxAutocorrWord
*pWord
);
164 void LoadEntry(String sWrong
, String sRight
, sal_Bool bOnlyTxt
);
167 typedef std::vector
<SvxAutocorrWord
*> Content
;
168 Content
getSortedContent() const;
170 const SvxAutocorrWord
* SearchWordsInList(const String
& rTxt
, xub_StrLen
& rStt
, xub_StrLen nEndPos
) const;
173 class EDITENG_DLLPUBLIC SvxAutoCorrectLanguageLists
175 String sShareAutoCorrFile
, sUserAutoCorrFile
;
176 // If the AutoCorr file is newer
178 Time aModifiedTime
, aLastCheckTime
;
180 SvStringsISortDtor
* pCplStt_ExcptLst
;
181 SvStringsISortDtor
* pWrdStt_ExcptLst
;
182 SvxAutocorrWordList
* pAutocorr_List
;
183 SvxAutoCorrect
& rAutoCorrect
;
187 sal_Bool
IsFileChanged_Imp();
188 void LoadXMLExceptList_Imp( SvStringsISortDtor
*& rpLst
,
189 const sal_Char
* pStrmName
,
190 SotStorageRef
& rStg
);
191 void SaveExceptList_Imp( const SvStringsISortDtor
& rLst
,
192 const sal_Char
* pStrmName
,
194 sal_Bool bConvert
= sal_False
);
196 sal_Bool
MakeBlocklist_Imp( SotStorage
& rStg
);
197 void RemoveStream_Imp( const String
& rName
);
198 void MakeUserStorage_Impl();
201 SvxAutoCorrectLanguageLists( SvxAutoCorrect
& rParent
,
202 const String
& rShareAutoCorrectFile
,
203 const String
& rUserAutoCorrectFile
);
204 ~SvxAutoCorrectLanguageLists();
206 // Load, Set, Get - the replacement list
207 SvxAutocorrWordList
* LoadAutocorrWordList();
208 void SetAutocorrWordList( SvxAutocorrWordList
* pList
);
209 const SvxAutocorrWordList
* GetAutocorrWordList();
211 // Load, Set, Get - the exception list for Capital letter at the
212 // beginning of a sentence
213 SvStringsISortDtor
* LoadCplSttExceptList();
214 void SaveCplSttExceptList();
215 void SetCplSttExceptList( SvStringsISortDtor
* pList
);
216 SvStringsISortDtor
* GetCplSttExceptList();
217 sal_Bool
AddToCplSttExceptList(const String
& rNew
);
219 // Load, Set, Get the exception list for 2 Capital letters at the
220 // beginning of a word.
221 SvStringsISortDtor
* LoadWrdSttExceptList();
222 void SaveWrdSttExceptList();
223 void SetWrdSttExceptList( SvStringsISortDtor
* pList
);
224 SvStringsISortDtor
* GetWrdSttExceptList();
225 sal_Bool
AddToWrdSttExceptList(const String
& rNew
);
227 // Save word substitutions:
228 // Store these directly in the storage. The word list is updated
231 sal_Bool
PutText( const String
& rShort
, const String
& rLong
);
232 // - Text with attribution (only the SWG - SWG format!)
233 sal_Bool
PutText( const String
& rShort
, SfxObjectShell
& );
234 // - Deleting an entry
235 sal_Bool
DeleteText( const String
& rShort
);
236 // - Make combined changes in one pass
237 sal_Bool
MakeCombinedChanges( std::vector
<SvxAutocorrWord
>& aNewEntries
, std::vector
<SvxAutocorrWord
>& aDeleteEntries
);
240 class EDITENG_DLLPUBLIC SvxAutoCorrect
242 friend class SvxAutoCorrectLanguageLists
;
244 String sShareAutoCorrFile
, sUserAutoCorrFile
;
246 SvxSwAutoFmtFlags aSwFlags
; // StarWriter AutoFormat Flags
248 // all languages in a table
249 boost::ptr_map
<LanguageType
, SvxAutoCorrectLanguageLists
>* pLangTable
;
250 std::map
<LanguageType
, long> aLastFileTable
;
251 CharClass
* pCharClass
;
255 LanguageType eCharClassLang
;
258 sal_Unicode cStartDQuote
, cEndDQuote
, cStartSQuote
, cEndSQuote
,
263 SvxAutoCorrectLanguageLists
& _GetLanguageList( LanguageType eLang
);
265 void _GetCharClass( LanguageType eLang
);
268 // - Text with attribution (only the SWG - SWG format!)
269 // rShort is the stream name - encrypted!
270 virtual sal_Bool
PutText( const com::sun::star::uno::Reference
< com::sun::star::embed::XStorage
>& rStg
, const String
& rFileName
, const String
& rShort
, SfxObjectShell
& ,
273 // required language in the table add if possible only when the file exists
274 sal_Bool
CreateLanguageFile(LanguageType eLang
, sal_Bool bNewFile
= sal_True
);
275 // - Return the replacement text (only for SWG format, all others can be
276 // taken from the word list!)
277 // rShort is the stream name - encrypted!
280 sal_Unicode
GetQuote( sal_Unicode cInsChar
, sal_Bool bSttQuote
,
281 LanguageType eLang
) const;
282 virtual sal_Bool
GetLongText( const com::sun::star::uno::Reference
< com::sun::star::embed::XStorage
>& rStg
, const String
& rFileName
, const String
& rShort
, String
& rLong
);
286 SvxAutoCorrect( const String
& rShareAutocorrFile
,
287 const String
& rUserAutocorrFile
);
288 SvxAutoCorrect( const SvxAutoCorrect
& );
289 virtual ~SvxAutoCorrect();
291 // Execute an AutoCorrect.
292 // Returns what has been executed, according to the above flags
293 // FIXME: this has the horrible flaw that the rTxt must be a reference
294 // to the actual SwTxtNode/EditNode string because it inserts the character
295 // in rDoc and expects that to side-effect rTxt
296 sal_uLong
DoAutoCorrect( SvxAutoCorrDoc
& rDoc
, const String
& rTxt
,
297 xub_StrLen nPos
, sal_Unicode cInsChar
, sal_Bool bInsert
, Window
* pFrameWin
= NULL
);
299 // Return for the autotext expansion the previous word,
300 // AutoCorrect - corresponding algorithm
301 sal_Bool
GetPrevAutoCorrWord( SvxAutoCorrDoc
& rDoc
, const String
& rTxt
,
302 xub_StrLen nPos
, String
& rWord
) const;
304 // Search for or or the words in the replacement table.
305 // rText - check in this text the words of the list
306 // rStt - the detected starting position
307 // nEnd - to check position - as of this item forward
308 // rLang - Input: in which language is searched
309 // Output: in which "language list" was it found
310 const SvxAutocorrWord
* SearchWordsInList( const String
& rTxt
,
311 xub_StrLen
& rStt
, xub_StrLen nEndPos
,
312 SvxAutoCorrDoc
& rDoc
,
313 LanguageType
& rLang
);
315 // Query/Set the Character for the Quote substitution
316 sal_Unicode
GetStartSingleQuote() const { return cStartSQuote
; }
317 sal_Unicode
GetEndSingleQuote() const { return cEndSQuote
; }
318 sal_Unicode
GetStartDoubleQuote() const { return cStartDQuote
; }
319 sal_Unicode
GetEndDoubleQuote() const { return cEndDQuote
; }
321 void SetStartSingleQuote( const sal_Unicode cStart
) { cStartSQuote
= cStart
; }
322 void SetEndSingleQuote( const sal_Unicode cEnd
) { cEndSQuote
= cEnd
; }
323 void SetStartDoubleQuote( const sal_Unicode cStart
) { cStartDQuote
= cStart
; }
324 void SetEndDoubleQuote( const sal_Unicode cEnd
) { cEndDQuote
= cEnd
; }
326 String
GetQuote( SvxAutoCorrDoc
& rDoc
, xub_StrLen nInsPos
,
327 sal_Unicode cInsChar
, sal_Bool bSttQuote
);
328 void InsertQuote( SvxAutoCorrDoc
& rDoc
, xub_StrLen nInsPos
,
329 sal_Unicode cInsChar
, sal_Bool bSttQuote
, sal_Bool bIns
);
331 // Query/Set the name of the AutoCorrect file
332 // the default is "autocorr.dat"
333 OUString
GetAutoCorrFileName( LanguageType eLang
= LANGUAGE_SYSTEM
,
334 sal_Bool bNewFile
= sal_False
,
335 sal_Bool bTstUserExist
= sal_False
) const;
337 // Query/Set the current settings of AutoCorrect
338 long GetFlags() const { return nFlags
; }
339 inline SvxSwAutoFmtFlags
& GetSwFlags() { return aSwFlags
;}
340 sal_Bool
IsAutoCorrFlag( long nFlag
) const
341 { return nFlags
& nFlag
? sal_True
: sal_False
; }
342 void SetAutoCorrFlag( long nFlag
, sal_Bool bOn
= sal_True
);
344 // Load, Set, Get - the replacement list
345 SvxAutocorrWordList
* LoadAutocorrWordList(
346 LanguageType eLang
= LANGUAGE_SYSTEM
)
347 { return _GetLanguageList( eLang
).LoadAutocorrWordList(); }
348 const SvxAutocorrWordList
* GetAutocorrWordList(
349 LanguageType eLang
= LANGUAGE_SYSTEM
)
350 { return _GetLanguageList( eLang
).GetAutocorrWordList(); }
352 // Save word substitutions:
353 // Save these directly in the storage. The word list is updated
356 sal_Bool
PutText( const String
& rShort
, const String
& rLong
, LanguageType eLang
= LANGUAGE_SYSTEM
);
357 // - Text with attribution (only in the SWG - SWG format!)
358 sal_Bool
PutText( const String
& rShort
, SfxObjectShell
& rShell
,
359 LanguageType eLang
= LANGUAGE_SYSTEM
)
360 { return _GetLanguageList( eLang
).PutText(rShort
, rShell
); }
362 sal_Bool
MakeCombinedChanges( std::vector
<SvxAutocorrWord
>& aNewEntries
,
363 std::vector
<SvxAutocorrWord
>& aDeleteEntries
,
364 LanguageType eLang
= LANGUAGE_SYSTEM
);
366 // Load, Set, Get - the exception list for capital letters at the
367 // beginning of a sentence
368 void SaveCplSttExceptList( LanguageType eLang
= LANGUAGE_SYSTEM
);
369 SvStringsISortDtor
* LoadCplSttExceptList(
370 LanguageType eLang
= LANGUAGE_SYSTEM
)
371 { return _GetLanguageList( eLang
).LoadCplSttExceptList(); }
372 const SvStringsISortDtor
* GetCplSttExceptList(
373 LanguageType eLang
= LANGUAGE_SYSTEM
)
374 { return _GetLanguageList( eLang
).GetCplSttExceptList(); }
376 // Adds a single word. The list will be immediately written to the file!
377 sal_Bool
AddCplSttException( const String
& rNew
,
378 LanguageType eLang
= LANGUAGE_SYSTEM
);
380 // Load, Set, Get the exception list for 2 Capital letters at the
381 // beginning of a word.
382 void SaveWrdSttExceptList( LanguageType eLang
= LANGUAGE_SYSTEM
);
383 SvStringsISortDtor
* LoadWrdSttExceptList(
384 LanguageType eLang
= LANGUAGE_SYSTEM
)
385 { return _GetLanguageList( eLang
).LoadWrdSttExceptList(); }
386 const SvStringsISortDtor
* GetWrdSttExceptList(
387 LanguageType eLang
= LANGUAGE_SYSTEM
)
388 { return _GetLanguageList( eLang
).GetWrdSttExceptList(); }
389 // Adds a single word. The list will be immediately written to the file!
390 sal_Bool
AddWrtSttException( const String
& rNew
, LanguageType eLang
= LANGUAGE_SYSTEM
);
392 // Search through the Languages for the entry
393 sal_Bool
FindInWrdSttExceptList( LanguageType eLang
, const String
& sWord
);
394 sal_Bool
FindInCplSttExceptList( LanguageType eLang
, const String
& sWord
,
395 sal_Bool bAbbreviation
= sal_False
);
397 // Methods for the auto-correction
398 sal_Bool
FnCptlSttWrd( SvxAutoCorrDoc
&, const String
&,
399 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
400 LanguageType eLang
= LANGUAGE_SYSTEM
);
401 sal_Bool
FnChgOrdinalNumber( SvxAutoCorrDoc
&, const String
&,
402 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
403 LanguageType eLang
= LANGUAGE_SYSTEM
);
404 sal_Bool
FnChgToEnEmDash( SvxAutoCorrDoc
&, const String
&,
405 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
406 LanguageType eLang
= LANGUAGE_SYSTEM
);
407 sal_Bool
FnAddNonBrkSpace( SvxAutoCorrDoc
&, const String
&,
408 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
409 LanguageType eLang
= LANGUAGE_SYSTEM
);
410 sal_Bool
FnSetINetAttr( SvxAutoCorrDoc
&, const String
&,
411 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
412 LanguageType eLang
= LANGUAGE_SYSTEM
);
413 sal_Bool
FnChgWeightUnderl( SvxAutoCorrDoc
&, const String
&,
414 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
415 LanguageType eLang
= LANGUAGE_SYSTEM
);
416 sal_Bool
FnCptlSttSntnc( SvxAutoCorrDoc
&, const String
&, sal_Bool bNormalPos
,
417 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
418 LanguageType eLang
= LANGUAGE_SYSTEM
);
419 bool FnCorrectCapsLock( SvxAutoCorrDoc
&, const String
&,
420 xub_StrLen nSttPos
, xub_StrLen nEndPos
,
421 LanguageType eLang
= LANGUAGE_SYSTEM
);
423 bool HasRunNext() { return bRunNext
; }
425 static long GetDefaultFlags();
427 // returns sal_True for characters where the function
428 // 'SvxAutoCorrect::AutoCorrect' should be called.
429 // (used to avoid occasional 'collisions' with (Thai) input-sequence-checking)
430 static sal_Bool
IsAutoCorrectChar( sal_Unicode cChar
);
432 sal_Bool
NeedsHardspaceAutocorr( sal_Unicode cChar
);
434 CharClass
& GetCharClass( LanguageType eLang
)
436 if( !pCharClass
|| eLang
!= eCharClassLang
)
437 _GetCharClass( eLang
);
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */