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(),
106 return translations.toList();
111 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
112 QSet<Translation*> translations;
113 QFile dictionaryFile(path);
115 word = removeAccents(word);
118 QRegExp regWord(word);
119 regWord.setCaseSensitivity(Qt::CaseInsensitive);
120 regWord.setPatternSyntax(QRegExp::Wildcard);
121 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
122 qDebug()<<"Error: could not open file";
123 return translations.toList();
126 QXmlStreamReader reader(&dictionaryFile);
127 /*search words list*/
130 while(!reader.atEnd() && !stopped){
131 reader.readNextStartElement();
132 if(reader.name()=="ar") {
133 while(reader.name()!="k" && !reader.atEnd())
134 reader.readNextStartElement();
136 a = reader.readElementText();
137 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
140 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();
191 QString XdxfPlugin::searchFile(QString key) {
192 QFile dictionaryFile(path);
193 QString resultString("");
194 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
195 qDebug()<<"Error: could not open file";
198 QXmlStreamReader reader(&dictionaryFile);
205 while (!reader.atEnd()&& !stopped) {
207 if(reader.tokenType() == QXmlStreamReader::StartElement) {
208 if(reader.name()=="k") {
209 a = reader.readElementText();
216 while(reader.name()!="ar" && !reader.atEnd()) {
217 if(reader.name()!="" && reader.name()!="k") {
218 if(reader.tokenType()==QXmlStreamReader::EndElement)
220 if(reader.tokenType()==QXmlStreamReader::StartElement)
222 temp+=reader.name().toString();
223 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
224 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
227 temp+= reader.text().toString();
230 resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
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 if(!plugin->db.connectionName().isEmpty())
268 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
270 if(settings->value("cached").isEmpty() &&
271 settings->value("generateCache") == "true") {
272 plugin->makeCache("");
276 plugin->getDictionaryInfo();
280 bool XdxfPlugin::isAvailable() const {
284 void XdxfPlugin::setHash(uint _hash) {
288 uint XdxfPlugin::hash() const {
292 Settings* XdxfPlugin::settings() {
296 bool XdxfPlugin::isCached() {
297 if(_settings->value("cached") == "true")
302 void XdxfPlugin::setSettings(Settings *settings) {
304 QString oldPath = _settings->value("path");
305 if(oldPath != settings->value("path")) {
306 setPath(settings->value("path"));
309 if((_settings->value("cached") == "false" ||
310 _settings->value("cached").isEmpty()) &&
311 settings->value("generateCache") == "true") {
315 _settings->setValue("cached", "false");
318 emit settingsChanged();
322 void XdxfPlugin::getDictionaryInfo() {
323 QFile dictionaryFile(path);
324 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
325 qDebug()<<"Error: could not open file";
329 QXmlStreamReader reader(&dictionaryFile);
330 reader.readNextStartElement();
331 if(reader.name()=="xdxf") {
332 if(reader.attributes().hasAttribute("lang_from"))
333 _langFrom = reader.attributes().value("lang_from").toString();
334 if(reader.attributes().hasAttribute("lang_to"))
335 _langTo = reader.attributes().value("lang_to").toString();
337 reader.readNextStartElement();
338 if(reader.name()=="full_name")
339 _name=reader.readElementText();
340 reader.readNextStartElement();
341 if(reader.name()=="description")
342 _infoNote=reader.readElementText();
344 dictionaryFile.close();
347 QString XdxfPlugin::removeAccents(QString string) {
349 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
350 QString normalized = string.normalized(QString::NormalizationForm_D);
351 normalized = normalized;
352 for(int i=0; i<normalized.size(); i++) {
353 if( !normalized[i].isLetterOrNumber() &&
354 !normalized[i].isSpace() &&
355 !normalized[i].isDigit() &&
356 normalized[i] != '*' &&
357 normalized[i] != '%' &&
358 normalized[i] != '_' &&
359 normalized[i] != '?' ) {
360 normalized.remove(i,1);
366 QIcon* XdxfPlugin::icon() {
370 int XdxfPlugin::countWords() {
374 QFile dictionaryFile(path);
375 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
376 qDebug()<<"Error: could not open file";
380 dictionaryFile.seek(0);
385 while(!dictionaryFile.atEnd()) {
386 line = dictionaryFile.readLine();
387 if(line.contains("<k>")) {
391 _wordsCount = wordsCount;
392 dictionaryFile.close();
398 bool XdxfPlugin::makeCache(QString dir) {
399 cachingDialog->setVisible(true);
400 QCoreApplication::processEvents();
402 QFileInfo dictFileN(_settings->value("path"));
404 cachePathN = QDir::homePath() + "/.mdictionary/"
405 + dictFileN.completeBaseName() + ".cache";
407 QFile dictionaryFile(dictFileN.filePath());
410 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
414 QXmlStreamReader reader(&dictionaryFile);
416 db.setDatabaseName(cachePathN);
418 qDebug() << "Database error" << db.lastError().text() << endl;
421 QCoreApplication::processEvents();
423 cur.exec("PRAGMA synchronous = 0");
424 cur.exec("drop table dict");
425 QCoreApplication::processEvents();
426 cur.exec("create table dict(word text ,translation text)");
440 while (!reader.atEnd() && !stopped) {
442 QCoreApplication::processEvents();
445 if(reader.tokenType() == QXmlStreamReader::StartElement) {
446 if(reader.name()=="k"){
447 a = reader.readElementText();
453 while(reader.name()!="ar" && !reader.atEnd()) {
454 if(reader.name()!="" && reader.name()!="k") {
455 if(reader.tokenType()==QXmlStreamReader::EndElement)
457 if(reader.tokenType()==QXmlStreamReader::StartElement)
459 temp+=reader.name().toString();
460 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
461 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
464 temp+= reader.text().toString();
467 temp += tr("<t>") + temp.replace("\n","") + tr("</t>");
469 cur.prepare("insert into dict values(?,?)");
471 cur.addBindValue(temp);
474 int prog = counter*100/_wordsCount;
475 if(prog % 5 == 0 && lastProg != prog) {
476 Q_EMIT updateCachingProgress(prog,
484 cur.exec("select count(*) from dict");
487 cachingDialog->setVisible(false);
489 if(!cur.next() || countWords() != cur.value(0).toInt())
494 _settings->setValue("cache_path", cachePathN);
495 _settings->setValue("cached", "true");
502 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)