Merge branch 'cache'
[mdictionary] / trunk / src / base / backbone / Bookmarks.cpp
index bb78627..dfee9c8 100644 (file)
@@ -1,24 +1,30 @@
 #include "Bookmarks.h"
-
+#include "BookmarkTranslations.h"
+#include <QThread>
 
 Bookmarks::Bookmarks() {
-    dbName = QDir::homePath() + "/.mdictionary/"
-                 + "history.db";
-    db = QSqlDatabase::addDatabase("QSQLITE", "history");
-    db.setDatabaseName(dbName);
+    this->dbName = QDir::homePath() + "/.mdictionary/"
+                 + "bookmarks.db";
+    checkAndCreateDb();
 }
 
 
+QSqlDatabase Bookmarks::getDbCnx(QString dbName) {
+    QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE",
+            QString("%2").arg((int)QThread::currentThreadId()));
+    db.setDatabaseName(dbName);
+    return db;
+}
 
 bool Bookmarks::checkAndCreateDb() {
-    stopped = false;
-
-    if(!db.open()) {
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
         qDebug() << "Database error: " << db.lastError().text() << endl;
         return false;
     }
     QSqlQuery cur(db);
-    cur.exec("create table bookmarks(word text ,translation text)");
+    cur.exec("create table bookmarks(key text ,translation text)");
+    db.close();
 
     return true;
 }
@@ -26,80 +32,116 @@ bool Bookmarks::checkAndCreateDb() {
 
 
 void Bookmarks::clear() {
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return ;
+    }
     QSqlQuery cur(db);
     cur.exec("drop table bookmarks");
-    cur.exec("create table bookmarks(word text ,translation text)");
+    cur.exec("create table bookmarks(key text ,translation text)");
+    db.close();
 }
 
 
 
 void Bookmarks::add(Translation* translation) {
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return ;
+    }
+    translation->setBookmark(true);
     QSqlQuery cur(db);
     cur.prepare("insert into bookmarks values (?,?)");
-    cur.addBindValue(translation->key(), translation->key());
+    cur.addBindValue(translation->key());
+    cur.addBindValue(translation->toHtml());
     cur.exec();
+    db.close();
 }
 
 
-
 void Bookmarks::remove(Translation* translation) {
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return ;
+    }
     QSqlQuery cur(db);
-    cur.prepare("delete from bookmarks where key=? and translation=?");
-    cur.addBindValue(translation->key(), translation->key());
+    cur.prepare("delete from bookmarks where key=?");
+    cur.addBindValue(translation->key());
     cur.exec();
+    db.close();
 }
 
 
 
 QList<Translation*> Bookmarks::list() {
-    QSqlQuery cur(db);
-    cur.exec("select key from bookmarks");
     QList<Translation*> res;
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return res;
+    }
+    QSqlQuery cur(db);
+    cur.exec("select distinct key from bookmarks");
     while(cur.next())
-        res.append(new HistoryTranslation(cur.value(0).toString(), this));
+        res.append(new BookmarkTranslation(cur.value(0).toString(), this, dbName));
+    db.close();
     return res;
 }
 
 
 
 QList<Translation*> Bookmarks::searchWordList(QString word) {
-    if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
+
+    if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
         word+="%";
     word = word.replace("*", "%");
     word = word.replace("?", "_");
     word = removeAccents(word);
 
+    QList<Translation*> tr;
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return tr;
+    }
     QSqlQuery cur(db);
-    cur.prepare("select key from bookmarks where key=?");
+    cur.prepare("select key from bookmarks where key like ?");
     cur.addBindValue(word);
     cur.exec();
-    QList<Translation*> res;
+    QSet<QString> res;
     while(cur.next())
-        res.append(new HistoryTranslation(cur.value(0).toString(), this));
-    return res;
+        res.insert(cur.value(0).toString());
+    foreach(QString str, res.toList())
+        tr.append(new BookmarkTranslation(str, this, dbName));
+    db.close();
+    return tr;
 }
 
 
 
-QList<Translation*> Bookmarks::search(QString word) {
+QStringList Bookmarks::search(QString word, QString dbName) {
+    QStringList result;
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return result;
+    }
     QSqlQuery cur(db);
-    cur.prepare("select translation from bookmarks where word=? limit 1");
+    cur.prepare("select translation from bookmarks where key=?");
     cur.addBindValue(word);
     cur.exec();
-    if(cur.next())
-        result = cur.value(0).toString();
+    while(cur.next())
+        result << cur.value(0).toString();
+
+    db.close();
     return result;
 }
 
 
 
-void Bookmarks::clear() {
-    QSqlQuery cur(db);
-    cur.exec("drop table bookmarks");
-    cur.exec("create table bookmarks(word text ,translation text)");
-}
-
-
 QString Bookmarks::removeAccents(QString string) {
     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
     QString normalized = string.normalized(QString::NormalizationForm_D);
@@ -115,3 +157,21 @@ QString Bookmarks::removeAccents(QString string) {
     }
     return normalized;
 }
+
+
+
+bool Bookmarks::inBookmarks(QString word) {
+    QSqlDatabase db = getDbCnx(dbName);
+    if(!db.isOpen() && !db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return false;
+    }
+    QSqlQuery cur(db);
+    cur.prepare("select translation from bookmarks where key like ? limit 1");
+    cur.addBindValue(word);
+    cur.exec();
+    if(cur.next())
+        return true;
+    db.close();
+    return false;
+}