Changed repo structure
[mdictionary] / src / mdictionary / 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     db.open();
25     QSqlQuery cur(db);
26     cur.exec("create table bookmarks(key text, normalized text, translation text)");
27     db.close();
28     return true;
29 }
30
31
32
33 void Bookmarks::clear() {
34     checkAndCreateDb();
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, normalized text,translation text)");
43     db.close();
44 }
45
46
47
48 void Bookmarks::add(Translation* translation) {
49     checkAndCreateDb();
50     QSqlDatabase db = getDbCnx(dbName);
51     if(!db.isOpen() && !db.open()) {
52         qDebug() << "Database error: " << db.lastError().text() << endl;
53         return ;
54     }
55     translation->setBookmark(true);
56     QSqlQuery cur(db);
57     cur.prepare("insert into bookmarks values (?,?,?)");
58     cur.addBindValue(translation->key());
59     cur.addBindValue(removeAccents(translation->key()));
60     cur.addBindValue(translation->toHtml());
61     cur.exec();
62     db.close();
63 }
64
65
66 void Bookmarks::remove(Translation* translation) {
67     checkAndCreateDb();
68     QSqlDatabase db = getDbCnx(dbName);
69     if(!db.isOpen() && !db.open()) {
70         qDebug() << "Database error: " << db.lastError().text() << endl;
71         return ;
72     }
73     QSqlQuery cur(db);
74     cur.prepare("delete from bookmarks where key=?");
75     cur.addBindValue(translation->key());
76     cur.exec();
77     db.close();
78 }
79
80
81
82 QList<Translation*> Bookmarks::list() {
83     checkAndCreateDb();
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     checkAndCreateDb();
102     if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
103         word+="%";
104     word = word.replace("*", "%");
105     word = word.replace("?", "_");
106     qDebug() << word;
107
108     QList<Translation*> tr;
109     QSqlDatabase db = getDbCnx(dbName);
110     if(!db.isOpen() && !db.open()) {
111         qDebug() << "Database error: " << db.lastError().text() << endl;
112         return tr;
113     }
114     QSqlQuery cur(db);
115     cur.prepare("select key from bookmarks where key like ? or normalized like ?");
116     cur.addBindValue(word);
117     cur.addBindValue(word);
118     cur.exec();
119     QSet<QString> res;
120     while(cur.next())
121         res.insert(cur.value(0).toString());
122     foreach(QString str, res.toList())
123         tr.append(new BookmarkTranslation(str, this, dbName));
124     db.close();
125     return tr;
126 }
127
128
129
130 QStringList Bookmarks::search(QString word, QString dbName) {
131     checkAndCreateDb();
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     checkAndCreateDb();
154     QSqlDatabase db = getDbCnx(dbName);
155     if(!db.isOpen() && !db.open()) {
156         qDebug() << "Database error: " << db.lastError().text() << endl;
157         return false;
158     }
159     QSqlQuery cur(db);
160     cur.prepare("select translation from bookmarks where key like ? limit 1");
161     cur.addBindValue(word);
162     cur.exec();
163     if(cur.next())
164         return true;
165     db.close();
166     return false;
167 }