Merge branch 'cache' of ssh://drop.maemo.org/git/mdictionary into cache
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index 48e3857..0502a85 100644 (file)
@@ -86,10 +86,13 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         }
 
         stopped = false;
-        word = removeAccents(word);
         if(word.indexOf("*")==-1)
             word+="%";
+        qDebug() << word;
         word = word.replace("*", "%");
+        qDebug() << word;
+        word = removeAccents(word);
+        qDebug() << word;
 
         QSqlQuery cur(db);
         cur.prepare("select word from dict where word like ? limit ?");
@@ -156,6 +159,38 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
 }
 
 QString XdxfPlugin::search(QString key) {
+    if(_settings->value("cached") == "true")
+        return searchCache(key);
+    return searchFile(key);
+}
+
+
+
+QString XdxfPlugin::searchCache(QString key) {
+    qDebug() << "search cache";
+    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)) {
@@ -310,7 +345,9 @@ 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.remove(i,1);
         }
     }
@@ -378,8 +415,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;");