desktop/source/lib/init.cxx                                       |   63 
+++++++---
 lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx |   15 ++
 2 files changed, 62 insertions(+), 16 deletions(-)

New commits:
commit 21d0489a5efa970e975ce1a70dfda2fd9e2c186d
Author:     Szymon Kłos <szymon.k...@collabora.com>
AuthorDate: Wed Feb 8 11:10:58 2023 +0100
Commit:     Szymon Kłos <szymon.k...@collabora.com>
CommitDate: Fri Jul 7 19:30:47 2023 +0200

    lok: LanguageTool provides list of languages
    
    - it sends supported list to the LOK client
    - disables Spell Checker for locales supported by LanguageTool
    - duden protocol supports only german
    - initialize language tool config before usage to fetch correct
      list of supported languages
    
    Change-Id: Id9de8519303774163721def8661fa408da449348
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146665
    Reviewed-by: Henry Castro <hcas...@collabora.com>
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153960
    Reviewed-by: Andras Timar <andras.ti...@collabora.com>
    Tested-by: Andras Timar <andras.ti...@collabora.com>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154161
    Tested-by: Jenkins
    Reviewed-by: Szymon Kłos <szymon.k...@collabora.com>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index dbd236858ce8..539291218213 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -117,6 +117,7 @@
 #include <com/sun/star/linguistic2/LanguageGuessing.hpp>
 #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
 #include <com/sun/star/linguistic2/XSpellChecker.hpp>
+#include <com/sun/star/linguistic2/XProofreader.hpp>
 #include <com/sun/star/i18n/LocaleCalendar2.hpp>
 #include <com/sun/star/i18n/ScriptType.hpp>
 #include <com/sun/star/lang/DisposedException.hpp>
@@ -227,6 +228,8 @@ using namespace vcl;
 using namespace desktop;
 using namespace utl;
 
+using LanguageToolCfg = 
officecfg::Office::Linguistic::GrammarChecking::LanguageTool;
+
 static LibLibreOffice_Impl *gImpl = nullptr;
 static bool lok_preinit_2_called = false;
 static std::weak_ptr< LibreOfficeKitClass > gOfficeClass;
@@ -5760,12 +5763,28 @@ static void doc_resetSelection(LibreOfficeKitDocument* 
pThis)
     pDoc->resetSelection();
 }
 
+static void addLocale(boost::property_tree::ptree& rValues, css::lang::Locale 
const & rLocale)
+{
+    boost::property_tree::ptree aChild;
+    OUString sLanguage;
+    const LanguageTag aLanguageTag( rLocale );
+    sLanguage = 
SvtLanguageTable::GetLanguageString(aLanguageTag.getLanguageType());
+    if (sLanguage.endsWith("}"))
+        return;
+
+    sLanguage += ";" + aLanguageTag.getBcp47(false);
+    aChild.put("", sLanguage.toUtf8());
+    rValues.push_back(std::make_pair("", aChild));
+}
+
 static char* getLanguages(LibreOfficeKitDocument* pThis, const char* pCommand)
 {
     css::uno::Sequence< css::lang::Locale > aLocales;
+    css::uno::Sequence< css::lang::Locale > aGrammarLocales;
 
     if (xContext.is())
     {
+        // SpellChecker
         css::uno::Reference<css::linguistic2::XLinguServiceManager2> xLangSrv 
= css::linguistic2::LinguServiceManager::create(xContext);
         if (xLangSrv.is())
         {
@@ -5774,6 +5793,18 @@ static char* getLanguages(LibreOfficeKitDocument* pThis, 
const char* pCommand)
                 aLocales = xSpell->getLocales();
         }
 
+        // LanguageTool
+        if (LanguageToolCfg::IsEnabled::get())
+        {
+            uno::Reference< linguistic2::XProofreader > xGC(
+                    
xContext->getServiceManager()->createInstanceWithContext("org.openoffice.lingu.LanguageToolGrammarChecker",
 xContext),
+                    uno::UNO_QUERY_THROW );
+            uno::Reference< linguistic2::XSupportedLocales > xSuppLoc( xGC, 
uno::UNO_QUERY_THROW );
+            aGrammarLocales = xSuppLoc->getLocales();
+        }
+
+        // Fallback
+
         /* FIXME: To obtain the document languages the spell checker can be 
disabled,
            so a future re-work of the getLanguages function is needed in favor 
to use
            getDocLanguages */
@@ -5788,19 +5819,10 @@ static char* getLanguages(LibreOfficeKitDocument* 
pThis, const char* pCommand)
     boost::property_tree::ptree aTree;
     aTree.put("commandName", pCommand);
     boost::property_tree::ptree aValues;
-    boost::property_tree::ptree aChild;
-    OUString sLanguage;
-    for ( css::lang::Locale const & locale : std::as_const(aLocales) )
-    {
-        const LanguageTag aLanguageTag( locale );
-        sLanguage = 
SvtLanguageTable::GetLanguageString(aLanguageTag.getLanguageType());
-        if (sLanguage.startsWith("{") && sLanguage.endsWith("}"))
-            continue;
-
-        sLanguage += ";" + aLanguageTag.getBcp47(false);
-        aChild.put("", sLanguage.toUtf8());
-        aValues.push_back(std::make_pair("", aChild));
-    }
+    for ( css::lang::Locale const & rLocale : std::as_const(aLocales) )
+        addLocale(aValues, rLocale);
+    for ( css::lang::Locale const & rLocale : std::as_const(aGrammarLocales) )
+        addLocale(aValues, rLocale);
     aTree.add_child("commandValues", aValues);
     std::stringstream aStream;
     boost::property_tree::write_json(aStream, aTree);
@@ -7276,6 +7298,8 @@ static void preLoadShortCutAccelerators()
     batch->commit();
 }
 
+void setLanguageToolConfig();
+
 /// Used only by LibreOfficeKit when used by Online to pre-initialize
 static void preloadData()
 {
@@ -7294,6 +7318,9 @@ static void preloadData()
     if(bAbort)
         std::cerr << "CheckExtensionDependencies failed" << std::endl;
 
+    // setup LanguageTool config before spell checking init
+    setLanguageToolConfig();
+
     // preload all available dictionaries
     css::uno::Reference<css::linguistic2::XLinguServiceManager> xLngSvcMgr =
         
css::linguistic2::LinguServiceManager::create(comphelper::getProcessComponentContext());
@@ -7513,11 +7540,19 @@ void setLanguageToolConfig()
                 if (xSpell.is())
                 {
                     Sequence<OUString> aEmpty;
+                    static constexpr OUStringLiteral cSpell(SN_SPELLCHECKER);
                     Sequence<css::lang::Locale> aLocales = 
xSpell->getLocales();
 
+                    uno::Reference<linguistic2::XProofreader> xGC(
+                        
xContext->getServiceManager()->createInstanceWithContext("org.openoffice.lingu.LanguageToolGrammarChecker",
 xContext),
+                        uno::UNO_QUERY_THROW);
+                    uno::Reference<linguistic2::XSupportedLocales> 
xSuppLoc(xGC, uno::UNO_QUERY_THROW);
+
                     for (int itLocale = 0; itLocale < aLocales.getLength(); 
itLocale++)
                     {
-                        xLangSrv->setConfiguredServices(SN_SPELLCHECKER, 
aLocales[itLocale], aEmpty);
+                        // turn off spell checker if LanguageTool supports the 
locale already
+                        if (xSuppLoc->hasLocale(aLocales[itLocale]))
+                            xLangSrv->setConfiguredServices(cSpell, 
aLocales[itLocale], aEmpty);
                     }
                 }
             }
diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx 
b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
index 246723172469..fb115f582f30 100644
--- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
+++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
@@ -56,6 +56,8 @@ using namespace com::sun::star::beans;
 using namespace com::sun::star::lang;
 using namespace com::sun::star::linguistic2;
 
+constexpr OUStringLiteral sDuden = u"duden";
+
 namespace
 {
 constexpr size_t MAX_SUGGESTIONS_SIZE = 10;
@@ -335,8 +337,17 @@ uno::Sequence<Locale> SAL_CALL 
LanguageToolGrammarChecker::getLocales()
 
     SvtLinguConfig aLinguCfg;
     uno::Sequence<OUString> aLocaleList;
-    aLinguCfg.GetLocaleListFor("GrammarCheckers", 
"org.openoffice.lingu.LanguageToolGrammarChecker",
-                               aLocaleList);
+
+    if (LanguageToolCfg::RestProtocol::get().value_or("") == sDuden)
+    {
+        aLocaleList.realloc(3);
+        aLocaleList.getArray()[0] = "de-DE";
+        aLocaleList.getArray()[1] = "en-US";
+        aLocaleList.getArray()[2] = "en-GB";
+    }
+    else
+        aLinguCfg.GetLocaleListFor("GrammarCheckers",
+                                   
"org.openoffice.lingu.LanguageToolGrammarChecker", aLocaleList);
 
     auto nLength = aLocaleList.getLength();
     m_aSuppLocales.realloc(nLength);

Reply via email to