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 dictionaryReader(&dictionaryFile);
127 /*search words list*/
130 while(!dictionaryReader.atEnd() && !stopped){
131 dictionaryReader.readNextStartElement();
132 if(dictionaryReader.name()=="ar"){
133 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
134 dictionaryReader.readNextStartElement();
135 if(!dictionaryReader.atEnd())
136 a = dictionaryReader.readElementText();
137 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
140 foreach(tran,translations)
143 ok=false; /*if key word is in the dictionary more that one */
145 if(ok) /*add key word to list*/
146 translations<<(new TranslationXdxf(a,_infoNote,this));
148 if(i>=limit && limit!=0)
152 this->thread()->yieldCurrentThread();
155 dictionaryFile.close();
156 return translations.toList();
159 QString XdxfPlugin::search(QString key) {
160 // if(_settings->value("cached") == "true")
162 return searchCache(key);
163 return searchFile(key);
168 QString XdxfPlugin::searchCache(QString key) {
170 QString cacheFilePath = _settings->value("cache_path");
171 db.setDatabaseName(cacheFilePath);
174 qDebug() << "Database error" << db.lastError().text() << endl;
175 return searchFile(key);
179 cur.prepare("select translation from dict where word like ? limit 1");
180 cur.addBindValue(key);
183 result = cur.value(0).toString();
192 QString XdxfPlugin::searchFile(QString key) {
193 QFile dictionaryFile(path);
194 QString resultString("");
195 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
196 qDebug()<<"Error: could not open file";
199 QXmlStreamReader dictionaryReader(&dictionaryFile);
206 while (!dictionaryReader.atEnd()&& !stopped) {
207 dictionaryReader.readNext();
208 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
209 if(dictionaryReader.name()=="k") {
210 a = dictionaryReader.readElementText();
215 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
217 QString temp(dictionaryReader.text().toString());
218 temp.replace("\n","");
220 while(dictionaryReader.name()!="ar"&&
221 !dictionaryReader.atEnd()){
222 dictionaryReader.readNext();
223 temp+=dictionaryReader.text().toString();
226 resultString+=temp.replace("\n","")+"\n";
230 this->thread()->yieldCurrentThread();
233 dictionaryFile.close();
237 void XdxfPlugin::stop() {
241 DictDialog* XdxfPlugin::dictDialog() {
245 void XdxfPlugin::setPath(QString path){
247 _settings->setValue("path",path);
248 //getDictionaryInfo();
252 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
253 XdxfPlugin *plugin = new XdxfPlugin();
255 plugin->setPath(settings->value("path"));
257 QStringList list = settings->keys();
258 foreach(QString key, list)
259 plugin->settings()->setValue(key, settings->value(key));
262 plugin->db_name = plugin->_settings->value("type")
263 + plugin->_settings->value("path");
264 if(!plugin->db.connectionName().isEmpty())
265 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
267 if(settings->value("cached").isEmpty() &&
268 settings->value("generateCache") == "true") {
269 plugin->makeCache("");
273 plugin->getDictionaryInfo();
277 bool XdxfPlugin::isAvailable() const {
281 void XdxfPlugin::setHash(uint _hash)
286 uint XdxfPlugin::hash() const
291 Settings* XdxfPlugin::settings() {
295 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 dictionaryReader(&dictionaryFile);
330 dictionaryReader.readNextStartElement();
331 if(dictionaryReader.name()=="xdxf") {
332 if(dictionaryReader.attributes().hasAttribute("lang_from"))
333 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
334 if(dictionaryReader.attributes().hasAttribute("lang_to"))
335 _langTo = dictionaryReader.attributes().value("lang_to").toString();
337 dictionaryReader.readNextStartElement();
338 if(dictionaryReader.name()=="full_name")
339 _name=dictionaryReader.readElementText();
340 dictionaryReader.readNextStartElement();
341 if(dictionaryReader.name()=="description")
342 _infoNote=dictionaryReader.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);
417 db.setDatabaseName(cachePathN);
419 qDebug() << "Database error" << endl;
422 QCoreApplication::processEvents();
424 cur.exec("PRAGMA synchronous = 0");
425 cur.exec("drop table dict");
426 QCoreApplication::processEvents();
427 cur.exec("create table dict(word text ,translation text)");
441 while (!reader.atEnd() && !stopped) {
443 QCoreApplication::processEvents();
447 if(reader.tokenType() == QXmlStreamReader::StartElement) {
448 if(reader.name()=="k"){
449 a = reader.readElementText();
453 else if(reader.tokenType() == QXmlStreamReader::Characters) {
455 QString temp(reader.text().toString());
456 temp.replace("\n","");
458 while(reader.name()!="ar"&&
461 temp+=reader.text().toString();
465 cur.prepare("insert into dict values(?,?)");
467 cur.addBindValue(temp);
470 int prog = counter*100/_wordsCount;
471 if(prog % 5 == 0 && lastProg != prog) {
472 Q_EMIT updateCachingProgress(prog,
482 cur.exec("select count(*) from dict");
485 cachingDialog->setVisible(false);
487 if(!cur.next() || countWords() != cur.value(0).toInt())
492 _settings->setValue("cache_path", cachePathN);
493 _settings->setValue("cached", "true");
500 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)