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 #include "xdxfplugin.h"
25 #include <QXmlStreamReader>
27 #include "TranslationXdxf.h"
28 #include "../../../includes/settings.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, this);
36 cachingDialog = new XdxfCachingDialog(this);
38 connect(cachingDialog, SIGNAL(cancelCaching()),
41 _settings->setValue("type","xdxf");
45 _icon = QIcon(":/icons/xdxf.png");
48 QString XdxfPlugin::langFrom() const {
52 QString XdxfPlugin::langTo() const {
56 QString XdxfPlugin::name() const {
60 QString XdxfPlugin::type() const {
61 // return _settings->value("type");
65 QString XdxfPlugin::infoNote() const {
69 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
70 //if(_settings->value("cached") == "true")
71 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
72 && word.indexOf("%")==-1)
75 return searchWordListCache(word,limit);
76 return searchWordListFile(word, limit);
79 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
81 QSet<Translation*> translations;
82 QString cacheFilePath = _settings->value("cache_path");
83 db.setDatabaseName(cacheFilePath);
85 qDebug() << "Database error" << db.lastError().text() << endl;
86 return searchWordListFile(word, limit);
90 word = word.toLower();
91 word = word.replace("*", "%");
92 word = word.replace("?", "_");
93 word = removeAccents(word);
98 cur.prepare("select word from dict where word like ? limit ?");
100 cur.prepare("select word from dict where word like ?");
101 cur.addBindValue(word);
103 cur.addBindValue(limit);
108 foreach(tran,translations) {
109 if(tran->key().toLower()==cur.value(0).toString().toLower())
112 if(ok) /*add key word to list*/
113 translations.insert(new TranslationXdxf(
114 cur.value(0).toString().toLower(),
118 return translations.toList();
121 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
122 QSet<Translation*> translations;
123 QFile dictionaryFile(path);
125 word = word.toLower();
126 word = removeAccents(word);
129 QRegExp regWord(word);
130 regWord.setCaseSensitivity(Qt::CaseInsensitive);
131 regWord.setPatternSyntax(QRegExp::Wildcard);
132 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
133 qDebug()<<"Error: could not open file";
134 return translations.toList();
137 QXmlStreamReader reader(&dictionaryFile);
138 /*search words list*/
141 while(!reader.atEnd() && !stopped){
142 reader.readNextStartElement();
143 if(reader.name()=="ar") {
144 while(reader.name()!="k" && !reader.atEnd())
145 reader.readNextStartElement();
147 a = reader.readElementText();
148 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
151 foreach(tran,translations) {
152 if(tran->key().toLower()==a.toLower())
153 ok=false; /*if key word is in the dictionary more that one */
155 if(ok) /*add key word to list*/
156 translations<<(new TranslationXdxf(a.toLower(),
159 if(i>=limit && limit!=0)
163 this->thread()->yieldCurrentThread();
166 dictionaryFile.close();
167 return translations.toList();
170 QString XdxfPlugin::search(QString key) {
171 // if(_settings->value("cached") == "true")
173 return searchCache(key);
174 return searchFile(key);
177 QString XdxfPlugin::searchCache(QString key) {
179 QString cacheFilePath = _settings->value("cache_path");
180 db.setDatabaseName(cacheFilePath);
184 qDebug() << "Database error" << db.lastError().text() << endl;
185 return searchFile(key);
189 // cur.prepare("select translation from dict where word like ? limit 1");
190 cur.prepare("select translation from dict where word like ?");
191 cur.addBindValue(key);
195 // result = cur.value(0).toString();
196 result += cur.value(0).toString();
202 QString XdxfPlugin::searchFile(QString key) {
204 QFile dictionaryFile(path);
205 QString resultString("");
206 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
207 qDebug()<<"Error: could not open file";
210 QXmlStreamReader reader(&dictionaryFile);
218 while (!reader.atEnd()&& !stopped) {
220 if(reader.tokenType() == QXmlStreamReader::StartElement) {
221 if(reader.name()=="k") {
222 a = reader.readElementText();
223 if(a.toLower()==key.toLower())
229 while(reader.name()!="ar" && !reader.atEnd()) {
230 if(reader.name()!="" && reader.name()!="k") {
231 if(reader.tokenType()==QXmlStreamReader::EndElement)
233 if(reader.tokenType()==QXmlStreamReader::StartElement)
235 temp+=reader.name().toString();
236 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
237 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
240 temp+= reader.text().toString().replace("<","<").replace(">",">");
243 if(temp.at(0)==QChar('\n'))
245 resultString+=tr("<t>") + temp + tr("</t>"); //.replace("\n","")
248 this->thread()->yieldCurrentThread();
251 dictionaryFile.close();
256 void XdxfPlugin::stop() {
260 DictDialog* XdxfPlugin::dictDialog() {
264 void XdxfPlugin::setPath(QString path){
266 _settings->setValue("path",path);
267 //getDictionaryInfo();
270 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
271 XdxfPlugin *plugin = new XdxfPlugin();
273 plugin->setPath(settings->value("path"));
275 QStringList list = settings->keys();
276 foreach(QString key, list)
277 plugin->settings()->setValue(key, settings->value(key));
280 plugin->db_name = plugin->_settings->value("type")
281 + plugin->_settings->value("path");
282 // if(!plugin->db.connectionName().isEmpty() || settings->value("generateCache")=="true")
283 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
285 if(settings->value("cached").isEmpty() &&
286 settings->value("generateCache") == "true") {
287 plugin->makeCache("");
292 plugin->getDictionaryInfo();
296 bool XdxfPlugin::isAvailable() const {
300 void XdxfPlugin::setHash(uint _hash) {
304 uint XdxfPlugin::hash() const {
308 Settings* XdxfPlugin::settings() {
312 bool XdxfPlugin::isCached() {
313 if(_settings->value("cached") == "true")
318 void XdxfPlugin::setSettings(Settings *settings) {
320 QString oldPath = _settings->value("path");
321 if(oldPath != settings->value("path")) {
322 setPath(settings->value("path"));
325 if((_settings->value("cached") == "false" ||
326 _settings->value("cached").isEmpty()) &&
327 settings->value("generateCache") == "true") {
331 _settings->setValue("cached", "false");
335 emit settingsChanged();
338 void XdxfPlugin::getDictionaryInfo() {
339 QFile dictionaryFile(path);
340 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
341 qDebug()<<"Error: could not open file";
345 QXmlStreamReader reader(&dictionaryFile);
346 reader.readNextStartElement();
347 if(reader.name()=="xdxf") {
348 if(reader.attributes().hasAttribute("lang_from"))
349 _langFrom = reader.attributes().value("lang_from").toString();
350 if(reader.attributes().hasAttribute("lang_to"))
351 _langTo = reader.attributes().value("lang_to").toString();
353 reader.readNextStartElement();
354 if(reader.name()=="full_name")
355 _name=reader.readElementText();
356 reader.readNextStartElement();
357 if(reader.name()=="description")
358 _infoNote=reader.readElementText();
360 QString format = "png";
361 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
362 // qDebug()<<initialPath;
363 // qDebug()<<QPixmap(":/icons/xdxf.png").save(initialPath,format.toAscii());
365 _infoNote="<info path=\""+initialPath+"\">"+"\n" + _name + "(" + _type + ")" + "</info>";
369 dictionaryFile.close();
372 QString XdxfPlugin::removeAccents(QString string) {
373 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
374 QString normalized = string.normalized(QString::NormalizationForm_D);
375 normalized = normalized;
376 for(int i=0; i<normalized.size(); i++) {
377 if( !normalized[i].isLetterOrNumber() &&
378 !normalized[i].isSpace() &&
379 !normalized[i].isDigit() &&
380 normalized[i] != '*' &&
381 normalized[i] != '%' &&
382 normalized[i] != '_' &&
383 normalized[i] != '?' ) {
384 normalized.remove(i,1);
390 QIcon* XdxfPlugin::icon() {
394 int XdxfPlugin::countWords() {
398 QFile dictionaryFile(path);
399 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
400 qDebug()<<"Error: could not open file";
404 dictionaryFile.seek(0);
409 while(!dictionaryFile.atEnd()) {
410 line = dictionaryFile.readLine();
411 if(line.contains("<k>")) {
415 _wordsCount = wordsCount;
416 dictionaryFile.close();
420 bool XdxfPlugin::makeCache(QString dir) {
421 cachingDialog->setVisible(true);
422 QCoreApplication::processEvents();
424 QFileInfo dictFileN(_settings->value("path"));
426 cachePathN = QDir::homePath() + "/.mdictionary/"
427 + dictFileN.completeBaseName() + ".cache";
429 QFile dictionaryFile(dictFileN.filePath());
432 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
436 QXmlStreamReader reader(&dictionaryFile);
438 db.setDatabaseName(cachePathN);
440 qDebug() << "Database error" << db.lastError().text() << endl;
443 QCoreApplication::processEvents();
445 cur.exec("PRAGMA synchronous = 0");
446 cur.exec("drop table dict");
447 QCoreApplication::processEvents();
448 cur.exec("create table dict(word text ,translation text)");
462 while (!reader.atEnd() && !stopped) {
464 QCoreApplication::processEvents();
467 if(reader.tokenType() == QXmlStreamReader::StartElement) {
468 if(reader.name()=="k"){
469 a = reader.readElementText();
475 while(reader.name()!="ar" && !reader.atEnd()) {
476 if(reader.name()!="" && reader.name()!="k") {
477 if(reader.tokenType()==QXmlStreamReader::EndElement)
479 if(reader.tokenType()==QXmlStreamReader::StartElement)
481 temp+=reader.name().toString();
482 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
483 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
486 temp+= reader.text().toString().replace("<","<").replace(">",">");;
489 if(temp.at(0)==QChar('\n'))
491 temp=tr("<t>") + temp+ tr("</t>"); //.replace("\n","")
493 cur.prepare("insert into dict values(?,?)");
495 cur.addBindValue(temp);
498 int prog = counter*100/_wordsCount;
499 if(prog % 5 == 0 && lastProg != prog) {
500 Q_EMIT updateCachingProgress(prog,
508 cur.exec("select count(*) from dict");
511 cachingDialog->setVisible(false);
513 if(!cur.next() || countWords() != cur.value(0).toInt())
518 _settings->setValue("cache_path", cachePathN);
519 _settings->setValue("cached", "true");
525 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)