From f74031d4eaeb289f062f77209da10d464c09c2a6 Mon Sep 17 00:00:00 2001 From: Henry Castro Date: Wed, 11 Jan 2023 16:14:57 -0400 Subject: 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 Change-Id: Ic210f31eddd3208b29d073ff35ba4fa2d98ea772 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145363 Tested-by: Jenkins CollaboraOffice Reviewed-by: Andras Timar (cherry picked from commit bb0c63cc73998a0f67ae4c3e59acea3827a6e11c) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146399 Tested-by: Jenkins --- desktop/source/lib/init.cxx | 129 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 2 deletions(-) (limited to 'desktop') diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 71c53f2d097a..d19d2717ffaa 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -77,6 +77,7 @@ #include #include #include +#include #include #include #include @@ -102,6 +103,7 @@ #include #include +#include #include #include #include @@ -5384,6 +5386,119 @@ static void doc_setGraphicSelection(LibreOfficeKitDocument* pThis, int nType, in pDoc->setGraphicSelection(nType, nX, nY); } +static void getDocLanguages(LibreOfficeKitDocument* pThis, uno::Sequence& rSeq) +{ + SfxViewFrame* pViewFrame = SfxViewFrame::Current(); + if (!pViewFrame) + return; + + SfxDispatcher* pDispatcher = pViewFrame->GetBindings().GetDispatcher(); + if (!pDispatcher) + return; + + css::uno::Any aLangStatus; + pDispatcher->QueryState(SID_LANGUAGE_STATUS, aLangStatus); + + Sequence aSeqLang; + if (!(aLangStatus >>= aSeqLang)) + return; + + // (aSeqLang[0] == "Current Language", aSeqLang[1] == "Script Type", + // aSeqLang[2] == "Keyboard Language", aSeqLang[3] == "Guess Text Lang") + if (aSeqLang.getLength() != 4) + return; + + LanguageType nLangType; + std::set aLangItems; + SvtScriptType eScriptType = static_cast(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 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(pThis); + Reference xDocumentLanguages(pDocument->mxComponent, UNO_QUERY); + if (xDocumentLanguages.is()) + { + const Sequence aLocales(xDocumentLanguages->getDocumentLanguages( + static_cast(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 aLocales(aLangItems.size()); + auto pLocales = aLocales.getArray(); + for (const LanguageType& itLang : aLangItems) + { + pLocales[nLocale++] = LanguageTag::convertToLocale(itLang); + } + + rSeq = aLocales; +} + static void doc_resetSelection(LibreOfficeKitDocument* pThis) { comphelper::ProfileZone aZone("doc_resetSelection"); @@ -5401,7 +5516,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; @@ -5414,6 +5529,16 @@ 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()) + { + uno::Sequence< css::lang::Locale > aSeq; + getDocLanguages(pThis, aSeq); + aLocales = aSeq; + } } boost::property_tree::ptree aTree; @@ -5770,7 +5895,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")) { -- cgit