1 /*******************************************************************************
3 This file is part of mDictionary.
5 mDictionary is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 mDictionary is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mDictionary. If not, see <http://www.gnu.org/licenses/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 #include "xdxfplugin.h"
25 #include <QXmlStreamReader>
27 #include "TranslationXdxf.h"
28 #include "../../../includes/settings.h"
30 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
31 _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
32 _type(tr("xdxf")), _infoNote(tr("")) {
33 _settings = new Settings();
34 _dictDialog = new XdxfDictDialog(this);
35 _settings->setValue("type","xdxf");
37 _settings->setValue("Cached","true");
39 _settings->setValue("Cached","false");
45 QString XdxfPlugin::langFrom() const {
49 QString XdxfPlugin::langTo() const {
53 QString XdxfPlugin::name() const {
57 QString XdxfPlugin::type() const {
58 // return _settings->value("type");
62 QString XdxfPlugin::infoNote() const {
66 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
67 QSet<Translation*> translations;
68 QFile dictionaryFile(path);
71 if(word.indexOf("*")==-1)
73 QRegExp regWord(word);
74 regWord.setCaseSensitivity(Qt::CaseInsensitive);
75 regWord.setPatternSyntax(QRegExp::Wildcard);
76 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
77 qDebug()<<"Error: could not open file";
78 return translations.toList();
81 /*read information about dictionary*/
82 QXmlStreamReader dictionaryReader(&dictionaryFile);
83 dictionaryReader.readNextStartElement();
84 if(dictionaryReader.name()=="xdxf") {
85 if(dictionaryReader.attributes().hasAttribute("lang_from"))
86 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
87 if(dictionaryReader.attributes().hasAttribute("lang_to"))
88 _langTo = dictionaryReader.attributes().value("lang_to").toString();
90 dictionaryReader.readNextStartElement();
91 if(dictionaryReader.name()=="full_name")
92 _name=dictionaryReader.readElementText();
93 dictionaryReader.readNextStartElement();
94 if(dictionaryReader.name()=="description")
95 _infoNote=dictionaryReader.readElementText();
100 while(!dictionaryReader.atEnd() && !stopped){
101 dictionaryReader.readNextStartElement();
102 if(dictionaryReader.name()=="ar"){
103 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
104 dictionaryReader.readNextStartElement();
105 if(!dictionaryReader.atEnd())
106 a = dictionaryReader.readElementText();
107 if(regWord.exactMatch(a) && (i<limit || limit==0)) {
110 foreach(tran,translations)
113 ok=false; /*if key word is in the dictionary more that one */
115 if(ok) /*add key word to list*/
116 translations<<(new TranslationXdxf(a,_infoNote,this));
118 if(i>=limit && limit!=0)
124 dictionaryFile.close();
125 return translations.toList();
128 QString XdxfPlugin::search(QString key) {
129 QFile dictionaryFile(path);
130 QString resultString("");
131 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
132 qDebug()<<"Error: could not open file";
135 QXmlStreamReader dictionaryReader(&dictionaryFile);
139 while (!dictionaryReader.atEnd()) {
140 dictionaryReader.readNext();
141 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
142 if(dictionaryReader.name()=="k") {
143 a = dictionaryReader.readElementText();
148 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
150 QString temp(dictionaryReader.text().toString());
151 temp.replace("\n","");
153 while(dictionaryReader.name()!="ar" && !dictionaryReader.atEnd()){
154 dictionaryReader.readNext();
155 temp+=dictionaryReader.text().toString();
158 resultString+=temp.replace("\n","")+"\n";
163 dictionaryFile.close();
167 void XdxfPlugin::stop() {
171 DictDialog* XdxfPlugin::dictDialog() {
175 void XdxfPlugin::setPath(QString path){
177 _settings->setValue("path",path);
181 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
182 XdxfPlugin *plugin = new XdxfPlugin();
184 plugin->setPath(settings->value("path"));
188 bool XdxfPlugin::isAvailable() const {
192 void XdxfPlugin::setHash(uint _hash)
197 uint XdxfPlugin::hash() const
202 Settings* XdxfPlugin::settings() {
206 bool XdxfPlugin::isCached()
211 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)