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 _settings->setValue("type","xdxf");
40 _settings->setValue("cached","true");
42 _settings->setValue("cached","false");
47 _icon = QIcon(":/icons/xdxf.png");
50 QString XdxfPlugin::langFrom() const {
54 QString XdxfPlugin::langTo() const {
58 QString XdxfPlugin::name() const {
62 QString XdxfPlugin::type() const {
63 // return _settings->value("type");
67 QString XdxfPlugin::infoNote() const {
71 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
72 if(_settings->value("cached") == "true")
73 return searchWordListCache(word,limit);
74 return searchWordListFile(word, limit);
77 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
79 qDebug() << "search cache";
80 QSet<Translation*> translations;
81 QString cacheFilePath = _settings->value("cache_path");
82 db.setDatabaseName(cacheFilePath);
84 qDebug() << "Database error" << db.lastError().text() << endl;
85 return searchWordListFile(word, limit);
89 if(word.indexOf("*")==-1)
92 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 qDebug() << "search file";
112 QSet<Translation*> translations;
113 QFile dictionaryFile(path);
115 word = removeAccents(word);
118 if(word.indexOf("*")==-1)
120 QRegExp regWord(word);
121 regWord.setCaseSensitivity(Qt::CaseInsensitive);
122 regWord.setPatternSyntax(QRegExp::Wildcard);
123 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
124 qDebug()<<"Error: could not open file";
125 return translations.toList();
128 QXmlStreamReader dictionaryReader(&dictionaryFile);
129 /*search words list*/
132 while(!dictionaryReader.atEnd() && !stopped){
133 dictionaryReader.readNextStartElement();
134 if(dictionaryReader.name()=="ar"){
135 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
136 dictionaryReader.readNextStartElement();
137 if(!dictionaryReader.atEnd())
138 a = dictionaryReader.readElementText();
139 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
142 foreach(tran,translations)
145 ok=false; /*if key word is in the dictionary more that one */
147 if(ok) /*add key word to list*/
148 translations<<(new TranslationXdxf(a,_infoNote,this));
150 if(i>=limit && limit!=0)
154 this->thread()->yieldCurrentThread();
157 dictionaryFile.close();
158 return translations.toList();
161 QString XdxfPlugin::search(QString key) {
162 if(_settings->value("cached") == "true")
163 return searchCache(key);
164 return searchFile(key);
169 QString XdxfPlugin::searchCache(QString key) {
170 qDebug() << "search cache";
172 QString cacheFilePath = _settings->value("cache_path");
173 db.setDatabaseName(cacheFilePath);
176 qDebug() << "Database error" << db.lastError().text() << endl;
177 return searchFile(key);
181 cur.prepare("select translation from dict where word like ? limit 1");
182 cur.addBindValue(key);
185 result = cur.value(0).toString();
193 QString XdxfPlugin::searchFile(QString key) {
194 QFile dictionaryFile(path);
195 QString resultString("");
196 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
197 qDebug()<<"Error: could not open file";
200 QXmlStreamReader dictionaryReader(&dictionaryFile);
207 while (!dictionaryReader.atEnd()&& !stopped) {
208 dictionaryReader.readNext();
209 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
210 if(dictionaryReader.name()=="k") {
211 a = dictionaryReader.readElementText();
216 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
218 QString temp(dictionaryReader.text().toString());
219 temp.replace("\n","");
221 while(dictionaryReader.name()!="ar"&&
222 !dictionaryReader.atEnd()){
223 dictionaryReader.readNext();
224 temp+=dictionaryReader.text().toString();
227 resultString+=temp.replace("\n","")+"\n";
231 this->thread()->yieldCurrentThread();
234 dictionaryFile.close();
238 void XdxfPlugin::stop() {
242 DictDialog* XdxfPlugin::dictDialog() {
246 void XdxfPlugin::setPath(QString path){
248 _settings->setValue("path",path);
249 //getDictionaryInfo();
253 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
254 XdxfPlugin *plugin = new XdxfPlugin();
256 plugin->setPath(settings->value("path"));
258 QStringList list = settings->keys();
259 foreach(QString key, list)
260 plugin->settings()->setValue(key, settings->value(key));
262 //if(plugin->settings()->value("cached") != "true")
263 plugin->db_name = plugin->_settings->value("type")
264 + plugin->_settings->value("path");
265 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
266 plugin->makeCache("");
267 plugin->getDictionaryInfo();
273 bool XdxfPlugin::isAvailable() const {
277 void XdxfPlugin::setHash(uint _hash)
282 uint XdxfPlugin::hash() const
287 Settings* XdxfPlugin::settings() {
291 bool XdxfPlugin::isCached()
296 void XdxfPlugin::setSettings(Settings *settings) {
298 QString oldPath = _settings->value("path");
299 if(oldPath != settings->value("path")) {
300 setPath(settings->value("path"));
303 if(_settings->value("cached") == "false" &&
304 settings->value("cached") == "true") {
308 _settings->setValue("cached", settings->value("cached"));
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) {
342 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
343 QString normalized = string.normalized(QString::NormalizationForm_D);
344 normalized = normalized;
345 for(int i=0; i<normalized.size(); i++) {
346 if( !normalized[i].isLetterOrNumber() &&
347 !normalized[i].isSpace() &&
348 !normalized[i].isDigit() &&
349 normalized[i] != '*' &&
350 normalized[i] != '%') {
351 normalized.remove(i,1);
357 QIcon* XdxfPlugin::icon() {
361 int XdxfPlugin::countWords() {
365 QFile dictionaryFile(path);
366 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
367 qDebug()<<"Error: could not open file";
371 dictionaryFile.seek(0);
376 while(!dictionaryFile.atEnd()) {
377 line = dictionaryFile.readLine();
378 if(line.contains("<k>")) {
382 _wordsCount = wordsCount;
383 dictionaryFile.close();
389 bool XdxfPlugin::makeCache(QString dir) {
390 cachingDialog->setVisible(true);
391 QCoreApplication::processEvents();
392 QFileInfo dictFileN(_settings->value("path"));
394 cachePathN = QDir::homePath() + "/.mdictionary/"
395 + dictFileN.completeBaseName() + ".cache";
397 QFile dictionaryFile(dictFileN.filePath());
400 qDebug() << dictFileN.path();
401 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)");
431 while (!reader.atEnd()) {
433 QCoreApplication::processEvents();
437 if(reader.tokenType() == QXmlStreamReader::StartElement) {
438 if(reader.name()=="k"){
439 a = reader.readElementText();
443 else if(reader.tokenType() == QXmlStreamReader::Characters) {
445 QString temp(reader.text().toString());
446 temp.replace("\n","");
448 while(reader.name()!="ar"&&
451 temp+=reader.text().toString();
455 cur.prepare("insert into dict values(?,?)");
457 cur.addBindValue(temp);
460 int prog = counter*100/_wordsCount;
462 Q_EMIT updateCachingProgress(prog);
471 cur.exec("select count(*) from dict");
474 cachingDialog->setVisible(false);
476 if(!cur.next() || countWords() != cur.value(0).toInt())
478 _settings->setValue("cache_path", cachePathN);
479 _settings->setValue("cached", "true");
485 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)