1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 /*! \file xdxfplugin.cpp
25 #include "xdxfplugin.h"
27 #include "../../../includes/Notify.h"
29 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
30 _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
31 _type(tr("xdxf")), _infoNote(tr("")) {
33 _settings = new Settings();
34 _dictDialog = new XdxfDictDialog(this);
35 cachingDialog = new XdxfCachingDialog();
37 connect(cachingDialog, SIGNAL(cancelCaching()),
40 connect(this, SIGNAL(updateCachingProgress(int,int)),
41 cachingDialog, SLOT(updateCachingProgress(int,int)));
43 _settings->setValue("type","xdxf");
47 _icon = QIcon(":/icons/xdxf.png");
53 XdxfPlugin::~XdxfPlugin()
55 // QString connection(db.connectionName());
57 // QSqlDatabase::removeDatabase(connection);
62 void XdxfPlugin::initAccents() {
63 letters['a'] = QRegExp("[ÀàÁáÂÂâÃãÄäÅåæÆĀāĂ㥹]", Qt::CaseInsensitive);
64 letters['c'] = QRegExp("[ÇçÈçŒĆćĈĉĊċČč]", Qt::CaseInsensitive);
65 letters['d'] = QRegExp("[ÐĐđĎď]", Qt::CaseInsensitive);
66 letters['e'] = QRegExp("[ÈéèÉÊêËëĒēĔĕĖėĘęĚěē]", Qt::CaseInsensitive);
67 letters['f'] = QRegExp("[ſ]", Qt::CaseInsensitive);
68 letters['g'] = QRegExp("[ĠġĢģĜĝĞğ]", Qt::CaseInsensitive);
69 letters['h'] = QRegExp("[ħĤĥĦ]", Qt::CaseInsensitive);
70 letters['i'] = QRegExp("[ÌìÍíÎîÏïİijĨĩĪīĬĭĮį]", Qt::CaseInsensitive);
71 letters['j'] = QRegExp("[Ĵĵ]", Qt::CaseInsensitive);
72 letters['k'] = QRegExp("[Ķķĸ]", Qt::CaseInsensitive);
73 letters['l'] = QRegExp("[ŀŁłĹĺĻļĽľĿ]", Qt::CaseInsensitive);
74 letters['n'] = QRegExp("[ÑñŃńŅņŇňʼnŊŋ]", Qt::CaseInsensitive);
75 letters['o'] = QRegExp("[ÒòÓóÔÕõÖöØøŐőœŌōŎŏ]", Qt::CaseInsensitive);
76 letters['r'] = QRegExp("[ŕŖŖŗŘř]", Qt::CaseInsensitive);
77 letters['s'] = QRegExp("[ߊšŚśŜŝŞş]", Qt::CaseInsensitive);
78 letters['t'] = QRegExp("[ŢţŤťŦŧ]", Qt::CaseInsensitive);
79 letters['u'] = QRegExp("[ÙùÚúÛûÜüŰűŲųŨũŪūŬŮů]", Qt::CaseInsensitive);
80 letters['w'] = QRegExp("[Ŵŵ]", Qt::CaseInsensitive);
81 letters['y'] = QRegExp("[ÿÝýŶŷŸ]", Qt::CaseInsensitive);
82 letters['z'] = QRegExp("[ŹźŻżŽž]", Qt::CaseInsensitive);
83 noLetter = QRegExp("[^a-z ]", Qt::CaseInsensitive);
86 QString XdxfPlugin::langFrom() const {
90 QString XdxfPlugin::langTo() const {
94 QString XdxfPlugin::name() const {
98 QString XdxfPlugin::type() const {
99 // return _settings->value("type");
103 QString XdxfPlugin::infoNote() const {
107 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
108 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
109 && word.indexOf("%")==-1)
112 return searchWordListCache(word,limit);
113 return searchWordListFile(word, limit);
116 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
118 QSet<Translation*> translations;
119 QString cacheFilePath = _settings->value("cache_path");
120 db.setDatabaseName(cacheFilePath);
122 qDebug() << "Database error" << db.lastError().text() << endl;
123 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
124 "opened for %1 dictionary. Searching in xdxf file. "
125 "You may want to recache.").arg(name()));
126 return searchWordListFile(word, limit);
130 word = word.toLower();
131 word = word.replace("*", "%");
132 word = word.replace("?", "_");
136 cur.prepare("select word from dict where word like ? limit ?");
138 cur.prepare("select word from dict where word like ?");
139 cur.addBindValue(word);
141 cur.addBindValue(limit);
148 foreach(tran,translations) {
149 if(tran->key().toLower()==cur.value(0).toString().toLower())
152 if(ok) /*add key word to list*/
153 translations.insert(new TranslationXdxf(
154 cur.value(0).toString().toLower(),
161 cur.prepare("select word from dict where normalized like ? limit ?");
163 cur.prepare("select word from dict where normalized like ?");
164 cur.addBindValue(word);
166 cur.addBindValue(limit);
171 foreach(tran,translations) {
172 if(tran->key().toLower()==cur.value(0).toString().toLower())
175 if(ok) /*add key word to list*/
176 translations.insert(new TranslationXdxf(
177 cur.value(0).toString().toLower(),
182 return translations.toList();
185 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
188 QSet<Translation*> translations;
189 QFile dictionaryFile(path);
191 word = word.toLower();
192 //word = removeAccents(word);
195 QRegExp regWord(word);
196 regWord.setCaseSensitivity(Qt::CaseInsensitive);
197 regWord.setPatternSyntax(QRegExp::Wildcard);
198 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
199 qDebug()<<"Error: could not open file";
200 Q_EMIT notify(Notify::Warning,
201 QString("Xdxf file cannot be read for %1").arg(name()));
202 return translations.toList();
205 QXmlStreamReader reader(&dictionaryFile);
206 /*search words list*/
210 while(!reader.atEnd() && !stopped){
211 reader.readNextStartElement();
212 if(reader.name()=="ar") {
213 while(reader.name()!="k" && !reader.atEnd())
214 reader.readNextStartElement();
216 a = reader.readElementText();
217 if((regWord.exactMatch(a) || regWord.exactMatch(removeAccents(a))) &&
218 (i<limit || limit==0)) {
221 foreach(tran,translations) {
222 if(tran->key().toLower()==a.toLower())
223 ok=false; /*if key word is in the dictionary more that one */
225 if(ok) /*add key word to list*/
226 translations<<(new TranslationXdxf(a.toLower(),
229 if(i>=limit && limit!=0)
233 this->thread()->yieldCurrentThread();
236 dictionaryFile.close();
237 qDebug() << time.elapsed();
238 return translations.toList();
241 QString XdxfPlugin::search(QString key) {
242 // if(_settings->value("cached") == "true")
244 return searchCache(key);
245 return searchFile(key);
248 QString XdxfPlugin::searchCache(QString key) {
250 QString cacheFilePath = _settings->value("cache_path");
251 db.setDatabaseName(cacheFilePath);
255 qDebug() << "Database error" << db.lastError().text() << endl;
256 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
257 "opened for %1 dictionary. Searching in xdxf file. "
258 "You may want to recache.").arg(name()));
259 return searchFile(key);
263 cur.prepare("select translation from dict where word like ?");
264 cur.addBindValue(key);
267 result += cur.value(0).toString();
275 QString XdxfPlugin::searchFile(QString key) {
277 QFile dictionaryFile(path);
278 QString resultString("");
279 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
280 Q_EMIT notify(Notify::Warning,
281 QString("Xdxf file cannot be read for %1").arg(name()));
282 qDebug()<<"Error: could not open file";
285 QXmlStreamReader reader(&dictionaryFile);
290 while (!reader.atEnd()&& !stopped) {
292 if(reader.tokenType() == QXmlStreamReader::StartElement) {
293 if(reader.name()=="k") {
294 a = reader.readElementText();
295 if(a.toLower()==key.toLower())
301 while(reader.name()!="ar" && !reader.atEnd()) {
302 if(reader.name()!="" && reader.name()!="k") {
303 if(reader.tokenType()==QXmlStreamReader::EndElement)
305 if(reader.tokenType()==QXmlStreamReader::StartElement)
307 temp+=reader.name().toString();
308 if(reader.name().toString()=="c" &&
309 reader.tokenType()==QXmlStreamReader::StartElement)
310 temp= temp + tr(" c=\"") + reader.attributes().
311 value(tr("c")).toString() + tr("\"");
314 temp+= reader.text().toString().replace("<","<").
318 if(temp.at(0)==QChar('\n'))
320 resultString+=tr("<key>") + a +tr("</key>");
321 resultString+=tr("<t>") + temp + tr("</t>");
324 this->thread()->yieldCurrentThread();
327 dictionaryFile.close();
332 void XdxfPlugin::stop() {
336 DictDialog* XdxfPlugin::dictDialog() {
340 void XdxfPlugin::setPath(QString path){
342 _settings->setValue("path",path);
343 //getDictionaryInfo();
346 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
347 XdxfPlugin *plugin = new XdxfPlugin();
350 plugin->setPath(settings->value("path"));
351 QStringList list = settings->keys();
352 foreach(QString key, list)
353 plugin->settings()->setValue(key, settings->value(key));
356 plugin->db_name = plugin->_settings->value("type")
357 + plugin->_settings->value("path");
358 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
360 if(settings->value("cached").isEmpty() &&
361 settings->value("generateCache") == "true") {
362 plugin->makeCache("");
366 plugin->getDictionaryInfo();
370 bool XdxfPlugin::isAvailable() const {
374 void XdxfPlugin::setHash(uint _hash) {
378 uint XdxfPlugin::hash() const {
382 Settings* XdxfPlugin::settings() {
386 bool XdxfPlugin::isCached() {
387 if(_settings->value("cached") == "true")
392 void XdxfPlugin::setSettings(Settings *settings) {
394 QString oldPath = _settings->value("path");
395 if(oldPath != settings->value("path")) {
396 setPath(settings->value("path"));
399 if((_settings->value("cached") == "false" ||
400 _settings->value("cached").isEmpty()) &&
401 settings->value("generateCache") == "true") {
405 _settings->setValue("cached", "false");
409 emit settingsChanged();
412 void XdxfPlugin::getDictionaryInfo() {
413 QFile dictionaryFile(path);
414 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
415 Q_EMIT notify(Notify::Warning,
416 QString("Xdxf file cannot be read dictionary"));
417 qDebug()<<"Error: could not open file";
421 QXmlStreamReader reader(&dictionaryFile);
422 reader.readNextStartElement();
423 if(reader.name()=="xdxf") {
424 if(reader.attributes().hasAttribute("lang_from"))
425 _langFrom = reader.attributes().value("lang_from").toString();
426 if(reader.attributes().hasAttribute("lang_to"))
427 _langTo = reader.attributes().value("lang_to").toString();
429 reader.readNextStartElement();
430 if(reader.name()=="full_name")
431 _name=reader.readElementText();
432 reader.readNextStartElement();
433 if(reader.name()=="description")
434 _infoNote=reader.readElementText();
436 QString format = "png";
437 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
439 _infoNote="path=\""+initialPath+"\">"+"\n" + _name + " [" + _langFrom +
440 "-" + _langTo + "] "+ "(" + _type + ")";
442 dictionaryFile.close();
447 QString XdxfPlugin::removeAccents(QString string) {
448 string = string.toLower();
449 QString normalized = string.normalized(QString::NormalizationForm_KD);
450 foreach(QChar let, letters.keys())
451 normalized.replace(letters[let], QString(let));
453 normalized.replace(noLetter, "");
454 //qDebug() << "NORMALIZED: " << normalized;
461 QIcon* XdxfPlugin::icon() {
465 int XdxfPlugin::countWords() {
469 QFile dictionaryFile(path);
470 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
471 Q_EMIT notify(Notify::Warning,
472 QString("Xdxf file cannot be read for %1 dictionary")
474 qDebug()<<"Error: could not open file";
478 dictionaryFile.seek(0);
483 while(!dictionaryFile.atEnd()) {
484 line = dictionaryFile.readLine();
485 if(line.contains("<k>")) {
489 _wordsCount = wordsCount;
490 dictionaryFile.close();
494 bool XdxfPlugin::makeCache(QString dir) {
495 cachingDialog->setVisible(true);
496 QCoreApplication::processEvents();
498 QFileInfo dictFileN(_settings->value("path"));
500 cachePathN = QDir::homePath() + "/.mdictionary/"
501 + dictFileN.completeBaseName() + ".cache";
503 QFile dictionaryFile(dictFileN.filePath());
506 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
507 Q_EMIT updateCachingProgress(100, 0);
508 Q_EMIT notify(Notify::Warning,
509 QString("Xdxf file cannot be read for %1 dictionary")
513 QXmlStreamReader reader(&dictionaryFile);
515 db.setDatabaseName(cachePathN);
517 qDebug() << "Database error" << db.lastError().text() << endl;
518 Q_EMIT updateCachingProgress(100, 0);
519 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
520 "opened for %1 dictionary. Searching in xdxf file. "
521 "You may want to recache.").arg(name()));
524 QCoreApplication::processEvents();
526 cur.exec("PRAGMA synchronous = 0");
527 cur.exec("drop table dict");
528 QCoreApplication::processEvents();
529 cur.exec("create table dict(word text, normalized text ,translation text)");
543 while (!reader.atEnd() && !stopped) {
545 QCoreApplication::processEvents();
548 if(reader.tokenType() == QXmlStreamReader::StartElement) {
549 if(reader.name()=="k"){
550 a = reader.readElementText();
556 while(reader.name()!="ar" && !reader.atEnd()) {
557 if(reader.name()!="" && reader.name()!="k") {
558 if(reader.tokenType()==QXmlStreamReader::EndElement)
560 if(reader.tokenType()==QXmlStreamReader::StartElement)
562 temp+=reader.name().toString();
563 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
564 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
567 temp+= reader.text().toString().replace("<","<").replace(">",">");;
570 if(temp.at(0)==QChar('\n'))
572 temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
574 cur.prepare("insert into dict values(?,?,?)");
576 cur.addBindValue(removeAccents(a));
577 cur.addBindValue(temp);
580 int prog = counter*100/_wordsCount;
581 if(prog % 5 == 0 && lastProg != prog) {
583 Q_EMIT updateCachingProgress(prog,
591 cur.exec("select count(*) from dict");
594 cachingDialog->setVisible(false);
596 if(!cur.next() || countWords() != cur.value(0).toInt())
598 Q_EMIT updateCachingProgress(100, timer.restart());
599 Q_EMIT notify(Notify::Warning,
600 QString("Database caching error, please try againg."));
604 _settings->setValue("cache_path", cachePathN);
605 _settings->setValue("cached", "true");
612 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)