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 if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
92 word = word.toLower();
93 word = word.replace("*", "%");
94 word = word.replace("?", "_");
95 word = removeAccents(word);
99 cur.prepare("select word from dict where word like ? limit ?");
100 cur.addBindValue(word);
101 cur.addBindValue(limit);
104 translations.insert(new TranslationXdxf(
105 cur.value(0).toString().toLower(),
107 return translations.toList();
112 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
113 QSet<Translation*> translations;
114 QFile dictionaryFile(path);
116 word = word.toLower();
117 word = removeAccents(word);
120 QRegExp regWord(word);
121 regWord.setCaseSensitivity(Qt::CaseInsensitive);
122 regWord.setPatternSyntax(QRegExp::Wildcard);
123 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
124 qDebug()<<"Error: could not open file";
125 return translations.toList();
128 QXmlStreamReader dictionaryReader(&dictionaryFile);
129 /*search words list*/
132 while(!dictionaryReader.atEnd() && !stopped){
133 dictionaryReader.readNextStartElement();
134 if(dictionaryReader.name()=="ar"){
135 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
136 dictionaryReader.readNextStartElement();
137 if(!dictionaryReader.atEnd())
138 a = dictionaryReader.readElementText();
139 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
142 foreach(tran,translations) {
144 ok=false; /*if key word is in the dictionary more that one */
146 if(ok) /*add key word to list*/
147 translations<<(new TranslationXdxf(a.toLower(),
150 if(i>=limit && limit!=0)
154 this->thread()->yieldCurrentThread();
157 dictionaryFile.close();
158 return translations.toList();
161 QString XdxfPlugin::search(QString key) {
162 // if(_settings->value("cached") == "true")
164 return searchCache(key);
165 return searchFile(key);
170 QString XdxfPlugin::searchCache(QString key) {
172 QString cacheFilePath = _settings->value("cache_path");
173 db.setDatabaseName(cacheFilePath);
177 qDebug() << "Database error" << db.lastError().text() << endl;
178 return searchFile(key);
182 cur.prepare("select translation from dict where word like ? limit 1");
183 cur.addBindValue(key);
186 result = cur.value(0).toString();
194 QString XdxfPlugin::searchFile(QString key) {
196 QFile dictionaryFile(path);
197 QString resultString("");
198 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
199 qDebug()<<"Error: could not open file";
202 QXmlStreamReader dictionaryReader(&dictionaryFile);
209 while (!dictionaryReader.atEnd()&& !stopped) {
210 dictionaryReader.readNext();
211 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
212 if(dictionaryReader.name()=="k") {
213 a = dictionaryReader.readElementText();
218 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
220 /* QString temp(dictionaryReader.text().toString());
222 temp=dictionaryReader.readElementText();
223 temp.replace("\n","");
226 while(dictionaryReader.name()!="ar"&&
227 !dictionaryReader.atEnd()){
228 dictionaryReader.readNext();
229 if(dictionaryReader.name()!="" &&
230 dictionaryReader.name()!="ar") {
231 if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
233 if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
235 qDebug()<<dictionaryReader.tokenType();
236 temp=temp+dictionaryReader.name().toString() + tr(">");
239 temp+=dictionaryReader.text().toString();
242 resultString+=temp.replace("\n","")+"\n";
246 while(dictionaryReader.name()!="ar" && !dictionaryReader.atEnd()) {
247 if(dictionaryReader.name()!="") {
248 if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
250 if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
252 temp=temp+dictionaryReader.name().toString() + tr(">");
254 temp+= dictionaryReader.text().toString();
255 dictionaryReader.readNext();
257 resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
261 this->thread()->yieldCurrentThread();
264 dictionaryFile.close();
268 void XdxfPlugin::stop() {
272 DictDialog* XdxfPlugin::dictDialog() {
276 void XdxfPlugin::setPath(QString path){
278 _settings->setValue("path",path);
279 //getDictionaryInfo();
283 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
284 XdxfPlugin *plugin = new XdxfPlugin();
286 plugin->setPath(settings->value("path"));
288 QStringList list = settings->keys();
289 foreach(QString key, list)
290 plugin->settings()->setValue(key, settings->value(key));
293 plugin->db_name = plugin->_settings->value("type")
294 + plugin->_settings->value("path");
295 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
297 if(settings->value("cached").isEmpty() &&
298 settings->value("generateCache") == "true") {
299 plugin->makeCache("");
303 plugin->getDictionaryInfo();
307 bool XdxfPlugin::isAvailable() const {
311 void XdxfPlugin::setHash(uint _hash) {
315 uint XdxfPlugin::hash() const {
319 Settings* XdxfPlugin::settings() {
323 bool XdxfPlugin::isCached() {
324 if(_settings->value("cached") == "true")
329 void XdxfPlugin::setSettings(Settings *settings) {
331 QString oldPath = _settings->value("path");
332 if(oldPath != settings->value("path")) {
333 setPath(settings->value("path"));
336 if((_settings->value("cached") == "false" ||
337 _settings->value("cached").isEmpty()) &&
338 settings->value("generateCache") == "true") {
342 _settings->setValue("cached", "false");
345 emit settingsChanged();
349 void XdxfPlugin::getDictionaryInfo() {
350 QFile dictionaryFile(path);
351 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
352 qDebug()<<"Error: could not open file";
356 QXmlStreamReader dictionaryReader(&dictionaryFile);
357 dictionaryReader.readNextStartElement();
358 if(dictionaryReader.name()=="xdxf") {
359 if(dictionaryReader.attributes().hasAttribute("lang_from"))
360 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
361 if(dictionaryReader.attributes().hasAttribute("lang_to"))
362 _langTo = dictionaryReader.attributes().value("lang_to").toString();
364 dictionaryReader.readNextStartElement();
365 if(dictionaryReader.name()=="full_name")
366 _name=dictionaryReader.readElementText();
367 dictionaryReader.readNextStartElement();
368 if(dictionaryReader.name()=="description")
369 _infoNote=dictionaryReader.readElementText();
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();
424 bool XdxfPlugin::makeCache(QString dir) {
425 cachingDialog->setVisible(true);
426 QCoreApplication::processEvents();
428 QFileInfo dictFileN(_settings->value("path"));
430 cachePathN = QDir::homePath() + "/.mdictionary/"
431 + dictFileN.completeBaseName() + ".cache";
433 QFile dictionaryFile(dictFileN.filePath());
436 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
440 QXmlStreamReader reader(&dictionaryFile);
442 db.setDatabaseName(cachePathN);
444 qDebug() << "Database error" << db.lastError().text() << endl;
447 QCoreApplication::processEvents();
449 cur.exec("PRAGMA synchronous = 0");
450 cur.exec("drop table dict");
451 QCoreApplication::processEvents();
452 cur.exec("create table dict(word text ,translation text)");
466 while (!reader.atEnd() && !stopped) {
468 QCoreApplication::processEvents();
471 if(reader.tokenType() == QXmlStreamReader::StartElement) {
472 if(reader.name()=="k"){
473 a = reader.readElementText();
477 else if(reader.tokenType() == QXmlStreamReader::Characters) {
480 while(reader.name()!="ar" && !reader.atEnd()) {
481 if(reader.name()!="") {
482 if(reader.tokenType()==QXmlStreamReader::EndElement)
484 if(reader.tokenType()==QXmlStreamReader::StartElement)
486 temp=temp+reader.name().toString() + tr(">");
488 temp+= reader.text().toString();
491 temp=tr("<t>") + temp.replace("\n","") + tr("</t>");
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,
510 cur.exec("select count(*) from dict");
513 cachingDialog->setVisible(false);
515 if(!cur.next() || countWords() != cur.value(0).toInt())
517 _settings->setValue("cache_path", cachePathN);
518 _settings->setValue("cached", "true");
524 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)