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