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) {
84 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
85 QSet<Translation*> translations;
86 QFile dictionaryFile(path);
88 word = removeAccents(word);
91 if(word.indexOf("*")==-1)
93 QRegExp regWord(word);
94 regWord.setCaseSensitivity(Qt::CaseInsensitive);
95 regWord.setPatternSyntax(QRegExp::Wildcard);
96 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
97 qDebug()<<"Error: could not open file";
98 return translations.toList();
101 QXmlStreamReader dictionaryReader(&dictionaryFile);
102 /*search words list*/
105 while(!dictionaryReader.atEnd() && !stopped){
106 dictionaryReader.readNextStartElement();
107 if(dictionaryReader.name()=="ar"){
108 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
109 dictionaryReader.readNextStartElement();
110 if(!dictionaryReader.atEnd())
111 a = dictionaryReader.readElementText();
112 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
115 foreach(tran,translations)
118 ok=false; /*if key word is in the dictionary more that one */
120 if(ok) /*add key word to list*/
121 translations<<(new TranslationXdxf(a,_infoNote,this));
123 if(i>=limit && limit!=0)
127 this->thread()->yieldCurrentThread();
130 dictionaryFile.close();
131 return translations.toList();
134 QString XdxfPlugin::search(QString key) {
135 QFile dictionaryFile(path);
136 QString resultString("");
137 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
138 qDebug()<<"Error: could not open file";
141 QXmlStreamReader dictionaryReader(&dictionaryFile);
148 while (!dictionaryReader.atEnd()&& !stopped) {
149 dictionaryReader.readNext();
150 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
151 if(dictionaryReader.name()=="k") {
152 a = dictionaryReader.readElementText();
157 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
159 QString temp(dictionaryReader.text().toString());
160 temp.replace("\n","");
162 while(dictionaryReader.name()!="ar"&&
163 !dictionaryReader.atEnd()){
164 dictionaryReader.readNext();
165 temp+=dictionaryReader.text().toString();
168 resultString+=temp.replace("\n","")+"\n";
172 this->thread()->yieldCurrentThread();
175 dictionaryFile.close();
179 void XdxfPlugin::stop() {
183 DictDialog* XdxfPlugin::dictDialog() {
187 void XdxfPlugin::setPath(QString path){
189 _settings->setValue("path",path);
194 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
195 XdxfPlugin *plugin = new XdxfPlugin();
197 plugin->setPath(settings->value("path"));
198 QStringList list = settings->keys();
199 foreach(QString key, list)
200 plugin->settings()->setValue(key, settings->value(key));
201 plugin->makeCache("");
206 bool XdxfPlugin::isAvailable() const {
210 void XdxfPlugin::setHash(uint _hash)
215 uint XdxfPlugin::hash() const
220 Settings* XdxfPlugin::settings() {
224 bool XdxfPlugin::isCached()
229 void XdxfPlugin::setSettings(Settings *settings) {
230 _settings = settings;
231 setPath(_settings->value("path"));
232 emit settingsChanged();
236 void XdxfPlugin::getDictionaryInfo() {
237 QFile dictionaryFile(path);
238 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
239 qDebug()<<"Error: could not open file";
243 QXmlStreamReader dictionaryReader(&dictionaryFile);
244 dictionaryReader.readNextStartElement();
245 if(dictionaryReader.name()=="xdxf") {
246 if(dictionaryReader.attributes().hasAttribute("lang_from"))
247 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
248 if(dictionaryReader.attributes().hasAttribute("lang_to"))
249 _langTo = dictionaryReader.attributes().value("lang_to").toString();
251 dictionaryReader.readNextStartElement();
252 if(dictionaryReader.name()=="full_name")
253 _name=dictionaryReader.readElementText();
254 dictionaryReader.readNextStartElement();
255 if(dictionaryReader.name()=="description")
256 _infoNote=dictionaryReader.readElementText();
258 dictionaryFile.close();
261 QString XdxfPlugin::removeAccents(QString string) {
263 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
264 QString normalized = string.normalized(QString::NormalizationForm_D);
265 normalized = normalized;
266 for(int i=0; i<normalized.size(); i++) {
267 if( !normalized[i].isLetterOrNumber() &&
268 !normalized[i].isSpace() &&
269 !normalized[i].isDigit()) {
270 normalized.remove(i,1);
276 QIcon* XdxfPlugin::icon() {
280 int XdxfPlugin::countWords() {
284 QFile dictionaryFile(path);
285 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
286 qDebug()<<"Error: could not open file";
290 dictionaryFile.seek(0);
295 while(!dictionaryFile.atEnd()) {
296 line = dictionaryFile.readLine();
297 if(line.contains("<k>")) {
301 _wordsCount = wordsCount;
302 dictionaryFile.close();
308 bool XdxfPlugin::makeCache(QString dir) {
309 cachingDialog->setVisible(true);
310 QFileInfo dictFileN(_settings->value("path"));
312 cachePathN = dictFileN.dir().absolutePath() + "/"
313 + dictFileN.completeBaseName() + ".cache";
315 QFile dictionaryFile(dictFileN.filePath());
318 qDebug() << dictFileN.path();
319 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
324 QXmlStreamReader reader(&dictionaryFile);
326 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
328 db.setDatabaseName(cachePathN);
330 qDebug() << "Database error" << endl;
334 cur.exec("PRAGMA synchronous = 0");
335 cur.exec("drop table dict");
336 cur.exec("create table dict(word text ,transl text)");
348 while (!reader.atEnd()) {
350 QCoreApplication::processEvents();
354 if(reader.tokenType() == QXmlStreamReader::StartElement) {
355 if(reader.name()=="k"){
356 a = reader.readElementText();
360 else if(reader.tokenType() == QXmlStreamReader::Characters) {
362 QString temp(reader.text().toString());
363 temp.replace("\n","");
365 while(reader.name()!="ar"&&
368 temp+=reader.text().toString();
372 cur.prepare("insert into dict values(?,?)");
374 cur.addBindValue(temp);
377 int prog = counter*100/_wordsCount;
379 Q_EMIT updateCachingProgress(prog);
385 cachingDialog->setVisible(false);
389 cur.exec("select count(*) from dict");
390 if(!cur.next() || countWords() != cur.value(0).toInt()) {
391 qDebug() << countWords() << " " << cur.value(0).toInt();
392 qDebug() << "ŻLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
399 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)