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");
38 _settings->setValue("cached","true");
40 _settings->setValue("cached","false");
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 qDebug() << "search cache";
78 QSet<Translation*> translations;
79 QString cacheFilePath = _settings->value("cache_path");
80 db.setDatabaseName(cacheFilePath);
82 qDebug() << "Database error" << db.lastError().text() << endl;
83 return searchWordListFile(word, limit);
87 if(word.indexOf("*")==-1)
90 word = word.replace("*", "%");
92 word = removeAccents(word);
96 cur.prepare("select word from dict where word like ? limit ?");
97 cur.addBindValue(word);
98 cur.addBindValue(limit);
101 translations.insert(new TranslationXdxf(cur.value(0).toString(),
103 return translations.toList();
108 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
109 qDebug() << "search file";
110 QSet<Translation*> translations;
111 QFile dictionaryFile(path);
113 word = removeAccents(word);
116 if(word.indexOf("*")==-1)
118 QRegExp regWord(word);
119 regWord.setCaseSensitivity(Qt::CaseInsensitive);
120 regWord.setPatternSyntax(QRegExp::Wildcard);
121 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
122 qDebug()<<"Error: could not open file";
123 return translations.toList();
126 QXmlStreamReader dictionaryReader(&dictionaryFile);
127 /*search words list*/
130 while(!dictionaryReader.atEnd() && !stopped){
131 dictionaryReader.readNextStartElement();
132 if(dictionaryReader.name()=="ar"){
133 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
134 dictionaryReader.readNextStartElement();
135 if(!dictionaryReader.atEnd())
136 a = dictionaryReader.readElementText();
137 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
140 foreach(tran,translations)
143 ok=false; /*if key word is in the dictionary more that one */
145 if(ok) /*add key word to list*/
146 translations<<(new TranslationXdxf(a,_infoNote,this));
148 if(i>=limit && limit!=0)
152 this->thread()->yieldCurrentThread();
155 dictionaryFile.close();
156 return translations.toList();
159 QString XdxfPlugin::search(QString key) {
160 if(_settings->value("cached") == "true")
161 return searchCache(key);
162 return searchFile(key);
167 QString XdxfPlugin::searchCache(QString key) {
168 qDebug() << "search cache";
170 QString cacheFilePath = _settings->value("cache_path");
171 db.setDatabaseName(cacheFilePath);
174 qDebug() << "Database error" << db.lastError().text() << endl;
175 return searchFile(key);
179 cur.prepare("select translation from dict where word like ? limit 1");
180 cur.addBindValue(key);
183 result = cur.value(0).toString();
191 QString XdxfPlugin::searchFile(QString key) {
192 QFile dictionaryFile(path);
193 QString resultString("");
194 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
195 qDebug()<<"Error: could not open file";
198 QXmlStreamReader dictionaryReader(&dictionaryFile);
205 while (!dictionaryReader.atEnd()&& !stopped) {
206 dictionaryReader.readNext();
207 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
208 if(dictionaryReader.name()=="k") {
209 a = dictionaryReader.readElementText();
214 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
216 QString temp(dictionaryReader.text().toString());
217 temp.replace("\n","");
219 while(dictionaryReader.name()!="ar"&&
220 !dictionaryReader.atEnd()){
221 dictionaryReader.readNext();
222 temp+=dictionaryReader.text().toString();
225 resultString+=temp.replace("\n","")+"\n";
229 this->thread()->yieldCurrentThread();
232 dictionaryFile.close();
236 void XdxfPlugin::stop() {
240 DictDialog* XdxfPlugin::dictDialog() {
244 void XdxfPlugin::setPath(QString path){
246 _settings->setValue("path",path);
251 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
252 XdxfPlugin *plugin = new XdxfPlugin();
254 plugin->setPath(settings->value("path"));
255 QStringList list = settings->keys();
256 foreach(QString key, list)
257 plugin->settings()->setValue(key, settings->value(key));
258 //if(plugin->settings()->value("cached") != "true")
259 plugin->db_name = plugin->_settings->value("type")
260 + plugin->_settings->value("path");
261 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
262 plugin->makeCache("");
267 bool XdxfPlugin::isAvailable() const {
271 void XdxfPlugin::setHash(uint _hash)
276 uint XdxfPlugin::hash() const
281 Settings* XdxfPlugin::settings() {
285 bool XdxfPlugin::isCached()
290 void XdxfPlugin::setSettings(Settings *settings) {
291 _settings = settings;
292 setPath(_settings->value("path"));
293 emit settingsChanged();
297 void XdxfPlugin::getDictionaryInfo() {
298 QFile dictionaryFile(path);
299 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
300 qDebug()<<"Error: could not open file";
304 QXmlStreamReader dictionaryReader(&dictionaryFile);
305 dictionaryReader.readNextStartElement();
306 if(dictionaryReader.name()=="xdxf") {
307 if(dictionaryReader.attributes().hasAttribute("lang_from"))
308 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
309 if(dictionaryReader.attributes().hasAttribute("lang_to"))
310 _langTo = dictionaryReader.attributes().value("lang_to").toString();
312 dictionaryReader.readNextStartElement();
313 if(dictionaryReader.name()=="full_name")
314 _name=dictionaryReader.readElementText();
315 dictionaryReader.readNextStartElement();
316 if(dictionaryReader.name()=="description")
317 _infoNote=dictionaryReader.readElementText();
319 dictionaryFile.close();
322 QString XdxfPlugin::removeAccents(QString string) {
324 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
325 QString normalized = string.normalized(QString::NormalizationForm_D);
326 normalized = normalized;
327 for(int i=0; i<normalized.size(); i++) {
328 if( !normalized[i].isLetterOrNumber() &&
329 !normalized[i].isSpace() &&
330 !normalized[i].isDigit() &&
331 normalized[i] != '*' &&
332 normalized[i] != '%') {
333 normalized.remove(i,1);
339 QIcon* XdxfPlugin::icon() {
343 int XdxfPlugin::countWords() {
347 QFile dictionaryFile(path);
348 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
349 qDebug()<<"Error: could not open file";
353 dictionaryFile.seek(0);
358 while(!dictionaryFile.atEnd()) {
359 line = dictionaryFile.readLine();
360 if(line.contains("<k>")) {
364 _wordsCount = wordsCount;
365 dictionaryFile.close();
371 bool XdxfPlugin::makeCache(QString dir) {
372 QFileInfo dictFileN(_settings->value("path"));
374 cachePathN = QDir::homePath() + "/.mdictionary/"
375 + dictFileN.completeBaseName() + ".cache";
377 QFile dictionaryFile(dictFileN.filePath());
380 qDebug() << dictFileN.path();
381 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
386 QXmlStreamReader reader(&dictionaryFile);
389 db.setDatabaseName(cachePathN);
391 qDebug() << "Database error" << endl;
395 cur.exec("PRAGMA synchronous = 0");
396 cur.exec("drop table dict");
397 cur.exec("create table dict(word text ,translation text)");
409 while (!reader.atEnd()) {
413 if(reader.tokenType() == QXmlStreamReader::StartElement) {
414 if(reader.name()=="k"){
415 a = reader.readElementText();
419 else if(reader.tokenType() == QXmlStreamReader::Characters) {
421 QString temp(reader.text().toString());
422 temp.replace("\n","");
424 while(reader.name()!="ar"&&
427 temp+=reader.text().toString();
431 cur.prepare("insert into dict values(?,?)");
433 cur.addBindValue(temp);
436 int prog = counter*100/_wordsCount;
446 cur.exec("select count(*) from dict");
447 if(!cur.next() || countWords() != cur.value(0).toInt())
449 _settings->setValue("cache_path", cachePathN);
450 _settings->setValue("cached", "true");
455 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)