add change from searchFile to searchCache in XdxfPlugin
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index 48e3857..af10ad3 100644 (file)
@@ -35,12 +35,10 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
     _dictDialog = new XdxfDictDialog(this, this);
     cachingDialog = new XdxfCachingDialog(this);
 
-    _settings->setValue("type","xdxf");
-    if(isCached())
-        _settings->setValue("cached","true");
-    else
-        _settings->setValue("cached","false");
+    connect(cachingDialog, SIGNAL(cancelCaching()),
+            this, SLOT(stop()));
 
+    _settings->setValue("type","xdxf");
 
     stopped = false;
 
@@ -69,14 +67,17 @@ 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);
 }
 
 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
 
-    qDebug() << "search cache";
     QSet<Translation*> translations;
     QString cacheFilePath = _settings->value("cache_path");
         db.setDatabaseName(cacheFilePath);
@@ -86,10 +87,12 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         }
 
         stopped = false;
-        word = removeAccents(word);
-        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 ?");
@@ -105,15 +108,12 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
 
 
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
-    qDebug() << "search file";
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
 
     word = removeAccents(word);
 
     stopped = false;
-    if(word.indexOf("*")==-1)
-        word+="*";
     QRegExp regWord(word);
     regWord.setCaseSensitivity(Qt::CaseInsensitive);
     regWord.setPatternSyntax(QRegExp::Wildcard);
@@ -122,17 +122,17 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
         return translations.toList();
     }
 
-    QXmlStreamReader dictionaryReader(&dictionaryFile);
+    QXmlStreamReader reader(&dictionaryFile);
     /*search words list*/
     QString a;
     int i=0;
-    while(!dictionaryReader.atEnd() && !stopped){
-        dictionaryReader.readNextStartElement();
-        if(dictionaryReader.name()=="ar"){
-            while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
-                dictionaryReader.readNextStartElement();
-            if(!dictionaryReader.atEnd())
-                a = dictionaryReader.readElementText();
+    while(!reader.atEnd() && !stopped){
+        reader.readNextStartElement();
+        if(reader.name()=="ar") {
+            while(reader.name()!="k" && !reader.atEnd())
+                reader.readNextStartElement();
+            if(!reader.atEnd())
+                a = reader.readElementText();
             if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
                 bool ok=true;
                 Translation *tran;
@@ -156,42 +156,78 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
 }
 
 QString XdxfPlugin::search(QString key) {
+//    if(_settings->value("cached") == "true")
+    if(isCached())
+        return searchCache(key);
+    return searchFile(key);
+}
+
+
+
+QString XdxfPlugin::searchCache(QString key) {
+    QString result;
+    QString cacheFilePath = _settings->value("cache_path");
+    db.setDatabaseName(cacheFilePath);
+
+    if(!db.open()) {
+        qDebug() << "Database error" << db.lastError().text() << endl;
+        return searchFile(key);
+    }
+
+    QSqlQuery cur(db);
+    cur.prepare("select translation from dict where word like ? limit 1");
+    cur.addBindValue(key);
+    cur.exec();
+    if(cur.next())
+        result = cur.value(0).toString();
+    return result;
+
+}
+
+
+
+
+QString XdxfPlugin::searchFile(QString key) {
     QFile dictionaryFile(path);
     QString resultString("");
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
         qDebug()<<"Error: could not open file";
         return "";
     }
-    QXmlStreamReader dictionaryReader(&dictionaryFile);
+    QXmlStreamReader reader(&dictionaryFile);
 
 
     QString a;
 
     bool match =false;
     stopped = false;
-    while (!dictionaryReader.atEnd()&& !stopped) {
-        dictionaryReader.readNext();
-        if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
-            if(dictionaryReader.name()=="k") {
-                a = dictionaryReader.readElementText();
+    while (!reader.atEnd()&& !stopped) {
+        reader.readNext();
+        if(reader.tokenType() == QXmlStreamReader::StartElement) {
+            if(reader.name()=="k") {
+                a = reader.readElementText();
                 if(a==key)
                     match = true;
             }
         }
-        else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
-            if(match) {
-                QString temp(dictionaryReader.text().toString());
-                temp.replace("\n","");
-                if(temp == ""){
-                    while(dictionaryReader.name()!="ar"&&
-                                !dictionaryReader.atEnd()){
-                        dictionaryReader.readNext();
-                        temp+=dictionaryReader.text().toString();
-                    }
+        if(match) {
+            QString temp("");
+            while(reader.name()!="ar" && !reader.atEnd()) {
+                if(reader.name()!="" && reader.name()!="k") {
+                    if(reader.tokenType()==QXmlStreamReader::EndElement)
+                        temp+=tr("</");
+                    if(reader.tokenType()==QXmlStreamReader::StartElement)
+                        temp+=tr("<");
+                    temp+=reader.name().toString();
+                    if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
+                       temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
+                    temp+=tr(">");
                 }
-                resultString+=temp.replace("\n","")+"\n";
-                match=false;
+                temp+= reader.text().toString();
+                reader.readNext();
             }
+            resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
+            match=false;
         }
         this->thread()->yieldCurrentThread();
     }
@@ -223,15 +259,19 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
         QStringList list = settings->keys();
         foreach(QString key, list)
             plugin->settings()->setValue(key, settings->value(key));
-        
-//if(plugin->settings()->value("cached") != "true")
+
+
         plugin->db_name = plugin->_settings->value("type")
                + plugin->_settings->value("path");
         plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
-        plugin->makeCache("");
-        plugin->getDictionaryInfo();
-        
+
+        if(settings->value("cached").isEmpty() &&
+           settings->value("generateCache") == "true") {
+            plugin->makeCache("");
+        }
     }
+
+    plugin->getDictionaryInfo();
     return  plugin;
 }
 
@@ -255,6 +295,8 @@ Settings* XdxfPlugin::settings() {
 
 bool XdxfPlugin::isCached()
 {
+    if(_settings->value("cached") == "true")
+        return true;
     return false;
 }
 
@@ -265,12 +307,13 @@ void XdxfPlugin::setSettings(Settings *settings) {
         setPath(settings->value("path"));
     }
 
-    if(_settings->value("cached") == "false" &&
-       settings->value("cached") == "true") {
+    if((_settings->value("cached") == "false" ||
+        _settings->value("cached").isEmpty()) &&
+       settings->value("generateCache") == "true") {
         makeCache("");
     }
     else {
-        _settings->setValue("cached", settings->value("cached"));
+       _settings->setValue("cached", "false");
     }
 
     emit settingsChanged();
@@ -284,20 +327,20 @@ void XdxfPlugin::getDictionaryInfo() {
         return;
     }
 
-    QXmlStreamReader dictionaryReader(&dictionaryFile);
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="xdxf") {
-      if(dictionaryReader.attributes().hasAttribute("lang_from"))
-        _langFrom = dictionaryReader.attributes().value("lang_from").toString();
-      if(dictionaryReader.attributes().hasAttribute("lang_to"))
-        _langTo = dictionaryReader.attributes().value("lang_to").toString();
+    QXmlStreamReader reader(&dictionaryFile);
+    reader.readNextStartElement();
+    if(reader.name()=="xdxf") {
+      if(reader.attributes().hasAttribute("lang_from"))
+        _langFrom = reader.attributes().value("lang_from").toString();
+      if(reader.attributes().hasAttribute("lang_to"))
+        _langTo = reader.attributes().value("lang_to").toString();
     }
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="full_name")
-        _name=dictionaryReader.readElementText();
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="description")
-        _infoNote=dictionaryReader.readElementText();
+    reader.readNextStartElement();
+    if(reader.name()=="full_name")
+        _name=reader.readElementText();
+    reader.readNextStartElement();
+    if(reader.name()=="description")
+        _infoNote=reader.readElementText();
 
     dictionaryFile.close();
 }
@@ -310,7 +353,11 @@ QString XdxfPlugin::removeAccents(QString string) {
     for(int i=0; i<normalized.size(); i++) {
         if( !normalized[i].isLetterOrNumber() &&
             !normalized[i].isSpace() &&
-            !normalized[i].isDigit()) {
+            !normalized[i].isDigit() &&
+            normalized[i] != '*' &&
+            normalized[i] != '%' &&
+            normalized[i] != '_' &&
+            normalized[i] != '?' ) {
             normalized.remove(i,1);
         }
     }
@@ -352,6 +399,7 @@ int XdxfPlugin::countWords() {
 bool XdxfPlugin::makeCache(QString dir) {
     cachingDialog->setVisible(true);
     QCoreApplication::processEvents();
+    stopped = false;
     QFileInfo dictFileN(_settings->value("path"));
     QString cachePathN;
     cachePathN = QDir::homePath() + "/.mdictionary/"
@@ -360,11 +408,9 @@ bool XdxfPlugin::makeCache(QString dir) {
     QFile dictionaryFile(dictFileN.filePath());
 
 
-    qDebug() << dictFileN.path();
     if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
         return 0;
     }
-    qDebug() << "OLE";
 
     QXmlStreamReader reader(&dictionaryFile);
 
@@ -378,8 +424,8 @@ bool XdxfPlugin::makeCache(QString dir) {
     QSqlQuery cur(db);
     cur.exec("PRAGMA synchronous = 0");
     cur.exec("drop table dict");
-    cur.exec("create table dict(word text ,transl text)");
     QCoreApplication::processEvents();
+    cur.exec("create table dict(word text ,translation text)");
     int counter = 0;
     cur.exec("BEGIN;");
 
@@ -389,12 +435,14 @@ bool XdxfPlugin::makeCache(QString dir) {
     timer.start();
     countWords();
 
+    int lastProg = -1;
+
 
     counter=0;
-    while (!reader.atEnd()) {
+    while (!reader.atEnd() && !stopped) {
 
         QCoreApplication::processEvents();
-        //usleep(50);
+       // usleep(50);
         reader.readNext();
 
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
@@ -403,33 +451,38 @@ bool XdxfPlugin::makeCache(QString dir) {
                 match = true;
             }
         }
-        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();
-                    }
+        if(match) {
+            QString temp("");
+            while(reader.name()!="ar" && !reader.atEnd()) {
+                if(reader.name()!="" && reader.name()!="k") {
+                    if(reader.tokenType()==QXmlStreamReader::EndElement)
+                        temp+=tr("</");
+                    if(reader.tokenType()==QXmlStreamReader::StartElement)
+                        temp+=tr("<");
+                    temp+=reader.name().toString();
+                    if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
+                       temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
+                    temp+=tr(">");
                 }
-                match = false;
-                cur.prepare("insert into dict values(?,?)");
-                cur.addBindValue(a);
-                cur.addBindValue(temp);
-                cur.exec();
-                counter++;
-                int prog = counter*100/_wordsCount;
-                if(prog % 5 == 0)
-                    Q_EMIT updateCachingProgress(prog);
+                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);
+            cur.addBindValue(temp);
+            cur.exec();
+            counter++;
+            int prog = counter*100/_wordsCount;
+            if(prog % 5 == 0 && lastProg != prog) {
+                Q_EMIT updateCachingProgress(prog,
+                                             timer.restart());
+                lastProg = prog;
             }
-
         }
     }
 
-
-    qDebug()<<counter;
     cur.exec("END;");
     cur.exec("select count(*) from dict");