#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;
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)");
}
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();
}
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();
}
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;
}
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);
}
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;
+}