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