Line data Source code
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 : #ifndef EE_SVXACORR_HXX
21 : #define EE_SVXACORR_HXX
22 :
23 : #include <com/sun/star/embed/XStorage.hpp>
24 :
25 : #include <o3tl/sorted_vector.hxx>
26 : #include <tools/rtti.hxx>
27 : #include <i18npool/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"
33 :
34 : #include <map>
35 : #include <set>
36 : #include <boost/unordered_map.hpp>
37 : #include <boost/ptr_container/ptr_map.hpp>
38 :
39 : class CharClass;
40 : class SfxPoolItem;
41 : class SvxAutoCorrect;
42 : class SfxObjectShell;
43 : class SotStorageRef;
44 : class SotStorage;
45 : class Window;
46 :
47 : struct CompareSvStringsISortDtor
48 : {
49 0 : bool operator()( String* const& lhs, String* const& rhs ) const
50 : {
51 0 : return lhs->CompareIgnoreCaseToAscii( *rhs ) == COMPARE_LESS;
52 : }
53 : };
54 :
55 0 : class SvStringsISortDtor
56 : : public o3tl::sorted_vector<String*, CompareSvStringsISortDtor>
57 : {
58 : public:
59 0 : ~SvStringsISortDtor() { DeleteAndDestroyAll(); }
60 : };
61 :
62 : // Auto correct flags
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
77 :
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
81 :
82 : // TODO: handle unicodes > U+FFFF and check users of this class
83 :
84 : // only a mapping class
85 : class EDITENG_DLLPUBLIC SvxAutoCorrDoc
86 : {
87 : public:
88 0 : SvxAutoCorrDoc() {}
89 : virtual ~SvxAutoCorrDoc();
90 :
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;
95 :
96 : virtual sal_Bool SetAttr( xub_StrLen nStt, xub_StrLen nEnd, sal_uInt16 nSlotId,
97 : SfxPoolItem& ) = 0;
98 :
99 : virtual sal_Bool SetINetAttr( xub_StrLen nStt, xub_StrLen nEnd, const String& rURL ) = 0;
100 :
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;
108 :
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
113 : // - FnCptlSttWrd
114 : // - FnCptlSttSntnc
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,
118 : sal_Unicode cChar );
119 :
120 : // which language at the position?
121 : virtual LanguageType GetLanguage( xub_StrLen nPos, sal_Bool bPrevPara = sal_False ) const;
122 : };
123 :
124 :
125 0 : class EDITENG_DLLPUBLIC SvxAutocorrWord
126 : {
127 : String sShort, sLong;
128 : sal_Bool bIsTxtOnly; // Is pure ASCII - Text
129 : public:
130 0 : SvxAutocorrWord( const String& rS, const String& rL, sal_Bool bFlag = sal_True )
131 0 : : sShort( rS ), sLong( rL ), bIsTxtOnly( bFlag )
132 0 : {}
133 :
134 0 : const String& GetShort() const { return sShort; }
135 0 : const String& GetLong() const { return sLong; }
136 0 : sal_Bool IsTextOnly() const { return bIsTxtOnly; }
137 : };
138 :
139 : struct CompareSvxAutocorrWordList
140 : {
141 : bool operator()( SvxAutocorrWord* const& lhs, SvxAutocorrWord* const& rhs ) const;
142 : };
143 :
144 : typedef std::set<SvxAutocorrWord*, CompareSvxAutocorrWordList> SvxAutocorrWordList_Set;
145 : typedef ::boost::unordered_map< ::rtl::OUString, SvxAutocorrWord *,
146 : ::rtl::OUStringHash > SvxAutocorrWordList_Hash;
147 :
148 0 : class EDITENG_DLLPUBLIC SvxAutocorrWordList
149 : {
150 : // only one of these contains the data
151 : mutable SvxAutocorrWordList_Set maSet;
152 : mutable SvxAutocorrWordList_Hash maHash; // key is 'Short'
153 :
154 : bool WordMatches(const SvxAutocorrWord *pFnd,
155 : const String &rTxt,
156 : xub_StrLen &rStt,
157 : xub_StrLen nEndPos) const;
158 : public:
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);
165 : bool empty() const;
166 :
167 : typedef std::vector<SvxAutocorrWord *> Content;
168 : Content getSortedContent() const;
169 :
170 : const SvxAutocorrWord* SearchWordsInList(const String& rTxt, xub_StrLen& rStt, xub_StrLen nEndPos) const;
171 : };
172 :
173 : class EDITENG_DLLPUBLIC SvxAutoCorrectLanguageLists
174 : {
175 : String sShareAutoCorrFile, sUserAutoCorrFile;
176 : // If the AutoCorr file is newer
177 : Date aModifiedDate;
178 : Time aModifiedTime, aLastCheckTime;
179 :
180 : SvStringsISortDtor* pCplStt_ExcptLst;
181 : SvStringsISortDtor* pWrdStt_ExcptLst;
182 : SvxAutocorrWordList* pAutocorr_List;
183 : SvxAutoCorrect& rAutoCorrect;
184 :
185 : long nFlags;
186 :
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,
193 : SotStorageRef& rStg,
194 : sal_Bool bConvert = sal_False);
195 :
196 : sal_Bool MakeBlocklist_Imp( SotStorage& rStg );
197 : void RemoveStream_Imp( const String& rName );
198 : void MakeUserStorage_Impl();
199 :
200 : public:
201 : SvxAutoCorrectLanguageLists( SvxAutoCorrect& rParent,
202 : const String& rShareAutoCorrectFile,
203 : const String& rUserAutoCorrectFile);
204 : ~SvxAutoCorrectLanguageLists();
205 :
206 : // Load, Set, Get - the replacement list
207 : SvxAutocorrWordList* LoadAutocorrWordList();
208 : void SetAutocorrWordList( SvxAutocorrWordList* pList );
209 : const SvxAutocorrWordList* GetAutocorrWordList();
210 :
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);
218 :
219 : // Load, Set, Get the exception list for 2 Capital letters at the
220 : // begining of a word.
221 : SvStringsISortDtor* LoadWrdSttExceptList();
222 : void SaveWrdSttExceptList();
223 : void SetWrdSttExceptList( SvStringsISortDtor* pList );
224 : SvStringsISortDtor* GetWrdSttExceptList();
225 : sal_Bool AddToWrdSttExceptList(const String& rNew);
226 :
227 : // Save word substitutions:
228 : // Store these directly in the storage. The word list is updated
229 : // accordingly!
230 : // - pure Text
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 );
238 : };
239 :
240 : class EDITENG_DLLPUBLIC SvxAutoCorrect
241 : {
242 : friend class SvxAutoCorrectLanguageLists;
243 :
244 : String sShareAutoCorrFile, sUserAutoCorrFile;
245 :
246 : SvxSwAutoFmtFlags aSwFlags; // StarWriter AutoFormat Flags
247 :
248 : // all languages in a table
249 : boost::ptr_map<LanguageType, SvxAutoCorrectLanguageLists>* pLangTable;
250 : std::map<LanguageType, long> aLastFileTable;
251 : CharClass* pCharClass;
252 :
253 : bool bRunNext;
254 :
255 : LanguageType eCharClassLang;
256 :
257 : long nFlags;
258 : sal_Unicode cStartDQuote, cEndDQuote, cStartSQuote, cEndSQuote,
259 : cEmDash, cEnDash;
260 :
261 :
262 : // private methods
263 : SvxAutoCorrectLanguageLists& _GetLanguageList( LanguageType eLang );
264 :
265 : void _GetCharClass( LanguageType eLang );
266 :
267 : protected:
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& ,
271 : String& );
272 :
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!
278 : public:
279 :
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 );
283 :
284 : TYPEINFO();
285 :
286 : SvxAutoCorrect( const String& rShareAutocorrFile,
287 : const String& rUserAutocorrFile );
288 : SvxAutoCorrect( const SvxAutoCorrect& );
289 : virtual ~SvxAutoCorrect();
290 :
291 : // Execute an AutoCorrect.
292 : // Returns what has been executed, according to the above flags
293 : sal_uLong AutoCorrect( SvxAutoCorrDoc& rDoc, const String& rTxt,
294 : xub_StrLen nPos, sal_Unicode cInsChar, sal_Bool bInsert, Window* pFrameWin = NULL );
295 :
296 : // Return for the autotext expansion the previous word,
297 : // AutoCorrect - corresponding algorithm
298 : sal_Bool GetPrevAutoCorrWord( SvxAutoCorrDoc& rDoc, const String& rTxt,
299 : xub_StrLen nPos, String& rWord ) const;
300 :
301 : // Search for or or the words in the replacement table.
302 : // rText - check in this text the words of the list
303 : // rStt - the detected starting position
304 : // nEnd - to check position - as of this item forward
305 : // rLang - Input: in which language is searched
306 : // Output: in which "language list" was it found
307 : const SvxAutocorrWord* SearchWordsInList( const String& rTxt,
308 : xub_StrLen& rStt, xub_StrLen nEndPos,
309 : SvxAutoCorrDoc& rDoc,
310 : LanguageType& rLang );
311 :
312 : // Query/Set the Character for the Quote substitution
313 0 : sal_Unicode GetStartSingleQuote() const { return cStartSQuote; }
314 0 : sal_Unicode GetEndSingleQuote() const { return cEndSQuote; }
315 0 : sal_Unicode GetStartDoubleQuote() const { return cStartDQuote; }
316 0 : sal_Unicode GetEndDoubleQuote() const { return cEndDQuote; }
317 :
318 0 : void SetStartSingleQuote( const sal_Unicode cStart ) { cStartSQuote = cStart; }
319 0 : void SetEndSingleQuote( const sal_Unicode cEnd ) { cEndSQuote = cEnd; }
320 0 : void SetStartDoubleQuote( const sal_Unicode cStart ) { cStartDQuote = cStart; }
321 0 : void SetEndDoubleQuote( const sal_Unicode cEnd ) { cEndDQuote = cEnd; }
322 :
323 : String GetQuote( SvxAutoCorrDoc& rDoc, xub_StrLen nInsPos,
324 : sal_Unicode cInsChar, sal_Bool bSttQuote );
325 : void InsertQuote( SvxAutoCorrDoc& rDoc, xub_StrLen nInsPos,
326 : sal_Unicode cInsChar, sal_Bool bSttQuote, sal_Bool bIns );
327 :
328 : // Query/Set the name of the AutoCorrect file
329 : // the default is "autocorr.dat"
330 : String GetAutoCorrFileName( LanguageType eLang = LANGUAGE_SYSTEM,
331 : sal_Bool bNewFile = sal_False,
332 : sal_Bool bTstUserExist = sal_False ) const;
333 :
334 : // Query/Set the current settings of AutoCorrect
335 48 : long GetFlags() const { return nFlags; }
336 3582 : inline SvxSwAutoFmtFlags& GetSwFlags() { return aSwFlags;}
337 0 : sal_Bool IsAutoCorrFlag( long nFlag ) const
338 0 : { return nFlags & nFlag ? sal_True : sal_False; }
339 : void SetAutoCorrFlag( long nFlag, sal_Bool bOn = sal_True );
340 :
341 : // Load, Set, Get - the replacement list
342 0 : SvxAutocorrWordList* LoadAutocorrWordList(
343 : LanguageType eLang = LANGUAGE_SYSTEM )
344 0 : { return _GetLanguageList( eLang ).LoadAutocorrWordList(); }
345 : const SvxAutocorrWordList* GetAutocorrWordList(
346 : LanguageType eLang = LANGUAGE_SYSTEM )
347 : { return _GetLanguageList( eLang ).GetAutocorrWordList(); }
348 :
349 : // Save word substitutions:
350 : // Save these directly in the storage. The word list is updated
351 : // accordingly!
352 : // - pure Text
353 : sal_Bool PutText( const String& rShort, const String& rLong, LanguageType eLang = LANGUAGE_SYSTEM );
354 : // - Text with attribution (only in the SWG - SWG format!)
355 : sal_Bool PutText( const String& rShort, SfxObjectShell& rShell,
356 : LanguageType eLang = LANGUAGE_SYSTEM )
357 : { return _GetLanguageList( eLang ).PutText(rShort, rShell ); }
358 :
359 : sal_Bool MakeCombinedChanges( std::vector<SvxAutocorrWord>& aNewEntries,
360 : std::vector<SvxAutocorrWord>& aDeleteEntries,
361 : LanguageType eLang = LANGUAGE_SYSTEM );
362 :
363 : // Load, Set, Get - the exception list for capital letters at the
364 : // beginning of a sentence
365 : void SaveCplSttExceptList( LanguageType eLang = LANGUAGE_SYSTEM );
366 0 : SvStringsISortDtor* LoadCplSttExceptList(
367 : LanguageType eLang = LANGUAGE_SYSTEM)
368 0 : { return _GetLanguageList( eLang ).LoadCplSttExceptList(); }
369 0 : const SvStringsISortDtor* GetCplSttExceptList(
370 : LanguageType eLang = LANGUAGE_SYSTEM )
371 0 : { return _GetLanguageList( eLang ).GetCplSttExceptList(); }
372 :
373 : // Adds a single word. The list will be immediately written to the file!
374 : sal_Bool AddCplSttException( const String& rNew,
375 : LanguageType eLang = LANGUAGE_SYSTEM );
376 :
377 : // Load, Set, Get the exception list for 2 Capital letters at the
378 : // beginning of a word.
379 : void SaveWrdSttExceptList( LanguageType eLang = LANGUAGE_SYSTEM );
380 0 : SvStringsISortDtor* LoadWrdSttExceptList(
381 : LanguageType eLang = LANGUAGE_SYSTEM )
382 0 : { return _GetLanguageList( eLang ).LoadWrdSttExceptList(); }
383 0 : const SvStringsISortDtor* GetWrdSttExceptList(
384 : LanguageType eLang = LANGUAGE_SYSTEM )
385 0 : { return _GetLanguageList( eLang ).GetWrdSttExceptList(); }
386 : // Adds a single word. The list will be immediately written to the file!
387 : sal_Bool AddWrtSttException( const String& rNew, LanguageType eLang = LANGUAGE_SYSTEM);
388 :
389 : // Search through the Languages for the entry
390 : sal_Bool FindInWrdSttExceptList( LanguageType eLang, const String& sWord );
391 : sal_Bool FindInCplSttExceptList( LanguageType eLang, const String& sWord,
392 : sal_Bool bAbbreviation = sal_False);
393 :
394 : // Methods for the auto-correction
395 : sal_Bool FnCptlSttWrd( SvxAutoCorrDoc&, const String&,
396 : xub_StrLen nSttPos, xub_StrLen nEndPos,
397 : LanguageType eLang = LANGUAGE_SYSTEM );
398 : sal_Bool FnChgOrdinalNumber( SvxAutoCorrDoc&, const String&,
399 : xub_StrLen nSttPos, xub_StrLen nEndPos,
400 : LanguageType eLang = LANGUAGE_SYSTEM );
401 : sal_Bool FnChgToEnEmDash( SvxAutoCorrDoc&, const String&,
402 : xub_StrLen nSttPos, xub_StrLen nEndPos,
403 : LanguageType eLang = LANGUAGE_SYSTEM );
404 : sal_Bool FnAddNonBrkSpace( SvxAutoCorrDoc&, const String&,
405 : xub_StrLen nSttPos, xub_StrLen nEndPos,
406 : LanguageType eLang = LANGUAGE_SYSTEM );
407 : sal_Bool FnSetINetAttr( SvxAutoCorrDoc&, const String&,
408 : xub_StrLen nSttPos, xub_StrLen nEndPos,
409 : LanguageType eLang = LANGUAGE_SYSTEM );
410 : sal_Bool FnChgWeightUnderl( SvxAutoCorrDoc&, const String&,
411 : xub_StrLen nSttPos, xub_StrLen nEndPos,
412 : LanguageType eLang = LANGUAGE_SYSTEM );
413 : sal_Bool FnCptlSttSntnc( SvxAutoCorrDoc&, const String&, sal_Bool bNormalPos,
414 : xub_StrLen nSttPos, xub_StrLen nEndPos,
415 : LanguageType eLang = LANGUAGE_SYSTEM);
416 : bool FnCorrectCapsLock( SvxAutoCorrDoc&, const String&,
417 : xub_StrLen nSttPos, xub_StrLen nEndPos,
418 : LanguageType eLang = LANGUAGE_SYSTEM );
419 :
420 0 : bool HasRunNext() { return bRunNext; }
421 :
422 : static long GetDefaultFlags();
423 :
424 : // returns sal_True for charcters where the function
425 : // 'SvxAutoCorrect::AutoCorrect' should be called.
426 : // (used to avoid occasional 'collisions' with (Thai) input-sequence-checking)
427 : static sal_Bool IsAutoCorrectChar( sal_Unicode cChar );
428 :
429 : sal_Bool NeedsHardspaceAutocorr( sal_Unicode cChar );
430 :
431 0 : CharClass& GetCharClass( LanguageType eLang )
432 : {
433 0 : if( !pCharClass || eLang != eCharClassLang )
434 0 : _GetCharClass( eLang );
435 0 : return *pCharClass;
436 : }
437 : };
438 :
439 : #endif
440 :
441 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|