1*63ce064aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63ce064aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63ce064aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63ce064aSAndrew Rist  * distributed with this work for additional information
6*63ce064aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63ce064aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63ce064aSAndrew Rist  * "License"); you may not use this file except in compliance
9*63ce064aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63ce064aSAndrew Rist  *
11*63ce064aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63ce064aSAndrew Rist  *
13*63ce064aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63ce064aSAndrew Rist  * software distributed under the License is distributed on an
15*63ce064aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63ce064aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63ce064aSAndrew Rist  * specific language governing permissions and limitations
18*63ce064aSAndrew Rist  * under the License.
19*63ce064aSAndrew Rist  *
20*63ce064aSAndrew Rist  *************************************************************/
21*63ce064aSAndrew Rist 
22*63ce064aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _LINGUISTIC_GRAMMARCHECKINGITERATOR_HXX_
25cdf0e10cSrcweir #define _LINGUISTIC_GRAMMARCHECKINGITERATOR_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/i18n/XBreakIterator.hpp>
28cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XEventListener.hpp>
31cdf0e10cSrcweir #include <com/sun/star/linguistic2/XProofreadingIterator.hpp>
32cdf0e10cSrcweir #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
33cdf0e10cSrcweir #include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp>
34cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
35cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <cppuhelper/implbase5.hxx>
38cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
39cdf0e10cSrcweir #include <osl/mutex.hxx>
40cdf0e10cSrcweir #include <osl/conditn.hxx>
41cdf0e10cSrcweir #include <rtl/instance.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <map>
44cdf0e10cSrcweir #include <deque>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "defs.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir struct FPEntry
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     // flat paragraph iterator
54cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIterator > m_xParaIterator;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     // flat paragraph
57cdf0e10cSrcweir     ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraph > m_xPara;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     // document ID to identify different documents
60cdf0e10cSrcweir     ::rtl::OUString	m_aDocId;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     // the starting position to be checked
63cdf0e10cSrcweir     sal_Int32     	m_nStartIndex;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     // the flag to identify whether the document does automatical grammar checking
66cdf0e10cSrcweir     sal_Bool      	m_bAutomatic;
67cdf0e10cSrcweir 
FPEntryFPEntry68cdf0e10cSrcweir     FPEntry()
69cdf0e10cSrcweir         : m_aDocId()
70cdf0e10cSrcweir         , m_nStartIndex( 0 )
71cdf0e10cSrcweir         , m_bAutomatic( 0 )
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir };
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 
80cdf0e10cSrcweir class GrammarCheckingIterator:
81cdf0e10cSrcweir     public cppu::WeakImplHelper5
82cdf0e10cSrcweir     <
83cdf0e10cSrcweir         ::com::sun::star::linguistic2::XProofreadingIterator,
84cdf0e10cSrcweir         ::com::sun::star::linguistic2::XLinguServiceEventListener,
85cdf0e10cSrcweir         ::com::sun::star::linguistic2::XLinguServiceEventBroadcaster,
86cdf0e10cSrcweir         ::com::sun::star::lang::XComponent,
87cdf0e10cSrcweir         ::com::sun::star::lang::XServiceInfo
88cdf0e10cSrcweir     >,
89cdf0e10cSrcweir     public LinguDispatcher
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >    m_xMSF;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	//the queue is keeping track of all senteces to be checked
95cdf0e10cSrcweir 	//every element of this queue is a FlatParagraphEntry struct-object
96cdf0e10cSrcweir     typedef std::deque< FPEntry > FPQueue_t;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     // queue for entries to be processed
99cdf0e10cSrcweir     FPQueue_t       m_aFPEntriesQueue;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     // the flag to end the endless loop
102cdf0e10cSrcweir     sal_Bool        m_bEnd;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     // Note that it must be the pointer and not the uno-reference to check if it is the same implementation object
105cdf0e10cSrcweir     typedef std::map< XComponent *, ::rtl::OUString > DocMap_t;
106cdf0e10cSrcweir     DocMap_t        m_aDocIdMap;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     // parameter ::rtl::OUString --> implementation name
109cdf0e10cSrcweir     // parameter ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > --> list of locales supported by service
110cdf0e10cSrcweir //    typedef std::map< ::rtl::OUString, ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > > GCLocales_t;
111cdf0e10cSrcweir //    GCLocales_t     m_aGCLocalesByService;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     // language -> implname mapping
114cdf0e10cSrcweir     typedef std::map< LanguageType, ::rtl::OUString > GCImplNames_t;
115cdf0e10cSrcweir     GCImplNames_t   m_aGCImplNamesByLang;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     // implname -> UNO reference mapping
118cdf0e10cSrcweir     typedef std::map< ::rtl::OUString, ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > > GCReferences_t;
119cdf0e10cSrcweir     GCReferences_t  m_aGCReferencesByService;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     ::rtl::OUString m_aCurCheckedDocId;
122cdf0e10cSrcweir     sal_Bool        m_bGCServicesChecked;
123cdf0e10cSrcweir     sal_Int32       m_nDocIdCounter;
124cdf0e10cSrcweir     sal_Int32       m_nLastEndOfSentencePos;
125cdf0e10cSrcweir     osl::Condition  m_aWakeUpThread;
126cdf0e10cSrcweir     osl::Condition  m_aRequestEndThread;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     //! beware of initilization order !
129cdf0e10cSrcweir     struct MyMutex : public rtl::Static< osl::Mutex, MyMutex > {};
130cdf0e10cSrcweir 	//
131cdf0e10cSrcweir     cppu::OInterfaceContainerHelper     m_aEventListeners;
132cdf0e10cSrcweir     cppu::OInterfaceContainerHelper     m_aNotifyListeners;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > m_xBreakIterator;
135cdf0e10cSrcweir     mutable ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch >  m_xUpdateAccess;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     sal_Int32 NextDocId();
138cdf0e10cSrcweir     ::rtl::OUString GetOrCreateDocId( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &xComp );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     void AddEntry(
141cdf0e10cSrcweir             ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraphIterator > xFlatParaIterator,
142cdf0e10cSrcweir             ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraph > xFlatPara,
143cdf0e10cSrcweir             const ::rtl::OUString &rDocId, sal_Int32 nStartIndex, sal_Bool bAutomatic );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     void ProcessResult( const ::com::sun::star::linguistic2::ProofreadingResult &rRes,
146cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIterator > &rxFlatParagraphIterator,
147cdf0e10cSrcweir             bool bIsAutomaticChecking );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     sal_Int32 GetSuggestedEndOfSentence( const ::rtl::OUString &rText, sal_Int32 nSentenceStartPos, const ::com::sun::star::lang::Locale &rLocale );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     void GetConfiguredGCSvcs_Impl();
152cdf0e10cSrcweir //    void GetMatchingGCSvcs_Impl();
153cdf0e10cSrcweir //    void GetAvailableGCSvcs_Impl();
154cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > GetGrammarChecker( const ::com::sun::star::lang::Locale & rLocale );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch >   GetUpdateAccess() const;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     // disallow use of copy c-tor and assignment operator
159cdf0e10cSrcweir     GrammarCheckingIterator( const GrammarCheckingIterator & );
160cdf0e10cSrcweir     GrammarCheckingIterator & operator = ( const GrammarCheckingIterator & );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir public:
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     void DequeueAndCheck();
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     explicit GrammarCheckingIterator( const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > & rxMgr );
167cdf0e10cSrcweir     virtual ~GrammarCheckingIterator();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     // XProofreadingIterator
170cdf0e10cSrcweir     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);
171cdf0e10cSrcweir     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);
172cdf0e10cSrcweir     virtual void SAL_CALL resetIgnoreRules(  ) throw (::com::sun::star::uno::RuntimeException);
173cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL isProofreading( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xDocument ) throw (::com::sun::star::uno::RuntimeException);
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     // XLinguServiceEventListener
176cdf0e10cSrcweir     virtual void SAL_CALL processLinguServiceEvent( const ::com::sun::star::linguistic2::LinguServiceEvent& aLngSvcEvent ) throw (::com::sun::star::uno::RuntimeException);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     // XLinguServiceEventBroadcaster
179cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL addLinguServiceEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLinguServiceEventListener >& xLstnr ) throw (::com::sun::star::uno::RuntimeException);
180cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL removeLinguServiceEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLinguServiceEventListener >& xLstnr ) throw (::com::sun::star::uno::RuntimeException);
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     // XComponent
183cdf0e10cSrcweir     virtual void SAL_CALL dispose(  ) throw (::com::sun::star::uno::RuntimeException);
184cdf0e10cSrcweir     virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException);
185cdf0e10cSrcweir     virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     // XEventListener
188cdf0e10cSrcweir     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     // XServiceInfo
191cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
192cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
193cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     // LinguDispatcher
196cdf0e10cSrcweir     virtual void SetServiceList( const ::com::sun::star::lang::Locale &rLocale, const ::com::sun::star::uno::Sequence< rtl::OUString > &rSvcImplNames );
197cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< rtl::OUString > GetServiceList( const ::com::sun::star::lang::Locale &rLocale ) const;
198cdf0e10cSrcweir     virtual DspType GetDspType() const;
199cdf0e10cSrcweir };
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 
202cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
203cdf0e10cSrcweir 
204cdf0e10cSrcweir #endif
205cdf0e10cSrcweir 
206