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