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)
83 word = word.replace("*", "%");
84 word = removeAccents(word);
87 cur.prepare("select word from dict where word like ? limit ?");
88 cur.addBindValue(word);
89 cur.addBindValue(limit);
92 translations.insert(new TranslationXdxf(cur.value(0).toString(),
94 return translations.toList();
99 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
100 QSet<Translation*> translations;
101 QFile dictionaryFile(path);
103 word = removeAccents(word);
106 if(word.indexOf("*")==-1)
108 QRegExp regWord(word);
109 regWord.setCaseSensitivity(Qt::CaseInsensitive);
110 regWord.setPatternSyntax(QRegExp::Wildcard);
111 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
112 qDebug()<<"Error: could not open file";
113 return translations.toList();
116 QXmlStreamReader dictionaryReader(&dictionaryFile);
117 /*search words list*/
120 while(!dictionaryReader.atEnd() && !stopped){
121 dictionaryReader.readNextStartElement();
122 if(dictionaryReader.name()=="ar"){
123 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
124 dictionaryReader.readNextStartElement();
125 if(!dictionaryReader.atEnd())
126 a = dictionaryReader.readElementText();
127 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
130 foreach(tran,translations)
133 ok=false; /*if key word is in the dictionary more that one */
135 if(ok) /*add key word to list*/
136 translations<<(new TranslationXdxf(a,_infoNote,this));
138 if(i>=limit && limit!=0)
142 this->thread()->yieldCurrentThread();
145 dictionaryFile.close();
146 return translations.toList();
149 QString XdxfPlugin::search(QString key) {
150 if(_settings->value("cached") == "true")
151 return searchCache(key);
152 return searchFile(key);
157 QString XdxfPlugin::searchCache(QString key) {
159 QString cacheFilePath = _settings->value("cache_path");
160 db.setDatabaseName(cacheFilePath);
163 qDebug() << "Database error" << db.lastError().text() << endl;
164 return searchFile(key);
168 cur.prepare("select translation from dict where word like ? limit 1");
169 cur.addBindValue(key);
172 result = cur.value(0).toString();
180 QString XdxfPlugin::searchFile(QString key) {
181 QFile dictionaryFile(path);
182 QString resultString("");
183 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
184 qDebug()<<"Error: could not open file";
187 QXmlStreamReader dictionaryReader(&dictionaryFile);
194 while (!dictionaryReader.atEnd()&& !stopped) {
195 dictionaryReader.readNext();
196 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
197 if(dictionaryReader.name()=="k") {
198 a = dictionaryReader.readElementText();
203 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
205 QString temp(dictionaryReader.text().toString());
206 temp.replace("\n","");
208 while(dictionaryReader.name()!="ar"&&
209 !dictionaryReader.atEnd()){
210 dictionaryReader.readNext();
211 temp+=dictionaryReader.text().toString();
214 resultString+=temp.replace("\n","")+"\n";
218 this->thread()->yieldCurrentThread();
221 dictionaryFile.close();
225 void XdxfPlugin::stop() {
229 DictDialog* XdxfPlugin::dictDialog() {
233 void XdxfPlugin::setPath(QString path){
235 _settings->setValue("path",path);
240 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
241 XdxfPlugin *plugin = new XdxfPlugin();
243 plugin->setPath(settings->value("path"));
244 QStringList list = settings->keys();
245 foreach(QString key, list)
246 plugin->settings()->setValue(key, settings->value(key));
248 plugin->db_name = plugin->_settings->value("type")
249 + plugin->_settings->value("path");
250 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
255 bool XdxfPlugin::isAvailable() const {
259 void XdxfPlugin::setHash(uint _hash)
264 uint XdxfPlugin::hash() const
269 Settings* XdxfPlugin::settings() {
273 bool XdxfPlugin::isCached()
278 void XdxfPlugin::setSettings(Settings *settings) {
279 _settings = settings;
280 setPath(_settings->value("path"));
281 emit settingsChanged();
285 void XdxfPlugin::getDictionaryInfo() {
286 QFile dictionaryFile(path);
287 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
288 qDebug()<<"Error: could not open file";
292 QXmlStreamReader dictionaryReader(&dictionaryFile);
293 dictionaryReader.readNextStartElement();
294 if(dictionaryReader.name()=="xdxf") {
295 if(dictionaryReader.attributes().hasAttribute("lang_from"))
296 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
297 if(dictionaryReader.attributes().hasAttribute("lang_to"))
298 _langTo = dictionaryReader.attributes().value("lang_to").toString();
300 dictionaryReader.readNextStartElement();
301 if(dictionaryReader.name()=="full_name")
302 _name=dictionaryReader.readElementText();
303 dictionaryReader.readNextStartElement();
304 if(dictionaryReader.name()=="description")
305 _infoNote=dictionaryReader.readElementText();
307 dictionaryFile.close();
310 QString XdxfPlugin::removeAccents(QString string) {
312 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
313 QString normalized = string.normalized(QString::NormalizationForm_D);
314 normalized = normalized;
315 for(int i=0; i<normalized.size(); i++) {
316 if( !normalized[i].isLetterOrNumber() &&
317 !normalized[i].isSpace() &&
318 !normalized[i].isDigit() &&
319 normalized[i] != '*' &&
320 normalized[i] != '%') {
321 normalized.remove(i,1);
327 QIcon* XdxfPlugin::icon() {
331 int XdxfPlugin::countWords() {
335 QFile dictionaryFile(path);
336 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
337 qDebug()<<"Error: could not open file";
341 dictionaryFile.seek(0);
346 while(!dictionaryFile.atEnd()) {
347 line = dictionaryFile.readLine();
348 if(line.contains("<k>")) {
352 _wordsCount = wordsCount;
353 dictionaryFile.close();
359 bool XdxfPlugin::makeCache(QString dir) {
361 QFileInfo dictFileN(_settings->value("path"));
363 cachePathN = QDir::homePath() + "/.mdictionary/"
364 + dictFileN.completeBaseName() + ".cache";
366 QFile dictionaryFile(dictFileN.filePath());
369 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
373 QXmlStreamReader reader(&dictionaryFile);
376 db.setDatabaseName(cachePathN);
378 qDebug() << "Database error" << endl;
382 cur.exec("PRAGMA synchronous = 0");
383 cur.exec("drop table dict");
384 cur.exec("create table dict(word text ,translation text)");
396 while (!reader.atEnd() && !stopped) {
400 if(reader.tokenType() == QXmlStreamReader::StartElement) {
401 if(reader.name()=="k"){
402 a = reader.readElementText();
406 else if(reader.tokenType() == QXmlStreamReader::Characters) {
408 QString temp(reader.text().toString());
409 temp.replace("\n","");
411 while(reader.name()!="ar"&&
414 temp+=reader.text().toString();
418 cur.prepare("insert into dict values(?,?)");
420 cur.addBindValue(temp);
423 int prog = counter*100/_wordsCount;
432 cur.exec("select count(*) from dict");
433 if(!cur.next() || countWords() != cur.value(0).toInt())
435 _settings->setValue("cache_path", cachePathN);
436 _settings->setValue("cached", "true");
441 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)