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, this);
35 _settings->setValue("type","xdxf");
37 _settings->setValue("cached","true");
39 _settings->setValue("cached","false");
43 setPath("/usr/lib/mdictionary/dict.xdxf");
47 QString XdxfPlugin::langFrom() const {
51 QString XdxfPlugin::langTo() const {
55 QString XdxfPlugin::name() const {
59 QString XdxfPlugin::type() const {
60 // return _settings->value("type");
64 QString XdxfPlugin::infoNote() const {
68 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
69 QSet<Translation*> translations;
70 QFile dictionaryFile(path);
73 if(word.indexOf("*")==-1)
75 QRegExp regWord(word);
76 regWord.setCaseSensitivity(Qt::CaseInsensitive);
77 regWord.setPatternSyntax(QRegExp::Wildcard);
78 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
79 qDebug()<<"Error: could not open file";
80 return translations.toList();
83 QXmlStreamReader dictionaryReader(&dictionaryFile);
87 while(!dictionaryReader.atEnd() && !stopped){
88 dictionaryReader.readNextStartElement();
89 if(dictionaryReader.name()=="ar"){
90 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
91 dictionaryReader.readNextStartElement();
92 if(!dictionaryReader.atEnd())
93 a = dictionaryReader.readElementText();
94 if(regWord.exactMatch(a) && (i<limit || limit==0)) {
97 foreach(tran,translations)
100 ok=false; /*if key word is in the dictionary more that one */
102 if(ok) /*add key word to list*/
103 translations<<(new TranslationXdxf(a,_infoNote,this));
105 if(i>=limit && limit!=0)
109 this->thread()->yieldCurrentThread();
112 dictionaryFile.close();
113 return translations.toList();
116 QString XdxfPlugin::search(QString key) {
117 QFile dictionaryFile(path);
118 QString resultString("");
119 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
120 qDebug()<<"Error: could not open file";
123 QXmlStreamReader dictionaryReader(&dictionaryFile);
130 while (!dictionaryReader.atEnd()&& !stopped) {
131 dictionaryReader.readNext();
132 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
133 if(dictionaryReader.name()=="k") {
134 a = dictionaryReader.readElementText();
139 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
141 QString temp(dictionaryReader.text().toString());
142 temp.replace("\n","");
144 while(dictionaryReader.name()!="ar"&&
145 !dictionaryReader.atEnd()){
146 dictionaryReader.readNext();
147 temp+=dictionaryReader.text().toString();
150 resultString+=temp.replace("\n","")+"\n";
154 this->thread()->yieldCurrentThread();
157 dictionaryFile.close();
161 void XdxfPlugin::stop() {
165 DictDialog* XdxfPlugin::dictDialog() {
169 void XdxfPlugin::setPath(QString path){
171 _settings->setValue("path",path);
176 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
177 XdxfPlugin *plugin = new XdxfPlugin();
179 plugin->setPath(settings->value("path"));
183 bool XdxfPlugin::isAvailable() const {
187 void XdxfPlugin::setHash(uint _hash)
192 uint XdxfPlugin::hash() const
197 Settings* XdxfPlugin::settings() {
201 bool XdxfPlugin::isCached()
206 void XdxfPlugin::setSettings(Settings *settings) {
207 _settings = settings;
208 setPath(_settings->value("path"));
209 emit settingsChanged();
213 void XdxfPlugin::getDictionaryInfo() {
214 QFile dictionaryFile(path);
215 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
216 qDebug()<<"Error: could not open file";
220 QXmlStreamReader dictionaryReader(&dictionaryFile);
221 dictionaryReader.readNextStartElement();
222 if(dictionaryReader.name()=="xdxf") {
223 if(dictionaryReader.attributes().hasAttribute("lang_from"))
224 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
225 if(dictionaryReader.attributes().hasAttribute("lang_to"))
226 _langTo = dictionaryReader.attributes().value("lang_to").toString();
228 dictionaryReader.readNextStartElement();
229 if(dictionaryReader.name()=="full_name")
230 _name=dictionaryReader.readElementText();
231 dictionaryReader.readNextStartElement();
232 if(dictionaryReader.name()=="description")
233 _infoNote=dictionaryReader.readElementText();
235 dictionaryFile.seek(0);
240 while(!dictionaryFile.atEnd()) {
241 line = dictionaryFile.readLine();
242 if(line.contains("<ar>")) {
247 dictionaryFile.close();
250 QString XdxfPlugin::removeAccents(QString string) {
251 QString normalized = string.normalized(QString::NormalizationForm_D);
252 for(int i=0; i<normalized.size(); i++) {
253 if(!normalized[i].isLetterOrNumber() && !normalized[i].isSpace()) {
254 normalized.remove(i,1);
260 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)