Merge branch 'cache' of ssh://drop.maemo.org/git/mdictionary into cache
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index 84a9879..071dcd5 100644 (file)
@@ -76,12 +76,37 @@ QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
 
 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
 
+    qDebug() << "search cache";
+    QSet<Translation*> translations;
+    QString cacheFilePath = _settings->value("cache_path");
+    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
+        db.setDatabaseName(cacheFilePath);
+        if(!db.open()) {
+            qDebug() << "Database error" << endl;
+            return searchWordListFile(word, limit);
+        }
 
+        stopped = false;
+        word = removeAccents(word);
+        if(word.indexOf("*")==-1)
+            word+="%";
+        word = word.replace("*", "%");
+
+        QSqlQuery cur;
+        cur.prepare("select word from dict where word like ? limit ?");
+        cur.addBindValue(word);
+        cur.addBindValue(limit);
+        cur.exec();
+        while(cur.next())
+            translations.insert(new TranslationXdxf(cur.value(0).toString(),
+                                                    _infoNote, this));
+        return translations.toList();
 }
 
 
 
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
+    qDebug() << "search file";
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
 
@@ -198,7 +223,8 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
         QStringList list = settings->keys();
         foreach(QString key, list)
             plugin->settings()->setValue(key, settings->value(key));
-        plugin->makeCache("");
+        if(plugin->settings()->value("cached") != "true")
+            plugin->makeCache("");
     }
     return  plugin;
 }
@@ -392,6 +418,8 @@ bool XdxfPlugin::makeCache(QString dir) {
         qDebug() << "ŻLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
         return false;
     }
+    _settings->setValue("cache_path", cachePathN);
+    _settings->setValue("cached", "true");
     return true;
 }