1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
31 #include <cppuhelper/implbase.hxx>
32 #include <osl/mutex.hxx>
33 #include <osl/conditn.hxx>
34 #include <osl/thread.h>
36 #include <com/sun/star/uno/Any.hxx>
37 #include <comphelper/interfacecontainer3.hxx>
38 #include <i18nlangtag/lang.h>
50 // flat paragraph iterator
51 css::uno::Reference
< css::text::XFlatParagraphIterator
> m_xParaIterator
;
54 css::uno::Reference
< css::text::XFlatParagraph
> m_xPara
;
56 // document ID to identify different documents
59 // the starting position to be checked
60 sal_Int32 m_nStartIndex
;
62 // the flag to identify whether the document does automatic grammar checking
68 , m_bAutomatic( false )
74 class GrammarCheckingIterator
:
75 public cppu::WeakImplHelper
77 css::linguistic2::XProofreadingIterator
,
78 css::linguistic2::XLinguServiceEventListener
,
79 css::linguistic2::XLinguServiceEventBroadcaster
,
80 css::lang::XComponent
,
81 css::lang::XServiceInfo
83 public LinguDispatcher
85 //the queue is keeping track of all sentences to be checked
86 //every element of this queue is a FlatParagraphEntry struct-object
87 typedef std::deque
< FPEntry
> FPQueue_t
;
89 // queue for entries to be processed
90 FPQueue_t m_aFPEntriesQueue
;
92 // the flag to end the endless loop
95 // Note that it must be the pointer and not the uno-reference to check if it is the same implementation object
96 typedef std::map
< XComponent
*, OUString
> DocMap_t
;
100 // BCP-47 language tag -> implname mapping
101 typedef std::map
< OUString
, OUString
> GCImplNames_t
;
102 GCImplNames_t m_aGCImplNamesByLang
;
104 // implname -> UNO reference mapping
105 typedef std::map
< OUString
, css::uno::Reference
< css::linguistic2::XProofreader
> > GCReferences_t
;
106 GCReferences_t m_aGCReferencesByService
;
108 OUString m_aCurCheckedDocId
;
109 bool m_bGCServicesChecked
;
110 sal_Int32 m_nDocIdCounter
;
111 osl::Condition m_aWakeUpThread
;
114 //! beware of initialization order!
115 static osl::Mutex
& MyMutex();
116 comphelper::OInterfaceContainerHelper3
<css::lang::XEventListener
> m_aEventListeners
;
117 comphelper::OInterfaceContainerHelper3
<css::linguistic2::XLinguServiceEventListener
> m_aNotifyListeners
;
119 css::uno::Reference
< css::i18n::XBreakIterator
> m_xBreakIterator
;
120 mutable css::uno::Reference
< css::util::XChangesBatch
> m_xUpdateAccess
;
122 void TerminateThread();
124 sal_Int32
NextDocId();
125 OUString
GetOrCreateDocId( const css::uno::Reference
< css::lang::XComponent
> &xComp
);
128 const css::uno::Reference
< css::text::XFlatParagraphIterator
>& xFlatParaIterator
,
129 const css::uno::Reference
< css::text::XFlatParagraph
>& xFlatPara
,
130 const OUString
&rDocId
, sal_Int32 nStartIndex
, bool bAutomatic
);
132 void ProcessResult( const css::linguistic2::ProofreadingResult
&rRes
,
133 const css::uno::Reference
< css::text::XFlatParagraphIterator
> &rxFlatParagraphIterator
,
134 bool bIsAutomaticChecking
);
136 sal_Int32
GetSuggestedEndOfSentence( const OUString
&rText
, sal_Int32 nSentenceStartPos
, const css::lang::Locale
&rLocale
);
138 void GetConfiguredGCSvcs_Impl();
139 css::uno::Reference
< css::linguistic2::XProofreader
> GetGrammarChecker( css::lang::Locale
& rLocale
);
141 css::uno::Reference
< css::util::XChangesBatch
> const & GetUpdateAccess() const;
143 GrammarCheckingIterator( const GrammarCheckingIterator
& ) = delete;
144 GrammarCheckingIterator
& operator = ( const GrammarCheckingIterator
& ) = delete;
146 // Gets the grammar checker service, using fallback locales if necessary,
147 // and the BCP-47 tag for the updated locale, if the fallback was used.
148 // Precondition: MyMutex() is locked.
149 std::pair
<OUString
, std::optional
<OUString
>>
150 getServiceForLocale(const css::lang::Locale
& rLocale
) const;
154 void DequeueAndCheck();
156 explicit GrammarCheckingIterator();
157 virtual ~GrammarCheckingIterator() override
;
159 // XProofreadingIterator
160 virtual void SAL_CALL
startProofreading( const css::uno::Reference
< css::uno::XInterface
>& xDocument
, const css::uno::Reference
< css::text::XFlatParagraphIteratorProvider
>& xIteratorProvider
) override
;
161 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
;
162 virtual void SAL_CALL
resetIgnoreRules( ) override
;
163 virtual sal_Bool SAL_CALL
isProofreading( const css::uno::Reference
< css::uno::XInterface
>& xDocument
) override
;
165 // XLinguServiceEventListener
166 virtual void SAL_CALL
processLinguServiceEvent( const css::linguistic2::LinguServiceEvent
& aLngSvcEvent
) override
;
168 // XLinguServiceEventBroadcaster
169 virtual sal_Bool SAL_CALL
addLinguServiceEventListener( const css::uno::Reference
< css::linguistic2::XLinguServiceEventListener
>& xLstnr
) override
;
170 virtual sal_Bool SAL_CALL
removeLinguServiceEventListener( const css::uno::Reference
< css::linguistic2::XLinguServiceEventListener
>& xLstnr
) override
;
173 virtual void SAL_CALL
dispose( ) override
;
174 virtual void SAL_CALL
addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& xListener
) override
;
175 virtual void SAL_CALL
removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& aListener
) override
;
178 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
181 virtual OUString SAL_CALL
getImplementationName( ) override
;
182 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
183 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
;
186 virtual void SetServiceList( const css::lang::Locale
&rLocale
, const css::uno::Sequence
< OUString
> &rSvcImplNames
) override
;
187 virtual css::uno::Sequence
< OUString
> GetServiceList( const css::lang::Locale
&rLocale
) const override
;
191 /** Implementation of the css::container::XStringKeyMap interface
193 class LngXStringKeyMap
: public ::cppu::WeakImplHelper
<css::container::XStringKeyMap
>
198 virtual css::uno::Any SAL_CALL
getValue(const OUString
& aKey
) override
;
199 virtual sal_Bool SAL_CALL
hasValue(const OUString
& aKey
) override
;
200 virtual void SAL_CALL
insertValue(const OUString
& aKey
, const css::uno::Any
& aValue
) override
;
201 virtual ::sal_Int32 SAL_CALL
getCount() override
;
202 virtual OUString SAL_CALL
getKeyByIndex(::sal_Int32 nIndex
) override
;
203 virtual css::uno::Any SAL_CALL
getValueByIndex(::sal_Int32 nIndex
) override
;
206 LngXStringKeyMap(LngXStringKeyMap
const &) = delete;
207 void operator=(LngXStringKeyMap
const &) = delete;
209 ~LngXStringKeyMap() override
{};
211 std::map
<OUString
, css::uno::Any
> maMap
;
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */