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 /*! \file xdxfplugin.cpp
23 \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
26 #include "xdxfplugin.h"
29 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
30 _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
31 _type(tr("xdxf")), _infoNote(tr("")) {
33 _settings = new Settings();
34 _dictDialog = new XdxfDictDialog(this, this);
35 cachingDialog = new XdxfCachingDialog(this);
37 connect(cachingDialog, SIGNAL(cancelCaching()),
40 _settings->setValue("type","xdxf");
44 _icon = QIcon(":/icons/xdxf.png");
47 XdxfPlugin::~XdxfPlugin()
49 // QString connection(db.connectionName());
51 // QSqlDatabase::removeDatabase(connection);
56 QString XdxfPlugin::langFrom() const {
60 QString XdxfPlugin::langTo() const {
64 QString XdxfPlugin::name() const {
68 QString XdxfPlugin::type() const {
69 // return _settings->value("type");
73 QString XdxfPlugin::infoNote() const {
77 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
78 //if(_settings->value("cached") == "true")
79 if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
80 && word.indexOf("%")==-1)
83 return searchWordListCache(word,limit);
84 return searchWordListFile(word, limit);
87 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
89 QSet<Translation*> translations;
90 QString cacheFilePath = _settings->value("cache_path");
91 db.setDatabaseName(cacheFilePath);
93 qDebug() << "Database error" << db.lastError().text() << endl;
94 return searchWordListFile(word, limit);
98 word = word.toLower();
99 word = word.replace("*", "%");
100 word = word.replace("?", "_");
101 word = removeAccents(word);
106 cur.prepare("select word from dict where word like ? limit ?");
108 cur.prepare("select word from dict where word like ?");
109 cur.addBindValue(word);
111 cur.addBindValue(limit);
116 foreach(tran,translations) {
117 if(tran->key().toLower()==cur.value(0).toString().toLower())
120 if(ok) /*add key word to list*/
121 translations.insert(new TranslationXdxf(
122 cur.value(0).toString().toLower(),
126 return translations.toList();
129 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
130 QSet<Translation*> translations;
131 QFile dictionaryFile(path);
133 word = word.toLower();
134 word = removeAccents(word);
137 QRegExp regWord(word);
138 regWord.setCaseSensitivity(Qt::CaseInsensitive);
139 regWord.setPatternSyntax(QRegExp::Wildcard);
140 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
141 qDebug()<<"Error: could not open file";
142 return translations.toList();
145 QXmlStreamReader reader(&dictionaryFile);
146 /*search words list*/
149 while(!reader.atEnd() && !stopped){
150 reader.readNextStartElement();
151 if(reader.name()=="ar") {
152 while(reader.name()!="k" && !reader.atEnd())
153 reader.readNextStartElement();
155 a = reader.readElementText();
156 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
159 foreach(tran,translations) {
160 if(tran->key().toLower()==a.toLower())
161 ok=false; /*if key word is in the dictionary more that one */
163 if(ok) /*add key word to list*/
164 translations<<(new TranslationXdxf(a.toLower(),
167 if(i>=limit && limit!=0)
171 this->thread()->yieldCurrentThread();
174 dictionaryFile.close();
175 return translations.toList();
178 QString XdxfPlugin::search(QString key) {
179 // if(_settings->value("cached") == "true")
181 return searchCache(key);
182 return searchFile(key);
185 QString XdxfPlugin::searchCache(QString key) {
187 QString cacheFilePath = _settings->value("cache_path");
188 db.setDatabaseName(cacheFilePath);
192 qDebug() << "Database error" << db.lastError().text() << endl;
193 return searchFile(key);
197 cur.prepare("select translation from dict where word like ?");
198 cur.addBindValue(key);
201 result += cur.value(0).toString();
209 QString XdxfPlugin::searchFile(QString key) {
211 QFile dictionaryFile(path);
212 QString resultString("");
213 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
214 qDebug()<<"Error: could not open file when search";
217 QXmlStreamReader reader(&dictionaryFile);
222 while (!reader.atEnd()&& !stopped) {
224 if(reader.tokenType() == QXmlStreamReader::StartElement) {
225 if(reader.name()=="k") {
226 a = reader.readElementText();
227 if(a.toLower()==key.toLower())
233 while(reader.name()!="ar" && !reader.atEnd()) {
234 if(reader.name()!="" && reader.name()!="k") {
235 if(reader.tokenType()==QXmlStreamReader::EndElement)
237 if(reader.tokenType()==QXmlStreamReader::StartElement)
239 temp+=reader.name().toString();
240 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
241 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
244 temp+= reader.text().toString().replace("<","<").replace(">",">");
247 if(temp.at(0)==QChar('\n'))
249 resultString+=tr("<key>") + a +tr("</key>");
250 resultString+=tr("<t>") + temp + tr("</t>");
253 this->thread()->yieldCurrentThread();
256 dictionaryFile.close();
261 void XdxfPlugin::stop() {
265 DictDialog* XdxfPlugin::dictDialog() {
269 void XdxfPlugin::setPath(QString path){
271 _settings->setValue("path",path);
272 //getDictionaryInfo();
275 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
276 XdxfPlugin *plugin = new XdxfPlugin();
278 plugin->setPath(settings->value("path"));
279 QStringList list = settings->keys();
280 foreach(QString key, list)
281 plugin->settings()->setValue(key, settings->value(key));
283 plugin->db_name = plugin->_settings->value("type")
284 + plugin->_settings->value("path");
285 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
287 if(settings->value("cached").isEmpty() &&
288 settings->value("generateCache") == "true") {
289 plugin->makeCache("");
293 plugin->getDictionaryInfo();
297 bool XdxfPlugin::isAvailable() const {
301 void XdxfPlugin::setHash(uint _hash) {
305 uint XdxfPlugin::hash() const {
309 Settings* XdxfPlugin::settings() {
313 bool XdxfPlugin::isCached() {
314 if(_settings->value("cached") == "true")
319 void XdxfPlugin::setSettings(Settings *settings) {
321 QString oldPath = _settings->value("path");
322 if(oldPath != settings->value("path")) {
323 setPath(settings->value("path"));
326 if((_settings->value("cached") == "false" ||
327 _settings->value("cached").isEmpty()) &&
328 settings->value("generateCache") == "true") {
332 _settings->setValue("cached", "false");
336 Q_EMIT settingsChanged();
339 void XdxfPlugin::getDictionaryInfo() {
340 QFile dictionaryFile(path);
341 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
342 qDebug()<<"Error: could not open file";
346 QXmlStreamReader reader(&dictionaryFile);
347 reader.readNextStartElement();
348 if(reader.name()=="xdxf") {
349 if(reader.attributes().hasAttribute("lang_from"))
350 _langFrom = reader.attributes().value("lang_from").toString();
351 if(reader.attributes().hasAttribute("lang_to"))
352 _langTo = reader.attributes().value("lang_to").toString();
354 reader.readNextStartElement();
355 if(reader.name()=="full_name")
356 _name=reader.readElementText();
357 reader.readNextStartElement();
358 if(reader.name()=="description")
359 _infoNote=reader.readElementText();
361 QString format = "png";
362 QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
363 // qDebug()<<initialPath;
364 // QPixmap test(":/icons/xdxf.png");
365 // qDebug()<<QPixmap(test).save(initialPath,format.toAscii());
366 // qDebug()<<QPixmap("/home/jakub/star.jpg").save(initialPath,format.toAscii());
368 _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-" + _langTo + "] (" + _type + ")";
370 dictionaryFile.close();
373 QString XdxfPlugin::removeAccents(QString string) {
374 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
375 QString normalized = string.normalized(QString::NormalizationForm_D);
376 normalized = normalized;
377 for(int i=0; i<normalized.size(); i++) {
378 if( !normalized[i].isLetterOrNumber() &&
379 !normalized[i].isSpace() &&
380 !normalized[i].isDigit() &&
381 normalized[i] != '*' &&
382 normalized[i] != '%' &&
383 normalized[i] != '_' &&
384 normalized[i] != '?' ) {
385 normalized.remove(i,1);
391 QIcon* XdxfPlugin::icon() {
395 int XdxfPlugin::countWords() {
399 QFile dictionaryFile(path);
400 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
401 qDebug()<<"Error: could not open file";
405 dictionaryFile.seek(0);
410 while(!dictionaryFile.atEnd()) {
411 line = dictionaryFile.readLine();
412 if(line.contains("<k>")) {
416 _wordsCount = wordsCount;
417 dictionaryFile.close();
421 bool XdxfPlugin::makeCache(QString dir) {
422 cachingDialog->setVisible(true);
423 QCoreApplication::processEvents();
425 QFileInfo dictFileN(_settings->value("path"));
427 cachePathN = QDir::homePath() + "/.mdictionary/"
428 + dictFileN.completeBaseName() + ".cache";
430 QFile dictionaryFile(dictFileN.filePath());
433 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
437 QXmlStreamReader reader(&dictionaryFile);
439 db.setDatabaseName(cachePathN);
441 qDebug() << "Database error" << db.lastError().text() << endl;
444 QCoreApplication::processEvents();
446 cur.exec("PRAGMA synchronous = 0");
447 cur.exec("drop table dict");
448 QCoreApplication::processEvents();
449 cur.exec("create table dict(word text ,translation text)");
463 while (!reader.atEnd() && !stopped) {
465 QCoreApplication::processEvents();
468 if(reader.tokenType() == QXmlStreamReader::StartElement) {
469 if(reader.name()=="k"){
470 a = reader.readElementText();
476 while(reader.name()!="ar" && !reader.atEnd()) {
477 if(reader.name()!="" && reader.name()!="k") {
478 if(reader.tokenType()==QXmlStreamReader::EndElement)
480 if(reader.tokenType()==QXmlStreamReader::StartElement)
482 temp+=reader.name().toString();
483 if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
484 temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
487 temp+= reader.text().toString().replace("<","<").replace(">",">");;
490 if(temp.at(0)==QChar('\n'))
492 temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
494 cur.prepare("insert into dict values(?,?)");
496 cur.addBindValue(temp);
499 int prog = counter*100/_wordsCount;
500 if(prog % 5 == 0 && lastProg != prog) {
501 Q_EMIT updateCachingProgress(prog,
509 cur.exec("select count(*) from dict");
512 cachingDialog->setVisible(false);
514 if(!cur.next() || countWords() != cur.value(0).toInt())
519 _settings->setValue("cache_path", cachePathN);
520 _settings->setValue("cached", "true");
527 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)