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 qDebug() << "search cache";
73 QSet<Translation*> translations;
74 QString cacheFilePath = _settings->value("cache_path");
75 db.setDatabaseName(cacheFilePath);
77 qDebug() << "Database error" << db.lastError().text() << endl;
78 return searchWordListFile(word, limit);
82 if(word.indexOf("*")==-1)
85 word = word.replace("*", "%");
87 word = removeAccents(word);
91 cur.prepare("select word from dict where word like ? limit ?");
92 cur.addBindValue(word);
93 cur.addBindValue(limit);
96 translations.insert(new TranslationXdxf(cur.value(0).toString(),
98 return translations.toList();
103 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
104 qDebug() << "search file";
105 QSet<Translation*> translations;
106 QFile dictionaryFile(path);
108 word = removeAccents(word);
111 if(word.indexOf("*")==-1)
113 QRegExp regWord(word);
114 regWord.setCaseSensitivity(Qt::CaseInsensitive);
115 regWord.setPatternSyntax(QRegExp::Wildcard);
116 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
117 qDebug()<<"Error: could not open file";
118 return translations.toList();
121 QXmlStreamReader dictionaryReader(&dictionaryFile);
122 /*search words list*/
125 while(!dictionaryReader.atEnd() && !stopped){
126 dictionaryReader.readNextStartElement();
127 if(dictionaryReader.name()=="ar"){
128 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
129 dictionaryReader.readNextStartElement();
130 if(!dictionaryReader.atEnd())
131 a = dictionaryReader.readElementText();
132 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
135 foreach(tran,translations)
138 ok=false; /*if key word is in the dictionary more that one */
140 if(ok) /*add key word to list*/
141 translations<<(new TranslationXdxf(a,_infoNote,this));
143 if(i>=limit && limit!=0)
147 this->thread()->yieldCurrentThread();
150 dictionaryFile.close();
151 return translations.toList();
154 QString XdxfPlugin::search(QString key) {
155 if(_settings->value("cached") == "true")
156 return searchCache(key);
157 return searchFile(key);
162 QString XdxfPlugin::searchCache(QString key) {
163 qDebug() << "search cache";
165 QString cacheFilePath = _settings->value("cache_path");
166 db.setDatabaseName(cacheFilePath);
169 qDebug() << "Database error" << db.lastError().text() << endl;
170 return searchFile(key);
174 cur.prepare("select translation from dict where word like ? limit 1");
175 cur.addBindValue(key);
178 result = cur.value(0).toString();
186 QString XdxfPlugin::searchFile(QString key) {
187 QFile dictionaryFile(path);
188 QString resultString("");
189 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
190 qDebug()<<"Error: could not open file";
193 QXmlStreamReader dictionaryReader(&dictionaryFile);
200 while (!dictionaryReader.atEnd()&& !stopped) {
201 dictionaryReader.readNext();
202 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
203 if(dictionaryReader.name()=="k") {
204 a = dictionaryReader.readElementText();
209 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
211 QString temp(dictionaryReader.text().toString());
212 temp.replace("\n","");
214 while(dictionaryReader.name()!="ar"&&
215 !dictionaryReader.atEnd()){
216 dictionaryReader.readNext();
217 temp+=dictionaryReader.text().toString();
220 resultString+=temp.replace("\n","")+"\n";
224 this->thread()->yieldCurrentThread();
227 dictionaryFile.close();
231 void XdxfPlugin::stop() {
235 DictDialog* XdxfPlugin::dictDialog() {
239 void XdxfPlugin::setPath(QString path){
241 _settings->setValue("path",path);
246 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
247 XdxfPlugin *plugin = new XdxfPlugin();
249 plugin->setPath(settings->value("path"));
250 QStringList list = settings->keys();
251 foreach(QString key, list)
252 plugin->settings()->setValue(key, settings->value(key));
254 plugin->db_name = plugin->_settings->value("type")
255 + plugin->_settings->value("path");
256 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
261 bool XdxfPlugin::isAvailable() const {
265 void XdxfPlugin::setHash(uint _hash)
270 uint XdxfPlugin::hash() const
275 Settings* XdxfPlugin::settings() {
279 bool XdxfPlugin::isCached()
284 void XdxfPlugin::setSettings(Settings *settings) {
285 _settings = settings;
286 setPath(_settings->value("path"));
287 emit settingsChanged();
291 void XdxfPlugin::getDictionaryInfo() {
292 QFile dictionaryFile(path);
293 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
294 qDebug()<<"Error: could not open file";
298 QXmlStreamReader dictionaryReader(&dictionaryFile);
299 dictionaryReader.readNextStartElement();
300 if(dictionaryReader.name()=="xdxf") {
301 if(dictionaryReader.attributes().hasAttribute("lang_from"))
302 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
303 if(dictionaryReader.attributes().hasAttribute("lang_to"))
304 _langTo = dictionaryReader.attributes().value("lang_to").toString();
306 dictionaryReader.readNextStartElement();
307 if(dictionaryReader.name()=="full_name")
308 _name=dictionaryReader.readElementText();
309 dictionaryReader.readNextStartElement();
310 if(dictionaryReader.name()=="description")
311 _infoNote=dictionaryReader.readElementText();
313 dictionaryFile.close();
316 QString XdxfPlugin::removeAccents(QString string) {
318 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
319 QString normalized = string.normalized(QString::NormalizationForm_D);
320 normalized = normalized;
321 for(int i=0; i<normalized.size(); i++) {
322 if( !normalized[i].isLetterOrNumber() &&
323 !normalized[i].isSpace() &&
324 !normalized[i].isDigit() &&
325 normalized[i] != '*' &&
326 normalized[i] != '%') {
327 normalized.remove(i,1);
333 QIcon* XdxfPlugin::icon() {
337 int XdxfPlugin::countWords() {
341 QFile dictionaryFile(path);
342 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
343 qDebug()<<"Error: could not open file";
347 dictionaryFile.seek(0);
352 while(!dictionaryFile.atEnd()) {
353 line = dictionaryFile.readLine();
354 if(line.contains("<k>")) {
358 _wordsCount = wordsCount;
359 dictionaryFile.close();
365 bool XdxfPlugin::makeCache(QString dir) {
366 QFileInfo dictFileN(_settings->value("path"));
368 cachePathN = QDir::homePath() + "/.mdictionary/"
369 + dictFileN.completeBaseName() + ".cache";
371 QFile dictionaryFile(dictFileN.filePath());
374 qDebug() << dictFileN.path();
375 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
380 QXmlStreamReader reader(&dictionaryFile);
383 db.setDatabaseName(cachePathN);
385 qDebug() << "Database error" << endl;
389 cur.exec("PRAGMA synchronous = 0");
390 cur.exec("drop table dict");
391 cur.exec("create table dict(word text ,translation text)");
403 while (!reader.atEnd()) {
407 if(reader.tokenType() == QXmlStreamReader::StartElement) {
408 if(reader.name()=="k"){
409 a = reader.readElementText();
413 else if(reader.tokenType() == QXmlStreamReader::Characters) {
415 QString temp(reader.text().toString());
416 temp.replace("\n","");
418 while(reader.name()!="ar"&&
421 temp+=reader.text().toString();
425 cur.prepare("insert into dict values(?,?)");
427 cur.addBindValue(temp);
430 int prog = counter*100/_wordsCount;
440 cur.exec("select count(*) from dict");
441 if(!cur.next() || countWords() != cur.value(0).toInt())
443 _settings->setValue("cache_path", cachePathN);
444 _settings->setValue("cached", "true");
449 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)