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_SPELLDSP_HXX
21 #define INCLUDED_LINGUISTIC_SOURCE_SPELLDSP_HXX
22 
23 #include "defs.hxx"
24 #include <linguistic/misc.hxx>
25 #include <iprcache.hxx>
26 
27 #include <cppuhelper/implbase.hxx>
28 #include <com/sun/star/beans/PropertyValues.hpp>
29 #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
30 #include <com/sun/star/linguistic2/XSpellChecker.hpp>
31 #include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
32 
33 #include <map>
34 #include <memory>
35 #include <unotools/charclass.hxx>
36 
37 class LngSvcMgr;
38 
39 
40 class SpellCheckerDispatcher :
41     public cppu::WeakImplHelper
42     <
43         css::linguistic2::XSpellChecker1,
44         css::linguistic2::XSpellChecker
45     >,
46     public LinguDispatcher
47 {
48     typedef std::shared_ptr< LangSvcEntries_Spell >               LangSvcEntries_Spell_Ptr_t;
49     typedef std::map< LanguageType, LangSvcEntries_Spell_Ptr_t >    SpellSvcByLangMap_t;
50     SpellSvcByLangMap_t     m_aSvcMap;
51 
52     css::uno::Reference< css::linguistic2::XLinguProperties >           m_xPropSet;
53     css::uno::Reference< css::linguistic2::XSearchableDictionaryList >  m_xDicList;
54 
55     LngSvcMgr                       &m_rMgr;
56     mutable std::unique_ptr<linguistic::SpellCache> m_pCache; // Spell Cache (holds known words)
57     std::unique_ptr<CharClass>       m_pCharClass;
58 
59     SpellCheckerDispatcher(const SpellCheckerDispatcher &) = delete;
60     SpellCheckerDispatcher & operator = (const SpellCheckerDispatcher &) = delete;
61 
62     inline linguistic::SpellCache &  GetCache() const;
63 
64     inline const css::uno::Reference< css::linguistic2::XLinguProperties > &
65             GetPropSet();
66     inline const css::uno::Reference< css::linguistic2::XSearchableDictionaryList > &
67             GetDicList();
68 
69     /// @throws css::uno::RuntimeException
70     /// @throws css::lang::IllegalArgumentException
71     bool    isValid_Impl(const OUString& aWord, LanguageType nLanguage,
72                     const css::beans::PropertyValues& aProperties);
73 
74     /// @throws css::uno::RuntimeException
75     /// @throws css::lang::IllegalArgumentException
76     css::uno::Reference<
77         css::linguistic2::XSpellAlternatives >
78             spell_Impl(const OUString& aWord, LanguageType nLanguage,
79                     const css::beans::PropertyValues& aProperties);
80 
81 public:
82     explicit SpellCheckerDispatcher( LngSvcMgr &rLngSvcMgr );
83     virtual ~SpellCheckerDispatcher() override;
84 
85     // XSupportedLocales (for XSpellChecker)
86     virtual css::uno::Sequence< css::lang::Locale > SAL_CALL getLocales() override;
87     virtual sal_Bool SAL_CALL hasLocale( const css::lang::Locale& aLocale ) override;
88 
89     // XSpellChecker
90     virtual sal_Bool SAL_CALL isValid( const OUString& aWord, const css::lang::Locale& aLocale, const css::uno::Sequence< ::css::beans::PropertyValue >& aProperties ) override;
91     virtual css::uno::Reference< css::linguistic2::XSpellAlternatives > SAL_CALL spell( const OUString& aWord, const css::lang::Locale& aLocale, const css::uno::Sequence< ::css::beans::PropertyValue >& aProperties ) override;
92 
93     // XSupportedLanguages
94     virtual css::uno::Sequence< ::sal_Int16 > SAL_CALL getLanguages(  ) override;
95     virtual sal_Bool SAL_CALL hasLanguage( ::sal_Int16 nLanguage ) override;
96 
97     // XSpellChecker1
98     virtual sal_Bool SAL_CALL isValid( const OUString& aWord, ::sal_Int16 nLanguage, const css::uno::Sequence< css::beans::PropertyValue >& aProperties ) override;
99     virtual css::uno::Reference< css::linguistic2::XSpellAlternatives > SAL_CALL spell( const OUString& aWord, ::sal_Int16 nLanguage, const css::uno::Sequence< css::beans::PropertyValue >& aProperties ) override;
100 
101     // LinguDispatcher
102     virtual void SetServiceList( const css::lang::Locale &rLocale, const css::uno::Sequence< OUString > &rSvcImplNames ) override;
103     virtual css::uno::Sequence< OUString > GetServiceList( const css::lang::Locale &rLocale ) const override;
104 
105     void    FlushSpellCache();
106 
107 private:
108     void setCharClass(const LanguageTag& rLanguageTag);
109     static OUString makeLowerCase(const OUString&, CharClass const *);
110 
111 };
112 
GetCache() const113 inline linguistic::SpellCache & SpellCheckerDispatcher::GetCache() const
114 {
115     if (!m_pCache)
116         m_pCache.reset(new linguistic::SpellCache());
117     return *m_pCache;
118 }
119 
120 
121 inline const css::uno::Reference< css::linguistic2::XLinguProperties > &
GetPropSet()122         SpellCheckerDispatcher::GetPropSet()
123 {
124     if (!m_xPropSet.is())
125         m_xPropSet = linguistic::GetLinguProperties();
126     return m_xPropSet;
127 }
128 
129 
130 inline const css::uno::Reference< css::linguistic2::XSearchableDictionaryList > &
GetDicList()131         SpellCheckerDispatcher::GetDicList()
132 {
133     if (!m_xDicList.is())
134         m_xDicList = linguistic::GetDictionaryList();
135     return m_xDicList;
136 }
137 
138 
139 #endif
140 
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
142