refactoring xdxfPlugin
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Wed, 1 Sep 2010 09:32:49 +0000 (11:32 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Wed, 1 Sep 2010 09:32:49 +0000 (11:32 +0200)
trunk/src/plugins/xdxf/src/xdxfplugin.cpp

index 55028a3..fc38b2e 100644 (file)
@@ -109,9 +109,11 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
 
     QSqlQuery cur(db);
     if(limit !=0)
-        cur.prepare("select word from dict where word like ? or normalized like ? limit ?");
+        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.prepare("select word from dict where word like ? or normalized "
+                    "like ?");
     cur.addBindValue(word);
     cur.addBindValue(word);
     if(limit !=0)
@@ -138,17 +140,18 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
     return translations.toList();
 }
 
+
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     QSet<Translation*> translations;
-
     QFile dictionaryFile(_settings->value("path"));
-
     word = word.toLower();
-
     stopped = false;
+
     QRegExp regWord(word);
     regWord.setCaseSensitivity(Qt::CaseInsensitive);
     regWord.setPatternSyntax(QRegExp::Wildcard);
+
+    /*check xdxf file exist*/
     if(!QFile::exists(_settings->value("path"))
                 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
         qDebug()<<"Error: could not open file";
@@ -158,27 +161,29 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     }
 
     QXmlStreamReader reader(&dictionaryFile);
-    /*search words list*/
-    QString a;
+    QString readKey;
     int i=0;
+
+    /*search words list*/
     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(a) || regWord.exactMatch(removeAccents(a))) &&
-                    (i<limit || limit==0)) {
+                readKey = reader.readElementText();
+            if((regWord.exactMatch(readKey)
+                    || regWord.exactMatch(removeAccents(readKey)))
+                    && (i<limit || limit==0)) {
                 bool ok=true;
                 Translation *tran;
                 foreach(tran,translations) {
-                    if(tran->key().toLower()==a.toLower())
-                        ok=false;  /*if key word is in the dictionary more that one */
+                    if(tran->key().toLower()==readKey.toLower())
+                        ok=false; /*if key is in the dictionary more that one */
                 }
                 if(ok) {  /*add key word to list*/
-                    translations<<(new TranslationXdxf(a.toLower(),
-                                _infoNote,this));
+                    translations<<(new TranslationXdxf(readKey.toLower(),
+                                    _infoNote,this));
                     i++;
                 }
                 if(i>=limit && limit!=0)
@@ -229,10 +234,13 @@ QString XdxfPlugin::searchCache(QString key) {
 
 }
 
+
 QString XdxfPlugin::searchFile(QString key) {
-    key = key.toLower();
     QFile dictionaryFile(_settings->value("path"));
     QString resultString("");
+    key = key.toLower();
+
+    /*check xdxf file exist*/
     if(!QFile::exists(_settings->value("path"))
                 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
         Q_EMIT notify(Notify::Warning,
@@ -240,17 +248,19 @@ QString XdxfPlugin::searchFile(QString key) {
         qDebug()<<"Error: could not open file";
         return "";
     }
-    QXmlStreamReader reader(&dictionaryFile);
-    QString a;
 
+    QXmlStreamReader reader(&dictionaryFile);
+    QString readKey;
     bool match =false;
     stopped = false;
+
+    /*search translations for word*/
     while (!reader.atEnd()&& !stopped) {
         reader.readNext();
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
             if(reader.name()=="k") {
-                a = reader.readElementText();
-                if(a.toLower()==key.toLower())
+                readKey = reader.readElementText();
+                if(readKey.toLower()==key.toLower())
                     match = true;
             }
         }
@@ -275,7 +285,7 @@ QString XdxfPlugin::searchFile(QString key) {
             }
             if(temp.at(0)==QChar('\n'))
                 temp.remove(0,1);
-            resultString+="<key>" + a +"</key>";
+            resultString+="<key>" + readKey +"</key>";
             resultString+="<t>" + temp + "</t>";
             match=false;
         }
@@ -283,7 +293,6 @@ QString XdxfPlugin::searchFile(QString key) {
     }
     stopped=false;
     dictionaryFile.close();
-
     return resultString;
 }
 
@@ -449,16 +458,20 @@ int XdxfPlugin::countWords() {
 bool XdxfPlugin::makeCache(QString) {
     cachingDialog->setVisible(true);
     QCoreApplication::processEvents();
-    stopped = false;
     QFileInfo dictFileN(_settings->value("path"));
     QString cachePathN;
+    stopped = false;
+
+    /*create cache file name*/
     int i=0;
     do {
         cachePathN = QDir::homePath() + "/.mdictionary/"
-                 + dictFileN.completeBaseName()+"."+QString::number(i)+ ".cache";
+                                      + dictFileN.completeBaseName()+"."
+                                      +QString::number(i) + ".cache";
         i++;
     } while(QFile::exists(cachePathN));
 
+    /*checke errors (File open and db open)*/
     QFile dictionaryFile(dictFileN.filePath());
     if (!QFile::exists(_settings->value("path"))
                 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
@@ -469,8 +482,6 @@ bool XdxfPlugin::makeCache(QString) {
         return 0;
     }
     QXmlStreamReader reader(&dictionaryFile);
-
-
     db.setDatabaseName(cachePathN);
     if(!db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
@@ -480,6 +491,8 @@ bool XdxfPlugin::makeCache(QString) {
                 "You may want to recache.").arg(name())));
         return false;
     }
+
+    /*inicial sqlQuery*/
     QCoreApplication::processEvents();
     QSqlQuery cur(db);
     cur.exec("PRAGMA synchronous = 0");
@@ -489,27 +502,22 @@ bool XdxfPlugin::makeCache(QString) {
     int counter = 0;
     cur.exec("BEGIN;");
 
-    QString a;
+    QString readKey;
     bool match = false;
     QTime timer;
     timer.start();
     countWords();
-
     int lastProg = -1;
-
-    QString stripAcc = settings()->value("strip_accents");
     settings()->setValue("strip_accents", "true");
-
-
     counter=0;
-    while (!reader.atEnd() && !stopped) {
 
+    /*add all words to db*/
+    while (!reader.atEnd() && !stopped) {
         QCoreApplication::processEvents();
         reader.readNext();
-
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
             if(reader.name()=="k"){
-                a = reader.readElementText();
+                readKey = reader.readElementText();
                 match = true;
             }
         }
@@ -522,38 +530,41 @@ bool XdxfPlugin::makeCache(QString) {
                     if(reader.tokenType()==QXmlStreamReader::StartElement)
                         temp+="<";
                     temp+=reader.name().toString();
-                    if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
-                       temp= temp + " c=\"" + reader.attributes().value("c").toString() + "\"";
+                    if(reader.name().toString()=="c"
+                        && reader.tokenType()==QXmlStreamReader::StartElement) {
+                        temp= temp + " c=\""
+                                   + reader.attributes().value("c").toString()
+                                   + "\"";
+                    }
                     temp+=">";
                 }
-                temp+= reader.text().toString().replace("<","&lt;").replace(">","&gt;");;
+                temp+= reader.text().toString().replace("<","&lt;").replace(">"
+                              ,"&gt;");
                 reader.readNext();
             }
             if(temp.at(0)==QChar('\n'))
                 temp.remove(0,1);
-            temp="<key>" + a + "</key>" + "<t>" + temp+ "</t>";
+            temp="<key>" + readKey + "</key>" + "<t>" + temp+ "</t>";
             match=false;
             cur.prepare("insert into dict values(?,?,?)");
-            cur.addBindValue(a);
-            cur.addBindValue(removeAccents(a));
+            cur.addBindValue(readKey);
+            cur.addBindValue(removeAccents(readKey));
             cur.addBindValue(temp);
             cur.exec();
             counter++;
             int prog = counter*100/_wordsCount;
             if(prog % 5 == 0 && lastProg != prog) {
-                Q_EMIT updateCachingProgress(prog,
-                                             timer.restart());
+                Q_EMIT updateCachingProgress(prog,timer.restart());
                 lastProg = prog;
             }
         }
     }
-
     cur.exec("END;");
     cur.exec("select count(*) from dict");
-
-    countWords();
     cachingDialog->setVisible(false);
 
+    /*checke errors (wrong number of added words)*/
+    countWords();
     if(!cur.next() || countWords() != cur.value(0).toInt()) {
         Q_EMIT updateCachingProgress(100, timer.restart());
         Q_EMIT notify(Notify::Warning,
@@ -561,10 +572,10 @@ bool XdxfPlugin::makeCache(QString) {
         db.close();
         return false;
     }
+
     _settings->setValue("cache_path", cachePathN);
     _settings->setValue("cached", "true");
 
-
     db.close();
     return true;
 }