Clean and order documentation in source files. Source ready to beta 2 release
[mdictionary] / src / mdictionary / backbone / Bookmarks.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*!
23     \file Bookmarks.cpp
24     \brief Bookmarks functionality - marking words as favorite, managing marked
25     words, searching in marked words (with cached translations)
26
27     \author Bartosz Szatkowski <bulislaw@linux.com>
28 */
29
30 #include "Bookmarks.h"
31 #include "BookmarkTranslations.h"
32 #include <QThread>
33
34 Bookmarks::Bookmarks() {
35     this->dbName = QDir::homePath() + "/.mdictionary/"
36                  + "bookmarks.db";
37     mdb = QSqlDatabase::addDatabase("QSQLITE");
38     mdb.setDatabaseName(dbName);
39     checkAndCreateDb(dbName);
40     initAccents();
41
42 }
43
44
45 Bookmarks::~Bookmarks() {
46     mdb.close();
47 }
48
49
50 QSqlDatabase Bookmarks::getDbCnx(){
51     return mdb;
52 }
53
54
55 bool Bookmarks::checkAndCreateDb(QString dbName) {
56     QSqlDatabase db = QSqlDatabase::database();
57     if(dbName.size())
58         db.setDatabaseName(dbName);
59     else
60         db.setDatabaseName(this->dbName);
61     db.open();
62     QSqlQuery cur(db);
63     cur.exec("create table bookmarks(key text, normalized text, translation text)");
64     return true;
65 }
66
67
68 void Bookmarks::clear() {
69     checkAndCreateDb();
70     QSqlDatabase db = getDbCnx();
71     if(!db.isOpen() && !db.open()) {
72         qDebug() << "Database error: " << db.lastError().text() << endl;
73         return ;
74     }
75     QSqlQuery cur(db);
76     cur.exec("drop table bookmarks");
77     cur.exec("create table bookmarks(key text, normalized text,translation text)");
78 }
79
80
81 void Bookmarks::add(Translation* translation) {
82     if (!translation)
83         return ;
84     checkAndCreateDb();   
85     QSqlDatabase db = getDbCnx();
86     if(!db.isOpen() && !db.open()) {
87         qDebug() << "Database error: " << db.lastError().text() << endl;
88         return ;
89     }
90     translation->setBookmark(true);
91     QSqlQuery cur(db);
92     cur.prepare("insert into bookmarks values (?,?,?)");
93     cur.addBindValue(translation->key());
94     cur.addBindValue(removeAccents(translation->key()));
95     cur.addBindValue(translation->toXml());
96     cur.exec();
97 }
98
99
100 void Bookmarks::add(QString key,QString removeAccentKey,QString value){
101     checkAndCreateDb();
102     QSqlDatabase db = getDbCnx();
103     if(!db.isOpen() && !db.open()) {
104         qDebug() << "Database error: " << db.lastError().text() << endl;
105         return ;
106     }
107     QSqlQuery cur(db);
108     cur.prepare("insert into bookmarks values (?,?,?)");
109     cur.addBindValue(key);
110     cur.addBindValue(removeAccentKey);
111     cur.addBindValue(value);
112     cur.exec();
113 }
114
115
116 void Bookmarks::remove(Translation* translation) {
117     if (!translation)
118         return ;
119     checkAndCreateDb();
120     QSqlDatabase db = getDbCnx();
121     if(!db.isOpen() && !db.open()) {
122         qDebug() << "Database error: " << db.lastError().text() << endl;
123         return ;
124     }
125     QSqlQuery cur(db);
126     cur.prepare("delete from bookmarks where key=?");
127     cur.addBindValue(translation->key());
128     cur.exec();
129 }
130
131
132 QList<Translation*> Bookmarks::list() {
133     checkAndCreateDb();
134     QList<Translation*> res;
135     QSqlDatabase db = getDbCnx();
136     if(!db.isOpen() && !db.open()) {
137         qDebug() << "Database error: " << db.lastError().text() << endl;
138         return res;
139     }
140     QSqlQuery cur(db);
141     cur.exec("select distinct key from bookmarks");
142     while(cur.next())
143         res.append(new BookmarkTranslation(cur.value(0).toString(),
144                 this, dbName));
145     return res;
146 }
147
148
149 QList<Translation*> Bookmarks::searchWordList(QString word) {
150     checkAndCreateDb();
151     if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
152         word+="%";
153     word = word.replace("*", "%");
154     word = word.replace("?", "_");
155
156     QList<Translation*> tr;
157     QSqlDatabase db = getDbCnx();
158     if(!db.isOpen() && !db.open()) {
159         qDebug() << "Database error: " << db.lastError().text() << endl;
160         return tr;
161     }
162     QSqlQuery cur(db);
163     cur.prepare("select key from bookmarks where key like ? or normalized like ?");
164     cur.addBindValue(word);
165     cur.addBindValue(word);
166     cur.exec();
167     QSet<QString> res;
168     while(cur.next())
169         res.insert(cur.value(0).toString());
170     foreach(QString str, res.toList())
171         tr.append(new BookmarkTranslation(str, this, dbName));
172     return tr;
173 }
174
175
176 QStringList Bookmarks::search(QString word, QString dbName) {
177     QStringList result;
178     QSqlDatabase db = QSqlDatabase::database();
179     db.setDatabaseName(dbName);
180     if(!db.isOpen() && !db.open()) {
181         qDebug() << "Database error: " << db.lastError().text() << endl;
182         return result;
183     }
184     QSqlQuery cur(db);
185     cur.prepare("select translation from bookmarks where key=?");
186     cur.addBindValue(word);
187     cur.exec();
188     while(cur.next())
189         result << cur.value(0).toString();
190
191     return result;
192 }
193
194
195 bool Bookmarks::inBookmarks(QString word) {
196     if(!mdb.isOpen() && !mdb.open()) {
197         qDebug() << "Database error: " << mdb.lastError().text() << endl;
198         return false;
199     }
200     QSqlQuery cur(mdb);
201     cur.prepare("select translation from bookmarks where key like ? limit 1");
202     cur.addBindValue(word);
203     cur.exec();
204     if(cur.next())
205         return true;
206     return false;
207 }