update
This commit is contained in:
parent
0001d3f80e
commit
4c53895fdd
|
@ -26,13 +26,15 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
youdaotrans.cpp \
|
youdaotrans.cpp \
|
||||||
transdialog.cpp \
|
transdialog.cpp \
|
||||||
settingdialog.cpp
|
settingdialog.cpp \
|
||||||
|
youdaoservice.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
youdaotrans.h \
|
youdaotrans.h \
|
||||||
../WingTool/plugin/iwingtoolplg.h \
|
../WingTool/plugin/iwingtoolplg.h \
|
||||||
transdialog.h \
|
transdialog.h \
|
||||||
settingdialog.h
|
settingdialog.h \
|
||||||
|
youdaoservice.h
|
||||||
DISTFILES += YoudaoTrans.json
|
DISTFILES += YoudaoTrans.json
|
||||||
|
|
||||||
RESOURCES += \
|
RESOURCES += \
|
||||||
|
|
BIN
lang/zh.qm
BIN
lang/zh.qm
Binary file not shown.
43
lang/zh.ts
43
lang/zh.ts
|
@ -25,38 +25,55 @@
|
||||||
</message>
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
<name>YoudaoTrans</name>
|
<name>YoudaoService</name>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../youdaotrans.cpp" line="62"/>
|
<location filename="../youdaoservice.cpp" line="17"/>
|
||||||
<source>Request Youdao API Error</source>
|
<source>Request Youdao API Error</source>
|
||||||
<translation>访问有道翻译 API 失败!</translation>
|
<translation>访问有道翻译 API 失败!</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../youdaotrans.cpp" line="65"/>
|
<location filename="../youdaoservice.cpp" line="20"/>
|
||||||
<location filename="../youdaotrans.cpp" line="75"/>
|
<location filename="../youdaoservice.cpp" line="43"/>
|
||||||
<location filename="../youdaotrans.cpp" line="110"/>
|
|
||||||
<source>YoudaoTrans</source>
|
<source>YoudaoTrans</source>
|
||||||
<translation>有道翻译</translation>
|
<translation>有道翻译</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../youdaotrans.cpp" line="88"/>
|
<location filename="../youdaoservice.cpp" line="39"/>
|
||||||
<source>A useful En2Zh translate plugin for WingTool</source>
|
|
||||||
<translation>一个用于羽云工具箱的方便易用的英到汉翻译插件</translation>
|
|
||||||
</message>
|
|
||||||
<message>
|
|
||||||
<location filename="../youdaotrans.cpp" line="106"/>
|
|
||||||
<source>Enable</source>
|
<source>Enable</source>
|
||||||
<translation>启用</translation>
|
<translation>启用</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../youdaotrans.cpp" line="106"/>
|
<location filename="../youdaoservice.cpp" line="39"/>
|
||||||
<source>Disalbe</source>
|
<source>Disalbe</source>
|
||||||
<translation>禁用</translation>
|
<translation>禁用</translation>
|
||||||
</message>
|
</message>
|
||||||
<message>
|
<message>
|
||||||
<location filename="../youdaotrans.cpp" line="107"/>
|
<location filename="../youdaoservice.cpp" line="40"/>
|
||||||
<source>Success</source>
|
<source>Success</source>
|
||||||
<translation>有道翻译成功</translation>
|
<translation>有道翻译成功</translation>
|
||||||
</message>
|
</message>
|
||||||
|
<message>
|
||||||
|
<location filename="../youdaoservice.h" line="26"/>
|
||||||
|
<source>translate</source>
|
||||||
|
<translation>翻译</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<location filename="../youdaoservice.h" line="27"/>
|
||||||
|
<source>toggle</source>
|
||||||
|
<translation>开/关</translation>
|
||||||
|
</message>
|
||||||
|
</context>
|
||||||
|
<context>
|
||||||
|
<name>YoudaoTrans</name>
|
||||||
|
<message>
|
||||||
|
<location filename="../youdaotrans.cpp" line="50"/>
|
||||||
|
<source>YoudaoTrans</source>
|
||||||
|
<translation>有道翻译</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<location filename="../youdaotrans.cpp" line="61"/>
|
||||||
|
<source>A useful En2Zh translate plugin for WingTool</source>
|
||||||
|
<translation>一个用于羽云工具箱的方便易用的英到汉翻译插件</translation>
|
||||||
|
</message>
|
||||||
</context>
|
</context>
|
||||||
</TS>
|
</TS>
|
||||||
|
|
|
@ -49,6 +49,11 @@ void TransDialog::popup() {
|
||||||
raise();
|
raise();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TransDialog::closeEvent(QCloseEvent *event) {
|
||||||
|
Q_UNUSED(event);
|
||||||
|
hide();
|
||||||
|
}
|
||||||
|
|
||||||
void TransDialog::leaveEvent(QEvent *e) {
|
void TransDialog::leaveEvent(QEvent *e) {
|
||||||
Q_UNUSED(e);
|
Q_UNUSED(e);
|
||||||
hide();
|
hide();
|
||||||
|
|
|
@ -19,6 +19,7 @@ public:
|
||||||
void popup();
|
void popup();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void closeEvent(QCloseEvent *event) override;
|
||||||
void leaveEvent(QEvent *e) override;
|
void leaveEvent(QEvent *e) override;
|
||||||
void focusOutEvent(QFocusEvent *event) override;
|
void focusOutEvent(QFocusEvent *event) override;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,184 @@
|
||||||
|
#include "youdaoservice.h"
|
||||||
|
#include <DNotifySender>
|
||||||
|
|
||||||
|
DCORE_USE_NAMESPACE
|
||||||
|
|
||||||
|
YoudaoService::YoudaoService(QObject *parent) : QObject(parent) {
|
||||||
|
loadSettings();
|
||||||
|
|
||||||
|
dialog = new TransDialog;
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
|
||||||
|
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; });
|
||||||
|
}
|
||||||
|
|
||||||
|
YoudaoService::~YoudaoService() { dialog->deleteLater(); }
|
||||||
|
|
||||||
|
bool YoudaoService::enabled() const { return m_enabled; }
|
||||||
|
|
||||||
|
void YoudaoService::setEnabled(bool enabled) {
|
||||||
|
m_enabled = enabled;
|
||||||
|
sm->setYoudaoEnabled(enabled);
|
||||||
|
DUtil::DNotifySender sender((enabled ? tr("Enable") : tr("Disalbe")) +
|
||||||
|
tr("Success"));
|
||||||
|
sender.appIcon(m_enabled ? "dialog-ok" : "dialog-warning");
|
||||||
|
sender.timeOut(1000);
|
||||||
|
sender.appName(tr("YoudaoTrans"));
|
||||||
|
sender.call();
|
||||||
|
}
|
||||||
|
|
||||||
|
void YoudaoService::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 YoudaoService::toggle() { setEnabled(!enabled()); }
|
||||||
|
|
||||||
|
void YoudaoService::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 YoudaoService::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 YoudaoService::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 YoudaoService::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 YoudaoService::showSettings() { sm->exec(); }
|
||||||
|
|
||||||
|
bool YoudaoService::sel() const { return m_sel; }
|
||||||
|
|
||||||
|
void YoudaoService::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 YoudaoService::saveSettings() {
|
||||||
|
QSettings settings(QApplication::organizationName(), "YoudaoTrans");
|
||||||
|
settings.setValue("enablecp", m_cp);
|
||||||
|
settings.setValue("enablesel", m_sel);
|
||||||
|
settings.setValue("enabled", m_sel);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool YoudaoService::cp() const { return m_cp; }
|
|
@ -0,0 +1,51 @@
|
||||||
|
#ifndef YOUDAOSERVICE_H
|
||||||
|
#define YOUDAOSERVICE_H
|
||||||
|
|
||||||
|
#include "../WingTool/plugin/iwingtoolplg.h"
|
||||||
|
#include "settingdialog.h"
|
||||||
|
#include "transdialog.h"
|
||||||
|
#include <QDomDocument>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QtNetwork/QNetworkAccessManager>
|
||||||
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
|
||||||
|
class YoudaoService : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
YoudaoService(QObject *parent = nullptr);
|
||||||
|
virtual ~YoudaoService();
|
||||||
|
|
||||||
|
bool enabled() const;
|
||||||
|
void setEnabled(bool enabled);
|
||||||
|
|
||||||
|
bool cp() const;
|
||||||
|
bool sel() const;
|
||||||
|
|
||||||
|
// 用于本地化
|
||||||
|
void translation() {
|
||||||
|
tr("translate");
|
||||||
|
tr("toggle");
|
||||||
|
}
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
PLUGINSRV void translate(QString word);
|
||||||
|
PLUGINSRV void toggle();
|
||||||
|
|
||||||
|
void parse(QByteArray &content);
|
||||||
|
QString parseCustomTrans(QDomElement &ele);
|
||||||
|
QString parseYoudaoTrans(QDomElement &ele);
|
||||||
|
|
||||||
|
bool isVaildWord(QString &word);
|
||||||
|
void showSettings();
|
||||||
|
void loadSettings();
|
||||||
|
void saveSettings();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_enabled;
|
||||||
|
bool m_cp, m_sel;
|
||||||
|
QNetworkAccessManager *net;
|
||||||
|
SettingDialog *sm;
|
||||||
|
TransDialog *dialog;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // YOUDAOSERVICE_H
|
223
youdaotrans.cpp
223
youdaotrans.cpp
|
@ -1,82 +1,59 @@
|
||||||
#include "youdaotrans.h"
|
#include "youdaotrans.h"
|
||||||
#include <DNotifySender>
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QClipboard>
|
#include <QClipboard>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QtNetwork/QNetworkReply>
|
|
||||||
|
|
||||||
DCORE_USE_NAMESPACE
|
YoudaoTrans::YoudaoTrans(QObject *parent) {
|
||||||
|
Q_UNUSED(parent);
|
||||||
|
|
||||||
YoudaoTrans::YoudaoTrans(QObject *parent)
|
auto s = GETPLUGINQM("YoudaoTrans.qm");
|
||||||
: m_cp(true), m_sel(true), m_enabled(true) {
|
if (!translator.load(s) || !QApplication::installTranslator(&translator)) {
|
||||||
Q_UNUSED(parent)
|
QMessageBox::critical(nullptr, "Error", "Error Loading File!",
|
||||||
|
QMessageBox::Ok);
|
||||||
|
localiztion = false; // 构造函数不具有返回值,只能先存着标识
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
trans = new YoudaoService(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
int YoudaoTrans::sdkVersion() { return SDKVERSION; }
|
int YoudaoTrans::sdkVersion() { return SDKVERSION; }
|
||||||
|
|
||||||
QString YoudaoTrans::signature() { return WINGSUMMER; }
|
QString YoudaoTrans::signature() { return WINGSUMMER; }
|
||||||
|
|
||||||
YoudaoTrans::~YoudaoTrans() { dialog->deleteLater(); }
|
YoudaoTrans::~YoudaoTrans() {}
|
||||||
|
|
||||||
bool YoudaoTrans::init(QList<WingPluginInfo> loadedplugin) {
|
bool YoudaoTrans::init(QList<WingPluginInfo> loadedplugin) {
|
||||||
Q_UNUSED(loadedplugin);
|
Q_UNUSED(loadedplugin);
|
||||||
|
|
||||||
auto s = GETPLUGINQM("YoudaoTrans.qm");
|
if (!localiztion) // 加载语言本地化失败,报告失败
|
||||||
if (!translator.load(s) || !QApplication::installTranslator(&translator)) {
|
|
||||||
QMessageBox::critical(nullptr, "Error", "Error Loading File!",
|
|
||||||
QMessageBox::Ok);
|
|
||||||
return false;
|
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();
|
auto clipboard = qApp->clipboard();
|
||||||
connect(clipboard, &QClipboard::selectionChanged, this, [=] {
|
connect(clipboard, &QClipboard::selectionChanged, this, [=] {
|
||||||
if (m_sel) {
|
if (trans->sel()) {
|
||||||
auto word = clipboard->text(QClipboard::Selection);
|
auto word = clipboard->text(QClipboard::Selection);
|
||||||
if (this->isVaildWord(word))
|
if (trans->isVaildWord(word))
|
||||||
this->translate(word);
|
trans->translate(word);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
connect(clipboard, &QClipboard::dataChanged, this, [=] {
|
connect(clipboard, &QClipboard::dataChanged, this, [=] {
|
||||||
if (m_cp) {
|
if (trans->cp()) {
|
||||||
auto word = clipboard->text();
|
auto word = clipboard->text();
|
||||||
if (this->isVaildWord(word))
|
if (trans->isVaildWord(word))
|
||||||
this->translate(word);
|
trans->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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void YoudaoTrans::unload() { saveSettings(); }
|
void YoudaoTrans::unload() { trans->saveSettings(); }
|
||||||
|
|
||||||
QString YoudaoTrans::pluginName() { return tr("YoudaoTrans"); }
|
QString YoudaoTrans::pluginName() { return tr("YoudaoTrans"); }
|
||||||
|
|
||||||
QString YoudaoTrans::provider() { return "youdaotr"; }
|
|
||||||
|
|
||||||
QString YoudaoTrans::pluginAuthor() { return WINGSUMMER; }
|
QString YoudaoTrans::pluginAuthor() { return WINGSUMMER; }
|
||||||
|
|
||||||
IWingToolPlg::Catagorys YoudaoTrans::pluginCatagory() {
|
IWingToolPlg::Catagorys YoudaoTrans::pluginCatagory() {
|
||||||
|
@ -91,165 +68,19 @@ QString YoudaoTrans::pluginComment() {
|
||||||
|
|
||||||
QIcon YoudaoTrans::pluginIcon() { return QIcon(":/youdaotr/icon.png"); }
|
QIcon YoudaoTrans::pluginIcon() { return QIcon(":/youdaotr/icon.png"); }
|
||||||
|
|
||||||
QStringList YoudaoTrans::pluginServices() { return {"Translate", "Toggle"}; }
|
const QMetaObject *YoudaoTrans::serviceMeta() { return trans->metaObject(); }
|
||||||
|
|
||||||
QStringList YoudaoTrans::pluginServiceNames() { return {"翻译", "开/关"}; }
|
const QPointer<QObject> YoudaoTrans::serviceHandler() {
|
||||||
|
return QPointer<QObject>(trans);
|
||||||
|
}
|
||||||
|
|
||||||
QVariant YoudaoTrans::pluginServicePipe(int serviceID, QList<QVariant> params) {
|
QVariant YoudaoTrans::pluginServicePipe(int serviceID, QList<QVariant> params) {
|
||||||
switch (serviceID) {
|
Q_UNUSED(serviceID);
|
||||||
case 0: {
|
Q_UNUSED(params);
|
||||||
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();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
void YoudaoTrans::onSetting() { sm->exec(); }
|
void YoudaoTrans::onSetting() { trans->showSettings(); }
|
||||||
|
|
||||||
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
|
#if QT_VERSION < 0x050000
|
||||||
Q_EXPORT_PLUGIN2(YoudaoTrans, GenericPlugin)
|
Q_EXPORT_PLUGIN2(YoudaoTrans, GenericPlugin)
|
||||||
|
|
|
@ -3,11 +3,9 @@
|
||||||
|
|
||||||
#include "../WingTool/plugin/iwingtoolplg.h"
|
#include "../WingTool/plugin/iwingtoolplg.h"
|
||||||
#include "settingdialog.h"
|
#include "settingdialog.h"
|
||||||
#include "transdialog.h"
|
#include "youdaoservice.h"
|
||||||
#include <QDomDocument>
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
#include <QtNetwork/QNetworkAccessManager>
|
|
||||||
|
|
||||||
class YoudaoTrans : public IWingToolPlg {
|
class YoudaoTrans : public IWingToolPlg {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -27,38 +25,23 @@ public:
|
||||||
bool init(QList<WingPluginInfo> loadedplugin) override;
|
bool init(QList<WingPluginInfo> loadedplugin) override;
|
||||||
void unload() override;
|
void unload() override;
|
||||||
QString pluginName() override;
|
QString pluginName() override;
|
||||||
QString provider() override;
|
|
||||||
QString pluginAuthor() override;
|
QString pluginAuthor() override;
|
||||||
Catagorys pluginCatagory() override;
|
Catagorys pluginCatagory() override;
|
||||||
uint pluginVersion() override;
|
uint pluginVersion() override;
|
||||||
QString pluginComment() override;
|
QString pluginComment() override;
|
||||||
QIcon pluginIcon() override;
|
QIcon pluginIcon() override;
|
||||||
QStringList pluginServices() override;
|
const QMetaObject *serviceMeta() override;
|
||||||
QStringList pluginServiceNames() override;
|
const QPointer<QObject> serviceHandler() override;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
QVariant pluginServicePipe(int serviceID, QList<QVariant> params) override;
|
QVariant pluginServicePipe(int serviceID, QList<QVariant> params) override;
|
||||||
virtual void onSetting() override;
|
virtual void onSetting() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void translate(QString word);
|
|
||||||
void parse(QByteArray &content);
|
|
||||||
QString parseCustomTrans(QDomElement &ele);
|
|
||||||
QString parseYoudaoTrans(QDomElement &ele);
|
|
||||||
|
|
||||||
bool isVaildWord(QString &word);
|
|
||||||
|
|
||||||
void loadSettings();
|
|
||||||
void saveSettings();
|
|
||||||
|
|
||||||
private:
|
|
||||||
TransDialog *dialog;
|
|
||||||
SettingDialog *sm;
|
|
||||||
QTranslator translator;
|
QTranslator translator;
|
||||||
QNetworkAccessManager *net;
|
YoudaoService *trans;
|
||||||
|
|
||||||
bool m_cp, m_sel;
|
bool localiztion = true;
|
||||||
bool m_enabled;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GENERICPLUGIN_H
|
#endif // GENERICPLUGIN_H
|
||||||
|
|
Loading…
Reference in New Issue