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 db.setDatabaseName(cacheFilePath);
97 if(!QFile::exists(cacheFilePath) || !db.open()) {
98 qDebug() << "Database error" << db.lastError().text() << endl;
99 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
100 "opened for %1 dictionary. Searching in XDXF file. "
101 "You may want to recache.").arg(name())));
102 _settings->setValue("cached","false");
103 return searchWordListFile(word, limit);
106 word = word.toLower();
107 word = word.replace("*", "%");
108 word = word.replace("?", "_");
112 cur.prepare("select word from dict where word like ? or normalized "
115 cur.prepare("select word from dict where word like ? or normalized "
117 cur.addBindValue(word);
118 cur.addBindValue(word);
120 cur.addBindValue(limit);
124 while(cur.next() && (i<limit || limit==0 ) ) {
128 foreach(tran,translations) {
129 if(tran->key().toLower()==cur.value(0).toString().toLower())
132 if(ok) { /*add key word to list*/
133 translations.insert(new TranslationXdxf(
134 cur.value(0).toString().toLower(),
140 return translations.toList();
144 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
145 QSet<Translation*> translations;
146 QFile dictionaryFile(_settings->value("path"));
147 word = word.toLower();
150 QRegExp regWord(word);
151 regWord.setCaseSensitivity(Qt::CaseInsensitive);
152 regWord.setPatternSyntax(QRegExp::Wildcard);
154 /*check xdxf file exist*/
155 if(!QFile::exists(_settings->value("path"))
156 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
157 qDebug()<<"Error: could not open file";
158 Q_EMIT notify(Notify::Warning,
159 QString(tr("XDXF file cannot be read for %1").arg(name())));
160 return translations.toList();
163 QXmlStreamReader reader(&dictionaryFile);
167 /*search words list*/
168 while(!reader.atEnd() && !stopped){
169 reader.readNextStartElement();
170 if(reader.name()=="ar") {
171 while(reader.name()!="k" && !reader.atEnd())
172 reader.readNextStartElement();
174 readKey = reader.readElementText();
175 if((regWord.exactMatch(readKey)
176 || regWord.exactMatch(removeAccents(readKey)))
177 && (i<limit || limit==0)) {
180 foreach(tran,translations) {
181 if(tran->key().toLower()==readKey.toLower())
182 ok=false; /*if key is in the dictionary more that one */
184 if(ok) { /*add key word to list*/
185 translations<<(new TranslationXdxf(readKey.toLower(),
189 if(i>=limit && limit!=0)
193 this->thread()->yieldCurrentThread();
196 dictionaryFile.close();
197 return translations.toList();
201 QString XdxfPlugin::search(QString key) {
203 return searchCache(key);
204 return searchFile(key);
208 QString XdxfPlugin::searchCache(QString key) {
210 QString cacheFilePath = _settings->value("cache_path");
211 db.setDatabaseName(cacheFilePath);
214 if(!QFile::exists(cacheFilePath) || !db.open()) {
215 qDebug() << "Database error" << db.lastError().text() << endl;
216 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
217 "opened for %1 dictionary. Searching in XDXF file. "
218 "You may want to recache.").arg(name())));
219 _settings->setValue("cached","false");
220 return searchFile(key);
225 cur.prepare("select translation from dict where word like ?");
226 cur.addBindValue(key);
229 result += cur.value(0).toString();
238 QString XdxfPlugin::searchFile(QString key) {
239 QFile dictionaryFile(_settings->value("path"));
240 QString resultString("");
243 /*check xdxf file exist*/
244 if(!QFile::exists(_settings->value("path"))
245 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
246 Q_EMIT notify(Notify::Warning,
247 QString(tr("XDXF file cannot be read for %1").arg(name())));
248 qDebug()<<"Error: could not open file";
252 QXmlStreamReader reader(&dictionaryFile);
257 /*search translations for word*/
258 while (!reader.atEnd()&& !stopped) {
260 if(reader.tokenType() == QXmlStreamReader::StartElement) {
261 if(reader.name()=="k") {
262 readKey = reader.readElementText();
263 if(readKey.toLower()==key.toLower())
269 while(reader.name()!="ar" && !reader.atEnd()) {
270 if(reader.name()!="" && reader.name()!="k") {
271 if(reader.tokenType()==QXmlStreamReader::EndElement)
273 if(reader.tokenType()==QXmlStreamReader::StartElement)
275 temp+=reader.name().toString();
276 if(reader.name().toString()=="c" &&
277 reader.tokenType()==QXmlStreamReader::StartElement)
278 temp= temp + " c=\"" + reader.attributes().
279 value("c").toString() + "\"";
282 temp+= reader.text().toString().replace("<","<").
286 if(temp.at(0)==QChar('\n'))
288 resultString+="<key>" + readKey +"</key>";
289 resultString+="<t>" + temp + "</t>";
292 this->thread()->yieldCurrentThread();
295 dictionaryFile.close();
300 void XdxfPlugin::stop() {
305 DictDialog* XdxfPlugin::dictDialog() {
310 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
311 XdxfPlugin *plugin = new XdxfPlugin();
313 plugin->setSettings(settings);
319 bool XdxfPlugin::isAvailable() const {
324 void XdxfPlugin::setHash(uint _hash) {
329 uint XdxfPlugin::hash() const {
334 Settings* XdxfPlugin::settings() {
336 Settings *returnSettings=new Settings;
337 QStringList list = _settings->keys();
338 foreach(QString key, list)
339 returnSettings->setValue(key,_settings->value(key));
340 return returnSettings;
346 bool XdxfPlugin::isCached() {
347 if(_settings->value("cached") == "true")
353 void XdxfPlugin::setSettings(const Settings *settings) {
356 bool isPathChange=false;
357 QString oldPath = _settings->value("path");
358 if(oldPath != settings->value("path")) {
359 if(oldPath!="" && _settings->value("cache_path")!="")
364 foreach(QString key, settings->keys()) {
365 if(key != "generateCache")
366 _settings->setValue(key, settings->value(key));
372 _settings->setValue("cached","false");
373 if(_settings->value("cached")=="true"
374 && _settings->value("cache_path")!="") {
375 db_name = _settings->value("type")
376 + _settings->value("cache_path");
377 db = QSqlDatabase::addDatabase("QSQLITE",db_name);
381 if((_settings->value("cached") == "false" ||
382 _settings->value("cached").isEmpty()) &&
383 settings->value("generateCache") == "true") {
388 else if (settings->value("generateCache") == "false") {
389 _settings->setValue("cached", "false");
394 Q_EMIT settingsChanged();
398 void XdxfPlugin::getDictionaryInfo() {
399 QFile dictionaryFile(_settings->value("path"));
400 if(!QFile::exists(_settings->value("path"))
401 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
402 Q_EMIT notify(Notify::Warning,
403 QString(tr("XDXF dictionary cannot be read from file")));
404 qDebug()<<"Error: could not open file";
408 QXmlStreamReader reader(&dictionaryFile);
409 reader.readNextStartElement();
410 if(reader.name()=="xdxf") {
411 if(reader.attributes().hasAttribute("lang_from"))
412 _langFrom = reader.attributes().value("lang_from").toString();
413 if(reader.attributes().hasAttribute("lang_to"))
414 _langTo = reader.attributes().value("lang_to").toString();
416 reader.readNextStartElement();
417 if(reader.name()=="full_name")
418 _name=reader.readElementText();
419 reader.readNextStartElement();
420 if(reader.name()=="description")
421 _infoNote=reader.readElementText();
423 QString format = "png";
424 QString initialPath = QDir::currentPath() + "/xdxf." + format;
426 _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-"
427 + _langTo + "] ( xdxf )";
428 dictionaryFile.close();
432 QIcon* XdxfPlugin::icon() {
437 int XdxfPlugin::countWords() {
440 QFile dictionaryFile(_settings->value("path"));
441 if(!QFile::exists(_settings->value("path"))
442 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
443 Q_EMIT notify(Notify::Warning,
444 QString(tr("XDXF file cannot be read for %1 dictionary")
446 qDebug()<<"Error: could not open file";
450 dictionaryFile.seek(0);
455 while(!dictionaryFile.atEnd()) {
456 line = dictionaryFile.readLine();
457 if(line.contains("<k>")) {
461 _wordsCount = wordsCount;
462 dictionaryFile.close();
467 bool XdxfPlugin::makeCache(QString) {
468 cachingDialog->setVisible(true);
469 QCoreApplication::processEvents();
470 QFileInfo dictFileN(_settings->value("path"));
474 /*create cache file name*/
477 cachePathN = QDir::homePath() + "/.mdictionary/"
478 + dictFileN.completeBaseName()+"."
479 +QString::number(i) + ".cache";
481 } while(QFile::exists(cachePathN));
483 db_name = _settings->value("type") + cachePathN;
484 db = QSqlDatabase::addDatabase("QSQLITE",db_name);
486 qDebug()<<QSqlDatabase::connectionNames().size();
487 foreach(QString name,QSqlDatabase::connectionNames())
489 /*checke errors (File open and db open)*/
490 QFile dictionaryFile(dictFileN.filePath());
491 if (!QFile::exists(_settings->value("path"))
492 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
493 Q_EMIT updateCachingProgress(100, 0);
494 Q_EMIT notify(Notify::Warning,
495 QString(tr("XDXF file cannot be read for %1 dictionary")
499 QXmlStreamReader reader(&dictionaryFile);
500 db.setDatabaseName(cachePathN);
502 qDebug() << "Database error" << db.lastError().text() << endl;
503 Q_EMIT updateCachingProgress(100, 0);
504 Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
505 "opened for %1 dictionary. Searching in XDXF file. "
506 "You may want to recache.").arg(name())));
511 QCoreApplication::processEvents();
513 cur.exec("PRAGMA synchronous = 0");
514 cur.exec("drop table dict");
515 QCoreApplication::processEvents();
516 cur.exec("create table dict(word text, normalized text ,translation text)");
526 _settings->setValue("strip_accents", "true");
529 /*add all words to db*/
530 while (!reader.atEnd() && !stopped) {
531 QCoreApplication::processEvents();
533 if(reader.tokenType() == QXmlStreamReader::StartElement) {
534 if(reader.name()=="k"){
535 readKey = reader.readElementText();
541 while(reader.name()!="ar" && !reader.atEnd()) {
542 if(reader.name()!="" && reader.name()!="k") {
543 if(reader.tokenType()==QXmlStreamReader::EndElement)
545 if(reader.tokenType()==QXmlStreamReader::StartElement)
547 temp+=reader.name().toString();
548 if(reader.name().toString()=="c"
549 && reader.tokenType()==QXmlStreamReader::StartElement) {
551 + reader.attributes().value("c").toString()
556 temp+= reader.text().toString().replace("<","<").replace(">"
560 if(temp.at(0)==QChar('\n'))
562 temp="<key>" + readKey + "</key>" + "<t>" + temp+ "</t>";
564 cur.prepare("insert into dict values(?,?,?)");
565 cur.addBindValue(readKey);
566 cur.addBindValue(removeAccents(readKey));
567 cur.addBindValue(temp);
570 int prog = counter*100/_wordsCount;
571 if(prog % 5 == 0 && lastProg != prog) {
572 Q_EMIT updateCachingProgress(prog,timer.restart());
578 cur.exec("select count(*) from dict");
579 cachingDialog->setVisible(false);
581 /*checke errors (wrong number of added words)*/
583 if(!cur.next() || countWords() != cur.value(0).toInt()) {
584 Q_EMIT updateCachingProgress(100, timer.restart());
585 Q_EMIT notify(Notify::Warning,
586 QString(tr("Database caching error, please try againg.")));
591 _settings->setValue("cache_path", cachePathN);
592 _settings->setValue("cached", "true");
599 void XdxfPlugin::clean() {
600 if(QFile::exists(_settings->value("cache_path"))) {
601 QFile(_settings->value("cache_path")).remove();
602 QSqlDatabase::removeDatabase(db_name);
607 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)