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(tr("")), _langTo(tr("")),_name(tr("")),
32 _type(tr("xdxf")), _infoNote(tr("")) {
34 _settings = new Settings();
35 _dictDialog = new XdxfDictDialog(this);
36 cachingDialog = new XdxfCachingDialog();
38 connect(cachingDialog, SIGNAL(cancelCaching()),
41 connect(this, SIGNAL(updateCachingProgress(int,int)),
42 cachingDialog, SLOT(updateCachingProgress(int,int)));
44 _settings->setValue("type","xdxf");
48 _icon = QIcon(":/icons/xdxf.png");
54 XdxfPlugin::~XdxfPlugin() {
59 QString XdxfPlugin::langFrom() const {
63 QString XdxfPlugin::langTo() const {
67 QString XdxfPlugin::name() const {
71 QString XdxfPlugin::type() const {
75 QString XdxfPlugin::infoNote() const {
79 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
80 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
81 && word.indexOf("%")==-1)
84 return searchWordListCache(word,limit);
85 return searchWordListFile(word, limit);
88 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
90 QSet<Translation*> translations;
91 QString cacheFilePath = _settings->value("cache_path");
92 db.setDatabaseName(cacheFilePath);
94 qDebug() << "Database error" << db.lastError().text() << endl;
95 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
96 "opened for %1 dictionary. Searching in xdxf file. "
97 "You may want to recache.").arg(name()));
98 return searchWordListFile(word, limit);
102 word = word.toLower();
103 word = word.replace("*", "%");
104 word = word.replace("?", "_");
108 cur.prepare("select word from dict where word like ? or normalized like ? limit ?");
110 cur.prepare("select word from dict where word like ? or normalized like ?");
111 cur.addBindValue(word);
112 cur.addBindValue(word);
114 cur.addBindValue(limit);
121 foreach(tran,translations) {
122 if(tran->key().toLower()==cur.value(0).toString().toLower())
125 if(ok) /*add key word to list*/
126 translations.insert(new TranslationXdxf(
127 cur.value(0).toString().toLower(),
131 return translations.toList();
134 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
137 QSet<Translation*> translations;
138 QFile dictionaryFile(path);
140 word = word.toLower();
141 //word = removeAccents(word);
144 QRegExp regWord(word);
145 regWord.setCaseSensitivity(Qt::CaseInsensitive);
146 regWord.setPatternSyntax(QRegExp::Wildcard);
147 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
148 qDebug()<<"Error: could not open file";
149 Q_EMIT notify(Notify::Warning,
150 QString("Xdxf file cannot be read for %1").arg(name()));
151 return translations.toList();
154 QXmlStreamReader reader(&dictionaryFile);
155 /*search words list*/
159 while(!reader.atEnd() && !stopped){
160 reader.readNextStartElement();
161 if(reader.name()=="ar") {
162 while(reader.name()!="k" && !reader.atEnd())
163 reader.readNextStartElement();
165 a = reader.readElementText();
166 if((regWord.exactMatch(a) || regWord.exactMatch(removeAccents(a))) &&
167 (i<limit || limit==0)) {
170 foreach(tran,translations) {
171 if(tran->key().toLower()==a.toLower())
172 ok=false; /*if key word is in the dictionary more that one */
174 if(ok) /*add key word to list*/
175 translations<<(new TranslationXdxf(a.toLower(),
178 if(i>=limit && limit!=0)
182 this->thread()->yieldCurrentThread();
185 dictionaryFile.close();
186 qDebug() << time.elapsed();
187 return translations.toList();
190 QString XdxfPlugin::search(QString key) {
191 // if(_settings->value("cached") == "true")
193 return searchCache(key);
194 return searchFile(key);
197 QString XdxfPlugin::searchCache(QString key) {
199 QString cacheFilePath = _settings->value("cache_path");
200 db.setDatabaseName(cacheFilePath);
204 qDebug() << "Database error" << db.lastError().text() << endl;
205 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
206 "opened for %1 dictionary. Searching in xdxf file. "
207 "You may want to recache.").arg(name()));
208 return searchFile(key);
212 cur.prepare("select translation from dict where word like ?");
213 cur.addBindValue(key);
216 result += cur.value(0).toString();
224 QString XdxfPlugin::searchFile(QString key) {
226 QFile dictionaryFile(path);
227 QString resultString("");
228 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
229 Q_EMIT notify(Notify::Warning,
230 QString("Xdxf file cannot be read for %1").arg(name()));
231 qDebug()<<"Error: could not open file";
234 QXmlStreamReader reader(&dictionaryFile);
239 while (!reader.atEnd()&& !stopped) {
241 if(reader.tokenType() == QXmlStreamReader::StartElement) {
242 if(reader.name()=="k") {
243 a = reader.readElementText();
244 if(a.toLower()==key.toLower())
250 while(reader.name()!="ar" && !reader.atEnd()) {
251 if(reader.name()!="" && reader.name()!="k") {
252 if(reader.tokenType()==QXmlStreamReader::EndElement)
254 if(reader.tokenType()==QXmlStreamReader::StartElement)
256 temp+=reader.name().toString();
257 if(reader.name().toString()=="c" &&
258 reader.tokenType()==QXmlStreamReader::StartElement)
259 temp= temp + tr(" c=\"") + reader.attributes().
260 value(tr("c")).toString() + tr("\"");
263 temp+= reader.text().toString().replace("<","<").
267 if(temp.at(0)==QChar('\n'))
269 resultString+=tr("<key>") + a +tr("</key>");
270 resultString+=tr("<t>") + temp + tr("</t>");
273 this->thread()->yieldCurrentThread();
276 dictionaryFile.close();
281 void XdxfPlugin::stop() {
285 DictDialog* XdxfPlugin::dictDialog() {
289 void XdxfPlugin::setPath(QString path){
291 _settings->setValue("path",path);
292 //getDictionaryInfo();
295 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
296 XdxfPlugin *plugin = new XdxfPlugin();
298 plugin->setPath(settings->value("path"));
299 QStringList list = settings->keys();
301 plugin->db_name = plugin->_settings->value("type")
302 + plugin->_settings->value("path");
303 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
305 if(settings->value("cached").isEmpty() &&
306 settings->value("generateCache") == "true") {
307 plugin->makeCache("");
309 foreach(QString key, list)
310 if(key != "generateCache")
311 plugin->settings()->setValue(key, settings->value(key));
314 plugin->getDictionaryInfo();
318 bool XdxfPlugin::isAvailable() const {
324 void XdxfPlugin::setHash(uint _hash) {
330 uint XdxfPlugin::hash() const {
336 Settings* XdxfPlugin::settings() {
342 bool XdxfPlugin::isCached() {
343 if(_settings->value("cached") == "true")
350 void XdxfPlugin::setSettings(Settings *settings) {
352 QString oldPath = _settings->value("path");
353 if(oldPath != settings->value("path")) {
354 setPath(settings->value("path"));
357 foreach(QString key, settings->keys())
358 if(key != "generateCache")
359 _settings->setValue(key, settings->value(key));
361 if((_settings->value("cached") == "false" ||
362 _settings->value("cached").isEmpty()) &&
363 settings->value("generateCache") == "true") {
366 else if (settings->value("generateCache") != "true") {
367 _settings->setValue("cached", "false");
373 Q_EMIT settingsChanged();
378 void XdxfPlugin::getDictionaryInfo() {
379 QFile dictionaryFile(path);
380 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
381 Q_EMIT notify(Notify::Warning,
382 QString("Xdxf file cannot be read dictionary"));
383 qDebug()<<"Error: could not open file";
387 QXmlStreamReader reader(&dictionaryFile);
388 reader.readNextStartElement();
389 if(reader.name()=="xdxf") {
390 if(reader.attributes().hasAttribute("lang_from"))
391 _langFrom = reader.attributes().value("lang_from").toString();
392 if(reader.attributes().hasAttribute("lang_to"))
393 _langTo = reader.attributes().value("lang_to").toString();
395 reader.readNextStartElement();
396 if(reader.name()=="full_name")
397 _name=reader.readElementText();
398 reader.readNextStartElement();
399 if(reader.name()=="description")
400 _infoNote=reader.readElementText();
402 QString format = "png";
403 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
405 _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-" + _langTo + "] (" + _type + ")";
406 dictionaryFile.close();
411 QIcon* XdxfPlugin::icon() {
415 int XdxfPlugin::countWords() {
419 QFile dictionaryFile(path);
420 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
421 Q_EMIT notify(Notify::Warning,
422 QString("Xdxf file cannot be read for %1 dictionary")
424 qDebug()<<"Error: could not open file";
428 dictionaryFile.seek(0);
433 while(!dictionaryFile.atEnd()) {
434 line = dictionaryFile.readLine();
435 if(line.contains("<k>")) {
439 _wordsCount = wordsCount;
440 dictionaryFile.close();
444 bool XdxfPlugin::makeCache(QString dir) {
445 cachingDialog->setVisible(true);
446 QCoreApplication::processEvents();
448 QFileInfo dictFileN(_settings->value("path"));
450 cachePathN = QDir::homePath() + "/.mdictionary/"
451 + dictFileN.completeBaseName() + ".cache";
453 QFile dictionaryFile(dictFileN.filePath());
456 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
457 Q_EMIT updateCachingProgress(100, 0);
458 Q_EMIT notify(Notify::Warning,
459 QString("Xdxf file cannot be read for %1 dictionary")
463 QXmlStreamReader reader(&dictionaryFile);
466 db.setDatabaseName(cachePathN);
468 qDebug() << "Database error" << db.lastError().text() << endl;
469 Q_EMIT updateCachingProgress(100, 0);
470 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
471 "opened for %1 dictionary. Searching in xdxf file. "
472 "You may want to recache.").arg(name()));
475 QCoreApplication::processEvents();
477 cur.exec("PRAGMA synchronous = 0");
478 cur.exec("drop table dict");
479 QCoreApplication::processEvents();
480 cur.exec("create table dict(word text, normalized text ,translation text)");
492 QString stripAcc = settings()->value("strip_accents");
493 settings()->setValue("strip_accents", "true");
497 while (!reader.atEnd() && !stopped) {
499 QCoreApplication::processEvents();
502 if(reader.tokenType() == QXmlStreamReader::StartElement) {
503 if(reader.name()=="k"){
504 a = reader.readElementText();
510 while(reader.name()!="ar" && !reader.atEnd()) {
511 if(reader.name()!="" && reader.name()!="k") {
512 if(reader.tokenType()==QXmlStreamReader::EndElement)
514 if(reader.tokenType()==QXmlStreamReader::StartElement)
516 temp+=reader.name().toString();
517 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
518 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
521 temp+= reader.text().toString().replace("<","<").replace(">",">");;
524 if(temp.at(0)==QChar('\n'))
526 temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
528 cur.prepare("insert into dict values(?,?,?)");
530 cur.addBindValue(removeAccents(a));
531 cur.addBindValue(temp);
534 int prog = counter*100/_wordsCount;
535 if(prog % 5 == 0 && lastProg != prog) {
536 Q_EMIT updateCachingProgress(prog,
544 cur.exec("select count(*) from dict");
545 settings()->setValue("strip_accents", stripAcc);
548 cachingDialog->setVisible(false);
550 if(!cur.next() || countWords() != cur.value(0).toInt())
552 Q_EMIT updateCachingProgress(100, timer.restart());
553 Q_EMIT notify(Notify::Warning,
554 QString("Database caching error, please try againg."));
558 _settings->setValue("cache_path", cachePathN);
559 _settings->setValue("cached", "true");
566 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)