Added default configuration handling
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 #include "xdxfplugin.h"
23 #include <QDebug>
24 #include <QFile>
25 #include <QXmlStreamReader>
26 #include <QtPlugin>
27 #include "TranslationXdxf.h"
28 #include "../../../includes/settings.h"
29
30 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
31                     _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
32                     _type(tr("xdxf")), _infoNote(tr("")) {
33     _settings = new Settings();
34     _dictDialog = new XdxfDictDialog(this, this);
35     _settings->setValue("type","xdxf");
36     if(isCached())
37         _settings->setValue("cached","true");
38     else
39         _settings->setValue("cached","false");
40
41     _wordsCount = 0;
42
43     stopped = false;
44
45
46 }
47
48 QString XdxfPlugin::langFrom() const {   
49     return _langFrom;
50 }
51
52 QString XdxfPlugin::langTo() const {
53     return  _langTo;
54 }
55
56 QString XdxfPlugin::name() const {
57     return  _name;
58 }
59
60 QString XdxfPlugin::type() const {
61 //    return _settings->value("type");
62     return _type;
63 }
64
65 QString XdxfPlugin::infoNote() const {
66     return  _infoNote;
67 }
68
69 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) { 
70     QSet<Translation*> translations;
71     QFile dictionaryFile(path);
72
73     word = removeAccents(word);
74     qDebug()<<word;
75
76     stopped = false;
77     if(word.indexOf("*")==-1)
78         word+="*";
79     QRegExp regWord(word);
80     regWord.setCaseSensitivity(Qt::CaseInsensitive);
81     regWord.setPatternSyntax(QRegExp::Wildcard);
82     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
83         qDebug()<<"Error: could not open file";
84         return translations.toList();
85     }
86
87     QXmlStreamReader dictionaryReader(&dictionaryFile);
88     /*search words list*/
89     QString a;
90     int i=0;
91     while(!dictionaryReader.atEnd() && !stopped){
92         dictionaryReader.readNextStartElement();
93         if(dictionaryReader.name()=="ar"){
94             while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
95                 dictionaryReader.readNextStartElement();
96             if(!dictionaryReader.atEnd())
97                 a = dictionaryReader.readElementText();
98             if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
99                 bool ok=true;
100                 Translation *tran;
101                 foreach(tran,translations)
102                 {
103                     if(tran->key()==a)
104                         ok=false;  /*if key word is in the dictionary more that one */
105                 }
106                 if(ok)  /*add key word to list*/
107                     translations<<(new TranslationXdxf(a,_infoNote,this));
108                 i++;
109                 if(i>=limit && limit!=0)
110                     break;
111             }
112         }
113         this->thread()->yieldCurrentThread();
114     }
115     stopped=false;
116     dictionaryFile.close();
117     return translations.toList();
118 }
119
120 QString XdxfPlugin::search(QString key) {
121     QFile dictionaryFile(path);
122     QString resultString("");
123     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
124         qDebug()<<"Error: could not open file";
125         return "";
126     }
127     QXmlStreamReader dictionaryReader(&dictionaryFile);
128
129
130     QString a;
131
132     bool match =false;
133     stopped = false;
134     while (!dictionaryReader.atEnd()&& !stopped) {
135         dictionaryReader.readNext();
136         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
137             if(dictionaryReader.name()=="k") {
138                 a = dictionaryReader.readElementText();
139                 if(a==key)
140                     match = true;
141             }
142         }
143         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
144             if(match) {
145                 QString temp(dictionaryReader.text().toString());
146                 temp.replace("\n","");
147                 if(temp == ""){
148                     while(dictionaryReader.name()!="ar"&&
149                                 !dictionaryReader.atEnd()){
150                         dictionaryReader.readNext();
151                         temp+=dictionaryReader.text().toString();
152                     }
153                 }
154                 resultString+=temp.replace("\n","")+"\n";
155                 match=false;
156             }
157         }
158         this->thread()->yieldCurrentThread();
159     }
160     stopped=false;
161     dictionaryFile.close();
162     return resultString;
163 }
164
165 void XdxfPlugin::stop() {
166     stopped=true;
167 }
168
169 DictDialog* XdxfPlugin::dictDialog() {
170      return _dictDialog;
171 }
172
173 void XdxfPlugin::setPath(QString path){
174     this->path=path;
175     _settings->setValue("path",path);
176     getDictionaryInfo();
177 }
178
179
180 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
181     XdxfPlugin *plugin = new XdxfPlugin();
182     if(settings){
183         plugin->setPath(settings->value("path"));
184         QStringList list = settings->keys();
185         foreach(QString key, list)
186             plugin->settings()->setValue(key, settings->value(key));
187     }
188     return  plugin;
189 }
190
191 bool XdxfPlugin::isAvailable() const {
192     return true;
193 }
194
195 void XdxfPlugin::setHash(uint _hash)
196 {
197     this->_hash=_hash;
198 }
199
200 uint XdxfPlugin::hash() const
201 {
202    return _hash;
203 }
204
205 Settings* XdxfPlugin::settings() {
206     return _settings;
207 }
208
209 bool XdxfPlugin::isCached()
210 {
211     return false;
212 }
213
214 void XdxfPlugin::setSettings(Settings *settings) {
215     _settings = settings;
216     setPath(_settings->value("path"));
217     emit settingsChanged();
218 }
219
220
221 void XdxfPlugin::getDictionaryInfo() {
222     QFile dictionaryFile(path);
223     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
224         qDebug()<<"Error: could not open file";
225         return;
226     }
227
228     QXmlStreamReader dictionaryReader(&dictionaryFile);
229     dictionaryReader.readNextStartElement();
230     if(dictionaryReader.name()=="xdxf") {
231       if(dictionaryReader.attributes().hasAttribute("lang_from"))
232         _langFrom = dictionaryReader.attributes().value("lang_from").toString();
233       if(dictionaryReader.attributes().hasAttribute("lang_to"))
234         _langTo = dictionaryReader.attributes().value("lang_to").toString();
235     }
236     dictionaryReader.readNextStartElement();
237     if(dictionaryReader.name()=="full_name")
238         _name=dictionaryReader.readElementText();
239     dictionaryReader.readNextStartElement();
240     if(dictionaryReader.name()=="description")
241         _infoNote=dictionaryReader.readElementText();
242
243     /*dictionaryFile.seek(0);
244
245     long wordsCount = 0;
246
247     QString line;
248     while(!dictionaryFile.atEnd()) {
249         line = dictionaryFile.readLine();
250         if(line.contains("<ar>")) {
251             wordsCount++;
252         }
253     }*/
254
255     dictionaryFile.close();
256 }
257
258 QString XdxfPlugin::removeAccents(QString string) {
259
260     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
261     QString normalized = string.normalized(QString::NormalizationForm_D);
262     normalized = normalized;
263     for(int i=0; i<normalized.size(); i++) {
264         if( !normalized[i].isLetterOrNumber() &&
265             !normalized[i].isSpace() &&
266             !normalized[i].isDigit()) {
267             normalized.remove(i,1);
268         }
269     }
270     return normalized;
271 }
272
273 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)