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