/src/libreoffice/linguistic/source/gciterator.cxx
Line | Count | Source |
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 | | #include <sal/macros.h> |
21 | | #include <com/sun/star/beans/XPropertySet.hpp> |
22 | | #include <com/sun/star/container/ElementExistException.hpp> |
23 | | #include <com/sun/star/container/XNameAccess.hpp> |
24 | | #include <com/sun/star/configuration/theDefaultProvider.hpp> |
25 | | #include <com/sun/star/i18n/BreakIterator.hpp> |
26 | | #include <com/sun/star/lang/IndexOutOfBoundsException.hpp> |
27 | | #include <com/sun/star/lang/XComponent.hpp> |
28 | | #include <com/sun/star/lang/XServiceInfo.hpp> |
29 | | #include <com/sun/star/lang/XMultiServiceFactory.hpp> |
30 | | #include <com/sun/star/linguistic2/XDictionary.hpp> |
31 | | #include <com/sun/star/linguistic2/XSupportedLocales.hpp> |
32 | | #include <com/sun/star/linguistic2/XProofreader.hpp> |
33 | | #include <com/sun/star/linguistic2/XProofreadingIterator.hpp> |
34 | | #include <com/sun/star/linguistic2/SingleProofreadingError.hpp> |
35 | | #include <com/sun/star/linguistic2/ProofreadingResult.hpp> |
36 | | #include <com/sun/star/linguistic2/LinguServiceEvent.hpp> |
37 | | #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp> |
38 | | #include <com/sun/star/text/TextMarkupType.hpp> |
39 | | #include <com/sun/star/text/TextMarkupDescriptor.hpp> |
40 | | #include <com/sun/star/text/XMultiTextMarkup.hpp> |
41 | | #include <com/sun/star/text/XFlatParagraph.hpp> |
42 | | #include <com/sun/star/text/XFlatParagraphIterator.hpp> |
43 | | #include <com/sun/star/uno/XComponentContext.hpp> |
44 | | |
45 | | #include <sal/config.h> |
46 | | #include <sal/log.hxx> |
47 | | #include <o3tl/safeint.hxx> |
48 | | #include <osl/conditn.hxx> |
49 | | #include <cppuhelper/supportsservice.hxx> |
50 | | #include <cppuhelper/weak.hxx> |
51 | | #include <i18nlangtag/languagetag.hxx> |
52 | | #include <comphelper/processfactory.hxx> |
53 | | #include <comphelper/propertysequence.hxx> |
54 | | #include <tools/debug.hxx> |
55 | | #include <comphelper/diagnose_ex.hxx> |
56 | | |
57 | | #include <map> |
58 | | #include <algorithm> |
59 | | |
60 | | #include <linguistic/misc.hxx> |
61 | | |
62 | | #include "gciterator.hxx" |
63 | | |
64 | | using namespace linguistic; |
65 | | using namespace ::com::sun::star; |
66 | | |
67 | | // white space list: obtained from the fonts.config.txt of a Linux system. |
68 | | const sal_Unicode aWhiteSpaces[] = |
69 | | { |
70 | | 0x0020, /* SPACE */ |
71 | | 0x00a0, /* NO-BREAK SPACE */ |
72 | | 0x00ad, /* SOFT HYPHEN */ |
73 | | 0x115f, /* HANGUL CHOSEONG FILLER */ |
74 | | 0x1160, /* HANGUL JUNGSEONG FILLER */ |
75 | | 0x1680, /* OGHAM SPACE MARK */ |
76 | | 0x2000, /* EN QUAD */ |
77 | | 0x2001, /* EM QUAD */ |
78 | | 0x2002, /* EN SPACE */ |
79 | | 0x2003, /* EM SPACE */ |
80 | | 0x2004, /* THREE-PER-EM SPACE */ |
81 | | 0x2005, /* FOUR-PER-EM SPACE */ |
82 | | 0x2006, /* SIX-PER-EM SPACE */ |
83 | | 0x2007, /* FIGURE SPACE */ |
84 | | 0x2008, /* PUNCTUATION SPACE */ |
85 | | 0x2009, /* THIN SPACE */ |
86 | | 0x200a, /* HAIR SPACE */ |
87 | | 0x200b, /* ZERO WIDTH SPACE */ |
88 | | 0x200c, /* ZERO WIDTH NON-JOINER */ |
89 | | 0x200d, /* ZERO WIDTH JOINER */ |
90 | | 0x200e, /* LEFT-TO-RIGHT MARK */ |
91 | | 0x200f, /* RIGHT-TO-LEFT MARK */ |
92 | | 0x2028, /* LINE SEPARATOR */ |
93 | | 0x2029, /* PARAGRAPH SEPARATOR */ |
94 | | 0x202a, /* LEFT-TO-RIGHT EMBEDDING */ |
95 | | 0x202b, /* RIGHT-TO-LEFT EMBEDDING */ |
96 | | 0x202c, /* POP DIRECTIONAL FORMATTING */ |
97 | | 0x202d, /* LEFT-TO-RIGHT OVERRIDE */ |
98 | | 0x202e, /* RIGHT-TO-LEFT OVERRIDE */ |
99 | | 0x202f, /* NARROW NO-BREAK SPACE */ |
100 | | 0x205f, /* MEDIUM MATHEMATICAL SPACE */ |
101 | | 0x2060, /* WORD JOINER */ |
102 | | 0x2061, /* FUNCTION APPLICATION */ |
103 | | 0x2062, /* INVISIBLE TIMES */ |
104 | | 0x2063, /* INVISIBLE SEPARATOR */ |
105 | | 0x206A, /* INHIBIT SYMMETRIC SWAPPING */ |
106 | | 0x206B, /* ACTIVATE SYMMETRIC SWAPPING */ |
107 | | 0x206C, /* INHIBIT ARABIC FORM SHAPING */ |
108 | | 0x206D, /* ACTIVATE ARABIC FORM SHAPING */ |
109 | | 0x206E, /* NATIONAL DIGIT SHAPES */ |
110 | | 0x206F, /* NOMINAL DIGIT SHAPES */ |
111 | | 0x3000, /* IDEOGRAPHIC SPACE */ |
112 | | 0x3164, /* HANGUL FILLER */ |
113 | | 0xfeff, /* ZERO WIDTH NO-BREAK SPACE */ |
114 | | 0xffa0, /* HALFWIDTH HANGUL FILLER */ |
115 | | 0xfff9, /* INTERLINEAR ANNOTATION ANCHOR */ |
116 | | 0xfffa, /* INTERLINEAR ANNOTATION SEPARATOR */ |
117 | | 0xfffb /* INTERLINEAR ANNOTATION TERMINATOR */ |
118 | | }; |
119 | | |
120 | | // Information about reason for proofreading (ProofInfo) |
121 | | const sal_Int32 PROOFINFO_GET_PROOFRESULT = 1; |
122 | | const sal_Int32 PROOFINFO_MARK_PARAGRAPH = 2; |
123 | | |
124 | | static bool lcl_IsWhiteSpace( sal_Unicode cChar ) |
125 | 0 | { |
126 | 0 | return std::any_of(std::begin(aWhiteSpaces), std::end(aWhiteSpaces), |
127 | 0 | [&cChar](const sal_Unicode c) { return c == cChar; }); |
128 | 0 | } |
129 | | |
130 | | static sal_Int32 lcl_SkipWhiteSpaces( const OUString &rText, sal_Int32 nStartPos ) |
131 | 0 | { |
132 | | // note having nStartPos point right behind the string is OK since that one |
133 | | // is a correct end-of-sentence position to be returned from a grammar checker... |
134 | |
|
135 | 0 | const sal_Int32 nLen = rText.getLength(); |
136 | 0 | bool bIllegalArgument = false; |
137 | 0 | if (nStartPos < 0) |
138 | 0 | { |
139 | 0 | bIllegalArgument = true; |
140 | 0 | nStartPos = 0; |
141 | 0 | } |
142 | 0 | if (nStartPos > nLen) |
143 | 0 | { |
144 | 0 | bIllegalArgument = true; |
145 | 0 | nStartPos = nLen; |
146 | 0 | } |
147 | 0 | if (bIllegalArgument) |
148 | 0 | { |
149 | 0 | SAL_WARN( "linguistic", "lcl_SkipWhiteSpaces: illegal arguments" ); |
150 | 0 | } |
151 | | |
152 | 0 | sal_Int32 nRes = nStartPos; |
153 | 0 | if (0 <= nStartPos && nStartPos < nLen) |
154 | 0 | { |
155 | 0 | const sal_Unicode* const pEnd = rText.getStr() + nLen; |
156 | 0 | const sal_Unicode *pText = rText.getStr() + nStartPos; |
157 | 0 | while (pText != pEnd && lcl_IsWhiteSpace(*pText)) |
158 | 0 | ++pText; |
159 | 0 | nRes = pText - rText.getStr(); |
160 | 0 | } |
161 | |
|
162 | 0 | DBG_ASSERT( 0 <= nRes && nRes <= nLen, "lcl_SkipWhiteSpaces return value out of range" ); |
163 | 0 | return nRes; |
164 | 0 | } |
165 | | |
166 | | static sal_Int32 lcl_BacktraceWhiteSpaces( const OUString &rText, sal_Int32 nStartPos ) |
167 | 0 | { |
168 | | // note: having nStartPos point right behind the string is OK since that one |
169 | | // is a correct end-of-sentence position to be returned from a grammar checker... |
170 | |
|
171 | 0 | const sal_Int32 nLen = rText.getLength(); |
172 | 0 | bool bIllegalArgument = false; |
173 | 0 | if (nStartPos < 0) |
174 | 0 | { |
175 | 0 | bIllegalArgument = true; |
176 | 0 | nStartPos = 0; |
177 | 0 | } |
178 | 0 | if (nStartPos > nLen) |
179 | 0 | { |
180 | 0 | bIllegalArgument = true; |
181 | 0 | nStartPos = nLen; |
182 | 0 | } |
183 | 0 | if (bIllegalArgument) |
184 | 0 | { |
185 | 0 | SAL_WARN( "linguistic", "lcl_BacktraceWhiteSpaces: illegal arguments" ); |
186 | 0 | } |
187 | | |
188 | 0 | sal_Int32 nRes = nStartPos; |
189 | 0 | sal_Int32 nPosBefore = nStartPos - 1; |
190 | 0 | const sal_Unicode *pStart = rText.getStr(); |
191 | 0 | if (0 <= nPosBefore && nPosBefore < nLen && lcl_IsWhiteSpace( pStart[ nPosBefore ] )) |
192 | 0 | { |
193 | 0 | nStartPos = nPosBefore; |
194 | 0 | const sal_Unicode *pText = rText.getStr() + nStartPos; |
195 | 0 | while (pText > pStart && lcl_IsWhiteSpace( *pText )) |
196 | 0 | --pText; |
197 | | // now add 1 since we want to point to the first char after the last char in the sentence... |
198 | 0 | nRes = pText - pStart + 1; |
199 | 0 | } |
200 | |
|
201 | 0 | DBG_ASSERT( 0 <= nRes && nRes <= nLen, "lcl_BacktraceWhiteSpaces return value out of range" ); |
202 | 0 | return nRes; |
203 | 0 | } |
204 | | |
205 | | |
206 | | extern "C" { |
207 | | |
208 | | static void lcl_workerfunc (void * gci) |
209 | 0 | { |
210 | 0 | osl_setThreadName("GrammarCheckingIterator"); |
211 | |
|
212 | 0 | static_cast<GrammarCheckingIterator*>(gci)->DequeueAndCheck(); |
213 | 0 | } |
214 | | |
215 | | } |
216 | | |
217 | | static lang::Locale lcl_GetPrimaryLanguageOfSentence( |
218 | | const uno::Reference< text::XFlatParagraph >& xFlatPara, |
219 | | sal_Int32 nStartIndex ) |
220 | 0 | { |
221 | | //get the language of the first word |
222 | 0 | return xFlatPara->getLanguageOfText( nStartIndex, 1 ); |
223 | 0 | } |
224 | | |
225 | | |
226 | 0 | LngXStringKeyMap::LngXStringKeyMap() {} |
227 | | |
228 | | void SAL_CALL LngXStringKeyMap::insertValue(const OUString& aKey, const css::uno::Any& aValue) |
229 | 0 | { |
230 | 0 | std::map<OUString, css::uno::Any>::const_iterator aIter = maMap.find(aKey); |
231 | 0 | if (aIter != maMap.end()) |
232 | 0 | throw css::container::ElementExistException(); |
233 | | |
234 | 0 | maMap[aKey] = aValue; |
235 | 0 | } |
236 | | |
237 | | css::uno::Any SAL_CALL LngXStringKeyMap::getValue(const OUString& aKey) |
238 | 0 | { |
239 | 0 | std::map<OUString, css::uno::Any>::const_iterator aIter = maMap.find(aKey); |
240 | 0 | if (aIter == maMap.end()) |
241 | 0 | throw css::container::NoSuchElementException(); |
242 | | |
243 | 0 | return (*aIter).second; |
244 | 0 | } |
245 | | |
246 | | sal_Bool SAL_CALL LngXStringKeyMap::hasValue(const OUString& aKey) |
247 | 0 | { |
248 | 0 | return maMap.contains(aKey); |
249 | 0 | } |
250 | | |
251 | 0 | ::sal_Int32 SAL_CALL LngXStringKeyMap::getCount() { return maMap.size(); } |
252 | | |
253 | | OUString SAL_CALL LngXStringKeyMap::getKeyByIndex(::sal_Int32 nIndex) |
254 | 0 | { |
255 | 0 | if (nIndex < 0 || o3tl::make_unsigned(nIndex) >= maMap.size()) |
256 | 0 | throw css::lang::IndexOutOfBoundsException(); |
257 | | |
258 | 0 | return OUString(); |
259 | 0 | } |
260 | | |
261 | | css::uno::Any SAL_CALL LngXStringKeyMap::getValueByIndex(::sal_Int32 nIndex) |
262 | 0 | { |
263 | 0 | if (nIndex < 0 || o3tl::make_unsigned(nIndex) >= maMap.size()) |
264 | 0 | throw css::lang::IndexOutOfBoundsException(); |
265 | | |
266 | 0 | return css::uno::Any(); |
267 | 0 | } |
268 | | |
269 | | |
270 | | osl::Mutex& GrammarCheckingIterator::MyMutex() |
271 | 0 | { |
272 | 0 | static osl::Mutex SINGLETON; |
273 | 0 | return SINGLETON; |
274 | 0 | } |
275 | | |
276 | | GrammarCheckingIterator::GrammarCheckingIterator() : |
277 | 0 | m_bEnd( false ), |
278 | 0 | m_bGCServicesChecked( false ), |
279 | 0 | m_nDocIdCounter( 0 ), |
280 | 0 | m_thread(nullptr), |
281 | 0 | m_aEventListeners( MyMutex() ), |
282 | 0 | m_aNotifyListeners( MyMutex() ) |
283 | 0 | { |
284 | 0 | } |
285 | | |
286 | | |
287 | | GrammarCheckingIterator::~GrammarCheckingIterator() |
288 | 0 | { |
289 | 0 | TerminateThread(); |
290 | 0 | } |
291 | | |
292 | | void GrammarCheckingIterator::TerminateThread() |
293 | 0 | { |
294 | 0 | oslThread t; |
295 | 0 | { |
296 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
297 | 0 | t = m_thread; |
298 | 0 | m_thread = nullptr; |
299 | 0 | m_bEnd = true; |
300 | 0 | m_aWakeUpThread.set(); |
301 | 0 | } |
302 | 0 | if (t != nullptr) |
303 | 0 | { |
304 | 0 | osl_joinWithThread(t); |
305 | 0 | osl_destroyThread(t); |
306 | 0 | } |
307 | | // After m_bEnd was used to flag lcl_workerfunc to quit, now |
308 | | // reset it so lcl_workerfunc could be relaunched later. |
309 | 0 | { |
310 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
311 | 0 | m_bEnd = false; |
312 | 0 | } |
313 | 0 | } |
314 | | |
315 | | bool GrammarCheckingIterator::joinThreads() |
316 | 0 | { |
317 | 0 | TerminateThread(); |
318 | 0 | return true; |
319 | 0 | } |
320 | | |
321 | | |
322 | | sal_Int32 GrammarCheckingIterator::NextDocId() |
323 | 0 | { |
324 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
325 | 0 | m_nDocIdCounter += 1; |
326 | 0 | return m_nDocIdCounter; |
327 | 0 | } |
328 | | |
329 | | |
330 | | OUString GrammarCheckingIterator::GetOrCreateDocId( |
331 | | const uno::Reference< lang::XComponent > &xComponent ) |
332 | 0 | { |
333 | | // internal method; will always be called with locked mutex |
334 | |
|
335 | 0 | OUString aRes; |
336 | 0 | if (xComponent.is()) |
337 | 0 | { |
338 | 0 | if (m_aDocIdMap.contains( xComponent.get() )) |
339 | 0 | { |
340 | | // return already existing entry |
341 | 0 | aRes = m_aDocIdMap[ xComponent.get() ]; |
342 | 0 | } |
343 | 0 | else // add new entry |
344 | 0 | { |
345 | 0 | sal_Int32 nRes = NextDocId(); |
346 | 0 | aRes = OUString::number( nRes ); |
347 | 0 | m_aDocIdMap[ xComponent.get() ] = aRes; |
348 | 0 | xComponent->addEventListener( this ); |
349 | 0 | } |
350 | 0 | } |
351 | 0 | return aRes; |
352 | 0 | } |
353 | | |
354 | | |
355 | | void GrammarCheckingIterator::AddEntry( |
356 | | const uno::Reference< text::XFlatParagraphIterator >& xFlatParaIterator, |
357 | | const uno::Reference< text::XFlatParagraph >& xFlatPara, |
358 | | const OUString & rDocId, |
359 | | sal_Int32 nStartIndex, |
360 | | bool bAutomatic ) |
361 | 0 | { |
362 | | // we may not need/have a xFlatParaIterator (e.g. if checkGrammarAtPos was called) |
363 | | // but we always need a xFlatPara... |
364 | 0 | if (!xFlatPara.is()) |
365 | 0 | return; |
366 | | |
367 | 0 | FPEntry aNewFPEntry; |
368 | 0 | aNewFPEntry.m_xParaIterator = xFlatParaIterator; |
369 | 0 | aNewFPEntry.m_xPara = xFlatPara; |
370 | 0 | aNewFPEntry.m_aDocId = rDocId; |
371 | 0 | aNewFPEntry.m_nStartIndex = nStartIndex; |
372 | 0 | aNewFPEntry.m_bAutomatic = bAutomatic; |
373 | | |
374 | | // add new entry to the end of this queue |
375 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
376 | 0 | if (!m_thread) |
377 | 0 | m_thread = osl_createThread( lcl_workerfunc, this ); |
378 | 0 | m_aFPEntriesQueue.push_back( aNewFPEntry ); |
379 | | |
380 | | // wake up the thread in order to do grammar checking |
381 | 0 | m_aWakeUpThread.set(); |
382 | 0 | } |
383 | | |
384 | | |
385 | | void GrammarCheckingIterator::ProcessResult( |
386 | | const linguistic2::ProofreadingResult &rRes, |
387 | | const uno::Reference< text::XFlatParagraphIterator > &rxFlatParagraphIterator, |
388 | | bool bIsAutomaticChecking ) |
389 | 0 | { |
390 | 0 | DBG_ASSERT( rRes.xFlatParagraph.is(), "xFlatParagraph is missing" ); |
391 | | //no guard necessary as no members are used |
392 | 0 | bool bContinueWithNextPara = false; |
393 | 0 | if (!rRes.xFlatParagraph.is() || rRes.xFlatParagraph->isModified()) |
394 | 0 | { |
395 | | // if paragraph was modified/deleted meanwhile continue with the next one... |
396 | 0 | bContinueWithNextPara = true; |
397 | 0 | } |
398 | 0 | else // paragraph is still unchanged... |
399 | 0 | { |
400 | | // mark found errors... |
401 | |
|
402 | 0 | sal_Int32 nTextLen = rRes.aText.getLength(); |
403 | 0 | bool bBoundariesOk = 0 <= rRes.nStartOfSentencePosition && rRes.nStartOfSentencePosition <= nTextLen && |
404 | 0 | 0 <= rRes.nBehindEndOfSentencePosition && rRes.nBehindEndOfSentencePosition <= nTextLen && |
405 | 0 | 0 <= rRes.nStartOfNextSentencePosition && rRes.nStartOfNextSentencePosition <= nTextLen && |
406 | 0 | rRes.nStartOfSentencePosition <= rRes.nBehindEndOfSentencePosition && |
407 | 0 | rRes.nBehindEndOfSentencePosition <= rRes.nStartOfNextSentencePosition; |
408 | 0 | DBG_ASSERT( bBoundariesOk, "inconsistent sentence boundaries" ); |
409 | |
|
410 | 0 | uno::Reference< text::XMultiTextMarkup > xMulti( rRes.xFlatParagraph, uno::UNO_QUERY ); |
411 | 0 | if (xMulti.is()) // use new API for markups |
412 | 0 | { |
413 | 0 | try |
414 | 0 | { |
415 | | // length = number of found errors + 1 sentence markup |
416 | 0 | sal_Int32 nErrors = rRes.aErrors.getLength(); |
417 | 0 | uno::Sequence< text::TextMarkupDescriptor > aDescriptors( nErrors + 1 ); |
418 | 0 | text::TextMarkupDescriptor * pDescriptors = aDescriptors.getArray(); |
419 | |
|
420 | 0 | uno::Reference< linguistic2::XDictionary > xIgnoreAll = ::GetIgnoreAllList(); |
421 | 0 | sal_Int32 ignoredCount = 0; |
422 | | |
423 | | // at pos 0 .. nErrors-1 -> all grammar errors |
424 | 0 | for (const linguistic2::SingleProofreadingError &rError : rRes.aErrors) |
425 | 0 | { |
426 | 0 | OUString word(rRes.aText.subView(rError.nErrorStart, rError.nErrorLength)); |
427 | 0 | bool ignored = xIgnoreAll.is() && xIgnoreAll->getEntry(word).is(); |
428 | |
|
429 | 0 | if (!ignored) |
430 | 0 | { |
431 | 0 | text::TextMarkupDescriptor &rDesc = *pDescriptors++; |
432 | |
|
433 | 0 | rDesc.nType = rError.nErrorType; |
434 | 0 | rDesc.nOffset = rError.nErrorStart; |
435 | 0 | rDesc.nLength = rError.nErrorLength; |
436 | | |
437 | | // the proofreader may return SPELLING but right now our core |
438 | | // does only handle PROOFREADING if the result is from the proofreader... |
439 | | // (later on we may wish to color spelling errors found by the proofreader |
440 | | // differently for example. But no special handling right now. |
441 | 0 | if (rDesc.nType == text::TextMarkupType::SPELLCHECK) |
442 | 0 | rDesc.nType = text::TextMarkupType::PROOFREADING; |
443 | |
|
444 | 0 | uno::Reference< container::XStringKeyMap > xKeyMap(new LngXStringKeyMap()); |
445 | 0 | for( const beans::PropertyValue& rProperty : rError.aProperties ) |
446 | 0 | { |
447 | 0 | if ( rProperty.Name == "LineColor" ) |
448 | 0 | { |
449 | 0 | xKeyMap->insertValue(rProperty.Name, rProperty.Value); |
450 | 0 | rDesc.xMarkupInfoContainer = xKeyMap; |
451 | 0 | } |
452 | 0 | else if ( rProperty.Name == "LineType" ) |
453 | 0 | { |
454 | 0 | xKeyMap->insertValue(rProperty.Name, rProperty.Value); |
455 | 0 | rDesc.xMarkupInfoContainer = xKeyMap; |
456 | 0 | } |
457 | 0 | } |
458 | 0 | } |
459 | 0 | else |
460 | 0 | ignoredCount++; |
461 | 0 | } |
462 | |
|
463 | 0 | if (ignoredCount != 0) |
464 | 0 | { |
465 | 0 | aDescriptors.realloc(aDescriptors.getLength() - ignoredCount); |
466 | 0 | pDescriptors = aDescriptors.getArray(); |
467 | 0 | pDescriptors += aDescriptors.getLength() - 1; |
468 | 0 | } |
469 | | |
470 | | // at pos nErrors -> sentence markup |
471 | | // nSentenceLength: includes the white-spaces following the sentence end... |
472 | 0 | const sal_Int32 nSentenceLength = rRes.nStartOfNextSentencePosition - rRes.nStartOfSentencePosition; |
473 | 0 | pDescriptors->nType = text::TextMarkupType::SENTENCE; |
474 | 0 | pDescriptors->nOffset = rRes.nStartOfSentencePosition; |
475 | 0 | pDescriptors->nLength = nSentenceLength; |
476 | |
|
477 | 0 | xMulti->commitMultiTextMarkup( aDescriptors ) ; |
478 | 0 | } |
479 | 0 | catch (lang::IllegalArgumentException &) |
480 | 0 | { |
481 | 0 | TOOLS_WARN_EXCEPTION( "linguistic", "commitMultiTextMarkup" ); |
482 | 0 | } |
483 | 0 | } |
484 | | |
485 | | // other sentences left to be checked in this paragraph? |
486 | 0 | if (rRes.nStartOfNextSentencePosition < rRes.aText.getLength()) |
487 | 0 | { |
488 | 0 | AddEntry( rxFlatParagraphIterator, rRes.xFlatParagraph, rRes.aDocumentIdentifier, rRes.nStartOfNextSentencePosition, bIsAutomaticChecking ); |
489 | 0 | } |
490 | 0 | else // current paragraph finished |
491 | 0 | { |
492 | | // set "already checked" flag for the current flat paragraph |
493 | 0 | if (rRes.xFlatParagraph.is()) |
494 | 0 | rRes.xFlatParagraph->setChecked( text::TextMarkupType::PROOFREADING, true ); |
495 | |
|
496 | 0 | bContinueWithNextPara = true; |
497 | 0 | } |
498 | 0 | } |
499 | | |
500 | 0 | if (bContinueWithNextPara) |
501 | 0 | { |
502 | | // we need to continue with the next paragraph |
503 | 0 | if (rxFlatParagraphIterator.is()) |
504 | 0 | AddEntry(rxFlatParagraphIterator, rxFlatParagraphIterator->getNextPara(), |
505 | 0 | rRes.aDocumentIdentifier, 0, bIsAutomaticChecking); |
506 | 0 | } |
507 | 0 | } |
508 | | |
509 | | |
510 | | std::pair<OUString, std::optional<OUString>> |
511 | | GrammarCheckingIterator::getServiceForLocale(const lang::Locale& rLocale) const |
512 | 0 | { |
513 | 0 | if (!rLocale.Language.isEmpty()) |
514 | 0 | { |
515 | 0 | const OUString sBcp47 = LanguageTag::convertToBcp47(rLocale, false); |
516 | 0 | GCImplNames_t::const_iterator aLangIt(m_aGCImplNamesByLang.find(sBcp47)); |
517 | 0 | if (aLangIt != m_aGCImplNamesByLang.end()) |
518 | 0 | return { aLangIt->second, {} }; |
519 | | |
520 | 0 | for (const auto& sFallbackBcp47 : LanguageTag(rLocale).getFallbackStrings(false)) |
521 | 0 | { |
522 | 0 | aLangIt = m_aGCImplNamesByLang.find(sFallbackBcp47); |
523 | 0 | if (aLangIt != m_aGCImplNamesByLang.end()) |
524 | 0 | return { aLangIt->second, sFallbackBcp47 }; |
525 | 0 | } |
526 | 0 | } |
527 | | |
528 | 0 | return {}; |
529 | 0 | } |
530 | | |
531 | | |
532 | | uno::Reference< linguistic2::XProofreader > GrammarCheckingIterator::GetGrammarChecker( |
533 | | lang::Locale &rLocale ) |
534 | 0 | { |
535 | 0 | uno::Reference< linguistic2::XProofreader > xRes; |
536 | | |
537 | | // ---- THREAD SAFE START ---- |
538 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
539 | | |
540 | | // check supported locales for each grammarchecker if not already done |
541 | 0 | if (!m_bGCServicesChecked) |
542 | 0 | { |
543 | 0 | GetConfiguredGCSvcs_Impl(); |
544 | 0 | m_bGCServicesChecked = true; |
545 | 0 | } |
546 | |
|
547 | 0 | if (const auto [aSvcImplName, oFallbackBcp47] = getServiceForLocale(rLocale); |
548 | 0 | !aSvcImplName.isEmpty()) // matching configured language found? |
549 | 0 | { |
550 | 0 | if (oFallbackBcp47) |
551 | 0 | rLocale = LanguageTag::convertToLocale(*oFallbackBcp47, false); |
552 | 0 | GCReferences_t::const_iterator aImplNameIt( m_aGCReferencesByService.find( aSvcImplName ) ); |
553 | 0 | if (aImplNameIt != m_aGCReferencesByService.end()) // matching impl name found? |
554 | 0 | { |
555 | 0 | xRes = aImplNameIt->second; |
556 | 0 | } |
557 | 0 | else // the service is to be instantiated here for the first time... |
558 | 0 | { |
559 | 0 | try |
560 | 0 | { |
561 | 0 | const uno::Reference< uno::XComponentContext >& xContext( comphelper::getProcessComponentContext() ); |
562 | 0 | uno::Reference< linguistic2::XProofreader > xGC( |
563 | 0 | xContext->getServiceManager()->createInstanceWithContext(aSvcImplName, xContext), |
564 | 0 | uno::UNO_QUERY_THROW ); |
565 | 0 | uno::Reference< linguistic2::XSupportedLocales > xSuppLoc( xGC, uno::UNO_QUERY_THROW ); |
566 | |
|
567 | 0 | if (xSuppLoc->hasLocale( rLocale )) |
568 | 0 | { |
569 | 0 | m_aGCReferencesByService[ aSvcImplName ] = xGC; |
570 | 0 | xRes = xGC; |
571 | |
|
572 | 0 | uno::Reference< linguistic2::XLinguServiceEventBroadcaster > xBC( xGC, uno::UNO_QUERY ); |
573 | 0 | if (xBC.is()) |
574 | 0 | xBC->addLinguServiceEventListener( this ); |
575 | 0 | } |
576 | 0 | else |
577 | 0 | { |
578 | 0 | SAL_WARN( "linguistic", "grammar checker does not support required locale" ); |
579 | 0 | } |
580 | 0 | } |
581 | 0 | catch (uno::Exception &) |
582 | 0 | { |
583 | 0 | SAL_WARN( "linguistic", "instantiating grammar checker failed" ); |
584 | 0 | } |
585 | 0 | } |
586 | 0 | } |
587 | 0 | else // not found - quite normal |
588 | 0 | { |
589 | 0 | SAL_INFO("linguistic", "No grammar checker found for \"" |
590 | 0 | << LanguageTag::convertToBcp47(rLocale, false) << "\""); |
591 | 0 | } |
592 | | // ---- THREAD SAFE END ---- |
593 | | |
594 | 0 | return xRes; |
595 | 0 | } |
596 | | |
597 | | static uno::Sequence<beans::PropertyValue> |
598 | | lcl_makeProperties(uno::Reference<text::XFlatParagraph> const& xFlatPara, sal_Int32 nProofInfo) |
599 | 0 | { |
600 | 0 | uno::Reference<beans::XPropertySet> const xProps( |
601 | 0 | xFlatPara, uno::UNO_QUERY_THROW); |
602 | 0 | css::uno::Any a (nProofInfo); |
603 | 0 | return comphelper::InitPropertySequence({ |
604 | 0 | { "FieldPositions", xProps->getPropertyValue(u"FieldPositions"_ustr) }, |
605 | 0 | { "FootnotePositions", xProps->getPropertyValue(u"FootnotePositions"_ustr) }, |
606 | 0 | { "SortedTextId", xProps->getPropertyValue(u"SortedTextId"_ustr) }, |
607 | 0 | { "DocumentElementsCount", xProps->getPropertyValue(u"DocumentElementsCount"_ustr) }, |
608 | 0 | { "ProofInfo", a } |
609 | 0 | }); |
610 | 0 | } |
611 | | |
612 | | void GrammarCheckingIterator::DequeueAndCheck() |
613 | 0 | { |
614 | 0 | for (;;) |
615 | 0 | { |
616 | | // ---- THREAD SAFE START ---- |
617 | 0 | bool bQueueEmpty = false; |
618 | 0 | { |
619 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
620 | 0 | if (m_bEnd) |
621 | 0 | { |
622 | 0 | break; |
623 | 0 | } |
624 | 0 | bQueueEmpty = m_aFPEntriesQueue.empty(); |
625 | 0 | } |
626 | | // ---- THREAD SAFE END ---- |
627 | | |
628 | 0 | if (!bQueueEmpty) |
629 | 0 | { |
630 | 0 | uno::Reference< text::XFlatParagraphIterator > xFPIterator; |
631 | 0 | uno::Reference< text::XFlatParagraph > xFlatPara; |
632 | 0 | FPEntry aFPEntryItem; |
633 | 0 | OUString aCurDocId; |
634 | | // ---- THREAD SAFE START ---- |
635 | 0 | { |
636 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
637 | 0 | aFPEntryItem = m_aFPEntriesQueue.front(); |
638 | 0 | xFPIterator = aFPEntryItem.m_xParaIterator; |
639 | 0 | xFlatPara = aFPEntryItem.m_xPara; |
640 | 0 | m_aCurCheckedDocId = aFPEntryItem.m_aDocId; |
641 | 0 | aCurDocId = m_aCurCheckedDocId; |
642 | |
|
643 | 0 | m_aFPEntriesQueue.pop_front(); |
644 | 0 | } |
645 | | // ---- THREAD SAFE END ---- |
646 | |
|
647 | 0 | if (xFlatPara.is() && xFPIterator.is()) |
648 | 0 | { |
649 | 0 | try |
650 | 0 | { |
651 | 0 | OUString aCurTxt( xFlatPara->getText() ); |
652 | 0 | lang::Locale aCurLocale = lcl_GetPrimaryLanguageOfSentence( xFlatPara, aFPEntryItem.m_nStartIndex ); |
653 | |
|
654 | 0 | const bool bModified = xFlatPara->isModified(); |
655 | 0 | if (!bModified) |
656 | 0 | { |
657 | 0 | linguistic2::ProofreadingResult aRes; |
658 | | |
659 | | // ---- THREAD SAFE START ---- |
660 | 0 | { |
661 | 0 | osl::ClearableMutexGuard aGuard(MyMutex()); |
662 | |
|
663 | 0 | sal_Int32 nStartPos = aFPEntryItem.m_nStartIndex; |
664 | 0 | sal_Int32 nSuggestedEnd |
665 | 0 | = GetSuggestedEndOfSentence(aCurTxt, nStartPos, aCurLocale); |
666 | 0 | DBG_ASSERT((nSuggestedEnd == 0 && aCurTxt.isEmpty()) |
667 | 0 | || nSuggestedEnd > nStartPos, |
668 | 0 | "nSuggestedEndOfSentencePos calculation failed?"); |
669 | |
|
670 | 0 | uno::Reference<linguistic2::XProofreader> xGC = |
671 | 0 | GetGrammarChecker(aCurLocale); |
672 | 0 | if (xGC.is()) |
673 | 0 | { |
674 | 0 | aGuard.clear(); |
675 | 0 | uno::Sequence<beans::PropertyValue> const aProps( |
676 | 0 | lcl_makeProperties(xFlatPara, PROOFINFO_MARK_PARAGRAPH)); |
677 | 0 | aRes = xGC->doProofreading(aCurDocId, aCurTxt, aCurLocale, |
678 | 0 | nStartPos, nSuggestedEnd, aProps); |
679 | | |
680 | | //!! work-around to prevent looping if the grammar checker |
681 | | //!! failed to properly identify the sentence end |
682 | 0 | if (aRes.nBehindEndOfSentencePosition <= nStartPos |
683 | 0 | && aRes.nBehindEndOfSentencePosition != nSuggestedEnd) |
684 | 0 | { |
685 | 0 | SAL_WARN( |
686 | 0 | "linguistic", |
687 | 0 | "!! Grammarchecker failed to provide end of sentence !!"); |
688 | 0 | aRes.nBehindEndOfSentencePosition = nSuggestedEnd; |
689 | 0 | } |
690 | | |
691 | 0 | aRes.xFlatParagraph = std::move(xFlatPara); |
692 | 0 | aRes.nStartOfSentencePosition = nStartPos; |
693 | 0 | } |
694 | 0 | else |
695 | 0 | { |
696 | | // no grammar checker -> no error |
697 | | // but we need to provide the data below in order to continue with the next sentence |
698 | 0 | aRes.aDocumentIdentifier = aCurDocId; |
699 | 0 | aRes.xFlatParagraph = std::move(xFlatPara); |
700 | 0 | aRes.aText = aCurTxt; |
701 | 0 | aRes.aLocale = std::move(aCurLocale); |
702 | 0 | aRes.nStartOfSentencePosition = nStartPos; |
703 | 0 | aRes.nBehindEndOfSentencePosition = nSuggestedEnd; |
704 | 0 | } |
705 | 0 | aRes.nStartOfNextSentencePosition |
706 | 0 | = lcl_SkipWhiteSpaces(aCurTxt, aRes.nBehindEndOfSentencePosition); |
707 | 0 | aRes.nBehindEndOfSentencePosition = lcl_BacktraceWhiteSpaces( |
708 | 0 | aCurTxt, aRes.nStartOfNextSentencePosition); |
709 | | |
710 | | //guard has to be cleared as ProcessResult calls out of this class |
711 | 0 | } |
712 | | // ---- THREAD SAFE END ---- |
713 | 0 | ProcessResult( aRes, xFPIterator, aFPEntryItem.m_bAutomatic ); |
714 | 0 | } |
715 | 0 | else |
716 | 0 | { |
717 | | // the paragraph changed meanwhile... (and maybe is still edited) |
718 | | // thus we simply continue to ask for the next to be checked. |
719 | 0 | uno::Reference< text::XFlatParagraph > xFlatParaNext( xFPIterator->getNextPara() ); |
720 | 0 | AddEntry( xFPIterator, xFlatParaNext, aCurDocId, 0, aFPEntryItem.m_bAutomatic ); |
721 | 0 | } |
722 | 0 | } |
723 | 0 | catch (css::uno::Exception &) |
724 | 0 | { |
725 | 0 | TOOLS_WARN_EXCEPTION("linguistic", "GrammarCheckingIterator::DequeueAndCheck ignoring"); |
726 | 0 | } |
727 | 0 | } |
728 | | |
729 | | // ---- THREAD SAFE START ---- |
730 | 0 | { |
731 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
732 | 0 | m_aCurCheckedDocId.clear(); |
733 | 0 | } |
734 | | // ---- THREAD SAFE END ---- |
735 | 0 | } |
736 | 0 | else |
737 | 0 | { |
738 | | // ---- THREAD SAFE START ---- |
739 | 0 | { |
740 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
741 | 0 | if (m_bEnd) |
742 | 0 | { |
743 | 0 | break; |
744 | 0 | } |
745 | | // Check queue state again |
746 | 0 | if (m_aFPEntriesQueue.empty()) |
747 | 0 | m_aWakeUpThread.reset(); |
748 | 0 | } |
749 | | // ---- THREAD SAFE END ---- |
750 | | |
751 | | //if the queue is empty |
752 | | // IMPORTANT: Don't call condition.wait() with locked |
753 | | // mutex. Otherwise you would keep out other threads |
754 | | // to add entries to the queue! A condition is thread- |
755 | | // safe implemented. |
756 | 0 | m_aWakeUpThread.wait(); |
757 | 0 | } |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | | |
762 | | void SAL_CALL GrammarCheckingIterator::startProofreading( |
763 | | const uno::Reference< ::uno::XInterface > & xDoc, |
764 | | const uno::Reference< text::XFlatParagraphIteratorProvider > & xIteratorProvider ) |
765 | 0 | { |
766 | | // get paragraph to start checking with |
767 | 0 | const bool bAutomatic = true; |
768 | 0 | uno::Reference<text::XFlatParagraphIterator> xFPIterator = xIteratorProvider->getFlatParagraphIterator( |
769 | 0 | text::TextMarkupType::PROOFREADING, bAutomatic ); |
770 | 0 | uno::Reference< text::XFlatParagraph > xPara( xFPIterator.is()? xFPIterator->getFirstPara() : nullptr ); |
771 | 0 | uno::Reference< lang::XComponent > xComponent( xDoc, uno::UNO_QUERY ); |
772 | | |
773 | | // ---- THREAD SAFE START ---- |
774 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
775 | 0 | if (xPara.is() && xComponent.is()) |
776 | 0 | { |
777 | 0 | OUString aDocId = GetOrCreateDocId( xComponent ); |
778 | | |
779 | | // create new entry and add it to queue |
780 | 0 | AddEntry( xFPIterator, xPara, aDocId, 0, bAutomatic ); |
781 | 0 | } |
782 | | // ---- THREAD SAFE END ---- |
783 | 0 | } |
784 | | |
785 | | |
786 | | linguistic2::ProofreadingResult SAL_CALL GrammarCheckingIterator::checkSentenceAtPosition( |
787 | | const uno::Reference< uno::XInterface >& xDoc, |
788 | | const uno::Reference< text::XFlatParagraph >& xFlatPara, |
789 | | const OUString& rText, |
790 | | const lang::Locale&, |
791 | | sal_Int32 nStartOfSentencePos, |
792 | | sal_Int32 nSuggestedEndOfSentencePos, |
793 | | sal_Int32 nErrorPosInPara ) |
794 | 0 | { |
795 | | // for the context menu... |
796 | |
|
797 | 0 | uno::Reference< lang::XComponent > xComponent( xDoc, uno::UNO_QUERY ); |
798 | 0 | const bool bDoCheck = (xFlatPara.is() && xComponent.is() && |
799 | 0 | ( nErrorPosInPara < 0 || nErrorPosInPara < rText.getLength())); |
800 | |
|
801 | 0 | if (!bDoCheck) |
802 | 0 | return linguistic2::ProofreadingResult(); |
803 | | |
804 | | // iterate through paragraph until we find the sentence we are interested in |
805 | 0 | linguistic2::ProofreadingResult aTmpRes; |
806 | 0 | sal_Int32 nStartPos = nStartOfSentencePos >= 0 ? nStartOfSentencePos : 0; |
807 | |
|
808 | 0 | bool bFound = false; |
809 | 0 | try |
810 | 0 | { |
811 | 0 | do |
812 | 0 | { |
813 | 0 | lang::Locale aCurLocale = lcl_GetPrimaryLanguageOfSentence(xFlatPara, nStartPos); |
814 | 0 | sal_Int32 nOldStartOfSentencePos = nStartPos; |
815 | 0 | uno::Reference<linguistic2::XProofreader> xGC; |
816 | 0 | OUString aDocId; |
817 | | |
818 | | // ---- THREAD SAFE START ---- |
819 | 0 | { |
820 | 0 | ::osl::Guard<::osl::Mutex> aGuard(MyMutex()); |
821 | 0 | aDocId = GetOrCreateDocId(xComponent); |
822 | 0 | nSuggestedEndOfSentencePos |
823 | 0 | = GetSuggestedEndOfSentence(rText, nStartPos, aCurLocale); |
824 | 0 | DBG_ASSERT(nSuggestedEndOfSentencePos > nStartPos, |
825 | 0 | "nSuggestedEndOfSentencePos calculation failed?"); |
826 | |
|
827 | 0 | xGC = GetGrammarChecker(aCurLocale); |
828 | 0 | } |
829 | | // ---- THREAD SAFE START ---- |
830 | 0 | sal_Int32 nEndPos = -1; |
831 | 0 | if (xGC.is()) |
832 | 0 | { |
833 | 0 | uno::Sequence<beans::PropertyValue> const aProps( |
834 | 0 | lcl_makeProperties(xFlatPara, PROOFINFO_GET_PROOFRESULT)); |
835 | 0 | aTmpRes = xGC->doProofreading(aDocId, rText, aCurLocale, nStartPos, |
836 | 0 | nSuggestedEndOfSentencePos, aProps); |
837 | | |
838 | | //!! work-around to prevent looping if the grammar checker |
839 | | //!! failed to properly identify the sentence end |
840 | 0 | if (aTmpRes.nBehindEndOfSentencePosition <= nStartPos) |
841 | 0 | { |
842 | 0 | SAL_WARN("linguistic", |
843 | 0 | "!! Grammarchecker failed to provide end of sentence !!"); |
844 | 0 | aTmpRes.nBehindEndOfSentencePosition = nSuggestedEndOfSentencePos; |
845 | 0 | } |
846 | | |
847 | 0 | aTmpRes.xFlatParagraph = xFlatPara; |
848 | 0 | aTmpRes.nStartOfSentencePosition = nStartPos; |
849 | 0 | nEndPos = aTmpRes.nBehindEndOfSentencePosition; |
850 | |
|
851 | 0 | if ((nErrorPosInPara < 0 || nStartPos <= nErrorPosInPara) |
852 | 0 | && nErrorPosInPara < nEndPos) |
853 | 0 | bFound = true; |
854 | 0 | } |
855 | 0 | if (nEndPos == -1) // no result from grammar checker |
856 | 0 | nEndPos = nSuggestedEndOfSentencePos; |
857 | 0 | nStartPos = lcl_SkipWhiteSpaces(rText, nEndPos); |
858 | 0 | aTmpRes.nBehindEndOfSentencePosition = nEndPos; |
859 | 0 | aTmpRes.nStartOfNextSentencePosition = nStartPos; |
860 | 0 | aTmpRes.nBehindEndOfSentencePosition |
861 | 0 | = lcl_BacktraceWhiteSpaces(rText, aTmpRes.nStartOfNextSentencePosition); |
862 | | |
863 | | // prevent endless loop by forcefully advancing if needs be... |
864 | 0 | if (nStartPos <= nOldStartOfSentencePos) |
865 | 0 | { |
866 | 0 | SAL_WARN("linguistic", "end-of-sentence detection failed?"); |
867 | 0 | nStartPos = nOldStartOfSentencePos + 1; |
868 | 0 | } |
869 | 0 | } while (!bFound && nStartPos < rText.getLength()); |
870 | 0 | } |
871 | 0 | catch (css::uno::Exception&) |
872 | 0 | { |
873 | 0 | TOOLS_WARN_EXCEPTION("linguistic", |
874 | 0 | "GrammarCheckingIterator::checkSentenceAtPosition ignoring"); |
875 | 0 | } |
876 | | |
877 | 0 | if (bFound && !xFlatPara->isModified()) |
878 | 0 | return aTmpRes; |
879 | | |
880 | 0 | return linguistic2::ProofreadingResult(); |
881 | 0 | } |
882 | | |
883 | | sal_Int32 GrammarCheckingIterator::GetSuggestedEndOfSentence( |
884 | | const OUString &rText, |
885 | | sal_Int32 nSentenceStartPos, |
886 | | const lang::Locale &rLocale ) |
887 | 0 | { |
888 | | // internal method; will always be called with locked mutex |
889 | |
|
890 | 0 | if (!m_xBreakIterator.is()) |
891 | 0 | { |
892 | 0 | const uno::Reference< uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext(); |
893 | 0 | m_xBreakIterator = i18n::BreakIterator::create(xContext); |
894 | 0 | } |
895 | 0 | sal_Int32 nTextLen = rText.getLength(); |
896 | 0 | sal_Int32 nEndPosition(0); |
897 | 0 | sal_Int32 nTmpStartPos = nSentenceStartPos; |
898 | 0 | do |
899 | 0 | { |
900 | 0 | sal_Int32 const nPrevEndPosition(nEndPosition); |
901 | 0 | nEndPosition = nTextLen; |
902 | 0 | if (nTmpStartPos < nTextLen) |
903 | 0 | { |
904 | 0 | nEndPosition = m_xBreakIterator->endOfSentence( rText, nTmpStartPos, rLocale ); |
905 | 0 | if (nEndPosition <= nPrevEndPosition) |
906 | 0 | { |
907 | | // fdo#68750 if there's no progress at all then presumably |
908 | | // there's no end of sentence in this paragraph so just |
909 | | // set the end position to end of paragraph |
910 | 0 | nEndPosition = nTextLen; |
911 | 0 | } |
912 | 0 | } |
913 | 0 | if (nEndPosition < 0) |
914 | 0 | nEndPosition = nTextLen; |
915 | |
|
916 | 0 | ++nTmpStartPos; |
917 | 0 | } |
918 | 0 | while (nEndPosition <= nSentenceStartPos && nEndPosition < nTextLen); |
919 | 0 | if (nEndPosition > nTextLen) |
920 | 0 | nEndPosition = nTextLen; |
921 | 0 | return nEndPosition; |
922 | 0 | } |
923 | | |
924 | | |
925 | | void SAL_CALL GrammarCheckingIterator::resetIgnoreRules( ) |
926 | 0 | { |
927 | 0 | for (auto const& elem : m_aGCReferencesByService) |
928 | 0 | { |
929 | 0 | uno::Reference< linguistic2::XProofreader > xGC(elem.second); |
930 | 0 | if (xGC.is()) |
931 | 0 | xGC->resetIgnoreRules(); |
932 | 0 | } |
933 | 0 | } |
934 | | |
935 | | |
936 | | sal_Bool SAL_CALL GrammarCheckingIterator::isProofreading( |
937 | | const uno::Reference< uno::XInterface >& xDoc ) |
938 | 0 | { |
939 | | // ---- THREAD SAFE START ---- |
940 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
941 | |
|
942 | 0 | bool bRes = false; |
943 | |
|
944 | 0 | uno::Reference< lang::XComponent > xComponent( xDoc, uno::UNO_QUERY ); |
945 | 0 | if (xComponent.is()) |
946 | 0 | { |
947 | | // if the component was already used in one of the two calls to check text |
948 | | // i.e. in startGrammarChecking or checkGrammarAtPos it will be found in the |
949 | | // m_aDocIdMap unless the document already disposed. |
950 | | // If it is not found then it is not yet being checked (or requested to being checked) |
951 | 0 | const DocMap_t::const_iterator aIt( m_aDocIdMap.find( xComponent.get() ) ); |
952 | 0 | if (aIt != m_aDocIdMap.end()) |
953 | 0 | { |
954 | | // check in document is checked automatically in the background... |
955 | 0 | OUString aDocId = aIt->second; |
956 | 0 | if (!m_aCurCheckedDocId.isEmpty() && m_aCurCheckedDocId == aDocId) |
957 | 0 | { |
958 | | // an entry for that document was dequeued and is currently being checked. |
959 | 0 | bRes = true; |
960 | 0 | } |
961 | 0 | else |
962 | 0 | { |
963 | | // we need to check if there is an entry for that document in the queue... |
964 | | // That is the document is going to be checked sooner or later. |
965 | |
|
966 | 0 | sal_Int32 nSize = m_aFPEntriesQueue.size(); |
967 | 0 | for (sal_Int32 i = 0; i < nSize && !bRes; ++i) |
968 | 0 | { |
969 | 0 | if (aDocId == m_aFPEntriesQueue[i].m_aDocId) |
970 | 0 | bRes = true; |
971 | 0 | } |
972 | 0 | } |
973 | 0 | } |
974 | 0 | } |
975 | | // ---- THREAD SAFE END ---- |
976 | |
|
977 | 0 | return bRes; |
978 | 0 | } |
979 | | |
980 | | |
981 | | void SAL_CALL GrammarCheckingIterator::processLinguServiceEvent( |
982 | | const linguistic2::LinguServiceEvent& rLngSvcEvent ) |
983 | 0 | { |
984 | 0 | if (rLngSvcEvent.nEvent != linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN) |
985 | 0 | return; |
986 | | |
987 | 0 | try |
988 | 0 | { |
989 | 0 | uno::Reference< uno::XInterface > xThis( getXWeak() ); |
990 | 0 | linguistic2::LinguServiceEvent aEvent( xThis, linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN ); |
991 | 0 | m_aNotifyListeners.notifyEach( |
992 | 0 | &linguistic2::XLinguServiceEventListener::processLinguServiceEvent, |
993 | 0 | aEvent); |
994 | 0 | } |
995 | 0 | catch (uno::RuntimeException &) |
996 | 0 | { |
997 | 0 | throw; |
998 | 0 | } |
999 | 0 | catch (const ::uno::Exception &) |
1000 | 0 | { |
1001 | | // ignore |
1002 | 0 | TOOLS_WARN_EXCEPTION("linguistic", "processLinguServiceEvent"); |
1003 | 0 | } |
1004 | 0 | } |
1005 | | |
1006 | | |
1007 | | sal_Bool SAL_CALL GrammarCheckingIterator::addLinguServiceEventListener( |
1008 | | const uno::Reference< linguistic2::XLinguServiceEventListener >& xListener ) |
1009 | 0 | { |
1010 | 0 | if (xListener.is()) |
1011 | 0 | { |
1012 | 0 | m_aNotifyListeners.addInterface( xListener ); |
1013 | 0 | } |
1014 | 0 | return true; |
1015 | 0 | } |
1016 | | |
1017 | | |
1018 | | sal_Bool SAL_CALL GrammarCheckingIterator::removeLinguServiceEventListener( |
1019 | | const uno::Reference< linguistic2::XLinguServiceEventListener >& xListener ) |
1020 | 0 | { |
1021 | 0 | if (xListener.is()) |
1022 | 0 | { |
1023 | 0 | m_aNotifyListeners.removeInterface( xListener ); |
1024 | 0 | } |
1025 | 0 | return true; |
1026 | 0 | } |
1027 | | |
1028 | | |
1029 | | void SAL_CALL GrammarCheckingIterator::dispose() |
1030 | 0 | { |
1031 | 0 | lang::EventObject aEvt( static_cast<linguistic2::XProofreadingIterator *>(this) ); |
1032 | 0 | m_aEventListeners.disposeAndClear( aEvt ); |
1033 | |
|
1034 | 0 | TerminateThread(); |
1035 | | |
1036 | | // ---- THREAD SAFE START ---- |
1037 | 0 | { |
1038 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
1039 | | |
1040 | | // release all UNO references |
1041 | |
|
1042 | 0 | m_xBreakIterator.clear(); |
1043 | | |
1044 | | // clear containers with UNO references AND have those references released |
1045 | 0 | GCReferences_t aTmpEmpty1; |
1046 | 0 | DocMap_t aTmpEmpty2; |
1047 | 0 | FPQueue_t aTmpEmpty3; |
1048 | 0 | m_aGCReferencesByService.swap( aTmpEmpty1 ); |
1049 | 0 | m_aDocIdMap.swap( aTmpEmpty2 ); |
1050 | 0 | m_aFPEntriesQueue.swap( aTmpEmpty3 ); |
1051 | 0 | } |
1052 | | // ---- THREAD SAFE END ---- |
1053 | 0 | } |
1054 | | |
1055 | | |
1056 | | void SAL_CALL GrammarCheckingIterator::addEventListener( |
1057 | | const uno::Reference< lang::XEventListener >& xListener ) |
1058 | 0 | { |
1059 | 0 | if (xListener.is()) |
1060 | 0 | { |
1061 | 0 | m_aEventListeners.addInterface( xListener ); |
1062 | 0 | } |
1063 | 0 | } |
1064 | | |
1065 | | |
1066 | | void SAL_CALL GrammarCheckingIterator::removeEventListener( |
1067 | | const uno::Reference< lang::XEventListener >& xListener ) |
1068 | 0 | { |
1069 | 0 | if (xListener.is()) |
1070 | 0 | { |
1071 | 0 | m_aEventListeners.removeInterface( xListener ); |
1072 | 0 | } |
1073 | 0 | } |
1074 | | |
1075 | | |
1076 | | void SAL_CALL GrammarCheckingIterator::disposing( const lang::EventObject &rSource ) |
1077 | 0 | { |
1078 | | // if the component (document) is disposing release all references |
1079 | | //!! There is no need to remove entries from the queue that are from this document |
1080 | | //!! since the respectives xFlatParagraphs should become invalid (isModified() == true) |
1081 | | //!! and the call to xFlatParagraphIterator->getNextPara() will result in an empty reference. |
1082 | | //!! And if an entry is currently checked by a grammar checker upon return the results |
1083 | | //!! should be ignored. |
1084 | | //!! Also GetOrCreateDocId will not use that very same Id again... |
1085 | | //!! All of the above resulting in that we only have to get rid of the implementation pointer here. |
1086 | 0 | uno::Reference< lang::XComponent > xDoc( rSource.Source, uno::UNO_QUERY ); |
1087 | 0 | if (xDoc.is()) |
1088 | 0 | { |
1089 | | // ---- THREAD SAFE START ---- |
1090 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
1091 | 0 | m_aDocIdMap.erase( xDoc.get() ); |
1092 | | // ---- THREAD SAFE END ---- |
1093 | 0 | } |
1094 | 0 | } |
1095 | | |
1096 | | |
1097 | | uno::Reference< util::XChangesBatch > const & GrammarCheckingIterator::GetUpdateAccess() const |
1098 | 0 | { |
1099 | 0 | if (!m_xUpdateAccess.is()) |
1100 | 0 | { |
1101 | 0 | try |
1102 | 0 | { |
1103 | | // get configuration provider |
1104 | 0 | const uno::Reference< uno::XComponentContext >& xContext = comphelper::getProcessComponentContext(); |
1105 | 0 | uno::Reference< lang::XMultiServiceFactory > xConfigurationProvider = |
1106 | 0 | configuration::theDefaultProvider::get( xContext ); |
1107 | | |
1108 | | // get configuration update access |
1109 | 0 | beans::PropertyValue aValue; |
1110 | 0 | aValue.Name = "nodepath"; |
1111 | 0 | aValue.Value <<= u"org.openoffice.Office.Linguistic/ServiceManager"_ustr; |
1112 | 0 | uno::Sequence< uno::Any > aProps{ uno::Any(aValue) }; |
1113 | 0 | m_xUpdateAccess.set( |
1114 | 0 | xConfigurationProvider->createInstanceWithArguments( |
1115 | 0 | u"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr, aProps ), |
1116 | 0 | uno::UNO_QUERY_THROW ); |
1117 | 0 | } |
1118 | 0 | catch (uno::Exception &) |
1119 | 0 | { |
1120 | 0 | } |
1121 | 0 | } |
1122 | |
|
1123 | 0 | return m_xUpdateAccess; |
1124 | 0 | } |
1125 | | |
1126 | | |
1127 | | void GrammarCheckingIterator::GetConfiguredGCSvcs_Impl() |
1128 | 0 | { |
1129 | 0 | GCImplNames_t aTmpGCImplNamesByLang; |
1130 | |
|
1131 | 0 | try |
1132 | 0 | { |
1133 | | // get node names (locale iso strings) for configured grammar checkers |
1134 | 0 | uno::Reference< container::XNameAccess > xNA( GetUpdateAccess(), uno::UNO_QUERY_THROW ); |
1135 | 0 | xNA.set( xNA->getByName( u"GrammarCheckerList"_ustr ), uno::UNO_QUERY_THROW ); |
1136 | 0 | const uno::Sequence< OUString > aElementNames( xNA->getElementNames() ); |
1137 | |
|
1138 | 0 | for (const OUString& rElementName : aElementNames) |
1139 | 0 | { |
1140 | 0 | uno::Sequence< OUString > aImplNames; |
1141 | 0 | uno::Any aTmp( xNA->getByName( rElementName ) ); |
1142 | 0 | if (aTmp >>= aImplNames) |
1143 | 0 | { |
1144 | 0 | if (aImplNames.hasElements()) |
1145 | 0 | { |
1146 | | // only the first entry is used, there should be only one grammar checker per language |
1147 | 0 | aTmpGCImplNamesByLang[rElementName] = aImplNames[0]; |
1148 | 0 | } |
1149 | 0 | } |
1150 | 0 | else |
1151 | 0 | { |
1152 | 0 | SAL_WARN( "linguistic", "failed to get aImplNames. Wrong type?" ); |
1153 | 0 | } |
1154 | 0 | } |
1155 | 0 | } |
1156 | 0 | catch (uno::Exception const &) |
1157 | 0 | { |
1158 | 0 | TOOLS_WARN_EXCEPTION( "linguistic", "exception caught. Failed to get configured services" ); |
1159 | 0 | } |
1160 | | |
1161 | 0 | { |
1162 | | // ---- THREAD SAFE START ---- |
1163 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
1164 | 0 | m_aGCImplNamesByLang.swap(aTmpGCImplNamesByLang); |
1165 | | // ---- THREAD SAFE END ---- |
1166 | 0 | } |
1167 | 0 | } |
1168 | | |
1169 | | |
1170 | | sal_Bool SAL_CALL GrammarCheckingIterator::supportsService( |
1171 | | const OUString & rServiceName ) |
1172 | 0 | { |
1173 | 0 | return cppu::supportsService(this, rServiceName); |
1174 | 0 | } |
1175 | | |
1176 | | |
1177 | | OUString SAL_CALL GrammarCheckingIterator::getImplementationName( ) |
1178 | 0 | { |
1179 | 0 | return u"com.sun.star.lingu2.ProofreadingIterator"_ustr; |
1180 | 0 | } |
1181 | | |
1182 | | |
1183 | | uno::Sequence< OUString > SAL_CALL GrammarCheckingIterator::getSupportedServiceNames( ) |
1184 | 0 | { |
1185 | 0 | return { u"com.sun.star.linguistic2.ProofreadingIterator"_ustr }; |
1186 | 0 | } |
1187 | | |
1188 | | |
1189 | | void GrammarCheckingIterator::SetServiceList( |
1190 | | const lang::Locale &rLocale, |
1191 | | const uno::Sequence< OUString > &rSvcImplNames ) |
1192 | 0 | { |
1193 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
1194 | |
|
1195 | 0 | OUString sBcp47 = LanguageTag::convertToBcp47(rLocale, false); |
1196 | 0 | OUString aImplName; |
1197 | 0 | if (rSvcImplNames.hasElements()) |
1198 | 0 | aImplName = rSvcImplNames[0]; // there is only one grammar checker per language |
1199 | |
|
1200 | 0 | if (!LinguIsUnspecified(sBcp47) && !sBcp47.isEmpty()) |
1201 | 0 | { |
1202 | 0 | if (!aImplName.isEmpty()) |
1203 | 0 | m_aGCImplNamesByLang[sBcp47] = aImplName; |
1204 | 0 | else |
1205 | 0 | m_aGCImplNamesByLang.erase(sBcp47); |
1206 | 0 | } |
1207 | 0 | } |
1208 | | |
1209 | | |
1210 | | uno::Sequence< OUString > GrammarCheckingIterator::GetServiceList( |
1211 | | const lang::Locale &rLocale ) const |
1212 | 0 | { |
1213 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( MyMutex() ); |
1214 | |
|
1215 | 0 | const OUString aImplName = getServiceForLocale(rLocale).first; // there is only one grammar checker per language |
1216 | |
|
1217 | 0 | if (!aImplName.isEmpty()) |
1218 | 0 | return { aImplName }; |
1219 | 0 | return {}; |
1220 | 0 | } |
1221 | | |
1222 | | |
1223 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
1224 | | linguistic_GrammarCheckingIterator_get_implementation( |
1225 | | css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&) |
1226 | 0 | { |
1227 | 0 | return cppu::acquire(new GrammarCheckingIterator()); |
1228 | 0 | } |
1229 | | |
1230 | | |
1231 | | |
1232 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |