this->dbName = QDir::homePath() + "/.mdictionary/"
+ "bookmarks.db";
checkAndCreateDb();
+ initAccents();
+
+ mdb = QSqlDatabase::addDatabase("QSQLITE",
+ QString("%2").arg((int)QThread::currentThreadId()));
+ mdb.setDatabaseName(dbName);
+}
+
+
+Bookmarks::~Bookmarks() {
+ mdb.close();
}
QSqlDatabase Bookmarks::getDbCnx(QString dbName) {
+ QSqlDatabase::removeDatabase(
+ QString("%2").arg((int)QThread::currentThreadId()));
QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE",
QString("%2").arg((int)QThread::currentThreadId()));
db.setDatabaseName(dbName);
return db;
}
+
+
bool Bookmarks::checkAndCreateDb() {
QSqlDatabase db = getDbCnx(dbName);
- if(!db.isOpen() && !db.open()) {
- qDebug() << "Database error: " << db.lastError().text() << endl;
- return false;
- }
+ db.open();
QSqlQuery cur(db);
- cur.exec("create table bookmarks(key text ,translation text)");
+ cur.exec("create table bookmarks(key text, normalized text, translation text)");
db.close();
-
return true;
}
void Bookmarks::clear() {
+ checkAndCreateDb();
QSqlDatabase db = getDbCnx(dbName);
if(!db.isOpen() && !db.open()) {
qDebug() << "Database error: " << db.lastError().text() << endl;
}
QSqlQuery cur(db);
cur.exec("drop table bookmarks");
- cur.exec("create table bookmarks(key text ,translation text)");
+ cur.exec("create table bookmarks(key text, normalized text,translation text)");
db.close();
}
void Bookmarks::add(Translation* translation) {
+ checkAndCreateDb();
QSqlDatabase db = getDbCnx(dbName);
if(!db.isOpen() && !db.open()) {
qDebug() << "Database error: " << db.lastError().text() << endl;
}
translation->setBookmark(true);
QSqlQuery cur(db);
- cur.prepare("insert into bookmarks values (?,?)");
+ cur.prepare("insert into bookmarks values (?,?,?)");
cur.addBindValue(translation->key());
+ cur.addBindValue(removeAccents(translation->key()));
cur.addBindValue(translation->toHtml());
cur.exec();
db.close();
void Bookmarks::remove(Translation* translation) {
+ checkAndCreateDb();
QSqlDatabase db = getDbCnx(dbName);
if(!db.isOpen() && !db.open()) {
qDebug() << "Database error: " << db.lastError().text() << endl;
QList<Translation*> Bookmarks::list() {
+ checkAndCreateDb();
QList<Translation*> res;
QSqlDatabase db = getDbCnx(dbName);
if(!db.isOpen() && !db.open()) {
QSqlQuery cur(db);
cur.exec("select distinct key from bookmarks");
while(cur.next())
- res.append(new BookmarkTranslation(cur.value(0).toString(), this, dbName));
+ res.append(new BookmarkTranslation(cur.value(0).toString(),
+ this, dbName));
db.close();
return res;
}
QList<Translation*> Bookmarks::searchWordList(QString word) {
-
+ checkAndCreateDb();
if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
word+="%";
word = word.replace("*", "%");
word = word.replace("?", "_");
- word = removeAccents(word);
QList<Translation*> tr;
QSqlDatabase db = getDbCnx(dbName);
return tr;
}
QSqlQuery cur(db);
- cur.prepare("select key from bookmarks where key like ?");
+ cur.prepare("select key from bookmarks where key like ? or normalized like ?");
+ cur.addBindValue(word);
cur.addBindValue(word);
cur.exec();
QSet<QString> res;
QStringList Bookmarks::search(QString word, QString dbName) {
+ //checkAndCreateDb();
QStringList result;
QSqlDatabase db = getDbCnx(dbName);
if(!db.isOpen() && !db.open()) {
-QString Bookmarks::removeAccents(QString string) {
- string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
- QString normalized = string.normalized(QString::NormalizationForm_D);
- normalized = normalized;
- for(int i=0; i<normalized.size(); i++) {
- if( !normalized[i].isLetterOrNumber() &&
- !normalized[i].isSpace() &&
- !normalized[i].isDigit() &&
- normalized[i] != '*' &&
- normalized[i] != '%') {
- normalized.remove(i,1);
- }
- }
- return normalized;
-}
-
-
bool Bookmarks::inBookmarks(QString word) {
- QSqlDatabase db = getDbCnx(dbName);
- if(!db.isOpen() && !db.open()) {
- qDebug() << "Database error: " << db.lastError().text() << endl;
+ if(!mdb.isOpen() && !mdb.open()) {
+ qDebug() << "Database error: " << mdb.lastError().text() << endl;
return false;
}
- QSqlQuery cur(db);
+ QSqlQuery cur(mdb);
cur.prepare("select translation from bookmarks where key like ? limit 1");
cur.addBindValue(word);
cur.exec();
if(cur.next())
return true;
- db.close();
return false;
}