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 
28*cdf0e10cSrcweir #ifndef _LINGUISTIC_GRAMMARCHECKINGITERATOR_HXX_
29*cdf0e10cSrcweir #define _LINGUISTIC_GRAMMARCHECKINGITERATOR_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <com/sun/star/i18n/XBreakIterator.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/lang/XEventListener.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/linguistic2/XProofreadingIterator.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <cppuhelper/implbase5.hxx>
42*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
43*cdf0e10cSrcweir #include <osl/mutex.hxx>
44*cdf0e10cSrcweir #include <osl/conditn.hxx>
45*cdf0e10cSrcweir #include <rtl/instance.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <map>
48*cdf0e10cSrcweir #include <deque>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include "defs.hxx"
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir struct FPEntry
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     // flat paragraph iterator
58*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIterator > m_xParaIterator;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     // flat paragraph
61*cdf0e10cSrcweir     ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraph > m_xPara;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     // document ID to identify different documents
64*cdf0e10cSrcweir     ::rtl::OUString	m_aDocId;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     // the starting position to be checked
67*cdf0e10cSrcweir     sal_Int32     	m_nStartIndex;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     // the flag to identify whether the document does automatical grammar checking
70*cdf0e10cSrcweir     sal_Bool      	m_bAutomatic;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     FPEntry()
73*cdf0e10cSrcweir         : m_aDocId()
74*cdf0e10cSrcweir         , m_nStartIndex( 0 )
75*cdf0e10cSrcweir         , m_bAutomatic( 0 )
76*cdf0e10cSrcweir     {
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir };
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir class GrammarCheckingIterator:
85*cdf0e10cSrcweir     public cppu::WeakImplHelper5
86*cdf0e10cSrcweir     <
87*cdf0e10cSrcweir         ::com::sun::star::linguistic2::XProofreadingIterator,
88*cdf0e10cSrcweir         ::com::sun::star::linguistic2::XLinguServiceEventListener,
89*cdf0e10cSrcweir         ::com::sun::star::linguistic2::XLinguServiceEventBroadcaster,
90*cdf0e10cSrcweir         ::com::sun::star::lang::XComponent,
91*cdf0e10cSrcweir         ::com::sun::star::lang::XServiceInfo
92*cdf0e10cSrcweir     >,
93*cdf0e10cSrcweir     public LinguDispatcher
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >    m_xMSF;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	//the queue is keeping track of all senteces to be checked
99*cdf0e10cSrcweir 	//every element of this queue is a FlatParagraphEntry struct-object
100*cdf0e10cSrcweir     typedef std::deque< FPEntry > FPQueue_t;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     // queue for entries to be processed
103*cdf0e10cSrcweir     FPQueue_t       m_aFPEntriesQueue;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     // the flag to end the endless loop
106*cdf0e10cSrcweir     sal_Bool        m_bEnd;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     // Note that it must be the pointer and not the uno-reference to check if it is the same implementation object
109*cdf0e10cSrcweir     typedef std::map< XComponent *, ::rtl::OUString > DocMap_t;
110*cdf0e10cSrcweir     DocMap_t        m_aDocIdMap;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     // parameter ::rtl::OUString --> implementation name
113*cdf0e10cSrcweir     // parameter ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > --> list of locales supported by service
114*cdf0e10cSrcweir //    typedef std::map< ::rtl::OUString, ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > > GCLocales_t;
115*cdf0e10cSrcweir //    GCLocales_t     m_aGCLocalesByService;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     // language -> implname mapping
118*cdf0e10cSrcweir     typedef std::map< LanguageType, ::rtl::OUString > GCImplNames_t;
119*cdf0e10cSrcweir     GCImplNames_t   m_aGCImplNamesByLang;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     // implname -> UNO reference mapping
122*cdf0e10cSrcweir     typedef std::map< ::rtl::OUString, ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > > GCReferences_t;
123*cdf0e10cSrcweir     GCReferences_t  m_aGCReferencesByService;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     ::rtl::OUString m_aCurCheckedDocId;
126*cdf0e10cSrcweir     sal_Bool        m_bGCServicesChecked;
127*cdf0e10cSrcweir     sal_Int32       m_nDocIdCounter;
128*cdf0e10cSrcweir     sal_Int32       m_nLastEndOfSentencePos;
129*cdf0e10cSrcweir     osl::Condition  m_aWakeUpThread;
130*cdf0e10cSrcweir     osl::Condition  m_aRequestEndThread;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     //! beware of initilization order !
133*cdf0e10cSrcweir     struct MyMutex : public rtl::Static< osl::Mutex, MyMutex > {};
134*cdf0e10cSrcweir 	//
135*cdf0e10cSrcweir     cppu::OInterfaceContainerHelper     m_aEventListeners;
136*cdf0e10cSrcweir     cppu::OInterfaceContainerHelper     m_aNotifyListeners;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > m_xBreakIterator;
139*cdf0e10cSrcweir     mutable ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch >  m_xUpdateAccess;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     sal_Int32 NextDocId();
142*cdf0e10cSrcweir     ::rtl::OUString GetOrCreateDocId( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &xComp );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     void AddEntry(
145*cdf0e10cSrcweir             ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraphIterator > xFlatParaIterator,
146*cdf0e10cSrcweir             ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraph > xFlatPara,
147*cdf0e10cSrcweir             const ::rtl::OUString &rDocId, sal_Int32 nStartIndex, sal_Bool bAutomatic );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     void ProcessResult( const ::com::sun::star::linguistic2::ProofreadingResult &rRes,
150*cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIterator > &rxFlatParagraphIterator,
151*cdf0e10cSrcweir             bool bIsAutomaticChecking );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     sal_Int32 GetSuggestedEndOfSentence( const ::rtl::OUString &rText, sal_Int32 nSentenceStartPos, const ::com::sun::star::lang::Locale &rLocale );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     void GetConfiguredGCSvcs_Impl();
156*cdf0e10cSrcweir //    void GetMatchingGCSvcs_Impl();
157*cdf0e10cSrcweir //    void GetAvailableGCSvcs_Impl();
158*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > GetGrammarChecker( const ::com::sun::star::lang::Locale & rLocale );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch >   GetUpdateAccess() const;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     // disallow use of copy c-tor and assignment operator
163*cdf0e10cSrcweir     GrammarCheckingIterator( const GrammarCheckingIterator & );
164*cdf0e10cSrcweir     GrammarCheckingIterator & operator = ( const GrammarCheckingIterator & );
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir public:
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     void DequeueAndCheck();
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     explicit GrammarCheckingIterator( const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > & rxMgr );
171*cdf0e10cSrcweir     virtual ~GrammarCheckingIterator();
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     // XProofreadingIterator
174*cdf0e10cSrcweir     virtual void SAL_CALL startProofreading( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xDocument, const ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIteratorProvider >& xIteratorProvider ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
175*cdf0e10cSrcweir     virtual ::com::sun::star::linguistic2::ProofreadingResult SAL_CALL checkSentenceAtPosition( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xDocument, const ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraph >& xFlatParagraph, const ::rtl::OUString& aText, const ::com::sun::star::lang::Locale& aLocale, ::sal_Int32 nStartOfSentencePosition, ::sal_Int32 nSuggestedBehindEndOfSentencePosition, ::sal_Int32 nErrorPositionInParagraph ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
176*cdf0e10cSrcweir     virtual void SAL_CALL resetIgnoreRules(  ) throw (::com::sun::star::uno::RuntimeException);
177*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL isProofreading( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xDocument ) throw (::com::sun::star::uno::RuntimeException);
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     // XLinguServiceEventListener
180*cdf0e10cSrcweir     virtual void SAL_CALL processLinguServiceEvent( const ::com::sun::star::linguistic2::LinguServiceEvent& aLngSvcEvent ) throw (::com::sun::star::uno::RuntimeException);
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     // XLinguServiceEventBroadcaster
183*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL addLinguServiceEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLinguServiceEventListener >& xLstnr ) throw (::com::sun::star::uno::RuntimeException);
184*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL removeLinguServiceEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLinguServiceEventListener >& xLstnr ) throw (::com::sun::star::uno::RuntimeException);
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     // XComponent
187*cdf0e10cSrcweir     virtual void SAL_CALL dispose(  ) throw (::com::sun::star::uno::RuntimeException);
188*cdf0e10cSrcweir     virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException);
189*cdf0e10cSrcweir     virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     // XEventListener
192*cdf0e10cSrcweir     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     // XServiceInfo
195*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
196*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
197*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     // LinguDispatcher
200*cdf0e10cSrcweir     virtual void SetServiceList( const ::com::sun::star::lang::Locale &rLocale, const ::com::sun::star::uno::Sequence< rtl::OUString > &rSvcImplNames );
201*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< rtl::OUString > GetServiceList( const ::com::sun::star::lang::Locale &rLocale ) const;
202*cdf0e10cSrcweir     virtual DspType GetDspType() const;
203*cdf0e10cSrcweir };
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir #endif
209*cdf0e10cSrcweir 
210