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);
63 QString XdxfPlugin::langFrom() const {
67 QString XdxfPlugin::langTo() const {
71 QString XdxfPlugin::name() const {
75 QString XdxfPlugin::type() const {
76 // return _settings->value("type");
80 QString XdxfPlugin::infoNote() const {
84 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
85 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
86 && word.indexOf("%")==-1)
89 return searchWordListCache(word,limit);
90 return searchWordListFile(word, limit);
93 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
95 QSet<Translation*> translations;
96 QString cacheFilePath = _settings->value("cache_path");
97 db.setDatabaseName(cacheFilePath);
99 qDebug() << "Database error" << db.lastError().text() << endl;
100 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
101 "opened for %1 dictionary. Searching in xdxf file. "
102 "You may want to recache.").arg(name()));
103 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 like ? limit ?");
115 cur.prepare("select word from dict where word like ? or normalized like ?");
116 cur.addBindValue(word);
117 cur.addBindValue(word);
119 cur.addBindValue(limit);
126 foreach(tran,translations) {
127 if(tran->key().toLower()==cur.value(0).toString().toLower())
130 if(ok) /*add key word to list*/
131 translations.insert(new TranslationXdxf(
132 cur.value(0).toString().toLower(),
136 return translations.toList();
139 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
142 QSet<Translation*> translations;
143 QFile dictionaryFile(path);
145 word = word.toLower();
146 //word = removeAccents(word);
149 QRegExp regWord(word);
150 regWord.setCaseSensitivity(Qt::CaseInsensitive);
151 regWord.setPatternSyntax(QRegExp::Wildcard);
152 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
153 qDebug()<<"Error: could not open file";
154 Q_EMIT notify(Notify::Warning,
155 QString("Xdxf file cannot be read for %1").arg(name()));
156 return translations.toList();
159 QXmlStreamReader reader(&dictionaryFile);
160 /*search words list*/
164 while(!reader.atEnd() && !stopped){
165 reader.readNextStartElement();
166 if(reader.name()=="ar") {
167 while(reader.name()!="k" && !reader.atEnd())
168 reader.readNextStartElement();
170 a = reader.readElementText();
171 if((regWord.exactMatch(a) || regWord.exactMatch(removeAccents(a))) &&
172 (i<limit || limit==0)) {
175 foreach(tran,translations) {
176 if(tran->key().toLower()==a.toLower())
177 ok=false; /*if key word is in the dictionary more that one */
179 if(ok) /*add key word to list*/
180 translations<<(new TranslationXdxf(a.toLower(),
183 if(i>=limit && limit!=0)
187 this->thread()->yieldCurrentThread();
190 dictionaryFile.close();
191 qDebug() << time.elapsed();
192 return translations.toList();
195 QString XdxfPlugin::search(QString key) {
196 // if(_settings->value("cached") == "true")
198 return searchCache(key);
199 return searchFile(key);
202 QString XdxfPlugin::searchCache(QString key) {
204 QString cacheFilePath = _settings->value("cache_path");
205 db.setDatabaseName(cacheFilePath);
209 qDebug() << "Database error" << db.lastError().text() << endl;
210 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
211 "opened for %1 dictionary. Searching in xdxf file. "
212 "You may want to recache.").arg(name()));
213 return searchFile(key);
217 cur.prepare("select translation from dict where word like ?");
218 cur.addBindValue(key);
221 result += cur.value(0).toString();
229 QString XdxfPlugin::searchFile(QString key) {
231 QFile dictionaryFile(path);
232 QString resultString("");
233 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
234 Q_EMIT notify(Notify::Warning,
235 QString("Xdxf file cannot be read for %1").arg(name()));
236 qDebug()<<"Error: could not open file";
239 QXmlStreamReader reader(&dictionaryFile);
244 while (!reader.atEnd()&& !stopped) {
246 if(reader.tokenType() == QXmlStreamReader::StartElement) {
247 if(reader.name()=="k") {
248 a = reader.readElementText();
249 if(a.toLower()==key.toLower())
255 while(reader.name()!="ar" && !reader.atEnd()) {
256 if(reader.name()!="" && reader.name()!="k") {
257 if(reader.tokenType()==QXmlStreamReader::EndElement)
259 if(reader.tokenType()==QXmlStreamReader::StartElement)
261 temp+=reader.name().toString();
262 if(reader.name().toString()=="c" &&
263 reader.tokenType()==QXmlStreamReader::StartElement)
264 temp= temp + tr(" c=\"") + reader.attributes().
265 value(tr("c")).toString() + tr("\"");
268 temp+= reader.text().toString().replace("<","<").
272 if(temp.at(0)==QChar('\n'))
274 resultString+=tr("<key>") + a +tr("</key>");
275 resultString+=tr("<t>") + temp + tr("</t>");
278 this->thread()->yieldCurrentThread();
281 dictionaryFile.close();
286 void XdxfPlugin::stop() {
290 DictDialog* XdxfPlugin::dictDialog() {
294 void XdxfPlugin::setPath(QString path){
296 _settings->setValue("path",path);
297 //getDictionaryInfo();
300 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
301 XdxfPlugin *plugin = new XdxfPlugin();
304 plugin->setPath(settings->value("path"));
305 QStringList list = settings->keys();
306 foreach(QString key, list)
307 plugin->settings()->setValue(key, settings->value(key));
310 plugin->db_name = plugin->_settings->value("type")
311 + plugin->_settings->value("path");
312 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
314 if(settings->value("cached").isEmpty() &&
315 settings->value("generateCache") == "true") {
316 plugin->makeCache("");
320 plugin->getDictionaryInfo();
324 bool XdxfPlugin::isAvailable() const {
328 void XdxfPlugin::setHash(uint _hash) {
332 uint XdxfPlugin::hash() const {
336 Settings* XdxfPlugin::settings() {
340 bool XdxfPlugin::isCached() {
341 if(_settings->value("cached") == "true")
346 void XdxfPlugin::setSettings(Settings *settings) {
348 QString oldPath = _settings->value("path");
349 if(oldPath != settings->value("path")) {
350 setPath(settings->value("path"));
353 if((_settings->value("cached") == "false" ||
354 _settings->value("cached").isEmpty()) &&
355 settings->value("generateCache") == "true") {
359 _settings->setValue("cached", "false");
361 foreach(QString key, settings->keys())
362 if(key != "generateCache")
363 _settings->setValue(key, settings->value(key));
366 emit settingsChanged();
369 void XdxfPlugin::getDictionaryInfo() {
370 QFile dictionaryFile(path);
371 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
372 Q_EMIT notify(Notify::Warning,
373 QString("Xdxf file cannot be read dictionary"));
374 qDebug()<<"Error: could not open file";
378 QXmlStreamReader reader(&dictionaryFile);
379 reader.readNextStartElement();
380 if(reader.name()=="xdxf") {
381 if(reader.attributes().hasAttribute("lang_from"))
382 _langFrom = reader.attributes().value("lang_from").toString();
383 if(reader.attributes().hasAttribute("lang_to"))
384 _langTo = reader.attributes().value("lang_to").toString();
386 reader.readNextStartElement();
387 if(reader.name()=="full_name")
388 _name=reader.readElementText();
389 reader.readNextStartElement();
390 if(reader.name()=="description")
391 _infoNote=reader.readElementText();
393 QString format = "png";
394 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
396 _infoNote="path=\""+initialPath+"\">"+"\n" + _name + " [" + _langFrom +
397 "-" + _langTo + "] "+ "(" + _type + ")";
399 dictionaryFile.close();
404 QIcon* XdxfPlugin::icon() {
408 int XdxfPlugin::countWords() {
412 QFile dictionaryFile(path);
413 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
414 Q_EMIT notify(Notify::Warning,
415 QString("Xdxf file cannot be read for %1 dictionary")
417 qDebug()<<"Error: could not open file";
421 dictionaryFile.seek(0);
426 while(!dictionaryFile.atEnd()) {
427 line = dictionaryFile.readLine();
428 if(line.contains("<k>")) {
432 _wordsCount = wordsCount;
433 dictionaryFile.close();
437 bool XdxfPlugin::makeCache(QString dir) {
438 cachingDialog->setVisible(true);
439 QCoreApplication::processEvents();
441 QFileInfo dictFileN(_settings->value("path"));
443 cachePathN = QDir::homePath() + "/.mdictionary/"
444 + dictFileN.completeBaseName() + ".cache";
446 QFile dictionaryFile(dictFileN.filePath());
449 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
450 Q_EMIT updateCachingProgress(100, 0);
451 Q_EMIT notify(Notify::Warning,
452 QString("Xdxf file cannot be read for %1 dictionary")
456 QXmlStreamReader reader(&dictionaryFile);
458 db.setDatabaseName(cachePathN);
460 qDebug() << "Database error" << db.lastError().text() << endl;
461 Q_EMIT updateCachingProgress(100, 0);
462 Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
463 "opened for %1 dictionary. Searching in xdxf file. "
464 "You may want to recache.").arg(name()));
467 QCoreApplication::processEvents();
469 cur.exec("PRAGMA synchronous = 0");
470 cur.exec("drop table dict");
471 QCoreApplication::processEvents();
472 cur.exec("create table dict(word text, normalized text ,translation text)");
486 while (!reader.atEnd() && !stopped) {
488 QCoreApplication::processEvents();
491 if(reader.tokenType() == QXmlStreamReader::StartElement) {
492 if(reader.name()=="k"){
493 a = reader.readElementText();
499 while(reader.name()!="ar" && !reader.atEnd()) {
500 if(reader.name()!="" && reader.name()!="k") {
501 if(reader.tokenType()==QXmlStreamReader::EndElement)
503 if(reader.tokenType()==QXmlStreamReader::StartElement)
505 temp+=reader.name().toString();
506 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
507 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
510 temp+= reader.text().toString().replace("<","<").replace(">",">");;
513 if(temp.at(0)==QChar('\n'))
515 temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
517 cur.prepare("insert into dict values(?,?,?)");
519 cur.addBindValue(removeAccents(a));
520 cur.addBindValue(temp);
523 int prog = counter*100/_wordsCount;
524 if(prog % 5 == 0 && lastProg != prog) {
526 Q_EMIT updateCachingProgress(prog,
534 cur.exec("select count(*) from dict");
537 cachingDialog->setVisible(false);
539 if(!cur.next() || countWords() != cur.value(0).toInt())
541 Q_EMIT updateCachingProgress(100, timer.restart());
542 Q_EMIT notify(Notify::Warning,
543 QString("Database caching error, please try againg."));
547 _settings->setValue("cache_path", cachePathN);
548 _settings->setValue("cached", "true");
555 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)