change search limit (from keys to translation)
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Thu, 26 Aug 2010 12:47:28 +0000 (14:47 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Thu, 26 Aug 2010 12:47:28 +0000 (14:47 +0200)
trunk/src/plugins/google/src/GooglePlugin.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.cpp

index 41663a5..c0e89c5 100644 (file)
@@ -160,7 +160,7 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int ) {
         QString url = QString("/translate_a/t?client=t&sl=%1&tl=%2").arg(_langFrom,
                                                                          _langTo);
         QHttpRequestHeader head = QHttpRequestHeader("POST", url, 1,1);
-        head.setValue("Host", "www.google.pl");
+        head.setValue("Host","www.google.pl");
         head.setValue("User-Agent", "Mozilla/5.0");
         head.setValue("Accept-Encoding", "deflate");
         head.setContentLength(word.length());
@@ -185,7 +185,7 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int ) {
         QString text = QString::fromUtf8(http->readAll());
         text=jsonParse(text);
         if(text!="") {
-            text=tr("<key>") + word + tr("</key>") + tr("<t>") + text + tr("</t>");
+            text="<key>" + word + "</key>" + "<t>" + text + "</t>";
             translations<<(new TranslationGoogle(word,text,_infoNote,this));
         }
     }
@@ -193,27 +193,40 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int ) {
 }
 
 QString GooglePlugin::jsonParse(QString result) {
-    int pos=0,pos2=0;
-    int index=0;
-    int size=0;
+    int pos=0,pos2=0,index=0,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(""); // wrong format of data
-    translation=list1.at(1);
-    original=list1.at(3);
+
+    translation=list1.at(index*2+1);
+    index++;
+    original=list1.at(index*2+1);
     pos=result.indexOf("]");
+    index++;
+
+    while(result.at(pos+1)==QChar(',')) {
+        index++;
+        translation+=list1.at(index*2+1);
+        index++;
+        original=list1.at(index*2+1);
+        pos=result.indexOf("]",pos+1);
+        index++;
+    }
+
     pos=result.indexOf("]",pos+1);
     pos++;
-    index=3;
+    index++;
     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);
index a0f8d5e..322feb0 100644 (file)
@@ -86,48 +86,51 @@ QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
 }
 
 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
-
+    int i=0;
     QSet<Translation*> translations;
+
     QString cacheFilePath = _settings->value("cache_path");
-        db.setDatabaseName(cacheFilePath);
-        if(!db.open()) {
-            qDebug() << "Database error" << db.lastError().text() << endl;
-            Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
-                    "opened for %1 dictionary. Searching in xdxf file. "
-                    "You may want to recache.").arg(name()));
-            return searchWordListFile(word, limit);
-        }
+    db.setDatabaseName(cacheFilePath);
+    if(!db.open()) {
+        qDebug() << "Database error" << db.lastError().text() << endl;
+        Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
+                "opened for %1 dictionary. Searching in xdxf file. "
+                "You may want to recache.").arg(name()));
+        return searchWordListFile(word, limit);
+    }
 
-        stopped = false;
-        word = word.toLower();
-        word = word.replace("*", "%");
-        word = word.replace("?", "_");
-
-        QSqlQuery cur(db);
-        if(limit !=0)
-            cur.prepare("select word from dict where word like ? or normalized like ? limit ?");
-        else
-            cur.prepare("select word from dict where word like ? or normalized like ?");
-        cur.addBindValue(word);
-        cur.addBindValue(word);
-        if(limit !=0)
-            cur.addBindValue(limit);
-        cur.exec();
-        bool in = false;
-        while(cur.next()){
-            in = true;
-            bool ok=true;
-            Translation *tran;
-            foreach(tran,translations) {
-                if(tran->key().toLower()==cur.value(0).toString().toLower())
-                        ok=false;
-            }
-            if(ok)  /*add key word to list*/
-                translations.insert(new TranslationXdxf(
-                        cur.value(0).toString().toLower(),
-                        _infoNote, this));
+    stopped = false;
+    word = word.toLower();
+    word = word.replace("*", "%");
+    word = word.replace("?", "_");
+
+    QSqlQuery cur(db);
+    if(limit !=0)
+        cur.prepare("select word from dict where word like ? or normalized like ? limit ?");
+    else
+        cur.prepare("select word from dict where word like ? or normalized like ?");
+    cur.addBindValue(word);
+    cur.addBindValue(word);
+    if(limit !=0)
+        cur.addBindValue(limit);
+    cur.exec();
+    bool in = false;
+    while(cur.next() && (i<limit || limit==0 ) ) {
+        in = true;
+        bool ok=true;
+        Translation *tran;
+        foreach(tran,translations) {
+            if(tran->key().toLower()==cur.value(0).toString().toLower())
+                    ok=false;
         }
-        db.close();
+        if(ok) {  /*add key word to list*/
+            translations.insert(new TranslationXdxf(
+                    cur.value(0).toString().toLower(),
+                    _infoNote, this));
+            i++;
+        }
+    }
+    db.close();
     return translations.toList();
 }
 
@@ -171,10 +174,11 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                     if(tran->key().toLower()==a.toLower())
                         ok=false;  /*if key word is in the dictionary more that one */
                 }
-                if(ok)  /*add key word to list*/
+                if(ok) {  /*add key word to list*/
                     translations<<(new TranslationXdxf(a.toLower(),
                                 _infoNote,this));
-                i++;
+                    i++;
+                }
                 if(i>=limit && limit!=0)
                     break;
             }