Merge branch 'cache'
[mdictionary] / trunk / src / base / backbone / Bookmarks.cpp
index 09516b7..dfee9c8 100644 (file)
@@ -1,4 +1,177 @@
 #include "Bookmarks.h"
-Bookmarks::Bookmarks()
-{
+#include "BookmarkTranslations.h"
+#include <QThread>
+
+Bookmarks::Bookmarks() {
+    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() {
+    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(key text ,translation text)");
+    db.close();
+
+    return true;
+}
+
+
+
+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(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());
+    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=?");
+    cur.addBindValue(translation->key());
+    cur.exec();
+    db.close();
+}
+
+
+
+QList<Translation*> Bookmarks::list() {
+    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 BookmarkTranslation(cur.value(0).toString(), this, dbName));
+    db.close();
+    return res;
+}
+
+
+
+QList<Translation*> Bookmarks::searchWordList(QString word) {
+
+    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 like ?");
+    cur.addBindValue(word);
+    cur.exec();
+    QSet<QString> res;
+    while(cur.next())
+        res.insert(cur.value(0).toString());
+    foreach(QString str, res.toList())
+        tr.append(new BookmarkTranslation(str, this, dbName));
+    db.close();
+    return tr;
+}
+
+
+
+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 key=?");
+    cur.addBindValue(word);
+    cur.exec();
+    while(cur.next())
+        result << cur.value(0).toString();
+
+    db.close();
+    return result;
+}
+
+
+
+QString Bookmarks::removeAccents(QString string) {
+    string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
+    QString normalized = string.normalized(QString::NormalizationForm_D);
+    normalized = normalized;
+    for(int i=0; i<normalized.size(); i++) {
+        if( !normalized[i].isLetterOrNumber() &&
+            !normalized[i].isSpace() &&
+            !normalized[i].isDigit() &&
+            normalized[i] != '*' &&
+            normalized[i] != '%') {
+            normalized.remove(i,1);
+        }
+    }
+    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;
 }