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);
35 _settings->setValue("Type","xdxf");
37 _settings->setValue("Cached","true");
39 _settings->setValue("Cached","false");
44 QString XdxfPlugin::langFrom() const {
48 QString XdxfPlugin::langTo() const {
52 QString XdxfPlugin::name() const {
56 QString XdxfPlugin::type() const {
57 // return _settings->value("type");
61 QString XdxfPlugin::infoNote() const {
65 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
66 QSet<Translation*> translations;
67 QFile dictionaryFile(path);
70 if(word.indexOf("*")==-1)
72 QRegExp regWord(word);
73 regWord.setCaseSensitivity(Qt::CaseInsensitive);
74 regWord.setPatternSyntax(QRegExp::Wildcard);
75 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
76 qDebug()<<"Error: could not open file";
77 return translations.toList();
80 /*read information about dictionary*/
81 QXmlStreamReader dictionaryReader(&dictionaryFile);
82 dictionaryReader.readNextStartElement();
83 if(dictionaryReader.name()=="xdxf") {
84 if(dictionaryReader.attributes().hasAttribute("lang_from"))
85 _langFrom = dictionaryReader.attributes().value("lang_from").toString();
86 if(dictionaryReader.attributes().hasAttribute("lang_to"))
87 _langTo = dictionaryReader.attributes().value("lang_to").toString();
89 dictionaryReader.readNextStartElement();
90 if(dictionaryReader.name()=="full_name")
91 _name=dictionaryReader.readElementText();
92 dictionaryReader.readNextStartElement();
93 if(dictionaryReader.name()=="description")
94 _infoNote=dictionaryReader.readElementText();
99 while(!dictionaryReader.atEnd() && !stopped){
100 dictionaryReader.readNextStartElement();
101 if(dictionaryReader.name()=="ar"){
102 while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
103 dictionaryReader.readNextStartElement();
104 if(!dictionaryReader.atEnd())
105 a = dictionaryReader.readElementText();
106 if(regWord.exactMatch(a) && (i<limit || limit==0)) {
109 foreach(tran,translations)
112 ok=false; /*if key word is in the dictionary more that one */
114 if(ok) /*add key word to list*/
115 translations<<(new TranslationXdxf(a,_infoNote,this));
117 if(i>=limit && limit!=0)
121 this->thread()->yieldCurrentThread();
124 dictionaryFile.close();
125 return translations.toList();
128 QString XdxfPlugin::search(QString key) {
129 QFile dictionaryFile(path);
130 QString resultString("");
131 if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
132 qDebug()<<"Error: could not open file";
135 QXmlStreamReader dictionaryReader(&dictionaryFile);
140 while (!dictionaryReader.atEnd()&& !stopped) {
141 dictionaryReader.readNext();
142 if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
143 if(dictionaryReader.name()=="k") {
144 a = dictionaryReader.readElementText();
149 else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
151 QString temp(dictionaryReader.text().toString());
152 temp.replace("\n","");
154 while(dictionaryReader.name()!="ar"&&
155 !dictionaryReader.atEnd()){
156 dictionaryReader.readNext();
157 temp+=dictionaryReader.text().toString();
160 resultString+=temp.replace("\n","")+"\n";
164 this->thread()->yieldCurrentThread();
167 dictionaryFile.close();
171 void XdxfPlugin::stop() {
175 DictDialog* XdxfPlugin::dictDialog() {
179 void XdxfPlugin::setPath(QString path){
181 _settings->setValue("path",path);
185 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
186 XdxfPlugin *plugin = new XdxfPlugin();
188 plugin->setPath(settings->value("path"));
192 bool XdxfPlugin::isAvailable() const {
196 void XdxfPlugin::setHash(uint _hash)
201 uint XdxfPlugin::hash() const
206 Settings* XdxfPlugin::settings() {
210 bool XdxfPlugin::isCached()
215 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)