2 #include "BookmarkTranslations.h"
4 Bookmarks::Bookmarks() {
5 dbName = QDir::homePath() + "/.mdictionary/"
7 db = QSqlDatabase::addDatabase("QSQLITE", "bookmarks");
8 db.setDatabaseName(dbName);
14 bool Bookmarks::checkAndCreateDb() {
16 qDebug() << "Database error: " << db.lastError().text() << endl;
20 cur.exec("create table bookmarks(word text ,translation text)");
27 void Bookmarks::clear() {
29 cur.exec("drop table bookmarks");
30 cur.exec("create table bookmarks(key text ,translation text)");
35 void Bookmarks::add(Translation* translation) {
37 cur.prepare("insert into bookmarks values (?,?)");
38 cur.addBindValue(translation->key());
39 cur.addBindValue(translation->toHtml());
45 void Bookmarks::remove(Translation* translation) {
47 cur.prepare("delete from bookmarks where key=? and translation=?");
48 cur.addBindValue(translation->key());
49 cur.addBindValue(translation->key());
55 QList<Translation*> Bookmarks::list() {
57 cur.exec("select distinct key from bookmarks");
58 QList<Translation*> res;
60 res.append(new BookmarkTranslation(cur.value(0).toString(), this));
66 QList<Translation*> Bookmarks::searchWordList(QString word) {
67 if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
69 word = word.replace("*", "%");
70 word = word.replace("?", "_");
71 word = removeAccents(word);
74 cur.prepare("select key from bookmarks where key=?");
75 cur.addBindValue(word);
77 QList<Translation*> res;
79 res.append(new BookmarkTranslation(cur.value(0).toString(), this));
85 QStringList Bookmarks::search(QString word) {
88 cur.prepare("select translation from bookmarks where word=?");
89 cur.addBindValue(word);
92 result << cur.value(0).toString();
98 QString Bookmarks::removeAccents(QString string) {
99 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
100 QString normalized = string.normalized(QString::NormalizationForm_D);
101 normalized = normalized;
102 for(int i=0; i<normalized.size(); i++) {
103 if( !normalized[i].isLetterOrNumber() &&
104 !normalized[i].isSpace() &&
105 !normalized[i].isDigit() &&
106 normalized[i] != '*' &&
107 normalized[i] != '%') {
108 normalized.remove(i,1);
116 bool Bookmarks::inBookmarks(QString word) {
118 cur.prepare("select translation from bookmarks where word=? limit 1");
119 cur.addBindValue(word);