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);
217 while (!reader.atEnd()&& !stopped) {
219 if(reader.tokenType() == QXmlStreamReader::StartElement) {
220 if(reader.name()=="k") {
221 a = reader.readElementText();
222 if(a.toLower()==key.toLower())
228 while(reader.name()!="ar" && !reader.atEnd()) {
229 if(reader.name()!="" && reader.name()!="k") {
230 if(reader.tokenType()==QXmlStreamReader::EndElement)
232 if(reader.tokenType()==QXmlStreamReader::StartElement)
234 temp+=reader.name().toString();
235 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
236 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
239 temp+= reader.text().toString();
242 if(temp.at(0)==QChar('\n'))
244 resultString+=tr("<t>") + temp + tr("</t>"); //.replace("\n","")
247 this->thread()->yieldCurrentThread();
250 dictionaryFile.close();
255 void XdxfPlugin::stop() {
259 DictDialog* XdxfPlugin::dictDialog() {
263 void XdxfPlugin::setPath(QString path){
265 _settings->setValue("path",path);
266 //getDictionaryInfo();
269 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
270 XdxfPlugin *plugin = new XdxfPlugin();
272 plugin->setPath(settings->value("path"));
274 QStringList list = settings->keys();
275 foreach(QString key, list)
276 plugin->settings()->setValue(key, settings->value(key));
279 plugin->db_name = plugin->_settings->value("type")
280 + plugin->_settings->value("path");
281 // if(!plugin->db.connectionName().isEmpty() || settings->value("generateCache")=="true")
282 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
284 if(settings->value("cached").isEmpty() &&
285 settings->value("generateCache") == "true") {
286 plugin->makeCache("");
290 plugin->getDictionaryInfo();
294 bool XdxfPlugin::isAvailable() const {
298 void XdxfPlugin::setHash(uint _hash) {
302 uint XdxfPlugin::hash() const {
306 Settings* XdxfPlugin::settings() {
310 bool XdxfPlugin::isCached() {
311 if(_settings->value("cached") == "true")
316 void XdxfPlugin::setSettings(Settings *settings) {
318 QString oldPath = _settings->value("path");
319 if(oldPath != settings->value("path")) {
320 setPath(settings->value("path"));
323 if((_settings->value("cached") == "false" ||
324 _settings->value("cached").isEmpty()) &&
325 settings->value("generateCache") == "true") {
329 _settings->setValue("cached", "false");
332 emit settingsChanged();
335 void XdxfPlugin::getDictionaryInfo() {
336 QFile dictionaryFile(path);
337 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
338 qDebug()<<"Error: could not open file";
342 QXmlStreamReader reader(&dictionaryFile);
343 reader.readNextStartElement();
344 if(reader.name()=="xdxf") {
345 if(reader.attributes().hasAttribute("lang_from"))
346 _langFrom = reader.attributes().value("lang_from").toString();
347 if(reader.attributes().hasAttribute("lang_to"))
348 _langTo = reader.attributes().value("lang_to").toString();
350 reader.readNextStartElement();
351 if(reader.name()=="full_name")
352 _name=reader.readElementText();
353 reader.readNextStartElement();
354 if(reader.name()=="description")
355 _infoNote=reader.readElementText();
357 QString format = "png";
358 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
359 // qDebug()<<initialPath;
360 // qDebug()<<QPixmap(":/icons/xdxf.png").save(initialPath,format.toAscii());
362 _infoNote="<info path=\""+initialPath+"\">"+"\n" + _name + "(" + _type + ")" + "</info>";
366 dictionaryFile.close();
369 QString XdxfPlugin::removeAccents(QString string) {
370 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
371 QString normalized = string.normalized(QString::NormalizationForm_D);
372 normalized = normalized;
373 for(int i=0; i<normalized.size(); i++) {
374 if( !normalized[i].isLetterOrNumber() &&
375 !normalized[i].isSpace() &&
376 !normalized[i].isDigit() &&
377 normalized[i] != '*' &&
378 normalized[i] != '%' &&
379 normalized[i] != '_' &&
380 normalized[i] != '?' ) {
381 normalized.remove(i,1);
387 QIcon* XdxfPlugin::icon() {
391 int XdxfPlugin::countWords() {
395 QFile dictionaryFile(path);
396 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
397 qDebug()<<"Error: could not open file";
401 dictionaryFile.seek(0);
406 while(!dictionaryFile.atEnd()) {
407 line = dictionaryFile.readLine();
408 if(line.contains("<k>")) {
412 _wordsCount = wordsCount;
413 dictionaryFile.close();
417 bool XdxfPlugin::makeCache(QString dir) {
418 cachingDialog->setVisible(true);
419 QCoreApplication::processEvents();
421 QFileInfo dictFileN(_settings->value("path"));
423 cachePathN = QDir::homePath() + "/.mdictionary/"
424 + dictFileN.completeBaseName() + ".cache";
426 QFile dictionaryFile(dictFileN.filePath());
429 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
433 QXmlStreamReader reader(&dictionaryFile);
435 db.setDatabaseName(cachePathN);
437 qDebug() << "Database error" << db.lastError().text() << endl;
440 QCoreApplication::processEvents();
442 cur.exec("PRAGMA synchronous = 0");
443 cur.exec("drop table dict");
444 QCoreApplication::processEvents();
445 cur.exec("create table dict(word text ,translation text)");
459 while (!reader.atEnd() && !stopped) {
461 QCoreApplication::processEvents();
464 if(reader.tokenType() == QXmlStreamReader::StartElement) {
465 if(reader.name()=="k"){
466 a = reader.readElementText();
472 while(reader.name()!="ar" && !reader.atEnd()) {
473 if(reader.name()!="" && reader.name()!="k") {
474 if(reader.tokenType()==QXmlStreamReader::EndElement)
476 if(reader.tokenType()==QXmlStreamReader::StartElement)
478 temp+=reader.name().toString();
479 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
480 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
483 temp+= reader.text().toString();
486 if(temp.at(0)==QChar('\n'))
488 temp=tr("<t>") + temp+ tr("</t>"); //.replace("\n","")
490 cur.prepare("insert into dict values(?,?)");
492 cur.addBindValue(temp);
495 int prog = counter*100/_wordsCount;
496 if(prog % 5 == 0 && lastProg != prog) {
497 Q_EMIT updateCachingProgress(prog,
505 cur.exec("select count(*) from dict");
508 cachingDialog->setVisible(false);
510 if(!cur.next() || countWords() != cur.value(0).toInt())
515 _settings->setValue("cache_path", cachePathN);
516 _settings->setValue("cached", "true");
522 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)