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) {
139 ok=false; /*if key word is in the dictionary more that one */
141 if(ok) /*add key word to list*/
142 translations<<(new TranslationXdxf(a,_infoNote,this));
144 if(i>=limit && limit!=0)
148 this->thread()->yieldCurrentThread();
151 dictionaryFile.close();
152 return translations.toList();
155 QString XdxfPlugin::search(QString key) {
156 if(_settings->value("cached") == "true")
157 return searchCache(key);
158 return searchFile(key);
163 QString XdxfPlugin::searchCache(QString key) {
165 QString cacheFilePath = _settings->value("cache_path");
166 db.setDatabaseName(cacheFilePath);
169 qDebug() << "Database error" << db.lastError().text() << endl;
170 return searchFile(key);
174 cur.prepare("select translation from dict where word like ? limit 1");
175 cur.addBindValue(key);
178 result = cur.value(0).toString();
186 QString XdxfPlugin::searchFile(QString key) {
187 QFile dictionaryFile(path);
188 QString resultString("");
189 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
190 qDebug()<<"Error: could not open file";
193 QXmlStreamReader dictionaryReader(&dictionaryFile);
200 while (!dictionaryReader.atEnd()&& !stopped) {
201 dictionaryReader.readNext();
202 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
203 if(dictionaryReader.name()=="k") {
204 a = dictionaryReader.readElementText();
209 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
211 QString temp(dictionaryReader.text().toString());
212 temp.replace("\n","");
214 while(dictionaryReader.name()!="ar"&&
215 !dictionaryReader.atEnd()){
216 dictionaryReader.readNext();
217 temp+=dictionaryReader.text().toString();
220 resultString+=temp.replace("\n","")+"\n";
224 this->thread()->yieldCurrentThread();
227 dictionaryFile.close();
231 void XdxfPlugin::stop() {
235 DictDialog* XdxfPlugin::dictDialog() {
239 void XdxfPlugin::setPath(QString path){
241 _settings->setValue("path",path);
242 //getDictionaryInfo();
246 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
247 XdxfPlugin *plugin = new XdxfPlugin();
249 plugin->setPath(settings->value("path"));
251 QStringList list = settings->keys();
252 foreach(QString key, list)
253 plugin->settings()->setValue(key, settings->value(key));
256 plugin->db_name = plugin->_settings->value("type")
257 + plugin->_settings->value("path");
258 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
260 if(settings->value("cached").isEmpty() &&
261 settings->value("generateCache") == "true") {
262 plugin->makeCache("");
266 plugin->getDictionaryInfo();
270 bool XdxfPlugin::isAvailable() const {
274 void XdxfPlugin::setHash(uint _hash) {
278 uint XdxfPlugin::hash() const {
282 Settings* XdxfPlugin::settings() {
286 bool XdxfPlugin::isCached() {
290 void XdxfPlugin::setSettings(Settings *settings) {
292 QString oldPath = _settings->value("path");
293 if(oldPath != settings->value("path")) {
294 setPath(settings->value("path"));
297 if((_settings->value("cached") == "false" ||
298 _settings->value("cached").isEmpty()) &&
299 settings->value("generateCache") == "true") {
303 _settings->setValue("cached", "false");
306 emit settingsChanged();
310 void XdxfPlugin::getDictionaryInfo() {
311 QFile dictionaryFile(path);
312 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
313 qDebug()<<"Error: could not open file";
317 QXmlStreamReader dictionaryReader(&dictionaryFile);
318 dictionaryReader.readNextStartElement();
319 if(dictionaryReader.name()=="xdxf") {
320 if(dictionaryReader.attributes().hasAttribute("lang_from"))
321 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
322 if(dictionaryReader.attributes().hasAttribute("lang_to"))
323 _langTo = dictionaryReader.attributes().value("lang_to").toString();
325 dictionaryReader.readNextStartElement();
326 if(dictionaryReader.name()=="full_name")
327 _name=dictionaryReader.readElementText();
328 dictionaryReader.readNextStartElement();
329 if(dictionaryReader.name()=="description")
330 _infoNote=dictionaryReader.readElementText();
332 dictionaryFile.close();
335 QString XdxfPlugin::removeAccents(QString string) {
337 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
338 QString normalized = string.normalized(QString::NormalizationForm_D);
339 normalized = normalized;
340 for(int i=0; i<normalized.size(); i++) {
341 if( !normalized[i].isLetterOrNumber() &&
342 !normalized[i].isSpace() &&
343 !normalized[i].isDigit() &&
344 normalized[i] != '*' &&
345 normalized[i] != '%') {
346 normalized.remove(i,1);
352 QIcon* XdxfPlugin::icon() {
356 int XdxfPlugin::countWords() {
360 QFile dictionaryFile(path);
361 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
362 qDebug()<<"Error: could not open file";
366 dictionaryFile.seek(0);
371 while(!dictionaryFile.atEnd()) {
372 line = dictionaryFile.readLine();
373 if(line.contains("<k>")) {
377 _wordsCount = wordsCount;
378 dictionaryFile.close();
384 bool XdxfPlugin::makeCache(QString dir) {
385 cachingDialog->setVisible(true);
386 QCoreApplication::processEvents();
388 QFileInfo dictFileN(_settings->value("path"));
390 cachePathN = QDir::homePath() + "/.mdictionary/"
391 + dictFileN.completeBaseName() + ".cache";
393 QFile dictionaryFile(dictFileN.filePath());
396 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
400 QXmlStreamReader reader(&dictionaryFile);
403 db.setDatabaseName(cachePathN);
405 qDebug() << "Database error" << endl;
408 QCoreApplication::processEvents();
410 cur.exec("PRAGMA synchronous = 0");
411 cur.exec("drop table dict");
412 QCoreApplication::processEvents();
413 cur.exec("create table dict(word text ,translation text)");
427 while (!reader.atEnd() && !stopped) {
429 QCoreApplication::processEvents();
433 if(reader.tokenType() == QXmlStreamReader::StartElement) {
434 if(reader.name()=="k"){
435 a = reader.readElementText();
439 else if(reader.tokenType() == QXmlStreamReader::Characters) {
441 QString temp(reader.text().toString());
442 temp.replace("\n","");
444 while(reader.name()!="ar"&&
447 temp+=reader.text().toString();
451 cur.prepare("insert into dict values(?,?)");
453 cur.addBindValue(temp);
456 int prog = counter*100/_wordsCount;
457 if(prog % 5 == 0 && lastProg != prog) {
458 Q_EMIT updateCachingProgress(prog,
468 cur.exec("select count(*) from dict");
471 cachingDialog->setVisible(false);
473 if(!cur.next() || countWords() != cur.value(0).toInt())
475 _settings->setValue("cache_path", cachePathN);
476 _settings->setValue("cached", "true");
482 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)