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