Merged with stashed state
[mdictionary] / trunk / src / base / backbone / Bookmarks.cpp
1 #include "Bookmarks.h"
2
3
4 Bookmarks::Bookmarks() {
5     dbName = QDir::homePath() + "/.mdictionary/"
6                  + "history.db";
7     db = QSqlDatabase::addDatabase("QSQLITE", "history");
8     db.setDatabaseName(dbName);
9 }
10
11
12
13 bool Bookmarks::checkAndCreateDb() {
14     stopped = false;
15
16     if(!db.open()) {
17         qDebug() << "Database error: " << db.lastError().text() << endl;
18         return false;
19     }
20     QSqlQuery cur(db);
21     cur.exec("create table bookmarks(word text ,translation text)");
22
23     return true;
24 }
25
26
27
28 void Bookmarks::clear() {
29     QSqlQuery cur(db);
30     cur.exec("drop table bookmarks");
31     cur.exec("create table bookmarks(word text ,translation text)");
32 }
33
34
35
36 void Bookmarks::add(Translation* translation) {
37     QSqlQuery cur(db);
38     cur.prepare("insert into bookmarks values (?,?)");
39     cur.addBindValue(translation->key(), translation->key());
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(), translation->key());
49     cur.exec();
50 }
51
52
53
54 QList<Translation*> Bookmarks::list() {
55     QSqlQuery cur(db);
56     cur.exec("select key from bookmarks");
57     QList<Translation*> res;
58     while(cur.next())
59         res.append(new HistoryTranslation(cur.value(0).toString(), this));
60     return res;
61 }
62
63
64
65 QList<Translation*> Bookmarks::searchWordList(QString word) {
66     if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
67         word+="%";
68     word = word.replace("*", "%");
69     word = word.replace("?", "_");
70     word = removeAccents(word);
71
72     QSqlQuery cur(db);
73     cur.prepare("select key from bookmarks where key=?");
74     cur.addBindValue(word);
75     cur.exec();
76     QList<Translation*> res;
77     while(cur.next())
78         res.append(new HistoryTranslation(cur.value(0).toString(), this));
79     return res;
80 }
81
82
83
84 QList<Translation*> Bookmarks::search(QString word) {
85     QSqlQuery cur(db);
86     cur.prepare("select translation from bookmarks where word=? limit 1");
87     cur.addBindValue(word);
88     cur.exec();
89     if(cur.next())
90         result = cur.value(0).toString();
91     return result;
92 }
93
94
95
96 void Bookmarks::clear() {
97     QSqlQuery cur(db);
98     cur.exec("drop table bookmarks");
99     cur.exec("create table bookmarks(word text ,translation text)");
100 }
101
102
103 QString Bookmarks::removeAccents(QString string) {
104     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
105     QString normalized = string.normalized(QString::NormalizationForm_D);
106     normalized = normalized;
107     for(int i=0; i<normalized.size(); i++) {
108         if( !normalized[i].isLetterOrNumber() &&
109             !normalized[i].isSpace() &&
110             !normalized[i].isDigit() &&
111             normalized[i] != '*' &&
112             normalized[i] != '%') {
113             normalized.remove(i,1);
114         }
115     }
116     return normalized;
117 }