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 if(word.indexOf("*")==-1)
90 word = word.replace("*", "%");
92 word = removeAccents(word);
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->db_name = plugin->_settings->value("type")
228 + plugin->_settings->value("path");
229 plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
230 plugin->makeCache("");
235 bool XdxfPlugin::isAvailable() const {
239 void XdxfPlugin::setHash(uint _hash)
244 uint XdxfPlugin::hash() const
249 Settings* XdxfPlugin::settings() {
253 bool XdxfPlugin::isCached()
258 void XdxfPlugin::setSettings(Settings *settings) {
259 _settings = settings;
260 setPath(_settings->value("path"));
261 emit settingsChanged();
265 void XdxfPlugin::getDictionaryInfo() {
266 QFile dictionaryFile(path);
267 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
268 qDebug()<<"Error: could not open file";
272 QXmlStreamReader dictionaryReader(&dictionaryFile);
273 dictionaryReader.readNextStartElement();
274 if(dictionaryReader.name()=="xdxf") {
275 if(dictionaryReader.attributes().hasAttribute("lang_from"))
276 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
277 if(dictionaryReader.attributes().hasAttribute("lang_to"))
278 _langTo = dictionaryReader.attributes().value("lang_to").toString();
280 dictionaryReader.readNextStartElement();
281 if(dictionaryReader.name()=="full_name")
282 _name=dictionaryReader.readElementText();
283 dictionaryReader.readNextStartElement();
284 if(dictionaryReader.name()=="description")
285 _infoNote=dictionaryReader.readElementText();
287 dictionaryFile.close();
290 QString XdxfPlugin::removeAccents(QString string) {
292 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
293 QString normalized = string.normalized(QString::NormalizationForm_D);
294 normalized = normalized;
295 for(int i=0; i<normalized.size(); i++) {
296 if( !normalized[i].isLetterOrNumber() &&
297 !normalized[i].isSpace() &&
298 !normalized[i].isDigit() &&
299 normalized[i] != '*' &&
300 normalized[i] != '%') {
301 normalized.remove(i,1);
307 QIcon* XdxfPlugin::icon() {
311 int XdxfPlugin::countWords() {
315 QFile dictionaryFile(path);
316 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
317 qDebug()<<"Error: could not open file";
321 dictionaryFile.seek(0);
326 while(!dictionaryFile.atEnd()) {
327 line = dictionaryFile.readLine();
328 if(line.contains("<k>")) {
332 _wordsCount = wordsCount;
333 dictionaryFile.close();
339 bool XdxfPlugin::makeCache(QString dir) {
340 QFileInfo dictFileN(_settings->value("path"));
342 cachePathN = QDir::homePath() + "/.mdictionary/"
343 + dictFileN.completeBaseName() + ".cache";
345 QFile dictionaryFile(dictFileN.filePath());
348 qDebug() << dictFileN.path();
349 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
354 QXmlStreamReader reader(&dictionaryFile);
357 db.setDatabaseName(cachePathN);
359 qDebug() << "Database error" << endl;
363 cur.exec("PRAGMA synchronous = 0");
364 cur.exec("drop table dict");
365 cur.exec("create table dict(word text ,transl text)");
377 while (!reader.atEnd()) {
381 if(reader.tokenType() == QXmlStreamReader::StartElement) {
382 if(reader.name()=="k"){
383 a = reader.readElementText();
387 else if(reader.tokenType() == QXmlStreamReader::Characters) {
389 QString temp(reader.text().toString());
390 temp.replace("\n","");
392 while(reader.name()!="ar"&&
395 temp+=reader.text().toString();
399 cur.prepare("insert into dict values(?,?)");
401 cur.addBindValue(temp);
404 int prog = counter*100/_wordsCount;
414 cur.exec("select count(*) from dict");
415 if(!cur.next() || countWords() != cur.value(0).toInt())
417 _settings->setValue("cache_path", cachePathN);
418 _settings->setValue("cached", "true");
423 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)