From 9aca8e1333393af69972450d5e7e924cdfed4269 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Thu, 9 Mar 2023 17:17:41 +0300 Subject: [PATCH] Use officecfg instead of SvxLanguageToolOptions Change-Id: Ia9add4ff3ebe20ba491e33de1e9a2644a48ef7a3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148548 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- cui/source/options/optlanguagetool.cxx | 47 ++-- cui/source/options/optlanguagetool.hxx | 2 - desktop/source/lib/init.cxx | 19 +- include/svtools/languagetoolcfg.hxx | 65 ------ lingucomponent/Library_LanguageTool.mk | 4 + .../languagetool/languagetoolimp.cxx | 62 +++-- .../CppunitTest_linguistic_restprotocol.mk | 4 + linguistic/qa/restprotocol.cxx | 28 ++- svtools/Library_svt.mk | 1 - svtools/source/config/languagetoolcfg.cxx | 211 ------------------ 10 files changed, 100 insertions(+), 343 deletions(-) delete mode 100644 include/svtools/languagetoolcfg.hxx delete mode 100644 svtools/source/config/languagetoolcfg.cxx diff --git a/cui/source/options/optlanguagetool.cxx b/cui/source/options/optlanguagetool.cxx index 7a087b09e8c0..485e8ecb1be6 100644 --- a/cui/source/options/optlanguagetool.cxx +++ b/cui/source/options/optlanguagetool.cxx @@ -18,11 +18,14 @@ */ #include "optlanguagetool.hxx" -#include +#include #include #include #include +using LanguageToolCfg = officecfg::Office::Linguistic::GrammarChecking::LanguageTool; +constexpr OUStringLiteral LANGUAGETOOL_DEFAULT_URL = u"https://api.languagetool.org/v2"; + OptLanguageToolTabPage::OptLanguageToolTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet) @@ -36,8 +39,7 @@ OptLanguageToolTabPage::OptLanguageToolTabPage(weld::Container* pPage, , m_xApiSettingsFrame(m_xBuilder->weld_frame("apisettings")) { m_xActivateBox->connect_toggled(LINK(this, OptLanguageToolTabPage, CheckHdl)); - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - EnableControls(rLanguageOpts.getEnabled()); + EnableControls(LanguageToolCfg::IsEnabled::get()); // tdf#150494 Set default values as placeholder text m_xBaseURLED->set_placeholder_text(CuiResId(RID_LANGUAGETOOL_LEAVE_EMPTY)); @@ -49,11 +51,15 @@ OptLanguageToolTabPage::~OptLanguageToolTabPage() {} void OptLanguageToolTabPage::EnableControls(bool bEnable) { - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - rLanguageOpts.setEnabled(bEnable); + if (bEnable != LanguageToolCfg::IsEnabled::get()) + { + auto batch(comphelper::ConfigurationChanges::create()); + LanguageToolCfg::IsEnabled::set(bEnable, batch); + batch->commit(); + } m_xApiSettingsFrame->set_visible(bEnable); m_xActivateBox->set_active(bEnable); - m_xSSLDisableVerificationBox->set_active(rLanguageOpts.getSSLVerification() != true); + m_xSSLDisableVerificationBox->set_active(!LanguageToolCfg::SSLCertVerify::get()); } IMPL_LINK_NOARG(OptLanguageToolTabPage, CheckHdl, weld::Toggleable&, void) @@ -63,36 +69,35 @@ IMPL_LINK_NOARG(OptLanguageToolTabPage, CheckHdl, weld::Toggleable&, void) void OptLanguageToolTabPage::Reset(const SfxItemSet*) { - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - // tdf#150494 If no URL has been set, use the default URL - OUString aBaseURL = rLanguageOpts.getBaseURL(); + OUString aBaseURL = LanguageToolCfg::BaseURL::get().value_or(""); if (aBaseURL.isEmpty()) m_xBaseURLED->set_text(LANGUAGETOOL_DEFAULT_URL); else - m_xBaseURLED->set_text(rLanguageOpts.getBaseURL()); + m_xBaseURLED->set_text(aBaseURL); - m_xUsernameED->set_text(rLanguageOpts.getUsername()); - m_xApiKeyED->set_text(rLanguageOpts.getApiKey()); - m_xRestProtocol->set_text(rLanguageOpts.getRestProtocol()); - m_xSSLDisableVerificationBox->set_active(rLanguageOpts.getSSLVerification() != true); + m_xUsernameED->set_text(LanguageToolCfg::Username::get().value_or("")); + m_xApiKeyED->set_text(LanguageToolCfg::ApiKey::get().value_or("")); + m_xRestProtocol->set_text(LanguageToolCfg::RestProtocol::get().value_or("")); + m_xSSLDisableVerificationBox->set_active(!LanguageToolCfg::SSLCertVerify::get()); } bool OptLanguageToolTabPage::FillItemSet(SfxItemSet*) { - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); + auto batch(comphelper::ConfigurationChanges::create()); // tdf#150494 If no URL has been set, then save the default URL OUString aBaseURL = m_xBaseURLED->get_text(); if (aBaseURL.isEmpty()) - rLanguageOpts.setBaseURL(LANGUAGETOOL_DEFAULT_URL); + LanguageToolCfg::BaseURL::set(LANGUAGETOOL_DEFAULT_URL, batch); else - rLanguageOpts.setBaseURL(aBaseURL); + LanguageToolCfg::BaseURL::set(aBaseURL, batch); - rLanguageOpts.setUsername(m_xUsernameED->get_text()); - rLanguageOpts.setApiKey(m_xApiKeyED->get_text()); - rLanguageOpts.setRestProtocol(m_xRestProtocol->get_text()); - rLanguageOpts.setSSLVerification(m_xSSLDisableVerificationBox->get_active() != true); + LanguageToolCfg::Username::set(m_xUsernameED->get_text(), batch); + LanguageToolCfg::ApiKey::set(m_xApiKeyED->get_text(), batch); + LanguageToolCfg::RestProtocol::set(m_xRestProtocol->get_text(), batch); + LanguageToolCfg::SSLCertVerify::set(!m_xSSLDisableVerificationBox->get_active(), batch); + batch->commit(); return false; } diff --git a/cui/source/options/optlanguagetool.hxx b/cui/source/options/optlanguagetool.hxx index 141d88f63bb4..2dbe889f57da 100644 --- a/cui/source/options/optlanguagetool.hxx +++ b/cui/source/options/optlanguagetool.hxx @@ -20,8 +20,6 @@ #include #include -inline constexpr OUStringLiteral LANGUAGETOOL_DEFAULT_URL = u"https://api.languagetool.org/v2"; - class OptLanguageToolTabPage : public SfxTabPage { public: diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 54ffbac8e322..cf23f09f1413 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -144,7 +144,6 @@ #include #include #include -#include #include #ifdef IOS #include @@ -196,6 +195,7 @@ #include "lokclipboard.hxx" #include #include +#include #include #include #include @@ -7372,26 +7372,29 @@ void setLanguageToolConfig() OUString aBaseUrl = OStringToOUString(pBaseUrlString, RTL_TEXTENCODING_UTF8); try { - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - rLanguageOpts.setBaseURL(aBaseUrl); - rLanguageOpts.setEnabled(true); + using LanguageToolCfg = officecfg::Office::Linguistic::GrammarChecking::LanguageTool; + auto batch(comphelper::ConfigurationChanges::create()); + + LanguageToolCfg::BaseURL::set(aBaseUrl, batch); + LanguageToolCfg::IsEnabled::set(true, batch); if (pSSLVerification) { OUString aSSLVerification = OStringToOUString(pSSLVerification, RTL_TEXTENCODING_UTF8); - rLanguageOpts.setSSLVerification(aSSLVerification == "true"); + LanguageToolCfg::SSLCertVerify::set(aSSLVerification == "true", batch); } if (pRestProtocol) { OUString aRestProtocol = OStringToOUString(pRestProtocol, RTL_TEXTENCODING_UTF8); - rLanguageOpts.setRestProtocol(aRestProtocol); + LanguageToolCfg::RestProtocol::set(aRestProtocol, batch); } if (pUsername && pApikey) { OUString aUsername = OStringToOUString(pUsername, RTL_TEXTENCODING_UTF8); OUString aApiKey = OStringToOUString(pApikey, RTL_TEXTENCODING_UTF8); - rLanguageOpts.setUsername(aUsername); - rLanguageOpts.setApiKey(aApiKey); + LanguageToolCfg::Username::set(aUsername, batch); + LanguageToolCfg::ApiKey::set(aApiKey, batch); } + batch->commit(); css::uno::Reference xLangSrv = css::linguistic2::LinguServiceManager::create(xContext); diff --git a/include/svtools/languagetoolcfg.hxx b/include/svtools/languagetoolcfg.hxx deleted file mode 100644 index 24c4de6408aa..000000000000 --- a/include/svtools/languagetoolcfg.hxx +++ /dev/null @@ -1,65 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#pragma once -#include -#include -#include - -using namespace utl; -using namespace com::sun::star::uno; - -struct LanguageToolOptions_Impl; - -class SVT_DLLPUBLIC SvxLanguageToolOptions final : public utl::ConfigItem -{ -public: - SvxLanguageToolOptions(); - virtual ~SvxLanguageToolOptions() override; - - virtual void Notify(const css::uno::Sequence& _rPropertyNames) override; - static SvxLanguageToolOptions& Get(); - - const OUString& getBaseURL() const; - void setBaseURL(const OUString& rVal); - - const OUString& getRestProtocol() const; - void setRestProtocol(const OUString& rVal); - - const OUString& getUsername() const; - void setUsername(const OUString& rVal); - - OUString getLocaleListURL() const; - OUString getCheckerURL() const; - - const OUString& getApiKey() const; - void setApiKey(const OUString& rVal); - - bool getEnabled() const; - void setEnabled(bool enabled); - - bool getSSLVerification() const; - void setSSLVerification(bool enabled); - -private: - std::unique_ptr pImpl; - void Load(const css::uno::Sequence& rPropertyNames); - virtual void ImplCommit() override; - static const Sequence& GetPropertyNames(); -}; diff --git a/lingucomponent/Library_LanguageTool.mk b/lingucomponent/Library_LanguageTool.mk index 98ff7cf7edf2..91907fe2b52f 100644 --- a/lingucomponent/Library_LanguageTool.mk +++ b/lingucomponent/Library_LanguageTool.mk @@ -41,6 +41,10 @@ $(eval $(call gb_Library_use_externals,LanguageTool,\ curl \ )) +$(eval $(call gb_Library_use_custom_headers,LanguageTool,\ + officecfg/registry \ +)) + $(eval $(call gb_Library_add_exception_objects,LanguageTool,\ lingucomponent/source/spellcheck/languagetool/languagetoolimp \ )) diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx index 1c167e61df60..c68a17cbd466 100644 --- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx +++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx @@ -34,13 +34,14 @@ #include #include +#include + #include #include #include #include #include #include -#include #include #include #include @@ -58,6 +59,7 @@ using namespace com::sun::star::linguistic2; namespace { constexpr size_t MAX_SUGGESTIONS_SIZE = 10; +using LanguageToolCfg = officecfg::Office::Linguistic::GrammarChecking::LanguageTool; PropertyValue lcl_GetLineColorPropertyFromErrorId(const std::string& rErrorId) { @@ -145,9 +147,8 @@ std::string makeHttpRequest_impl(std::u16string_view aURL, HTTP_METHOD method, (void)curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, WriteCallback); (void)curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response_body); - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); // allow unknown or self-signed certificates - if (rLanguageOpts.getSSLVerification() == false) + if (!LanguageToolCfg::SSLCertVerify::get()) { (void)curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, false); (void)curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, false); @@ -174,8 +175,8 @@ std::string makeDudenHttpRequest(std::u16string_view aURL, const OString& aPostD tools::Long& nStatusCode) { struct curl_slist* pList = nullptr; - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - OString sAccessToken = OUStringToOString(rLanguageOpts.getApiKey(), RTL_TEXTENCODING_UTF8); + OString sAccessToken + = OUStringToOString(LanguageToolCfg::ApiKey::get().value_or(""), RTL_TEXTENCODING_UTF8); pList = curl_slist_append(pList, "Cache-Control: no-cache"); pList = curl_slist_append(pList, "Content-Type: application/json"); @@ -194,9 +195,10 @@ std::string makeHttpRequest(std::u16string_view aURL, HTTP_METHOD method, const OString realPostData(aPostData); if (method == HTTP_METHOD::HTTP_POST) { - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - OString apiKey = OUStringToOString(rLanguageOpts.getApiKey(), RTL_TEXTENCODING_UTF8); - OString username = OUStringToOString(rLanguageOpts.getUsername(), RTL_TEXTENCODING_UTF8); + OString apiKey + = OUStringToOString(LanguageToolCfg::ApiKey::get().value_or(""), RTL_TEXTENCODING_UTF8); + OString username = OUStringToOString(LanguageToolCfg::Username::get().value_or(""), + RTL_TEXTENCODING_UTF8); if (!apiKey.isEmpty() && !username.isEmpty()) realPostData += "&username=" + username + "&apiKey=" + apiKey; } @@ -205,7 +207,7 @@ std::string makeHttpRequest(std::u16string_view aURL, HTTP_METHOD method, const } template -Sequence parseJson(std::string&& json, std::string path, Func f) +uno::Sequence parseJson(std::string&& json, std::string path, Func f) { std::stringstream aStream(std::move(json)); // Optimized in C++20 boost::property_tree::ptree aRoot; @@ -213,7 +215,7 @@ Sequence parseJson(std::string&& json, std::string path if (auto tree = aRoot.get_child_optional(path)) { - Sequence aErrors(tree->size()); + uno::Sequence aErrors(tree->size()); auto it = tree->begin(); for (auto& rError : asNonConstRange(aErrors)) { @@ -292,6 +294,22 @@ void parseProofreadingJSONResponse(ProofreadingResult& rResult, std::string&& aJ } }); } + +OUString getLocaleListURL() +{ + if (auto oURL = LanguageToolCfg::BaseURL::get()) + if (!oURL->isEmpty()) + return *oURL + "/languages"; + return {}; +} + +OUString getCheckerURL() +{ + if (auto oURL = LanguageToolCfg::BaseURL::get()) + if (!oURL->isEmpty()) + return *oURL + "/check"; + return {}; +} } LanguageToolGrammarChecker::LanguageToolGrammarChecker() @@ -316,17 +334,16 @@ sal_Bool SAL_CALL LanguageToolGrammarChecker::hasLocale(const Locale& rLocale) return false; } -Sequence SAL_CALL LanguageToolGrammarChecker::getLocales() +uno::Sequence SAL_CALL LanguageToolGrammarChecker::getLocales() { if (m_aSuppLocales.hasElements()) return m_aSuppLocales; - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - if (!rLanguageOpts.getEnabled()) + if (!LanguageToolCfg::IsEnabled::get()) { return m_aSuppLocales; } - OUString localeUrl = rLanguageOpts.getLocaleListURL(); + OUString localeUrl = getLocaleListURL(); if (localeUrl.isEmpty()) { return m_aSuppLocales; @@ -363,7 +380,7 @@ Sequence SAL_CALL LanguageToolGrammarChecker::getLocales() ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading( const OUString& aDocumentIdentifier, const OUString& aText, const Locale& aLocale, sal_Int32 nStartOfSentencePosition, sal_Int32 nSuggestedBehindEndOfSentencePosition, - const Sequence& aProperties) + const uno::Sequence& aProperties) { // ProofreadingResult declared here instead of parseHttpJSONResponse because of the early exists. ProofreadingResult xRes; @@ -372,9 +389,9 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading( xRes.aLocale = aLocale; xRes.nStartOfSentencePosition = nStartOfSentencePosition; xRes.nBehindEndOfSentencePosition = nSuggestedBehindEndOfSentencePosition; - xRes.aProperties = Sequence(); + xRes.aProperties = {}; xRes.xProofreader = this; - xRes.aErrors = Sequence(); + xRes.aErrors = {}; if (aText.isEmpty()) { @@ -388,13 +405,12 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading( xRes.nStartOfNextSentencePosition = aText.getLength(); - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - if (rLanguageOpts.getEnabled() == false) + if (!LanguageToolCfg::IsEnabled::get()) { return xRes; } - OUString checkerURL = rLanguageOpts.getCheckerURL(); + OUString checkerURL = getCheckerURL(); if (checkerURL.isEmpty()) { return xRes; @@ -424,7 +440,7 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading( OString langTag(LanguageTag::convertToBcp47(aLocale, false).toUtf8()); OString postData = encodeTextForLanguageTool(aText); - const bool bDudenProtocol = rLanguageOpts.getRestProtocol() == "duden"; + const bool bDudenProtocol = LanguageToolCfg::RestProtocol::get().value_or("") == "duden"; if (bDudenProtocol) { std::stringstream aStream; @@ -501,12 +517,12 @@ sal_Bool SAL_CALL LanguageToolGrammarChecker::supportsService(const OUString& Se return cppu::supportsService(this, ServiceName); } -Sequence SAL_CALL LanguageToolGrammarChecker::getSupportedServiceNames() +uno::Sequence SAL_CALL LanguageToolGrammarChecker::getSupportedServiceNames() { return { SN_GRAMMARCHECKER }; } -void SAL_CALL LanguageToolGrammarChecker::initialize(const Sequence& /*rArguments*/) {} +void SAL_CALL LanguageToolGrammarChecker::initialize(const uno::Sequence&) {} extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* lingucomponent_LanguageToolGrammarChecker_get_implementation( diff --git a/linguistic/CppunitTest_linguistic_restprotocol.mk b/linguistic/CppunitTest_linguistic_restprotocol.mk index 4e56286efe0d..5a825a4fbb34 100644 --- a/linguistic/CppunitTest_linguistic_restprotocol.mk +++ b/linguistic/CppunitTest_linguistic_restprotocol.mk @@ -36,4 +36,8 @@ $(eval $(call gb_CppunitTest_use_ure,linguistic_restprotocol)) $(eval $(call gb_CppunitTest_use_rdb,linguistic_restprotocol,services)) +$(eval $(call gb_CppunitTest_use_custom_headers,linguistic_restprotocol,\ + officecfg/registry \ +)) + # vim: set noet sw=4 ts=4: diff --git a/linguistic/qa/restprotocol.cxx b/linguistic/qa/restprotocol.cxx index 61c04185c757..c189228e6635 100644 --- a/linguistic/qa/restprotocol.cxx +++ b/linguistic/qa/restprotocol.cxx @@ -18,8 +18,8 @@ #include #include #include -#include #include +#include #include #include @@ -138,24 +138,28 @@ private: void TestRestProtocol::testProofreading() { css::lang::Locale aLocale("en", "US", ""); - Sequence<::com::sun::star::beans::PropertyValue> aProperties; - SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get(); - rLanguageOpts.setBaseURL("http://127.0.0.1:2022/api"); - rLanguageOpts.setUsername("hcastro"); - rLanguageOpts.setApiKey("hcvhcvhcv"); - rLanguageOpts.setEnabled(true); - rLanguageOpts.setSSLVerification(false); - rLanguageOpts.setRestProtocol("duden"); - CPPUNIT_ASSERT_EQUAL(OUString("duden"), rLanguageOpts.getRestProtocol()); + using LanguageToolCfg = officecfg::Office::Linguistic::GrammarChecking::LanguageTool; + auto batch(comphelper::ConfigurationChanges::create()); + + LanguageToolCfg::BaseURL::set("http://127.0.0.1:2022/api", batch); + LanguageToolCfg::Username::set("hcastro", batch); + LanguageToolCfg::ApiKey::set("hcvhcvhcv", batch); + LanguageToolCfg::IsEnabled::set(true, batch); + LanguageToolCfg::SSLCertVerify::set(false, batch); + LanguageToolCfg::RestProtocol::set("duden", batch); + + batch->commit(); + + CPPUNIT_ASSERT_EQUAL(OUString("duden"), *LanguageToolCfg::RestProtocol::get()); Reference<::com::sun::star::linguistic2::XProofreader> xProofreader( m_xSFactory->createInstance("com.sun.star.linguistic2.Proofreader"), UNO_QUERY); CPPUNIT_ASSERT(xProofreader.is()); com::sun::star::linguistic2::ProofreadingResult aResult - = xProofreader->doProofreading("id", "ths is a tst", aLocale, 0, 0, aProperties); + = xProofreader->doProofreading("id", "ths is a tst", aLocale, 0, 0, {}); - CPPUNIT_ASSERT_EQUAL(2, aResult.aErrors.getLength()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aResult.aErrors.getLength()); } void TestRestProtocol::setUp() diff --git a/svtools/Library_svt.mk b/svtools/Library_svt.mk index ec2405d65636..96714fdcfea1 100644 --- a/svtools/Library_svt.mk +++ b/svtools/Library_svt.mk @@ -84,7 +84,6 @@ $(eval $(call gb_Library_add_exception_objects,svt,\ svtools/source/config/extcolorcfg \ svtools/source/config/fontsubstconfig \ svtools/source/config/htmlcfg \ - svtools/source/config/languagetoolcfg \ svtools/source/config/deeplcfg \ svtools/source/config/itemholder2 \ svtools/source/config/miscopt \ diff --git a/svtools/source/config/languagetoolcfg.cxx b/svtools/source/config/languagetoolcfg.cxx deleted file mode 100644 index bc18e23980db..000000000000 --- a/svtools/source/config/languagetoolcfg.cxx +++ /dev/null @@ -1,211 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ - -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include -#include -#include -#include -#include - -using namespace utl; -using namespace com::sun::star::uno; - -struct LanguageToolOptions_Impl -{ - OUString sBaseURL; - OUString sUsername; - OUString sApiKey; - OUString sRestProtocol; - bool bEnabled; - bool bSSLCertVerificatrionEnabled; -}; - -const Sequence& SvxLanguageToolOptions::GetPropertyNames() -{ - static Sequence const aNames{ - "LanguageTool/BaseURL", "LanguageTool/Username", "LanguageTool/ApiKey", - "LanguageTool/IsEnabled", "LanguageTool/SSLCertVerify", "LanguageTool/RestProtocol" - }; - return aNames; -} - -const OUString& SvxLanguageToolOptions::getBaseURL() const { return pImpl->sBaseURL; } - -void SvxLanguageToolOptions::setBaseURL(const OUString& rVal) -{ - pImpl->sBaseURL = rVal; - SetModified(); -} - -const OUString& SvxLanguageToolOptions::getUsername() const { return pImpl->sUsername; } - -void SvxLanguageToolOptions::setUsername(const OUString& rVal) -{ - pImpl->sUsername = rVal; - SetModified(); -} - -OUString SvxLanguageToolOptions::getLocaleListURL() const -{ - if (pImpl->sBaseURL.isEmpty()) - return OUString(); - return pImpl->sBaseURL + "/languages"; -} - -OUString SvxLanguageToolOptions::getCheckerURL() const -{ - if (pImpl->sBaseURL.isEmpty()) - return OUString(); - return pImpl->sBaseURL + "/check"; -} - -const OUString& SvxLanguageToolOptions::getApiKey() const { return pImpl->sApiKey; } - -const OUString& SvxLanguageToolOptions::getRestProtocol() const { return pImpl->sRestProtocol; } - -void SvxLanguageToolOptions::setApiKey(const OUString& rVal) -{ - pImpl->sApiKey = rVal; - SetModified(); -} - -void SvxLanguageToolOptions::setRestProtocol(const OUString& rVal) -{ - if (pImpl->sRestProtocol != rVal) - { - pImpl->sRestProtocol = rVal; - SetModified(); - } -} - -bool SvxLanguageToolOptions::getEnabled() const { return pImpl->bEnabled; } - -bool SvxLanguageToolOptions::getSSLVerification() const -{ - return pImpl->bSSLCertVerificatrionEnabled; -} - -void SvxLanguageToolOptions::setSSLVerification(bool bEnabled) -{ - pImpl->bSSLCertVerificatrionEnabled = bEnabled; - SetModified(); -} - -void SvxLanguageToolOptions::setEnabled(bool bEnabled) -{ - pImpl->bEnabled = bEnabled; - SetModified(); -} - -namespace -{ -class theSvxLanguageToolOptions - : public rtl::Static -{ -}; -} - -SvxLanguageToolOptions& SvxLanguageToolOptions::Get() { return theSvxLanguageToolOptions::get(); } - -SvxLanguageToolOptions::SvxLanguageToolOptions() - : ConfigItem("Office.Linguistic/GrammarChecking") - , pImpl(new LanguageToolOptions_Impl) -{ - Load(GetPropertyNames()); -} - -SvxLanguageToolOptions::~SvxLanguageToolOptions() {} -void SvxLanguageToolOptions::Notify(const css::uno::Sequence&) -{ - Load(GetPropertyNames()); -} - -void SvxLanguageToolOptions::Load(const css::uno::Sequence& aNames) -{ - Sequence aValues = GetProperties(aNames); - const Any* pValues = aValues.getConstArray(); - DBG_ASSERT(aValues.getLength() == aNames.getLength(), "GetProperties failed"); - if (aValues.getLength() != aNames.getLength()) - return; - for (int nProp = 0; nProp < aNames.getLength(); nProp++) - { - if (!pValues[nProp].hasValue()) - continue; - switch (nProp) - { - case 0: - pValues[nProp] >>= pImpl->sBaseURL; - break; - case 1: - pValues[nProp] >>= pImpl->sUsername; - break; - case 2: - pValues[nProp] >>= pImpl->sApiKey; - break; - case 3: - pValues[nProp] >>= pImpl->bEnabled; - break; - case 4: - pValues[nProp] >>= pImpl->bSSLCertVerificatrionEnabled; - break; - case 5: - pValues[nProp] >>= pImpl->sRestProtocol; - break; - default: - break; - } - } -} - -void SvxLanguageToolOptions::ImplCommit() -{ - const Sequence& aNames = GetPropertyNames(); - Sequence aValues(aNames.getLength()); - Any* pValues = aValues.getArray(); - for (int nProp = 0; nProp < aNames.getLength(); nProp++) - { - switch (nProp) - { - case 0: - pValues[nProp] <<= pImpl->sBaseURL; - break; - case 1: - pValues[nProp] <<= pImpl->sUsername; - break; - case 2: - pValues[nProp] <<= pImpl->sApiKey; - break; - case 3: - pValues[nProp] <<= pImpl->bEnabled; - break; - case 4: - pValues[nProp] <<= pImpl->bSSLCertVerificatrionEnabled; - break; - case 5: - pValues[nProp] <<= pImpl->sRestProtocol; - break; - default: - break; - } - } - PutProperties(aNames, aValues); -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */