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)
88 word = word.replace("*", "%");
89 word = removeAccents(word);
92 cur.prepare("select word from dict where word like ? limit ?");
93 cur.addBindValue(word);
94 cur.addBindValue(limit);
97 translations.insert(new TranslationXdxf(cur.value(0).toString(),
99 return translations.toList();
104 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
105 QSet<Translation*> translations;
106 QFile dictionaryFile(path);
108 word = removeAccents(word);
111 if(word.indexOf("*")==-1)
113 QRegExp regWord(word);
114 regWord.setCaseSensitivity(Qt::CaseInsensitive);
115 regWord.setPatternSyntax(QRegExp::Wildcard);
116 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
117 qDebug()<<"Error: could not open file";
118 return translations.toList();
121 QXmlStreamReader dictionaryReader(&dictionaryFile);
122 /*search words list*/
125 while(!dictionaryReader.atEnd() && !stopped){
126 dictionaryReader.readNextStartElement();
127 if(dictionaryReader.name()=="ar"){
128 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
129 dictionaryReader.readNextStartElement();
130 if(!dictionaryReader.atEnd())
131 a = dictionaryReader.readElementText();
132 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
135 foreach(tran,translations)
138 ok=false; /*if key word is in the dictionary more that one */
140 if(ok) /*add key word to list*/
141 translations<<(new TranslationXdxf(a,_infoNote,this));
143 if(i>=limit && limit!=0)
147 this->thread()->yieldCurrentThread();
150 dictionaryFile.close();
151 return translations.toList();
154 QString XdxfPlugin::search(QString key) {
155 if(_settings->value("cached") == "true")
156 return searchCache(key);
157 return searchFile(key);
162 QString XdxfPlugin::searchCache(QString key) {
164 QString cacheFilePath = _settings->value("cache_path");
165 db.setDatabaseName(cacheFilePath);
168 qDebug() << "Database error" << db.lastError().text() << endl;
169 return searchFile(key);
173 cur.prepare("select translation from dict where word like ? limit 1");
174 cur.addBindValue(key);
177 result = cur.value(0).toString();
185 QString XdxfPlugin::searchFile(QString key) {
186 QFile dictionaryFile(path);
187 QString resultString("");
188 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
189 qDebug()<<"Error: could not open file";
192 QXmlStreamReader dictionaryReader(&dictionaryFile);
199 while (!dictionaryReader.atEnd()&& !stopped) {
200 dictionaryReader.readNext();
201 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
202 if(dictionaryReader.name()=="k") {
203 a = dictionaryReader.readElementText();
208 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
210 QString temp(dictionaryReader.text().toString());
211 temp.replace("\n","");
213 while(dictionaryReader.name()!="ar"&&
214 !dictionaryReader.atEnd()){
215 dictionaryReader.readNext();
216 temp+=dictionaryReader.text().toString();
219 resultString+=temp.replace("\n","")+"\n";
223 this->thread()->yieldCurrentThread();
226 dictionaryFile.close();
230 void XdxfPlugin::stop() {
234 DictDialog* XdxfPlugin::dictDialog() {
238 void XdxfPlugin::setPath(QString path){
240 _settings->setValue("path",path);
241 //getDictionaryInfo();
245 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
246 XdxfPlugin *plugin = new XdxfPlugin();
248 plugin->setPath(settings->value("path"));
250 QStringList list = settings->keys();
251 foreach(QString key, list)
252 plugin->settings()->setValue(key, settings->value(key));
255 plugin->db_name = plugin->_settings->value("type")
256 + plugin->_settings->value("path");
257 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
259 if(settings->value("cached").isEmpty() &&
260 settings->value("generateCache") == "true") {
261 plugin->makeCache("");
265 plugin->getDictionaryInfo();
269 bool XdxfPlugin::isAvailable() const {
273 void XdxfPlugin::setHash(uint _hash)
278 uint XdxfPlugin::hash() const
283 Settings* XdxfPlugin::settings() {
287 bool XdxfPlugin::isCached()
292 void XdxfPlugin::setSettings(Settings *settings) {
294 QString oldPath = _settings->value("path");
295 if(oldPath != settings->value("path")) {
296 setPath(settings->value("path"));
299 if((_settings->value("cached") == "false" ||
300 _settings->value("cached").isEmpty()) &&
301 settings->value("generateCache") == "true") {
305 emit settingsChanged();
309 void XdxfPlugin::getDictionaryInfo() {
310 QFile dictionaryFile(path);
311 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
312 qDebug()<<"Error: could not open file";
316 QXmlStreamReader dictionaryReader(&dictionaryFile);
317 dictionaryReader.readNextStartElement();
318 if(dictionaryReader.name()=="xdxf") {
319 if(dictionaryReader.attributes().hasAttribute("lang_from"))
320 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
321 if(dictionaryReader.attributes().hasAttribute("lang_to"))
322 _langTo = dictionaryReader.attributes().value("lang_to").toString();
324 dictionaryReader.readNextStartElement();
325 if(dictionaryReader.name()=="full_name")
326 _name=dictionaryReader.readElementText();
327 dictionaryReader.readNextStartElement();
328 if(dictionaryReader.name()=="description")
329 _infoNote=dictionaryReader.readElementText();
331 dictionaryFile.close();
334 QString XdxfPlugin::removeAccents(QString string) {
336 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
337 QString normalized = string.normalized(QString::NormalizationForm_D);
338 normalized = normalized;
339 for(int i=0; i<normalized.size(); i++) {
340 if( !normalized[i].isLetterOrNumber() &&
341 !normalized[i].isSpace() &&
342 !normalized[i].isDigit() &&
343 normalized[i] != '*' &&
344 normalized[i] != '%') {
345 normalized.remove(i,1);
351 QIcon* XdxfPlugin::icon() {
355 int XdxfPlugin::countWords() {
359 QFile dictionaryFile(path);
360 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
361 qDebug()<<"Error: could not open file";
365 dictionaryFile.seek(0);
370 while(!dictionaryFile.atEnd()) {
371 line = dictionaryFile.readLine();
372 if(line.contains("<k>")) {
376 _wordsCount = wordsCount;
377 dictionaryFile.close();
383 bool XdxfPlugin::makeCache(QString dir) {
384 cachingDialog->setVisible(true);
385 QCoreApplication::processEvents();
387 QFileInfo dictFileN(_settings->value("path"));
389 cachePathN = QDir::homePath() + "/.mdictionary/"
390 + dictFileN.completeBaseName() + ".cache";
392 QFile dictionaryFile(dictFileN.filePath());
395 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
399 QXmlStreamReader reader(&dictionaryFile);
402 db.setDatabaseName(cachePathN);
404 qDebug() << "Database error" << endl;
407 QCoreApplication::processEvents();
409 cur.exec("PRAGMA synchronous = 0");
410 cur.exec("drop table dict");
411 QCoreApplication::processEvents();
412 cur.exec("create table dict(word text ,translation text)");
424 while (!reader.atEnd() && !stopped) {
426 QCoreApplication::processEvents();
430 if(reader.tokenType() == QXmlStreamReader::StartElement) {
431 if(reader.name()=="k"){
432 a = reader.readElementText();
436 else if(reader.tokenType() == QXmlStreamReader::Characters) {
438 QString temp(reader.text().toString());
439 temp.replace("\n","");
441 while(reader.name()!="ar"&&
444 temp+=reader.text().toString();
448 cur.prepare("insert into dict values(?,?)");
450 cur.addBindValue(temp);
453 int prog = counter*100/_wordsCount;
455 Q_EMIT updateCachingProgress(prog);
462 cur.exec("select count(*) from dict");
465 cachingDialog->setVisible(false);
467 if(!cur.next() || countWords() != cur.value(0).toInt())
469 _settings->setValue("cache_path", cachePathN);
470 _settings->setValue("cached", "true");
476 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)