Merge branch 'master' of ssh://drop.maemo.org/git/mdictionary
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index d3af1ea..b544870 100644 (file)
@@ -67,7 +67,11 @@ QString XdxfPlugin::infoNote() const {
 }
 
 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
-    if(_settings->value("cached") == "true")
+    //if(_settings->value("cached") == "true")
+    if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
+       && word.indexOf("%")==-1)
+        word+="*";
+    if(isCached())
         return searchWordListCache(word,limit);
     return searchWordListFile(word, limit);
 }
@@ -83,10 +87,12 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         }
 
         stopped = false;
-        if(word.indexOf("*")==-1)
+        if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
             word+="%";
         word = word.replace("*", "%");
+        word = word.replace("?", "_");
         word = removeAccents(word);
+        qDebug() << word;
 
         QSqlQuery cur(db);
         cur.prepare("select word from dict where word like ? limit ?");
@@ -108,8 +114,6 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     word = removeAccents(word);
 
     stopped = false;
-    if(word.indexOf("*")==-1)
-        word+="*";
     QRegExp regWord(word);
     regWord.setCaseSensitivity(Qt::CaseInsensitive);
     regWord.setPatternSyntax(QRegExp::Wildcard);
@@ -132,8 +136,7 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
             if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
                 bool ok=true;
                 Translation *tran;
-                foreach(tran,translations)
-                {
+                foreach(tran,translations) {
                     if(tran->key()==a)
                         ok=false;  /*if key word is in the dictionary more that one */
                 }
@@ -152,7 +155,8 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
 }
 
 QString XdxfPlugin::search(QString key) {
-    if(_settings->value("cached") == "true")
+//    if(_settings->value("cached") == "true")
+    if(isCached())
         return searchCache(key);
     return searchFile(key);
 }
@@ -207,17 +211,45 @@ QString XdxfPlugin::searchFile(QString key) {
         }
         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
             if(match) {
-                QString temp(dictionaryReader.text().toString());
+ /*             QString temp(dictionaryReader.text().toString());
+                if(temp=="\n")
+                    temp=dictionaryReader.readElementText();
                 temp.replace("\n","");
                 if(temp == ""){
+                    int i=0;
                     while(dictionaryReader.name()!="ar"&&
                                 !dictionaryReader.atEnd()){
                         dictionaryReader.readNext();
+                        if(dictionaryReader.name()!="" &&
+                                         dictionaryReader.name()!="ar") {
+                            if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
+                                temp+=tr("</");
+                            if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
+                                temp+=tr("<");
+                            qDebug()<<dictionaryReader.tokenType();
+                            temp=temp+dictionaryReader.name().toString() + tr(">");
+                            i++;
+                        }
                         temp+=dictionaryReader.text().toString();
                     }
                 }
                 resultString+=temp.replace("\n","")+"\n";
                 match=false;
+*/
+                QString temp("");
+                while(dictionaryReader.name()!="ar" && !dictionaryReader.atEnd()) {
+                    if(dictionaryReader.name()!="") {
+                        if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
+                            temp+=tr("</");
+                        if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
+                            temp+=tr("<");
+                        temp=temp+dictionaryReader.name().toString() + tr(">");
+                    }
+                    temp+= dictionaryReader.text().toString();
+                    dictionaryReader.readNext();
+                }
+                resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
+                match=false;
             }
         }
         this->thread()->yieldCurrentThread();
@@ -270,13 +302,11 @@ bool XdxfPlugin::isAvailable() const {
     return true;
 }
 
-void XdxfPlugin::setHash(uint _hash)
-{
+void XdxfPlugin::setHash(uint _hash) {
     this->_hash=_hash;
 }
 
-uint XdxfPlugin::hash() const
-{
+uint XdxfPlugin::hash() const {
    return _hash;
 }
 
@@ -284,8 +314,9 @@ Settings* XdxfPlugin::settings() {
     return _settings;
 }
 
-bool XdxfPlugin::isCached()
-{
+bool XdxfPlugin::isCached() {
+    if(_settings->value("cached") == "true")
+        return true;
     return false;
 }
 
@@ -301,6 +332,9 @@ void XdxfPlugin::setSettings(Settings *settings) {
        settings->value("generateCache") == "true") {
         makeCache("");
     }
+    else {
+       _settings->setValue("cached", "false");
+    }
 
     emit settingsChanged();
 }
@@ -341,7 +375,9 @@ QString XdxfPlugin::removeAccents(QString string) {
             !normalized[i].isSpace() &&
             !normalized[i].isDigit() &&
             normalized[i] != '*' &&
-            normalized[i] != '%') {
+            normalized[i] != '%' &&
+            normalized[i] != '_' &&
+            normalized[i] != '?' ) {
             normalized.remove(i,1);
         }
     }
@@ -398,10 +434,9 @@ bool XdxfPlugin::makeCache(QString dir) {
 
     QXmlStreamReader reader(&dictionaryFile);
 
-
     db.setDatabaseName(cachePathN);
     if(!db.open()) {
-        qDebug() << "Database error" << endl;
+        qDebug() << "Database error" << db.lastError().text() << endl;
         return false;
     }
     QCoreApplication::processEvents();
@@ -419,12 +454,13 @@ bool XdxfPlugin::makeCache(QString dir) {
     timer.start();
     countWords();
 
+    int lastProg = -1;
+
 
     counter=0;
     while (!reader.atEnd() && !stopped) {
 
         QCoreApplication::processEvents();
-        //usleep(50);
         reader.readNext();
 
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
@@ -435,15 +471,19 @@ bool XdxfPlugin::makeCache(QString dir) {
         }
         else if(reader.tokenType() == QXmlStreamReader::Characters) {
              if(match) {
-                QString temp(reader.text().toString());
-                temp.replace("\n","");
-                if(temp == ""){
-                    while(reader.name()!="ar"&&
-                                !reader.atEnd()){
-                        reader.readNext();
-                        temp+=reader.text().toString();
+                QString temp("");
+                while(reader.name()!="ar" && !reader.atEnd()) {
+                    if(reader.name()!="") {
+                        if(reader.tokenType()==QXmlStreamReader::EndElement)
+                            temp+=tr("</");
+                        if(reader.tokenType()==QXmlStreamReader::StartElement)
+                            temp+=tr("<");
+                        temp=temp+reader.name().toString() + tr(">");
                     }
+                    temp+= reader.text().toString();
+                    reader.readNext();
                 }
+                temp=tr("<t>") + temp.replace("\n","") + tr("</t>");
                 match = false;
                 cur.prepare("insert into dict values(?,?)");
                 cur.addBindValue(a);
@@ -451,8 +491,11 @@ bool XdxfPlugin::makeCache(QString dir) {
                 cur.exec();
                 counter++;
                 int prog = counter*100/_wordsCount;
-                if(prog % 5 == 0)
-                    Q_EMIT updateCachingProgress(prog);
+                if(prog % 5 == 0 && lastProg != prog) {
+                    Q_EMIT updateCachingProgress(prog,
+                                                 timer.restart());
+                    lastProg = prog;
+                }
             }
 
         }