Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef EE_SVXACORR_HXX
30 : : #define EE_SVXACORR_HXX
31 : :
32 : : #include <com/sun/star/embed/XStorage.hpp>
33 : :
34 : : #include <o3tl/sorted_vector.hxx>
35 : : #include <tools/rtti.hxx>
36 : : #include <i18npool/lang.h>
37 : : #include <tools/time.hxx>
38 : : #include <tools/date.hxx>
39 : : #include <tools/string.hxx>
40 : : #include <editeng/swafopt.hxx>
41 : : #include "editeng/editengdllapi.h"
42 : :
43 : : #include <map>
44 : : #include <set>
45 : : #include <boost/ptr_container/ptr_map.hpp>
46 : :
47 : : class CharClass;
48 : : class SfxPoolItem;
49 : : class SvxAutoCorrect;
50 : : class SfxObjectShell;
51 : : class SotStorageRef;
52 : : class SotStorage;
53 : : class Window;
54 : :
55 : : struct CompareSvStringsISortDtor
56 : : {
57 : 0 : bool operator()( String* const& lhs, String* const& rhs ) const
58 : : {
59 : 0 : return lhs->CompareIgnoreCaseToAscii( *rhs ) == COMPARE_LESS;
60 : : }
61 : : };
62 : :
63 : 0 : class SvStringsISortDtor
64 : : : public o3tl::sorted_vector<String*, CompareSvStringsISortDtor>
65 : : {
66 : : public:
67 [ # # ]: 0 : ~SvStringsISortDtor() { DeleteAndDestroyAll(); }
68 : : };
69 : :
70 : : // Auto correct flags
71 : : const long CptlSttSntnc = 0x00000001; // Capital letters at the beginning of a sentence
72 : : const long CptlSttWrd = 0x00000002; // not two Capital letters at the beginning of a word
73 : : const long AddNonBrkSpace = 0x00000004; // Add non breaking space before :;?!%
74 : : const long ChgOrdinalNumber = 0x00000008; // Ordinal-Number 1st, 2nd,..
75 : : const long ChgToEnEmDash = 0x00000010; // - -> Endash/Emdash
76 : : const long ChgWeightUnderl = 0x00000020; // * -> Bold, _ -> Underscore
77 : : const long SetINetAttr = 0x00000040; // Set INetAttribut
78 : : const long Autocorrect = 0x00000080; // Call AutoCorrect
79 : : const long ChgQuotes = 0x00000100; // replace double quotes
80 : : const long SaveWordCplSttLst= 0x00000200; // Save Auto correction of Capital letter at beginning of sentence.
81 : : const long SaveWordWrdSttLst= 0x00000400; // Save Auto correction of 2 Capital letter at beginning of word.
82 : : const long IgnoreDoubleSpace= 0x00000800; // Ignore 2 Spaces
83 : : const long ChgSglQuotes = 0x00001000; // Replace simple quotes
84 : : const long CorrectCapsLock = 0x00002000; // Correct accidental use of cAPS LOCK key
85 : :
86 : : const long ChgWordLstLoad = 0x20000000; // Replacement list loaded
87 : : const long CplSttLstLoad = 0x40000000; // Exception list for Capital letters Start loaded
88 : : const long WrdSttLstLoad = 0x80000000; // Exception list for Word Start loaded
89 : :
90 : : // TODO: handle unicodes > U+FFFF and check users of this class
91 : :
92 : : // only a mapping class
93 : : class EDITENG_DLLPUBLIC SvxAutoCorrDoc
94 : : {
95 : : public:
96 : 0 : SvxAutoCorrDoc() {}
97 : : virtual ~SvxAutoCorrDoc();
98 : :
99 : : virtual sal_Bool Delete( xub_StrLen nStt, xub_StrLen nEnd ) = 0;
100 : : virtual sal_Bool Insert( xub_StrLen nPos, const String& rTxt ) = 0;
101 : : virtual sal_Bool Replace( xub_StrLen nPos, const String& rTxt ) = 0;
102 : : virtual sal_Bool ReplaceRange( xub_StrLen nPos, xub_StrLen nLen, const String& rTxt ) = 0;
103 : :
104 : : virtual sal_Bool SetAttr( xub_StrLen nStt, xub_StrLen nEnd, sal_uInt16 nSlotId,
105 : : SfxPoolItem& ) = 0;
106 : :
107 : : virtual sal_Bool SetINetAttr( xub_StrLen nStt, xub_StrLen nEnd, const String& rURL ) = 0;
108 : :
109 : : // Return the text of a previous paragraph. This must not be empty!
110 : : // If no paragraph exits or just an empty one, then return 0.
111 : : // The flag indicates:
112 : : // TRUE: before the normal insertion position (TRUE)
113 : : // FALSE: in which the corrected word was inserted.
114 : : // (Does not to have to be the same paragraph !!!!)
115 : : virtual const String* GetPrevPara( sal_Bool bAtNormalPos ) = 0;
116 : :
117 : : virtual sal_Bool ChgAutoCorrWord( xub_StrLen& rSttPos, xub_StrLen nEndPos,
118 : : SvxAutoCorrect& rACorrect,
119 : : const String** ppPara ) = 0;
120 : : // Is called after the change of the signs by the functions
121 : : // - FnCptlSttWrd
122 : : // - FnCptlSttSntnc
123 : : // As an option, the words can then be inserted into the exception lists.
124 : : virtual void SaveCpltSttWord( sal_uLong nFlag, xub_StrLen nPos,
125 : : const String& rExceptWord,
126 : : sal_Unicode cChar );
127 : :
128 : : // which language at the position?
129 : : virtual LanguageType GetLanguage( xub_StrLen nPos, sal_Bool bPrevPara = sal_False ) const;
130 : : };
131 : :
132 : :
133 [ # # ]: 0 : class EDITENG_DLLPUBLIC SvxAutocorrWord
134 : : {
135 : : String sShort, sLong;
136 : : sal_Bool bIsTxtOnly; // Is pure ASCII - Text
137 : : public:
138 : 0 : SvxAutocorrWord( const String& rS, const String& rL, sal_Bool bFlag = sal_True )
139 [ # # ]: 0 : : sShort( rS ), sLong( rL ), bIsTxtOnly( bFlag )
140 : 0 : {}
141 : :
142 : 0 : const String& GetShort() const { return sShort; }
143 : 0 : const String& GetLong() const { return sLong; }
144 : 0 : sal_Bool IsTextOnly() const { return bIsTxtOnly; }
145 : : };
146 : :
147 : : struct CompareSvxAutocorrWordList
148 : : {
149 : : bool operator()( SvxAutocorrWord* const& lhs, SvxAutocorrWord* const& rhs ) const;
150 : : };
151 : 0 : class EDITENG_DLLPUBLIC SvxAutocorrWordList : public std::set<SvxAutocorrWord*, CompareSvxAutocorrWordList>
152 : : {
153 : : public:
154 : : // free any objects still in the set
155 : : ~SvxAutocorrWordList();
156 : : void DeleteAndDestroyAll();
157 : : };
158 : :
159 : : class EDITENG_DLLPUBLIC SvxAutoCorrectLanguageLists
160 : : {
161 : : String sShareAutoCorrFile, sUserAutoCorrFile;
162 : : // If the AutoCorr file is newer
163 : : Date aModifiedDate;
164 : : Time aModifiedTime, aLastCheckTime;
165 : :
166 : : SvStringsISortDtor* pCplStt_ExcptLst;
167 : : SvStringsISortDtor* pWrdStt_ExcptLst;
168 : : SvxAutocorrWordList* pAutocorr_List;
169 : : SvxAutoCorrect& rAutoCorrect;
170 : :
171 : : long nFlags;
172 : :
173 : : sal_Bool IsFileChanged_Imp();
174 : : void LoadXMLExceptList_Imp( SvStringsISortDtor*& rpLst,
175 : : const sal_Char* pStrmName,
176 : : SotStorageRef& rStg);
177 : : void SaveExceptList_Imp( const SvStringsISortDtor& rLst,
178 : : const sal_Char* pStrmName,
179 : : SotStorageRef& rStg,
180 : : sal_Bool bConvert = sal_False);
181 : :
182 : : sal_Bool MakeBlocklist_Imp( SotStorage& rStg );
183 : : void RemoveStream_Imp( const String& rName );
184 : : void MakeUserStorage_Impl();
185 : :
186 : : public:
187 : : SvxAutoCorrectLanguageLists( SvxAutoCorrect& rParent,
188 : : const String& rShareAutoCorrectFile,
189 : : const String& rUserAutoCorrectFile);
190 : : ~SvxAutoCorrectLanguageLists();
191 : :
192 : : // Load, Set, Get - the replacement list
193 : : SvxAutocorrWordList* LoadAutocorrWordList();
194 : : void SetAutocorrWordList( SvxAutocorrWordList* pList );
195 : : const SvxAutocorrWordList* GetAutocorrWordList();
196 : :
197 : : // Load, Set, Get - the exception list for Capital letter at the
198 : : // beginning of a sentence
199 : : SvStringsISortDtor* LoadCplSttExceptList();
200 : : void SaveCplSttExceptList();
201 : : void SetCplSttExceptList( SvStringsISortDtor* pList );
202 : : SvStringsISortDtor* GetCplSttExceptList();
203 : : sal_Bool AddToCplSttExceptList(const String& rNew);
204 : :
205 : : // Load, Set, Get the exception list for 2 Capital letters at the
206 : : // begining of a word.
207 : : SvStringsISortDtor* LoadWrdSttExceptList();
208 : : void SaveWrdSttExceptList();
209 : : void SetWrdSttExceptList( SvStringsISortDtor* pList );
210 : : SvStringsISortDtor* GetWrdSttExceptList();
211 : : sal_Bool AddToWrdSttExceptList(const String& rNew);
212 : :
213 : : // Save word substitutions:
214 : : // Store these directly in the storage. The word list is updated
215 : : // accordingly!
216 : : // - pure Text
217 : : sal_Bool PutText( const String& rShort, const String& rLong );
218 : : // - Text with attribution (only the SWG - SWG format!)
219 : : sal_Bool PutText( const String& rShort, SfxObjectShell& );
220 : : // - Deleting an entry
221 : : sal_Bool DeleteText( const String& rShort );
222 : : };
223 : :
224 : :
225 : : class EDITENG_DLLPUBLIC SvxAutoCorrect
226 : : {
227 : : friend class SvxAutoCorrectLanguageLists;
228 : :
229 : : String sShareAutoCorrFile, sUserAutoCorrFile;
230 : :
231 : : SvxSwAutoFmtFlags aSwFlags; // StarWriter AutoFormat Flags
232 : :
233 : : // all languages in a table
234 : : boost::ptr_map<LanguageType, SvxAutoCorrectLanguageLists>* pLangTable;
235 : : std::map<LanguageType, long> aLastFileTable;
236 : : CharClass* pCharClass;
237 : :
238 : : bool bRunNext;
239 : :
240 : : LanguageType eCharClassLang;
241 : :
242 : : long nFlags;
243 : : sal_Unicode cStartDQuote, cEndDQuote, cStartSQuote, cEndSQuote,
244 : : cEmDash, cEnDash;
245 : :
246 : :
247 : : // private methods
248 : : SvxAutoCorrectLanguageLists& _GetLanguageList( LanguageType eLang );
249 : :
250 : : void _GetCharClass( LanguageType eLang );
251 : :
252 : : protected:
253 : : // - Text with attribution (only the SWG - SWG format!)
254 : : // rShort is the stream name - encrypted!
255 : : virtual sal_Bool PutText( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rStg, const String& rFileName, const String& rShort, SfxObjectShell& ,
256 : : String& );
257 : :
258 : : // required language in the table add if possible only when the file exists
259 : : sal_Bool CreateLanguageFile(LanguageType eLang, sal_Bool bNewFile = sal_True);
260 : : // - Return the replacement text (only for SWG format, all others can be
261 : : // taken from the word list!)
262 : : // rShort is the stream name - encrypted!
263 : : public:
264 : :
265 : : sal_Unicode GetQuote( sal_Unicode cInsChar, sal_Bool bSttQuote,
266 : : LanguageType eLang ) const;
267 : : virtual sal_Bool GetLongText( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rStg, const String& rFileName, const String& rShort, String& rLong );
268 : :
269 : : TYPEINFO();
270 : :
271 : : SvxAutoCorrect( const String& rShareAutocorrFile,
272 : : const String& rUserAutocorrFile );
273 : : SvxAutoCorrect( const SvxAutoCorrect& );
274 : : virtual ~SvxAutoCorrect();
275 : :
276 : : // Execute an AutoCorrect.
277 : : // Returns what has been executed, according to the above flags
278 : : sal_uLong AutoCorrect( SvxAutoCorrDoc& rDoc, const String& rTxt,
279 : : xub_StrLen nPos, sal_Unicode cInsChar, sal_Bool bInsert, Window* pFrameWin = NULL );
280 : :
281 : : // Return for the autotext expansion the previous word,
282 : : // AutoCorrect - corresponding algorithm
283 : : sal_Bool GetPrevAutoCorrWord( SvxAutoCorrDoc& rDoc, const String& rTxt,
284 : : xub_StrLen nPos, String& rWord ) const;
285 : :
286 : : // Search for or or the words in the replacement table.
287 : : // rText - check in this text the words of the list
288 : : // rStt - the detected starting position
289 : : // nEnd - to check position - as of this item forward
290 : : // rLang - Input: in which language is searched
291 : : // Output: in which "language list" was it found
292 : : const SvxAutocorrWord* SearchWordsInList( const String& rTxt,
293 : : xub_StrLen& rStt, xub_StrLen nEndPos,
294 : : SvxAutoCorrDoc& rDoc,
295 : : LanguageType& rLang );
296 : :
297 : : // Query/Set the Character for the Quote substitution
298 : 0 : sal_Unicode GetStartSingleQuote() const { return cStartSQuote; }
299 : 0 : sal_Unicode GetEndSingleQuote() const { return cEndSQuote; }
300 : 0 : sal_Unicode GetStartDoubleQuote() const { return cStartDQuote; }
301 : 0 : sal_Unicode GetEndDoubleQuote() const { return cEndDQuote; }
302 : :
303 : 0 : void SetStartSingleQuote( const sal_Unicode cStart ) { cStartSQuote = cStart; }
304 : 0 : void SetEndSingleQuote( const sal_Unicode cEnd ) { cEndSQuote = cEnd; }
305 : 0 : void SetStartDoubleQuote( const sal_Unicode cStart ) { cStartDQuote = cStart; }
306 : 0 : void SetEndDoubleQuote( const sal_Unicode cEnd ) { cEndDQuote = cEnd; }
307 : :
308 : : String GetQuote( SvxAutoCorrDoc& rDoc, xub_StrLen nInsPos,
309 : : sal_Unicode cInsChar, sal_Bool bSttQuote );
310 : : void InsertQuote( SvxAutoCorrDoc& rDoc, xub_StrLen nInsPos,
311 : : sal_Unicode cInsChar, sal_Bool bSttQuote, sal_Bool bIns );
312 : :
313 : : // Query/Set the name of the AutoCorrect file
314 : : // the default is "autocorr.dat"
315 : : String GetAutoCorrFileName( LanguageType eLang = LANGUAGE_SYSTEM,
316 : : sal_Bool bNewFile = sal_False,
317 : : sal_Bool bTstUserExist = sal_False ) const;
318 : :
319 : : // Query/Set the current settings of AutoCorrect
320 : 146 : long GetFlags() const { return nFlags; }
321 : 15950 : inline SvxSwAutoFmtFlags& GetSwFlags() { return aSwFlags;}
322 : 0 : sal_Bool IsAutoCorrFlag( long nFlag ) const
323 [ # # ]: 0 : { return nFlags & nFlag ? sal_True : sal_False; }
324 : : void SetAutoCorrFlag( long nFlag, sal_Bool bOn = sal_True );
325 : :
326 : : // Load, Set, Get - the replacement list
327 : 0 : SvxAutocorrWordList* LoadAutocorrWordList(
328 : : LanguageType eLang = LANGUAGE_SYSTEM )
329 : 0 : { return _GetLanguageList( eLang ).LoadAutocorrWordList(); }
330 : : const SvxAutocorrWordList* GetAutocorrWordList(
331 : : LanguageType eLang = LANGUAGE_SYSTEM )
332 : : { return _GetLanguageList( eLang ).GetAutocorrWordList(); }
333 : :
334 : : // Save word substitutions:
335 : : // Save these directly in the storage. The word list is updated
336 : : // accordingly!
337 : : // - pure Text
338 : : sal_Bool PutText( const String& rShort, const String& rLong, LanguageType eLang = LANGUAGE_SYSTEM );
339 : : // - Text with attribution (only in the SWG - SWG format!)
340 : 0 : sal_Bool PutText( const String& rShort, SfxObjectShell& rShell,
341 : : LanguageType eLang = LANGUAGE_SYSTEM )
342 : 0 : { return _GetLanguageList( eLang ).PutText(rShort, rShell ); }
343 : :
344 : : // - Delete a entry
345 : : sal_Bool DeleteText( const String& rShort, LanguageType eLang = LANGUAGE_SYSTEM);
346 : :
347 : : // Load, Set, Get - the exception list for capital letters at the
348 : : // beginning of a sentence
349 : : void SaveCplSttExceptList( LanguageType eLang = LANGUAGE_SYSTEM );
350 : 0 : SvStringsISortDtor* LoadCplSttExceptList(
351 : : LanguageType eLang = LANGUAGE_SYSTEM)
352 : 0 : { return _GetLanguageList( eLang ).LoadCplSttExceptList(); }
353 : 0 : const SvStringsISortDtor* GetCplSttExceptList(
354 : : LanguageType eLang = LANGUAGE_SYSTEM )
355 : 0 : { return _GetLanguageList( eLang ).GetCplSttExceptList(); }
356 : :
357 : : // Adds a single word. The list will be immediately written to the file!
358 : : sal_Bool AddCplSttException( const String& rNew,
359 : : LanguageType eLang = LANGUAGE_SYSTEM );
360 : :
361 : : // Load, Set, Get the exception list for 2 Capital letters at the
362 : : // beginning of a word.
363 : : void SaveWrdSttExceptList( LanguageType eLang = LANGUAGE_SYSTEM );
364 : 0 : SvStringsISortDtor* LoadWrdSttExceptList(
365 : : LanguageType eLang = LANGUAGE_SYSTEM )
366 : 0 : { return _GetLanguageList( eLang ).LoadWrdSttExceptList(); }
367 : 0 : const SvStringsISortDtor* GetWrdSttExceptList(
368 : : LanguageType eLang = LANGUAGE_SYSTEM )
369 : 0 : { return _GetLanguageList( eLang ).GetWrdSttExceptList(); }
370 : : // Adds a single word. The list will be immediately written to the file!
371 : : sal_Bool AddWrtSttException( const String& rNew, LanguageType eLang = LANGUAGE_SYSTEM);
372 : :
373 : : // Search through the Languages for the entry
374 : : sal_Bool FindInWrdSttExceptList( LanguageType eLang, const String& sWord );
375 : : sal_Bool FindInCplSttExceptList( LanguageType eLang, const String& sWord,
376 : : sal_Bool bAbbreviation = sal_False);
377 : :
378 : : // Methods for the auto-correction
379 : : sal_Bool FnCptlSttWrd( SvxAutoCorrDoc&, const String&,
380 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
381 : : LanguageType eLang = LANGUAGE_SYSTEM );
382 : : sal_Bool FnChgOrdinalNumber( SvxAutoCorrDoc&, const String&,
383 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
384 : : LanguageType eLang = LANGUAGE_SYSTEM );
385 : : sal_Bool FnChgToEnEmDash( SvxAutoCorrDoc&, const String&,
386 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
387 : : LanguageType eLang = LANGUAGE_SYSTEM );
388 : : sal_Bool FnAddNonBrkSpace( SvxAutoCorrDoc&, const String&,
389 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
390 : : LanguageType eLang = LANGUAGE_SYSTEM );
391 : : sal_Bool FnSetINetAttr( SvxAutoCorrDoc&, const String&,
392 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
393 : : LanguageType eLang = LANGUAGE_SYSTEM );
394 : : sal_Bool FnChgWeightUnderl( SvxAutoCorrDoc&, const String&,
395 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
396 : : LanguageType eLang = LANGUAGE_SYSTEM );
397 : : sal_Bool FnCptlSttSntnc( SvxAutoCorrDoc&, const String&, sal_Bool bNormalPos,
398 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
399 : : LanguageType eLang = LANGUAGE_SYSTEM);
400 : : bool FnCorrectCapsLock( SvxAutoCorrDoc&, const String&,
401 : : xub_StrLen nSttPos, xub_StrLen nEndPos,
402 : : LanguageType eLang = LANGUAGE_SYSTEM );
403 : :
404 : 0 : bool HasRunNext() { return bRunNext; }
405 : :
406 : : static long GetDefaultFlags();
407 : :
408 : : // returns sal_True for charcters where the function
409 : : // 'SvxAutoCorrect::AutoCorrect' should be called.
410 : : // (used to avoid occasional 'collisions' with (Thai) input-sequence-checking)
411 : : static sal_Bool IsAutoCorrectChar( sal_Unicode cChar );
412 : :
413 : : sal_Bool NeedsHardspaceAutocorr( sal_Unicode cChar );
414 : :
415 : 0 : CharClass& GetCharClass( LanguageType eLang )
416 : : {
417 [ # # ][ # # ]: 0 : if( !pCharClass || eLang != eCharClassLang )
418 : 0 : _GetCharClass( eLang );
419 : 0 : return *pCharClass;
420 : : }
421 : : };
422 : :
423 : : #endif
424 : :
425 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|