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