2 #include "BookmarkTranslations.h"
4 Bookmarks::Bookmarks() {
5 dbName = QDir::homePath() + "/.mdictionary/"
7 db = QSqlDatabase::addDatabase("QSQLITE", "bookmarks");
8 db.setDatabaseName(dbName);
14 bool Bookmarks::checkAndCreateDb() {
16 qDebug() << "Database error: " << db.lastError().text() << endl;
20 cur.exec("create table bookmarks(key text ,translation text)");
27 void Bookmarks::clear() {
29 cur.exec("drop table bookmarks");
30 cur.exec("create table bookmarks(key text ,translation text)");
35 void Bookmarks::add(Translation* translation) {
38 cur.prepare("insert into bookmarks values (?,?)");
39 cur.addBindValue(translation->key());
40 cur.addBindValue(translation->toHtml());
46 void Bookmarks::remove(Translation* translation) {
48 cur.prepare("delete from bookmarks where key=? and translation=?");
49 cur.addBindValue(translation->key());
50 cur.addBindValue(translation->key());
56 QList<Translation*> Bookmarks::list() {
58 cur.exec("select distinct key from bookmarks");
59 QList<Translation*> res;
61 res.append(new BookmarkTranslation(cur.value(0).toString(), this));
67 QList<Translation*> Bookmarks::searchWordList(QString word) {
69 if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
71 word = word.replace("*", "%");
72 word = word.replace("?", "_");
73 word = removeAccents(word);
76 cur.prepare("select key from bookmarks where key like ?");
77 cur.addBindValue(word);
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));
91 QStringList Bookmarks::search(QString word) {
92 qDebug() << "bookmarks::search";
95 cur.prepare("select translation from bookmarks where key=?");
96 cur.addBindValue(word);
99 result << cur.value(0).toString();
101 qDebug() << result.size() << " " << result;
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);
125 bool Bookmarks::inBookmarks(QString word) {
127 cur.prepare("select translation from bookmarks where key like ? limit 1");
128 cur.addBindValue(word);