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);
72 word = removeAccents(word);
75 if(word.indexOf("*")==-1)
77 QRegExp regWord(word);
78 regWord.setCaseSensitivity(Qt::CaseInsensitive);
79 regWord.setPatternSyntax(QRegExp::Wildcard);
80 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
81 qDebug()<<"Error: could not open file";
82 return translations.toList();
85 QXmlStreamReader dictionaryReader(&dictionaryFile);
89 while(!dictionaryReader.atEnd() && !stopped){
90 dictionaryReader.readNextStartElement();
91 if(dictionaryReader.name()=="ar"){
92 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
93 dictionaryReader.readNextStartElement();
94 if(!dictionaryReader.atEnd())
95 a = dictionaryReader.readElementText();
96 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
99 foreach(tran,translations)
102 ok=false; /*if key word is in the dictionary more that one */
104 if(ok) /*add key word to list*/
105 translations<<(new TranslationXdxf(a,_infoNote,this));
107 if(i>=limit && limit!=0)
111 this->thread()->yieldCurrentThread();
114 dictionaryFile.close();
115 return translations.toList();
118 QString XdxfPlugin::search(QString key) {
119 QFile dictionaryFile(path);
120 QString resultString("");
121 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
122 qDebug()<<"Error: could not open file";
125 QXmlStreamReader dictionaryReader(&dictionaryFile);
132 while (!dictionaryReader.atEnd()&& !stopped) {
133 dictionaryReader.readNext();
134 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
135 if(dictionaryReader.name()=="k") {
136 a = dictionaryReader.readElementText();
141 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
143 QString temp(dictionaryReader.text().toString());
144 temp.replace("\n","");
146 while(dictionaryReader.name()!="ar"&&
147 !dictionaryReader.atEnd()){
148 dictionaryReader.readNext();
149 temp+=dictionaryReader.text().toString();
152 resultString+=temp.replace("\n","")+"\n";
156 this->thread()->yieldCurrentThread();
159 dictionaryFile.close();
163 void XdxfPlugin::stop() {
167 DictDialog* XdxfPlugin::dictDialog() {
171 void XdxfPlugin::setPath(QString path){
173 _settings->setValue("path",path);
178 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
179 XdxfPlugin *plugin = new XdxfPlugin();
181 plugin->setPath(settings->value("path"));
185 bool XdxfPlugin::isAvailable() const {
189 void XdxfPlugin::setHash(uint _hash)
194 uint XdxfPlugin::hash() const
199 Settings* XdxfPlugin::settings() {
203 bool XdxfPlugin::isCached()
208 void XdxfPlugin::setSettings(Settings *settings) {
209 _settings = settings;
210 setPath(_settings->value("path"));
211 emit settingsChanged();
215 void XdxfPlugin::getDictionaryInfo() {
216 QFile dictionaryFile(path);
217 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
218 qDebug()<<"Error: could not open file";
222 QXmlStreamReader dictionaryReader(&dictionaryFile);
223 dictionaryReader.readNextStartElement();
224 if(dictionaryReader.name()=="xdxf") {
225 if(dictionaryReader.attributes().hasAttribute("lang_from"))
226 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
227 if(dictionaryReader.attributes().hasAttribute("lang_to"))
228 _langTo = dictionaryReader.attributes().value("lang_to").toString();
230 dictionaryReader.readNextStartElement();
231 if(dictionaryReader.name()=="full_name")
232 _name=dictionaryReader.readElementText();
233 dictionaryReader.readNextStartElement();
234 if(dictionaryReader.name()=="description")
235 _infoNote=dictionaryReader.readElementText();
237 /*dictionaryFile.seek(0);
242 while(!dictionaryFile.atEnd()) {
243 line = dictionaryFile.readLine();
244 if(line.contains("<ar>")) {
249 dictionaryFile.close();
252 QString XdxfPlugin::removeAccents(QString string) {
254 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
255 QString normalized = string.normalized(QString::NormalizationForm_D);
256 normalized = normalized;
257 for(int i=0; i<normalized.size(); i++) {
258 if( !normalized[i].isLetterOrNumber() &&
259 !normalized[i].isSpace() &&
260 !normalized[i].isDigit()) {
261 normalized.remove(i,1);
267 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)