Merged with stashed state
authorBartosz Szatkowski <bulislaw@linux.com>
Mon, 16 Aug 2010 10:37:24 +0000 (12:37 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Mon, 16 Aug 2010 10:37:24 +0000 (12:37 +0200)
trunk/src/base/backbone/Bookmarks.cpp
trunk/src/base/backbone/Bookmarks.h
trunk/src/plugins/xdxf/src/xdxfplugin.cpp

index 09516b7..bb78627 100644 (file)
@@ -1,4 +1,117 @@
 #include "Bookmarks.h"
-Bookmarks::Bookmarks()
-{
+
+
+Bookmarks::Bookmarks() {
+    dbName = QDir::homePath() + "/.mdictionary/"
+                 + "history.db";
+    db = QSqlDatabase::addDatabase("QSQLITE", "history");
+    db.setDatabaseName(dbName);
+}
+
+
+
+bool Bookmarks::checkAndCreateDb() {
+    stopped = false;
+
+    if(!db.open()) {
+        qDebug() << "Database error: " << db.lastError().text() << endl;
+        return false;
+    }
+    QSqlQuery cur(db);
+    cur.exec("create table bookmarks(word text ,translation text)");
+
+    return true;
+}
+
+
+
+void Bookmarks::clear() {
+    QSqlQuery cur(db);
+    cur.exec("drop table bookmarks");
+    cur.exec("create table bookmarks(word text ,translation text)");
+}
+
+
+
+void Bookmarks::add(Translation* translation) {
+    QSqlQuery cur(db);
+    cur.prepare("insert into bookmarks values (?,?)");
+    cur.addBindValue(translation->key(), translation->key());
+    cur.exec();
+}
+
+
+
+void Bookmarks::remove(Translation* translation) {
+    QSqlQuery cur(db);
+    cur.prepare("delete from bookmarks where key=? and translation=?");
+    cur.addBindValue(translation->key(), translation->key());
+    cur.exec();
+}
+
+
+
+QList<Translation*> Bookmarks::list() {
+    QSqlQuery cur(db);
+    cur.exec("select key from bookmarks");
+    QList<Translation*> res;
+    while(cur.next())
+        res.append(new HistoryTranslation(cur.value(0).toString(), this));
+    return res;
+}
+
+
+
+QList<Translation*> Bookmarks::searchWordList(QString word) {
+    if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
+        word+="%";
+    word = word.replace("*", "%");
+    word = word.replace("?", "_");
+    word = removeAccents(word);
+
+    QSqlQuery cur(db);
+    cur.prepare("select key from bookmarks where key=?");
+    cur.addBindValue(word);
+    cur.exec();
+    QList<Translation*> res;
+    while(cur.next())
+        res.append(new HistoryTranslation(cur.value(0).toString(), this));
+    return res;
+}
+
+
+
+QList<Translation*> Bookmarks::search(QString word) {
+    QSqlQuery cur(db);
+    cur.prepare("select translation from bookmarks where word=? limit 1");
+    cur.addBindValue(word);
+    cur.exec();
+    if(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);
+    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;
 }
index e996135..22817c3 100644 (file)
 #ifndef BOOKMARKS_H
 #define BOOKMARKS_H
 
+#include <QString>
+#include <QVariant>
+#include <QStringList>
+#include <QList>
+#include <QSqlDatabase>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QDir>
 #include "../../includes/settings.h"
 #include "../../includes/translation.h"
+#include "HistoryTranslation.h"
 
 /*! Bookmarks are way to store words that You think You will need to search
   for often.
@@ -62,7 +71,7 @@ public:
 
     /*! search in bookmarks for given word (wildcards may apply '*' and '?')
       \param word to search for
-      \return list of matching translation object (word and translation)
+      \return list of matching Translation objects
       */
     QList<Translation*> searchWordList(QString word);
 
@@ -73,6 +82,17 @@ public:
       */
     QStringList search(QString word);
 
+
+    /*! clars bookmarks database */
+    void clear();
+
+private:
+    QString dbName;
+    QSqlDatabase db;
+
+    bool checkAndCreateDb();
+    QString removeAccents();
+
 };
 
 #endif // BOOKMARKS_H
index 5c75498..8497d1d 100644 (file)
@@ -338,7 +338,6 @@ void XdxfPlugin::getDictionaryInfo() {
 }
 
 QString XdxfPlugin::removeAccents(QString string) {
-
     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
     QString normalized = string.normalized(QString::NormalizationForm_D);
     normalized = normalized;