1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "optlanguagetool.hxx"
21 #include <officecfg/Office/Linguistic.hxx>
22 #include <sal/log.hxx>
23 #include <dialmgr.hxx>
24 #include <strings.hrc>
26 using LanguageToolCfg
= officecfg::Office::Linguistic::GrammarChecking::LanguageTool
;
27 constexpr OUStringLiteral LANGUAGETOOL_DEFAULT_URL
= u
"https://api.languagetool.org/v2";
29 OptLanguageToolTabPage::OptLanguageToolTabPage(weld::Container
* pPage
,
30 weld::DialogController
* pController
,
31 const SfxItemSet
& rSet
)
32 : SfxTabPage(pPage
, pController
, "cui/ui/langtoolconfigpage.ui", "OptLangToolPage", &rSet
)
33 , m_xBaseURLED(m_xBuilder
->weld_entry("baseurl"))
34 , m_xUsernameED(m_xBuilder
->weld_entry("username"))
35 , m_xApiKeyED(m_xBuilder
->weld_entry("apikey"))
36 , m_xRestProtocol(m_xBuilder
->weld_entry("restprotocol"))
37 , m_xActivateBox(m_xBuilder
->weld_check_button("activate"))
38 , m_xSSLDisableVerificationBox(m_xBuilder
->weld_check_button("verifyssl"))
39 , m_xApiSettingsFrame(m_xBuilder
->weld_frame("apisettings"))
41 m_xActivateBox
->connect_toggled(LINK(this, OptLanguageToolTabPage
, CheckHdl
));
42 EnableControls(LanguageToolCfg::IsEnabled::get());
44 // tdf#150494 Set default values as placeholder text
45 m_xBaseURLED
->set_placeholder_text(CuiResId(RID_LANGUAGETOOL_LEAVE_EMPTY
));
46 m_xUsernameED
->set_placeholder_text(CuiResId(RID_LANGUAGETOOL_LEAVE_EMPTY
));
47 m_xApiKeyED
->set_placeholder_text(CuiResId(RID_LANGUAGETOOL_LEAVE_EMPTY
));
48 m_xRestProtocol
->set_placeholder_text(CuiResId(RID_LANGUAGETOOL_REST_LEAVE_EMPTY
));
51 OptLanguageToolTabPage::~OptLanguageToolTabPage() {}
53 void OptLanguageToolTabPage::EnableControls(bool bEnable
)
55 if (bEnable
!= LanguageToolCfg::IsEnabled::get())
57 auto batch(comphelper::ConfigurationChanges::create());
58 LanguageToolCfg::IsEnabled::set(bEnable
, batch
);
61 m_xApiSettingsFrame
->set_visible(bEnable
);
62 m_xActivateBox
->set_active(bEnable
);
63 m_xSSLDisableVerificationBox
->set_active(!LanguageToolCfg::SSLCertVerify::get());
66 IMPL_LINK_NOARG(OptLanguageToolTabPage
, CheckHdl
, weld::Toggleable
&, void)
68 EnableControls(m_xActivateBox
->get_active());
71 void OptLanguageToolTabPage::Reset(const SfxItemSet
*)
73 // tdf#150494 If no URL has been set, use the default URL
74 OUString aBaseURL
= LanguageToolCfg::BaseURL::get().value_or("");
75 if (aBaseURL
.isEmpty())
76 m_xBaseURLED
->set_text(LANGUAGETOOL_DEFAULT_URL
);
78 m_xBaseURLED
->set_text(aBaseURL
);
80 m_xUsernameED
->set_text(LanguageToolCfg::Username::get().value_or(""));
81 m_xApiKeyED
->set_text(LanguageToolCfg::ApiKey::get().value_or(""));
82 m_xRestProtocol
->set_text(LanguageToolCfg::RestProtocol::get().value_or(""));
83 m_xSSLDisableVerificationBox
->set_active(!LanguageToolCfg::SSLCertVerify::get());
86 bool OptLanguageToolTabPage::FillItemSet(SfxItemSet
*)
88 auto batch(comphelper::ConfigurationChanges::create());
90 // tdf#150494 If no URL has been set, then save the default URL
91 OUString aBaseURL
= m_xBaseURLED
->get_text();
92 if (aBaseURL
.isEmpty())
93 LanguageToolCfg::BaseURL::set(LANGUAGETOOL_DEFAULT_URL
, batch
);
95 LanguageToolCfg::BaseURL::set(aBaseURL
, batch
);
97 LanguageToolCfg::Username::set(m_xUsernameED
->get_text(), batch
);
98 LanguageToolCfg::ApiKey::set(m_xApiKeyED
->get_text(), batch
);
99 LanguageToolCfg::RestProtocol::set(m_xRestProtocol
->get_text(), batch
);
100 LanguageToolCfg::SSLCertVerify::set(!m_xSSLDisableVerificationBox
->get_active(), batch
);
105 std::unique_ptr
<SfxTabPage
> OptLanguageToolTabPage::Create(weld::Container
* pPage
,
106 weld::DialogController
* pController
,
107 const SfxItemSet
* rAttrSet
)
109 return std::make_unique
<OptLanguageToolTabPage
>(pPage
, pController
, *rAttrSet
);