Bookamarks adding, removing, searching (basic), listing -> ready
authorBartosz Szatkowski <bulislaw@linux.com>
Mon, 16 Aug 2010 13:14:05 +0000 (15:14 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Mon, 16 Aug 2010 13:14:05 +0000 (15:14 +0200)
mdictionary.pro
trunk/src/base/backbone/BookmarkTranslations.h
trunk/src/base/backbone/Bookmarks.cpp
trunk/src/base/backbone/Bookmarks.h
trunk/src/base/backbone/backbone.cpp
trunk/src/base/backbone/backbone.h
trunk/src/base/base.pro
trunk/src/base/gui/MainWindow.cpp

index c54e852..912067e 100644 (file)
@@ -4,7 +4,7 @@ isEmpty( ISQT4 ) {
 error("Use the qmake include with Qt4.4 or greater, on Debian that is qmake-qt4");
 }
 
-
+QT += sql
 TEMPLATE = subdirs
 SUBDIRS  = trunk
 
index 0226f6a..5848390 100644 (file)
 
 #include "../../includes/settings.h"
 #include "../../includes/translation.h"
+#include "Bookmarks.h"
 
-class Bookmarks;
 
 
 class BookmarkTranslation : public Translation
 {
 public:
-    TranslationXdxf();
-    TranslationXdxf(QString key, Bookmarks* bookmarks) {
+    BookmarkTranslation(QString key, Bookmarks* bookmarks) {
         _key = key;
         _dictionaryInfo = "Bookmarks";
         _bookmarks = bookmarks;
@@ -57,7 +56,12 @@ public:
 
     //! \return parsed raw format into html
     QString toHtml() const {
-        return _bookmarks->search(_key);
+        QStringList list = _bookmarks->search(_key);
+        QString result;
+        foreach(QString translation, list)
+            result += translation;
+        return result;
+
     }
 
     /*! sets the word for which we want to find a translation
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;
+}
index 22817c3..eeaa594 100644 (file)
 #ifndef BOOKMARKS_H
 #define BOOKMARKS_H
 
+#include <QtSql>
 #include <QString>
 #include <QVariant>
 #include <QStringList>
 #include <QList>
-#include <QSqlDatabase>
 #include <QSqlQuery>
+#include <QSqlDatabase>
 #include <QSqlError>
 #include <QDir>
+#include <QDebug>
 #include "../../includes/settings.h"
 #include "../../includes/translation.h"
-#include "HistoryTranslation.h"
+class BookmarkTranslation;
+
 
 /*! Bookmarks are way to store words that You think You will need to search
   for often.
@@ -86,12 +89,18 @@ public:
     /*! clars bookmarks database */
     void clear();
 
+
+    /*! \return true if given word is already in bookmarks
+      \param word to check
+      */
+    bool inBookmarks(QString word);
+
 private:
     QString dbName;
     QSqlDatabase db;
 
     bool checkAndCreateDb();
-    QString removeAccents();
+    QString removeAccents(QString);
 
 };
 
index 6270791..f15e1cd 100644 (file)
@@ -72,6 +72,8 @@ void Backbone::init() {
    connect(&_resultWatcher, SIGNAL(finished()), this, SLOT(translationReady()));
    connect(&_htmlResultWatcher, SIGNAL(finished()), this,
            SLOT(htmlTranslationReady()));
+   connect(&_bookmarkWatcher, SIGNAL(finished()), this,
+           SLOT(bookmarksListReady()));
 
    QThreadPool::globalInstance()->setMaxThreadCount(
            QThreadPool::globalInstance()->maxThreadCount()+1);
@@ -452,3 +454,10 @@ QList<CommonDictInterface*> Backbone::activeDicts() {
     return res;
 
 }
+
+
+
+void Backbone::bookmarksListReady() {
+   _bookmarksResult = _innerBookmarks.result();
+   Q_EMIT bookmarksReady();
+}
index 142d729..d92fa50 100644 (file)
@@ -47,6 +47,7 @@
 #include "../../includes/settings.h"
 #include "../../includes/translation.h"
 #include "../../includes/History.h"
+#include "Bookmarks.h"
 
 
 /*! Inner part of dictionary - glues together GUI and plugins
@@ -138,8 +139,43 @@ public Q_SLOTS:
       */
     void searchHtml(QList<Translation*>);
 
-    // TODO addToBookmark(Translation*);
-    // TODO removeFromBookmark(Translation*);
+
+    /*! add bookmarks to given translations (translation object is fetched and
+      added to bookmarks data base (key and translation stored in db)
+      \param translation translation object  to be stored in db
+      */
+    void addBookmark(QList<Translation*> translations) {
+        foreach(Translation* translation, translations)
+            QtConcurrent::run(&bookmarks, &Bookmarks::add, translation);
+    }
+
+
+    /*! Remove bookmarks to given translatios
+      \param translation remove bookmark to this translation
+      */
+    void removeBookmark(QList<Translation*> translations) {
+        foreach(Translation* translation, translations)
+            bookmarks.remove(translation);
+    }
+
+
+   /*! Searching for list of bookmarks may take some time, so i moved it to
+       new thread (to avoid gui blocking), when ready bookmarksReady is emited
+       and result is returned after calling getBookmarks()
+       */
+   void fetchBookmarks() {
+       _bookmarksResult.clear();
+       _innerBookmarks = QtConcurrent::run(bookmarks, &Bookmarks::list);
+       _bookmarkWatcher.setFuture(_innerBookmarks);
+   }
+
+   /*! \return list of all bookmarks
+     */
+   QList<Translation*> getBookmarks() {
+       return _bookmarksResult;
+   }
+
+
 
 Q_SIGNALS:
     /*! emmited when backbone is ready to close - after getting stop signal it
@@ -155,6 +191,11 @@ Q_SIGNALS:
     //! throwed when searches are stopped
     void searchCanceled();
 
+    //! emmited when bookmark list is ready to fetch
+    void bookmarksReady();
+
+private Q_SLOTS:
+    void bookmarksListReady();
 
 
 private:
@@ -162,19 +203,23 @@ private:
     QList<CommonDictInterface*> _plugins;
     QFuture<QList<Translation*> > _innerResult;
     QFuture<QString> _innerHtmlResult;
+    QFuture<QList<Translation*> > _innerBookmarks;
     QMultiHash<QString, Translation*> _result;
     QStringList _htmlResult;
+    QList<Translation*> _bookmarksResult;
     //QTime _time;
     QString _pluginPath, _defaultPluginPath;
     QString _configPath;
     QString _defaultConfigPath;
     QFutureWatcher<QList<Translation*> > _resultWatcher;
+    QFutureWatcher<QList<Translation*> > _bookmarkWatcher;
     QFutureWatcher<QString> _htmlResultWatcher;
     int _searchLimit, _defaultSearchLimit;
     int _activeSearchNum;
     int _historyLen, _defaultHistoryLen;
     bool dryRun;
     bool stopped;
+    Bookmarks bookmarks;
 
 
     void init();
index 02bd90d..2a02ca7 100644 (file)
@@ -4,7 +4,7 @@
 #
 #-------------------------------------------------
 
-QT       += core gui
+QT       += core gui sql
 
 maemo5 {
     QT += maemo5
index c4e3958..3948a18 100644 (file)
@@ -324,6 +324,11 @@ void MainWindow::connectWordList() {
     connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
             this, SIGNAL(searchTranslations(QList<Translation*>)));
 
+    //TODO TEMP
+    connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
+            backbone, SLOT(addBookmark(QList<Translation*>)));
+
+
 
     connect(this, SIGNAL(setBusy()),
             wordListWidget, SLOT(lockList()));