466b7a6c3eef1e1cb2523d3981afb27b2455c36f
[mdictionary] / trunk / src / plugins / xdxf / src / XdxfPlugin / xdxfplugin.cpp
1 #include "xdxfplugin.h"
2 #include <QDebug>
3 #include <QFile>
4 #include <QXmlStreamReader>
5 #include <QtPlugin>
6 #include "TranslationXdxf.h"
7
8 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
9                     _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
10                     _type(tr("xdxf")), _infoNote(tr("")) {
11     path="dict.xdxf";
12     stopped = false;
13 }
14
15 QString XdxfPlugin::langFrom() const {
16     return  _langFrom;
17 }
18
19 QString XdxfPlugin::langTo() const {
20     return  _langTo;
21 }
22
23 QString XdxfPlugin::name() const {
24     return  _name;
25 }
26
27 QString XdxfPlugin::type() const {
28     return _type;
29 }
30
31 QString XdxfPlugin::infoNote() const {
32     return  _infoNote;
33 }
34
35 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
36     stopped = false;
37     QRegExp regWord(word);
38     regWord.setCaseSensitivity(Qt::CaseInsensitive);
39     regWord.setPatternSyntax(QRegExp::Wildcard);
40
41     QList<Translation*> translations;
42     QFile dictionaryFile(path);
43     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
44         qDebug()<<"Error: could not open file";
45         return translations;
46     }
47     QXmlStreamReader dictionaryReader(&dictionaryFile);
48     dictionaryReader.readNextStartElement();
49     if(dictionaryReader.name()=="xdxf") {
50       if(dictionaryReader.attributes().hasAttribute("lang_from"))
51         _langFrom = dictionaryReader.attributes().value("lang_from").toString();
52       if(dictionaryReader.attributes().hasAttribute("lang_to"))
53         _langTo = dictionaryReader.attributes().value("lang_to").toString();
54     }
55     dictionaryReader.readNextStartElement();
56     if(dictionaryReader.name()=="full_name")
57         _name=dictionaryReader.readElementText();
58     dictionaryReader.readNextStartElement();
59     if(dictionaryReader.name()=="description")
60         _infoNote=dictionaryReader.readElementText();
61     QString a;
62     int i=0;
63     while(!dictionaryReader.atEnd() && !stopped){
64         dictionaryReader.readNextStartElement();
65         if(dictionaryReader.name()=="ar"){
66             while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
67                 dictionaryReader.readNextStartElement();
68             a = dictionaryReader.readElementText();
69             if(regWord.exactMatch(a) && i<limit) {
70                 translations.append(new TranslationXdxf(a,_infoNote,this));
71                 i++;
72                 if(i>=limit)
73                     break;
74             }
75         }
76     }
77     stopped=false;
78     dictionaryFile.close();
79     return translations;
80 }
81
82 QString XdxfPlugin::search(QString key) {
83     QFile dictionaryFile(path);
84     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
85         qDebug()<<"Error: could not open file";
86         return "";
87     }
88     QXmlStreamReader dictionaryReader(&dictionaryFile);
89
90     QString a;
91     bool match =false;
92     while (!dictionaryReader.atEnd()) {
93         dictionaryReader.readNext();
94         if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
95             if(dictionaryReader.name()=="k") {
96                 a = dictionaryReader.readElementText();
97                 if(a==key)
98                     match = true;
99             }
100         }
101         else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters)  {
102             if(match) {
103               QString temp(dictionaryReader.text().toString().replace("\n",""));
104               dictionaryFile.close();
105               return temp;
106             }
107         }
108     }
109     return "";
110 }
111
112 void XdxfPlugin::stop() {
113     stopped=true;
114 }
115
116 QDialog* XdxfPlugin::loadDialog() {
117      path="dict.xdxf";
118 }
119
120 QDialog* XdxfPlugin::settingsDialog() {
121     path="dict.xdxf";
122 }
123
124 CommonDictInterface* XdxfPlugin::getNew(const Settings*) const {
125   return new XdxfPlugin();
126 }
127
128 bool XdxfPlugin::isAvailable() const {
129     return true;
130 }
131
132 void XdxfPlugin::setHash(uint _hash)
133 {
134     this->_hash=_hash;
135 }
136
137 uint XdxfPlugin::hash() const
138 {
139    return _hash;
140 }
141
142 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)