Merge branch 'bookmarks' of ssh://drop.maemo.org/git/mdictionary into bookmarks
[mdictionary] / trunk / src / base / backbone / Bookmarks.cpp
1 #include "Bookmarks.h"
2 #include "BookmarkTranslations.h"
3
4 Bookmarks::Bookmarks() {
5     dbName = QDir::homePath() + "/.mdictionary/"
6                  + "bookmarks.db";
7     db = QSqlDatabase::addDatabase("QSQLITE", "bookmarks");
8     db.setDatabaseName(dbName);
9     checkAndCreateDb();
10 }
11
12
13
14 bool Bookmarks::checkAndCreateDb() {
15     if(!db.open()) {
16         qDebug() << "Database error: " << db.lastError().text() << endl;
17         return false;
18     }
19     QSqlQuery cur(db);
20     cur.exec("create table bookmarks(key text ,translation text)");
21
22     return true;
23 }
24
25
26
27 void Bookmarks::clear() {
28     QSqlQuery cur(db);
29     cur.exec("drop table bookmarks");
30     cur.exec("create table bookmarks(key text ,translation text)");
31 }
32
33
34
35 void Bookmarks::add(Translation* translation) {
36     qDebug()<<"added";
37     QSqlQuery cur(db);
38     cur.prepare("insert into bookmarks values (?,?)");
39     cur.addBindValue(translation->key());
40     cur.addBindValue(translation->toHtml());
41     cur.exec();
42 }
43
44
45
46 void Bookmarks::remove(Translation* translation) {
47     QSqlQuery cur(db);
48     cur.prepare("delete from bookmarks where key=? and translation=?");
49     cur.addBindValue(translation->key());
50     cur.addBindValue(translation->key());
51     cur.exec();
52 }
53
54
55
56 QList<Translation*> Bookmarks::list() {
57     QSqlQuery cur(db);
58     cur.exec("select distinct key from bookmarks");
59     QList<Translation*> res;
60     while(cur.next())
61         res.append(new BookmarkTranslation(cur.value(0).toString(), this));
62     return res;
63 }
64
65
66
67 QList<Translation*> Bookmarks::searchWordList(QString word) {
68
69     if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
70         word+="%";
71     word = word.replace("*", "%");
72     word = word.replace("?", "_");
73     word = removeAccents(word);
74
75     QSqlQuery cur(db);
76     cur.prepare("select key from bookmarks where key like ?");
77     cur.addBindValue(word);
78     cur.exec();
79     QSet<QString> res;
80     while(cur.next())
81         res.insert(cur.value(0).toString());
82     qDebug() << "searchWordList " << res.size();
83     QList<Translation*> tr;
84     foreach(QString str, res.toList())
85         tr.append(new BookmarkTranslation(str, this));
86     return tr;
87 }
88
89
90
91 QStringList Bookmarks::search(QString word) {
92     qDebug() << "bookmarks::search";
93     QSqlQuery cur(db);
94     QStringList result;
95     cur.prepare("select translation from bookmarks where key=?");
96     cur.addBindValue(word);
97     cur.exec();
98     while(cur.next())
99         result << cur.value(0).toString();
100
101     qDebug() << result.size() << " " << result;
102     return result;
103 }
104
105
106
107 QString Bookmarks::removeAccents(QString string) {
108     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
109     QString normalized = string.normalized(QString::NormalizationForm_D);
110     normalized = normalized;
111     for(int i=0; i<normalized.size(); i++) {
112         if( !normalized[i].isLetterOrNumber() &&
113             !normalized[i].isSpace() &&
114             !normalized[i].isDigit() &&
115             normalized[i] != '*' &&
116             normalized[i] != '%') {
117             normalized.remove(i,1);
118         }
119     }
120     return normalized;
121 }
122
123
124
125 bool Bookmarks::inBookmarks(QString word) {
126     QSqlQuery cur(db);
127     cur.prepare("select translation from bookmarks where key like ? limit 1");
128     cur.addBindValue(word);
129     cur.exec();
130     if(cur.next())
131         return true;
132     return false;
133 }