Added direct search of translation, changed searching for translation of words list...
[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 QString XdxfPlugin::langFrom() const {   
48     return _langFrom;
49 }
50
51 QString XdxfPlugin::langTo() const {
52     return  _langTo;
53 }
54
55 QString XdxfPlugin::name() const {
56     return  _name;
57 }
58
59 QString XdxfPlugin::type() const {
60 //    return _settings->value("type");
61     return _type;
62 }
63
64 QString XdxfPlugin::infoNote() const {
65     return  _infoNote;
66 }
67
68 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) { 
69     QSet<Translation*> translations;
70     QFile dictionaryFile(path);
71
72     word = removeAccents(word);
73
74     stopped = false;
75     if(word.indexOf("*")==-1)
76         word+="*";
77     QRegExp regWord(word);
78     regWord.setCaseSensitivity(Qt::CaseInsensitive);
79     regWord.setPatternSyntax(QRegExp::Wildcard);
80     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
81         qDebug()<<"Error: could not open file";
82         return translations.toList();
83     }
84
85     QXmlStreamReader dictionaryReader(&dictionaryFile);
86     /*search words list*/
87     QString a;
88     int i=0;
89     while(!dictionaryReader.atEnd() && !stopped){
90         dictionaryReader.readNextStartElement();
91         if(dictionaryReader.name()=="ar"){
92             while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
93                 dictionaryReader.readNextStartElement();
94             if(!dictionaryReader.atEnd())
95                 a = dictionaryReader.readElementText();
96             if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
97                 bool ok=true;
98                 Translation *tran;
99                 foreach(tran,translations)
100                 {
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     return  plugin;
183 }
184
185 bool XdxfPlugin::isAvailable() const {
186     return true;
187 }
188
189 void XdxfPlugin::setHash(uint _hash)
190 {
191     this->_hash=_hash;
192 }
193
194 uint XdxfPlugin::hash() const
195 {
196    return _hash;
197 }
198
199 Settings* XdxfPlugin::settings() {
200     return _settings;
201 }
202
203 bool XdxfPlugin::isCached()
204 {
205     return false;
206 }
207
208 void XdxfPlugin::setSettings(Settings *settings) {
209     _settings = settings;
210     setPath(_settings->value("path"));
211     emit settingsChanged();
212 }
213
214
215 void XdxfPlugin::getDictionaryInfo() {
216     QFile dictionaryFile(path);
217     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
218         qDebug()<<"Error: could not open file";
219         return;
220     }
221
222     QXmlStreamReader dictionaryReader(&dictionaryFile);
223     dictionaryReader.readNextStartElement();
224     if(dictionaryReader.name()=="xdxf") {
225       if(dictionaryReader.attributes().hasAttribute("lang_from"))
226         _langFrom = dictionaryReader.attributes().value("lang_from").toString();
227       if(dictionaryReader.attributes().hasAttribute("lang_to"))
228         _langTo = dictionaryReader.attributes().value("lang_to").toString();
229     }
230     dictionaryReader.readNextStartElement();
231     if(dictionaryReader.name()=="full_name")
232         _name=dictionaryReader.readElementText();
233     dictionaryReader.readNextStartElement();
234     if(dictionaryReader.name()=="description")
235         _infoNote=dictionaryReader.readElementText();
236
237     /*dictionaryFile.seek(0);
238
239     long wordsCount = 0;
240
241     QString line;
242     while(!dictionaryFile.atEnd()) {
243         line = dictionaryFile.readLine();
244         if(line.contains("<ar>")) {
245             wordsCount++;
246         }
247     }*/
248
249     dictionaryFile.close();
250 }
251
252 QString XdxfPlugin::removeAccents(QString string) {
253
254     string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
255     QString normalized = string.normalized(QString::NormalizationForm_D);
256     normalized = normalized;
257     for(int i=0; i<normalized.size(); i++) {
258         if( !normalized[i].isLetterOrNumber() &&
259             !normalized[i].isSpace() &&
260             !normalized[i].isDigit()) {
261             normalized.remove(i,1);
262         }
263     }
264     return normalized;
265 }
266
267 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)