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
23 \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
26 #include "xdxfplugin.h"
28 #include "../../../includes/Notify.h"
30 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
31 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
32 _settings = new Settings();
33 _dictDialog = new XdxfDictDialog(this);
34 cachingDialog = new XdxfCachingDialog(this);
36 _settings->setValue("type","xdxf");
37 _icon = QIcon(":/icons/xdxf.png");
41 connect(cachingDialog, SIGNAL(cancelCaching()),
43 connect(this, SIGNAL(updateCachingProgress(int,int)),
44 cachingDialog, SLOT(updateCachingProgress(int,int)));
49 XdxfPlugin::~XdxfPlugin() {
55 QString XdxfPlugin::langFrom() const {
60 QString XdxfPlugin::langTo() const {
65 QString XdxfPlugin::name() const {
70 QString XdxfPlugin::type() const {
71 return QString("xdxf");
75 QString XdxfPlugin::infoNote() const {
80 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
81 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
82 word.indexOf("_")==-1 && word.indexOf("%")==-1)
86 return searchWordListCache(word,limit);
87 return searchWordListFile(word, limit);
91 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
93 QSet<Translation*> translations;
94 QString cacheFilePath = _settings->value("cache_path");
96 // QSqlDatabase::removeDatabase(cacheFilePath);
97 db.setDatabaseName(cacheFilePath);
98 if(!QFile::exists(cacheFilePath) || !db.open()) {
99 qDebug() << "Database error" << db.lastError().text() << endl;
100 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
101 "opened for %1 dictionary. Searching in XDXF file. "
102 "You may want to recache.").arg(name())));
103 _settings->setValue("cached","false");
104 return searchWordListFile(word, limit);
107 word = word.toLower();
108 word = word.replace("*", "%");
109 word = word.replace("?", "_");
113 cur.prepare("select word from dict where word like ? or normalized "
116 cur.prepare("select word from dict where word like ? or normalized "
118 cur.addBindValue(word);
119 cur.addBindValue(word);
121 cur.addBindValue(limit);
125 while(cur.next() && (i<limit || limit==0 ) ) {
129 foreach(tran,translations) {
130 if(tran->key().toLower()==cur.value(0).toString().toLower())
133 if(ok) { /*add key word to list*/
134 translations.insert(new TranslationXdxf(
135 cur.value(0).toString().toLower(),
141 return translations.toList();
145 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
146 QSet<Translation*> translations;
147 QFile dictionaryFile(_settings->value("path"));
148 word = word.toLower();
151 QRegExp regWord(word);
152 regWord.setCaseSensitivity(Qt::CaseInsensitive);
153 regWord.setPatternSyntax(QRegExp::Wildcard);
155 /*check xdxf file exist*/
156 if(!QFile::exists(_settings->value("path"))
157 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
158 qDebug()<<"Error: could not open file";
159 Q_EMIT notify(Notify::Warning,
160 QString(tr("XDXF file cannot be read for %1").arg(name())));
161 return translations.toList();
164 QXmlStreamReader reader(&dictionaryFile);
168 /*search words list*/
169 while(!reader.atEnd() && !stopped){
170 reader.readNextStartElement();
171 if(reader.name()=="ar") {
172 while(reader.name()!="k" && !reader.atEnd())
173 reader.readNextStartElement();
175 readKey = reader.readElementText();
176 if((regWord.exactMatch(readKey)
177 || regWord.exactMatch(removeAccents(readKey)))
178 && (i<limit || limit==0)) {
181 foreach(tran,translations) {
182 if(tran->key().toLower()==readKey.toLower())
183 ok=false; /*if key is in the dictionary more that one */
185 if(ok) { /*add key word to list*/
186 translations<<(new TranslationXdxf(readKey.toLower(),
190 if(i>=limit && limit!=0)
194 this->thread()->yieldCurrentThread();
197 dictionaryFile.close();
198 return translations.toList();
202 QString XdxfPlugin::search(QString key) {
204 return searchCache(key);
205 return searchFile(key);
209 QString XdxfPlugin::searchCache(QString key) {
211 QString cacheFilePath = _settings->value("cache_path");
212 db.setDatabaseName(cacheFilePath);
215 if(!QFile::exists(cacheFilePath) || !db.open()) {
216 qDebug() << "Database error" << db.lastError().text() << endl;
217 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
218 "opened for %1 dictionary. Searching in XDXF file. "
219 "You may want to recache.").arg(name())));
220 _settings->setValue("cached","false");
221 return searchFile(key);
226 cur.prepare("select translation from dict where word like ?");
227 cur.addBindValue(key);
230 result += cur.value(0).toString();
239 QString XdxfPlugin::searchFile(QString key) {
240 QFile dictionaryFile(_settings->value("path"));
241 QString resultString("");
244 /*check xdxf file exist*/
245 if(!QFile::exists(_settings->value("path"))
246 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
247 Q_EMIT notify(Notify::Warning,
248 QString(tr("XDXF file cannot be read for %1").arg(name())));
249 qDebug()<<"Error: could not open file";
253 QXmlStreamReader reader(&dictionaryFile);
258 /*search translations for word*/
259 while (!reader.atEnd()&& !stopped) {
261 if(reader.tokenType() == QXmlStreamReader::StartElement) {
262 if(reader.name()=="k") {
263 readKey = reader.readElementText();
264 if(readKey.toLower()==key.toLower())
270 while(reader.name()!="ar" && !reader.atEnd()) {
271 if(reader.name()!="" && reader.name()!="k") {
272 if(reader.tokenType()==QXmlStreamReader::EndElement)
274 if(reader.tokenType()==QXmlStreamReader::StartElement)
276 temp+=reader.name().toString();
277 if(reader.name().toString()=="c" &&
278 reader.tokenType()==QXmlStreamReader::StartElement)
279 temp= temp + " c=\"" + reader.attributes().
280 value("c").toString() + "\"";
283 temp+= reader.text().toString().replace("<","<").
287 if(temp.at(0)==QChar('\n'))
289 resultString+="<key>" + readKey +"</key>";
290 resultString+="<t>" + temp + "</t>";
293 this->thread()->yieldCurrentThread();
296 dictionaryFile.close();
301 void XdxfPlugin::stop() {
306 DictDialog* XdxfPlugin::dictDialog() {
311 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
312 XdxfPlugin *plugin = new XdxfPlugin();
314 plugin->setSettings(settings);
320 bool XdxfPlugin::isAvailable() const {
325 void XdxfPlugin::setHash(uint _hash) {
330 uint XdxfPlugin::hash() const {
335 Settings* XdxfPlugin::settings() {
337 Settings *returnSettings=new Settings;
338 QStringList list = _settings->keys();
339 foreach(QString key, list)
340 returnSettings->setValue(key,_settings->value(key));
341 return returnSettings;
347 bool XdxfPlugin::isCached() {
348 if(_settings->value("cached") == "true")
354 void XdxfPlugin::setSettings(const Settings *settings) {
357 bool isPathChange=false;
358 QString oldPath = _settings->value("path");
359 if(oldPath != settings->value("path")) {
360 if(oldPath!="" && _settings->value("cache_path")!="")
365 foreach(QString key, settings->keys()) {
366 if(key != "generateCache")
367 _settings->setValue(key, settings->value(key));
373 _settings->setValue("cached","false");
374 QSqlDatabase::removeDatabase(db_name);
376 db_name = _settings->value("type") + _settings->value("path");
377 db = QSqlDatabase::addDatabase("QSQLITE",db_name);
380 if((_settings->value("cached") == "false" ||
381 _settings->value("cached").isEmpty()) &&
382 settings->value("generateCache") == "true") {
387 else if (settings->value("generateCache") == "false") {
388 _settings->setValue("cached", "false");
393 Q_EMIT settingsChanged();
397 void XdxfPlugin::getDictionaryInfo() {
398 QFile dictionaryFile(_settings->value("path"));
399 if(!QFile::exists(_settings->value("path"))
400 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
401 Q_EMIT notify(Notify::Warning,
402 QString(tr("XDXF dictionary cannot be read from file")));
403 qDebug()<<"Error: could not open file";
407 QXmlStreamReader reader(&dictionaryFile);
408 reader.readNextStartElement();
409 if(reader.name()=="xdxf") {
410 if(reader.attributes().hasAttribute("lang_from"))
411 _langFrom = reader.attributes().value("lang_from").toString();
412 if(reader.attributes().hasAttribute("lang_to"))
413 _langTo = reader.attributes().value("lang_to").toString();
415 reader.readNextStartElement();
416 if(reader.name()=="full_name")
417 _name=reader.readElementText();
418 reader.readNextStartElement();
419 if(reader.name()=="description")
420 _infoNote=reader.readElementText();
422 QString format = "png";
423 QString initialPath = QDir::currentPath() + "/xdxf." + format;
425 _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-"
426 + _langTo + "] ( xdxf )";
427 dictionaryFile.close();
431 QIcon* XdxfPlugin::icon() {
436 int XdxfPlugin::countWords() {
439 QFile dictionaryFile(_settings->value("path"));
440 if(!QFile::exists(_settings->value("path"))
441 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
442 Q_EMIT notify(Notify::Warning,
443 QString(tr("XDXF file cannot be read for %1 dictionary")
445 qDebug()<<"Error: could not open file";
449 dictionaryFile.seek(0);
454 while(!dictionaryFile.atEnd()) {
455 line = dictionaryFile.readLine();
456 if(line.contains("<k>")) {
460 _wordsCount = wordsCount;
461 dictionaryFile.close();
466 bool XdxfPlugin::makeCache(QString) {
467 cachingDialog->setVisible(true);
468 QCoreApplication::processEvents();
469 QFileInfo dictFileN(_settings->value("path"));
473 /*create cache file name*/
476 cachePathN = QDir::homePath() + "/.mdictionary/"
477 + dictFileN.completeBaseName()+"."
478 +QString::number(i) + ".cache";
480 } while(QFile::exists(cachePathN));
482 /*checke errors (File open and db open)*/
483 QFile dictionaryFile(dictFileN.filePath());
484 if (!QFile::exists(_settings->value("path"))
485 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
486 Q_EMIT updateCachingProgress(100, 0);
487 Q_EMIT notify(Notify::Warning,
488 QString(tr("XDXF file cannot be read for %1 dictionary")
492 QXmlStreamReader reader(&dictionaryFile);
493 db.setDatabaseName(cachePathN);
495 qDebug() << "Database error" << db.lastError().text() << endl;
496 Q_EMIT updateCachingProgress(100, 0);
497 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
498 "opened for %1 dictionary. Searching in XDXF file. "
499 "You may want to recache.").arg(name())));
504 QCoreApplication::processEvents();
506 cur.exec("PRAGMA synchronous = 0");
507 cur.exec("drop table dict");
508 QCoreApplication::processEvents();
509 cur.exec("create table dict(word text, normalized text ,translation text)");
519 _settings->setValue("strip_accents", "true");
522 /*add all words to db*/
523 while (!reader.atEnd() && !stopped) {
524 QCoreApplication::processEvents();
526 if(reader.tokenType() == QXmlStreamReader::StartElement) {
527 if(reader.name()=="k"){
528 readKey = reader.readElementText();
534 while(reader.name()!="ar" && !reader.atEnd()) {
535 if(reader.name()!="" && reader.name()!="k") {
536 if(reader.tokenType()==QXmlStreamReader::EndElement)
538 if(reader.tokenType()==QXmlStreamReader::StartElement)
540 temp+=reader.name().toString();
541 if(reader.name().toString()=="c"
542 && reader.tokenType()==QXmlStreamReader::StartElement) {
544 + reader.attributes().value("c").toString()
549 temp+= reader.text().toString().replace("<","<").replace(">"
553 if(temp.at(0)==QChar('\n'))
555 temp="<key>" + readKey + "</key>" + "<t>" + temp+ "</t>";
557 cur.prepare("insert into dict values(?,?,?)");
558 cur.addBindValue(readKey);
559 cur.addBindValue(removeAccents(readKey));
560 cur.addBindValue(temp);
563 int prog = counter*100/_wordsCount;
564 if(prog % 5 == 0 && lastProg != prog) {
565 Q_EMIT updateCachingProgress(prog,timer.restart());
571 cur.exec("select count(*) from dict");
572 cachingDialog->setVisible(false);
574 /*checke errors (wrong number of added words)*/
576 if(!cur.next() || countWords() != cur.value(0).toInt()) {
577 Q_EMIT updateCachingProgress(100, timer.restart());
578 Q_EMIT notify(Notify::Warning,
579 QString(tr("Database caching error, please try againg.")));
584 _settings->setValue("cache_path", cachePathN);
585 _settings->setValue("cached", "true");
592 void XdxfPlugin::clean() {
593 if(QFile::exists(_settings->value("cache_path")))
594 QFile(_settings->value("cache_path")).remove();
598 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)