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 <vcl/svapp.hxx>
21 #include <osl/diagnose.h>
23 #include <swtypes.hxx>
26 #include <viewopt.hxx>
27 #include <translatelangselect.hxx>
28 #include <pagedesc.hxx>
29 #include <poolfmt.hxx>
30 #include <sal/log.hxx>
32 #include <shellio.hxx>
33 #include <vcl/idle.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>
39 #include <linguistic/translate.hxx>
40 #include <officecfg/Office/Linguistic.hxx>
42 static const std::vector
<SwLanguageListItem
>& getLanguageVec()
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)")
77 int SwTranslateLangSelectDlg::selectedLangIdx
= -1;
78 SwTranslateLangSelectDlg::SwTranslateLangSelectDlg(weld::Window
* pParent
, SwWrtShell
& rSh
)
79 : GenericDialogController(pParent
, "modules/swriter/ui/translationdialog.ui",
80 "LanguageSelectDialog")
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)
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
));
92 for (const auto& item
: getLanguageVec())
94 m_xLanguageListBox
->append_text(OStringToOUString(item
.getName(), RTL_TEXTENCODING_UTF8
));
97 if (SwTranslateLangSelectDlg::selectedLangIdx
!= -1)
99 m_xLanguageListBox
->set_active(SwTranslateLangSelectDlg::selectedLangIdx
);
103 std::optional
<SwLanguageListItem
> SwTranslateLangSelectDlg::GetSelectedLanguage()
105 if (SwTranslateLangSelectDlg::selectedLangIdx
!= -1)
107 return getLanguageVec().at(SwTranslateLangSelectDlg::selectedLangIdx
);
113 IMPL_STATIC_LINK(SwTranslateLangSelectDlg
, LangSelectHdl
, weld::ComboBox
&, rBox
, void)
115 const auto selected
= rBox
.get_active();
116 SwTranslateLangSelectDlg::selectedLangIdx
= selected
;
119 IMPL_LINK_NOARG(SwTranslateLangSelectDlg
, LangSelectCancelHdl
, weld::Button
&, void)
121 // stop translation first
122 if (m_bTranslationStarted
)
123 m_bCancelTranslation
= true;
125 m_xDialog
->response(RET_CANCEL
);
128 IMPL_LINK_NOARG(SwTranslateLangSelectDlg
, LangSelectTranslateHdl
, weld::Button
&, void)
130 if (SwTranslateLangSelectDlg::selectedLangIdx
== -1)
132 m_xDialog
->response(RET_CANCEL
);
136 std::optional
<OUString
> oDeeplAPIUrl
137 = officecfg::Office::Linguistic::Translation::Deepl::ApiURL::get();
138 std::optional
<OUString
> oDeeplKey
139 = officecfg::Office::Linguistic::Translation::Deepl::AuthKey::get();
140 if (!oDeeplAPIUrl
|| oDeeplAPIUrl
->isEmpty() || !oDeeplKey
|| oDeeplKey
->isEmpty())
142 SAL_WARN("sw.ui", "SwTranslateLangSelectDlg: API options are not set");
143 m_xDialog
->response(RET_CANCEL
);
147 const OString aAPIUrl
148 = OUStringToOString(rtl::Concat2View(*oDeeplAPIUrl
+ "?tag_handling=html"),
149 RTL_TEXTENCODING_UTF8
)
151 const OString aAuthKey
= OUStringToOString(*oDeeplKey
, RTL_TEXTENCODING_UTF8
).trim();
152 const auto aTargetLang
153 = getLanguageVec().at(SwTranslateLangSelectDlg::selectedLangIdx
).getLanguage();
155 m_bTranslationStarted
= true;
157 SwTranslateHelper::TranslateAPIConfig
aConfig({ aAPIUrl
, aAuthKey
, aTargetLang
});
158 SwTranslateHelper::TranslateDocumentCancellable(m_rWrtSh
, aConfig
, m_bCancelTranslation
);
159 m_xDialog
->response(RET_OK
);
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */