1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _LINGUISTIC_GRAMMARCHECKINGITERATOR_HXX_
29 #define _LINGUISTIC_GRAMMARCHECKINGITERATOR_HXX_
30 
31 #include <com/sun/star/i18n/XBreakIterator.hpp>
32 #include <com/sun/star/lang/XComponent.hpp>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/lang/XEventListener.hpp>
35 #include <com/sun/star/linguistic2/XProofreadingIterator.hpp>
36 #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
37 #include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp>
38 #include <com/sun/star/uno/XComponentContext.hpp>
39 #include <com/sun/star/util/XChangesBatch.hpp>
40 
41 #include <cppuhelper/implbase5.hxx>
42 #include <cppuhelper/weakref.hxx>
43 #include <osl/mutex.hxx>
44 #include <osl/conditn.hxx>
45 #include <rtl/instance.hxx>
46 
47 #include <map>
48 #include <deque>
49 
50 #include "defs.hxx"
51 
52 //////////////////////////////////////////////////////////////////////
53 
54 
55 struct FPEntry
56 {
57     // flat paragraph iterator
58     ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIterator > m_xParaIterator;
59 
60     // flat paragraph
61     ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraph > m_xPara;
62 
63     // document ID to identify different documents
64     ::rtl::OUString	m_aDocId;
65 
66     // the starting position to be checked
67     sal_Int32     	m_nStartIndex;
68 
69     // the flag to identify whether the document does automatical grammar checking
70     sal_Bool      	m_bAutomatic;
71 
72     FPEntry()
73         : m_aDocId()
74         , m_nStartIndex( 0 )
75         , m_bAutomatic( 0 )
76     {
77     }
78 };
79 
80 
81 ///////////////////////////////////////////////////////////////////////////
82 
83 
84 class GrammarCheckingIterator:
85     public cppu::WeakImplHelper5
86     <
87         ::com::sun::star::linguistic2::XProofreadingIterator,
88         ::com::sun::star::linguistic2::XLinguServiceEventListener,
89         ::com::sun::star::linguistic2::XLinguServiceEventBroadcaster,
90         ::com::sun::star::lang::XComponent,
91         ::com::sun::star::lang::XServiceInfo
92     >,
93     public LinguDispatcher
94 {
95     com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >    m_xMSF;
96 
97 
98 	//the queue is keeping track of all senteces to be checked
99 	//every element of this queue is a FlatParagraphEntry struct-object
100     typedef std::deque< FPEntry > FPQueue_t;
101 
102     // queue for entries to be processed
103     FPQueue_t       m_aFPEntriesQueue;
104 
105     // the flag to end the endless loop
106     sal_Bool        m_bEnd;
107 
108     // Note that it must be the pointer and not the uno-reference to check if it is the same implementation object
109     typedef std::map< XComponent *, ::rtl::OUString > DocMap_t;
110     DocMap_t        m_aDocIdMap;
111 
112     // parameter ::rtl::OUString --> implementation name
113     // parameter ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > --> list of locales supported by service
114 //    typedef std::map< ::rtl::OUString, ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > > GCLocales_t;
115 //    GCLocales_t     m_aGCLocalesByService;
116 
117     // language -> implname mapping
118     typedef std::map< LanguageType, ::rtl::OUString > GCImplNames_t;
119     GCImplNames_t   m_aGCImplNamesByLang;
120 
121     // implname -> UNO reference mapping
122     typedef std::map< ::rtl::OUString, ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > > GCReferences_t;
123     GCReferences_t  m_aGCReferencesByService;
124 
125     ::rtl::OUString m_aCurCheckedDocId;
126     sal_Bool        m_bGCServicesChecked;
127     sal_Int32       m_nDocIdCounter;
128     sal_Int32       m_nLastEndOfSentencePos;
129     osl::Condition  m_aWakeUpThread;
130     osl::Condition  m_aRequestEndThread;
131 
132     //! beware of initilization order !
133     struct MyMutex : public rtl::Static< osl::Mutex, MyMutex > {};
134 	//
135     cppu::OInterfaceContainerHelper     m_aEventListeners;
136     cppu::OInterfaceContainerHelper     m_aNotifyListeners;
137 
138     ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > m_xBreakIterator;
139     mutable ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch >  m_xUpdateAccess;
140 
141     sal_Int32 NextDocId();
142     ::rtl::OUString GetOrCreateDocId( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &xComp );
143 
144     void AddEntry(
145             ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraphIterator > xFlatParaIterator,
146             ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraph > xFlatPara,
147             const ::rtl::OUString &rDocId, sal_Int32 nStartIndex, sal_Bool bAutomatic );
148 
149     void ProcessResult( const ::com::sun::star::linguistic2::ProofreadingResult &rRes,
150             const ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIterator > &rxFlatParagraphIterator,
151             bool bIsAutomaticChecking );
152 
153     sal_Int32 GetSuggestedEndOfSentence( const ::rtl::OUString &rText, sal_Int32 nSentenceStartPos, const ::com::sun::star::lang::Locale &rLocale );
154 
155     void GetConfiguredGCSvcs_Impl();
156 //    void GetMatchingGCSvcs_Impl();
157 //    void GetAvailableGCSvcs_Impl();
158     ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > GetGrammarChecker( const ::com::sun::star::lang::Locale & rLocale );
159 
160     ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch >   GetUpdateAccess() const;
161 
162     // disallow use of copy c-tor and assignment operator
163     GrammarCheckingIterator( const GrammarCheckingIterator & );
164     GrammarCheckingIterator & operator = ( const GrammarCheckingIterator & );
165 
166 public:
167 
168     void DequeueAndCheck();
169 
170     explicit GrammarCheckingIterator( const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > & rxMgr );
171     virtual ~GrammarCheckingIterator();
172 
173     // XProofreadingIterator
174     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     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     virtual void SAL_CALL resetIgnoreRules(  ) throw (::com::sun::star::uno::RuntimeException);
177     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 
179     // XLinguServiceEventListener
180     virtual void SAL_CALL processLinguServiceEvent( const ::com::sun::star::linguistic2::LinguServiceEvent& aLngSvcEvent ) throw (::com::sun::star::uno::RuntimeException);
181 
182     // XLinguServiceEventBroadcaster
183     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     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 
186     // XComponent
187     virtual void SAL_CALL dispose(  ) throw (::com::sun::star::uno::RuntimeException);
188     virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException);
189     virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
190 
191     // XEventListener
192     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
193 
194     // XServiceInfo
195     virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
196     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
197     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
198 
199     // LinguDispatcher
200     virtual void SetServiceList( const ::com::sun::star::lang::Locale &rLocale, const ::com::sun::star::uno::Sequence< rtl::OUString > &rSvcImplNames );
201     virtual ::com::sun::star::uno::Sequence< rtl::OUString > GetServiceList( const ::com::sun::star::lang::Locale &rLocale ) const;
202     virtual DspType GetDspType() const;
203 };
204 
205 
206 ///////////////////////////////////////////////////////////////////////////
207 
208 #endif
209 
210