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());
221 temp.replace("\n","");
223 while(dictionaryReader.name()!="ar"&&
224 !dictionaryReader.atEnd()){
225 dictionaryReader.readNext();
226 temp+=dictionaryReader.text().toString();
229 resultString+=temp.replace("\n","")+"\n";
233 this->thread()->yieldCurrentThread();
236 dictionaryFile.close();
240 void XdxfPlugin::stop() {
244 DictDialog* XdxfPlugin::dictDialog() {
248 void XdxfPlugin::setPath(QString path){
250 _settings->setValue("path",path);
251 //getDictionaryInfo();
255 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
256 XdxfPlugin *plugin = new XdxfPlugin();
258 plugin->setPath(settings->value("path"));
260 QStringList list = settings->keys();
261 foreach(QString key, list)
262 plugin->settings()->setValue(key, settings->value(key));
265 plugin->db_name = plugin->_settings->value("type")
266 + plugin->_settings->value("path");
267 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
269 if(settings->value("cached").isEmpty() &&
270 settings->value("generateCache") == "true") {
271 plugin->makeCache("");
275 plugin->getDictionaryInfo();
279 bool XdxfPlugin::isAvailable() const {
283 void XdxfPlugin::setHash(uint _hash) {
287 uint XdxfPlugin::hash() const {
291 Settings* XdxfPlugin::settings() {
295 bool XdxfPlugin::isCached() {
296 if(_settings->value("cached") == "true")
301 void XdxfPlugin::setSettings(Settings *settings) {
303 QString oldPath = _settings->value("path");
304 if(oldPath != settings->value("path")) {
305 setPath(settings->value("path"));
308 if((_settings->value("cached") == "false" ||
309 _settings->value("cached").isEmpty()) &&
310 settings->value("generateCache") == "true") {
314 _settings->setValue("cached", "false");
317 emit settingsChanged();
321 void XdxfPlugin::getDictionaryInfo() {
322 QFile dictionaryFile(path);
323 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
324 qDebug()<<"Error: could not open file";
328 QXmlStreamReader dictionaryReader(&dictionaryFile);
329 dictionaryReader.readNextStartElement();
330 if(dictionaryReader.name()=="xdxf") {
331 if(dictionaryReader.attributes().hasAttribute("lang_from"))
332 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
333 if(dictionaryReader.attributes().hasAttribute("lang_to"))
334 _langTo = dictionaryReader.attributes().value("lang_to").toString();
336 dictionaryReader.readNextStartElement();
337 if(dictionaryReader.name()=="full_name")
338 _name=dictionaryReader.readElementText();
339 dictionaryReader.readNextStartElement();
340 if(dictionaryReader.name()=="description")
341 _infoNote=dictionaryReader.readElementText();
343 dictionaryFile.close();
346 QString XdxfPlugin::removeAccents(QString string) {
347 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
348 QString normalized = string.normalized(QString::NormalizationForm_D);
349 normalized = normalized;
350 for(int i=0; i<normalized.size(); i++) {
351 if( !normalized[i].isLetterOrNumber() &&
352 !normalized[i].isSpace() &&
353 !normalized[i].isDigit() &&
354 normalized[i] != '*' &&
355 normalized[i] != '%' &&
356 normalized[i] != '_' &&
357 normalized[i] != '?' ) {
358 normalized.remove(i,1);
364 QIcon* XdxfPlugin::icon() {
368 int XdxfPlugin::countWords() {
372 QFile dictionaryFile(path);
373 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
374 qDebug()<<"Error: could not open file";
378 dictionaryFile.seek(0);
383 while(!dictionaryFile.atEnd()) {
384 line = dictionaryFile.readLine();
385 if(line.contains("<k>")) {
389 _wordsCount = wordsCount;
390 dictionaryFile.close();
396 bool XdxfPlugin::makeCache(QString dir) {
397 cachingDialog->setVisible(true);
398 QCoreApplication::processEvents();
400 QFileInfo dictFileN(_settings->value("path"));
402 cachePathN = QDir::homePath() + "/.mdictionary/"
403 + dictFileN.completeBaseName() + ".cache";
405 QFile dictionaryFile(dictFileN.filePath());
408 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
412 QXmlStreamReader reader(&dictionaryFile);
414 db.setDatabaseName(cachePathN);
416 qDebug() << "Database error" << db.lastError().text() << endl;
419 QCoreApplication::processEvents();
421 cur.exec("PRAGMA synchronous = 0");
422 cur.exec("drop table dict");
423 QCoreApplication::processEvents();
424 cur.exec("create table dict(word text ,translation text)");
438 while (!reader.atEnd() && !stopped) {
440 QCoreApplication::processEvents();
443 if(reader.tokenType() == QXmlStreamReader::StartElement) {
444 if(reader.name()=="k"){
445 a = reader.readElementText();
449 else if(reader.tokenType() == QXmlStreamReader::Characters) {
451 QString temp(reader.text().toString());
452 temp.replace("\n","");
454 while(reader.name()!="ar"&&
457 temp+=reader.text().toString();
461 cur.prepare("insert into dict values(?,?)");
463 cur.addBindValue(temp);
466 int prog = counter*100/_wordsCount;
467 if(prog % 5 == 0 && lastProg != prog) {
468 Q_EMIT updateCachingProgress(prog,
478 cur.exec("select count(*) from dict");
481 cachingDialog->setVisible(false);
483 if(!cur.next() || countWords() != cur.value(0).toInt())
485 _settings->setValue("cache_path", cachePathN);
486 _settings->setValue("cached", "true");
492 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)