Added exception and event handling: gui & plugin
[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     QSqlQuery cur(db);
55     cur.prepare("insert into bookmarks values (?,?)");
56     cur.addBindValue(translation->key());
57     cur.addBindValue(translation->toHtml());
58     cur.exec();
59     db.close();
60 }
61
62
63 void Bookmarks::remove(Translation* translation) {
64     QSqlDatabase db = getDbCnx(dbName);
65     if(!db.isOpen() && !db.open()) {
66         qDebug() << "Database error: " << db.lastError().text() << endl;
67         return ;
68     }
69     QSqlQuery cur(db);
70     cur.prepare("delete from bookmarks where key=?");
71     cur.addBindValue(translation->key());
72     cur.exec();
73     db.close();
74 }
75
76
77
78 QList<Translation*> Bookmarks::list() {
79     QList<Translation*> res;
80     QSqlDatabase db = getDbCnx(dbName);
81     if(!db.isOpen() && !db.open()) {
82         qDebug() << "Database error: " << db.lastError().text() << endl;
83         return res;
84     }
85     QSqlQuery cur(db);
86     cur.exec("select distinct key from bookmarks");
87     while(cur.next())
88         res.append(new BookmarkTranslation(cur.value(0).toString(), this, dbName));
89     db.close();
90     return res;
91 }
92
93
94
95 QList<Translation*> Bookmarks::searchWordList(QString word) {
96
97     if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
98         word+="%";
99     word = word.replace("*", "%");
100     word = word.replace("?", "_");
101     word = removeAccents(word);
102
103     QList<Translation*> tr;
104     QSqlDatabase db = getDbCnx(dbName);
105     if(!db.isOpen() && !db.open()) {
106         qDebug() << "Database error: " << db.lastError().text() << endl;
107         return tr;
108     }
109     QSqlQuery cur(db);
110     cur.prepare("select key from bookmarks where key like ?");
111     cur.addBindValue(word);
112     cur.exec();
113     QSet<QString> res;
114     while(cur.next())
115         res.insert(cur.value(0).toString());
116     foreach(QString str, res.toList())
117         tr.append(new BookmarkTranslation(str, this, dbName));
118     db.close();
119     return tr;
120 }
121
122
123
124 QStringList Bookmarks::search(QString word, QString dbName) {
125     QStringList result;
126     QSqlDatabase db = getDbCnx(dbName);
127     if(!db.isOpen() && !db.open()) {
128         qDebug() << "Database error: " << db.lastError().text() << endl;
129         return result;
130     }
131     QSqlQuery cur(db);
132     cur.prepare("select translation from bookmarks where key=?");
133     cur.addBindValue(word);
134     cur.exec();
135     while(cur.next())
136         result << cur.value(0).toString();
137
138     db.close();
139     return result;
140 }
141
142
143
144 QString Bookmarks::removeAccents(QString string) {
145     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
146     QString normalized = string.normalized(QString::NormalizationForm_D);
147     normalized = normalized;
148     for(int i=0; i<normalized.size(); i++) {
149         if( !normalized[i].isLetterOrNumber() &&
150             !normalized[i].isSpace() &&
151             !normalized[i].isDigit() &&
152             normalized[i] != '*' &&
153             normalized[i] != '%') {
154             normalized.remove(i,1);
155         }
156     }
157     return normalized;
158 }
159
160
161
162 bool Bookmarks::inBookmarks(QString word) {
163     QSqlDatabase db = getDbCnx(dbName);
164     if(!db.isOpen() && !db.open()) {
165         qDebug() << "Database error: " << db.lastError().text() << endl;
166         return false;
167     }
168     QSqlQuery cur(db);
169     cur.prepare("select translation from bookmarks where key like ? limit 1");
170     cur.addBindValue(word);
171     cur.exec();
172     if(cur.next())
173         return true;
174     db.close();
175     return false;
176 }