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 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
29 _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
30 _type(tr("xdxf")), _infoNote(tr("")) {
32 _settings = new Settings();
33 _dictDialog = new XdxfDictDialog(this, this);
34 cachingDialog = new XdxfCachingDialog(this);
36 connect(cachingDialog, SIGNAL(cancelCaching()),
39 _settings->setValue("type","xdxf");
43 _icon = QIcon(":/icons/xdxf.png");
46 XdxfPlugin::~XdxfPlugin()
48 // QString connection(db.connectionName());
50 // QSqlDatabase::removeDatabase(connection);
55 QString XdxfPlugin::langFrom() const {
59 QString XdxfPlugin::langTo() const {
63 QString XdxfPlugin::name() const {
67 QString XdxfPlugin::type() const {
68 // return _settings->value("type");
72 QString XdxfPlugin::infoNote() const {
76 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
77 //if(_settings->value("cached") == "true")
78 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
79 && word.indexOf("%")==-1)
82 return searchWordListCache(word,limit);
83 return searchWordListFile(word, limit);
86 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
88 QSet<Translation*> translations;
89 QString cacheFilePath = _settings->value("cache_path");
90 db.setDatabaseName(cacheFilePath);
92 qDebug() << "Database error" << db.lastError().text() << endl;
93 return searchWordListFile(word, limit);
97 word = word.toLower();
98 word = word.replace("*", "%");
99 word = word.replace("?", "_");
100 word = removeAccents(word);
105 cur.prepare("select word from dict where word like ? limit ?");
107 cur.prepare("select word from dict where word like ?");
108 cur.addBindValue(word);
110 cur.addBindValue(limit);
115 foreach(tran,translations) {
116 if(tran->key().toLower()==cur.value(0).toString().toLower())
119 if(ok) /*add key word to list*/
120 translations.insert(new TranslationXdxf(
121 cur.value(0).toString().toLower(),
125 return translations.toList();
128 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
129 QSet<Translation*> translations;
130 QFile dictionaryFile(path);
132 word = word.toLower();
133 word = removeAccents(word);
136 QRegExp regWord(word);
137 regWord.setCaseSensitivity(Qt::CaseInsensitive);
138 regWord.setPatternSyntax(QRegExp::Wildcard);
139 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
140 qDebug()<<"Error: could not open file";
141 return translations.toList();
144 QXmlStreamReader reader(&dictionaryFile);
145 /*search words list*/
148 while(!reader.atEnd() && !stopped){
149 reader.readNextStartElement();
150 if(reader.name()=="ar") {
151 while(reader.name()!="k" && !reader.atEnd())
152 reader.readNextStartElement();
154 a = reader.readElementText();
155 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
158 foreach(tran,translations) {
159 if(tran->key().toLower()==a.toLower())
160 ok=false; /*if key word is in the dictionary more that one */
162 if(ok) /*add key word to list*/
163 translations<<(new TranslationXdxf(a.toLower(),
166 if(i>=limit && limit!=0)
170 this->thread()->yieldCurrentThread();
173 dictionaryFile.close();
174 return translations.toList();
177 QString XdxfPlugin::search(QString key) {
178 // if(_settings->value("cached") == "true")
180 return searchCache(key);
181 return searchFile(key);
184 QString XdxfPlugin::searchCache(QString key) {
186 QString cacheFilePath = _settings->value("cache_path");
187 db.setDatabaseName(cacheFilePath);
191 qDebug() << "Database error" << db.lastError().text() << endl;
192 return searchFile(key);
196 cur.prepare("select translation from dict where word like ?");
197 cur.addBindValue(key);
200 result += cur.value(0).toString();
208 QString XdxfPlugin::searchFile(QString key) {
210 QFile dictionaryFile(path);
211 QString resultString("");
212 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
213 qDebug()<<"Error: could not open file when search";
216 QXmlStreamReader reader(&dictionaryFile);
221 while (!reader.atEnd()&& !stopped) {
223 if(reader.tokenType() == QXmlStreamReader::StartElement) {
224 if(reader.name()=="k") {
225 a = reader.readElementText();
226 if(a.toLower()==key.toLower())
232 while(reader.name()!="ar" && !reader.atEnd()) {
233 if(reader.name()!="" && reader.name()!="k") {
234 if(reader.tokenType()==QXmlStreamReader::EndElement)
236 if(reader.tokenType()==QXmlStreamReader::StartElement)
238 temp+=reader.name().toString();
239 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
240 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
243 temp+= reader.text().toString().replace("<","<").replace(">",">");
246 if(temp.at(0)==QChar('\n'))
248 resultString+=tr("<key>") + a +tr("</key>");
249 resultString+=tr("<t>") + temp + tr("</t>");
252 this->thread()->yieldCurrentThread();
255 dictionaryFile.close();
260 void XdxfPlugin::stop() {
264 DictDialog* XdxfPlugin::dictDialog() {
268 void XdxfPlugin::setPath(QString path){
270 _settings->setValue("path",path);
271 //getDictionaryInfo();
274 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
275 XdxfPlugin *plugin = new XdxfPlugin();
278 plugin->setPath(settings->value("path"));
279 QStringList list = settings->keys();
280 foreach(QString key, list)
281 plugin->settings()->setValue(key, settings->value(key));
284 plugin->db_name = plugin->_settings->value("type")
285 + plugin->_settings->value("path");
286 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
288 if(settings->value("cached").isEmpty() &&
289 settings->value("generateCache") == "true") {
290 plugin->makeCache("");
294 plugin->getDictionaryInfo();
298 bool XdxfPlugin::isAvailable() const {
302 void XdxfPlugin::setHash(uint _hash) {
306 uint XdxfPlugin::hash() const {
310 Settings* XdxfPlugin::settings() {
314 bool XdxfPlugin::isCached() {
315 if(_settings->value("cached") == "true")
320 void XdxfPlugin::setSettings(Settings *settings) {
322 QString oldPath = _settings->value("path");
323 if(oldPath != settings->value("path")) {
324 setPath(settings->value("path"));
327 if((_settings->value("cached") == "false" ||
328 _settings->value("cached").isEmpty()) &&
329 settings->value("generateCache") == "true") {
333 _settings->setValue("cached", "false");
337 emit settingsChanged();
340 void XdxfPlugin::getDictionaryInfo() {
341 QFile dictionaryFile(path);
342 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
343 qDebug()<<"Error: could not open file";
347 QXmlStreamReader reader(&dictionaryFile);
348 reader.readNextStartElement();
349 if(reader.name()=="xdxf") {
350 if(reader.attributes().hasAttribute("lang_from"))
351 _langFrom = reader.attributes().value("lang_from").toString();
352 if(reader.attributes().hasAttribute("lang_to"))
353 _langTo = reader.attributes().value("lang_to").toString();
355 reader.readNextStartElement();
356 if(reader.name()=="full_name")
357 _name=reader.readElementText();
358 reader.readNextStartElement();
359 if(reader.name()=="description")
360 _infoNote=reader.readElementText();
362 QString format = "png";
363 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
364 // qDebug()<<initialPath;
365 // QPixmap test(":/icons/xdxf.png");
366 // qDebug()<<QPixmap(test).save(initialPath,format.toAscii());
367 // qDebug()<<QPixmap("/home/jakub/star.jpg").save(initialPath,format.toAscii());
369 _infoNote="path=\""+initialPath+"\">"+"\n" + _name + " [" + _langFrom + "-" + _langTo + "] "+ "(" + _type + ")";
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");
528 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)