/src/libreoffice/linguistic/source/gciterator.hxx
Line | Count | Source |
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 | | #pragma once |
21 | | |
22 | | #include <com/sun/star/i18n/XBreakIterator.hpp> |
23 | | #include <com/sun/star/lang/XComponent.hpp> |
24 | | #include <com/sun/star/lang/XServiceInfo.hpp> |
25 | | #include <com/sun/star/lang/XEventListener.hpp> |
26 | | #include <com/sun/star/linguistic2/XProofreadingIterator.hpp> |
27 | | #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp> |
28 | | #include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp> |
29 | | #include <com/sun/star/util/XChangesBatch.hpp> |
30 | | |
31 | | #include <cppuhelper/implbase.hxx> |
32 | | #include <osl/mutex.hxx> |
33 | | #include <osl/conditn.hxx> |
34 | | #include <osl/thread.h> |
35 | | |
36 | | #include <com/sun/star/uno/Any.hxx> |
37 | | #include <comphelper/lok.hxx> |
38 | | #include <comphelper/interfacecontainer3.hxx> |
39 | | #include <i18nlangtag/lang.h> |
40 | | |
41 | | #include <map> |
42 | | #include <optional> |
43 | | #include <utility> |
44 | | #include <deque> |
45 | | |
46 | | #include "defs.hxx" |
47 | | |
48 | | |
49 | | struct FPEntry |
50 | | { |
51 | | // flat paragraph iterator |
52 | | css::uno::Reference< css::text::XFlatParagraphIterator > m_xParaIterator; |
53 | | |
54 | | // flat paragraph |
55 | | css::uno::Reference< css::text::XFlatParagraph > m_xPara; |
56 | | |
57 | | // document ID to identify different documents |
58 | | OUString m_aDocId; |
59 | | |
60 | | // the starting position to be checked |
61 | | sal_Int32 m_nStartIndex; |
62 | | |
63 | | // the flag to identify whether the document does automatic grammar checking |
64 | | bool m_bAutomatic; |
65 | | |
66 | | FPEntry() |
67 | 0 | : m_aDocId() |
68 | 0 | , m_nStartIndex( 0 ) |
69 | 0 | , m_bAutomatic( false ) |
70 | 0 | { |
71 | 0 | } |
72 | | }; |
73 | | |
74 | | |
75 | | class GrammarCheckingIterator: |
76 | | public cppu::WeakImplHelper |
77 | | < |
78 | | css::linguistic2::XProofreadingIterator, |
79 | | css::linguistic2::XLinguServiceEventListener, |
80 | | css::linguistic2::XLinguServiceEventBroadcaster, |
81 | | css::lang::XComponent, |
82 | | css::lang::XServiceInfo |
83 | | >, |
84 | | public LinguDispatcher, |
85 | | public comphelper::LibreOfficeKit::ThreadJoinable |
86 | | { |
87 | | //the queue is keeping track of all sentences to be checked |
88 | | //every element of this queue is a FlatParagraphEntry struct-object |
89 | | typedef std::deque< FPEntry > FPQueue_t; |
90 | | |
91 | | // queue for entries to be processed |
92 | | FPQueue_t m_aFPEntriesQueue; |
93 | | |
94 | | // the flag to end the endless loop |
95 | | bool m_bEnd; |
96 | | |
97 | | // Note that it must be the pointer and not the uno-reference to check if it is the same implementation object |
98 | | typedef std::map< XComponent *, OUString > DocMap_t; |
99 | | DocMap_t m_aDocIdMap; |
100 | | |
101 | | |
102 | | // BCP-47 language tag -> implname mapping |
103 | | typedef std::map< OUString, OUString > GCImplNames_t; |
104 | | GCImplNames_t m_aGCImplNamesByLang; |
105 | | |
106 | | // implname -> UNO reference mapping |
107 | | typedef std::map< OUString, css::uno::Reference< css::linguistic2::XProofreader > > GCReferences_t; |
108 | | GCReferences_t m_aGCReferencesByService; |
109 | | |
110 | | OUString m_aCurCheckedDocId; |
111 | | bool m_bGCServicesChecked; |
112 | | sal_Int32 m_nDocIdCounter; |
113 | | osl::Condition m_aWakeUpThread; |
114 | | oslThread m_thread; |
115 | | |
116 | | //! beware of initialization order! |
117 | | static osl::Mutex& MyMutex(); |
118 | | comphelper::OInterfaceContainerHelper3<css::lang::XEventListener> m_aEventListeners; |
119 | | comphelper::OInterfaceContainerHelper3<css::linguistic2::XLinguServiceEventListener> m_aNotifyListeners; |
120 | | |
121 | | css::uno::Reference< css::i18n::XBreakIterator > m_xBreakIterator; |
122 | | mutable css::uno::Reference< css::util::XChangesBatch > m_xUpdateAccess; |
123 | | |
124 | | void TerminateThread(); |
125 | | |
126 | | sal_Int32 NextDocId(); |
127 | | OUString GetOrCreateDocId( const css::uno::Reference< css::lang::XComponent > &xComp ); |
128 | | |
129 | | void AddEntry( |
130 | | const css::uno::Reference< css::text::XFlatParagraphIterator >& xFlatParaIterator, |
131 | | const css::uno::Reference< css::text::XFlatParagraph >& xFlatPara, |
132 | | const OUString &rDocId, sal_Int32 nStartIndex, bool bAutomatic ); |
133 | | |
134 | | void ProcessResult( const css::linguistic2::ProofreadingResult &rRes, |
135 | | const css::uno::Reference< css::text::XFlatParagraphIterator > &rxFlatParagraphIterator, |
136 | | bool bIsAutomaticChecking ); |
137 | | |
138 | | sal_Int32 GetSuggestedEndOfSentence( const OUString &rText, sal_Int32 nSentenceStartPos, const css::lang::Locale &rLocale ); |
139 | | |
140 | | void GetConfiguredGCSvcs_Impl(); |
141 | | css::uno::Reference< css::linguistic2::XProofreader > GetGrammarChecker( css::lang::Locale & rLocale ); |
142 | | |
143 | | css::uno::Reference< css::util::XChangesBatch > const & GetUpdateAccess() const; |
144 | | |
145 | | GrammarCheckingIterator( const GrammarCheckingIterator & ) = delete; |
146 | | GrammarCheckingIterator & operator = ( const GrammarCheckingIterator & ) = delete; |
147 | | |
148 | | // Gets the grammar checker service, using fallback locales if necessary, |
149 | | // and the BCP-47 tag for the updated locale, if the fallback was used. |
150 | | // Precondition: MyMutex() is locked. |
151 | | std::pair<OUString, std::optional<OUString>> |
152 | | getServiceForLocale(const css::lang::Locale& rLocale) const; |
153 | | |
154 | | public: |
155 | | |
156 | | void DequeueAndCheck(); |
157 | | |
158 | | explicit GrammarCheckingIterator(); |
159 | | virtual ~GrammarCheckingIterator() override; |
160 | | |
161 | | // XProofreadingIterator |
162 | | virtual void SAL_CALL startProofreading( const css::uno::Reference< css::uno::XInterface >& xDocument, const css::uno::Reference< css::text::XFlatParagraphIteratorProvider >& xIteratorProvider ) override; |
163 | | virtual css::linguistic2::ProofreadingResult SAL_CALL checkSentenceAtPosition( const css::uno::Reference< css::uno::XInterface >& xDocument, const css::uno::Reference< css::text::XFlatParagraph >& xFlatParagraph, const OUString& aText, const css::lang::Locale& aLocale, ::sal_Int32 nStartOfSentencePosition, ::sal_Int32 nSuggestedBehindEndOfSentencePosition, ::sal_Int32 nErrorPositionInParagraph ) override; |
164 | | virtual void SAL_CALL resetIgnoreRules( ) override; |
165 | | virtual sal_Bool SAL_CALL isProofreading( const css::uno::Reference< css::uno::XInterface >& xDocument ) override; |
166 | | |
167 | | // XLinguServiceEventListener |
168 | | virtual void SAL_CALL processLinguServiceEvent( const css::linguistic2::LinguServiceEvent& aLngSvcEvent ) override; |
169 | | |
170 | | // XLinguServiceEventBroadcaster |
171 | | virtual sal_Bool SAL_CALL addLinguServiceEventListener( const css::uno::Reference< css::linguistic2::XLinguServiceEventListener >& xLstnr ) override; |
172 | | virtual sal_Bool SAL_CALL removeLinguServiceEventListener( const css::uno::Reference< css::linguistic2::XLinguServiceEventListener >& xLstnr ) override; |
173 | | |
174 | | // XComponent |
175 | | virtual void SAL_CALL dispose( ) override; |
176 | | virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) override; |
177 | | virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override; |
178 | | |
179 | | // XEventListener |
180 | | virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override; |
181 | | |
182 | | // XServiceInfo |
183 | | virtual OUString SAL_CALL getImplementationName( ) override; |
184 | | virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; |
185 | | virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override; |
186 | | |
187 | | // LinguDispatcher |
188 | | virtual void SetServiceList( const css::lang::Locale &rLocale, const css::uno::Sequence< OUString > &rSvcImplNames ) override; |
189 | | virtual css::uno::Sequence< OUString > GetServiceList( const css::lang::Locale &rLocale ) const override; |
190 | | |
191 | | // comphelper::LibreOfficeKit::ThreadJoinable |
192 | | virtual bool joinThreads() override; |
193 | | }; |
194 | | |
195 | | |
196 | | /** Implementation of the css::container::XStringKeyMap interface |
197 | | */ |
198 | | class LngXStringKeyMap : public ::cppu::WeakImplHelper<css::container::XStringKeyMap> |
199 | | { |
200 | | public: |
201 | | LngXStringKeyMap(); |
202 | | |
203 | | virtual css::uno::Any SAL_CALL getValue(const OUString& aKey) override; |
204 | | virtual sal_Bool SAL_CALL hasValue(const OUString& aKey) override; |
205 | | virtual void SAL_CALL insertValue(const OUString& aKey, const css::uno::Any& aValue) override; |
206 | | virtual ::sal_Int32 SAL_CALL getCount() override; |
207 | | virtual OUString SAL_CALL getKeyByIndex(::sal_Int32 nIndex) override; |
208 | | virtual css::uno::Any SAL_CALL getValueByIndex(::sal_Int32 nIndex) override; |
209 | | |
210 | | private: |
211 | | LngXStringKeyMap(LngXStringKeyMap const &) = delete; |
212 | | void operator=(LngXStringKeyMap const &) = delete; |
213 | | |
214 | 0 | ~LngXStringKeyMap() override{}; |
215 | | |
216 | | std::map<OUString, css::uno::Any> maMap; |
217 | | }; |
218 | | |
219 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |