add information about bookmark to translation (in toHtml())
[mdictionary] / trunk / src / base / backbone / Bookmarks.cpp
1 #include "Bookmarks.h"
2 #include "BookmarkTranslations.h"
3 #include <QThread>
4
5 Bookmarks::Bookmarks() {
6     this->dbName = QDir::homePath() + "/.mdictionary/"
7                  + "bookmarks.db";
8     checkAndCreateDb();
9 }
10
11
12 QSqlDatabase Bookmarks::getDbCnx(QString dbName) {
13     QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE",
14             QString("%2").arg((int)QThread::currentThreadId()));
15     db.setDatabaseName(dbName);
16     return db;
17 }
18
19 bool Bookmarks::checkAndCreateDb() {
20     QSqlDatabase db = getDbCnx(dbName);
21     if(!db.isOpen() && !db.open()) {
22         qDebug() << "Database error: " << db.lastError().text() << endl;
23         return false;
24     }
25     QSqlQuery cur(db);
26     cur.exec("create table bookmarks(key text ,translation text)");
27     db.close();
28
29     return true;
30 }
31
32
33
34 void Bookmarks::clear() {
35     QSqlDatabase db = getDbCnx(dbName);
36     if(!db.isOpen() && !db.open()) {
37         qDebug() << "Database error: " << db.lastError().text() << endl;
38         return ;
39     }
40     QSqlQuery cur(db);
41     cur.exec("drop table bookmarks");
42     cur.exec("create table bookmarks(key text ,translation text)");
43     db.close();
44 }
45
46
47
48 void Bookmarks::add(Translation* translation) {
49     QSqlDatabase db = getDbCnx(dbName);
50     if(!db.isOpen() && !db.open()) {
51         qDebug() << "Database error: " << db.lastError().text() << endl;
52         return ;
53     }
54     translation->setBookmark(true);
55     QSqlQuery cur(db);
56     cur.prepare("insert into bookmarks values (?,?)");
57     cur.addBindValue(translation->key());
58     cur.addBindValue(translation->toHtml());
59     cur.exec();
60     db.close();
61 }
62
63
64
65 void Bookmarks::remove(Translation* translation) {
66     QSqlDatabase db = getDbCnx(dbName);
67     if(!db.isOpen() && !db.open()) {
68         qDebug() << "Database error: " << db.lastError().text() << endl;
69         return ;
70     }
71     QSqlQuery cur(db);
72     cur.prepare("delete from bookmarks where key=?");
73     cur.addBindValue(translation->key());
74     cur.exec();
75     db.close();
76 }
77
78
79
80 QList<Translation*> Bookmarks::list() {
81     QList<Translation*> res;
82     QSqlDatabase db = getDbCnx(dbName);
83     if(!db.isOpen() && !db.open()) {
84         qDebug() << "Database error: " << db.lastError().text() << endl;
85         return res;
86     }
87     QSqlQuery cur(db);
88     cur.exec("select distinct key from bookmarks");
89     while(cur.next())
90         res.append(new BookmarkTranslation(cur.value(0).toString(), this, dbName));
91     db.close();
92     return res;
93 }
94
95
96
97 QList<Translation*> Bookmarks::searchWordList(QString word) {
98
99     if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
100         word+="%";
101     word = word.replace("*", "%");
102     word = word.replace("?", "_");
103     word = removeAccents(word);
104
105     QList<Translation*> tr;
106     QSqlDatabase db = getDbCnx(dbName);
107     if(!db.isOpen() && !db.open()) {
108         qDebug() << "Database error: " << db.lastError().text() << endl;
109         return tr;
110     }
111     QSqlQuery cur(db);
112     cur.prepare("select key from bookmarks where key like ?");
113     cur.addBindValue(word);
114     cur.exec();
115     QSet<QString> res;
116     while(cur.next())
117         res.insert(cur.value(0).toString());
118     foreach(QString str, res.toList())
119         tr.append(new BookmarkTranslation(str, this, dbName));
120     db.close();
121     return tr;
122 }
123
124
125
126 QStringList Bookmarks::search(QString word, QString dbName) {
127     QStringList result;
128     QSqlDatabase db = getDbCnx(dbName);
129     if(!db.isOpen() && !db.open()) {
130         qDebug() << "Database error: " << db.lastError().text() << endl;
131         return result;
132     }
133     QSqlQuery cur(db);
134     cur.prepare("select translation from bookmarks where key=?");
135     cur.addBindValue(word);
136     cur.exec();
137     while(cur.next())
138         result << cur.value(0).toString();
139
140     db.close();
141     return result;
142 }
143
144
145
146 QString Bookmarks::removeAccents(QString string) {
147     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
148     QString normalized = string.normalized(QString::NormalizationForm_D);
149     normalized = normalized;
150     for(int i=0; i<normalized.size(); i++) {
151         if( !normalized[i].isLetterOrNumber() &&
152             !normalized[i].isSpace() &&
153             !normalized[i].isDigit() &&
154             normalized[i] != '*' &&
155             normalized[i] != '%') {
156             normalized.remove(i,1);
157         }
158     }
159     return normalized;
160 }
161
162
163
164 bool Bookmarks::inBookmarks(QString word) {
165     QSqlDatabase db = getDbCnx(dbName);
166     if(!db.isOpen() && !db.open()) {
167         qDebug() << "Database error: " << db.lastError().text() << endl;
168         return false;
169     }
170     QSqlQuery cur(db);
171     cur.prepare("select translation from bookmarks where key like ? limit 1");
172     cur.addBindValue(word);
173     cur.exec();
174     if(cur.next())
175         return true;
176     db.close();
177     return false;
178 }