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