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