Merge branch 'cache' of ssh://drop.maemo.org/git/mdictionary into cache
[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     cachingDialog = new XdxfCachingDialog(this);
37
38     connect(cachingDialog, SIGNAL(cancelCaching()),
39             this, SLOT(stop()));
40
41     _settings->setValue("type","xdxf");
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     QSet<Translation*> translations;
78     QString cacheFilePath = _settings->value("cache_path");
79         db.setDatabaseName(cacheFilePath);
80         if(!db.open()) {
81             qDebug() << "Database error" << db.lastError().text() << endl;
82             return searchWordListFile(word, limit);
83         }
84
85         stopped = false;
86         if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
87             word+="%";
88         word = word.replace("*", "%");
89         word = word.replace("?", "_");
90         word = removeAccents(word);
91         qDebug() << word;
92
93         QSqlQuery cur(db);
94         cur.prepare("select word from dict where word like ? limit ?");
95         cur.addBindValue(word);
96         cur.addBindValue(limit);
97         cur.exec();
98         while(cur.next())
99             translations.insert(new TranslationXdxf(cur.value(0).toString(),
100                                                     _infoNote, this));
101         return translations.toList();
102 }
103
104
105
106 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
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     if(_settings->value("cached") == "true")
158         return searchCache(key);
159     return searchFile(key);
160 }
161
162
163
164 QString XdxfPlugin::searchCache(QString key) {
165     QString result;
166     QString cacheFilePath = _settings->value("cache_path");
167     db.setDatabaseName(cacheFilePath);
168
169     if(!db.open()) {
170         qDebug() << "Database error" << db.lastError().text() << endl;
171         return searchFile(key);
172     }
173
174     QSqlQuery cur(db);
175     cur.prepare("select translation from dict where word like ? limit 1");
176     cur.addBindValue(key);
177     cur.exec();
178     if(cur.next())
179         result = cur.value(0).toString();
180     return result;
181
182 }
183
184
185
186
187 QString XdxfPlugin::searchFile(QString key) {
188     QFile dictionaryFile(path);
189     QString resultString("");
190     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
191         qDebug()<<"Error: could not open file";
192         return "";
193     }
194     QXmlStreamReader dictionaryReader(&dictionaryFile);
195
196
197     QString a;
198
199     bool match =false;
200     stopped = false;
201     while (!dictionaryReader.atEnd()&& !stopped) {
202         dictionaryReader.readNext();
203         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
204             if(dictionaryReader.name()=="k") {
205                 a = dictionaryReader.readElementText();
206                 if(a==key)
207                     match = true;
208             }
209         }
210         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
211             if(match) {
212                 QString temp(dictionaryReader.text().toString());
213                 temp.replace("\n","");
214                 if(temp == ""){
215                     while(dictionaryReader.name()!="ar"&&
216                                 !dictionaryReader.atEnd()){
217                         dictionaryReader.readNext();
218                         temp+=dictionaryReader.text().toString();
219                     }
220                 }
221                 resultString+=temp.replace("\n","")+"\n";
222                 match=false;
223             }
224         }
225         this->thread()->yieldCurrentThread();
226     }
227     stopped=false;
228     dictionaryFile.close();
229     return resultString;
230 }
231
232 void XdxfPlugin::stop() {
233     stopped=true;
234 }
235
236 DictDialog* XdxfPlugin::dictDialog() {
237      return _dictDialog;
238 }
239
240 void XdxfPlugin::setPath(QString path){
241     this->path=path;
242     _settings->setValue("path",path);
243     //getDictionaryInfo();
244 }
245
246
247 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
248     XdxfPlugin *plugin = new XdxfPlugin();
249     if(settings){
250         plugin->setPath(settings->value("path"));
251
252         QStringList list = settings->keys();
253         foreach(QString key, list)
254             plugin->settings()->setValue(key, settings->value(key));
255
256
257         plugin->db_name = plugin->_settings->value("type")
258                + plugin->_settings->value("path");
259         plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
260
261         if(settings->value("cached").isEmpty() &&
262            settings->value("generateCache") == "true") {
263             plugin->makeCache("");
264         }
265     }
266
267     plugin->getDictionaryInfo();
268     return  plugin;
269 }
270
271 bool XdxfPlugin::isAvailable() const {
272     return true;
273 }
274
275 void XdxfPlugin::setHash(uint _hash)
276 {
277     this->_hash=_hash;
278 }
279
280 uint XdxfPlugin::hash() const
281 {
282    return _hash;
283 }
284
285 Settings* XdxfPlugin::settings() {
286     return _settings;
287 }
288
289 bool XdxfPlugin::isCached()
290 {
291     return false;
292 }
293
294 void XdxfPlugin::setSettings(Settings *settings) {
295
296     QString oldPath = _settings->value("path");
297     if(oldPath != settings->value("path")) {
298         setPath(settings->value("path"));
299     }
300
301     if((_settings->value("cached") == "false" ||
302         _settings->value("cached").isEmpty()) &&
303        settings->value("generateCache") == "true") {
304         makeCache("");
305     }
306     else {
307        _settings->setValue("cached", "false");
308     }
309
310     emit settingsChanged();
311 }
312
313
314 void XdxfPlugin::getDictionaryInfo() {
315     QFile dictionaryFile(path);
316     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
317         qDebug()<<"Error: could not open file";
318         return;
319     }
320
321     QXmlStreamReader dictionaryReader(&dictionaryFile);
322     dictionaryReader.readNextStartElement();
323     if(dictionaryReader.name()=="xdxf") {
324       if(dictionaryReader.attributes().hasAttribute("lang_from"))
325         _langFrom = dictionaryReader.attributes().value("lang_from").toString();
326       if(dictionaryReader.attributes().hasAttribute("lang_to"))
327         _langTo = dictionaryReader.attributes().value("lang_to").toString();
328     }
329     dictionaryReader.readNextStartElement();
330     if(dictionaryReader.name()=="full_name")
331         _name=dictionaryReader.readElementText();
332     dictionaryReader.readNextStartElement();
333     if(dictionaryReader.name()=="description")
334         _infoNote=dictionaryReader.readElementText();
335
336     dictionaryFile.close();
337 }
338
339 QString XdxfPlugin::removeAccents(QString string) {
340
341     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
342     QString normalized = string.normalized(QString::NormalizationForm_D);
343     normalized = normalized;
344     for(int i=0; i<normalized.size(); i++) {
345         if( !normalized[i].isLetterOrNumber() &&
346             !normalized[i].isSpace() &&
347             !normalized[i].isDigit() &&
348             normalized[i] != '*' &&
349             normalized[i] != '%') {
350             normalized.remove(i,1);
351         }
352     }
353     return normalized;
354 }
355
356 QIcon* XdxfPlugin::icon() {
357     return &_icon;
358 }
359
360 int XdxfPlugin::countWords() {
361     if(_wordsCount > 0)
362         return _wordsCount;
363
364     QFile dictionaryFile(path);
365     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
366         qDebug()<<"Error: could not open file";
367         return -1;
368     }
369
370     dictionaryFile.seek(0);
371
372     long wordsCount = 0;
373
374     QString line;
375     while(!dictionaryFile.atEnd()) {
376         line = dictionaryFile.readLine();
377         if(line.contains("<k>")) {
378             wordsCount++;
379         }
380     }
381     _wordsCount = wordsCount;
382     dictionaryFile.close();
383     return wordsCount;
384 }
385
386
387
388 bool XdxfPlugin::makeCache(QString dir) {
389     cachingDialog->setVisible(true);
390     QCoreApplication::processEvents();
391     stopped = false;
392     QFileInfo dictFileN(_settings->value("path"));
393     QString cachePathN;
394     cachePathN = QDir::homePath() + "/.mdictionary/"
395                  + dictFileN.completeBaseName() + ".cache";
396
397     QFile dictionaryFile(dictFileN.filePath());
398
399
400     if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
401         return 0;
402     }
403
404     QXmlStreamReader reader(&dictionaryFile);
405
406
407     db.setDatabaseName(cachePathN);
408     if(!db.open()) {
409         qDebug() << "Database error" << endl;
410         return false;
411     }
412     QCoreApplication::processEvents();
413     QSqlQuery cur(db);
414     cur.exec("PRAGMA synchronous = 0");
415     cur.exec("drop table dict");
416     QCoreApplication::processEvents();
417     cur.exec("create table dict(word text ,translation text)");
418     int counter = 0;
419     cur.exec("BEGIN;");
420
421     QString a;
422     bool match = false;
423     QTime timer;
424     timer.start();
425     countWords();
426
427     int lastProg = -1;
428
429
430     counter=0;
431     while (!reader.atEnd() && !stopped) {
432
433         QCoreApplication::processEvents();
434         //usleep(50);
435         reader.readNext();
436
437         if(reader.tokenType() == QXmlStreamReader::StartElement) {
438             if(reader.name()=="k"){
439                 a = reader.readElementText();
440                 match = true;
441             }
442         }
443         else if(reader.tokenType() == QXmlStreamReader::Characters) {
444              if(match) {
445                 QString temp(reader.text().toString());
446                 temp.replace("\n","");
447                 if(temp == ""){
448                     while(reader.name()!="ar"&&
449                                 !reader.atEnd()){
450                         reader.readNext();
451                         temp+=reader.text().toString();
452                     }
453                 }
454                 match = false;
455                 cur.prepare("insert into dict values(?,?)");
456                 cur.addBindValue(a);
457                 cur.addBindValue(temp);
458                 cur.exec();
459                 counter++;
460                 int prog = counter*100/_wordsCount;
461                 if(prog % 5 == 0 && lastProg != prog) {
462                     Q_EMIT updateCachingProgress(prog,
463                                                  timer.restart());
464                     lastProg = prog;
465                 }
466             }
467
468         }
469     }
470
471     cur.exec("END;");
472     cur.exec("select count(*) from dict");
473
474     countWords();
475     cachingDialog->setVisible(false);
476
477     if(!cur.next() || countWords() != cur.value(0).toInt())
478         return false;
479     _settings->setValue("cache_path", cachePathN);
480     _settings->setValue("cached", "true");
481
482     return true;
483 }
484
485
486 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)