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 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
81 db.setDatabaseName(cacheFilePath);
83 qDebug() << "Database error" << endl;
84 return searchWordListFile(word, limit);
88 word = removeAccents(word);
89 if(word.indexOf("*")==-1)
91 word = word.replace("*", "%");
94 cur.prepare("select word from dict where word like ? limit ?");
95 cur.addBindValue(word);
96 cur.addBindValue(limit);
99 translations.insert(new TranslationXdxf(cur.value(0).toString(),
101 return translations.toList();
106 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
107 qDebug() << "search file";
108 QSet<Translation*> translations;
109 QFile dictionaryFile(path);
111 word = removeAccents(word);
114 if(word.indexOf("*")==-1)
116 QRegExp regWord(word);
117 regWord.setCaseSensitivity(Qt::CaseInsensitive);
118 regWord.setPatternSyntax(QRegExp::Wildcard);
119 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
120 qDebug()<<"Error: could not open file";
121 return translations.toList();
124 QXmlStreamReader dictionaryReader(&dictionaryFile);
125 /*search words list*/
128 while(!dictionaryReader.atEnd() && !stopped){
129 dictionaryReader.readNextStartElement();
130 if(dictionaryReader.name()=="ar"){
131 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
132 dictionaryReader.readNextStartElement();
133 if(!dictionaryReader.atEnd())
134 a = dictionaryReader.readElementText();
135 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
138 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 QFile dictionaryFile(path);
159 QString resultString("");
160 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
161 qDebug()<<"Error: could not open file";
164 QXmlStreamReader dictionaryReader(&dictionaryFile);
171 while (!dictionaryReader.atEnd()&& !stopped) {
172 dictionaryReader.readNext();
173 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
174 if(dictionaryReader.name()=="k") {
175 a = dictionaryReader.readElementText();
180 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
182 QString temp(dictionaryReader.text().toString());
183 temp.replace("\n","");
185 while(dictionaryReader.name()!="ar"&&
186 !dictionaryReader.atEnd()){
187 dictionaryReader.readNext();
188 temp+=dictionaryReader.text().toString();
191 resultString+=temp.replace("\n","")+"\n";
195 this->thread()->yieldCurrentThread();
198 dictionaryFile.close();
202 void XdxfPlugin::stop() {
206 DictDialog* XdxfPlugin::dictDialog() {
210 void XdxfPlugin::setPath(QString path){
212 _settings->setValue("path",path);
217 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
218 XdxfPlugin *plugin = new XdxfPlugin();
220 plugin->setPath(settings->value("path"));
221 QStringList list = settings->keys();
222 foreach(QString key, list)
223 plugin->settings()->setValue(key, settings->value(key));
224 if(plugin->settings()->value("cached") != "true")
225 plugin->makeCache("");
230 bool XdxfPlugin::isAvailable() const {
234 void XdxfPlugin::setHash(uint _hash)
239 uint XdxfPlugin::hash() const
244 Settings* XdxfPlugin::settings() {
248 bool XdxfPlugin::isCached()
253 void XdxfPlugin::setSettings(Settings *settings) {
254 _settings = settings;
255 setPath(_settings->value("path"));
256 emit settingsChanged();
260 void XdxfPlugin::getDictionaryInfo() {
261 QFile dictionaryFile(path);
262 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
263 qDebug()<<"Error: could not open file";
267 QXmlStreamReader dictionaryReader(&dictionaryFile);
268 dictionaryReader.readNextStartElement();
269 if(dictionaryReader.name()=="xdxf") {
270 if(dictionaryReader.attributes().hasAttribute("lang_from"))
271 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
272 if(dictionaryReader.attributes().hasAttribute("lang_to"))
273 _langTo = dictionaryReader.attributes().value("lang_to").toString();
275 dictionaryReader.readNextStartElement();
276 if(dictionaryReader.name()=="full_name")
277 _name=dictionaryReader.readElementText();
278 dictionaryReader.readNextStartElement();
279 if(dictionaryReader.name()=="description")
280 _infoNote=dictionaryReader.readElementText();
282 dictionaryFile.close();
285 QString XdxfPlugin::removeAccents(QString string) {
287 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
288 QString normalized = string.normalized(QString::NormalizationForm_D);
289 normalized = normalized;
290 for(int i=0; i<normalized.size(); i++) {
291 if( !normalized[i].isLetterOrNumber() &&
292 !normalized[i].isSpace() &&
293 !normalized[i].isDigit()) {
294 normalized.remove(i,1);
300 QIcon* XdxfPlugin::icon() {
304 int XdxfPlugin::countWords() {
308 QFile dictionaryFile(path);
309 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
310 qDebug()<<"Error: could not open file";
314 dictionaryFile.seek(0);
319 while(!dictionaryFile.atEnd()) {
320 line = dictionaryFile.readLine();
321 if(line.contains("<k>")) {
325 _wordsCount = wordsCount;
326 dictionaryFile.close();
332 bool XdxfPlugin::makeCache(QString dir) {
333 QFileInfo dictFileN(_settings->value("path"));
335 cachePathN = dictFileN.dir().absolutePath() + "/"
336 + dictFileN.completeBaseName() + ".cache";
338 QFile dictionaryFile(dictFileN.filePath());
341 qDebug() << dictFileN.path();
342 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
347 QXmlStreamReader reader(&dictionaryFile);
349 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
351 db.setDatabaseName(cachePathN);
353 qDebug() << "Database error" << endl;
357 cur.exec("PRAGMA synchronous = 0");
358 cur.exec("drop table dict");
359 cur.exec("create table dict(word text ,transl text)");
371 while (!reader.atEnd()) {
375 if(reader.tokenType() == QXmlStreamReader::StartElement) {
376 if(reader.name()=="k"){
377 a = reader.readElementText();
381 else if(reader.tokenType() == QXmlStreamReader::Characters) {
383 QString temp(reader.text().toString());
384 temp.replace("\n","");
386 while(reader.name()!="ar"&&
389 temp+=reader.text().toString();
393 cur.prepare("insert into dict values(?,?)");
395 cur.addBindValue(temp);
398 int prog = counter*100/_wordsCount;
408 cur.exec("select count(*) from dict");
409 if(!cur.next() || countWords() != cur.value(0).toInt()) {
410 qDebug() << countWords() << " " << cur.value(0).toInt();
411 qDebug() << "ŻLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
414 _settings->setValue("cache_path", cachePathN);
415 _settings->setValue("cached", "true");
420 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)