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(),
105 return translations.toList();
110 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
111 QSet<Translation*> translations;
112 QFile dictionaryFile(path);
114 word = removeAccents(word);
117 QRegExp regWord(word);
118 regWord.setCaseSensitivity(Qt::CaseInsensitive);
119 regWord.setPatternSyntax(QRegExp::Wildcard);
120 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
121 qDebug()<<"Error: could not open file";
122 return translations.toList();
125 QXmlStreamReader dictionaryReader(&dictionaryFile);
126 /*search words list*/
129 while(!dictionaryReader.atEnd() && !stopped){
130 dictionaryReader.readNextStartElement();
131 if(dictionaryReader.name()=="ar"){
132 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
133 dictionaryReader.readNextStartElement();
134 if(!dictionaryReader.atEnd())
135 a = dictionaryReader.readElementText();
136 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
139 foreach(tran,translations) {
141 ok=false; /*if key word is in the dictionary more that one */
143 if(ok) /*add key word to list*/
144 translations<<(new TranslationXdxf(a,_infoNote,this));
146 if(i>=limit && limit!=0)
150 this->thread()->yieldCurrentThread();
153 dictionaryFile.close();
154 return translations.toList();
157 QString XdxfPlugin::search(QString key) {
158 // if(_settings->value("cached") == "true")
160 return searchCache(key);
161 return searchFile(key);
166 QString XdxfPlugin::searchCache(QString key) {
168 QString cacheFilePath = _settings->value("cache_path");
169 db.setDatabaseName(cacheFilePath);
172 qDebug() << "Database error" << db.lastError().text() << endl;
173 return searchFile(key);
177 cur.prepare("select translation from dict where word like ? limit 1");
178 cur.addBindValue(key);
181 result = cur.value(0).toString();
189 QString XdxfPlugin::searchFile(QString key) {
190 QFile dictionaryFile(path);
191 QString resultString("");
192 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
193 qDebug()<<"Error: could not open file";
196 QXmlStreamReader dictionaryReader(&dictionaryFile);
203 while (!dictionaryReader.atEnd()&& !stopped) {
204 dictionaryReader.readNext();
205 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
206 if(dictionaryReader.name()=="k") {
207 a = dictionaryReader.readElementText();
212 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
214 QString temp(dictionaryReader.text().toString());
215 temp.replace("\n","");
217 while(dictionaryReader.name()!="ar"&&
218 !dictionaryReader.atEnd()){
219 dictionaryReader.readNext();
220 temp+=dictionaryReader.text().toString();
223 resultString+=temp.replace("\n","")+"\n";
227 this->thread()->yieldCurrentThread();
230 dictionaryFile.close();
234 void XdxfPlugin::stop() {
238 DictDialog* XdxfPlugin::dictDialog() {
242 void XdxfPlugin::setPath(QString path){
244 _settings->setValue("path",path);
245 //getDictionaryInfo();
249 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
250 XdxfPlugin *plugin = new XdxfPlugin();
252 plugin->setPath(settings->value("path"));
254 QStringList list = settings->keys();
255 foreach(QString key, list)
256 plugin->settings()->setValue(key, settings->value(key));
259 plugin->db_name = plugin->_settings->value("type")
260 + plugin->_settings->value("path");
261 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
263 if(settings->value("cached").isEmpty() &&
264 settings->value("generateCache") == "true") {
265 plugin->makeCache("");
269 plugin->getDictionaryInfo();
273 bool XdxfPlugin::isAvailable() const {
277 void XdxfPlugin::setHash(uint _hash) {
281 uint XdxfPlugin::hash() const {
285 Settings* XdxfPlugin::settings() {
289 bool XdxfPlugin::isCached() {
290 if(_settings->value("cached") == "true")
295 void XdxfPlugin::setSettings(Settings *settings) {
297 QString oldPath = _settings->value("path");
298 if(oldPath != settings->value("path")) {
299 setPath(settings->value("path"));
302 if((_settings->value("cached") == "false" ||
303 _settings->value("cached").isEmpty()) &&
304 settings->value("generateCache") == "true") {
308 _settings->setValue("cached", "false");
311 emit settingsChanged();
315 void XdxfPlugin::getDictionaryInfo() {
316 QFile dictionaryFile(path);
317 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
318 qDebug()<<"Error: could not open file";
322 QXmlStreamReader dictionaryReader(&dictionaryFile);
323 dictionaryReader.readNextStartElement();
324 if(dictionaryReader.name()=="xdxf") {
325 if(dictionaryReader.attributes().hasAttribute("lang_from"))
326 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
327 if(dictionaryReader.attributes().hasAttribute("lang_to"))
328 _langTo = dictionaryReader.attributes().value("lang_to").toString();
330 dictionaryReader.readNextStartElement();
331 if(dictionaryReader.name()=="full_name")
332 _name=dictionaryReader.readElementText();
333 dictionaryReader.readNextStartElement();
334 if(dictionaryReader.name()=="description")
335 _infoNote=dictionaryReader.readElementText();
337 dictionaryFile.close();
340 QString XdxfPlugin::removeAccents(QString string) {
341 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
342 QString normalized = string.normalized(QString::NormalizationForm_D);
343 normalized = normalized;
344 for(int i=0; i<normalized.size(); i++) {
345 if( !normalized[i].isLetterOrNumber() &&
346 !normalized[i].isSpace() &&
347 !normalized[i].isDigit() &&
348 normalized[i] != '*' &&
349 normalized[i] != '%' &&
350 normalized[i] != '_' &&
351 normalized[i] != '?' ) {
352 normalized.remove(i,1);
358 QIcon* XdxfPlugin::icon() {
362 int XdxfPlugin::countWords() {
366 QFile dictionaryFile(path);
367 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
368 qDebug()<<"Error: could not open file";
372 dictionaryFile.seek(0);
377 while(!dictionaryFile.atEnd()) {
378 line = dictionaryFile.readLine();
379 if(line.contains("<k>")) {
383 _wordsCount = wordsCount;
384 dictionaryFile.close();
390 bool XdxfPlugin::makeCache(QString dir) {
391 cachingDialog->setVisible(true);
392 QCoreApplication::processEvents();
394 QFileInfo dictFileN(_settings->value("path"));
396 cachePathN = QDir::homePath() + "/.mdictionary/"
397 + dictFileN.completeBaseName() + ".cache";
399 QFile dictionaryFile(dictFileN.filePath());
402 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
406 QXmlStreamReader reader(&dictionaryFile);
409 db.setDatabaseName(cachePathN);
411 qDebug() << "Database error" << endl;
414 QCoreApplication::processEvents();
416 cur.exec("PRAGMA synchronous = 0");
417 cur.exec("drop table dict");
418 QCoreApplication::processEvents();
419 cur.exec("create table dict(word text ,translation text)");
433 while (!reader.atEnd() && !stopped) {
435 QCoreApplication::processEvents();
439 if(reader.tokenType() == QXmlStreamReader::StartElement) {
440 if(reader.name()=="k"){
441 a = reader.readElementText();
445 else if(reader.tokenType() == QXmlStreamReader::Characters) {
447 QString temp(reader.text().toString());
448 temp.replace("\n","");
450 while(reader.name()!="ar"&&
453 temp+=reader.text().toString();
457 cur.prepare("insert into dict values(?,?)");
459 cur.addBindValue(temp);
462 int prog = counter*100/_wordsCount;
463 if(prog % 5 == 0 && lastProg != prog) {
464 Q_EMIT updateCachingProgress(prog,
474 cur.exec("select count(*) from dict");
477 cachingDialog->setVisible(false);
479 if(!cur.next() || countWords() != cur.value(0).toInt())
481 _settings->setValue("cache_path", cachePathN);
482 _settings->setValue("cached", "true");
488 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)