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");
41 setPath("/usr/lib/mdictionary/dict.xdxf");
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)
122 this->thread()->yieldCurrentThread();
125 dictionaryFile.close();
126 return translations.toList();
129 QString XdxfPlugin::search(QString key) {
130 QFile dictionaryFile(path);
131 QString resultString("");
132 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
133 qDebug()<<"Error: could not open file";
136 QXmlStreamReader dictionaryReader(&dictionaryFile);
143 while (!dictionaryReader.atEnd()&& !stopped) {
144 dictionaryReader.readNext();
145 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
146 if(dictionaryReader.name()=="k") {
147 a = dictionaryReader.readElementText();
152 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
154 QString temp(dictionaryReader.text().toString());
155 temp.replace("\n","");
157 while(dictionaryReader.name()!="ar"&&
158 !dictionaryReader.atEnd()){
159 dictionaryReader.readNext();
160 temp+=dictionaryReader.text().toString();
163 resultString+=temp.replace("\n","")+"\n";
167 this->thread()->yieldCurrentThread();
170 dictionaryFile.close();
174 void XdxfPlugin::stop() {
178 DictDialog* XdxfPlugin::dictDialog() {
182 void XdxfPlugin::setPath(QString path){
184 _settings->setValue("path",path);
188 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
189 XdxfPlugin *plugin = new XdxfPlugin();
191 plugin->setPath(settings->value("path"));
195 bool XdxfPlugin::isAvailable() const {
199 void XdxfPlugin::setHash(uint _hash)
204 uint XdxfPlugin::hash() const
209 Settings* XdxfPlugin::settings() {
213 bool XdxfPlugin::isCached()
218 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)