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