1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/macros.h>
21 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
22 #include <com/sun/star/container/XEnumeration.hpp>
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/container/XNameContainer.hpp>
25 #include <com/sun/star/container/XNameReplace.hpp>
26 #include <com/sun/star/configuration/theDefaultProvider.hpp>
27 #include <com/sun/star/i18n/BreakIterator.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.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/registry/XRegistryKey.hpp>
39 #include <com/sun/star/text/TextMarkupType.hpp>
40 #include <com/sun/star/text/TextMarkupDescriptor.hpp>
41 #include <com/sun/star/text/XTextMarkup.hpp>
42 #include <com/sun/star/text/XMultiTextMarkup.hpp>
43 #include <com/sun/star/text/XFlatParagraph.hpp>
44 #include <com/sun/star/text/XFlatParagraphIterator.hpp>
45 #include <com/sun/star/uno/XComponentContext.hpp>
46 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
48 #include <sal/config.h>
49 #include <osl/conditn.hxx>
50 #include <osl/thread.hxx>
51 #include <cppuhelper/implbase4.hxx>
52 #include <cppuhelper/implementationentry.hxx>
53 #include <cppuhelper/interfacecontainer.h>
54 #include <cppuhelper/factory.hxx>
55 #include <i18nlangtag/languagetag.hxx>
56 #include <comphelper/processfactory.hxx>
57 #include <comphelper/extract.hxx>
63 #include "linguistic/misc.hxx"
67 #include "gciterator.hxx"
69 using namespace linguistic
;
70 using namespace ::com::sun::star
;
72 // forward declarations
73 static OUString
GrammarCheckingIterator_getImplementationName() throw();
74 static uno::Sequence
< OUString
> GrammarCheckingIterator_getSupportedServiceNames() throw();
78 // white space list: obtained from the fonts.config.txt of a Linux system.
79 static sal_Unicode aWhiteSpaces
[] =
82 0x00a0, /* NO-BREAK SPACE */
83 0x00ad, /* SOFT HYPHEN */
84 0x115f, /* HANGUL CHOSEONG FILLER */
85 0x1160, /* HANGUL JUNGSEONG FILLER */
86 0x1680, /* OGHAM SPACE MARK */
89 0x2002, /* EN SPACE */
90 0x2003, /* EM SPACE */
91 0x2004, /* THREE-PER-EM SPACE */
92 0x2005, /* FOUR-PER-EM SPACE */
93 0x2006, /* SIX-PER-EM SPACE */
94 0x2007, /* FIGURE SPACE */
95 0x2008, /* PUNCTUATION SPACE */
96 0x2009, /* THIN SPACE */
97 0x200a, /* HAIR SPACE */
98 0x200b, /* ZERO WIDTH SPACE */
99 0x200c, /* ZERO WIDTH NON-JOINER */
100 0x200d, /* ZERO WIDTH JOINER */
101 0x200e, /* LEFT-TO-RIGHT MARK */
102 0x200f, /* RIGHT-TO-LEFT MARK */
103 0x2028, /* LINE SEPARATOR */
104 0x2029, /* PARAGRAPH SEPARATOR */
105 0x202a, /* LEFT-TO-RIGHT EMBEDDING */
106 0x202b, /* RIGHT-TO-LEFT EMBEDDING */
107 0x202c, /* POP DIRECTIONAL FORMATTING */
108 0x202d, /* LEFT-TO-RIGHT OVERRIDE */
109 0x202e, /* RIGHT-TO-LEFT OVERRIDE */
110 0x202f, /* NARROW NO-BREAK SPACE */
111 0x205f, /* MEDIUM MATHEMATICAL SPACE */
112 0x2060, /* WORD JOINER */
113 0x2061, /* FUNCTION APPLICATION */
114 0x2062, /* INVISIBLE TIMES */
115 0x2063, /* INVISIBLE SEPARATOR */
116 0x206A, /* INHIBIT SYMMETRIC SWAPPING */
117 0x206B, /* ACTIVATE SYMMETRIC SWAPPING */
118 0x206C, /* INHIBIT ARABIC FORM SHAPING */
119 0x206D, /* ACTIVATE ARABIC FORM SHAPING */
120 0x206E, /* NATIONAL DIGIT SHAPES */
121 0x206F, /* NOMINAL DIGIT SHAPES */
122 0x3000, /* IDEOGRAPHIC SPACE */
123 0x3164, /* HANGUL FILLER */
124 0xfeff, /* ZERO WIDTH NO-BREAK SPACE */
125 0xffa0, /* HALFWIDTH HANGUL FILLER */
126 0xfff9, /* INTERLINEAR ANNOTATION ANCHOR */
127 0xfffa, /* INTERLINEAR ANNOTATION SEPARATOR */
128 0xfffb /* INTERLINEAR ANNOTATION TERMINATOR */
131 static int nWhiteSpaces
= sizeof( aWhiteSpaces
) / sizeof( aWhiteSpaces
[0] );
133 static bool lcl_IsWhiteSpace( sal_Unicode cChar
)
136 for (int i
= 0; i
< nWhiteSpaces
&& !bFound
; ++i
)
138 if (cChar
== aWhiteSpaces
[i
])
144 static sal_Int32
lcl_SkipWhiteSpaces( const OUString
&rText
, sal_Int32 nStartPos
)
146 // note having nStartPos point right behind the string is OK since that one
147 // is a correct end-of-sentence position to be returned from a grammar checker...
149 const sal_Int32 nLen
= rText
.getLength();
150 bool bIllegalArgument
= false;
153 bIllegalArgument
= true;
156 if (nStartPos
> nLen
)
158 bIllegalArgument
= true;
161 if (bIllegalArgument
)
163 DBG_ASSERT( 0, "lcl_SkipWhiteSpaces: illegal arguments" );
166 sal_Int32 nRes
= nStartPos
;
167 if (0 <= nStartPos
&& nStartPos
< nLen
)
169 const sal_Unicode
*pText
= rText
.getStr() + nStartPos
;
170 while (nStartPos
< nLen
&& lcl_IsWhiteSpace( *pText
))
172 nRes
= pText
- rText
.getStr();
175 DBG_ASSERT( 0 <= nRes
&& nRes
<= nLen
, "lcl_SkipWhiteSpaces return value out of range" );
179 static sal_Int32
lcl_BacktraceWhiteSpaces( const OUString
&rText
, sal_Int32 nStartPos
)
181 // note: having nStartPos point right behind the string is OK since that one
182 // is a correct end-of-sentence position to be returned from a grammar checker...
184 const sal_Int32 nLen
= rText
.getLength();
185 bool bIllegalArgument
= false;
188 bIllegalArgument
= true;
191 if (nStartPos
> nLen
)
193 bIllegalArgument
= true;
196 if (bIllegalArgument
)
198 DBG_ASSERT( 0, "lcl_BacktraceWhiteSpaces: illegal arguments" );
201 sal_Int32 nRes
= nStartPos
;
202 sal_Int32 nPosBefore
= nStartPos
- 1;
203 const sal_Unicode
*pStart
= rText
.getStr();
204 if (0 <= nPosBefore
&& nPosBefore
< nLen
&& lcl_IsWhiteSpace( pStart
[ nPosBefore
] ))
206 nStartPos
= nPosBefore
;
207 if (0 <= nStartPos
&& nStartPos
< nLen
)
209 const sal_Unicode
*pText
= rText
.getStr() + nStartPos
;
210 while (pText
> pStart
&& lcl_IsWhiteSpace( *pText
))
212 // now add 1 since we want to point to the first char after the last char in the sentence...
213 nRes
= pText
- pStart
+ 1;
217 DBG_ASSERT( 0 <= nRes
&& nRes
<= nLen
, "lcl_BacktraceWhiteSpaces return value out of range" );
222 extern "C" void lcl_workerfunc (void * gci
)
224 ((GrammarCheckingIterator
*)gci
)->DequeueAndCheck();
227 static lang::Locale
lcl_GetPrimaryLanguageOfSentence(
228 uno::Reference
< text::XFlatParagraph
> xFlatPara
,
229 sal_Int32 nStartIndex
)
231 //get the language of the first word
232 return xFlatPara
->getLanguageOfText( nStartIndex
, 1 );
236 GrammarCheckingIterator::GrammarCheckingIterator() :
238 m_aCurCheckedDocId(),
239 m_bGCServicesChecked( sal_False
),
240 m_nDocIdCounter( 0 ),
241 m_nLastEndOfSentencePos( -1 ),
242 m_aEventListeners( MyMutex::get() ),
243 m_aNotifyListeners( MyMutex::get() )
245 m_thread
= osl_createThread( lcl_workerfunc
, this );
249 GrammarCheckingIterator::~GrammarCheckingIterator()
254 void GrammarCheckingIterator::TerminateThread()
258 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
262 m_aWakeUpThread
.set();
266 osl_joinWithThread(t
);
267 osl_destroyThread(t
);
271 sal_Int32
GrammarCheckingIterator::NextDocId()
273 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
274 m_nDocIdCounter
+= 1;
275 return m_nDocIdCounter
;
279 OUString
GrammarCheckingIterator::GetOrCreateDocId(
280 const uno::Reference
< lang::XComponent
> &xComponent
)
282 // internal method; will always be called with locked mutex
287 if (m_aDocIdMap
.find( xComponent
.get() ) != m_aDocIdMap
.end())
289 // return already existing entry
290 aRes
= m_aDocIdMap
[ xComponent
.get() ];
292 else // add new entry
294 sal_Int32 nRes
= NextDocId();
295 aRes
= OUString::valueOf( nRes
);
296 m_aDocIdMap
[ xComponent
.get() ] = aRes
;
297 xComponent
->addEventListener( this );
304 void GrammarCheckingIterator::AddEntry(
305 uno::WeakReference
< text::XFlatParagraphIterator
> xFlatParaIterator
,
306 uno::WeakReference
< text::XFlatParagraph
> xFlatPara
,
307 const OUString
& rDocId
,
308 sal_Int32 nStartIndex
,
309 sal_Bool bAutomatic
)
311 // we may not need/have a xFlatParaIterator (e.g. if checkGrammarAtPos was called)
312 // but we always need a xFlatPara...
313 uno::Reference
< text::XFlatParagraph
> xPara( xFlatPara
);
317 aNewFPEntry
.m_xParaIterator
= xFlatParaIterator
;
318 aNewFPEntry
.m_xPara
= xFlatPara
;
319 aNewFPEntry
.m_aDocId
= rDocId
;
320 aNewFPEntry
.m_nStartIndex
= nStartIndex
;
321 aNewFPEntry
.m_bAutomatic
= bAutomatic
;
323 // add new entry to the end of this queue
324 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
325 m_aFPEntriesQueue
.push_back( aNewFPEntry
);
327 // wake up the thread in order to do grammar checking
328 m_aWakeUpThread
.set();
333 void GrammarCheckingIterator::ProcessResult(
334 const linguistic2::ProofreadingResult
&rRes
,
335 const uno::Reference
< text::XFlatParagraphIterator
> &rxFlatParagraphIterator
,
336 bool bIsAutomaticChecking
)
338 DBG_ASSERT( rRes
.xFlatParagraph
.is(), "xFlatParagraph is missing" );
339 //no guard necessary as no members are used
340 sal_Bool bContinueWithNextPara
= sal_False
;
341 if (!rRes
.xFlatParagraph
.is() || rRes
.xFlatParagraph
->isModified())
343 // if paragraph was modified/deleted meanwhile continue with the next one...
344 bContinueWithNextPara
= sal_True
;
346 else // paragraph is still unchanged...
348 // mark found errors...
350 sal_Int32 nTextLen
= rRes
.aText
.getLength();
351 bool bBoundariesOk
= 0 <= rRes
.nStartOfSentencePosition
&& rRes
.nStartOfSentencePosition
<= nTextLen
&&
352 0 <= rRes
.nBehindEndOfSentencePosition
&& rRes
.nBehindEndOfSentencePosition
<= nTextLen
&&
353 0 <= rRes
.nStartOfNextSentencePosition
&& rRes
.nStartOfNextSentencePosition
<= nTextLen
&&
354 rRes
.nStartOfSentencePosition
<= rRes
.nBehindEndOfSentencePosition
&&
355 rRes
.nBehindEndOfSentencePosition
<= rRes
.nStartOfNextSentencePosition
;
356 (void) bBoundariesOk
;
357 DBG_ASSERT( bBoundariesOk
, "inconsistent sentence boundaries" );
358 uno::Sequence
< linguistic2::SingleProofreadingError
> aErrors
= rRes
.aErrors
;
360 uno::Reference
< text::XMultiTextMarkup
> xMulti( rRes
.xFlatParagraph
, uno::UNO_QUERY
);
361 if (xMulti
.is()) // use new API for markups
365 // length = number of found errors + 1 sentence markup
366 sal_Int32 nErrors
= rRes
.aErrors
.getLength();
367 uno::Sequence
< text::TextMarkupDescriptor
> aDescriptors( nErrors
+ 1 );
368 text::TextMarkupDescriptor
* pDescriptors
= aDescriptors
.getArray();
370 // at pos 0 .. nErrors-1 -> all grammar errors
371 for (sal_Int32 i
= 0; i
< nErrors
; ++i
)
373 const linguistic2::SingleProofreadingError
&rError
= rRes
.aErrors
[i
];
374 text::TextMarkupDescriptor
&rDesc
= aDescriptors
[i
];
376 rDesc
.nType
= rError
.nErrorType
;
377 rDesc
.nOffset
= rError
.nErrorStart
;
378 rDesc
.nLength
= rError
.nErrorLength
;
380 // the proofreader may return SPELLING but right now our core
381 // does only handle PROOFREADING if the result is from the proofreader...
382 // (later on we may wish to color spelling errors found by the proofreader
383 // differently for example. But no special handling right now.
384 if (rDesc
.nType
== text::TextMarkupType::SPELLCHECK
)
385 rDesc
.nType
= text::TextMarkupType::PROOFREADING
;
388 // at pos nErrors -> sentence markup
389 // nSentenceLength: includes the white-spaces following the sentence end...
390 const sal_Int32 nSentenceLength
= rRes
.nStartOfNextSentencePosition
- rRes
.nStartOfSentencePosition
;
391 pDescriptors
[ nErrors
].nType
= text::TextMarkupType::SENTENCE
;
392 pDescriptors
[ nErrors
].nOffset
= rRes
.nStartOfSentencePosition
;
393 pDescriptors
[ nErrors
].nLength
= nSentenceLength
;
395 xMulti
->commitMultiTextMarkup( aDescriptors
) ;
397 catch (lang::IllegalArgumentException
&)
399 OSL_FAIL( "commitMultiTextMarkup: IllegalArgumentException exception caught" );
403 // other sentences left to be checked in this paragraph?
404 if (rRes
.nStartOfNextSentencePosition
< rRes
.aText
.getLength())
406 AddEntry( rxFlatParagraphIterator
, rRes
.xFlatParagraph
, rRes
.aDocumentIdentifier
, rRes
.nStartOfNextSentencePosition
, bIsAutomaticChecking
);
408 else // current paragraph finished
410 // set "already checked" flag for the current flat paragraph
411 if (rRes
.xFlatParagraph
.is())
412 rRes
.xFlatParagraph
->setChecked( text::TextMarkupType::PROOFREADING
, true );
414 bContinueWithNextPara
= sal_True
;
418 if (bContinueWithNextPara
)
420 // we need to continue with the next paragraph
421 uno::Reference
< text::XFlatParagraph
> xFlatParaNext
;
422 if (rxFlatParagraphIterator
.is())
423 xFlatParaNext
= rxFlatParagraphIterator
->getNextPara();
425 AddEntry( rxFlatParagraphIterator
, xFlatParaNext
, rRes
.aDocumentIdentifier
, 0, bIsAutomaticChecking
);
431 uno::Reference
< linguistic2::XProofreader
> GrammarCheckingIterator::GetGrammarChecker(
432 const lang::Locale
&rLocale
)
435 uno::Reference
< linguistic2::XProofreader
> xRes
;
437 // ---- THREAD SAFE START ----
438 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
440 // check supported locales for each grammarchecker if not already done
441 if (!m_bGCServicesChecked
)
443 GetConfiguredGCSvcs_Impl();
444 m_bGCServicesChecked
= sal_True
;
447 const LanguageType nLang
= LanguageTag( rLocale
).getLanguageType( false);
448 GCImplNames_t::const_iterator
aLangIt( m_aGCImplNamesByLang
.find( nLang
) );
449 if (aLangIt
!= m_aGCImplNamesByLang
.end()) // matching configured language found?
451 OUString
aSvcImplName( aLangIt
->second
);
452 GCReferences_t::const_iterator
aImplNameIt( m_aGCReferencesByService
.find( aSvcImplName
) );
453 if (aImplNameIt
!= m_aGCReferencesByService
.end()) // matching impl name found?
455 xRes
= aImplNameIt
->second
;
457 else // the service is to be instatiated here for the first time...
461 uno::Reference
< uno::XComponentContext
> xContext( comphelper::getProcessComponentContext() );
462 uno::Reference
< linguistic2::XProofreader
> xGC(
463 xContext
->getServiceManager()->createInstanceWithContext(aSvcImplName
, xContext
),
464 uno::UNO_QUERY_THROW
);
465 uno::Reference
< linguistic2::XSupportedLocales
> xSuppLoc( xGC
, uno::UNO_QUERY_THROW
);
467 if (xSuppLoc
->hasLocale( rLocale
))
469 m_aGCReferencesByService
[ aSvcImplName
] = xGC
;
472 uno::Reference
< linguistic2::XLinguServiceEventBroadcaster
> xBC( xGC
, uno::UNO_QUERY
);
474 xBC
->addLinguServiceEventListener( this );
478 DBG_ASSERT( 0, "grammar checker does not support required locale" );
481 catch (uno::Exception
&)
483 DBG_ASSERT( 0, "instantiating grammar checker failed" );
487 // ---- THREAD SAFE END ----
493 void GrammarCheckingIterator::DequeueAndCheck()
495 uno::Sequence
< sal_Int32
> aLangPortions
;
496 uno::Sequence
< lang::Locale
> aLangPortionsLocale
;
500 // ---- THREAD SAFE START ----
501 bool bQueueEmpty
= false;
503 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
508 bQueueEmpty
= m_aFPEntriesQueue
.empty();
510 // ---- THREAD SAFE END ----
514 uno::Reference
< text::XFlatParagraphIterator
> xFPIterator
;
515 uno::Reference
< text::XFlatParagraph
> xFlatPara
;
516 FPEntry aFPEntryItem
;
518 sal_Bool bModified
= sal_False
;
519 // ---- THREAD SAFE START ----
521 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
522 aFPEntryItem
= m_aFPEntriesQueue
.front();
523 xFPIterator
= aFPEntryItem
.m_xParaIterator
;
524 xFlatPara
= aFPEntryItem
.m_xPara
;
525 m_aCurCheckedDocId
= aFPEntryItem
.m_aDocId
;
526 aCurDocId
= m_aCurCheckedDocId
;
528 m_aFPEntriesQueue
.pop_front();
530 // ---- THREAD SAFE END ----
532 if (xFlatPara
.is() && xFPIterator
.is())
534 OUString
aCurTxt( xFlatPara
->getText() );
535 lang::Locale aCurLocale
= lcl_GetPrimaryLanguageOfSentence( xFlatPara
, aFPEntryItem
.m_nStartIndex
);
537 bModified
= xFlatPara
->isModified();
540 // ---- THREAD SAFE START ----
541 ::osl::ClearableGuard
< ::osl::Mutex
> aGuard( MyMutex::get() );
543 sal_Int32 nStartPos
= aFPEntryItem
.m_nStartIndex
;
544 sal_Int32 nSuggestedEnd
= GetSuggestedEndOfSentence( aCurTxt
, nStartPos
, aCurLocale
);
545 DBG_ASSERT( (nSuggestedEnd
== 0 && aCurTxt
.isEmpty()) || nSuggestedEnd
> nStartPos
,
546 "nSuggestedEndOfSentencePos calculation failed?" );
548 linguistic2::ProofreadingResult aRes
;
550 uno::Reference
< linguistic2::XProofreader
> xGC( GetGrammarChecker( aCurLocale
), uno::UNO_QUERY
);
554 uno::Sequence
< beans::PropertyValue
> aEmptyProps
;
555 aRes
= xGC
->doProofreading( aCurDocId
, aCurTxt
, aCurLocale
, nStartPos
, nSuggestedEnd
, aEmptyProps
);
557 //!! work-around to prevent looping if the grammar checker
558 //!! failed to properly identify the sentence end
560 aRes
.nBehindEndOfSentencePosition
<= nStartPos
&&
561 aRes
.nBehindEndOfSentencePosition
!= nSuggestedEnd
564 DBG_ASSERT( 0, "!! Grammarchecker failed to provide end of sentence !!" );
565 aRes
.nBehindEndOfSentencePosition
= nSuggestedEnd
;
568 aRes
.xFlatParagraph
= xFlatPara
;
569 aRes
.nStartOfSentencePosition
= nStartPos
;
573 // no grammar checker -> no error
574 // but we need to provide the data below in order to continue with the next sentence
575 aRes
.aDocumentIdentifier
= aCurDocId
;
576 aRes
.xFlatParagraph
= xFlatPara
;
577 aRes
.aText
= aCurTxt
;
578 aRes
.aLocale
= aCurLocale
;
579 aRes
.nStartOfSentencePosition
= nStartPos
;
580 aRes
.nBehindEndOfSentencePosition
= nSuggestedEnd
;
582 aRes
.nStartOfNextSentencePosition
= lcl_SkipWhiteSpaces( aCurTxt
, aRes
.nBehindEndOfSentencePosition
);
583 aRes
.nBehindEndOfSentencePosition
= lcl_BacktraceWhiteSpaces( aCurTxt
, aRes
.nStartOfNextSentencePosition
);
585 //guard has to be cleared as ProcessResult calls out of this class
587 ProcessResult( aRes
, xFPIterator
, aFPEntryItem
.m_bAutomatic
);
588 // ---- THREAD SAFE END ----
592 // the paragraph changed meanwhile... (and maybe is still edited)
593 // thus we simply continue to ask for the next to be checked.
594 uno::Reference
< text::XFlatParagraph
> xFlatParaNext( xFPIterator
->getNextPara() );
595 AddEntry( xFPIterator
, xFlatParaNext
, aCurDocId
, 0, aFPEntryItem
.m_bAutomatic
);
599 // ---- THREAD SAFE START ----
601 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
602 m_aCurCheckedDocId
= OUString();
604 // ---- THREAD SAFE END ----
608 // ---- THREAD SAFE START ----
610 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
615 // Check queue state again
616 if (m_aFPEntriesQueue
.empty())
617 m_aWakeUpThread
.reset();
619 // ---- THREAD SAFE END ----
621 //if the queue is empty
622 // IMPORTANT: Don't call condition.wait() with locked
623 // mutex. Otherwise you would keep out other threads
624 // to add entries to the queue! A condition is thread-
626 m_aWakeUpThread
.wait();
632 void SAL_CALL
GrammarCheckingIterator::startProofreading(
633 const uno::Reference
< ::uno::XInterface
> & xDoc
,
634 const uno::Reference
< text::XFlatParagraphIteratorProvider
> & xIteratorProvider
)
635 throw (uno::RuntimeException
, lang::IllegalArgumentException
)
637 // get paragraph to start checking with
638 const bool bAutomatic
= true;
639 uno::Reference
<text::XFlatParagraphIterator
> xFPIterator
= xIteratorProvider
->getFlatParagraphIterator(
640 text::TextMarkupType::PROOFREADING
, bAutomatic
);
641 uno::Reference
< text::XFlatParagraph
> xPara( xFPIterator
.is()? xFPIterator
->getFirstPara() : NULL
);
642 uno::Reference
< lang::XComponent
> xComponent( xDoc
, uno::UNO_QUERY
);
644 // ---- THREAD SAFE START ----
645 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
646 if (xPara
.is() && xComponent
.is())
648 OUString aDocId
= GetOrCreateDocId( xComponent
);
650 // create new entry and add it to queue
651 AddEntry( xFPIterator
, xPara
, aDocId
, 0, bAutomatic
);
653 // ---- THREAD SAFE END ----
657 linguistic2::ProofreadingResult SAL_CALL
GrammarCheckingIterator::checkSentenceAtPosition(
658 const uno::Reference
< uno::XInterface
>& xDoc
,
659 const uno::Reference
< text::XFlatParagraph
>& xFlatPara
,
660 const OUString
& rText
,
661 const lang::Locale
& rLocale
,
662 sal_Int32 nStartOfSentencePos
,
663 sal_Int32 nSuggestedEndOfSentencePos
,
664 sal_Int32 nErrorPosInPara
)
665 throw (lang::IllegalArgumentException
, uno::RuntimeException
)
669 // for the context menu...
671 linguistic2::ProofreadingResult aRes
;
673 uno::Reference
< lang::XComponent
> xComponent( xDoc
, uno::UNO_QUERY
);
674 if (xFlatPara
.is() && xComponent
.is() &&
675 ( nErrorPosInPara
< 0 || nErrorPosInPara
< rText
.getLength()))
677 // iterate through paragraph until we find the sentence we are interested in
678 linguistic2::ProofreadingResult aTmpRes
;
679 sal_Int32 nStartPos
= nStartOfSentencePos
>= 0 ? nStartOfSentencePos
: 0;
684 lang::Locale aCurLocale
= lcl_GetPrimaryLanguageOfSentence( xFlatPara
, nStartPos
);
685 sal_Int32 nOldStartOfSentencePos
= nStartPos
;
686 uno::Reference
< linguistic2::XProofreader
> xGC
;
689 // ---- THREAD SAFE START ----
691 ::osl::ClearableGuard
< ::osl::Mutex
> aGuard( MyMutex::get() );
692 aDocId
= GetOrCreateDocId( xComponent
);
693 nSuggestedEndOfSentencePos
= GetSuggestedEndOfSentence( rText
, nStartPos
, aCurLocale
);
694 DBG_ASSERT( nSuggestedEndOfSentencePos
> nStartPos
, "nSuggestedEndOfSentencePos calculation failed?" );
696 xGC
= GetGrammarChecker( aCurLocale
);
698 // ---- THREAD SAFE START ----
699 sal_Int32 nEndPos
= -1;
702 uno::Sequence
< beans::PropertyValue
> aEmptyProps
;
703 aTmpRes
= xGC
->doProofreading( aDocId
, rText
, aCurLocale
, nStartPos
, nSuggestedEndOfSentencePos
, aEmptyProps
);
705 //!! work-around to prevent looping if the grammar checker
706 //!! failed to properly identify the sentence end
707 if (aTmpRes
.nBehindEndOfSentencePosition
<= nStartPos
)
709 DBG_ASSERT( 0, "!! Grammarchecker failed to provide end of sentence !!" );
710 aTmpRes
.nBehindEndOfSentencePosition
= nSuggestedEndOfSentencePos
;
713 aTmpRes
.xFlatParagraph
= xFlatPara
;
714 aTmpRes
.nStartOfSentencePosition
= nStartPos
;
715 nEndPos
= aTmpRes
.nBehindEndOfSentencePosition
;
717 if ((nErrorPosInPara
< 0 || nStartPos
<= nErrorPosInPara
) && nErrorPosInPara
< nEndPos
)
720 if (nEndPos
== -1) // no result from grammar checker
721 nEndPos
= nSuggestedEndOfSentencePos
;
722 nStartPos
= lcl_SkipWhiteSpaces( rText
, nEndPos
);
723 aTmpRes
.nBehindEndOfSentencePosition
= nEndPos
;
724 aTmpRes
.nStartOfNextSentencePosition
= nStartPos
;
725 aTmpRes
.nBehindEndOfSentencePosition
= lcl_BacktraceWhiteSpaces( rText
, aTmpRes
.nStartOfNextSentencePosition
);
727 // prevent endless loop by forcefully advancing if needs be...
728 if (nStartPos
<= nOldStartOfSentencePos
)
730 DBG_ASSERT( 0, "end-of-sentence detection failed?" );
731 nStartPos
= nOldStartOfSentencePos
+ 1;
734 while (!bFound
&& nStartPos
< rText
.getLength());
736 if (bFound
&& !xFlatPara
->isModified())
744 sal_Int32
GrammarCheckingIterator::GetSuggestedEndOfSentence(
745 const OUString
&rText
,
746 sal_Int32 nSentenceStartPos
,
747 const lang::Locale
&rLocale
)
749 // internal method; will always be called with locked mutex
751 if (!m_xBreakIterator
.is())
753 uno::Reference
< uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
754 m_xBreakIterator
= i18n::BreakIterator::create(xContext
);
756 sal_Int32 nTextLen
= rText
.getLength();
757 sal_Int32
nEndPosition(0);
758 sal_Int32 nTmpStartPos
= nSentenceStartPos
;
761 sal_Int32
const nPrevEndPosition(nEndPosition
);
762 nEndPosition
= nTextLen
;
763 if (nTmpStartPos
< nTextLen
)
765 nEndPosition
= m_xBreakIterator
->endOfSentence( rText
, nTmpStartPos
, rLocale
);
766 if (nEndPosition
<= nPrevEndPosition
)
768 // fdo#68750 if there's no progress at all then presumably
769 // there's no end of sentence in this paragraph so just
770 // set the end position to end of paragraph
771 nEndPosition
= nTextLen
;
774 if (nEndPosition
< 0)
775 nEndPosition
= nTextLen
;
779 while (nEndPosition
<= nSentenceStartPos
&& nEndPosition
< nTextLen
);
780 if (nEndPosition
> nTextLen
)
781 nEndPosition
= nTextLen
;
786 void SAL_CALL
GrammarCheckingIterator::resetIgnoreRules( )
787 throw (uno::RuntimeException
)
789 GCReferences_t::iterator
aIt( m_aGCReferencesByService
.begin() );
790 while (aIt
!= m_aGCReferencesByService
.end())
792 uno::Reference
< linguistic2::XProofreader
> xGC( aIt
->second
);
794 xGC
->resetIgnoreRules();
800 sal_Bool SAL_CALL
GrammarCheckingIterator::isProofreading(
801 const uno::Reference
< uno::XInterface
>& xDoc
)
802 throw (uno::RuntimeException
)
804 // ---- THREAD SAFE START ----
805 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
807 sal_Bool bRes
= sal_False
;
809 uno::Reference
< lang::XComponent
> xComponent( xDoc
, uno::UNO_QUERY
);
812 // if the component was already used in one of the two calls to check text
813 // i.e. in startGrammarChecking or checkGrammarAtPos it will be found in the
814 // m_aDocIdMap unless the document already disposed.
815 // If it is not found then it is not yet being checked (or requested to being checked)
816 const DocMap_t::const_iterator
aIt( m_aDocIdMap
.find( xComponent
.get() ) );
817 if (aIt
!= m_aDocIdMap
.end())
819 // check in document is checked automatically in the background...
820 OUString aDocId
= aIt
->second
;
821 if (!m_aCurCheckedDocId
.isEmpty() && m_aCurCheckedDocId
== aDocId
)
823 // an entry for that document was dequed and is currently being checked.
828 // we need to check if there is an entry for that document in the queue...
829 // That is the document is going to be checked sooner or later.
831 sal_Int32 nSize
= m_aFPEntriesQueue
.size();
832 for (sal_Int32 i
= 0; i
< nSize
&& !bRes
; ++i
)
834 if (aDocId
== m_aFPEntriesQueue
[i
].m_aDocId
)
840 // ---- THREAD SAFE END ----
846 void SAL_CALL
GrammarCheckingIterator::processLinguServiceEvent(
847 const linguistic2::LinguServiceEvent
& rLngSvcEvent
)
848 throw (uno::RuntimeException
)
850 if (rLngSvcEvent
.nEvent
== linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN
)
854 uno::Reference
< uno::XInterface
> xThis( dynamic_cast< XLinguServiceEventBroadcaster
* >(this) );
855 linguistic2::LinguServiceEvent
aEvent( xThis
, linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN
);
856 m_aNotifyListeners
.notifyEach(
857 &linguistic2::XLinguServiceEventListener::processLinguServiceEvent
,
860 catch (uno::RuntimeException
&)
864 catch (const ::uno::Exception
&rE
)
868 DBG_WARNING1("processLinguServiceEvent: exception:\n%s",
869 OUStringToOString(rE
.Message
, RTL_TEXTENCODING_UTF8
).getStr());
875 sal_Bool SAL_CALL
GrammarCheckingIterator::addLinguServiceEventListener(
876 const uno::Reference
< linguistic2::XLinguServiceEventListener
>& xListener
)
877 throw (uno::RuntimeException
)
881 m_aNotifyListeners
.addInterface( xListener
);
887 sal_Bool SAL_CALL
GrammarCheckingIterator::removeLinguServiceEventListener(
888 const uno::Reference
< linguistic2::XLinguServiceEventListener
>& xListener
)
889 throw (uno::RuntimeException
)
893 m_aNotifyListeners
.removeInterface( xListener
);
899 void SAL_CALL
GrammarCheckingIterator::dispose()
900 throw (uno::RuntimeException
)
902 lang::EventObject
aEvt( (linguistic2::XProofreadingIterator
*) this );
903 m_aEventListeners
.disposeAndClear( aEvt
);
907 // ---- THREAD SAFE START ----
909 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
911 // releaase all UNO references
913 m_xBreakIterator
.clear();
915 // clear containers with UNO references AND have those references released
916 GCReferences_t aTmpEmpty1
;
918 FPQueue_t aTmpEmpty3
;
919 m_aGCReferencesByService
.swap( aTmpEmpty1
);
920 m_aDocIdMap
.swap( aTmpEmpty2
);
921 m_aFPEntriesQueue
.swap( aTmpEmpty3
);
923 // ---- THREAD SAFE END ----
927 void SAL_CALL
GrammarCheckingIterator::addEventListener(
928 const uno::Reference
< lang::XEventListener
>& xListener
)
929 throw (uno::RuntimeException
)
933 m_aEventListeners
.addInterface( xListener
);
938 void SAL_CALL
GrammarCheckingIterator::removeEventListener(
939 const uno::Reference
< lang::XEventListener
>& xListener
)
940 throw (uno::RuntimeException
)
944 m_aEventListeners
.removeInterface( xListener
);
949 void SAL_CALL
GrammarCheckingIterator::disposing( const lang::EventObject
&rSource
)
950 throw (uno::RuntimeException
)
952 // if the component (document) is disposing release all references
953 //!! There is no need to remove entries from the queue that are from this document
954 //!! since the respectives xFlatParagraphs should become invalid (isModified() == true)
955 //!! and the call to xFlatParagraphIterator->getNextPara() will result in an empty reference.
956 //!! And if an entry is currently checked by a grammar checker upon return the results
957 //!! should be ignored.
958 //!! Also GetOrCreateDocId will not use that very same Id again...
959 //!! All of the above resulting in that we only have to get rid of the implementation pointer here.
960 uno::Reference
< lang::XComponent
> xDoc( rSource
.Source
, uno::UNO_QUERY
);
963 // ---- THREAD SAFE START ----
964 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
965 m_aDocIdMap
.erase( xDoc
.get() );
966 // ---- THREAD SAFE END ----
971 uno::Reference
< util::XChangesBatch
> GrammarCheckingIterator::GetUpdateAccess() const
973 if (!m_xUpdateAccess
.is())
977 // get configuration provider
978 uno::Reference
< uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
979 uno::Reference
< lang::XMultiServiceFactory
> xConfigurationProvider
=
980 configuration::theDefaultProvider::get( xContext
);
982 // get configuration update access
983 beans::PropertyValue aValue
;
984 aValue
.Name
= "nodepath";
985 aValue
.Value
= uno::makeAny( OUString("org.openoffice.Office.Linguistic/ServiceManager") );
986 uno::Sequence
< uno::Any
> aProps(1);
987 aProps
[0] <<= aValue
;
988 m_xUpdateAccess
= uno::Reference
< util::XChangesBatch
>(
989 xConfigurationProvider
->createInstanceWithArguments(
990 "com.sun.star.configuration.ConfigurationUpdateAccess", aProps
),
991 uno::UNO_QUERY_THROW
);
993 catch (uno::Exception
&)
998 return m_xUpdateAccess
;
1002 void GrammarCheckingIterator::GetConfiguredGCSvcs_Impl()
1004 GCImplNames_t aTmpGCImplNamesByLang
;
1008 // get node names (locale iso strings) for configured grammar checkers
1009 uno::Reference
< container::XNameAccess
> xNA( GetUpdateAccess(), uno::UNO_QUERY_THROW
);
1010 xNA
.set( xNA
->getByName( "GrammarCheckerList" ), uno::UNO_QUERY_THROW
);
1011 const uno::Sequence
< OUString
> aElementNames( xNA
->getElementNames() );
1012 const OUString
*pElementNames
= aElementNames
.getConstArray();
1014 sal_Int32 nLen
= aElementNames
.getLength();
1015 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
1017 uno::Sequence
< OUString
> aImplNames
;
1018 uno::Any
aTmp( xNA
->getByName( pElementNames
[i
] ) );
1019 if (aTmp
>>= aImplNames
)
1021 if (aImplNames
.getLength() > 0)
1023 // only the first entry is used, there should be only one grammar checker per language
1024 const OUString
aImplName( aImplNames
[0] );
1025 const LanguageType nLang
= LanguageTag( pElementNames
[i
] ).getLanguageType();
1026 aTmpGCImplNamesByLang
[ nLang
] = aImplName
;
1031 DBG_ASSERT( 0, "failed to get aImplNames. Wrong type?" );
1035 catch (uno::Exception
&)
1037 DBG_ASSERT( 0, "exception caught. Failed to get configured services" );
1041 // ---- THREAD SAFE START ----
1042 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
1043 m_aGCImplNamesByLang
= aTmpGCImplNamesByLang
;
1044 // ---- THREAD SAFE END ----
1051 sal_Bool SAL_CALL
GrammarCheckingIterator::supportsService(
1052 const OUString
& rServiceName
)
1053 throw(uno::RuntimeException
)
1055 uno::Sequence
< OUString
> aSNL
= getSupportedServiceNames();
1056 const OUString
* pArray
= aSNL
.getConstArray();
1057 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); ++i
)
1058 if( pArray
[i
] == rServiceName
)
1064 OUString SAL_CALL
GrammarCheckingIterator::getImplementationName( ) throw (uno::RuntimeException
)
1066 return GrammarCheckingIterator_getImplementationName();
1070 uno::Sequence
< OUString
> SAL_CALL
GrammarCheckingIterator::getSupportedServiceNames( ) throw (uno::RuntimeException
)
1072 return GrammarCheckingIterator_getSupportedServiceNames();
1076 void GrammarCheckingIterator::SetServiceList(
1077 const lang::Locale
&rLocale
,
1078 const uno::Sequence
< OUString
> &rSvcImplNames
)
1080 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
1082 LanguageType nLanguage
= LinguLocaleToLanguage( rLocale
);
1084 if (rSvcImplNames
.getLength() > 0)
1085 aImplName
= rSvcImplNames
[0]; // there is only one grammar checker per language
1087 if (!LinguIsUnspecified(nLanguage
) && nLanguage
!= LANGUAGE_DONTKNOW
)
1089 if (!aImplName
.isEmpty())
1090 m_aGCImplNamesByLang
[ nLanguage
] = aImplName
;
1092 m_aGCImplNamesByLang
.erase( nLanguage
);
1097 uno::Sequence
< OUString
> GrammarCheckingIterator::GetServiceList(
1098 const lang::Locale
&rLocale
) const
1100 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex::get() );
1102 uno::Sequence
< OUString
> aRes(1);
1104 OUString aImplName
; // there is only one grammar checker per language
1105 LanguageType nLang
= LinguLocaleToLanguage( rLocale
);
1106 GCImplNames_t::const_iterator
aIt( m_aGCImplNamesByLang
.find( nLang
) );
1107 if (aIt
!= m_aGCImplNamesByLang
.end())
1108 aImplName
= aIt
->second
;
1110 if (!aImplName
.isEmpty())
1111 aRes
[0] = aImplName
;
1119 LinguDispatcher::DspType
GrammarCheckingIterator::GetDspType() const
1127 static OUString
GrammarCheckingIterator_getImplementationName() throw()
1129 return OUString( "com.sun.star.lingu2.ProofreadingIterator" );
1133 static uno::Sequence
< OUString
> GrammarCheckingIterator_getSupportedServiceNames() throw()
1135 uno::Sequence
< OUString
> aSNS( 1 );
1136 aSNS
[0] = "com.sun.star.linguistic2.ProofreadingIterator";
1141 static uno::Reference
< uno::XInterface
> SAL_CALL
GrammarCheckingIterator_createInstance(
1142 const uno::Reference
< lang::XMultiServiceFactory
> & /*rxSMgr*/ )
1143 throw(uno::Exception
)
1145 return static_cast< ::cppu::OWeakObject
* >(new GrammarCheckingIterator());
1149 void * SAL_CALL
GrammarCheckingIterator_getFactory(
1150 const sal_Char
*pImplName
,
1151 lang::XMultiServiceFactory
*pServiceManager
,
1152 void * /*pRegistryKey*/ )
1155 if ( !GrammarCheckingIterator_getImplementationName().compareToAscii( pImplName
) )
1157 uno::Reference
< lang::XSingleServiceFactory
> xFactory
=
1158 cppu::createOneInstanceFactory(
1160 GrammarCheckingIterator_getImplementationName(),
1161 GrammarCheckingIterator_createInstance
,
1162 GrammarCheckingIterator_getSupportedServiceNames());
1163 // acquire, because we return an interface pointer instead of a reference
1164 xFactory
->acquire();
1165 pRet
= xFactory
.get();
1170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */