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