LibreOffice Module sw (master) 1
translatelangselect.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <vcl/svapp.hxx>
21#include <osl/diagnose.h>
22#include <uitool.hxx>
23#include <swtypes.hxx>
24#include <wrtsh.hxx>
25#include <view.hxx>
26#include <viewopt.hxx>
28#include <pagedesc.hxx>
29#include <poolfmt.hxx>
30#include <sal/log.hxx>
31#include <ndtxt.hxx>
32#include <shellio.hxx>
33#include <vcl/idle.hxx>
34#include <mdiexp.hxx>
35#include <strings.hrc>
36#include <com/sun/star/task/XStatusIndicator.hpp>
37#include <sfx2/viewfrm.hxx>
38#include <com/sun/star/task/XStatusIndicatorFactory.hpp>
40#include <officecfg/Office/Linguistic.hxx>
41
42static const std::vector<SwLanguageListItem>& getLanguageVec()
43{
44 static const std::vector<SwLanguageListItem> gLanguageVec{
45 SwLanguageListItem("BG", "Bulgarian"),
46 SwLanguageListItem("CS", "Czech"),
47 SwLanguageListItem("DA", "Danish"),
48 SwLanguageListItem("DE", "German"),
49 SwLanguageListItem("EL", "Greek"),
50 SwLanguageListItem("EN-GB", "English (British)"),
51 SwLanguageListItem("EN-US", "English (American)"),
52 SwLanguageListItem("ES", "Spanish"),
53 SwLanguageListItem("ET", "Estonian"),
54 SwLanguageListItem("FI", "Finnish"),
55 SwLanguageListItem("FR", "French"),
56 SwLanguageListItem("HU", "Hungarian"),
57 SwLanguageListItem("ID", "Indonesian"),
58 SwLanguageListItem("IT", "Italian"),
59 SwLanguageListItem("JA", "Japanese"),
60 SwLanguageListItem("LT", "Lithuanian"),
61 SwLanguageListItem("LV", "Latvian"),
62 SwLanguageListItem("NL", "Dutch"),
63 SwLanguageListItem("PL", "Polish"),
64 SwLanguageListItem("PT-BR", "Portuguese (Brazilian)"),
65 SwLanguageListItem("PT-PT", "Portuguese (European)"),
66 SwLanguageListItem("RO", "Romanian"),
67 SwLanguageListItem("RU", "Russian"),
68 SwLanguageListItem("SK", "Slovak"),
69 SwLanguageListItem("SL", "Slovenian"),
70 SwLanguageListItem("SV", "Swedish"),
71 SwLanguageListItem("TR", "Turkish"),
72 SwLanguageListItem("ZH", "Chinese (simplified)")
73 };
74 return gLanguageVec;
75}
76
79 : GenericDialogController(pParent, "modules/swriter/ui/translationdialog.ui",
80 "LanguageSelectDialog")
81 , m_rWrtSh(rSh)
82 , m_xLanguageListBox(m_xBuilder->weld_combo_box("combobox1"))
83 , m_xBtnCancel(m_xBuilder->weld_button("cancel"))
84 , m_xBtnTranslate(m_xBuilder->weld_button("translate"))
85 , m_bTranslationStarted(false)
86 , m_bCancelTranslation(false)
87{
88 m_xLanguageListBox->connect_changed(LINK(this, SwTranslateLangSelectDlg, LangSelectHdl));
89 m_xBtnCancel->connect_clicked(LINK(this, SwTranslateLangSelectDlg, LangSelectCancelHdl));
90 m_xBtnTranslate->connect_clicked(LINK(this, SwTranslateLangSelectDlg, LangSelectTranslateHdl));
91
92 for (const auto& item : getLanguageVec())
93 {
94 m_xLanguageListBox->append_text(OStringToOUString(item.getName(), RTL_TEXTENCODING_UTF8));
95 }
96
98 {
100 }
101}
102
103std::optional<SwLanguageListItem> SwTranslateLangSelectDlg::GetSelectedLanguage()
104{
106 {
108 }
109
110 return {};
111}
112
114{
115 const auto selected = rBox.get_active();
117}
118
120{
121 // stop translation first
122 if (m_bTranslationStarted)
123 m_bCancelTranslation = true;
124 else
125 m_xDialog->response(RET_CANCEL);
126}
127
129{
131 {
132 m_xDialog->response(RET_CANCEL);
133 return;
134 }
135
136 std::optional<OUString> oDeeplAPIUrl
138 std::optional<OUString> oDeeplKey
140 if (!oDeeplAPIUrl || oDeeplAPIUrl->isEmpty() || !oDeeplKey || oDeeplKey->isEmpty())
141 {
142 SAL_WARN("sw.ui", "SwTranslateLangSelectDlg: API options are not set");
143 m_xDialog->response(RET_CANCEL);
144 return;
145 }
146
147 const OString aAPIUrl
148 = OUStringToOString(rtl::Concat2View(*oDeeplAPIUrl + "?tag_handling=html"),
149 RTL_TEXTENCODING_UTF8)
150 .trim();
151 const OString aAuthKey = OUStringToOString(*oDeeplKey, RTL_TEXTENCODING_UTF8).trim();
152 const auto aTargetLang
154
155 m_bTranslationStarted = true;
156
157 SwTranslateHelper::TranslateAPIConfig aConfig({ aAPIUrl, aAuthKey, aTargetLang });
158 SwTranslateHelper::TranslateDocumentCancellable(m_rWrtSh, aConfig, m_bCancelTranslation);
159 m_xDialog->response(RET_OK);
160}
161
162/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
std::unique_ptr< weld::ComboBox > m_xLanguageListBox
static std::optional< SwLanguageListItem > GetSelectedLanguage()
SwTranslateLangSelectDlg(weld::Window *pParent, SwWrtShell &rSh)
std::unique_ptr< weld::Button > m_xBtnTranslate
std::unique_ptr< weld::Button > m_xBtnCancel
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
#define SAL_WARN(area, stream)
SW_DLLPUBLIC void TranslateDocumentCancellable(SwWrtShell &rWrtSh, const TranslateAPIConfig &rConfig, bool &rCancelTranslation)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
static const std::vector< SwLanguageListItem > & getLanguageVec()
IMPL_LINK_NOARG(SwTranslateLangSelectDlg, LangSelectCancelHdl, weld::Button &, void)
IMPL_STATIC_LINK(SwTranslateLangSelectDlg, LangSelectHdl, weld::ComboBox &, rBox, void)
RET_OK
RET_CANCEL