forked from jgoday/googletranslaterunner
/
googletranslatehttp.cpp
70 lines (59 loc) · 2.12 KB
/
googletranslatehttp.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
/*
* Copyright (C) 2009 Javier Goday <jgoday@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "googletranslatehttp.h"
#include "googletranslateutil.h"
#include <QHttpHeader>
#include <QHttpRequestHeader>
#include <QDebug>
GoogleTranslateHttp::GoogleTranslateHttp(QObject *parent) :
QHttp(parent)
{
connect(this, SIGNAL(done(bool)), SLOT(requestFinished()));
}
GoogleTranslateHttp::~GoogleTranslateHttp()
{
loop.exit();
}
QString GoogleTranslateHttp::request(const QString &text, const QString &fromLanguage,
const QString &toLanguage)
{
const QString &word = GoogleTranslateUtil::getSearchWord(text);
qDebug() << "Searching " << word << " from = " << fromLanguage << " to = " << toLanguage;
QString url = QString("/translate_a/t?client=t&sl="+
fromLanguage +
"&tl=" +
toLanguage);
setHost("www.google.com");
QHttpRequestHeader header = QHttpRequestHeader("POST", url, 1, 1);
header.setValue("Host", "www.google.com");
header.setValue("User-Agent", "Mozilla/5.0");
header.setContentLength(word.length());
QByteArray ba("text=");
ba.append(word.toUtf8());
QHttp::request(header, ba);
loop.exec();
return QString::fromUtf8(readAll());
}
bool GoogleTranslateHttp::hasError() const
{
return error() != QHttp::NoError;
}
void GoogleTranslateHttp::requestFinished()
{
loop.exit();
}