add test for searchCache and searchWordListCache
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index 7f1e985..663fafb 100644 (file)
@@ -87,29 +87,37 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         }
 
         stopped = false;
-        if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
-            word+="%";
         word = word.toLower();
         word = word.replace("*", "%");
         word = word.replace("?", "_");
         word = removeAccents(word);
-        qDebug() << word;
+        //qDebug() << word;
 
         QSqlQuery cur(db);
-        cur.prepare("select word from dict where word like ? limit ?");
+        if(limit !=0)
+            cur.prepare("select word from dict where word like ? limit ?");
+        else
+            cur.prepare("select word from dict where word like ?");
         cur.addBindValue(word);
-        cur.addBindValue(limit);
+        if(limit !=0)
+            cur.addBindValue(limit);
         cur.exec();
-        while(cur.next())
-            translations.insert(new TranslationXdxf(
+        while(cur.next()){
+            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));
+        }
         db.close();
        return translations.toList();
 }
 
-
-
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
@@ -141,7 +149,7 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                 bool ok=true;
                 Translation *tran;
                 foreach(tran,translations) {
-                    if(tran->key()==a)
+                    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*/
@@ -166,10 +174,8 @@ QString XdxfPlugin::search(QString key) {
     return searchFile(key);
 }
 
-
-
 QString XdxfPlugin::searchCache(QString key) {
-    QString result;
+    QString result("");
     QString cacheFilePath = _settings->value("cache_path");
     db.setDatabaseName(cacheFilePath);
     key = key.toLower();
@@ -180,19 +186,19 @@ QString XdxfPlugin::searchCache(QString key) {
     }
 
     QSqlQuery cur(db);
-    cur.prepare("select translation from dict where word like ? limit 1");
+//    cur.prepare("select translation from dict where word like ? limit 1");
+    cur.prepare("select translation from dict where word like ?");
     cur.addBindValue(key);
     cur.exec();
-    if(cur.next())
-        result = cur.value(0).toString();
+//  if(cur.next())
+    while(cur.next())
+//      result = cur.value(0).toString();
+        result += cur.value(0).toString();
     db.close();
     return result;
 
 }
 
-
-
-
 QString XdxfPlugin::searchFile(QString key) {
     key = key.toLower();
     QFile dictionaryFile(path);
@@ -213,7 +219,7 @@ QString XdxfPlugin::searchFile(QString key) {
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
             if(reader.name()=="k") {
                 a = reader.readElementText();
-                if(a==key)
+                if(a.toLower()==key.toLower())
                     match = true;
             }
         }
@@ -258,7 +264,6 @@ void XdxfPlugin::setPath(QString path){
     //getDictionaryInfo();
 }
 
-
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
     if(settings){
@@ -271,8 +276,8 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
 
         plugin->db_name = plugin->_settings->value("type")
                + plugin->_settings->value("path");
-        if(!plugin->db.connectionName().isEmpty())
-        plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
+  //      if(!plugin->db.connectionName().isEmpty() || settings->value("generateCache")=="true")
+            plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
 
         if(settings->value("cached").isEmpty() &&
            settings->value("generateCache") == "true") {
@@ -325,7 +330,6 @@ void XdxfPlugin::setSettings(Settings *settings) {
     emit settingsChanged();
 }
 
-
 void XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
@@ -401,8 +405,6 @@ int XdxfPlugin::countWords() {
     return wordsCount;
 }
 
-
-
 bool XdxfPlugin::makeCache(QString dir) {
     cachingDialog->setVisible(true);
     QCoreApplication::processEvents();
@@ -472,7 +474,7 @@ bool XdxfPlugin::makeCache(QString dir) {
                 temp+= reader.text().toString();
                 reader.readNext();
             }
-            temp += tr("<t>") + temp.replace("\n","") + tr("</t>");
+            temp=tr("<t>") + temp.replace("\n","") + tr("</t>");
             match=false;
             cur.prepare("insert into dict values(?,?)");
             cur.addBindValue(a);
@@ -506,5 +508,4 @@ bool XdxfPlugin::makeCache(QString dir) {
     return true;
 }
 
-
 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)