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) {
38 cur.prepare("insert into bookmarks values (?,?)");
39 cur.addBindValue(translation->key());
40 cur.addBindValue(translation->toHtml());
46 void Bookmarks::remove(Translation* translation) {
48 cur.prepare("delete from bookmarks where key=? and translation=?");
49 cur.addBindValue(translation->key());
50 cur.addBindValue(translation->key());
56 QList<Translation*> Bookmarks::list() {
58 cur.exec("select distinct key from bookmarks");
59 QList<Translation*> res;
61 res.append(new BookmarkTranslation(cur.value(0).toString(), this));
67 QList<Translation*> Bookmarks::searchWordList(QString word) {
68 if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
70 word = word.replace("*", "%");
71 word = word.replace("?", "_");
72 word = removeAccents(word);
75 cur.prepare("select key from bookmarks where key=?");
76 cur.addBindValue(word);
78 QList<Translation*> res;
80 res.append(new BookmarkTranslation(cur.value(0).toString(), this));
86 QStringList Bookmarks::search(QString word) {
89 cur.prepare("select translation from bookmarks where word=?");
90 cur.addBindValue(word);
93 result << cur.value(0).toString();
99 QString Bookmarks::removeAccents(QString string) {
100 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
101 QString normalized = string.normalized(QString::NormalizationForm_D);
102 normalized = normalized;
103 for(int i=0; i<normalized.size(); i++) {
104 if( !normalized[i].isLetterOrNumber() &&
105 !normalized[i].isSpace() &&
106 !normalized[i].isDigit() &&
107 normalized[i] != '*' &&
108 normalized[i] != '%') {
109 normalized.remove(i,1);
117 bool Bookmarks::inBookmarks(QString word) {
119 cur.prepare("select translation from bookmarks where word=? limit 1");
120 cur.addBindValue(word);