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 _settings->setValue("type","xdxf");
40 _icon = QIcon(":/icons/xdxf.png");
43 QString XdxfPlugin::langFrom() const {
47 QString XdxfPlugin::langTo() const {
51 QString XdxfPlugin::name() const {
55 QString XdxfPlugin::type() const {
56 // return _settings->value("type");
60 QString XdxfPlugin::infoNote() const {
64 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
65 if(_settings->value("cached") == "true")
66 return searchWordListCache(word,limit);
67 return searchWordListFile(word, limit);
70 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
72 QSet<Translation*> translations;
73 QString cacheFilePath = _settings->value("cache_path");
74 db.setDatabaseName(cacheFilePath);
76 qDebug() << "Database error" << db.lastError().text() << endl;
77 return searchWordListFile(word, limit);
81 if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
83 word = word.replace("*", "%");
84 word = word.replace("?", "_");
85 word = removeAccents(word);
89 cur.prepare("select word from dict where word like ? limit ?");
90 cur.addBindValue(word);
91 cur.addBindValue(limit);
94 translations.insert(new TranslationXdxf(cur.value(0).toString(),
96 return translations.toList();
101 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
102 QSet<Translation*> translations;
103 QFile dictionaryFile(path);
105 word = removeAccents(word);
108 if(word.indexOf("*")==-1)
110 QRegExp regWord(word);
111 regWord.setCaseSensitivity(Qt::CaseInsensitive);
112 regWord.setPatternSyntax(QRegExp::Wildcard);
113 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
114 qDebug()<<"Error: could not open file";
115 return translations.toList();
118 QXmlStreamReader dictionaryReader(&dictionaryFile);
119 /*search words list*/
122 while(!dictionaryReader.atEnd() && !stopped){
123 dictionaryReader.readNextStartElement();
124 if(dictionaryReader.name()=="ar"){
125 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
126 dictionaryReader.readNextStartElement();
127 if(!dictionaryReader.atEnd())
128 a = dictionaryReader.readElementText();
129 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
132 foreach(tran,translations)
135 ok=false; /*if key word is in the dictionary more that one */
137 if(ok) /*add key word to list*/
138 translations<<(new TranslationXdxf(a,_infoNote,this));
140 if(i>=limit && limit!=0)
144 this->thread()->yieldCurrentThread();
147 dictionaryFile.close();
148 return translations.toList();
151 QString XdxfPlugin::search(QString key) {
152 if(_settings->value("cached") == "true")
153 return searchCache(key);
154 return searchFile(key);
159 QString XdxfPlugin::searchCache(QString key) {
161 QString cacheFilePath = _settings->value("cache_path");
162 db.setDatabaseName(cacheFilePath);
165 qDebug() << "Database error" << db.lastError().text() << endl;
166 return searchFile(key);
170 cur.prepare("select translation from dict where word like ? limit 1");
171 cur.addBindValue(key);
174 result = cur.value(0).toString();
182 QString XdxfPlugin::searchFile(QString key) {
183 QFile dictionaryFile(path);
184 QString resultString("");
185 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
186 qDebug()<<"Error: could not open file";
189 QXmlStreamReader dictionaryReader(&dictionaryFile);
196 while (!dictionaryReader.atEnd()&& !stopped) {
197 dictionaryReader.readNext();
198 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
199 if(dictionaryReader.name()=="k") {
200 a = dictionaryReader.readElementText();
205 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
207 QString temp(dictionaryReader.text().toString());
208 temp.replace("\n","");
210 while(dictionaryReader.name()!="ar"&&
211 !dictionaryReader.atEnd()){
212 dictionaryReader.readNext();
213 temp+=dictionaryReader.text().toString();
216 resultString+=temp.replace("\n","")+"\n";
220 this->thread()->yieldCurrentThread();
223 dictionaryFile.close();
227 void XdxfPlugin::stop() {
231 DictDialog* XdxfPlugin::dictDialog() {
235 void XdxfPlugin::setPath(QString path){
237 _settings->setValue("path",path);
242 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
243 XdxfPlugin *plugin = new XdxfPlugin();
245 plugin->setPath(settings->value("path"));
246 QStringList list = settings->keys();
247 foreach(QString key, list)
248 plugin->settings()->setValue(key, settings->value(key));
250 plugin->db_name = plugin->_settings->value("type")
251 + plugin->_settings->value("path");
252 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
257 bool XdxfPlugin::isAvailable() const {
261 void XdxfPlugin::setHash(uint _hash)
266 uint XdxfPlugin::hash() const
271 Settings* XdxfPlugin::settings() {
275 bool XdxfPlugin::isCached()
280 void XdxfPlugin::setSettings(Settings *settings) {
281 _settings = settings;
282 setPath(_settings->value("path"));
283 emit settingsChanged();
287 void XdxfPlugin::getDictionaryInfo() {
288 QFile dictionaryFile(path);
289 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
290 qDebug()<<"Error: could not open file";
294 QXmlStreamReader dictionaryReader(&dictionaryFile);
295 dictionaryReader.readNextStartElement();
296 if(dictionaryReader.name()=="xdxf") {
297 if(dictionaryReader.attributes().hasAttribute("lang_from"))
298 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
299 if(dictionaryReader.attributes().hasAttribute("lang_to"))
300 _langTo = dictionaryReader.attributes().value("lang_to").toString();
302 dictionaryReader.readNextStartElement();
303 if(dictionaryReader.name()=="full_name")
304 _name=dictionaryReader.readElementText();
305 dictionaryReader.readNextStartElement();
306 if(dictionaryReader.name()=="description")
307 _infoNote=dictionaryReader.readElementText();
309 dictionaryFile.close();
312 QString XdxfPlugin::removeAccents(QString string) {
314 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
315 QString normalized = string.normalized(QString::NormalizationForm_D);
316 normalized = normalized;
317 for(int i=0; i<normalized.size(); i++) {
318 if( !normalized[i].isLetterOrNumber() &&
319 !normalized[i].isSpace() &&
320 !normalized[i].isDigit() &&
321 normalized[i] != '*' &&
322 normalized[i] != '%') {
323 normalized.remove(i,1);
329 QIcon* XdxfPlugin::icon() {
333 int XdxfPlugin::countWords() {
337 QFile dictionaryFile(path);
338 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
339 qDebug()<<"Error: could not open file";
343 dictionaryFile.seek(0);
348 while(!dictionaryFile.atEnd()) {
349 line = dictionaryFile.readLine();
350 if(line.contains("<k>")) {
354 _wordsCount = wordsCount;
355 dictionaryFile.close();
361 bool XdxfPlugin::makeCache(QString dir) {
363 QFileInfo dictFileN(_settings->value("path"));
365 cachePathN = QDir::homePath() + "/.mdictionary/"
366 + dictFileN.completeBaseName() + ".cache";
368 QFile dictionaryFile(dictFileN.filePath());
371 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
375 QXmlStreamReader reader(&dictionaryFile);
378 db.setDatabaseName(cachePathN);
380 qDebug() << "Database error" << endl;
384 cur.exec("PRAGMA synchronous = 0");
385 cur.exec("drop table dict");
386 cur.exec("create table dict(word text ,translation text)");
398 while (!reader.atEnd() && !stopped) {
402 if(reader.tokenType() == QXmlStreamReader::StartElement) {
403 if(reader.name()=="k"){
404 a = reader.readElementText();
408 else if(reader.tokenType() == QXmlStreamReader::Characters) {
410 QString temp(reader.text().toString());
411 temp.replace("\n","");
413 while(reader.name()!="ar"&&
416 temp+=reader.text().toString();
420 cur.prepare("insert into dict values(?,?)");
422 cur.addBindValue(temp);
425 int prog = counter*100/_wordsCount;
434 cur.exec("select count(*) from dict");
435 if(!cur.next() || countWords() != cur.value(0).toInt())
437 _settings->setValue("cache_path", cachePathN);
438 _settings->setValue("cached", "true");
443 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)