desktop/source/lib/init.cxx |  127 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 2 deletions(-)

New commits:
commit bb0c63cc73998a0f67ae4c3e59acea3827a6e11c
Author:     Henry Castro <hcas...@collabora.com>
AuthorDate: Wed Jan 11 16:14:57 2023 -0400
Commit:     Andras Timar <andras.ti...@collabora.com>
CommitDate: Fri Jan 13 19:58:59 2023 +0000

    lok: add function getDocLanguages
    
    If the spell checker is disabled to use a remote
    spelling feature, the getLanguages function is obsolete.
    
    In order to not break stable functions, introduce the
    getDocLanguages for the remote spelling feature.
    
    Signed-off-by: Henry Castro <hcas...@collabora.com>
    Change-Id: Ic210f31eddd3208b29d073ff35ba4fa2d98ea772
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145363
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Andras Timar <andras.ti...@collabora.com>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 377667804e74..0567c12edca4 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -75,6 +75,7 @@
 #include <com/sun/star/document/MacroExecMode.hpp>
 #include <com/sun/star/beans/XPropertySet.hpp>
 #include <com/sun/star/container/XNameAccess.hpp>
+#include <com/sun/star/document/XDocumentLanguages.hpp>
 #include <com/sun/star/frame/Desktop.hpp>
 #include <com/sun/star/frame/DispatchResultEvent.hpp>
 #include <com/sun/star/frame/DispatchResultState.hpp>
@@ -100,6 +101,7 @@
 #include <com/sun/star/xml/crypto/XCertificateCreator.hpp>
 #include <com/sun/star/security/XCertificate.hpp>
 
+#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/i18n/LocaleCalendar2.hpp>
@@ -5379,6 +5381,119 @@ static void 
doc_setGraphicSelection(LibreOfficeKitDocument* pThis, int nType, in
     pDoc->setGraphicSelection(nType, nX, nY);
 }
 
+static uno::Any getDocLanguages(LibreOfficeKitDocument* pThis)
+{
+    SfxViewFrame* pViewFrame = SfxViewFrame::Current();
+    if (!pViewFrame)
+        return uno::makeAny(Sequence<lang::Locale>());
+
+    SfxDispatcher* pDispatcher = pViewFrame->GetBindings().GetDispatcher();
+    if (!pDispatcher)
+        return uno::makeAny(Sequence<lang::Locale>());
+
+    css::uno::Any aLangStatus;
+    pDispatcher->QueryState(SID_LANGUAGE_STATUS, aLangStatus);
+
+    Sequence<OUString> aSeqLang;
+    if (!(aLangStatus >>= aSeqLang))
+        return uno::makeAny(Sequence<lang::Locale>());
+
+    // (aSeqLang[0] == "Current Language",  aSeqLang[1] == "Script Type",
+    //  aSeqLang[2] == "Keyboard Language", aSeqLang[3] == "Guess Text Lang")
+    if (aSeqLang.getLength() != 4)
+        return uno::makeAny(Sequence<lang::Locale>());
+
+    LanguageType nLangType;
+    std::set<LanguageType> aLangItems;
+    SvtScriptType eScriptType = 
static_cast<SvtScriptType>(aSeqLang[1].toInt32());
+
+    if (!aSeqLang[0].isEmpty())
+    {
+        nLangType = SvtLanguageTable::GetLanguageType(aSeqLang[0]);
+        if (nLangType != LANGUAGE_DONTKNOW)
+        {
+            aLangItems.insert(nLangType);
+        }
+    }
+
+    const AllSettings& rAllSettings = Application::GetSettings();
+    nLangType = rAllSettings.GetLanguageTag().getLanguageType();
+    if (nLangType != LANGUAGE_DONTKNOW &&
+        (eScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType)))
+    {
+        aLangItems.insert(nLangType);
+    }
+
+    nLangType = rAllSettings.GetUILanguageTag().getLanguageType();
+    if (nLangType != LANGUAGE_DONTKNOW &&
+        (eScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType)))
+    {
+        aLangItems.insert(nLangType);
+    }
+
+    if (!aSeqLang[2].isEmpty())
+    {
+        nLangType = SvtLanguageTable::GetLanguageType(aSeqLang[2]);
+        if (nLangType != LANGUAGE_DONTKNOW &&
+            (eScriptType & 
SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType)))
+        {
+            aLangItems.insert(nLangType);
+        }
+    }
+
+    if (!aSeqLang[3].isEmpty())
+    {
+        Reference<linguistic2::XLanguageGuessing> xLangGuesser;
+        try
+        {
+            xLangGuesser = linguistic2::LanguageGuessing::create(xContext);
+        }
+        catch(...)
+        {
+        }
+
+        if (xLangGuesser.is())
+        {
+            lang::Locale aLocale = 
xLangGuesser->guessPrimaryLanguage(aSeqLang[3], 0,
+                                                                      
aSeqLang[3].getLength());
+            nLangType = LanguageTag(aLocale).makeFallback().getLanguageType();
+            if (nLangType != LANGUAGE_DONTKNOW &&
+                (eScriptType & 
SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType)))
+            {
+                aLangItems.insert(nLangType);
+            }
+        }
+    }
+
+    LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
+    Reference<document::XDocumentLanguages> 
xDocumentLanguages(pDocument->mxComponent, UNO_QUERY);
+    if (xDocumentLanguages.is())
+    {
+        const Sequence<lang::Locale> 
aLocales(xDocumentLanguages->getDocumentLanguages(
+                                                  
static_cast<sal_Int16>(eScriptType), 64));
+
+        for (const lang::Locale& aLocale : aLocales)
+        {
+            nLangType = SvtLanguageTable::GetLanguageType(aLocale.Language);
+            if (nLangType != LANGUAGE_DONTKNOW &&
+                (eScriptType & 
SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType)))
+            {
+                aLangItems.insert(nLangType);
+            }
+        }
+    }
+
+    int nLocale = 0;
+    Sequence<lang::Locale> aLocales(aLangItems.size());
+    auto pLocales = aLocales.getArray();
+    for (const LanguageType& itLang : aLangItems)
+    {
+        pLocales[nLocale++] = LanguageTag::convertToLocale(itLang);
+    }
+
+    return uno::makeAny(aLocales);
+}
+
 static void doc_resetSelection(LibreOfficeKitDocument* pThis)
 {
     comphelper::ProfileZone aZone("doc_resetSelection");
@@ -5396,7 +5511,7 @@ static void doc_resetSelection(LibreOfficeKitDocument* 
pThis)
     pDoc->resetSelection();
 }
 
-static char* getLanguages(const char* pCommand)
+static char* getLanguages(LibreOfficeKitDocument* pThis, const char* pCommand)
 {
     css::uno::Sequence< css::lang::Locale > aLocales;
 
@@ -5409,6 +5524,14 @@ static char* getLanguages(const char* pCommand)
             if (xSpell.is())
                 aLocales = xSpell->getLocales();
         }
+
+        /* 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 */
+        if (!aLocales.hasElements())
+        {
+            getDocLanguages(pThis) >>= aLocales;
+        }
     }
 
     boost::property_tree::ptree aTree;
@@ -5763,7 +5886,7 @@ static char* doc_getCommandValues(LibreOfficeKitDocument* 
pThis, const char* pCo
 
     if (!strcmp(pCommand, ".uno:LanguageStatus"))
     {
-        return getLanguages(pCommand);
+        return getLanguages(pThis, pCommand);
     }
     else if (!strcmp(pCommand, ".uno:CharFontName"))
     {

Reply via email to