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