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 if(_settings->value("cached") == "true")
71 return searchWordListCache(word,limit);
72 return searchWordListFile(word, limit);
75 QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
82 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
83 QSet<Translation*> translations;
84 QFile dictionaryFile(path);
86 word = removeAccents(word);
89 if(word.indexOf("*")==-1)
91 QRegExp regWord(word);
92 regWord.setCaseSensitivity(Qt::CaseInsensitive);
93 regWord.setPatternSyntax(QRegExp::Wildcard);
94 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
95 qDebug()<<"Error: could not open file";
96 return translations.toList();
99 QXmlStreamReader dictionaryReader(&dictionaryFile);
100 /*search words list*/
103 while(!dictionaryReader.atEnd() && !stopped){
104 dictionaryReader.readNextStartElement();
105 if(dictionaryReader.name()=="ar"){
106 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
107 dictionaryReader.readNextStartElement();
108 if(!dictionaryReader.atEnd())
109 a = dictionaryReader.readElementText();
110 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
113 foreach(tran,translations)
116 ok=false; /*if key word is in the dictionary more that one */
118 if(ok) /*add key word to list*/
119 translations<<(new TranslationXdxf(a,_infoNote,this));
121 if(i>=limit && limit!=0)
125 this->thread()->yieldCurrentThread();
128 dictionaryFile.close();
129 return translations.toList();
132 QString XdxfPlugin::search(QString key) {
133 QFile dictionaryFile(path);
134 QString resultString("");
135 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
136 qDebug()<<"Error: could not open file";
139 QXmlStreamReader dictionaryReader(&dictionaryFile);
146 while (!dictionaryReader.atEnd()&& !stopped) {
147 dictionaryReader.readNext();
148 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
149 if(dictionaryReader.name()=="k") {
150 a = dictionaryReader.readElementText();
155 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
157 QString temp(dictionaryReader.text().toString());
158 temp.replace("\n","");
160 while(dictionaryReader.name()!="ar"&&
161 !dictionaryReader.atEnd()){
162 dictionaryReader.readNext();
163 temp+=dictionaryReader.text().toString();
166 resultString+=temp.replace("\n","")+"\n";
170 this->thread()->yieldCurrentThread();
173 dictionaryFile.close();
177 void XdxfPlugin::stop() {
181 DictDialog* XdxfPlugin::dictDialog() {
185 void XdxfPlugin::setPath(QString path){
187 _settings->setValue("path",path);
192 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
193 XdxfPlugin *plugin = new XdxfPlugin();
195 plugin->setPath(settings->value("path"));
196 QStringList list = settings->keys();
197 foreach(QString key, list)
198 plugin->settings()->setValue(key, settings->value(key));
203 bool XdxfPlugin::isAvailable() const {
207 void XdxfPlugin::setHash(uint _hash)
212 uint XdxfPlugin::hash() const
217 Settings* XdxfPlugin::settings() {
221 bool XdxfPlugin::isCached()
226 void XdxfPlugin::setSettings(Settings *settings) {
227 _settings = settings;
228 setPath(_settings->value("path"));
229 emit settingsChanged();
233 void XdxfPlugin::getDictionaryInfo() {
234 QFile dictionaryFile(path);
235 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
236 qDebug()<<"Error: could not open file";
240 QXmlStreamReader dictionaryReader(&dictionaryFile);
241 dictionaryReader.readNextStartElement();
242 if(dictionaryReader.name()=="xdxf") {
243 if(dictionaryReader.attributes().hasAttribute("lang_from"))
244 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
245 if(dictionaryReader.attributes().hasAttribute("lang_to"))
246 _langTo = dictionaryReader.attributes().value("lang_to").toString();
248 dictionaryReader.readNextStartElement();
249 if(dictionaryReader.name()=="full_name")
250 _name=dictionaryReader.readElementText();
251 dictionaryReader.readNextStartElement();
252 if(dictionaryReader.name()=="description")
253 _infoNote=dictionaryReader.readElementText();
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 QIcon* XdxfPlugin::icon() {
277 int XdxfPlugin::countWords() {
279 QFile dictionaryFile(path);
280 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
281 qDebug()<<"Error: could not open file";
285 dictionaryFile.seek(0);
290 while(!dictionaryFile.atEnd()) {
291 line = dictionaryFile.readLine();
292 if(line.contains("<ar>")) {
296 _wordsCount = wordsCount;
297 dictionaryFile.close();
301 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)