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 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
83 db.setDatabaseName(cacheFilePath);
85 qDebug() << "Database error" << endl;
86 return searchWordListFile(word, limit);
90 word = removeAccents(word);
91 if(word.indexOf("*")==-1)
93 word = word.replace("*", "%");
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 QFile dictionaryFile(path);
161 QString resultString("");
162 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
163 qDebug()<<"Error: could not open file";
166 QXmlStreamReader dictionaryReader(&dictionaryFile);
173 while (!dictionaryReader.atEnd()&& !stopped) {
174 dictionaryReader.readNext();
175 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
176 if(dictionaryReader.name()=="k") {
177 a = dictionaryReader.readElementText();
182 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
184 QString temp(dictionaryReader.text().toString());
185 temp.replace("\n","");
187 while(dictionaryReader.name()!="ar"&&
188 !dictionaryReader.atEnd()){
189 dictionaryReader.readNext();
190 temp+=dictionaryReader.text().toString();
193 resultString+=temp.replace("\n","")+"\n";
197 this->thread()->yieldCurrentThread();
200 dictionaryFile.close();
204 void XdxfPlugin::stop() {
208 DictDialog* XdxfPlugin::dictDialog() {
212 void XdxfPlugin::setPath(QString path){
214 _settings->setValue("path",path);
219 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
220 XdxfPlugin *plugin = new XdxfPlugin();
222 plugin->setPath(settings->value("path"));
223 QStringList list = settings->keys();
224 foreach(QString key, list)
225 plugin->settings()->setValue(key, settings->value(key));
226 if(plugin->settings()->value("cached") != "true")
227 plugin->makeCache("");
232 bool XdxfPlugin::isAvailable() const {
236 void XdxfPlugin::setHash(uint _hash)
241 uint XdxfPlugin::hash() const
246 Settings* XdxfPlugin::settings() {
250 bool XdxfPlugin::isCached()
255 void XdxfPlugin::setSettings(Settings *settings) {
256 _settings = settings;
257 setPath(_settings->value("path"));
258 emit settingsChanged();
262 void XdxfPlugin::getDictionaryInfo() {
263 QFile dictionaryFile(path);
264 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
265 qDebug()<<"Error: could not open file";
269 QXmlStreamReader dictionaryReader(&dictionaryFile);
270 dictionaryReader.readNextStartElement();
271 if(dictionaryReader.name()=="xdxf") {
272 if(dictionaryReader.attributes().hasAttribute("lang_from"))
273 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
274 if(dictionaryReader.attributes().hasAttribute("lang_to"))
275 _langTo = dictionaryReader.attributes().value("lang_to").toString();
277 dictionaryReader.readNextStartElement();
278 if(dictionaryReader.name()=="full_name")
279 _name=dictionaryReader.readElementText();
280 dictionaryReader.readNextStartElement();
281 if(dictionaryReader.name()=="description")
282 _infoNote=dictionaryReader.readElementText();
284 dictionaryFile.close();
287 QString XdxfPlugin::removeAccents(QString string) {
289 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
290 QString normalized = string.normalized(QString::NormalizationForm_D);
291 normalized = normalized;
292 for(int i=0; i<normalized.size(); i++) {
293 if( !normalized[i].isLetterOrNumber() &&
294 !normalized[i].isSpace() &&
295 !normalized[i].isDigit()) {
296 normalized.remove(i,1);
302 QIcon* XdxfPlugin::icon() {
306 int XdxfPlugin::countWords() {
310 QFile dictionaryFile(path);
311 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
312 qDebug()<<"Error: could not open file";
316 dictionaryFile.seek(0);
321 while(!dictionaryFile.atEnd()) {
322 line = dictionaryFile.readLine();
323 if(line.contains("<k>")) {
327 _wordsCount = wordsCount;
328 dictionaryFile.close();
334 bool XdxfPlugin::makeCache(QString dir) {
335 cachingDialog->setVisible(true);
336 QFileInfo dictFileN(_settings->value("path"));
338 cachePathN = dictFileN.dir().absolutePath() + "/"
339 + dictFileN.completeBaseName() + ".cache";
341 QFile dictionaryFile(dictFileN.filePath());
344 qDebug() << dictFileN.path();
345 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
350 QXmlStreamReader reader(&dictionaryFile);
352 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
354 db.setDatabaseName(cachePathN);
356 qDebug() << "Database error" << endl;
360 cur.exec("PRAGMA synchronous = 0");
361 cur.exec("drop table dict");
362 cur.exec("create table dict(word text ,transl text)");
374 while (!reader.atEnd()) {
376 QCoreApplication::processEvents();
380 if(reader.tokenType() == QXmlStreamReader::StartElement) {
381 if(reader.name()=="k"){
382 a = reader.readElementText();
386 else if(reader.tokenType() == QXmlStreamReader::Characters) {
388 QString temp(reader.text().toString());
389 temp.replace("\n","");
391 while(reader.name()!="ar"&&
394 temp+=reader.text().toString();
398 cur.prepare("insert into dict values(?,?)");
400 cur.addBindValue(temp);
403 int prog = counter*100/_wordsCount;
405 Q_EMIT updateCachingProgress(prog);
411 cachingDialog->setVisible(false);
415 cur.exec("select count(*) from dict");
416 if(!cur.next() || countWords() != cur.value(0).toInt()) {
417 qDebug() << countWords() << " " << cur.value(0).toInt();
418 qDebug() << "ŻLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
421 _settings->setValue("cache_path", cachePathN);
422 _settings->setValue("cached", "true");
427 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)