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.replace("*", "%");
93 word = word.replace("?", "_");
94 word = removeAccents(word);
98 cur.prepare("select word from dict where word like ? limit ?");
99 cur.addBindValue(word);
100 cur.addBindValue(limit);
103 translations.insert(new TranslationXdxf(cur.value(0).toString(),
105 return translations.toList();
110 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
111 QSet<Translation*> translations;
112 QFile dictionaryFile(path);
114 word = removeAccents(word);
117 QRegExp regWord(word);
118 regWord.setCaseSensitivity(Qt::CaseInsensitive);
119 regWord.setPatternSyntax(QRegExp::Wildcard);
120 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
121 qDebug()<<"Error: could not open file";
122 return translations.toList();
125 QXmlStreamReader dictionaryReader(&dictionaryFile);
126 /*search words list*/
129 while(!dictionaryReader.atEnd() && !stopped){
130 dictionaryReader.readNextStartElement();
131 if(dictionaryReader.name()=="ar"){
132 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
133 dictionaryReader.readNextStartElement();
134 if(!dictionaryReader.atEnd())
135 a = dictionaryReader.readElementText();
136 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
139 foreach(tran,translations)
142 ok=false; /*if key word is in the dictionary more that one */
144 if(ok) /*add key word to list*/
145 translations<<(new TranslationXdxf(a,_infoNote,this));
147 if(i>=limit && limit!=0)
151 this->thread()->yieldCurrentThread();
154 dictionaryFile.close();
155 return translations.toList();
158 QString XdxfPlugin::search(QString key) {
159 // if(_settings->value("cached") == "true")
161 return searchCache(key);
162 return searchFile(key);
167 QString XdxfPlugin::searchCache(QString key) {
169 QString cacheFilePath = _settings->value("cache_path");
170 db.setDatabaseName(cacheFilePath);
173 qDebug() << "Database error" << db.lastError().text() << endl;
174 return searchFile(key);
178 cur.prepare("select translation from dict where word like ? limit 1");
179 cur.addBindValue(key);
182 result = cur.value(0).toString();
190 QString XdxfPlugin::searchFile(QString key) {
191 QFile dictionaryFile(path);
192 QString resultString("");
193 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
194 qDebug()<<"Error: could not open file";
197 QXmlStreamReader dictionaryReader(&dictionaryFile);
204 while (!dictionaryReader.atEnd()&& !stopped) {
205 dictionaryReader.readNext();
206 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
207 if(dictionaryReader.name()=="k") {
208 a = dictionaryReader.readElementText();
213 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
215 QString temp(dictionaryReader.text().toString());
217 temp=dictionaryReader.readElementText();
218 temp.replace("\n","");
221 while(dictionaryReader.name()!="ar"&&
222 !dictionaryReader.atEnd()){
223 dictionaryReader.readNext();
224 if(dictionaryReader.name()!="" && dictionaryReader.name()!="ar") {
229 temp=temp+dictionaryReader.name().toString() + tr(">");
232 temp+=dictionaryReader.text().toString();
235 resultString+=temp.replace("\n","")+"\n";
239 this->thread()->yieldCurrentThread();
242 dictionaryFile.close();
246 void XdxfPlugin::stop() {
250 DictDialog* XdxfPlugin::dictDialog() {
254 void XdxfPlugin::setPath(QString path){
256 _settings->setValue("path",path);
257 //getDictionaryInfo();
261 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
262 XdxfPlugin *plugin = new XdxfPlugin();
264 plugin->setPath(settings->value("path"));
266 QStringList list = settings->keys();
267 foreach(QString key, list)
268 plugin->settings()->setValue(key, settings->value(key));
271 plugin->db_name = plugin->_settings->value("type")
272 + plugin->_settings->value("path");
273 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
275 if(settings->value("cached").isEmpty() &&
276 settings->value("generateCache") == "true") {
277 plugin->makeCache("");
281 plugin->getDictionaryInfo();
285 bool XdxfPlugin::isAvailable() const {
289 void XdxfPlugin::setHash(uint _hash)
294 uint XdxfPlugin::hash() const
299 Settings* XdxfPlugin::settings() {
303 bool XdxfPlugin::isCached()
305 if(_settings->value("cached") == "true")
310 void XdxfPlugin::setSettings(Settings *settings) {
312 QString oldPath = _settings->value("path");
313 if(oldPath != settings->value("path")) {
314 setPath(settings->value("path"));
317 if((_settings->value("cached") == "false" ||
318 _settings->value("cached").isEmpty()) &&
319 settings->value("generateCache") == "true") {
323 _settings->setValue("cached", "false");
326 emit settingsChanged();
330 void XdxfPlugin::getDictionaryInfo() {
331 QFile dictionaryFile(path);
332 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
333 qDebug()<<"Error: could not open file";
337 QXmlStreamReader dictionaryReader(&dictionaryFile);
338 dictionaryReader.readNextStartElement();
339 if(dictionaryReader.name()=="xdxf") {
340 if(dictionaryReader.attributes().hasAttribute("lang_from"))
341 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
342 if(dictionaryReader.attributes().hasAttribute("lang_to"))
343 _langTo = dictionaryReader.attributes().value("lang_to").toString();
345 dictionaryReader.readNextStartElement();
346 if(dictionaryReader.name()=="full_name")
347 _name=dictionaryReader.readElementText();
348 dictionaryReader.readNextStartElement();
349 if(dictionaryReader.name()=="description")
350 _infoNote=dictionaryReader.readElementText();
352 dictionaryFile.close();
355 QString XdxfPlugin::removeAccents(QString string) {
357 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
358 QString normalized = string.normalized(QString::NormalizationForm_D);
359 normalized = normalized;
360 for(int i=0; i<normalized.size(); i++) {
361 if( !normalized[i].isLetterOrNumber() &&
362 !normalized[i].isSpace() &&
363 !normalized[i].isDigit() &&
364 normalized[i] != '*' &&
365 normalized[i] != '%' &&
366 normalized[i] != '_' &&
367 normalized[i] != '?' ) {
368 normalized.remove(i,1);
374 QIcon* XdxfPlugin::icon() {
378 int XdxfPlugin::countWords() {
382 QFile dictionaryFile(path);
383 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
384 qDebug()<<"Error: could not open file";
388 dictionaryFile.seek(0);
393 while(!dictionaryFile.atEnd()) {
394 line = dictionaryFile.readLine();
395 if(line.contains("<k>")) {
399 _wordsCount = wordsCount;
400 dictionaryFile.close();
406 bool XdxfPlugin::makeCache(QString dir) {
407 cachingDialog->setVisible(true);
408 QCoreApplication::processEvents();
410 QFileInfo dictFileN(_settings->value("path"));
412 cachePathN = QDir::homePath() + "/.mdictionary/"
413 + dictFileN.completeBaseName() + ".cache";
415 QFile dictionaryFile(dictFileN.filePath());
418 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
422 QXmlStreamReader reader(&dictionaryFile);
425 db.setDatabaseName(cachePathN);
427 qDebug() << "Database error" << endl;
430 QCoreApplication::processEvents();
432 cur.exec("PRAGMA synchronous = 0");
433 cur.exec("drop table dict");
434 QCoreApplication::processEvents();
435 cur.exec("create table dict(word text ,translation text)");
449 while (!reader.atEnd() && !stopped) {
451 QCoreApplication::processEvents();
455 if(reader.tokenType() == QXmlStreamReader::StartElement) {
456 if(reader.name()=="k"){
457 a = reader.readElementText();
461 else if(reader.tokenType() == QXmlStreamReader::Characters) {
463 QString temp(reader.text().toString());
464 temp.replace("\n","");
467 while(reader.name()!="ar"&&
470 if(reader.name()!="" && reader.name()!="ar") {
475 temp=temp+reader.name().toString() + tr(">");
478 temp+=reader.text().toString();
482 cur.prepare("insert into dict values(?,?)");
484 cur.addBindValue(temp);
487 int prog = counter*100/_wordsCount;
488 if(prog % 5 == 0 && lastProg != prog) {
489 Q_EMIT updateCachingProgress(prog,
499 cur.exec("select count(*) from dict");
502 cachingDialog->setVisible(false);
504 if(!cur.next() || countWords() != cur.value(0).toInt())
506 _settings->setValue("cache_path", cachePathN);
507 _settings->setValue("cached", "true");
513 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)