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 INCLUDED_LINGUISTIC_SOURCE_GCITERATOR_HXX
21 : #define INCLUDED_LINGUISTIC_SOURCE_GCITERATOR_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 : 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 : bool m_bAutomatic;
63 :
64 0 : FPEntry()
65 : : m_aDocId()
66 : , m_nStartIndex( 0 )
67 0 : , m_bAutomatic( false )
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 : //the queue is keeping track of all senteces to be checked
87 : //every element of this queue is a FlatParagraphEntry struct-object
88 : typedef std::deque< FPEntry > FPQueue_t;
89 :
90 : // queue for entries to be processed
91 : FPQueue_t m_aFPEntriesQueue;
92 :
93 : // the flag to end the endless loop
94 : bool m_bEnd;
95 :
96 : // Note that it must be the pointer and not the uno-reference to check if it is the same implementation object
97 : typedef std::map< XComponent *, OUString > DocMap_t;
98 : DocMap_t m_aDocIdMap;
99 :
100 :
101 : // language -> implname mapping
102 : typedef std::map< LanguageType, OUString > GCImplNames_t;
103 : GCImplNames_t m_aGCImplNamesByLang;
104 :
105 : // implname -> UNO reference mapping
106 : typedef std::map< OUString, ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > > GCReferences_t;
107 : GCReferences_t m_aGCReferencesByService;
108 :
109 : OUString m_aCurCheckedDocId;
110 : bool m_bGCServicesChecked;
111 : sal_Int32 m_nDocIdCounter;
112 : sal_Int32 m_nLastEndOfSentencePos;
113 : osl::Condition m_aWakeUpThread;
114 : oslThread m_thread;
115 :
116 : //! beware of initilization order !
117 : struct MyMutex : public rtl::Static< osl::Mutex, MyMutex > {};
118 : cppu::OInterfaceContainerHelper m_aEventListeners;
119 : cppu::OInterfaceContainerHelper m_aNotifyListeners;
120 :
121 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > m_xBreakIterator;
122 : mutable ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch > m_xUpdateAccess;
123 :
124 : void TerminateThread();
125 :
126 : sal_Int32 NextDocId();
127 : OUString GetOrCreateDocId( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &xComp );
128 :
129 : void AddEntry(
130 : ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraphIterator > xFlatParaIterator,
131 : ::com::sun::star::uno::WeakReference< ::com::sun::star::text::XFlatParagraph > xFlatPara,
132 : const OUString &rDocId, sal_Int32 nStartIndex, bool bAutomatic );
133 :
134 : void ProcessResult( const ::com::sun::star::linguistic2::ProofreadingResult &rRes,
135 : const ::com::sun::star::uno::Reference< ::com::sun::star::text::XFlatParagraphIterator > &rxFlatParagraphIterator,
136 : bool bIsAutomaticChecking );
137 :
138 : sal_Int32 GetSuggestedEndOfSentence( const OUString &rText, sal_Int32 nSentenceStartPos, const ::com::sun::star::lang::Locale &rLocale );
139 :
140 : void GetConfiguredGCSvcs_Impl();
141 : ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XProofreader > GetGrammarChecker( const ::com::sun::star::lang::Locale & rLocale );
142 :
143 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XChangesBatch > GetUpdateAccess() const;
144 :
145 : // disallow use of copy c-tor and assignment operator
146 : GrammarCheckingIterator( const GrammarCheckingIterator & );
147 : GrammarCheckingIterator & operator = ( const GrammarCheckingIterator & );
148 :
149 : public:
150 :
151 : void DequeueAndCheck();
152 :
153 : explicit GrammarCheckingIterator();
154 : virtual ~GrammarCheckingIterator();
155 :
156 : // XProofreadingIterator
157 : 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, std::exception) SAL_OVERRIDE;
158 : 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 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, std::exception) SAL_OVERRIDE;
159 : virtual void SAL_CALL resetIgnoreRules( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
160 : virtual sal_Bool SAL_CALL isProofreading( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xDocument ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
161 :
162 : // XLinguServiceEventListener
163 : virtual void SAL_CALL processLinguServiceEvent( const ::com::sun::star::linguistic2::LinguServiceEvent& aLngSvcEvent ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
164 :
165 : // XLinguServiceEventBroadcaster
166 : virtual sal_Bool SAL_CALL addLinguServiceEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLinguServiceEventListener >& xLstnr ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
167 : virtual sal_Bool SAL_CALL removeLinguServiceEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLinguServiceEventListener >& xLstnr ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
168 :
169 : // XComponent
170 : virtual void SAL_CALL dispose( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
171 : virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
172 : virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
173 :
174 : // XEventListener
175 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
176 :
177 : // XServiceInfo
178 : virtual OUString SAL_CALL getImplementationName( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
179 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
180 : virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
181 :
182 : // LinguDispatcher
183 : virtual void SetServiceList( const ::com::sun::star::lang::Locale &rLocale, const ::com::sun::star::uno::Sequence< OUString > &rSvcImplNames ) SAL_OVERRIDE;
184 : virtual ::com::sun::star::uno::Sequence< OUString > GetServiceList( const ::com::sun::star::lang::Locale &rLocale ) const SAL_OVERRIDE;
185 : virtual DspType GetDspType() const SAL_OVERRIDE;
186 : };
187 :
188 :
189 :
190 : #endif
191 :
192 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|