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