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"
28 #include <QXmlStreamReader>
30 #include "TranslationXdxf.h"
31 #include "../../../includes/settings.h"
33 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
34 _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
35 _type(tr("xdxf")), _infoNote(tr("")) {
37 _settings = new Settings();
38 _dictDialog = new XdxfDictDialog(this, this);
39 cachingDialog = new XdxfCachingDialog(this);
41 connect(cachingDialog, SIGNAL(cancelCaching()),
44 _settings->setValue("type","xdxf");
48 _icon = QIcon(":/icons/xdxf.png");
51 XdxfPlugin::~XdxfPlugin()
53 qDebug()<<"/n /n destruktor";
54 if(!db.connectionName().isEmpty())
55 db.removeDatabase(_type+path);
58 QString XdxfPlugin::langFrom() const {
62 QString XdxfPlugin::langTo() const {
66 QString XdxfPlugin::name() const {
70 QString XdxfPlugin::type() const {
71 // return _settings->value("type");
75 QString XdxfPlugin::infoNote() const {
79 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
80 //if(_settings->value("cached") == "true")
81 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
82 && word.indexOf("%")==-1)
85 return searchWordListCache(word,limit);
86 return searchWordListFile(word, limit);
89 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
91 QSet<Translation*> translations;
92 QString cacheFilePath = _settings->value("cache_path");
93 db.setDatabaseName(cacheFilePath);
95 qDebug() << "Database error" << db.lastError().text() << endl;
96 return searchWordListFile(word, limit);
100 word = word.toLower();
101 word = word.replace("*", "%");
102 word = word.replace("?", "_");
103 word = removeAccents(word);
108 cur.prepare("select word from dict where word like ? limit ?");
110 cur.prepare("select word from dict where word like ?");
111 cur.addBindValue(word);
113 cur.addBindValue(limit);
118 foreach(tran,translations) {
119 if(tran->key().toLower()==cur.value(0).toString().toLower())
122 if(ok) /*add key word to list*/
123 translations.insert(new TranslationXdxf(
124 cur.value(0).toString().toLower(),
128 return translations.toList();
131 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
132 QSet<Translation*> translations;
133 QFile dictionaryFile(path);
135 word = word.toLower();
136 word = removeAccents(word);
139 QRegExp regWord(word);
140 regWord.setCaseSensitivity(Qt::CaseInsensitive);
141 regWord.setPatternSyntax(QRegExp::Wildcard);
142 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
143 qDebug()<<"Error: could not open file";
144 return translations.toList();
147 QXmlStreamReader reader(&dictionaryFile);
148 /*search words list*/
151 while(!reader.atEnd() && !stopped){
152 reader.readNextStartElement();
153 if(reader.name()=="ar") {
154 while(reader.name()!="k" && !reader.atEnd())
155 reader.readNextStartElement();
157 a = reader.readElementText();
158 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
161 foreach(tran,translations) {
162 if(tran->key().toLower()==a.toLower())
163 ok=false; /*if key word is in the dictionary more that one */
165 if(ok) /*add key word to list*/
166 translations<<(new TranslationXdxf(a.toLower(),
169 if(i>=limit && limit!=0)
173 this->thread()->yieldCurrentThread();
176 dictionaryFile.close();
177 return translations.toList();
180 QString XdxfPlugin::search(QString key) {
181 // if(_settings->value("cached") == "true")
183 return searchCache(key);
184 return searchFile(key);
187 QString XdxfPlugin::searchCache(QString key) {
189 QString cacheFilePath = _settings->value("cache_path");
190 db.setDatabaseName(cacheFilePath);
194 qDebug() << "Database error" << db.lastError().text() << endl;
195 return searchFile(key);
199 cur.prepare("select translation from dict where word like ?");
200 cur.addBindValue(key);
203 result += cur.value(0).toString();
209 QString XdxfPlugin::searchFile(QString key) {
211 QFile dictionaryFile(path);
212 QString resultString("");
213 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
214 qDebug()<<"Error: could not open file";
217 QXmlStreamReader reader(&dictionaryFile);
225 while (!reader.atEnd()&& !stopped) {
227 if(reader.tokenType() == QXmlStreamReader::StartElement) {
228 if(reader.name()=="k") {
229 a = reader.readElementText();
230 if(a.toLower()==key.toLower())
236 while(reader.name()!="ar" && !reader.atEnd()) {
237 if(reader.name()!="" && reader.name()!="k") {
238 if(reader.tokenType()==QXmlStreamReader::EndElement)
240 if(reader.tokenType()==QXmlStreamReader::StartElement)
242 temp+=reader.name().toString();
243 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
244 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
247 temp+= reader.text().toString().replace("<","<").replace(">",">");
250 if(temp.at(0)==QChar('\n'))
252 resultString+=tr("<key>") + a +tr("</key>");
253 resultString+=tr("<t>") + temp + tr("</t>");
256 this->thread()->yieldCurrentThread();
259 dictionaryFile.close();
264 void XdxfPlugin::stop() {
268 DictDialog* XdxfPlugin::dictDialog() {
272 void XdxfPlugin::setPath(QString path){
274 _settings->setValue("path",path);
275 //getDictionaryInfo();
278 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
279 XdxfPlugin *plugin = new XdxfPlugin();
282 plugin->setPath(settings->value("path"));
283 QStringList list = settings->keys();
284 foreach(QString key, list)
285 plugin->settings()->setValue(key, settings->value(key));
288 plugin->db_name = plugin->_settings->value("type")
289 + plugin->_settings->value("path");
290 // if(!plugin->db.connectionName().isEmpty() || settings->value("generateCache")=="true")
291 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
293 if(settings->value("cached").isEmpty() &&
294 settings->value("generateCache") == "true") {
295 plugin->makeCache("");
299 plugin->getDictionaryInfo();
303 bool XdxfPlugin::isAvailable() const {
307 void XdxfPlugin::setHash(uint _hash) {
311 uint XdxfPlugin::hash() const {
315 Settings* XdxfPlugin::settings() {
319 bool XdxfPlugin::isCached() {
320 if(_settings->value("cached") == "true")
325 void XdxfPlugin::setSettings(Settings *settings) {
327 QString oldPath = _settings->value("path");
328 if(oldPath != settings->value("path")) {
329 setPath(settings->value("path"));
332 if((_settings->value("cached") == "false" ||
333 _settings->value("cached").isEmpty()) &&
334 settings->value("generateCache") == "true") {
338 _settings->setValue("cached", "false");
342 Q_EMIT settingsChanged();
345 void XdxfPlugin::getDictionaryInfo() {
346 QFile dictionaryFile(path);
347 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
348 qDebug()<<"Error: could not open file";
352 QXmlStreamReader reader(&dictionaryFile);
353 reader.readNextStartElement();
354 if(reader.name()=="xdxf") {
355 if(reader.attributes().hasAttribute("lang_from"))
356 _langFrom = reader.attributes().value("lang_from").toString();
357 if(reader.attributes().hasAttribute("lang_to"))
358 _langTo = reader.attributes().value("lang_to").toString();
360 reader.readNextStartElement();
361 if(reader.name()=="full_name")
362 _name=reader.readElementText();
363 reader.readNextStartElement();
364 if(reader.name()=="description")
365 _infoNote=reader.readElementText();
367 QString format = "png";
368 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
369 // qDebug()<<initialPath;
370 // QPixmap test(":/icons/xdxf.png");
371 // qDebug()<<QPixmap(test).save(initialPath,format.toAscii());
372 // qDebug()<<QPixmap("/home/jakub/star.jpg").save(initialPath,format.toAscii());
374 _infoNote="<info path=\""+initialPath+"\">"+"\n" + _name + " [" + _langFrom + "-" + _langTo + "] "+ "(" + _type + ")" + "</info>";
378 dictionaryFile.close();
381 QString XdxfPlugin::removeAccents(QString string) {
382 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
383 QString normalized = string.normalized(QString::NormalizationForm_D);
384 normalized = normalized;
385 for(int i=0; i<normalized.size(); i++) {
386 if( !normalized[i].isLetterOrNumber() &&
387 !normalized[i].isSpace() &&
388 !normalized[i].isDigit() &&
389 normalized[i] != '*' &&
390 normalized[i] != '%' &&
391 normalized[i] != '_' &&
392 normalized[i] != '?' ) {
393 normalized.remove(i,1);
399 QIcon* XdxfPlugin::icon() {
403 int XdxfPlugin::countWords() {
407 QFile dictionaryFile(path);
408 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
409 qDebug()<<"Error: could not open file";
413 dictionaryFile.seek(0);
418 while(!dictionaryFile.atEnd()) {
419 line = dictionaryFile.readLine();
420 if(line.contains("<k>")) {
424 _wordsCount = wordsCount;
425 dictionaryFile.close();
429 bool XdxfPlugin::makeCache(QString dir) {
430 cachingDialog->setVisible(true);
431 QCoreApplication::processEvents();
433 QFileInfo dictFileN(_settings->value("path"));
435 cachePathN = QDir::homePath() + "/.mdictionary/"
436 + dictFileN.completeBaseName() + ".cache";
438 QFile dictionaryFile(dictFileN.filePath());
441 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
445 QXmlStreamReader reader(&dictionaryFile);
447 db.setDatabaseName(cachePathN);
449 qDebug() << "Database error" << db.lastError().text() << endl;
452 QCoreApplication::processEvents();
454 cur.exec("PRAGMA synchronous = 0");
455 cur.exec("drop table dict");
456 QCoreApplication::processEvents();
457 cur.exec("create table dict(word text ,translation text)");
471 while (!reader.atEnd() && !stopped) {
473 QCoreApplication::processEvents();
476 if(reader.tokenType() == QXmlStreamReader::StartElement) {
477 if(reader.name()=="k"){
478 a = reader.readElementText();
484 while(reader.name()!="ar" && !reader.atEnd()) {
485 if(reader.name()!="" && reader.name()!="k") {
486 if(reader.tokenType()==QXmlStreamReader::EndElement)
488 if(reader.tokenType()==QXmlStreamReader::StartElement)
490 temp+=reader.name().toString();
491 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
492 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
495 temp+= reader.text().toString().replace("<","<").replace(">",">");;
498 if(temp.at(0)==QChar('\n'))
500 temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
502 cur.prepare("insert into dict values(?,?)");
504 cur.addBindValue(temp);
507 int prog = counter*100/_wordsCount;
508 if(prog % 5 == 0 && lastProg != prog) {
509 Q_EMIT updateCachingProgress(prog,
517 cur.exec("select count(*) from dict");
520 cachingDialog->setVisible(false);
522 if(!cur.next() || countWords() != cur.value(0).toInt())
527 _settings->setValue("cache_path", cachePathN);
528 _settings->setValue("cached", "true");
534 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)