add Json parse to Google translator
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Tue, 24 Aug 2010 12:47:29 +0000 (14:47 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Tue, 24 Aug 2010 12:47:29 +0000 (14:47 +0200)
trunk/src/plugins/google/src/GoogleDictDialog.cpp
trunk/src/plugins/google/src/GooglePlugin.cpp
trunk/src/plugins/google/src/GooglePlugin.h
trunk/src/plugins/google/src/TranslationGoogle.cpp
trunk/src/plugins/google/src/TranslationGoogle.h
trunk/src/plugins/google/tests/test.cpp
trunk/src/plugins/xdxf/src/TranslationXdxf.cpp
trunk/src/plugins/xdxf/src/TranslationXdxf.h

index 5c4eabc..33d7e8c 100644 (file)
@@ -2,15 +2,14 @@
 
 GoogleDictDialog::GoogleDictDialog(GooglePlugin *plugin, QObject *parent) :
     DictDialog(parent) {
-
     this->plugin = plugin;
 }
 
 
 Settings* GoogleDictDialog::addNewDictionary(QWidget *parent) {
     Settings *settings = new Settings();
-    settings->setValue("langTo","en");
-    settings->setValue("langFrom","pl");
+    settings->setValue("langTo","pl");
+    settings->setValue("langFrom","en");
     return settings;
     //return GoogleLoadDialog::getSettings(parent);
 }
index 98e174b..bb286d6 100644 (file)
@@ -24,7 +24,7 @@
 
 #include "GooglePlugin.h"
 #include <QDebug>
-#include "GoogleDictDialog.h"S
+#include "GoogleDictDialog.h"
 
 
 GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent),
@@ -119,21 +119,26 @@ CommonDictInterface* GooglePlugin::getNew(const Settings* settings) const
 }
 
 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");
@@ -149,12 +154,71 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int limit) {
     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;
 }
index a4ba857..632b04d 100644 (file)
@@ -98,7 +98,6 @@ public:
 
     Translation* getTranslationFor(QString key);
 
-
 public slots:
     /*! performs search in dictionary
       \param  word word to search in dictionary
@@ -117,6 +116,7 @@ public slots:
 
 private:
     void initLanguages();
+    QString jsonParse(QString result);
     QMap<QString, QString> languages;
 
     //! language from which we translate
index 4ccd0c6..9db6069 100644 (file)
@@ -1,7 +1,7 @@
 #include "TranslationGoogle.h"
 
-TranslationGoogle::TranslationGoogle()
-{
+TranslationGoogle::TranslationGoogle():_key(""),_trans(""),_dictionaryInfo("") {
+    googlePlugin=0;
 }
 
 TranslationGoogle::TranslationGoogle(QString _key,QString _trans,
@@ -10,23 +10,24 @@ 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;
 }
 
index e486d71..0ce3a90 100644 (file)
@@ -44,6 +44,7 @@ private:
     QString _trans;
     GooglePlugin *googlePlugin;
     int _dictHash;
+    bool error;
 
 };
 
index 17cd944..be9d2e2 100644 (file)
@@ -30,9 +30,7 @@ void GoogleTest::getNew() {
     settings->setValue("langTo","en");
 
 
-    CommonDictInterface *googlePlugin2 = googlePlugin.getNew(settings);
-    QSignalSpy signa( googlePlugin2, SIGNAL( done() ) );
-    QList<Translation*> te=googlePlugin2->searchWordList("kot",8);
+jsonParse(QString result)
 
 
 
index 0a1246b..10b3a66 100644 (file)
@@ -25,7 +25,8 @@
 #include "TranslationXdxf.h"
 #include <QDebug>
 
-TranslationXdxf::TranslationXdxf() {
+TranslationXdxf::TranslationXdxf():_key(""),_dictionaryInfo("") {
+    xdxfPlugin=0;
 }
 
 TranslationXdxf::TranslationXdxf(QString _key, QString _dictionaryInfo,
@@ -33,6 +34,7 @@ TranslationXdxf::TranslationXdxf(QString _key, QString _dictionaryInfo,
     this->xdxfPlugin=xdxfPlugin;
     if(xdxfPlugin)
         _dictHash = xdxfPlugin->hash();
+    _bookmark=0;
 }
 
 QString TranslationXdxf::key() const {
index 8236e39..f01acaa 100644 (file)
@@ -63,7 +63,6 @@ private:
     QString _dictionaryInfo;
     XdxfPlugin *xdxfPlugin;
     int _dictHash;
-
 };
 
 #endif // TRANSLATIONXDXF_H