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