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