Show translator script errors
This commit is contained in:
parent
835714d76d
commit
9da8c516b6
@ -22,6 +22,7 @@ public:
|
||||
QStringList translators;
|
||||
|
||||
QString error;
|
||||
QStringList translatorErrors;
|
||||
};
|
||||
|
||||
using TaskPtr = std::shared_ptr<Task>;
|
||||
|
@ -210,7 +210,7 @@ void Translator::processQueue()
|
||||
std::unordered_set<Task *> busyTasks;
|
||||
|
||||
for (auto &i : pages_) {
|
||||
if (i.second->isBusy())
|
||||
if (i.second->checkBusy())
|
||||
busyTasks.insert(i.second->task().get());
|
||||
else
|
||||
idlePages.insert(i.first);
|
||||
@ -228,7 +228,8 @@ void Translator::processQueue()
|
||||
continue;
|
||||
|
||||
if (task->translators.isEmpty()) {
|
||||
task->error = tr("All translators failed");
|
||||
task->error = tr("All translators failed (%1)")
|
||||
.arg(task->translatorErrors.join(", "));
|
||||
finishedTasks.push_back(task);
|
||||
continue;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ WebPage::WebPage(Translator &translator, const QString &script,
|
||||
const QString &scriptName)
|
||||
: QWebEnginePage(new QWebEngineProfile)
|
||||
, translator_(translator)
|
||||
, scriptName_(scriptName)
|
||||
, proxy_(new WebPageProxy(*this))
|
||||
{
|
||||
profile()->setParent(this);
|
||||
@ -22,7 +23,7 @@ WebPage::WebPage(Translator &translator, const QString &script,
|
||||
&WebPage::authenticateProxy);
|
||||
|
||||
scheduleWebchannelInitScript();
|
||||
scheduleTranslatorScript(script, scriptName);
|
||||
scheduleTranslatorScript(script);
|
||||
|
||||
setLoadImages(false);
|
||||
|
||||
@ -62,13 +63,12 @@ if (typeof init === "function") init ();
|
||||
profile()->scripts()->insert(js);
|
||||
}
|
||||
|
||||
void WebPage::scheduleTranslatorScript(const QString &script,
|
||||
const QString &scriptName)
|
||||
void WebPage::scheduleTranslatorScript(const QString &script)
|
||||
{
|
||||
QWebEngineScript js;
|
||||
|
||||
js.setSourceCode(script);
|
||||
js.setName(scriptName);
|
||||
js.setName(scriptName_);
|
||||
js.setWorldId(QWebEngineScript::UserWorld);
|
||||
js.setInjectionPoint(QWebEngineScript::Deferred);
|
||||
js.setRunsOnSubFrames(false);
|
||||
@ -77,6 +77,13 @@ void WebPage::scheduleTranslatorScript(const QString &script,
|
||||
profile()->scripts()->insert(js);
|
||||
}
|
||||
|
||||
void WebPage::addErrorToTask(const QString &text) const
|
||||
{
|
||||
if (!task_)
|
||||
return;
|
||||
task_->translatorErrors.append(QString("%1: %2").arg(scriptName_, text));
|
||||
}
|
||||
|
||||
void WebPage::setIgnoreSslErrors(bool ignoreSslErrors)
|
||||
{
|
||||
ignoreSslErrors_ = ignoreSslErrors;
|
||||
@ -105,14 +112,23 @@ void WebPage::start(const TaskPtr &task)
|
||||
proxy_->translate(task->corrected, task->sourceLanguage, langCodes->iso639_1);
|
||||
}
|
||||
|
||||
bool WebPage::isBusy() const
|
||||
bool WebPage::checkBusy()
|
||||
{
|
||||
return task_ && isBusy_ && QDateTime::currentDateTime() < nextIdleTime_;
|
||||
if (!task_ || !isBusy_)
|
||||
return false;
|
||||
|
||||
if (QDateTime::currentDateTime() < nextIdleTime_)
|
||||
return true;
|
||||
|
||||
addErrorToTask(tr("timed out"));
|
||||
isBusy_ = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void WebPage::setTranslated(const QString &text)
|
||||
{
|
||||
if (!isBusy())
|
||||
if (!checkBusy())
|
||||
return;
|
||||
|
||||
isBusy_ = false;
|
||||
@ -124,14 +140,12 @@ void WebPage::setTranslated(const QString &text)
|
||||
|
||||
void WebPage::setFailed(const QString &error)
|
||||
{
|
||||
if (!isBusy())
|
||||
if (!checkBusy())
|
||||
return;
|
||||
|
||||
isBusy_ = false;
|
||||
|
||||
SOFT_ASSERT(task_, return )
|
||||
// task_->error = error;
|
||||
translator_.finish(task_);
|
||||
addErrorToTask(error);
|
||||
}
|
||||
|
||||
TaskPtr WebPage::task() const
|
||||
|
@ -21,7 +21,7 @@ public:
|
||||
void start(const TaskPtr &task);
|
||||
void setTranslated(const QString &text);
|
||||
void setFailed(const QString &error);
|
||||
bool isBusy() const;
|
||||
bool checkBusy();
|
||||
TaskPtr task() const;
|
||||
|
||||
bool isLoadImages() const;
|
||||
@ -40,10 +40,11 @@ private:
|
||||
void authenticateProxy(const QUrl &requestUrl, QAuthenticator *authenticator,
|
||||
const QString &proxyHost);
|
||||
void scheduleWebchannelInitScript();
|
||||
void scheduleTranslatorScript(const QString &script,
|
||||
const QString &scriptName);
|
||||
void scheduleTranslatorScript(const QString &script);
|
||||
void addErrorToTask(const QString &text) const;
|
||||
|
||||
Translator &translator_;
|
||||
QString scriptName_;
|
||||
std::unique_ptr<WebPageProxy> proxy_;
|
||||
TaskPtr task_;
|
||||
bool ignoreSslErrors_{false};
|
||||
|
Loading…
Reference in New Issue
Block a user