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