#include "GooglePlugin.h"
#include <QDebug>
-#include "GoogleDictDialog.h"S
+#include "GoogleDictDialog.h"
GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent),
}
void GooglePlugin::setSettings(Settings* settings) {
- plugin->setLangFrom(settings->value("langFrom"));
- plugin->setLangTo(settings->value("langTo"));
+ _langFrom=settings->value("langFrom");
+ _langTo=settings->value("langTo");
QStringList list = settings->keys();
foreach(QString key, list)
- plugin->settings()->setValue(key, settings->value(key));
+ _settings->setValue(key, settings->value(key));
delete settings;
}
-Translation* getTranslationFor(QString key) {
- return searchWordList(key);
+Translation* GooglePlugin::getTranslationFor(QString key) {
+ QList<Translation*> translations = searchWordList(key);
+ if(translations.size()>0)
+ return translations.at(0);
+ else
+ return new TranslationGoogle();
}
QList<Translation*> GooglePlugin::searchWordList(QString word, int limit) {
QList<Translation*> translations;
QString url = QString("/translate_a/t?client=t&sl=%1+&tl=%2").arg(_langFrom,_langTo);
+ qDebug()<<url;
QHttpRequestHeader head = QHttpRequestHeader("POST", url, 1,1);
head.setValue("Host", "www.google.pl");
head.setValue("User-Agent", "Mozilla/5.0");
while(wait);
QString text = QString::fromUtf8(http->readAll());
- text=tr("<key>") + key + tr("</key>") + tr("<t>") + text + tr("</t>");
- translations<<(new TranslationGoogle(word,text,_infoNote,this));
+ text=jsonParse(text);
+
+ if(text!=""){
+ text=tr("<key>") + word + tr("</key>") + tr("<t>") + text + tr("</t>");
+ translations<<(new TranslationGoogle(word,text,_infoNote,this));
+ }
return translations;
}
+QString GooglePlugin::jsonParse(QString result) {
+ int pos=0,pos2=0;
+ int index=0;
+ int size=0;
+
+ QString returnLang;
+ QString translation;
+ QString original;
+ QList<QString> partOfSpeach;
+ QList<QList<QString>* > words;
+
+ QStringList list1 = result.split("\"");
+ size=(list1.size()-1)/2;
+ if(size<=2)
+ return QString("");
+ translation=list1.at(1);
+ original=list1.at(3);
+ pos=result.indexOf("]");
+ pos=result.indexOf("]",pos+1);
+ pos++;
+ index=3;
+ if(result.at(pos+1)==QChar(','))
+ returnLang=list1.at(index*2+1);
+ while(result.indexOf("[",pos+1)!=-1){
+ partOfSpeach.append(list1.at(index*2+1));
+ pos2=result.indexOf("]",pos+1);
+ pos=result.indexOf("\"",pos+1);
+ pos=result.indexOf("\"",pos+1);
+ pos=result.indexOf("\"",pos+1);
+ pos=result.indexOf("\"",pos+1);
+ while(pos2>pos && pos2!=-1 && pos!= -1) {
+ index++;
+ if(size==index)
+ return QString("");
+ QList<QString> *list=new QList<QString>;
+ list->append(list1.at(index*2+1));
+ words.append(list);
+ pos=result.indexOf("\"",pos+1);
+ pos=result.indexOf("\"",pos+1);
+ }
+ index++;
+ if(size==index)
+ return QString("");
+ pos=pos2+2;
+ }
+ returnLang=list1.at(index*2+1);
+
+ if(returnLang!=_langFrom)
+ return QString("");
+
+
+
+ return result;
+}
+
void GooglePlugin::done() {
wait=false;
}
#include "TranslationGoogle.h"
-TranslationGoogle::TranslationGoogle()
-{
+TranslationGoogle::TranslationGoogle():_key(""),_trans(""),_dictionaryInfo("") {
+ googlePlugin=0;
}
TranslationGoogle::TranslationGoogle(QString _key,QString _trans,
this->googlePlugin=googlePlugin;
if(googlePlugin)
_dictHash = googlePlugin->hash();
-
+ _bookmark=0;
}
QString TranslationGoogle::key() const{
return _key;
}
-
QString TranslationGoogle::dictionaryInfo() const {
return _dictionaryInfo;
}
-
QString TranslationGoogle::toHtml() const {
QString result("");
- result+="<dict> <info bookmark=\"false\">GOOGLE TRANSLATOR </info>";
- result+=_trans +"</dict>";
+ if(googlePlugin) {
+ result+="<dict> <info bookmark=\"false\">GOOGLE TRANSLATOR </info>";
+ result+=_trans +"</dict>";
+ }
+ qDebug()<<"test"+result;
return result;
}