-
Notifications
You must be signed in to change notification settings - Fork 15
/
gtrans.cpp
173 lines (135 loc) · 5.58 KB
/
gtrans.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// gtrans.cpp
// Copyright (c) 2010, Jeremiah LaRocco [email protected]
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "gtrans.h"
#include <QtGui>
#include <QMap>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QClipboard>
#include <unistd.h>
GTrans::GTrans() {
//lzt QTextcode modify for Utf8
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QTextCodec::setCodecForCStrings( QTextCodec::codecForLocale());
//Layout
// QLayout *mainLayout = new QVBoxLayout;
QLayout *mainLayout = new QStackedLayout;
// The input section
// QLayout *top = new QHBoxLayout;
// QBoxLayout *tl = new QVBoxLayout;
// QLabel *inLabel = new QLabel(tr("Input:"));
// mainLayout->addWidget(inLabel); //lzt add
// fromLang = new QComboBox;
// tl->addWidget(inLabel);
// tl->addSpacing(10);
// tl->addWidget(fromLang);
// tl->addStretch();
// top->addItem(tl);
// inputTxt = new QTextEdit;
// top->addWidget(inputTxt);
// mainLayout->addWidget(inputTxt); //lzt add
// The output section
// QLayout *bottom = new QHBoxLayout;
// QBoxLayout *bl = new QVBoxLayout;
// QLabel *outLabel = new QLabel(tr("Output:"));
// toLang = new QComboBox;
// bl->addWidget(outLabel);
// bl->addSpacing(10);
// bl->addWidget(toLang);
// bl->addStretch();
// bottom->addItem(bl);
// mainLayout->addWidget(outLabel); //lzt add
outputTxt = new QTextEdit;
// outputTxt->setReadOnly(true);
// bottom->addWidget(outputTxt);
mainLayout->addWidget(outputTxt); //lzt add
// mainLayout->addItem(top);
// mainLayout->addItem(bottom);
// Translate button
// trans_b = new QPushButton(tr("Translate"));
// mainLayout->addWidget(trans_b);
setLayout(mainLayout);
setWindowTitle(tr("Translate"));
// trans_b->setDefault(true);
// connect(trans_b, SIGNAL(clicked()), this, SLOT(doTrans()));
//lzt clip
QClipboard *clipboard = QApplication::clipboard();
connect(clipboard, SIGNAL(selectionChanged()), this, SLOT(doTrans()));
//
// Setup foxus and tab order.
// inputTxt->setTabChangesFocus(true);
// inputTxt->setFocus(Qt::ActiveWindowFocusReason);
// outputTxt->setFocusProxy(trans_b);
}
void GTrans::doTrans() {
nam = new QNetworkAccessManager(this);
// Figure out the URL for translate.google.com
// QStringList words = inputTxt->toPlainText().split(QRegExp("\\s+"));
//lzt clipboard
QClipboard *clipboard = QApplication::clipboard();
QStringList words = clipboard->text(QClipboard::Selection).split(QRegExp("\\s+"));
outputTxt->setPlainText(words.join(" "));
//
//QString gtUrl = tr("https://translate.google.com/translate_t?langpair=%1|%2&text=").arg(languages[fromLang->currentText()]).arg(languages[toLang->currentText()]) + words.join("+");
QString gtUrl = tr("http://dict.youdao.com/search?q=") + words.join(" ");
// outputTxt->setPlainText(gtUrl);
//lzt1
QUrl url(gtUrl);
QNetworkRequest qnr(url);
// Google checks for certain user agents, so spoof one
qnr.setRawHeader("User-Agent", "");
// Make the request.
nam->get(qnr);
// Tell it to get back to us...
connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
// Go back to the text input
// inputTxt->selectAll();
// inputTxt->setFocus();
usleep(500000);
// outputTxt->selectAll();
outputTxt->setFocus();
}
void GTrans::finishedSlot(QNetworkReply* reply) {
// The request finished, so check for an error
if (reply->error() == QNetworkReply::NoError) {
// outputTxt->setPlainText(tr(reply->readAll()));
// outputTxt->setHtml(tr(reply->readAll()));
//lzt2
// Find the translated string - this is a bit of a hack, but works and is easier than parsing the HTML...
// QRegExp rx(tr("input type=hidden name=gtrans value=\"(.*)\"><div id=sug_exp>"));
QRegExp rx(tr("<div id=\"phrsListTab\" class=\"trans-wrapper clearfix\">(.*)<div id=\"ads\" class=\"ads\">"));
QString rep(reply->readAll());
rep.replace("\n", " ");
// Check that the translated text was found
if (rx.indexIn(rep) != -1) {
// The text in rx.cap(1) is html character encoded so that "&" => "&"
// That f's up QTextEdit's "setHtml", so fix it
QString tmp = rx.cap(1);
tmp.replace(tr("&"), tr("&"));
// Display the results
outputTxt->setHtml(tmp);
} else {
// outputTxt->setPlainText(tr("Could not find translated string in result..."));
outputTxt->append(tr("Could not find translated string in result..."));
//lzt3
}
} else {
outputTxt->append(tr("A network error occured."));
}
// Schedule old network stuff for deletion
reply->deleteLater();
nam->deleteLater();
}