#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;
}
#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.
/*! 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);
*/
QStringList search(QString word);
+
+ /*! clars bookmarks database */
+ void clear();
+
+private:
+ QString dbName;
+ QSqlDatabase db;
+
+ bool checkAndCreateDb();
+ QString removeAccents();
+
};
#endif // BOOKMARKS_H