Fixed crashes and db error connected with threads
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.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 #include "xdxfplugin.h"
23 #include <QDebug>
24 #include <QFile>
25 #include <QXmlStreamReader>
26 #include <QtPlugin>
27 #include "TranslationXdxf.h"
28 #include "../../../includes/settings.h"
29
30 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
31                     _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
32                     _type(tr("xdxf")), _infoNote(tr("")) {
33     _wordsCount = -1;
34     _settings = new Settings();
35     _dictDialog = new XdxfDictDialog(this, this);
36     _settings->setValue("type","xdxf");
37     if(isCached())
38         _settings->setValue("cached","true");
39     else
40         _settings->setValue("cached","false");
41
42
43     stopped = false;
44
45     _icon = QIcon(":/icons/xdxf.png");
46 }
47
48 QString XdxfPlugin::langFrom() const {   
49     return _langFrom;
50 }
51
52 QString XdxfPlugin::langTo() const {
53     return  _langTo;
54 }
55
56 QString XdxfPlugin::name() const {
57     return  _name;
58 }
59
60 QString XdxfPlugin::type() const {
61 //    return _settings->value("type");
62     return _type;
63 }
64
65 QString XdxfPlugin::infoNote() const {
66     return  _infoNote;
67 }
68
69 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
70     if(_settings->value("cached") == "true")
71         return searchWordListCache(word,limit);
72     return searchWordListFile(word, limit);
73 }
74
75 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
76
77     qDebug() << "search cache";
78     QSet<Translation*> translations;
79     QString cacheFilePath = _settings->value("cache_path");
80         db.setDatabaseName(cacheFilePath);
81         if(!db.open()) {
82             qDebug() << "Database error" << db.lastError().text() << endl;
83             return searchWordListFile(word, limit);
84         }
85
86         stopped = false;
87         word = removeAccents(word);
88         if(word.indexOf("*")==-1)
89             word+="%";
90         word = word.replace("*", "%");
91
92         QSqlQuery cur(db);
93         cur.prepare("select word from dict where word like ? limit ?");
94         cur.addBindValue(word);
95         cur.addBindValue(limit);
96         cur.exec();
97         while(cur.next())
98             translations.insert(new TranslationXdxf(cur.value(0).toString(),
99                                                     _infoNote, this));
100         return translations.toList();
101 }
102
103
104
105 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
106     qDebug() << "search file";
107     QSet<Translation*> translations;
108     QFile dictionaryFile(path);
109
110     word = removeAccents(word);
111
112     stopped = false;
113     if(word.indexOf("*")==-1)
114         word+="*";
115     QRegExp regWord(word);
116     regWord.setCaseSensitivity(Qt::CaseInsensitive);
117     regWord.setPatternSyntax(QRegExp::Wildcard);
118     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
119         qDebug()<<"Error: could not open file";
120         return translations.toList();
121     }
122
123     QXmlStreamReader dictionaryReader(&dictionaryFile);
124     /*search words list*/
125     QString a;
126     int i=0;
127     while(!dictionaryReader.atEnd() && !stopped){
128         dictionaryReader.readNextStartElement();
129         if(dictionaryReader.name()=="ar"){
130             while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
131                 dictionaryReader.readNextStartElement();
132             if(!dictionaryReader.atEnd())
133                 a = dictionaryReader.readElementText();
134             if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
135                 bool ok=true;
136                 Translation *tran;
137                 foreach(tran,translations)
138                 {
139                     if(tran->key()==a)
140                         ok=false;  /*if key word is in the dictionary more that one */
141                 }
142                 if(ok)  /*add key word to list*/
143                     translations<<(new TranslationXdxf(a,_infoNote,this));
144                 i++;
145                 if(i>=limit && limit!=0)
146                     break;
147             }
148         }
149         this->thread()->yieldCurrentThread();
150     }
151     stopped=false;
152     dictionaryFile.close();
153     return translations.toList();
154 }
155
156 QString XdxfPlugin::search(QString key) {
157     QFile dictionaryFile(path);
158     QString resultString("");
159     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
160         qDebug()<<"Error: could not open file";
161         return "";
162     }
163     QXmlStreamReader dictionaryReader(&dictionaryFile);
164
165
166     QString a;
167
168     bool match =false;
169     stopped = false;
170     while (!dictionaryReader.atEnd()&& !stopped) {
171         dictionaryReader.readNext();
172         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
173             if(dictionaryReader.name()=="k") {
174                 a = dictionaryReader.readElementText();
175                 if(a==key)
176                     match = true;
177             }
178         }
179         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
180             if(match) {
181                 QString temp(dictionaryReader.text().toString());
182                 temp.replace("\n","");
183                 if(temp == ""){
184                     while(dictionaryReader.name()!="ar"&&
185                                 !dictionaryReader.atEnd()){
186                         dictionaryReader.readNext();
187                         temp+=dictionaryReader.text().toString();
188                     }
189                 }
190                 resultString+=temp.replace("\n","")+"\n";
191                 match=false;
192             }
193         }
194         this->thread()->yieldCurrentThread();
195     }
196     stopped=false;
197     dictionaryFile.close();
198     return resultString;
199 }
200
201 void XdxfPlugin::stop() {
202     stopped=true;
203 }
204
205 DictDialog* XdxfPlugin::dictDialog() {
206      return _dictDialog;
207 }
208
209 void XdxfPlugin::setPath(QString path){
210     this->path=path;
211     _settings->setValue("path",path);
212     getDictionaryInfo();
213 }
214
215
216 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
217     XdxfPlugin *plugin = new XdxfPlugin();
218     if(settings){
219         plugin->setPath(settings->value("path"));
220         QStringList list = settings->keys();
221         foreach(QString key, list)
222             plugin->settings()->setValue(key, settings->value(key));
223         //if(plugin->settings()->value("cached") != "true")
224         plugin->db_name = plugin->_settings->value("type")
225                + plugin->_settings->value("path");
226         plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
227         plugin->makeCache("");
228     }
229     return  plugin;
230 }
231
232 bool XdxfPlugin::isAvailable() const {
233     return true;
234 }
235
236 void XdxfPlugin::setHash(uint _hash)
237 {
238     this->_hash=_hash;
239 }
240
241 uint XdxfPlugin::hash() const
242 {
243    return _hash;
244 }
245
246 Settings* XdxfPlugin::settings() {
247     return _settings;
248 }
249
250 bool XdxfPlugin::isCached()
251 {
252     return false;
253 }
254
255 void XdxfPlugin::setSettings(Settings *settings) {
256     _settings = settings;
257     setPath(_settings->value("path"));
258     emit settingsChanged();
259 }
260
261
262 void XdxfPlugin::getDictionaryInfo() {
263     QFile dictionaryFile(path);
264     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
265         qDebug()<<"Error: could not open file";
266         return;
267     }
268
269     QXmlStreamReader dictionaryReader(&dictionaryFile);
270     dictionaryReader.readNextStartElement();
271     if(dictionaryReader.name()=="xdxf") {
272       if(dictionaryReader.attributes().hasAttribute("lang_from"))
273         _langFrom = dictionaryReader.attributes().value("lang_from").toString();
274       if(dictionaryReader.attributes().hasAttribute("lang_to"))
275         _langTo = dictionaryReader.attributes().value("lang_to").toString();
276     }
277     dictionaryReader.readNextStartElement();
278     if(dictionaryReader.name()=="full_name")
279         _name=dictionaryReader.readElementText();
280     dictionaryReader.readNextStartElement();
281     if(dictionaryReader.name()=="description")
282         _infoNote=dictionaryReader.readElementText();
283
284     dictionaryFile.close();
285 }
286
287 QString XdxfPlugin::removeAccents(QString string) {
288
289     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
290     QString normalized = string.normalized(QString::NormalizationForm_D);
291     normalized = normalized;
292     for(int i=0; i<normalized.size(); i++) {
293         if( !normalized[i].isLetterOrNumber() &&
294             !normalized[i].isSpace() &&
295             !normalized[i].isDigit()) {
296             normalized.remove(i,1);
297         }
298     }
299     return normalized;
300 }
301
302 QIcon* XdxfPlugin::icon() {
303     return &_icon;
304 }
305
306 int XdxfPlugin::countWords() {
307     if(_wordsCount > 0)
308         return _wordsCount;
309
310     QFile dictionaryFile(path);
311     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
312         qDebug()<<"Error: could not open file";
313         return -1;
314     }
315
316     dictionaryFile.seek(0);
317
318     long wordsCount = 0;
319
320     QString line;
321     while(!dictionaryFile.atEnd()) {
322         line = dictionaryFile.readLine();
323         if(line.contains("<k>")) {
324             wordsCount++;
325         }
326     }
327     _wordsCount = wordsCount;
328     dictionaryFile.close();
329     return wordsCount;
330 }
331
332
333
334 bool XdxfPlugin::makeCache(QString dir) {
335     QFileInfo dictFileN(_settings->value("path"));
336     QString cachePathN;
337     cachePathN = QDir::homePath() + "/.mdictionary/"
338                  + dictFileN.completeBaseName() + ".cache";
339
340     QFile dictionaryFile(dictFileN.filePath());
341
342
343     qDebug() << dictFileN.path();
344     if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
345         return 0;
346     }
347     qDebug() << "OLE";
348
349     QXmlStreamReader reader(&dictionaryFile);
350
351
352     db.setDatabaseName(cachePathN);
353     if(!db.open()) {
354         qDebug() << "Database error" << endl;
355         return false;
356     }
357     QSqlQuery cur(db);
358     cur.exec("PRAGMA synchronous = 0");
359     cur.exec("drop table dict");
360     cur.exec("create table dict(word text ,transl text)");
361     int counter = 0;
362     cur.exec("BEGIN;");
363
364     QString a;
365     bool match = false;
366     QTime timer;
367     timer.start();
368     countWords();
369
370
371     counter=0;
372     while (!reader.atEnd()) {
373
374         reader.readNext();
375
376         if(reader.tokenType() == QXmlStreamReader::StartElement) {
377             if(reader.name()=="k"){
378                 a = reader.readElementText();
379                 match = true;
380             }
381         }
382         else if(reader.tokenType() == QXmlStreamReader::Characters) {
383              if(match) {
384                 QString temp(reader.text().toString());
385                 temp.replace("\n","");
386                 if(temp == ""){
387                     while(reader.name()!="ar"&&
388                                 !reader.atEnd()){
389                         reader.readNext();
390                         temp+=reader.text().toString();
391                     }
392                 }
393                 match = false;
394                 cur.prepare("insert into dict values(?,?)");
395                 cur.addBindValue(a);
396                 cur.addBindValue(temp);
397                 cur.exec();
398                 counter++;
399                 int prog = counter*100/_wordsCount;
400                 if(prog % 5 == 0)
401                     Q_EMIT update(prog);
402             }
403
404         }
405     }
406
407     qDebug()<<counter;
408     cur.exec("END;");
409     cur.exec("select count(*) from dict");
410     if(!cur.next() || countWords() != cur.value(0).toInt())
411         return false;
412     _settings->setValue("cache_path", cachePathN);
413     _settings->setValue("cached", "true");
414     return true;
415 }
416
417
418 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)