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 XdxfPlugin::~XdxfPlugin()
50 db.removeDatabase(_type+path);
53 QString XdxfPlugin::langFrom() const {
57 QString XdxfPlugin::langTo() const {
61 QString XdxfPlugin::name() const {
65 QString XdxfPlugin::type() const {
66 // return _settings->value("type");
70 QString XdxfPlugin::infoNote() const {
74 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
75 //if(_settings->value("cached") == "true")
76 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
77 && word.indexOf("%")==-1)
80 return searchWordListCache(word,limit);
81 return searchWordListFile(word, limit);
84 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
86 QSet<Translation*> translations;
87 QString cacheFilePath = _settings->value("cache_path");
88 db.setDatabaseName(cacheFilePath);
90 qDebug() << "Database error" << db.lastError().text() << endl;
91 return searchWordListFile(word, limit);
95 word = word.toLower();
96 word = word.replace("*", "%");
97 word = word.replace("?", "_");
98 word = removeAccents(word);
103 cur.prepare("select word from dict where word like ? limit ?");
105 cur.prepare("select word from dict where word like ?");
106 cur.addBindValue(word);
108 cur.addBindValue(limit);
113 foreach(tran,translations) {
114 if(tran->key().toLower()==cur.value(0).toString().toLower())
117 if(ok) /*add key word to list*/
118 translations.insert(new TranslationXdxf(
119 cur.value(0).toString().toLower(),
123 return translations.toList();
126 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
127 QSet<Translation*> translations;
128 QFile dictionaryFile(path);
130 word = word.toLower();
131 word = removeAccents(word);
134 QRegExp regWord(word);
135 regWord.setCaseSensitivity(Qt::CaseInsensitive);
136 regWord.setPatternSyntax(QRegExp::Wildcard);
137 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
138 qDebug()<<"Error: could not open file";
139 return translations.toList();
142 QXmlStreamReader reader(&dictionaryFile);
143 /*search words list*/
146 while(!reader.atEnd() && !stopped){
147 reader.readNextStartElement();
148 if(reader.name()=="ar") {
149 while(reader.name()!="k" && !reader.atEnd())
150 reader.readNextStartElement();
152 a = reader.readElementText();
153 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
156 foreach(tran,translations) {
157 if(tran->key().toLower()==a.toLower())
158 ok=false; /*if key word is in the dictionary more that one */
160 if(ok) /*add key word to list*/
161 translations<<(new TranslationXdxf(a.toLower(),
164 if(i>=limit && limit!=0)
168 this->thread()->yieldCurrentThread();
171 dictionaryFile.close();
172 return translations.toList();
175 QString XdxfPlugin::search(QString key) {
176 // if(_settings->value("cached") == "true")
178 return searchCache(key);
179 return searchFile(key);
182 QString XdxfPlugin::searchCache(QString key) {
184 QString cacheFilePath = _settings->value("cache_path");
185 db.setDatabaseName(cacheFilePath);
189 qDebug() << "Database error" << db.lastError().text() << endl;
190 return searchFile(key);
194 cur.prepare("select translation from dict where word like ?");
195 cur.addBindValue(key);
198 result += cur.value(0).toString();
204 QString XdxfPlugin::searchFile(QString key) {
206 QFile dictionaryFile(path);
207 QString resultString("");
208 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
209 qDebug()<<"Error: could not open file";
212 QXmlStreamReader reader(&dictionaryFile);
220 while (!reader.atEnd()&& !stopped) {
222 if(reader.tokenType() == QXmlStreamReader::StartElement) {
223 if(reader.name()=="k") {
224 a = reader.readElementText();
225 if(a.toLower()==key.toLower())
231 while(reader.name()!="ar" && !reader.atEnd()) {
232 if(reader.name()!="" && reader.name()!="k") {
233 if(reader.tokenType()==QXmlStreamReader::EndElement)
235 if(reader.tokenType()==QXmlStreamReader::StartElement)
237 temp+=reader.name().toString();
238 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
239 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
242 temp+= reader.text().toString().replace("<","<").replace(">",">");
245 if(temp.at(0)==QChar('\n'))
247 resultString+=tr("<key>") + a +tr("</key>");
248 resultString+=tr("<t>") + temp + tr("</t>");
251 this->thread()->yieldCurrentThread();
254 dictionaryFile.close();
259 void XdxfPlugin::stop() {
263 DictDialog* XdxfPlugin::dictDialog() {
267 void XdxfPlugin::setPath(QString path){
269 _settings->setValue("path",path);
270 //getDictionaryInfo();
273 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
274 XdxfPlugin *plugin = new XdxfPlugin();
277 plugin->setPath(settings->value("path"));
278 QStringList list = settings->keys();
279 foreach(QString key, list)
280 plugin->settings()->setValue(key, settings->value(key));
283 plugin->db_name = plugin->_settings->value("type")
284 + plugin->_settings->value("path");
285 // if(!plugin->db.connectionName().isEmpty() || settings->value("generateCache")=="true")
286 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
288 if(settings->value("cached").isEmpty() &&
289 settings->value("generateCache") == "true") {
290 plugin->makeCache("");
293 plugin->getDictionaryInfo();
297 bool XdxfPlugin::isAvailable() const {
301 void XdxfPlugin::setHash(uint _hash) {
305 uint XdxfPlugin::hash() const {
309 Settings* XdxfPlugin::settings() {
313 bool XdxfPlugin::isCached() {
314 if(_settings->value("cached") == "true")
319 void XdxfPlugin::setSettings(Settings *settings) {
321 QString oldPath = _settings->value("path");
322 if(oldPath != settings->value("path")) {
323 setPath(settings->value("path"));
326 if((_settings->value("cached") == "false" ||
327 _settings->value("cached").isEmpty()) &&
328 settings->value("generateCache") == "true") {
332 _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 // QPixmap test(":/icons/xdxf.png");
364 // qDebug()<<QPixmap(test).save(initialPath,format.toAscii());
365 // qDebug()<<QPixmap("/home/jakub/star.jpg").save(initialPath,format.toAscii());
367 _infoNote="<info path=\""+initialPath+"\">"+"\n" + _name + " [" + _langFrom + "-" + _langTo + "] "+ "(" + _type + ")" + "</info>";
371 dictionaryFile.close();
374 QString XdxfPlugin::removeAccents(QString string) {
375 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
376 QString normalized = string.normalized(QString::NormalizationForm_D);
377 normalized = normalized;
378 for(int i=0; i<normalized.size(); i++) {
379 if( !normalized[i].isLetterOrNumber() &&
380 !normalized[i].isSpace() &&
381 !normalized[i].isDigit() &&
382 normalized[i] != '*' &&
383 normalized[i] != '%' &&
384 normalized[i] != '_' &&
385 normalized[i] != '?' ) {
386 normalized.remove(i,1);
392 QIcon* XdxfPlugin::icon() {
396 int XdxfPlugin::countWords() {
400 QFile dictionaryFile(path);
401 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
402 qDebug()<<"Error: could not open file";
406 dictionaryFile.seek(0);
411 while(!dictionaryFile.atEnd()) {
412 line = dictionaryFile.readLine();
413 if(line.contains("<k>")) {
417 _wordsCount = wordsCount;
418 dictionaryFile.close();
422 bool XdxfPlugin::makeCache(QString dir) {
423 cachingDialog->setVisible(true);
424 QCoreApplication::processEvents();
426 QFileInfo dictFileN(_settings->value("path"));
428 cachePathN = QDir::homePath() + "/.mdictionary/"
429 + dictFileN.completeBaseName() + ".cache";
431 QFile dictionaryFile(dictFileN.filePath());
434 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
438 QXmlStreamReader reader(&dictionaryFile);
440 db.setDatabaseName(cachePathN);
442 qDebug() << "Database error" << db.lastError().text() << endl;
445 QCoreApplication::processEvents();
447 cur.exec("PRAGMA synchronous = 0");
448 cur.exec("drop table dict");
449 QCoreApplication::processEvents();
450 cur.exec("create table dict(word text ,translation text)");
464 while (!reader.atEnd() && !stopped) {
466 QCoreApplication::processEvents();
469 if(reader.tokenType() == QXmlStreamReader::StartElement) {
470 if(reader.name()=="k"){
471 a = reader.readElementText();
477 while(reader.name()!="ar" && !reader.atEnd()) {
478 if(reader.name()!="" && reader.name()!="k") {
479 if(reader.tokenType()==QXmlStreamReader::EndElement)
481 if(reader.tokenType()==QXmlStreamReader::StartElement)
483 temp+=reader.name().toString();
484 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
485 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
488 temp+= reader.text().toString().replace("<","<").replace(">",">");;
491 if(temp.at(0)==QChar('\n'))
493 temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
495 cur.prepare("insert into dict values(?,?)");
497 cur.addBindValue(temp);
500 int prog = counter*100/_wordsCount;
501 if(prog % 5 == 0 && lastProg != prog) {
502 Q_EMIT updateCachingProgress(prog,
510 cur.exec("select count(*) from dict");
513 cachingDialog->setVisible(false);
515 if(!cur.next() || countWords() != cur.value(0).toInt())
520 _settings->setValue("cache_path", cachePathN);
521 _settings->setValue("cached", "true");
527 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)