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