257 lines
7.2 KiB
C++
257 lines
7.2 KiB
C++
#include "youdaotrans.h"
|
|
#include <DNotifySender>
|
|
#include <QApplication>
|
|
#include <QClipboard>
|
|
#include <QMessageBox>
|
|
#include <QSettings>
|
|
#include <QtNetwork/QNetworkReply>
|
|
|
|
DCORE_USE_NAMESPACE
|
|
|
|
YoudaoTrans::YoudaoTrans(QObject *parent)
|
|
: m_cp(true), m_sel(true), m_enabled(true) {
|
|
Q_UNUSED(parent)
|
|
}
|
|
|
|
int YoudaoTrans::sdkVersion() { return SDKVERSION; }
|
|
|
|
QString YoudaoTrans::signature() { return WINGSUMMER; }
|
|
|
|
YoudaoTrans::~YoudaoTrans() { dialog->deleteLater(); }
|
|
|
|
bool YoudaoTrans::init(QList<WingPluginInfo> loadedplugin) {
|
|
Q_UNUSED(loadedplugin);
|
|
|
|
auto s = GETPLUGINQM("YoudaoTrans.qm");
|
|
if (!translator.load(s) || !QApplication::installTranslator(&translator)) {
|
|
QMessageBox::critical(nullptr, "Error", "Error Loading File!",
|
|
QMessageBox::Ok);
|
|
return false;
|
|
}
|
|
|
|
loadSettings();
|
|
dialog = new TransDialog;
|
|
sm = new SettingDialog(m_sel, m_cp);
|
|
sm->setYoudaoEnabled(m_enabled);
|
|
connect(sm, &SettingDialog::sigCpEnabled, this, [=](bool v) { m_cp = v; });
|
|
connect(sm, &SettingDialog::sigSelEnabled, this, [=](bool v) { m_sel = v; });
|
|
|
|
// 初始化剪切板监控
|
|
auto clipboard = qApp->clipboard();
|
|
connect(clipboard, &QClipboard::selectionChanged, this, [=] {
|
|
if (m_sel) {
|
|
auto word = clipboard->text(QClipboard::Selection);
|
|
if (this->isVaildWord(word))
|
|
this->translate(word);
|
|
}
|
|
});
|
|
connect(clipboard, &QClipboard::dataChanged, this, [=] {
|
|
if (m_cp) {
|
|
auto word = clipboard->text();
|
|
if (this->isVaildWord(word))
|
|
this->translate(word);
|
|
}
|
|
});
|
|
|
|
net = new QNetworkAccessManager(this);
|
|
connect(net, &QNetworkAccessManager::finished, this,
|
|
[=](QNetworkReply *reply) {
|
|
if (reply && reply->error() == QNetworkReply::NoError) {
|
|
QByteArray data = reply->readAll();
|
|
this->parse(data);
|
|
} else {
|
|
DUtil::DNotifySender sender(tr("Request Youdao API Error"));
|
|
sender.appIcon("dialog-error");
|
|
sender.timeOut(1000);
|
|
sender.appName(tr("YoudaoTrans"));
|
|
sender.call();
|
|
}
|
|
reply->close();
|
|
});
|
|
return true;
|
|
}
|
|
|
|
void YoudaoTrans::unload() { saveSettings(); }
|
|
|
|
QString YoudaoTrans::pluginName() { return tr("YoudaoTrans"); }
|
|
|
|
QString YoudaoTrans::provider() { return "youdaotr"; }
|
|
|
|
QString YoudaoTrans::pluginAuthor() { return WINGSUMMER; }
|
|
|
|
IWingToolPlg::Catagorys YoudaoTrans::pluginCatagory() {
|
|
return IWingToolPlg::Catagorys::Productivity;
|
|
}
|
|
|
|
uint YoudaoTrans::pluginVersion() { return 1; }
|
|
|
|
QString YoudaoTrans::pluginComment() {
|
|
return tr("A useful En2Zh translate plugin for WingTool");
|
|
}
|
|
|
|
QIcon YoudaoTrans::pluginIcon() { return QIcon(":/youdaotr/favicon.ico"); }
|
|
|
|
QStringList YoudaoTrans::pluginServices() { return {"Translate", "Toggle"}; }
|
|
|
|
QStringList YoudaoTrans::pluginServiceNames() { return {"翻译", "开/关"}; }
|
|
|
|
QVariant YoudaoTrans::pluginServicePipe(int serviceID, QList<QVariant> params) {
|
|
switch (serviceID) {
|
|
case 0: {
|
|
auto content = params.first();
|
|
if (content.canConvert(QMetaType::QString))
|
|
translate(content.toString());
|
|
} break;
|
|
case 1: {
|
|
m_enabled = !m_enabled;
|
|
sm->setYoudaoEnabled(m_enabled);
|
|
DUtil::DNotifySender sender((m_enabled ? tr("Enable") : tr("Disalbe")) +
|
|
tr("Success"));
|
|
sender.appIcon(m_enabled ? "dialog-ok" : "dialog-warning");
|
|
sender.timeOut(1000);
|
|
sender.appName(tr("YoudaoTrans"));
|
|
sender.call();
|
|
} break;
|
|
default:
|
|
break;
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
void YoudaoTrans::onSetting() { sm->exec(); }
|
|
|
|
void YoudaoTrans::translate(QString word) {
|
|
if (m_enabled && isVaildWord(word))
|
|
net->get(QNetworkRequest(
|
|
QUrl("http://dict.youdao.com/fsearch?client=deskdict&q=" +
|
|
QUrl::toPercentEncoding(word) +
|
|
"&pos=-1&doctype=xml&xmlVersion=3.2&dogVersion=1.0&"
|
|
"vendor=unknown&appVer=3.1.17.4208&le=eng")));
|
|
}
|
|
|
|
void YoudaoTrans::parse(QByteArray &content) {
|
|
QDomDocument dom;
|
|
QString errstr;
|
|
int row, col;
|
|
if (!dom.setContent(content, false, &errstr, &row, &col)) {
|
|
return;
|
|
}
|
|
|
|
auto doc = dom.documentElement();
|
|
if (doc.tagName() != "yodaodict")
|
|
return;
|
|
|
|
auto nodeList = doc.childNodes();
|
|
auto len = nodeList.size();
|
|
|
|
if (len) {
|
|
QString word, ukphonetic, usphonetic, trans, youtrans;
|
|
|
|
for (auto i = 0; i < len; i++) {
|
|
auto node = nodeList.at(i).toElement();
|
|
auto name = node.tagName();
|
|
|
|
if (name == "return-phrase") {
|
|
word = node.text();
|
|
} else if (name == "uk-phonetic-symbol") {
|
|
ukphonetic = node.text();
|
|
} else if (name == "us-phonetic-symbol") {
|
|
usphonetic = node.text();
|
|
} else if (name == "custom-translation") {
|
|
trans = parseCustomTrans(node);
|
|
} else if (name == "yodao-web-dict") {
|
|
youtrans = parseYoudaoTrans(node);
|
|
}
|
|
}
|
|
|
|
QString content;
|
|
if (trans.length()) {
|
|
content = trans;
|
|
if (youtrans.length()) {
|
|
content += "\n\n---\n\n" + youtrans;
|
|
}
|
|
} else {
|
|
content = youtrans;
|
|
}
|
|
|
|
dialog->setInfo(word, ukphonetic, usphonetic, content);
|
|
dialog->popup();
|
|
}
|
|
}
|
|
|
|
QString YoudaoTrans::parseCustomTrans(QDomElement &ele) {
|
|
auto nodeList = ele.childNodes();
|
|
auto len = nodeList.size();
|
|
QStringList content;
|
|
if (len) {
|
|
for (auto i = 0; i < len; i++) {
|
|
auto node = nodeList.at(i).toElement();
|
|
auto name = node.tagName();
|
|
if (name == "translation") {
|
|
content << node.firstChild().toElement().text();
|
|
}
|
|
}
|
|
return content.join("\n\n");
|
|
} else {
|
|
return QString();
|
|
}
|
|
}
|
|
|
|
QString YoudaoTrans::parseYoudaoTrans(QDomElement &ele) {
|
|
auto nodeList = ele.childNodes();
|
|
auto len = nodeList.size();
|
|
QStringList content;
|
|
if (len) {
|
|
for (auto i = 0; i < len; i++) {
|
|
QString key;
|
|
QStringList trans;
|
|
auto node = nodeList.at(i).toElement();
|
|
auto nodes = node.childNodes();
|
|
auto count = nodes.size();
|
|
for (auto y = 0; y < count; y++) {
|
|
auto inode = nodes.at(y).toElement();
|
|
auto name = inode.tagName();
|
|
if (name == "trans") {
|
|
trans << inode.firstChild().toElement().text();
|
|
} else if (name == "key") {
|
|
key = inode.text();
|
|
}
|
|
}
|
|
content << "### " + key + "\n\n* " + trans.join("\n\n* ");
|
|
}
|
|
return content.join("\n\n");
|
|
} else {
|
|
return QString();
|
|
}
|
|
}
|
|
|
|
bool YoudaoTrans::isVaildWord(QString &word) {
|
|
word = word.trimmed();
|
|
if (word.isEmpty())
|
|
return false;
|
|
for (auto &c : word) {
|
|
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' ||
|
|
c == ' '))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void YoudaoTrans::loadSettings() {
|
|
QSettings settings(QApplication::organizationName(), "YoudaoTrans");
|
|
m_cp = settings.value("enablecp", true).toBool();
|
|
m_sel = settings.value("enablesel", true).toBool();
|
|
m_enabled = settings.value("enabled", true).toBool();
|
|
}
|
|
|
|
void YoudaoTrans::saveSettings() {
|
|
QSettings settings(QApplication::organizationName(), "YoudaoTrans");
|
|
settings.setValue("enablecp", m_cp);
|
|
settings.setValue("enablesel", m_sel);
|
|
settings.setValue("enabled", m_sel);
|
|
}
|
|
|
|
#if QT_VERSION < 0x050000
|
|
Q_EXPORT_PLUGIN2(YoudaoTrans, GenericPlugin)
|
|
#endif // QT_VERSION < 0x050000
|