change in jsonParse 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 */
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     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 _langFrom;
52 }
53
54 QString GooglePlugin::langTo() const {
55     return _langTo;
56 }
57
58 QString GooglePlugin::name() const {
59     return _name;
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     return plugin;
119 }
120
121 void GooglePlugin::setSettings(Settings* settings) {
122     _langFrom=settings->value("langFrom");
123     _langTo=settings->value("langTo");
124     QStringList list = settings->keys();
125     foreach(QString key, list)
126         _settings->setValue(key, settings->value(key));
127     delete settings;
128 }
129
130 Translation* GooglePlugin::getTranslationFor(QString key) {
131     QList<Translation*> translations = searchWordList(key);
132     if(translations.size()>0)
133         return translations.at(0);
134     else
135         return new TranslationGoogle();
136 }
137
138 QList<Translation*> GooglePlugin::searchWordList(QString word, int limit) {
139     QList<Translation*> translations;
140     QString url = QString("/translate_a/t?client=t&sl=%1+&tl=%2").arg(_langFrom,_langTo);
141     qDebug()<<url;
142     QHttpRequestHeader head = QHttpRequestHeader("POST", url, 1,1);
143     head.setValue("Host", "www.google.pl");
144     head.setValue("User-Agent", "Mozilla/5.0");
145     head.setValue("Accept-Encoding", "deflate");
146     head.setContentLength(word.length());
147     head.setValue("Connection", "Close");
148
149     QByteArray data("text=");
150     data.append(word.toUtf8());
151     http->setHost("www.google.pl");
152     wait=true;
153     http->request(head, data);
154     while(wait);
155
156     QString text = QString::fromUtf8(http->readAll());
157
158     text=jsonParse(text);
159     if(text!=""){
160         text=tr("<key>") + word + tr("</key>") + tr("<t>") + text + tr("</t>");
161         translations<<(new TranslationGoogle(word,text,_infoNote,this));
162     }
163     return translations;
164 }
165
166 QString GooglePlugin::jsonParse(QString result) {
167     int pos=0,pos2=0;
168     int index=0;
169     int size=0;
170
171     QString returnLang;
172     QString translation;
173     QString original;
174     QList<QString> partOfSpeach;
175     QList<QList<QString>* > words;
176
177     QStringList list1 = result.split("\"");
178     size=(list1.size()-1)/2;
179     if(size<=2)
180         return QString("");
181     translation=list1.at(1);
182     original=list1.at(3);
183     pos=result.indexOf("]");
184     pos=result.indexOf("]",pos+1);
185     pos++;
186     index=3;
187     if(result.at(pos+1)==QChar(','))
188         returnLang=list1.at(index*2+1);
189     while(result.indexOf("[",pos+1)!=-1){
190         partOfSpeach.append(list1.at(index*2+1));
191         pos2=result.indexOf("]",pos+1);
192         pos=result.indexOf("\"",pos+1);
193         pos=result.indexOf("\"",pos+1);
194         pos=result.indexOf("\"",pos+1);
195         pos=result.indexOf("\"",pos+1);
196         while(pos2>pos && pos2!=-1 && pos!= -1) {
197             index++;
198             if(size==index)
199                 return QString("");
200             QList<QString> *list=new QList<QString>;
201             list->append(list1.at(index*2+1));
202             words.append(list);
203             pos=result.indexOf("\"",pos+1);
204             pos=result.indexOf("\"",pos+1);
205         }
206         index++;
207         if(size==index)
208             return QString("");
209         pos=pos2+2;
210     }
211     returnLang=list1.at(index*2+1);
212
213     if(returnLang!=_langFrom)
214         return QString("");
215
216     //result=translation;
217
218     return result;
219 }
220
221 void GooglePlugin::done() {
222     wait=false;
223 }
224
225 void GooglePlugin::started(int a) {
226     qDebug()<<"test";
227 }
228
229 void GooglePlugin::stop() {
230     stopped=true;
231 }
232
233 void GooglePlugin::initLanguages() {
234     languages["AFRIKAANS"] = "af";
235     languages["ALBANIAN"] = "sq";
236     languages["AMHARIC"] = "am";
237     languages["ARABIC"] = "ar";
238     languages["ARMENIAN"] = "hy";
239     languages["AZERBAIJANI"] = "az";
240     languages["BASQUE"] = "eu";
241     languages["BELARUSIAN"] = "be";
242     languages["BENGALI"] = "bn";
243     languages["BIHARI"] = "bh";
244     languages["BRETON"] = "br";
245     languages["BULGARIAN"] = "bg";
246     languages["BURMESE"] = "my";
247     languages["CATALAN"] = "ca";
248     languages["CHEROKEE"] = "chr";
249     languages["CHINESE"] = "zh";
250     languages["CHINESE_SIMPLIFIED"] = "zh-CN";
251     languages["CHINESE_TRADITIONAL"] = "zh-TW";
252     languages["CORSICAN"] = "co";
253     languages["CROATIAN"] = "hr";
254     languages["CZECH"] = "cs";
255     languages["DANISH"] = "da";
256     languages["DHIVEHI"] = "dv";
257     languages["DUTCH"] = "nl";
258     languages["ENGLISH"] = "en";
259     languages["ESPERANTO"] = "eo";
260     languages["ESTONIAN"] = "et";
261     languages["FAROESE"] = "fo";
262     languages["FILIPINO"] = "tl";
263     languages["FINNISH"] = "fi";
264     languages["FRENCH"] = "fr";
265     languages["FRISIAN"] = "fy";
266     languages["GALICIAN"] = "gl";
267     languages["GEORGIAN"] = "ka";
268     languages["GERMAN"] = "de";
269     languages["GREEK"] = "el";
270     languages["GUJARATI"] = "gu";
271     languages["HAITIAN_CREOLE"] = "ht";
272     languages["HEBREW"] = "iw";
273     languages["HINDI"] = "hi";
274     languages["HUNGARIAN"] = "hu";
275     languages["ICELANDIC"] = "is";
276     languages["INDONESIAN"] = "id";
277     languages["INUKTITUT"] = "iu";
278     languages["IRISH"] = "ga";
279     languages["ITALIAN"] = "it";
280     languages["JAPANESE"] = "ja";
281     languages["JAVANESE"] = "jw";
282     languages["KANNADA"] = "kn";
283     languages["KAZAKH"] = "kk";
284     languages["KHMER"] = "km";
285     languages["KOREAN"] = "ko";
286     languages["KURDISH"] = "ku";
287     languages["KYRGYZ"] = "ky";
288     languages["LAO"] = "lo";
289     languages["LATIN"] = "la";
290     languages["LATVIAN"] = "lv";
291     languages["LITHUANIAN"] = "lt";
292     languages["LUXEMBOURGISH"] = "lb";
293     languages["MACEDONIAN"] = "mk";
294     languages["MALAY"] = "ms";
295     languages["MALAYALAM"] = "ml";
296     languages["MALTESE"] = "mt";
297     languages["MAORI"] = "mi";
298     languages["MARATHI"] = "mr";
299     languages["MONGOLIAN"] = "mn";
300     languages["NEPALI"] = "ne";
301     languages["NORWEGIAN"] = "no";
302     languages["OCCITAN"] = "oc";
303     languages["ORIYA"] = "or";
304     languages["PASHTO"] = "ps";
305     languages["PERSIAN"] = "fa";
306     languages["POLISH"] = "pl";
307     languages["PORTUGUESE"] = "pt";
308     languages["PORTUGUESE_PORTUGAL"] = "pt-PT";
309     languages["PUNJABI"] = "pa";
310     languages["QUECHUA"] = "qu";
311     languages["ROMANIAN"] = "ro";
312     languages["RUSSIAN"] = "ru";
313     languages["SANSKRIT"] = "sa";
314     languages["SCOTS_GAELIC"] = "gd";
315     languages["SERBIAN"] = "sr";
316     languages["SINDHI"] = "sd";
317     languages["SINHALESE"] = "si";
318     languages["SLOVAK"] = "sk";
319     languages["SLOVENIAN"] = "sl";
320     languages["SPANISH"] = "es";
321     languages["SUNDANESE"] = "su";
322     languages["SWAHILI"] = "sw";
323     languages["SWEDISH"] = "sv";
324     languages["SYRIAC"] = "syr";
325     languages["TAJIK"] = "tg";
326     languages["TAMIL"] = "ta";
327     languages["TATAR"] = "tt";
328     languages["TELUGU"] = "te";
329     languages["THAI"] = "th";
330     languages["TIBETAN"] = "bo";
331     languages["TONGA"] = "to";
332     languages["TURKISH"] = "tr";
333     languages["UKRAINIAN"] = "uk";
334     languages["URDU"] = "ur";
335     languages["UZBEK"] = "uz";
336     languages["UIGHUR"] = "ug";
337     languages["VIETNAMESE"] = "vi";
338     languages["WELSH"] = "cy";
339     languages["YIDDISH"] = "yi";
340     languages["YORUBA"] = "yo";
341     languages["UNKNOWN"] = "";
342 }
343
344 Q_EXPORT_PLUGIN2(google, GooglePlugin)