d947107a752953c78ced6dc50e9a8f312901dd1e
[mdictionary] / trunk / src / plugins / google / src / GooglePlugin.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*! \file GooglePlugin.cpp
23     \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
24 */
25
26 #include "GooglePlugin.h"
27 #include <QDebug>
28 #include "GoogleDictDialog.h"
29
30
31 GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent),
32                     _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
33                     _type(tr("google")), _infoNote(tr("")) {
34
35     stopped = false;
36     _connectionAccept = false;
37     _settings = new Settings();
38     _dictDialog = new GoogleDictDialog(this,this);
39
40     _icon = QIcon(":/icons/drawing.png");
41     languages=initLanguages();
42     _settings->setValue("type","google");
43     _settings->setValue("connectionAcepted","false");
44
45     http = new QHttp(this);
46     connect(http, SIGNAL(done(bool)), this, SLOT(done()));
47 }
48
49 GooglePlugin::~GooglePlugin() {
50     delete _settings;
51 }
52
53 QString GooglePlugin::langFrom() const {
54     return languages.key(_langFrom);
55 }
56 QString GooglePlugin::langTo() const {
57     return languages.key(_langTo);
58 }
59
60 QString GooglePlugin::name() const {
61     return QString("dictionary");
62 }
63
64 QString GooglePlugin::type() const {
65     return _type;
66 }
67
68 QString GooglePlugin::infoNote() const {
69     return _infoNote;
70 }
71
72 void GooglePlugin::setLangTo(QString langTo){
73     _langTo=langTo;
74 }
75
76 void GooglePlugin::setLangFrom(QString langFrom){
77     _langFrom=langFrom;
78 }
79
80 DictDialog* GooglePlugin::dictDialog() {
81     return _dictDialog;
82 }
83
84 bool GooglePlugin::isAvailable() const {
85     if(_connectionAccept)
86         return true;
87     else
88         return false;
89 }
90
91 void GooglePlugin::setConnectionAccept(QString connectionAcepted) {
92     if(connectionAcepted=="true")
93         _connectionAccept=true;
94     else
95         _connectionAccept=false;
96 }
97
98 bool GooglePlugin::isConnectionAccept() {
99     return _connectionAccept;
100 }
101
102 QString GooglePlugin::search(QString key) {
103     /*function is not used on this plagin */
104     return QString("");
105 }
106
107 uint GooglePlugin::hash() const {
108     return _hash;
109 }
110
111 void GooglePlugin::setHash(uint) {
112     this->_hash=_hash;
113 }
114
115 Settings* GooglePlugin::settings() {
116     return _settings;
117 }
118
119 QIcon* GooglePlugin::icon() {
120     return &_icon;
121 }
122
123 CommonDictInterface* GooglePlugin::getNew(const Settings* settings) const
124 {
125     GooglePlugin *plugin = new GooglePlugin();
126     if(settings) {
127         plugin->setLangFrom(settings->value("langFrom"));
128         plugin->setLangTo(settings->value("langTo"));
129         plugin->setConnectionAccept(settings->value("connectionAccept"));
130         QStringList list = settings->keys();
131         foreach(QString key, list)
132             plugin->settings()->setValue(key, settings->value(key));
133     }
134     delete settings;
135     plugin->getDictionaryInfo();
136     return plugin;
137 }
138
139 void GooglePlugin::setSettings(Settings* settings) {
140     if(settings){
141         _langFrom=settings->value("langFrom");
142         _langTo=settings->value("langTo");
143         setConnectionAccept(settings->value("connectionAccept"));
144         QStringList list = settings->keys();
145         foreach(QString key, list)
146             _settings->setValue(key, settings->value(key));
147         delete settings;
148     }
149 }
150
151 Translation* GooglePlugin::getTranslationFor(QString key) {
152     QList<Translation*> translations;
153     return translations<<(new TranslationGoogle(key,"",_infoNote,this));
154 }
155
156 QList<Translation*> GooglePlugin::searchWordList(QString word, int limit) {
157     QList<Translation*> translations;
158     if(isAvailable()) {
159         word.replace("*","");
160         word.replace("?","");
161         QString url = QString("/translate_a/t?client=t&sl=%1&tl=%2").arg(_langFrom,
162                                                                          _langTo);
163         QHttpRequestHeader head = QHttpRequestHeader("POST", url, 1,1);
164         head.setValue("Host", "www.google.pl");
165         head.setValue("User-Agent", "Mozilla/5.0");
166         head.setValue("Accept-Encoding", "deflate");
167         head.setContentLength(word.length());
168         head.setValue("Connection", "Close");
169
170         QByteArray data("text=");
171         data.append(word.toUtf8());
172         http->setHost("www.google.pl");
173         wait=true;
174         http->request(head, data);
175         QString error("");
176         while(wait && (error=="" || error=="Unknown error"))
177             error=http->errorString();
178         if(error!="" && error!="Unknown error") {
179             qDebug()<<error;
180             Q_EMIT notify(Notify::Warning,
181                     QString("GooglePlugin: %1").arg(error));
182         }
183         QString text = QString::fromUtf8(http->readAll());
184         text=jsonParse(text);
185         if(text!="") {
186             text=tr("<key>") + word + tr("</key>") + tr("<t>") + text + tr("</t>");
187             translations<<(new TranslationGoogle(word,text,_infoNote,this));
188         }
189     }
190     return translations;
191 }
192
193 QString GooglePlugin::jsonParse(QString result) {
194     int pos=0,pos2=0;
195     int index=0;
196     int size=0;
197     QString returnLang;
198     QString translation;
199     QString original;
200     QList<QString> partOfSpeach;
201     QList<QList<QString>* > words;
202
203     QStringList list1 = result.split("\"");
204     size=(list1.size()-1)/2;
205     if(size<=2)
206         return QString(""); // wrong format of data
207     translation=list1.at(1);
208     original=list1.at(3);
209     pos=result.indexOf("]");
210     pos=result.indexOf("]",pos+1);
211     pos++;
212     index=3;
213     if(result.at(pos+1)==QChar(','))
214         returnLang=list1.at(index*2+1);
215     while(result.indexOf("[",pos+1)!=-1){
216         partOfSpeach.append(list1.at(index*2+1));
217         pos2=result.indexOf("]",pos+1);
218         pos=result.indexOf("\"",pos+1);
219         pos=result.indexOf("\"",pos+1);
220         pos=result.indexOf("\"",pos+1);
221         pos=result.indexOf("\"",pos+1);
222         QList<QString> *list=new QList<QString>;
223         while(pos2>pos && pos2!=-1 && pos!= -1) {
224             index++;
225             if(size==index)
226                 return QString("");
227             list->append(list1.at(index*2+1));
228             pos=result.indexOf("\"",pos+1);
229             pos=result.indexOf("\"",pos+1);
230         }
231         words.append(list);
232         index++;
233         if(size==index)
234             return QString("");
235         pos=pos2+2;
236     }
237     if(words.size()!=partOfSpeach.size()) {
238         qDebug()<<"Error in Google Plugin (JsonParse)";
239         Q_EMIT notify(Notify::Warning,
240                 QString("GooglePlugin: can't parse Json"));
241         return QString("");
242     }
243     returnLang=list1.at(index*2+1);
244     if(partOfSpeach.size()==0){
245         if(translation==original){
246             return QString(""); // word don't exist";
247         }
248         else
249             return "\""+translation+"\"";
250     }
251     result=translation;
252     for(int i=0; i<words.size();i++) {
253         result+="<br/>";
254         result+="<pos>"+partOfSpeach.at(i)+"</pos>:";
255         for(int j=0; j<words.at(i)->size();j++)
256             result+=words.at(i)->at(j)+", ";
257         result.remove(result.size()-2,2);
258     }
259     return result;
260 }
261
262 void GooglePlugin::done() {
263     wait=false;
264 }
265
266 void GooglePlugin::stop() {
267     stopped=true;
268 }
269
270 QMap<QString, QString> GooglePlugin::initLanguages() {
271     QMap<QString, QString> languages;
272     languages["AFRIKAANS"] = "af";
273     languages["ALBANIAN"] = "sq";
274     languages["AMHARIC"] = "am";
275     languages["ARABIC"] = "ar";
276     languages["ARMENIAN"] = "hy";
277     languages["AZERBAIJANI"] = "az";
278     languages["BASQUE"] = "eu";
279     languages["BELARUSIAN"] = "be";
280     languages["BENGALI"] = "bn";
281     languages["BIHARI"] = "bh";
282     languages["BRETON"] = "br";
283     languages["BULGARIAN"] = "bg";
284     languages["BURMESE"] = "my";
285     languages["CATALAN"] = "ca";
286     languages["CHEROKEE"] = "chr";
287     languages["CHINESE"] = "zh";
288     languages["CHINESE_SIMPLIFIED"] = "zh-CN";
289     languages["CHINESE_TRADITIONAL"] = "zh-TW";
290     languages["CORSICAN"] = "co";
291     languages["CROATIAN"] = "hr";
292     languages["CZECH"] = "cs";
293     languages["DANISH"] = "da";
294     languages["DHIVEHI"] = "dv";
295     languages["DUTCH"] = "nl";
296     languages["ENGLISH"] = "en";
297     languages["ESPERANTO"] = "eo";
298     languages["ESTONIAN"] = "et";
299     languages["FAROESE"] = "fo";
300     languages["FILIPINO"] = "tl";
301     languages["FINNISH"] = "fi";
302     languages["FRENCH"] = "fr";
303     languages["FRISIAN"] = "fy";
304     languages["GALICIAN"] = "gl";
305     languages["GEORGIAN"] = "ka";
306     languages["GERMAN"] = "de";
307     languages["GREEK"] = "el";
308     languages["GUJARATI"] = "gu";
309     languages["HAITIAN_CREOLE"] = "ht";
310     languages["HEBREW"] = "iw";
311     languages["HINDI"] = "hi";
312     languages["HUNGARIAN"] = "hu";
313     languages["ICELANDIC"] = "is";
314     languages["INDONESIAN"] = "id";
315     languages["INUKTITUT"] = "iu";
316     languages["IRISH"] = "ga";
317     languages["ITALIAN"] = "it";
318     languages["JAPANESE"] = "ja";
319     languages["JAVANESE"] = "jw";
320     languages["KANNADA"] = "kn";
321     languages["KAZAKH"] = "kk";
322     languages["KHMER"] = "km";
323     languages["KOREAN"] = "ko";
324     languages["KURDISH"] = "ku";
325     languages["KYRGYZ"] = "ky";
326     languages["LAO"] = "lo";
327     languages["LATIN"] = "la";
328     languages["LATVIAN"] = "lv";
329     languages["LITHUANIAN"] = "lt";
330     languages["LUXEMBOURGISH"] = "lb";
331     languages["MACEDONIAN"] = "mk";
332     languages["MALAY"] = "ms";
333     languages["MALAYALAM"] = "ml";
334     languages["MALTESE"] = "mt";
335     languages["MAORI"] = "mi";
336     languages["MARATHI"] = "mr";
337     languages["MONGOLIAN"] = "mn";
338     languages["NEPALI"] = "ne";
339     languages["NORWEGIAN"] = "no";
340     languages["OCCITAN"] = "oc";
341     languages["ORIYA"] = "or";
342     languages["PASHTO"] = "ps";
343     languages["PERSIAN"] = "fa";
344     languages["POLISH"] = "pl";
345     languages["PORTUGUESE"] = "pt";
346     languages["PORTUGUESE_PORTUGAL"] = "pt-PT";
347     languages["PUNJABI"] = "pa";
348     languages["QUECHUA"] = "qu";
349     languages["ROMANIAN"] = "ro";
350     languages["RUSSIAN"] = "ru";
351     languages["SANSKRIT"] = "sa";
352     languages["SCOTS_GAELIC"] = "gd";
353     languages["SERBIAN"] = "sr";
354     languages["SINDHI"] = "sd";
355     languages["SINHALESE"] = "si";
356     languages["SLOVAK"] = "sk";
357     languages["SLOVENIAN"] = "sl";
358     languages["SPANISH"] = "es";
359     languages["SUNDANESE"] = "su";
360     languages["SWAHILI"] = "sw";
361     languages["SWEDISH"] = "sv";
362     languages["SYRIAC"] = "syr";
363     languages["TAJIK"] = "tg";
364     languages["TAMIL"] = "ta";
365     languages["TATAR"] = "tt";
366     languages["TELUGU"] = "te";
367     languages["THAI"] = "th";
368     languages["TIBETAN"] = "bo";
369     languages["TONGA"] = "to";
370     languages["TURKISH"] = "tr";
371     languages["UKRAINIAN"] = "uk";
372     languages["URDU"] = "ur";
373     languages["UZBEK"] = "uz";
374     languages["UIGHUR"] = "ug";
375     languages["VIETNAMESE"] = "vi";
376     languages["WELSH"] = "cy";
377     languages["YIDDISH"] = "yi";
378     languages["YORUBA"] = "yo";
379     languages["UNKNOWN"] = "";
380     return languages;
381 }
382
383 void GooglePlugin::getDictionaryInfo() {
384     QString fullLangFrom=languages.key(_langFrom);
385     QString fullLangTo=languages.key(_langTo);
386     _infoNote=" [" + fullLangFrom + "-";
387     _infoNote+= fullLangTo + "] (" + _type + ")";
388 }
389
390
391 Q_EXPORT_PLUGIN2(google, GooglePlugin)