Added word counting function in xdxf plugin
[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     if(_settings->value("cached") == "true")
71         return searchWordListCache(word,limit);
72     return searchWordListFile(word, limit);
73 }
74
75 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
76
77
78 }
79
80
81
82 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
83     QSet<Translation*> translations;
84     QFile dictionaryFile(path);
85
86     word = removeAccents(word);
87
88     stopped = false;
89     if(word.indexOf("*")==-1)
90         word+="*";
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();
97     }
98
99     QXmlStreamReader dictionaryReader(&dictionaryFile);
100     /*search words list*/
101     QString a;
102     int i=0;
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)) {
111                 bool ok=true;
112                 Translation *tran;
113                 foreach(tran,translations)
114                 {
115                     if(tran->key()==a)
116                         ok=false;  /*if key word is in the dictionary more that one */
117                 }
118                 if(ok)  /*add key word to list*/
119                     translations<<(new TranslationXdxf(a,_infoNote,this));
120                 i++;
121                 if(i>=limit && limit!=0)
122                     break;
123             }
124         }
125         this->thread()->yieldCurrentThread();
126     }
127     stopped=false;
128     dictionaryFile.close();
129     return translations.toList();
130 }
131
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";
137         return "";
138     }
139     QXmlStreamReader dictionaryReader(&dictionaryFile);
140
141
142     QString a;
143
144     bool match =false;
145     stopped = false;
146     while (!dictionaryReader.atEnd()&& !stopped) {
147         dictionaryReader.readNext();
148         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
149             if(dictionaryReader.name()=="k") {
150                 a = dictionaryReader.readElementText();
151                 if(a==key)
152                     match = true;
153             }
154         }
155         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
156             if(match) {
157                 QString temp(dictionaryReader.text().toString());
158                 temp.replace("\n","");
159                 if(temp == ""){
160                     while(dictionaryReader.name()!="ar"&&
161                                 !dictionaryReader.atEnd()){
162                         dictionaryReader.readNext();
163                         temp+=dictionaryReader.text().toString();
164                     }
165                 }
166                 resultString+=temp.replace("\n","")+"\n";
167                 match=false;
168             }
169         }
170         this->thread()->yieldCurrentThread();
171     }
172     stopped=false;
173     dictionaryFile.close();
174     return resultString;
175 }
176
177 void XdxfPlugin::stop() {
178     stopped=true;
179 }
180
181 DictDialog* XdxfPlugin::dictDialog() {
182      return _dictDialog;
183 }
184
185 void XdxfPlugin::setPath(QString path){
186     this->path=path;
187     _settings->setValue("path",path);
188     getDictionaryInfo();
189 }
190
191
192 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
193     XdxfPlugin *plugin = new XdxfPlugin();
194     if(settings){
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     }
200     return  plugin;
201 }
202
203 bool XdxfPlugin::isAvailable() const {
204     return true;
205 }
206
207 void XdxfPlugin::setHash(uint _hash)
208 {
209     this->_hash=_hash;
210 }
211
212 uint XdxfPlugin::hash() const
213 {
214    return _hash;
215 }
216
217 Settings* XdxfPlugin::settings() {
218     return _settings;
219 }
220
221 bool XdxfPlugin::isCached()
222 {
223     return false;
224 }
225
226 void XdxfPlugin::setSettings(Settings *settings) {
227     _settings = settings;
228     setPath(_settings->value("path"));
229     emit settingsChanged();
230 }
231
232
233 void XdxfPlugin::getDictionaryInfo() {
234     QFile dictionaryFile(path);
235     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
236         qDebug()<<"Error: could not open file";
237         return;
238     }
239
240     QXmlStreamReader dictionaryReader(&dictionaryFile);
241     dictionaryReader.readNextStartElement();
242     if(dictionaryReader.name()=="xdxf") {
243       if(dictionaryReader.attributes().hasAttribute("lang_from"))
244         _langFrom = dictionaryReader.attributes().value("lang_from").toString();
245       if(dictionaryReader.attributes().hasAttribute("lang_to"))
246         _langTo = dictionaryReader.attributes().value("lang_to").toString();
247     }
248     dictionaryReader.readNextStartElement();
249     if(dictionaryReader.name()=="full_name")
250         _name=dictionaryReader.readElementText();
251     dictionaryReader.readNextStartElement();
252     if(dictionaryReader.name()=="description")
253         _infoNote=dictionaryReader.readElementText();
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 QIcon* XdxfPlugin::icon() {
274     return &_icon;
275 }
276
277 int XdxfPlugin::countWords() {
278
279     QFile dictionaryFile(path);
280     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
281         qDebug()<<"Error: could not open file";
282         return -1;
283     }
284
285     dictionaryFile.seek(0);
286
287     long wordsCount = 0;
288
289     QString line;
290     while(!dictionaryFile.atEnd()) {
291         line = dictionaryFile.readLine();
292         if(line.contains("<ar>")) {
293             wordsCount++;
294         }
295     }
296     _wordsCount = wordsCount;
297     dictionaryFile.close();
298     return wordsCount;
299 }
300
301 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)