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 db.setDatabaseName(cacheFilePath);
82 qDebug() << "Database error" << db.lastError().text() << endl;
83 return searchWordListFile(word, limit);
87 word = removeAccents(word);
88 if(word.indexOf("*")==-1)
90 word = word.replace("*", "%");
93 cur.prepare("select word from dict where word like ? limit ?");
94 cur.addBindValue(word);
95 cur.addBindValue(limit);
98 translations.insert(new TranslationXdxf(cur.value(0).toString(),
100 return translations.toList();
105 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
106 qDebug() << "search file";
107 QSet<Translation*> translations;
108 QFile dictionaryFile(path);
110 word = removeAccents(word);
113 if(word.indexOf("*")==-1)
115 QRegExp regWord(word);
116 regWord.setCaseSensitivity(Qt::CaseInsensitive);
117 regWord.setPatternSyntax(QRegExp::Wildcard);
118 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
119 qDebug()<<"Error: could not open file";
120 return translations.toList();
123 QXmlStreamReader dictionaryReader(&dictionaryFile);
124 /*search words list*/
127 while(!dictionaryReader.atEnd() && !stopped){
128 dictionaryReader.readNextStartElement();
129 if(dictionaryReader.name()=="ar"){
130 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
131 dictionaryReader.readNextStartElement();
132 if(!dictionaryReader.atEnd())
133 a = dictionaryReader.readElementText();
134 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
137 foreach(tran,translations)
140 ok=false; /*if key word is in the dictionary more that one */
142 if(ok) /*add key word to list*/
143 translations<<(new TranslationXdxf(a,_infoNote,this));
145 if(i>=limit && limit!=0)
149 this->thread()->yieldCurrentThread();
152 dictionaryFile.close();
153 return translations.toList();
156 QString XdxfPlugin::search(QString key) {
157 QFile dictionaryFile(path);
158 QString resultString("");
159 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
160 qDebug()<<"Error: could not open file";
163 QXmlStreamReader dictionaryReader(&dictionaryFile);
170 while (!dictionaryReader.atEnd()&& !stopped) {
171 dictionaryReader.readNext();
172 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
173 if(dictionaryReader.name()=="k") {
174 a = dictionaryReader.readElementText();
179 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
181 QString temp(dictionaryReader.text().toString());
182 temp.replace("\n","");
184 while(dictionaryReader.name()!="ar"&&
185 !dictionaryReader.atEnd()){
186 dictionaryReader.readNext();
187 temp+=dictionaryReader.text().toString();
190 resultString+=temp.replace("\n","")+"\n";
194 this->thread()->yieldCurrentThread();
197 dictionaryFile.close();
201 void XdxfPlugin::stop() {
205 DictDialog* XdxfPlugin::dictDialog() {
209 void XdxfPlugin::setPath(QString path){
211 _settings->setValue("path",path);
216 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
217 XdxfPlugin *plugin = new XdxfPlugin();
219 plugin->setPath(settings->value("path"));
220 QStringList list = settings->keys();
221 foreach(QString key, list)
222 plugin->settings()->setValue(key, settings->value(key));
223 //if(plugin->settings()->value("cached") != "true")
224 plugin->db_name = plugin->_settings->value("type")
225 + plugin->_settings->value("path");
226 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
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 QFileInfo dictFileN(_settings->value("path"));
337 cachePathN = QDir::homePath() + "/.mdictionary/"
338 + dictFileN.completeBaseName() + ".cache";
340 QFile dictionaryFile(dictFileN.filePath());
343 qDebug() << dictFileN.path();
344 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
349 QXmlStreamReader reader(&dictionaryFile);
352 db.setDatabaseName(cachePathN);
354 qDebug() << "Database error" << endl;
358 cur.exec("PRAGMA synchronous = 0");
359 cur.exec("drop table dict");
360 cur.exec("create table dict(word text ,transl text)");
372 while (!reader.atEnd()) {
376 if(reader.tokenType() == QXmlStreamReader::StartElement) {
377 if(reader.name()=="k"){
378 a = reader.readElementText();
382 else if(reader.tokenType() == QXmlStreamReader::Characters) {
384 QString temp(reader.text().toString());
385 temp.replace("\n","");
387 while(reader.name()!="ar"&&
390 temp+=reader.text().toString();
394 cur.prepare("insert into dict values(?,?)");
396 cur.addBindValue(temp);
399 int prog = counter*100/_wordsCount;
409 cur.exec("select count(*) from dict");
410 if(!cur.next() || countWords() != cur.value(0).toInt())
412 _settings->setValue("cache_path", cachePathN);
413 _settings->setValue("cached", "true");
418 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)