Fixed crashes and db error connected with threads
authorBartosz Szatkowski <bulislaw@linux.com>
Fri, 13 Aug 2010 08:59:37 +0000 (10:59 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Fri, 13 Aug 2010 08:59:37 +0000 (10:59 +0200)
trunk/src/plugins/xdxf/src/XdxfLoadDialog.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.h

index a63b258..5b2aea2 100644 (file)
@@ -95,10 +95,10 @@ Settings* XdxfLoadDialog::getSettings(QWidget *parent) {
     if(loadDialog.exec()==QDialog::Accepted) {
         settings->setValue("path", loadDialog.dicitonaryFilePath());
         if(loadDialog.generateCache()) {
-            settings->setValue("cache", "true");
+            settings->setValue("cached", "true");
         }
         else {
-            settings->setValue("cache", "false");
+            settings->setValue("cached", "false");
         }
 
         return settings;
index 4cfe266..188ea06 100644 (file)
@@ -77,10 +77,9 @@ 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;
+            qDebug() << "Database error" << db.lastError().text() << endl;
             return searchWordListFile(word, limit);
         }
 
@@ -90,7 +89,7 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
             word+="%";
         word = word.replace("*", "%");
 
-        QSqlQuery cur;
+        QSqlQuery cur(db);
         cur.prepare("select word from dict where word like ? limit ?");
         cur.addBindValue(word);
         cur.addBindValue(limit);
@@ -221,8 +220,11 @@ 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->makeCache("");
+        //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("");
     }
     return  plugin;
 }
@@ -332,7 +334,7 @@ int XdxfPlugin::countWords() {
 bool XdxfPlugin::makeCache(QString dir) {
     QFileInfo dictFileN(_settings->value("path"));
     QString cachePathN;
-    cachePathN = dictFileN.dir().absolutePath() + "/"
+    cachePathN = QDir::homePath() + "/.mdictionary/"
                  + dictFileN.completeBaseName() + ".cache";
 
     QFile dictionaryFile(dictFileN.filePath());
@@ -346,14 +348,13 @@ bool XdxfPlugin::makeCache(QString dir) {
 
     QXmlStreamReader reader(&dictionaryFile);
 
-    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
 
     db.setDatabaseName(cachePathN);
     if(!db.open()) {
         qDebug() << "Database error" << endl;
         return false;
     }
-    QSqlQuery cur;
+    QSqlQuery cur(db);
     cur.exec("PRAGMA synchronous = 0");
     cur.exec("drop table dict");
     cur.exec("create table dict(word text ,transl text)");
@@ -406,11 +407,8 @@ bool XdxfPlugin::makeCache(QString dir) {
     qDebug()<<counter;
     cur.exec("END;");
     cur.exec("select count(*) from dict");
-    if(!cur.next() || countWords() != cur.value(0).toInt()) {
-        qDebug() << countWords() << " " << cur.value(0).toInt();
-        qDebug() << "ŻLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
+    if(!cur.next() || countWords() != cur.value(0).toInt())
         return false;
-    }
     _settings->setValue("cache_path", cachePathN);
     _settings->setValue("cached", "true");
     return true;
index a309b14..1d609b1 100644 (file)
@@ -29,6 +29,7 @@
 #include <QTime>
 #include <QSqlQuery>
 #include <QSqlDatabase>
+#include <QSqlError>
 #include "XdxfDictDialog.h"
 
 class TranslationXdxf;
@@ -139,6 +140,8 @@ private:
     QString path;
     uint _hash;
     QIcon _icon;
+    QSqlDatabase db;
+    QString db_name;
 
     //! number of words in dicrionary
     long _wordsCount;