Use officecfg instead of SvxDeeplOptions

Change-Id: I977d9be45fa17b960cccb0915c5f2762bf11288f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148885
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
diff --git a/cui/source/options/optdeepl.cxx b/cui/source/options/optdeepl.cxx
index 5343f72..6544a8f 100644
--- a/cui/source/options/optdeepl.cxx
+++ b/cui/source/options/optdeepl.cxx
@@ -18,7 +18,7 @@
 */

#include "optdeepl.hxx"
#include <svtools/deeplcfg.hxx>
#include <officecfg/Office/Linguistic.hxx>

OptDeeplTabPage::OptDeeplTabPage(weld::Container* pPage, weld::DialogController* pController,
                                 const SfxItemSet& rSet)
@@ -32,16 +32,17 @@ OptDeeplTabPage::~OptDeeplTabPage() {}

void OptDeeplTabPage::Reset(const SfxItemSet*)
{
    SvxDeeplOptions& rDeeplOptions = SvxDeeplOptions::Get();
    m_xAPIUrl->set_text(rDeeplOptions.getAPIUrl());
    m_xAuthKey->set_text(rDeeplOptions.getAuthKey());
    m_xAPIUrl->set_text(*officecfg::Office::Linguistic::Translation::Deepl::ApiURL::get());
    m_xAuthKey->set_text(*officecfg::Office::Linguistic::Translation::Deepl::AuthKey::get());
}

bool OptDeeplTabPage::FillItemSet(SfxItemSet*)
{
    SvxDeeplOptions& rDeeplOptions = SvxDeeplOptions::Get();
    rDeeplOptions.setAPIUrl(m_xAPIUrl->get_text());
    rDeeplOptions.setAuthKey(m_xAuthKey->get_text());
    std::shared_ptr<comphelper::ConfigurationChanges> batch(
        comphelper::ConfigurationChanges::create());
    officecfg::Office::Linguistic::Translation::Deepl::ApiURL::set(m_xAPIUrl->get_text(), batch);
    officecfg::Office::Linguistic::Translation::Deepl::AuthKey::set(m_xAuthKey->get_text(), batch);
    batch->commit();
    return false;
}

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 1a29cdd..bdab0c0 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -143,7 +143,6 @@
#include <tools/json_writer.hxx>
#include <svtools/ctrltool.hxx>
#include <svtools/langtab.hxx>
#include <svtools/deeplcfg.hxx>
#include <vcl/fontcharmap.hxx>
#ifdef IOS
#include <vcl/sysdata.hxx>
@@ -7351,9 +7350,10 @@ void setDeeplConfig()
        OUString aAuthKey = OStringToOUString(pAuthKeyString, RTL_TEXTENCODING_UTF8);
        try
        {
            SvxDeeplOptions& rDeeplOptions = SvxDeeplOptions::Get();
            rDeeplOptions.setAPIUrl(aAPIUrl);
            rDeeplOptions.setAuthKey(aAuthKey);
            std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
            officecfg::Office::Linguistic::Translation::Deepl::ApiURL::set(aAPIUrl, batch);
            officecfg::Office::Linguistic::Translation::Deepl::AuthKey::set(aAuthKey, batch);
            batch->commit();
        }
        catch(uno::Exception const& rException)
        {
diff --git a/include/svtools/deeplcfg.hxx b/include/svtools/deeplcfg.hxx
deleted file mode 100644
index a943d72d..0000000
--- a/include/svtools/deeplcfg.hxx
+++ /dev/null
@@ -1,50 +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 <unotools/configitem.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <svtools/svtdllapi.h>

using namespace utl;
using namespace com::sun::star::uno;

struct DeeplOptions_Impl;

class SVT_DLLPUBLIC SvxDeeplOptions final : public utl::ConfigItem
{
public:
    SvxDeeplOptions();
    virtual ~SvxDeeplOptions() override;

    virtual void Notify(const css::uno::Sequence<OUString>& _rPropertyNames) override;
    static SvxDeeplOptions& Get();

    const OUString& getAPIUrl() const;
    void setAPIUrl(const OUString& rVal);

    const OUString& getAuthKey() const;
    void setAuthKey(const OUString& rVal);

private:
    std::unique_ptr<DeeplOptions_Impl> pImpl;
    void Load(const css::uno::Sequence<OUString>& rPropertyNames);
    virtual void ImplCommit() override;
    static const Sequence<OUString>& GetPropertyNames();
};
diff --git a/svtools/Library_svt.mk b/svtools/Library_svt.mk
index 96714fd..46b68fb 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/deeplcfg \
    svtools/source/config/itemholder2 \
    svtools/source/config/miscopt \
    svtools/source/config/slidesorterbaropt \
diff --git a/svtools/source/config/deeplcfg.cxx b/svtools/source/config/deeplcfg.cxx
deleted file mode 100644
index 3b022b7..0000000
--- a/svtools/source/config/deeplcfg.cxx
+++ /dev/null
@@ -1,124 +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 .
 */

#include <sal/log.hxx>
#include <sal/config.h>
#include <svtools/deeplcfg.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <tools/debug.hxx>

using namespace utl;
using namespace com::sun::star::uno;

struct DeeplOptions_Impl
{
    OUString sAPIUrl;
    OUString sAuthKey;
};

const Sequence<OUString>& SvxDeeplOptions::GetPropertyNames()
{
    static Sequence<OUString> const aNames{
        "Deepl/ApiURL",
        "Deepl/AuthKey",
    };
    return aNames;
}

const OUString& SvxDeeplOptions::getAPIUrl() const { return pImpl->sAPIUrl; }

void SvxDeeplOptions::setAPIUrl(const OUString& rVal)
{
    pImpl->sAPIUrl = rVal;
    SetModified();
}

const OUString& SvxDeeplOptions::getAuthKey() const { return pImpl->sAuthKey; }

void SvxDeeplOptions::setAuthKey(const OUString& rVal)
{
    pImpl->sAuthKey = rVal;
    SetModified();
}

namespace
{
class theSvxDeeplOptions : public rtl::Static<SvxDeeplOptions, theSvxDeeplOptions>
{
};
}

SvxDeeplOptions& SvxDeeplOptions::Get() { return theSvxDeeplOptions::get(); }

SvxDeeplOptions::SvxDeeplOptions()
    : ConfigItem("Office.Linguistic/Translation")
    , pImpl(new DeeplOptions_Impl)
{
    Load(GetPropertyNames());
}

SvxDeeplOptions::~SvxDeeplOptions() {}
void SvxDeeplOptions::Notify(const css::uno::Sequence<OUString>&) { Load(GetPropertyNames()); }

void SvxDeeplOptions::Load(const css::uno::Sequence<OUString>& aNames)
{
    Sequence<Any> 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->sAPIUrl;
                break;
            case 1:
                pValues[nProp] >>= pImpl->sAuthKey;
                break;
            default:
                break;
        }
    }
}

void SvxDeeplOptions::ImplCommit()
{
    const Sequence<OUString>& aNames = GetPropertyNames();
    Sequence<Any> aValues(aNames.getLength());
    Any* pValues = aValues.getArray();
    for (int nProp = 0; nProp < aNames.getLength(); nProp++)
    {
        switch (nProp)
        {
            case 0:
                pValues[nProp] <<= pImpl->sAPIUrl;
                break;
            case 1:
                pValues[nProp] <<= pImpl->sAuthKey;
                break;
            default:
                break;
        }
    }
    PutProperties(aNames, aValues);
}
\ No newline at end of file
diff --git a/sw/source/ui/misc/translatelangselect.cxx b/sw/source/ui/misc/translatelangselect.cxx
index 2d438ed..8036cd1 100644
--- a/sw/source/ui/misc/translatelangselect.cxx
+++ b/sw/source/ui/misc/translatelangselect.cxx
@@ -30,7 +30,6 @@
#include <sal/log.hxx>
#include <ndtxt.hxx>
#include <shellio.hxx>
#include <svtools/deeplcfg.hxx>
#include <vcl/idle.hxx>
#include <mdiexp.hxx>
#include <strings.hrc>
@@ -38,6 +37,7 @@
#include <sfx2/viewfrm.hxx>
#include <com/sun/star/task/XStatusIndicatorFactory.hpp>
#include <linguistic/translate.hxx>
#include <officecfg/Office/Linguistic.hxx>

static const std::vector<SwLanguageListItem>& getLanguageVec()
{
@@ -133,8 +133,11 @@ IMPL_LINK_NOARG(SwTranslateLangSelectDlg, LangSelectTranslateHdl, weld::Button&,
        return;
    }

    SvxDeeplOptions& rDeeplOptions = SvxDeeplOptions::Get();
    if (rDeeplOptions.getAPIUrl().isEmpty() || rDeeplOptions.getAuthKey().isEmpty())
    std::optional<OUString> oDeeplAPIUrl
        = officecfg::Office::Linguistic::Translation::Deepl::ApiURL::get();
    std::optional<OUString> oDeeplKey
        = officecfg::Office::Linguistic::Translation::Deepl::AuthKey::get();
    if (!oDeeplAPIUrl || oDeeplAPIUrl->isEmpty() || !oDeeplKey || oDeeplKey->isEmpty())
    {
        SAL_WARN("sw.ui", "SwTranslateLangSelectDlg: API options are not set");
        m_xDialog->response(RET_CANCEL);
@@ -142,11 +145,10 @@ IMPL_LINK_NOARG(SwTranslateLangSelectDlg, LangSelectTranslateHdl, weld::Button&,
    }

    const OString aAPIUrl
        = OUStringToOString(rtl::Concat2View(rDeeplOptions.getAPIUrl() + "?tag_handling=html"),
        = OUStringToOString(rtl::Concat2View(*oDeeplAPIUrl + "?tag_handling=html"),
                            RTL_TEXTENCODING_UTF8)
              .trim();
    const OString aAuthKey
        = OUStringToOString(rDeeplOptions.getAuthKey(), RTL_TEXTENCODING_UTF8).trim();
    const OString aAuthKey = OUStringToOString(*oDeeplKey, RTL_TEXTENCODING_UTF8).trim();
    const auto aTargetLang
        = getLanguageVec().at(SwTranslateLangSelectDlg::selectedLangIdx).getLanguage();

diff --git a/sw/source/uibase/shells/textsh1.cxx b/sw/source/uibase/shells/textsh1.cxx
index b52e267..c79d2be 100644
--- a/sw/source/uibase/shells/textsh1.cxx
+++ b/sw/source/uibase/shells/textsh1.cxx
@@ -108,9 +108,9 @@
#include <config_wasm_strip.h>
#if !ENABLE_WASM_STRIP_EXTRA
#include <officecfg/Office/Common.hxx>
#include <officecfg/Office/Linguistic.hxx>
#include <svl/visitem.hxx>
#include <translatelangselect.hxx>
#include <svtools/deeplcfg.hxx>
#endif // ENABLE_WASM_STRIP_EXTRA
#include <translatehelper.hxx>
#include <IDocumentContentOperations.hxx>
@@ -1940,14 +1940,15 @@ void SwTextShell::Execute(SfxRequest &rReq)
        const SfxPoolItem* pTargetLangStringItem = nullptr;
        if (pArgs && SfxItemState::SET == pArgs->GetItemState(SID_ATTR_TARGETLANG_STR, false, &pTargetLangStringItem))
        {
            SvxDeeplOptions& rDeeplOptions = SvxDeeplOptions::Get();
            if (rDeeplOptions.getAPIUrl().isEmpty() || rDeeplOptions.getAuthKey().isEmpty())
            std::optional<OUString> oDeeplAPIUrl = officecfg::Office::Linguistic::Translation::Deepl::ApiURL::get();
            std::optional<OUString> oDeeplKey = officecfg::Office::Linguistic::Translation::Deepl::AuthKey::get();
            if (!oDeeplAPIUrl || oDeeplAPIUrl->isEmpty() || !oDeeplKey || oDeeplKey->isEmpty())
            {
                SAL_WARN("sw.ui", "SID_FM_TRANSLATE: API options are not set");
                break;
            }
            const OString aAPIUrl = OUStringToOString(rtl::Concat2View(rDeeplOptions.getAPIUrl() + "?tag_handling=html"), RTL_TEXTENCODING_UTF8).trim();
            const OString aAuthKey = OUStringToOString(rDeeplOptions.getAuthKey(), RTL_TEXTENCODING_UTF8).trim();
            const OString aAPIUrl = OUStringToOString(rtl::Concat2View(*oDeeplAPIUrl + "?tag_handling=html"), RTL_TEXTENCODING_UTF8).trim();
            const OString aAuthKey = OUStringToOString(*oDeeplKey, RTL_TEXTENCODING_UTF8).trim();
            OString aTargetLang = OUStringToOString(static_cast<const SfxStringItem*>(pTargetLangStringItem)->GetValue(), RTL_TEXTENCODING_UTF8);
            SwTranslateHelper::TranslateAPIConfig aConfig({aAPIUrl, aAuthKey, aTargetLang});
            SwTranslateHelper::TranslateDocument(rWrtSh, aConfig);
@@ -2474,8 +2475,9 @@ void SwTextShell::GetState( SfxItemSet &rSet )
                        rSet.Put(SfxVisibilityItem(nWhich, false));
                        break;
                    }
                    const SvxDeeplOptions& rDeeplOptions = SvxDeeplOptions::Get();
                    if (rDeeplOptions.getAPIUrl().isEmpty() || rDeeplOptions.getAuthKey().isEmpty())
                    std::optional<OUString> oDeeplAPIUrl = officecfg::Office::Linguistic::Translation::Deepl::ApiURL::get();
                    std::optional<OUString> oDeeplKey = officecfg::Office::Linguistic::Translation::Deepl::AuthKey::get();
                    if (!oDeeplAPIUrl || oDeeplAPIUrl->isEmpty() || !oDeeplKey || oDeeplKey->isEmpty())
                    {
                        rSet.DisableItem(nWhich);
                    }