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