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());
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));
}
}
}
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);
}
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();
}
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;
}