Merge branch 'xdxf'
[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
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) {
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     }
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
137     QString a;
138     bool match =false;
139     while (!dictionaryReader.atEnd()) {
140         dictionaryReader.readNext();
141         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
142             if(dictionaryReader.name()=="k") {
143                 a = dictionaryReader.readElementText();
144                 if(a==key)
145                     match = true;
146             }
147         }
148         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters)  {
149             if(match) {
150                 QString temp(dictionaryReader.text().toString());
151                 temp.replace("\n","");
152                 if(temp == ""){
153                     while(dictionaryReader.name()!="ar" && !dictionaryReader.atEnd()){
154                         dictionaryReader.readNext();
155                         temp+=dictionaryReader.text().toString();
156                     }
157                 }
158                 resultString+=temp.replace("\n","");
159                 match=false;
160             }
161         }
162     }
163     dictionaryFile.close();
164     return resultString;
165 }
166
167 void XdxfPlugin::stop() {
168     stopped=true;
169 }
170
171 DictDialog* XdxfPlugin::dictDialog() {
172      return _dictDialog;
173 }
174
175 void XdxfPlugin::setPath(QString path){
176     this->path=path;
177     _settings->setValue("path",path);
178 }
179
180
181 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
182     XdxfPlugin *plugin = new XdxfPlugin();
183     plugin->setPath(settings->value("path"));
184     return  new XdxfPlugin();//plugin;
185 }
186
187 bool XdxfPlugin::isAvailable() const {
188     return true;
189 }
190
191 void XdxfPlugin::setHash(uint _hash)
192 {
193     this->_hash=_hash;
194 }
195
196 uint XdxfPlugin::hash() const
197 {
198    return _hash;
199 }
200
201 Settings* XdxfPlugin::settings() {
202     return _settings;
203 }
204
205 bool XdxfPlugin::isCached()
206 {
207     return false;
208 }
209
210 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)