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 return searchWordListCache(word,limit);
72 return searchWordListFile(word, limit);
75 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
77 QSet<Translation*> translations;
78 QString cacheFilePath = _settings->value("cache_path");
79 db.setDatabaseName(cacheFilePath);
81 qDebug() << "Database error" << db.lastError().text() << endl;
82 return searchWordListFile(word, limit);
86 if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
88 word = word.replace("*", "%");
89 word = word.replace("?", "_");
90 word = removeAccents(word);
94 cur.prepare("select word from dict where word like ? limit ?");
95 cur.addBindValue(word);
96 cur.addBindValue(limit);
99 translations.insert(new TranslationXdxf(cur.value(0).toString(),
101 return translations.toList();
106 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
107 QSet<Translation*> translations;
108 QFile dictionaryFile(path);
110 word = removeAccents(word);
113 if(word.indexOf("*")==-1)
115 QRegExp regWord(word);
116 regWord.setCaseSensitivity(Qt::CaseInsensitive);
117 regWord.setPatternSyntax(QRegExp::Wildcard);
118 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
119 qDebug()<<"Error: could not open file";
120 return translations.toList();
123 QXmlStreamReader dictionaryReader(&dictionaryFile);
124 /*search words list*/
127 while(!dictionaryReader.atEnd() && !stopped){
128 dictionaryReader.readNextStartElement();
129 if(dictionaryReader.name()=="ar"){
130 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
131 dictionaryReader.readNextStartElement();
132 if(!dictionaryReader.atEnd())
133 a = dictionaryReader.readElementText();
134 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
137 foreach(tran,translations)
140 ok=false; /*if key word is in the dictionary more that one */
142 if(ok) /*add key word to list*/
143 translations<<(new TranslationXdxf(a,_infoNote,this));
145 if(i>=limit && limit!=0)
149 this->thread()->yieldCurrentThread();
152 dictionaryFile.close();
153 return translations.toList();
156 QString XdxfPlugin::search(QString key) {
157 if(_settings->value("cached") == "true")
158 return searchCache(key);
159 return searchFile(key);
164 QString XdxfPlugin::searchCache(QString key) {
166 QString cacheFilePath = _settings->value("cache_path");
167 db.setDatabaseName(cacheFilePath);
170 qDebug() << "Database error" << db.lastError().text() << endl;
171 return searchFile(key);
175 cur.prepare("select translation from dict where word like ? limit 1");
176 cur.addBindValue(key);
179 result = cur.value(0).toString();
187 QString XdxfPlugin::searchFile(QString key) {
188 QFile dictionaryFile(path);
189 QString resultString("");
190 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
191 qDebug()<<"Error: could not open file";
194 QXmlStreamReader dictionaryReader(&dictionaryFile);
201 while (!dictionaryReader.atEnd()&& !stopped) {
202 dictionaryReader.readNext();
203 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
204 if(dictionaryReader.name()=="k") {
205 a = dictionaryReader.readElementText();
210 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
212 QString temp(dictionaryReader.text().toString());
213 temp.replace("\n","");
215 while(dictionaryReader.name()!="ar"&&
216 !dictionaryReader.atEnd()){
217 dictionaryReader.readNext();
218 temp+=dictionaryReader.text().toString();
221 resultString+=temp.replace("\n","")+"\n";
225 this->thread()->yieldCurrentThread();
228 dictionaryFile.close();
232 void XdxfPlugin::stop() {
236 DictDialog* XdxfPlugin::dictDialog() {
240 void XdxfPlugin::setPath(QString path){
242 _settings->setValue("path",path);
243 //getDictionaryInfo();
247 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
248 XdxfPlugin *plugin = new XdxfPlugin();
250 plugin->setPath(settings->value("path"));
252 QStringList list = settings->keys();
253 foreach(QString key, list)
254 plugin->settings()->setValue(key, settings->value(key));
257 plugin->db_name = plugin->_settings->value("type")
258 + plugin->_settings->value("path");
259 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
261 if(settings->value("cached").isEmpty() &&
262 settings->value("generateCache") == "true") {
263 plugin->makeCache("");
267 plugin->getDictionaryInfo();
271 bool XdxfPlugin::isAvailable() const {
275 void XdxfPlugin::setHash(uint _hash)
280 uint XdxfPlugin::hash() const
285 Settings* XdxfPlugin::settings() {
289 bool XdxfPlugin::isCached()
294 void XdxfPlugin::setSettings(Settings *settings) {
296 QString oldPath = _settings->value("path");
297 if(oldPath != settings->value("path")) {
298 setPath(settings->value("path"));
301 if((_settings->value("cached") == "false" ||
302 _settings->value("cached").isEmpty()) &&
303 settings->value("generateCache") == "true") {
307 _settings->setValue("cached", "false");
310 emit settingsChanged();
314 void XdxfPlugin::getDictionaryInfo() {
315 QFile dictionaryFile(path);
316 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
317 qDebug()<<"Error: could not open file";
321 QXmlStreamReader dictionaryReader(&dictionaryFile);
322 dictionaryReader.readNextStartElement();
323 if(dictionaryReader.name()=="xdxf") {
324 if(dictionaryReader.attributes().hasAttribute("lang_from"))
325 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
326 if(dictionaryReader.attributes().hasAttribute("lang_to"))
327 _langTo = dictionaryReader.attributes().value("lang_to").toString();
329 dictionaryReader.readNextStartElement();
330 if(dictionaryReader.name()=="full_name")
331 _name=dictionaryReader.readElementText();
332 dictionaryReader.readNextStartElement();
333 if(dictionaryReader.name()=="description")
334 _infoNote=dictionaryReader.readElementText();
336 dictionaryFile.close();
339 QString XdxfPlugin::removeAccents(QString string) {
341 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
342 QString normalized = string.normalized(QString::NormalizationForm_D);
343 normalized = normalized;
344 for(int i=0; i<normalized.size(); i++) {
345 if( !normalized[i].isLetterOrNumber() &&
346 !normalized[i].isSpace() &&
347 !normalized[i].isDigit() &&
348 normalized[i] != '*' &&
349 normalized[i] != '%') {
350 normalized.remove(i,1);
356 QIcon* XdxfPlugin::icon() {
360 int XdxfPlugin::countWords() {
364 QFile dictionaryFile(path);
365 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
366 qDebug()<<"Error: could not open file";
370 dictionaryFile.seek(0);
375 while(!dictionaryFile.atEnd()) {
376 line = dictionaryFile.readLine();
377 if(line.contains("<k>")) {
381 _wordsCount = wordsCount;
382 dictionaryFile.close();
388 bool XdxfPlugin::makeCache(QString dir) {
389 cachingDialog->setVisible(true);
390 QCoreApplication::processEvents();
392 QFileInfo dictFileN(_settings->value("path"));
394 cachePathN = QDir::homePath() + "/.mdictionary/"
395 + dictFileN.completeBaseName() + ".cache";
397 QFile dictionaryFile(dictFileN.filePath());
400 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
404 QXmlStreamReader reader(&dictionaryFile);
407 db.setDatabaseName(cachePathN);
409 qDebug() << "Database error" << endl;
412 QCoreApplication::processEvents();
414 cur.exec("PRAGMA synchronous = 0");
415 cur.exec("drop table dict");
416 QCoreApplication::processEvents();
417 cur.exec("create table dict(word text ,translation text)");
431 while (!reader.atEnd() && !stopped) {
433 QCoreApplication::processEvents();
437 if(reader.tokenType() == QXmlStreamReader::StartElement) {
438 if(reader.name()=="k"){
439 a = reader.readElementText();
443 else if(reader.tokenType() == QXmlStreamReader::Characters) {
445 QString temp(reader.text().toString());
446 temp.replace("\n","");
448 while(reader.name()!="ar"&&
451 temp+=reader.text().toString();
455 cur.prepare("insert into dict values(?,?)");
457 cur.addBindValue(temp);
460 int prog = counter*100/_wordsCount;
461 if(prog % 5 == 0 && lastProg != prog) {
462 Q_EMIT updateCachingProgress(prog,
472 cur.exec("select count(*) from dict");
475 cachingDialog->setVisible(false);
477 if(!cur.next() || countWords() != cur.value(0).toInt())
479 _settings->setValue("cache_path", cachePathN);
480 _settings->setValue("cached", "true");
486 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)