Bookamarks adding, removing, searching (basic), listing -> ready
[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     QSqlQuery cur(db);
37     cur.prepare("insert into bookmarks values (?,?)");
38     cur.addBindValue(translation->key());
39     cur.addBindValue(translation->toHtml());
40     cur.exec();
41 }
42
43
44
45 void Bookmarks::remove(Translation* translation) {
46     QSqlQuery cur(db);
47     cur.prepare("delete from bookmarks where key=? and translation=?");
48     cur.addBindValue(translation->key());
49     cur.addBindValue(translation->key());
50     cur.exec();
51 }
52
53
54
55 QList<Translation*> Bookmarks::list() {
56     QSqlQuery cur(db);
57     cur.exec("select distinct key from bookmarks");
58     QList<Translation*> res;
59     while(cur.next())
60         res.append(new BookmarkTranslation(cur.value(0).toString(), this));
61     return res;
62 }
63
64
65
66 QList<Translation*> Bookmarks::searchWordList(QString word) {
67     if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
68         word+="%";
69     word = word.replace("*", "%");
70     word = word.replace("?", "_");
71     word = removeAccents(word);
72
73     QSqlQuery cur(db);
74     cur.prepare("select key from bookmarks where key=?");
75     cur.addBindValue(word);
76     cur.exec();
77     QList<Translation*> res;
78     while(cur.next())
79         res.append(new BookmarkTranslation(cur.value(0).toString(), this));
80     return res;
81 }
82
83
84
85 QStringList Bookmarks::search(QString word) {
86     QSqlQuery cur(db);
87     QStringList result;
88     cur.prepare("select translation from bookmarks where word=?");
89     cur.addBindValue(word);
90     cur.exec();
91     while(cur.next())
92         result << cur.value(0).toString();
93     return result;
94 }
95
96
97
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);
109         }
110     }
111     return normalized;
112 }
113
114
115
116 bool Bookmarks::inBookmarks(QString word) {
117     QSqlQuery cur(db);
118     cur.prepare("select translation from bookmarks where word=? limit 1");
119     cur.addBindValue(word);
120     cur.exec();
121     if(cur.next())
122         return true;
123     return false;
124 }