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) {
82 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
83 QSet<Translation*> translations;
84 QFile dictionaryFile(path);
86 word = removeAccents(word);
89 if(word.indexOf("*")==-1)
91 QRegExp regWord(word);
92 regWord.setCaseSensitivity(Qt::CaseInsensitive);
93 regWord.setPatternSyntax(QRegExp::Wildcard);
94 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
95 qDebug()<<"Error: could not open file";
96 return translations.toList();
99 QXmlStreamReader dictionaryReader(&dictionaryFile);
100 /*search words list*/
103 while(!dictionaryReader.atEnd() && !stopped){
104 dictionaryReader.readNextStartElement();
105 if(dictionaryReader.name()=="ar"){
106 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
107 dictionaryReader.readNextStartElement();
108 if(!dictionaryReader.atEnd())
109 a = dictionaryReader.readElementText();
110 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
113 foreach(tran,translations)
116 ok=false; /*if key word is in the dictionary more that one */
118 if(ok) /*add key word to list*/
119 translations<<(new TranslationXdxf(a,_infoNote,this));
121 if(i>=limit && limit!=0)
125 this->thread()->yieldCurrentThread();
128 dictionaryFile.close();
129 return translations.toList();
132 QString XdxfPlugin::search(QString key) {
133 QFile dictionaryFile(path);
134 QString resultString("");
135 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
136 qDebug()<<"Error: could not open file";
139 QXmlStreamReader dictionaryReader(&dictionaryFile);
146 while (!dictionaryReader.atEnd()&& !stopped) {
147 dictionaryReader.readNext();
148 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
149 if(dictionaryReader.name()=="k") {
150 a = dictionaryReader.readElementText();
155 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
157 QString temp(dictionaryReader.text().toString());
158 temp.replace("\n","");
160 while(dictionaryReader.name()!="ar"&&
161 !dictionaryReader.atEnd()){
162 dictionaryReader.readNext();
163 temp+=dictionaryReader.text().toString();
166 resultString+=temp.replace("\n","")+"\n";
170 this->thread()->yieldCurrentThread();
173 dictionaryFile.close();
177 void XdxfPlugin::stop() {
181 DictDialog* XdxfPlugin::dictDialog() {
185 void XdxfPlugin::setPath(QString path){
187 _settings->setValue("path",path);
192 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
193 XdxfPlugin *plugin = new XdxfPlugin();
195 plugin->setPath(settings->value("path"));
196 QStringList list = settings->keys();
197 foreach(QString key, list)
198 plugin->settings()->setValue(key, settings->value(key));
199 plugin->makeCache("");
204 bool XdxfPlugin::isAvailable() const {
208 void XdxfPlugin::setHash(uint _hash)
213 uint XdxfPlugin::hash() const
218 Settings* XdxfPlugin::settings() {
222 bool XdxfPlugin::isCached()
227 void XdxfPlugin::setSettings(Settings *settings) {
228 _settings = settings;
229 setPath(_settings->value("path"));
230 emit settingsChanged();
234 void XdxfPlugin::getDictionaryInfo() {
235 QFile dictionaryFile(path);
236 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
237 qDebug()<<"Error: could not open file";
241 QXmlStreamReader dictionaryReader(&dictionaryFile);
242 dictionaryReader.readNextStartElement();
243 if(dictionaryReader.name()=="xdxf") {
244 if(dictionaryReader.attributes().hasAttribute("lang_from"))
245 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
246 if(dictionaryReader.attributes().hasAttribute("lang_to"))
247 _langTo = dictionaryReader.attributes().value("lang_to").toString();
249 dictionaryReader.readNextStartElement();
250 if(dictionaryReader.name()=="full_name")
251 _name=dictionaryReader.readElementText();
252 dictionaryReader.readNextStartElement();
253 if(dictionaryReader.name()=="description")
254 _infoNote=dictionaryReader.readElementText();
256 dictionaryFile.close();
259 QString XdxfPlugin::removeAccents(QString string) {
261 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
262 QString normalized = string.normalized(QString::NormalizationForm_D);
263 normalized = normalized;
264 for(int i=0; i<normalized.size(); i++) {
265 if( !normalized[i].isLetterOrNumber() &&
266 !normalized[i].isSpace() &&
267 !normalized[i].isDigit()) {
268 normalized.remove(i,1);
274 QIcon* XdxfPlugin::icon() {
278 int XdxfPlugin::countWords() {
282 QFile dictionaryFile(path);
283 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
284 qDebug()<<"Error: could not open file";
288 dictionaryFile.seek(0);
293 while(!dictionaryFile.atEnd()) {
294 line = dictionaryFile.readLine();
295 if(line.contains("<k>")) {
299 _wordsCount = wordsCount;
300 dictionaryFile.close();
306 bool XdxfPlugin::makeCache(QString dir) {
307 QFileInfo dictFileN(_settings->value("path"));
309 cachePathN = dictFileN.dir().absolutePath() + "/"
310 + dictFileN.completeBaseName() + ".cache";
312 QFile dictionaryFile(dictFileN.filePath());
315 qDebug() << dictFileN.path();
316 if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
321 QXmlStreamReader reader(&dictionaryFile);
323 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
325 db.setDatabaseName(cachePathN);
327 qDebug() << "Database error" << endl;
331 cur.exec("PRAGMA synchronous = 0");
332 cur.exec("drop table dict");
333 cur.exec("create table dict(word text ,transl text)");
345 while (!reader.atEnd()) {
349 if(reader.tokenType() == QXmlStreamReader::StartElement) {
350 if(reader.name()=="k"){
351 a = reader.readElementText();
355 else if(reader.tokenType() == QXmlStreamReader::Characters) {
357 QString temp(reader.text().toString());
358 temp.replace("\n","");
360 while(reader.name()!="ar"&&
363 temp+=reader.text().toString();
367 cur.prepare("insert into dict values(?,?)");
369 cur.addBindValue(temp);
372 int prog = counter*100/_wordsCount;
382 cur.exec("select count(*) from dict");
383 if(!cur.next() || countWords() != cur.value(0).toInt()) {
384 qDebug() << countWords() << " " << cur.value(0).toInt();
385 qDebug() << "ŻLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
392 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)