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 /*! \file xdxfplugin.cpp
23 \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
26 #include "StarDictPlugin.h"
28 #include "../../include/Notify.h"
29 #include <QTranslator>
30 #include <QCoreApplication>
33 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
34 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
35 _settings = new Settings();
36 _dictDialog = new StarDictDialog(this, this);
38 connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
39 this, SIGNAL(notify(Notify::NotifyType,QString)));
42 _settings->setValue("type","xdxf");
43 _icon = QIcon("/usr/share/mdictionary/xdxf.png");
50 void StarDictPlugin::retranslate() {
51 QString locale = QLocale::system().name();
53 QTranslator *translator = new QTranslator(this);
55 if(!translator->load(":/xdxf/translations/" + locale)) {
56 translator->load(":/xdxf/translations/en_US");
58 QCoreApplication::installTranslator(translator);
62 StarDictPlugin::~StarDictPlugin() {
68 QString StarDictPlugin::langFrom() const {
73 QString StarDictPlugin::langTo() const {
78 QString StarDictPlugin::name() const {
83 QString StarDictPlugin::type() const {
84 return QString("stardict");
88 QString StarDictPlugin::infoNote() const {
93 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
94 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
95 word.indexOf("_")==-1 && word.indexOf("%")==-1)
99 return searchWordListCache(word,limit);
100 return searchWordListFile(word, limit);
105 QList<Translation*> StarDictPlugin::searchWordListFile(QString word, int limit) {
106 QSet<Translation*> translations;
107 QFile dictionaryFile(_settings->value("path"));
108 word = word.toLower();
111 QRegExp regWord(word);
112 regWord.setCaseSensitivity(Qt::CaseInsensitive);
113 regWord.setPatternSyntax(QRegExp::Wildcard);
115 /*check xdxf file exist*/
116 if(!QFile::exists(_settings->value("path"))
117 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
118 qDebug()<<"Error: could not open file";
119 Q_EMIT notify(Notify::Warning,
120 QString(tr("XDXF file cannot be read for %1").arg(name())));
121 return translations.toList();
124 QXmlStreamReader reader(&dictionaryFile);
128 /*search words list*/
129 while(!reader.atEnd() && !stopped){
130 reader.readNextStartElement();
131 if(reader.name()=="ar") {
132 while(reader.name()!="k" && !reader.atEnd())
133 reader.readNextStartElement();
135 readKey = reader.readElementText();
136 if((regWord.exactMatch(readKey)
137 || regWord.exactMatch(removeAccents(readKey)))
138 && (i<limit || limit==0) && !reader.atEnd()) {
139 // qDebug()<<readKey;
140 translations<<(new TranslationStarDict(readKey.toLower(),
141 _dictionaryInfo,this));
142 if(translations.size()==limit && limit!=0)
146 this->thread()->yieldCurrentThread();
149 dictionaryFile.close();
150 return translations.toList();
154 QString StarDictPlugin::search(QString key) {
155 return searchFile(key);
160 QString StarDictPlugin::searchFile(QString key) {
161 QFile dictionaryFile(_settings->value("path"));
162 QString resultString("");
165 /*check xdxf file exist*/
166 if(!QFile::exists(_settings->value("path"))
167 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
168 Q_EMIT notify(Notify::Warning,
169 QString(tr("XDXF file cannot be read for %1").arg(name())));
170 qDebug()<<"Error: could not open file";
174 QXmlStreamReader reader(&dictionaryFile);
179 /*search translations for word*/
180 while (!reader.atEnd()&& !stopped) {
182 if(reader.tokenType() == QXmlStreamReader::StartElement) {
183 if(reader.name()=="k") {
184 readKey = reader.readElementText();
185 if(readKey.toLower()==key.toLower())
191 while(reader.name()!="ar" && !reader.atEnd()) {
192 if(reader.name()!="" && reader.name()!="k") {
193 if(reader.tokenType()==QXmlStreamReader::EndElement)
195 if(reader.tokenType()==QXmlStreamReader::StartElement)
197 temp+=reader.name().toString();
198 if(reader.name().toString()=="c" &&
199 reader.tokenType()==QXmlStreamReader::StartElement)
200 temp= temp + " c=\"" + reader.attributes().
201 value("c").toString() + "\"";
204 temp+= reader.text().toString().replace("<","<").
208 if(temp.at(0)==QChar('\n'))
210 resultString+="<key>" + readKey +"</key>";
211 resultString+="<t>" + temp + "</t>";
214 this->thread()->yieldCurrentThread();
217 dictionaryFile.close();
222 void StarDictPlugin::stop() {
228 DictDialog* StarDictPlugin::dictDialog() {
233 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
234 StarDictPlugin *plugin = new StarDictPlugin();
236 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
237 this, SIGNAL(notify(Notify::NotifyType,QString)));
239 ((StarDictDialog*)plugin->dictDialog())->setLastDialogParent(_dictDialog->lastDialogParent());
243 if(settings && plugin->setSettings(settings)) {
245 disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
246 this, SIGNAL(notify(Notify::NotifyType,QString)));
250 disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
251 this, SIGNAL(notify(Notify::NotifyType,QString)));
258 bool StarDictPlugin::isAvailable() const {
263 Settings* StarDictPlugin::settings() {
268 bool StarDictPlugin::isCached() {
269 if(_settings->value("cached") == "true")
275 bool StarDictPlugin::setSettings(const Settings *settings) {
277 bool isPathChange=false;
278 QString oldPath = _settings->value("path");
279 Settings *oldSettings = new Settings ;
281 if(oldPath != settings->value("path")) {
282 if(oldPath!="" && _settings->value("cache_path")!="")
287 foreach(QString key, _settings->keys())
288 oldSettings->setValue(key, _settings->value(key));
290 foreach(QString key, settings->keys()) {
291 if(key != "generateCache")
292 _settings->setValue(key, settings->value(key));
295 if(!getDictionaryInfo()) {
296 Q_EMIT notify(Notify::Warning,
297 QString(tr("XDXF file is in wrong format")));
298 qDebug()<<"Error: xdxf file is in wrong format";
300 _settings=oldSettings;
307 _settings->setValue("cached","false");
310 if((_settings->value("cached") == "false" ||
311 _settings->value("cached").isEmpty()) &&
312 settings->value("generateCache") == "true") {
316 else if (settings->value("generateCache") == "false") {
317 _settings->setValue("cached", "false");
322 Q_EMIT settingsChanged();
327 bool StarDictPlugin::getDictionaryInfo() {
328 QFile dictionaryFile(_settings->value("path"));
329 if(!QFile::exists(_settings->value("path"))
330 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
331 Q_EMIT notify(Notify::Warning,
332 QString(tr("XDXF dictionary cannot be read from file")));
333 qDebug()<<"Error: could not open file";
338 QXmlStreamReader reader(&dictionaryFile);
339 reader.readNextStartElement();
340 if(reader.name()=="xdxf") {
342 if(reader.attributes().hasAttribute("lang_from"))
343 _langFrom = reader.attributes().value("lang_from").toString();
344 if(reader.attributes().hasAttribute("lang_to"))
345 _langTo = reader.attributes().value("lang_to").toString();
347 reader.readNextStartElement();
348 if(reader.name()=="full_name")
349 _name=reader.readElementText();
351 qDebug()<<"no full_name";
352 reader.readNextStartElement();
353 if(reader.name()=="description")
354 _infoNote=reader.readElementText();
356 qDebug()<<"no description";
358 _dictionaryInfo= _name + " [" + _langFrom + "-"
361 dictionaryFile.close();
368 QIcon* StarDictPlugin::icon() {
373 int StarDictPlugin::countWords() {
376 QFile dictionaryFile(_settings->value("path"));
377 if(!QFile::exists(_settings->value("path"))
378 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
379 Q_EMIT notify(Notify::Warning,
380 QString(tr("XDXF file cannot be read for %1 dictionary")
382 qDebug()<<"Error: could not open file";
386 dictionaryFile.seek(0);
391 while(!dictionaryFile.atEnd()) {
392 line = dictionaryFile.readLine();
393 if(line.contains("<k>")) {
397 _wordsCount = wordsCount;
398 dictionaryFile.close();
404 void StarDictPlugin::clean() {
409 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)