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 "../../include/Notify.h"
30 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
31 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
32 _settings = new Settings();
33 _dictDialog = new XdxfDictDialog(this, this);
35 connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString, QWidget*)),
36 this, SIGNAL(notify(Notify::NotifyType,QString, QWidget*)));
38 cachingDialog = new XdxfCachingDialog(this);
42 _settings->setValue("type","xdxf");
43 _icon = QIcon("/usr/share/mdictionary/xdxf.png");
47 connect(cachingDialog, SIGNAL(cancelCaching()),
49 connect(this, SIGNAL(updateCachingProgress(int,int)),
50 cachingDialog, SLOT(updateCachingProgress(int,int)));
54 void XdxfPlugin::retranslate() {
55 QString locale = QLocale::system().name();
57 QTranslator *translator = new QTranslator(this);
60 translator->load(":/translations/dict_xdxf_pl");
62 translator->load(":/translations/dict_xdxf_en");
64 QCoreApplication::installTranslator(translator);
68 XdxfPlugin::~XdxfPlugin() {
75 QString XdxfPlugin::langFrom() const {
80 QString XdxfPlugin::langTo() const {
85 QString XdxfPlugin::name() const {
90 QString XdxfPlugin::type() const {
91 return QString("xdxf");
95 QString XdxfPlugin::infoNote() const {
100 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
101 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
102 word.indexOf("_")==-1 && word.indexOf("%")==-1)
106 return searchWordListCache(word,limit);
107 return searchWordListFile(word, limit);
111 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
113 QSet<Translation*> translations;
114 QString cacheFilePath = _settings->value("cache_path");
116 db.setDatabaseName(cacheFilePath);
117 if(!QFile::exists(cacheFilePath) || !db.open()) {
118 qDebug() << "Database error" << db.lastError().text() << endl;
119 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
120 "opened for %1 dictionary. Searching in XDXF file. "
121 "You may want to recache.").arg(name())));
122 _settings->setValue("cached","false");
123 return searchWordListFile(word, limit);
126 word = word.toLower();
127 word = word.replace("*", "%");
128 word = word.replace("?", "_");
132 cur.prepare("select word from dict where word like ? or normalized "
135 cur.prepare("select word from dict where word like ? or normalized "
137 cur.addBindValue(word);
138 cur.addBindValue(word);
140 cur.addBindValue(limit);
144 while(cur.next() && (i<limit || limit==0 ) ) {
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(),
160 return translations.toList();
164 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
165 QSet<Translation*> translations;
166 QFile dictionaryFile(_settings->value("path"));
167 word = word.toLower();
170 QRegExp regWord(word);
171 regWord.setCaseSensitivity(Qt::CaseInsensitive);
172 regWord.setPatternSyntax(QRegExp::Wildcard);
174 /*check xdxf file exist*/
175 if(!QFile::exists(_settings->value("path"))
176 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
177 qDebug()<<"Error: could not open file";
178 Q_EMIT notify(Notify::Warning,
179 QString(tr("XDXF file cannot be read for %1").arg(name())));
180 return translations.toList();
183 QXmlStreamReader reader(&dictionaryFile);
187 /*search words list*/
188 while(!reader.atEnd() && !stopped){
189 reader.readNextStartElement();
190 if(reader.name()=="ar") {
191 while(reader.name()!="k" && !reader.atEnd())
192 reader.readNextStartElement();
194 readKey = reader.readElementText();
195 if((regWord.exactMatch(readKey)
196 || regWord.exactMatch(removeAccents(readKey)))
197 && (i<limit || limit==0)) {
200 foreach(tran,translations) {
201 if(tran->key().toLower()==readKey.toLower())
202 ok=false; /*if key is in the dictionary more that one */
204 if(ok) { /*add key word to list*/
205 translations<<(new TranslationXdxf(readKey.toLower(),
209 if(i>=limit && limit!=0)
213 this->thread()->yieldCurrentThread();
216 dictionaryFile.close();
217 return translations.toList();
221 QString XdxfPlugin::search(QString key) {
223 return searchCache(key);
224 return searchFile(key);
228 QString XdxfPlugin::searchCache(QString key) {
230 QString cacheFilePath = _settings->value("cache_path");
231 db.setDatabaseName(cacheFilePath);
234 if(!QFile::exists(cacheFilePath) || !db.open()) {
235 qDebug() << "Database error" << db.lastError().text() << endl;
236 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
237 "opened for %1 dictionary. Searching in XDXF file. "
238 "You may want to recache.").arg(name())));
239 _settings->setValue("cached","false");
240 return searchFile(key);
245 cur.prepare("select translation from dict where word like ?");
246 cur.addBindValue(key);
249 result += cur.value(0).toString();
258 QString XdxfPlugin::searchFile(QString key) {
259 QFile dictionaryFile(_settings->value("path"));
260 QString resultString("");
263 /*check xdxf file exist*/
264 if(!QFile::exists(_settings->value("path"))
265 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
266 Q_EMIT notify(Notify::Warning,
267 QString(tr("XDXF file cannot be read for %1").arg(name())));
268 qDebug()<<"Error: could not open file";
272 QXmlStreamReader reader(&dictionaryFile);
277 /*search translations for word*/
278 while (!reader.atEnd()&& !stopped) {
280 if(reader.tokenType() == QXmlStreamReader::StartElement) {
281 if(reader.name()=="k") {
282 readKey = reader.readElementText();
283 if(readKey.toLower()==key.toLower())
289 while(reader.name()!="ar" && !reader.atEnd()) {
290 if(reader.name()!="" && reader.name()!="k") {
291 if(reader.tokenType()==QXmlStreamReader::EndElement)
293 if(reader.tokenType()==QXmlStreamReader::StartElement)
295 temp+=reader.name().toString();
296 if(reader.name().toString()=="c" &&
297 reader.tokenType()==QXmlStreamReader::StartElement)
298 temp= temp + " c=\"" + reader.attributes().
299 value("c").toString() + "\"";
302 temp+= reader.text().toString().replace("<","<").
306 if(temp.at(0)==QChar('\n'))
308 resultString+="<key>" + readKey +"</key>";
309 resultString+="<t>" + temp + "</t>";
312 this->thread()->yieldCurrentThread();
315 dictionaryFile.close();
320 void XdxfPlugin::stop() {
325 DictDialog* XdxfPlugin::dictDialog() {
330 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
331 XdxfPlugin *plugin = new XdxfPlugin();
332 if(settings && plugin->setSettings(settings)) {
342 bool XdxfPlugin::isAvailable() const {
347 void XdxfPlugin::setHash(uint _hash) {
352 uint XdxfPlugin::hash() const {
357 Settings* XdxfPlugin::settings() {
359 Settings *returnSettings=new Settings;
360 QStringList list = _settings->keys();
361 foreach(QString key, list)
362 returnSettings->setValue(key,_settings->value(key));
363 return returnSettings;
369 bool XdxfPlugin::isCached() {
370 if(_settings->value("cached") == "true")
376 bool XdxfPlugin::setSettings(const Settings *settings) {
378 bool isPathChange=false;
379 QString oldPath = _settings->value("path");
380 Settings *oldSettings = new Settings ;
382 if(oldPath != settings->value("path")) {
383 if(oldPath!="" && _settings->value("cache_path")!="")
388 foreach(QString key, _settings->keys())
389 oldSettings->setValue(key, _settings->value(key));
391 foreach(QString key, settings->keys()) {
392 if(key != "generateCache")
393 _settings->setValue(key, settings->value(key));
396 if(!getDictionaryInfo()) {
397 Q_EMIT notify(Notify::Warning,
398 QString(tr("XDXF file is in wrong format")));
399 qDebug()<<"Error: xdxf file is in wrong format";
401 _settings=oldSettings;
408 _settings->setValue("cached","false");
409 if(_settings->value("cached")=="true"
410 && _settings->value("cache_path")!="") {
411 db_name = _settings->value("type")
412 + _settings->value("cache_path");
413 db = QSqlDatabase::addDatabase("QSQLITE",db_name);
417 if((_settings->value("cached") == "false" ||
418 _settings->value("cached").isEmpty()) &&
419 settings->value("generateCache") == "true") {
424 else if (settings->value("generateCache") == "false") {
425 _settings->setValue("cached", "false");
430 Q_EMIT settingsChanged();
435 bool XdxfPlugin::getDictionaryInfo() {
436 QFile dictionaryFile(_settings->value("path"));
437 if(!QFile::exists(_settings->value("path"))
438 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
439 Q_EMIT notify(Notify::Warning,
440 QString(tr("XDXF dictionary cannot be read from file")));
441 qDebug()<<"Error: could not open file";
446 QXmlStreamReader reader(&dictionaryFile);
447 reader.readNextStartElement();
448 if(reader.name()=="xdxf") {
450 if(reader.attributes().hasAttribute("lang_from"))
451 _langFrom = reader.attributes().value("lang_from").toString();
452 if(reader.attributes().hasAttribute("lang_to"))
453 _langTo = reader.attributes().value("lang_to").toString();
455 reader.readNextStartElement();
456 if(reader.name()=="full_name")
457 _name=reader.readElementText();
458 reader.readNextStartElement();
459 if(reader.name()=="description")
460 _infoNote=reader.readElementText();
462 QString initialPath = "/usr/share/mdictionary/xdxf.png";
464 _infoNote=" path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-"
466 dictionaryFile.close();
473 QIcon* XdxfPlugin::icon() {
478 int XdxfPlugin::countWords() {
481 QFile dictionaryFile(_settings->value("path"));
482 if(!QFile::exists(_settings->value("path"))
483 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
484 Q_EMIT notify(Notify::Warning,
485 QString(tr("XDXF file cannot be read for %1 dictionary")
487 qDebug()<<"Error: could not open file";
491 dictionaryFile.seek(0);
496 while(!dictionaryFile.atEnd()) {
497 line = dictionaryFile.readLine();
498 if(line.contains("<k>")) {
502 _wordsCount = wordsCount;
503 dictionaryFile.close();
508 bool XdxfPlugin::makeCache(QString) {
509 cachingDialog->setVisible(true);
510 QCoreApplication::processEvents();
511 QFileInfo dictFileN(_settings->value("path"));
515 /*create cache file name*/
518 cachePathN = QDir::homePath() + "/.mdictionary/"
519 + dictFileN.completeBaseName()+"."
520 +QString::number(i) + ".cache";
522 } while(QFile::exists(cachePathN));
524 db_name = _settings->value("type") + cachePathN;
525 db = QSqlDatabase::addDatabase("QSQLITE",db_name);
527 /*checke errors (File open and db open)*/
528 QFile dictionaryFile(dictFileN.filePath());
529 if (!QFile::exists(_settings->value("path"))
530 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
531 Q_EMIT updateCachingProgress(100, 0);
532 Q_EMIT notify(Notify::Warning,
533 QString(tr("XDXF file cannot be read for %1 dictionary")
537 QXmlStreamReader reader(&dictionaryFile);
538 db.setDatabaseName(cachePathN);
540 qDebug() << "Database error" << db.lastError().text() << endl;
541 Q_EMIT updateCachingProgress(100, 0);
542 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
543 "opened for %1 dictionary. Searching in XDXF file. "
544 "You may want to recache.").arg(name())));
549 QCoreApplication::processEvents();
551 cur.exec("PRAGMA synchronous = 0");
552 cur.exec("drop table dict");
553 QCoreApplication::processEvents();
554 cur.exec("create table dict(word text, normalized text ,translation text)");
564 _settings->setValue("strip_accents", "true");
567 /*add all words to db*/
568 while (!reader.atEnd() && !stopped) {
569 QCoreApplication::processEvents();
571 if(reader.tokenType() == QXmlStreamReader::StartElement) {
572 if(reader.name()=="k"){
573 readKey = reader.readElementText();
579 while(reader.name()!="ar" && !reader.atEnd()) {
580 if(reader.name()!="" && reader.name()!="k") {
581 if(reader.tokenType()==QXmlStreamReader::EndElement)
583 if(reader.tokenType()==QXmlStreamReader::StartElement)
585 temp+=reader.name().toString();
586 if(reader.name().toString()=="c"
587 && reader.tokenType()==QXmlStreamReader::StartElement) {
589 + reader.attributes().value("c").toString()
594 temp+= reader.text().toString().replace("<","<").replace(">"
598 if(temp.at(0)==QChar('\n'))
600 temp="<key>" + readKey + "</key>" + "<t>" + temp+ "</t>";
602 cur.prepare("insert into dict values(?,?,?)");
603 cur.addBindValue(readKey);
604 cur.addBindValue(removeAccents(readKey));
605 cur.addBindValue(temp);
608 int prog = counter*100/_wordsCount;
609 if(prog % 5 == 0 && lastProg != prog) {
610 Q_EMIT updateCachingProgress(prog,timer.restart());
616 cur.exec("select count(*) from dict");
617 cachingDialog->setVisible(false);
619 /*checke errors (wrong number of added words)*/
621 if(!cur.next() || countWords() != cur.value(0).toInt()) {
622 Q_EMIT updateCachingProgress(100, timer.restart());
623 Q_EMIT notify(Notify::Warning,
624 QString(tr("Database caching error, please try again.")));
629 _settings->setValue("cache_path", cachePathN);
630 _settings->setValue("cached", "true");
637 void XdxfPlugin::clean() {
638 if(QFile::exists(_settings->value("cache_path"))) {
639 QFile(_settings->value("cache_path")).remove();
640 QSqlDatabase::removeDatabase(db_name);
645 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)