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 .
19 #include <config_wasm_strip.h>
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
;
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
);
56 SAL_WARN("sw.ui", "ExportPaMToHTML: failed to export selection to HTML");
60 = OString(static_cast<const char*>(aMemoryStream
.GetData()), aMemoryStream
.GetSize());
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();
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
))
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();
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;
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())
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
)
156 if (n
>= rWrtSh
.GetNodes().Count())
162 SwNode
* pNode
= pNodes
[n
];
163 if (pNode
->IsTextNode())
165 if (pNode
->GetTextNode()->GetText().isEmpty())
169 = Writer::NewUnoCursor(*rWrtSh
.GetDoc(), pNode
->GetIndex(), pNode
->GetIndex());
171 // set edges (start, end) for nodes inside the selection.
174 if (startNode
== endNode
)
177 cursor
->GetPoint()->nContent
= aPoint
.nContent
;
178 cursor
->GetMark()->nContent
= aMark
.nContent
;
180 else if (n
== startNode
)
183 cursor
->GetPoint()->nContent
= std::min(aPoint
.nContent
, aMark
.nContent
);
185 else if (n
== endNode
)
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
);
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