Connected button in word list to add bookmark signal
[mdictionary] / trunk / src / base / backbone / Bookmarks.cpp
1 #include "Bookmarks.h"
2 #include "BookmarkTranslations.h"
3
4 Bookmarks::Bookmarks() {
5     dbName = QDir::homePath() + "/.mdictionary/"
6                  + "bookmarks.db";
7     db = QSqlDatabase::addDatabase("QSQLITE", "bookmarks");
8     db.setDatabaseName(dbName);
9     checkAndCreateDb();
10 }
11
12
13
14 bool Bookmarks::checkAndCreateDb() {
15     if(!db.open()) {
16         qDebug() << "Database error: " << db.lastError().text() << endl;
17         return false;
18     }
19     QSqlQuery cur(db);
20     cur.exec("create table bookmarks(word text ,translation text)");
21
22     return true;
23 }
24
25
26
27 void Bookmarks::clear() {
28     QSqlQuery cur(db);
29     cur.exec("drop table bookmarks");
30     cur.exec("create table bookmarks(key text ,translation text)");
31 }
32
33
34
35 void Bookmarks::add(Translation* translation) {
36     qDebug()<<"added";
37     QSqlQuery cur(db);
38     cur.prepare("insert into bookmarks values (?,?)");
39     cur.addBindValue(translation->key());
40     cur.addBindValue(translation->toHtml());
41     cur.exec();
42 }
43
44
45
46 void Bookmarks::remove(Translation* translation) {
47     QSqlQuery cur(db);
48     cur.prepare("delete from bookmarks where key=? and translation=?");
49     cur.addBindValue(translation->key());
50     cur.addBindValue(translation->key());
51     cur.exec();
52 }
53
54
55
56 QList<Translation*> Bookmarks::list() {
57     QSqlQuery cur(db);
58     cur.exec("select distinct key from bookmarks");
59     QList<Translation*> res;
60     while(cur.next())
61         res.append(new BookmarkTranslation(cur.value(0).toString(), this));
62     return res;
63 }
64
65
66
67 QList<Translation*> Bookmarks::searchWordList(QString word) {
68     if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
69         word+="%";
70     word = word.replace("*", "%");
71     word = word.replace("?", "_");
72     word = removeAccents(word);
73
74     QSqlQuery cur(db);
75     cur.prepare("select key from bookmarks where key=?");
76     cur.addBindValue(word);
77     cur.exec();
78     QList<Translation*> res;
79     while(cur.next())
80         res.append(new BookmarkTranslation(cur.value(0).toString(), this));
81     return res;
82 }
83
84
85
86 QStringList Bookmarks::search(QString word) {
87     QSqlQuery cur(db);
88     QStringList result;
89     cur.prepare("select translation from bookmarks where word=?");
90     cur.addBindValue(word);
91     cur.exec();
92     while(cur.next())
93         result << cur.value(0).toString();
94     return result;
95 }
96
97
98
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);
110         }
111     }
112     return normalized;
113 }
114
115
116
117 bool Bookmarks::inBookmarks(QString word) {
118     QSqlQuery cur(db);
119     cur.prepare("select translation from bookmarks where word=? limit 1");
120     cur.addBindValue(word);
121     cur.exec();
122     if(cur.next())
123         return true;
124     return false;
125 }