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");
45 _icon = QIcon(":/icons/xdxf.png");
48 QString XdxfPlugin::langFrom() const {
52 QString XdxfPlugin::langTo() const {
56 QString XdxfPlugin::name() const {
60 QString XdxfPlugin::type() const {
61 // return _settings->value("type");
65 QString XdxfPlugin::infoNote() const {
69 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
70 QSet<Translation*> translations;
71 QFile dictionaryFile(path);
73 word = removeAccents(word);
76 if(word.indexOf("*")==-1)
78 QRegExp regWord(word);
79 regWord.setCaseSensitivity(Qt::CaseInsensitive);
80 regWord.setPatternSyntax(QRegExp::Wildcard);
81 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
82 qDebug()<<"Error: could not open file";
83 return translations.toList();
86 QXmlStreamReader dictionaryReader(&dictionaryFile);
90 while(!dictionaryReader.atEnd() && !stopped){
91 dictionaryReader.readNextStartElement();
92 if(dictionaryReader.name()=="ar"){
93 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
94 dictionaryReader.readNextStartElement();
95 if(!dictionaryReader.atEnd())
96 a = dictionaryReader.readElementText();
97 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
100 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"));
182 QStringList list = settings->keys();
183 foreach(QString key, list)
184 plugin->settings()->setValue(key, settings->value(key));
189 bool XdxfPlugin::isAvailable() const {
193 void XdxfPlugin::setHash(uint _hash) {
197 uint XdxfPlugin::hash() const {
201 Settings* XdxfPlugin::settings() {
205 bool XdxfPlugin::isCached() {
209 void XdxfPlugin::setSettings(Settings *settings) {
210 _settings = settings;
211 setPath(_settings->value("path"));
212 emit settingsChanged();
216 void XdxfPlugin::getDictionaryInfo() {
217 QFile dictionaryFile(path);
218 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
219 qDebug()<<"Error: could not open file";
223 QXmlStreamReader dictionaryReader(&dictionaryFile);
224 dictionaryReader.readNextStartElement();
225 if(dictionaryReader.name()=="xdxf") {
226 if(dictionaryReader.attributes().hasAttribute("lang_from"))
227 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
228 if(dictionaryReader.attributes().hasAttribute("lang_to"))
229 _langTo = dictionaryReader.attributes().value("lang_to").toString();
231 dictionaryReader.readNextStartElement();
232 if(dictionaryReader.name()=="full_name")
233 _name=dictionaryReader.readElementText();
234 dictionaryReader.readNextStartElement();
235 if(dictionaryReader.name()=="description")
236 _infoNote=dictionaryReader.readElementText();
238 /*dictionaryFile.seek(0);
243 while(!dictionaryFile.atEnd()) {
244 line = dictionaryFile.readLine();
245 if(line.contains("<ar>")) {
250 dictionaryFile.close();
253 QString XdxfPlugin::removeAccents(QString string) {
255 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
256 QString normalized = string.normalized(QString::NormalizationForm_D);
257 normalized = normalized;
258 for(int i=0; i<normalized.size(); i++) {
259 if( !normalized[i].isLetterOrNumber() &&
260 !normalized[i].isSpace() &&
261 !normalized[i].isDigit()) {
262 normalized.remove(i,1);
268 QIcon* XdxfPlugin::icon() {
272 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)