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");
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);
77 if(word.indexOf("*")==-1)
79 QRegExp regWord(word);
80 regWord.setCaseSensitivity(Qt::CaseInsensitive);
81 regWord.setPatternSyntax(QRegExp::Wildcard);
82 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
83 qDebug()<<"Error: could not open file";
84 return translations.toList();
87 QXmlStreamReader dictionaryReader(&dictionaryFile);
91 while(!dictionaryReader.atEnd() && !stopped){
92 dictionaryReader.readNextStartElement();
93 if(dictionaryReader.name()=="ar"){
94 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
95 dictionaryReader.readNextStartElement();
96 if(!dictionaryReader.atEnd())
97 a = dictionaryReader.readElementText();
98 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
101 foreach(tran,translations)
104 ok=false; /*if key word is in the dictionary more that one */
106 if(ok) /*add key word to list*/
107 translations<<(new TranslationXdxf(a,_infoNote,this));
109 if(i>=limit && limit!=0)
113 this->thread()->yieldCurrentThread();
116 dictionaryFile.close();
117 return translations.toList();
120 QString XdxfPlugin::search(QString key) {
121 QFile dictionaryFile(path);
122 QString resultString("");
123 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
124 qDebug()<<"Error: could not open file";
127 QXmlStreamReader dictionaryReader(&dictionaryFile);
134 while (!dictionaryReader.atEnd()&& !stopped) {
135 dictionaryReader.readNext();
136 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
137 if(dictionaryReader.name()=="k") {
138 a = dictionaryReader.readElementText();
143 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
145 QString temp(dictionaryReader.text().toString());
146 temp.replace("\n","");
148 while(dictionaryReader.name()!="ar"&&
149 !dictionaryReader.atEnd()){
150 dictionaryReader.readNext();
151 temp+=dictionaryReader.text().toString();
154 resultString+=temp.replace("\n","")+"\n";
158 this->thread()->yieldCurrentThread();
161 dictionaryFile.close();
165 void XdxfPlugin::stop() {
169 DictDialog* XdxfPlugin::dictDialog() {
173 void XdxfPlugin::setPath(QString path){
175 _settings->setValue("path",path);
180 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
181 XdxfPlugin *plugin = new XdxfPlugin();
183 plugin->setPath(settings->value("path"));
184 QStringList list = settings->keys();
185 foreach(QString key, list)
186 plugin->settings()->setValue(key, settings->value(key));
191 bool XdxfPlugin::isAvailable() const {
195 void XdxfPlugin::setHash(uint _hash)
200 uint XdxfPlugin::hash() const
205 Settings* XdxfPlugin::settings() {
209 bool XdxfPlugin::isCached()
214 void XdxfPlugin::setSettings(Settings *settings) {
215 _settings = settings;
216 setPath(_settings->value("path"));
217 emit settingsChanged();
221 void XdxfPlugin::getDictionaryInfo() {
222 QFile dictionaryFile(path);
223 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
224 qDebug()<<"Error: could not open file";
228 QXmlStreamReader dictionaryReader(&dictionaryFile);
229 dictionaryReader.readNextStartElement();
230 if(dictionaryReader.name()=="xdxf") {
231 if(dictionaryReader.attributes().hasAttribute("lang_from"))
232 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
233 if(dictionaryReader.attributes().hasAttribute("lang_to"))
234 _langTo = dictionaryReader.attributes().value("lang_to").toString();
236 dictionaryReader.readNextStartElement();
237 if(dictionaryReader.name()=="full_name")
238 _name=dictionaryReader.readElementText();
239 dictionaryReader.readNextStartElement();
240 if(dictionaryReader.name()=="description")
241 _infoNote=dictionaryReader.readElementText();
243 /*dictionaryFile.seek(0);
248 while(!dictionaryFile.atEnd()) {
249 line = dictionaryFile.readLine();
250 if(line.contains("<ar>")) {
255 dictionaryFile.close();
258 QString XdxfPlugin::removeAccents(QString string) {
260 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
261 QString normalized = string.normalized(QString::NormalizationForm_D);
262 normalized = normalized;
263 for(int i=0; i<normalized.size(); i++) {
264 if( !normalized[i].isLetterOrNumber() &&
265 !normalized[i].isSpace() &&
266 !normalized[i].isDigit()) {
267 normalized.remove(i,1);
273 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)