Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / shells / translatehelper.cxx
blob507c2812a6759468130a1274b99c06df7dce5800
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #include <config_wasm_strip.h>
20 #include <wrtsh.hxx>
21 #include <pam.hxx>
22 #include <node.hxx>
23 #include <ndtxt.hxx>
24 #include <translatehelper.hxx>
25 #include <sal/log.hxx>
26 #include <rtl/string.h>
27 #include <shellio.hxx>
28 #include <vcl/scheduler.hxx>
29 #include <vcl/svapp.hxx>
30 #include <boost/property_tree/ptree.hpp>
31 #include <boost/property_tree/json_parser.hpp>
32 #include <vcl/htmltransferable.hxx>
33 #include <vcl/transfer.hxx>
34 #include <swdtflvr.hxx>
35 #include <linguistic/translate.hxx>
36 #include <com/sun/star/task/XStatusIndicator.hpp>
37 #include <sfx2/viewfrm.hxx>
38 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
39 #include <strings.hrc>
41 namespace SwTranslateHelper
43 OString ExportPaMToHTML(SwPaM* pCursor, bool bReplacePTag)
45 SolarMutexGuard gMutex;
46 OString aResult;
47 WriterRef xWrt;
48 GetHTMLWriter(u"NoLineLimit,SkipHeaderFooter,NoPrettyPrint", OUString(), xWrt);
49 if (pCursor != nullptr)
51 SvMemoryStream aMemoryStream;
52 SwWriter aWriter(aMemoryStream, *pCursor);
53 ErrCode nError = aWriter.Write(xWrt);
54 if (nError.IsError())
56 SAL_WARN("sw.ui", "ExportPaMToHTML: failed to export selection to HTML");
57 return {};
59 aResult
60 = OString(static_cast<const char*>(aMemoryStream.GetData()), aMemoryStream.GetSize());
61 if (bReplacePTag)
63 aResult = aResult.replaceAll("<p", "<span");
64 aResult = aResult.replaceAll("</p>", "</span>");
67 // HTML has for that <br> and <p> also does new line
68 aResult = aResult.replaceAll("<ul>", "");
69 aResult = aResult.replaceAll("</ul>", "");
70 aResult = aResult.replaceAll("<ol>", "");
71 aResult = aResult.replaceAll("</ol>", "");
72 aResult = aResult.replaceAll("\n", "").trim();
73 return aResult;
75 return {};
78 void PasteHTMLToPaM(SwWrtShell& rWrtSh, SwPaM* pCursor, const OString& rData, bool bSetSelection)
80 SolarMutexGuard gMutex;
81 rtl::Reference<vcl::unohelper::HtmlTransferable> pHtmlTransferable
82 = new vcl::unohelper::HtmlTransferable(rData);
83 if (pHtmlTransferable.is())
85 TransferableDataHelper aDataHelper(pHtmlTransferable);
86 if (aDataHelper.GetXTransferable().is()
87 && SwTransferable::IsPasteSpecial(rWrtSh, aDataHelper))
89 if (bSetSelection)
91 rWrtSh.SetSelection(*pCursor);
93 SwTransferable::Paste(rWrtSh, aDataHelper);
94 rWrtSh.KillSelection(nullptr, false);
99 #if !ENABLE_WASM_STRIP_EXTRA
100 void TranslateDocument(SwWrtShell& rWrtSh, const TranslateAPIConfig& rConfig)
102 bool bCancel = false;
103 TranslateDocumentCancellable(rWrtSh, rConfig, bCancel);
106 void TranslateDocumentCancellable(SwWrtShell& rWrtSh, const TranslateAPIConfig& rConfig,
107 bool& rCancelTranslation)
109 auto m_pCurrentPam = rWrtSh.GetCursor();
110 bool bHasSelection = rWrtSh.HasSelection();
112 if (bHasSelection)
114 // iteration will start top to bottom
115 if (m_pCurrentPam->GetPoint()->nNode > m_pCurrentPam->GetMark()->nNode)
116 m_pCurrentPam->Exchange();
119 auto const& pNodes = rWrtSh.GetNodes();
120 SwPosition aPoint = *m_pCurrentPam->GetPoint();
121 SwPosition aMark = *m_pCurrentPam->GetMark();
122 auto startNode = bHasSelection ? aPoint.nNode.GetIndex() : SwNodeOffset(0);
123 auto endNode = bHasSelection ? aMark.nNode.GetIndex() : pNodes.Count() - 1;
125 sal_Int32 nCount(0);
126 sal_Int32 nProgress(0);
128 for (SwNodeOffset n(startNode); n <= endNode; ++n)
130 if (pNodes[n] && pNodes[n]->IsTextNode())
132 if (pNodes[n]->GetTextNode()->GetText().isEmpty())
133 continue;
134 nCount++;
138 SfxViewFrame* pFrame = SfxViewFrame::Current();
139 uno::Reference<frame::XFrame> xFrame(pFrame ? pFrame->GetFrame().GetFrameInterface() : nullptr);
140 uno::Reference<task::XStatusIndicatorFactory> xProgressFactory(xFrame, uno::UNO_QUERY);
141 uno::Reference<task::XStatusIndicator> xStatusIndicator;
143 if (xProgressFactory.is())
145 xStatusIndicator = xProgressFactory->createStatusIndicator();
148 if (xStatusIndicator.is())
149 xStatusIndicator->start(SwResId(STR_STATSTR_SWTRANSLATE), nCount);
151 for (SwNodeOffset n(startNode); n <= endNode; ++n)
153 if (rCancelTranslation)
154 break;
156 if (n >= rWrtSh.GetNodes().Count())
157 break;
159 if (!pNodes[n])
160 break;
162 SwNode* pNode = pNodes[n];
163 if (pNode->IsTextNode())
165 if (pNode->GetTextNode()->GetText().isEmpty())
166 continue;
168 auto cursor
169 = Writer::NewUnoCursor(*rWrtSh.GetDoc(), pNode->GetIndex(), pNode->GetIndex());
171 // set edges (start, end) for nodes inside the selection.
172 if (bHasSelection)
174 if (startNode == endNode)
176 cursor->SetMark();
177 cursor->GetPoint()->nContent = aPoint.nContent;
178 cursor->GetMark()->nContent = aMark.nContent;
180 else if (n == startNode)
182 cursor->SetMark();
183 cursor->GetPoint()->nContent = std::min(aPoint.nContent, aMark.nContent);
185 else if (n == endNode)
187 cursor->SetMark();
188 cursor->GetMark()->nContent = aMark.nContent;
189 cursor->GetPoint()->nContent = 0;
193 const auto aOut = SwTranslateHelper::ExportPaMToHTML(cursor.get(), true);
194 const auto aTranslatedOut = linguistic::Translate(
195 rConfig.m_xTargetLanguage, rConfig.m_xAPIUrl, rConfig.m_xAuthKey, aOut);
196 SwTranslateHelper::PasteHTMLToPaM(rWrtSh, cursor.get(), aTranslatedOut, true);
198 if (xStatusIndicator.is())
199 xStatusIndicator->setValue((100 * ++nProgress) / nCount);
201 Idle aIdle("ProgressBar::SetValue aIdle");
202 aIdle.SetPriority(TaskPriority::POST_PAINT);
203 aIdle.Start();
205 rWrtSh.LockView(true);
206 while (aIdle.IsActive() && !Application::IsQuit())
208 Application::Yield();
210 rWrtSh.LockView(false);
214 if (xStatusIndicator.is())
215 xStatusIndicator->end();
217 #endif // !ENABLE_WASM_STRIP_EXTRA