Fixed displaying translation of the same keyword from different dictionaries
[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);
35     _settings->setValue("Type","xdxf");
36     if(isCached())
37         _settings->setValue("Cached","true");
38     else
39         _settings->setValue("Cached","false");
40
41     setPath("/usr/lib/mdictionary/dict.xdxf");
42     stopped = false;
43 }
44
45 QString XdxfPlugin::langFrom() const {   
46     return _langFrom;
47 }
48
49 QString XdxfPlugin::langTo() const {
50     return  _langTo;
51 }
52
53 QString XdxfPlugin::name() const {
54     return  _name;
55 }
56
57 QString XdxfPlugin::type() const {
58 //    return _settings->value("type");
59     return _type;
60 }
61
62 QString XdxfPlugin::infoNote() const {
63     return  _infoNote;
64 }
65
66 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) { 
67     QSet<Translation*> translations;
68     QFile dictionaryFile(path);
69
70     stopped = false;
71     if(word.indexOf("*")==-1)
72         word+="*";
73     QRegExp regWord(word);
74     regWord.setCaseSensitivity(Qt::CaseInsensitive);
75     regWord.setPatternSyntax(QRegExp::Wildcard);
76     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
77         qDebug()<<"Error: could not open file";
78         return translations.toList();
79     }
80
81     /*read information about dictionary*/
82     QXmlStreamReader dictionaryReader(&dictionaryFile);
83     dictionaryReader.readNextStartElement();
84     if(dictionaryReader.name()=="xdxf") {
85       if(dictionaryReader.attributes().hasAttribute("lang_from"))
86         _langFrom = dictionaryReader.attributes().value("lang_from").toString();
87       if(dictionaryReader.attributes().hasAttribute("lang_to"))
88         _langTo = dictionaryReader.attributes().value("lang_to").toString();
89     }
90     dictionaryReader.readNextStartElement();
91     if(dictionaryReader.name()=="full_name")
92         _name=dictionaryReader.readElementText();
93     dictionaryReader.readNextStartElement();
94     if(dictionaryReader.name()=="description")
95         _infoNote=dictionaryReader.readElementText();
96
97     /*search words list*/
98     QString a;
99     int i=0;
100     while(!dictionaryReader.atEnd() && !stopped){
101         dictionaryReader.readNextStartElement();
102         if(dictionaryReader.name()=="ar"){
103             while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
104                 dictionaryReader.readNextStartElement();
105             if(!dictionaryReader.atEnd())
106                 a = dictionaryReader.readElementText();
107             if(regWord.exactMatch(a) && (i<limit || limit==0)) {
108                 bool ok=true;
109                 Translation *tran;
110                 foreach(tran,translations)
111                 {
112                     if(tran->key()==a)
113                         ok=false;  /*if key word is in the dictionary more that one */
114                 }
115                 if(ok)  /*add key word to list*/
116                     translations<<(new TranslationXdxf(a,_infoNote,this));
117                 i++;
118                 if(i>=limit && limit!=0)
119                     break;
120             }
121         }
122         this->thread()->yieldCurrentThread();
123     }
124     stopped=false;
125     dictionaryFile.close();
126     return translations.toList();
127 }
128
129 QString XdxfPlugin::search(QString key) {
130     QFile dictionaryFile(path);
131     QString resultString("");
132     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
133         qDebug()<<"Error: could not open file";
134         return "";
135     }
136     QXmlStreamReader dictionaryReader(&dictionaryFile);
137
138
139     QString a;
140
141     bool match =false;
142     stopped = false;
143     while (!dictionaryReader.atEnd()&& !stopped) {
144         dictionaryReader.readNext();
145         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
146             if(dictionaryReader.name()=="k") {
147                 a = dictionaryReader.readElementText();
148                 if(a==key)
149                     match = true;
150             }
151         }
152         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
153             if(match) {
154                 QString temp(dictionaryReader.text().toString());
155                 temp.replace("\n","");
156                 if(temp == ""){
157                     while(dictionaryReader.name()!="ar"&&
158                                 !dictionaryReader.atEnd()){
159                         dictionaryReader.readNext();
160                         temp+=dictionaryReader.text().toString();
161                     }
162                 }
163                 resultString+=temp.replace("\n","")+"\n";
164                 match=false;
165             }
166         }
167         this->thread()->yieldCurrentThread();
168     }
169     stopped=false;
170     dictionaryFile.close();
171     return resultString;
172 }
173
174 void XdxfPlugin::stop() {
175     stopped=true;
176 }
177
178 DictDialog* XdxfPlugin::dictDialog() {
179      return _dictDialog;
180 }
181
182 void XdxfPlugin::setPath(QString path){
183     this->path=path;
184     _settings->setValue("path",path);
185 }
186
187
188 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
189     XdxfPlugin *plugin = new XdxfPlugin();
190     if(settings)
191         plugin->setPath(settings->value("path"));
192     return  plugin;
193 }
194
195 bool XdxfPlugin::isAvailable() const {
196     return true;
197 }
198
199 void XdxfPlugin::setHash(uint _hash)
200 {
201     this->_hash=_hash;
202 }
203
204 uint XdxfPlugin::hash() const
205 {
206    return _hash;
207 }
208
209 Settings* XdxfPlugin::settings() {
210     return _settings;
211 }
212
213 bool XdxfPlugin::isCached()
214 {
215     return false;
216 }
217
218 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)