Bookamarks adding, removing, searching (basic), listing -> ready
[mdictionary] / trunk / src / base / backbone / Bookmarks.cpp
index bb78627..c90edc0 100644 (file)
@@ -1,18 +1,17 @@
 #include "Bookmarks.h"
-
+#include "BookmarkTranslations.h"
 
 Bookmarks::Bookmarks() {
     dbName = QDir::homePath() + "/.mdictionary/"
-                 + "history.db";
-    db = QSqlDatabase::addDatabase("QSQLITE", "history");
+                 + "bookmarks.db";
+    db = QSqlDatabase::addDatabase("QSQLITE", "bookmarks");
     db.setDatabaseName(dbName);
+    checkAndCreateDb();
 }
 
 
 
 bool Bookmarks::checkAndCreateDb() {
-    stopped = false;
-
     if(!db.open()) {
         qDebug() << "Database error: " << db.lastError().text() << endl;
         return false;
@@ -28,7 +27,7 @@ bool Bookmarks::checkAndCreateDb() {
 void Bookmarks::clear() {
     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)");
 }
 
 
@@ -36,7 +35,8 @@ void Bookmarks::clear() {
 void Bookmarks::add(Translation* translation) {
     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();
 }
 
@@ -45,7 +45,8 @@ void Bookmarks::add(Translation* translation) {
 void Bookmarks::remove(Translation* translation) {
     QSqlQuery cur(db);
     cur.prepare("delete from bookmarks where key=? and translation=?");
-    cur.addBindValue(translation->key(), translation->key());
+    cur.addBindValue(translation->key());
+    cur.addBindValue(translation->key());
     cur.exec();
 }
 
@@ -53,10 +54,10 @@ void Bookmarks::remove(Translation* translation) {
 
 QList<Translation*> Bookmarks::list() {
     QSqlQuery cur(db);
-    cur.exec("select key from bookmarks");
+    cur.exec("select distinct key from bookmarks");
     QList<Translation*> res;
     while(cur.next())
-        res.append(new HistoryTranslation(cur.value(0).toString(), this));
+        res.append(new BookmarkTranslation(cur.value(0).toString(), this));
     return res;
 }
 
@@ -75,31 +76,25 @@ QList<Translation*> Bookmarks::searchWordList(QString word) {
     cur.exec();
     QList<Translation*> res;
     while(cur.next())
-        res.append(new HistoryTranslation(cur.value(0).toString(), this));
+        res.append(new BookmarkTranslation(cur.value(0).toString(), this));
     return res;
 }
 
 
 
-QList<Translation*> Bookmarks::search(QString word) {
+QStringList Bookmarks::search(QString word) {
     QSqlQuery cur(db);
-    cur.prepare("select translation from bookmarks where word=? limit 1");
+    QStringList result;
+    cur.prepare("select translation from bookmarks where word=?");
     cur.addBindValue(word);
     cur.exec();
-    if(cur.next())
-        result = cur.value(0).toString();
+    while(cur.next())
+        result << cur.value(0).toString();
     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 +110,15 @@ QString Bookmarks::removeAccents(QString string) {
     }
     return normalized;
 }
+
+
+
+bool Bookmarks::inBookmarks(QString word) {
+    QSqlQuery cur(db);
+    cur.prepare("select translation from bookmarks where word=? limit 1");
+    cur.addBindValue(word);
+    cur.exec();
+    if(cur.next())
+        return true;
+    return false;
+}