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 qDebug()<<removeAccents(QString::fromUtf8("Słońce"));
50 QString XdxfPlugin::langFrom() const {
54 QString XdxfPlugin::langTo() const {
58 QString XdxfPlugin::name() const {
62 QString XdxfPlugin::type() const {
63 // return _settings->value("type");
67 QString XdxfPlugin::infoNote() const {
71 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
72 QSet<Translation*> translations;
73 QFile dictionaryFile(path);
75 word = removeAccents(word);
79 if(word.indexOf("*")==-1)
81 QRegExp regWord(word);
82 regWord.setCaseSensitivity(Qt::CaseInsensitive);
83 regWord.setPatternSyntax(QRegExp::Wildcard);
84 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
85 qDebug()<<"Error: could not open file";
86 return translations.toList();
89 QXmlStreamReader dictionaryReader(&dictionaryFile);
93 while(!dictionaryReader.atEnd() && !stopped){
94 dictionaryReader.readNextStartElement();
95 if(dictionaryReader.name()=="ar"){
96 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
97 dictionaryReader.readNextStartElement();
98 if(!dictionaryReader.atEnd())
99 a = dictionaryReader.readElementText();
100 if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
103 foreach(tran,translations)
106 ok=false; /*if key word is in the dictionary more that one */
108 if(ok) /*add key word to list*/
109 translations<<(new TranslationXdxf(a,_infoNote,this));
111 if(i>=limit && limit!=0)
115 this->thread()->yieldCurrentThread();
118 dictionaryFile.close();
119 return translations.toList();
122 QString XdxfPlugin::search(QString key) {
123 QFile dictionaryFile(path);
124 QString resultString("");
125 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
126 qDebug()<<"Error: could not open file";
129 QXmlStreamReader dictionaryReader(&dictionaryFile);
136 while (!dictionaryReader.atEnd()&& !stopped) {
137 dictionaryReader.readNext();
138 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
139 if(dictionaryReader.name()=="k") {
140 a = dictionaryReader.readElementText();
145 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
147 QString temp(dictionaryReader.text().toString());
148 temp.replace("\n","");
150 while(dictionaryReader.name()!="ar"&&
151 !dictionaryReader.atEnd()){
152 dictionaryReader.readNext();
153 temp+=dictionaryReader.text().toString();
156 resultString+=temp.replace("\n","")+"\n";
160 this->thread()->yieldCurrentThread();
163 dictionaryFile.close();
167 void XdxfPlugin::stop() {
171 DictDialog* XdxfPlugin::dictDialog() {
175 void XdxfPlugin::setPath(QString path){
177 _settings->setValue("path",path);
182 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
183 XdxfPlugin *plugin = new XdxfPlugin();
185 plugin->setPath(settings->value("path"));
189 bool XdxfPlugin::isAvailable() const {
193 void XdxfPlugin::setHash(uint _hash)
198 uint XdxfPlugin::hash() const
203 Settings* XdxfPlugin::settings() {
207 bool XdxfPlugin::isCached()
212 void XdxfPlugin::setSettings(Settings *settings) {
213 _settings = settings;
214 setPath(_settings->value("path"));
215 emit settingsChanged();
219 void XdxfPlugin::getDictionaryInfo() {
220 QFile dictionaryFile(path);
221 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
222 qDebug()<<"Error: could not open file";
226 QXmlStreamReader dictionaryReader(&dictionaryFile);
227 dictionaryReader.readNextStartElement();
228 if(dictionaryReader.name()=="xdxf") {
229 if(dictionaryReader.attributes().hasAttribute("lang_from"))
230 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
231 if(dictionaryReader.attributes().hasAttribute("lang_to"))
232 _langTo = dictionaryReader.attributes().value("lang_to").toString();
234 dictionaryReader.readNextStartElement();
235 if(dictionaryReader.name()=="full_name")
236 _name=dictionaryReader.readElementText();
237 dictionaryReader.readNextStartElement();
238 if(dictionaryReader.name()=="description")
239 _infoNote=dictionaryReader.readElementText();
241 dictionaryFile.seek(0);
246 while(!dictionaryFile.atEnd()) {
247 line = dictionaryFile.readLine();
248 if(line.contains("<ar>")) {
253 dictionaryFile.close();
256 QString XdxfPlugin::removeAccents(QString string) {
258 string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
259 QString normalized = string.normalized(QString::NormalizationForm_D);
260 normalized = normalized;
261 for(int i=0; i<normalized.size(); i++) {
262 if( !normalized[i].isLetterOrNumber() &&
263 !normalized[i].isSpace() &&
264 !normalized[i].isDigit()) {
265 normalized.remove(i,1);
271 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)