*******************************************************************************/
-/*! \file xdxfplugin.cpp
-\author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
+/*! \file stardictplugin.cpp
*/
#include "StarDictPlugin.h"
this, SIGNAL(notify(Notify::NotifyType,QString)));
- _settings->setValue("type","xdxf");
+ _settings->setValue("type","stardict");
_icon = QIcon("/usr/share/mdictionary/xdxf.png");
_wordsCount = -1;
stopped = false;
QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
- if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
- word.indexOf("_")==-1 && word.indexOf("%")==-1)
- word+="*";
-
- if(isCached())
- return searchWordListCache(word,limit);
- return searchWordListFile(word, limit);
-}
-
-
-
-QList<Translation*> StarDictPlugin::searchWordListFile(QString word, int limit) {
QSet<Translation*> translations;
- QFile dictionaryFile(_settings->value("path"));
- word = word.toLower();
- stopped = false;
-
- QRegExp regWord(word);
- regWord.setCaseSensitivity(Qt::CaseInsensitive);
- regWord.setPatternSyntax(QRegExp::Wildcard);
-
- /*check xdxf file exist*/
- if(!QFile::exists(_settings->value("path"))
- || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
- qDebug()<<"Error: could not open file";
- Q_EMIT notify(Notify::Warning,
- QString(tr("XDXF file cannot be read for %1").arg(name())));
- return translations.toList();
- }
-
- QXmlStreamReader reader(&dictionaryFile);
- QString readKey;
- int i=0;
-
- /*search words list*/
- while(!reader.atEnd() && !stopped){
- reader.readNextStartElement();
- if(reader.name()=="ar") {
- while(reader.name()!="k" && !reader.atEnd())
- reader.readNextStartElement();
- if(!reader.atEnd())
- readKey = reader.readElementText();
- if((regWord.exactMatch(readKey)
- || regWord.exactMatch(removeAccents(readKey)))
- && (i<limit || limit==0) && !reader.atEnd()) {
- // qDebug()<<readKey;
- translations<<(new TranslationStarDict(readKey.toLower(),
- _dictionaryInfo,this));
- if(translations.size()==limit && limit!=0)
- break;
- }
- }
- this->thread()->yieldCurrentThread();
- }
- stopped=false;
- dictionaryFile.close();
return translations.toList();
}
+
QString StarDictPlugin::search(QString key) {
- return searchFile(key);
+ return "";
}
-QString StarDictPlugin::searchFile(QString key) {
- QFile dictionaryFile(_settings->value("path"));
- QString resultString("");
- key = key.toLower();
-
- /*check xdxf file exist*/
- if(!QFile::exists(_settings->value("path"))
- || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
- Q_EMIT notify(Notify::Warning,
- QString(tr("XDXF file cannot be read for %1").arg(name())));
- qDebug()<<"Error: could not open file";
- return "";
- }
-
- QXmlStreamReader reader(&dictionaryFile);
- QString readKey;
- bool match =false;
- stopped = false;
-
- /*search translations for word*/
- while (!reader.atEnd()&& !stopped) {
- reader.readNext();
- if(reader.tokenType() == QXmlStreamReader::StartElement) {
- if(reader.name()=="k") {
- readKey = reader.readElementText();
- if(readKey.toLower()==key.toLower())
- match = true;
- }
- }
- if(match) {
- QString temp("");
- while(reader.name()!="ar" && !reader.atEnd()) {
- if(reader.name()!="" && reader.name()!="k") {
- if(reader.tokenType()==QXmlStreamReader::EndElement)
- temp+="</";
- if(reader.tokenType()==QXmlStreamReader::StartElement)
- temp+="<";
- temp+=reader.name().toString();
- if(reader.name().toString()=="c" &&
- reader.tokenType()==QXmlStreamReader::StartElement)
- temp= temp + " c=\"" + reader.attributes().
- value("c").toString() + "\"";
- temp+=">";
- }
- temp+= reader.text().toString().replace("<","<").
- replace(">",">");
- reader.readNext();
- }
- if(temp.at(0)==QChar('\n'))
- temp.remove(0,1);
- resultString+="<key>" + readKey +"</key>";
- resultString+="<t>" + temp + "</t>";
- match=false;
- }
- this->thread()->yieldCurrentThread();
- }
- stopped=false;
- dictionaryFile.close();
- return resultString;
-}
-
-
void StarDictPlugin::stop() {
- //qDebug()<<"stop";
stopped=true;
}
bool StarDictPlugin::isCached() {
- if(_settings->value("cached") == "true")
- return true;
return false;
}
bool StarDictPlugin::setSettings(const Settings *settings) {
if(settings) {
- bool isPathChange=false;
- QString oldPath = _settings->value("path");
- Settings *oldSettings = new Settings ;
-
- if(oldPath != settings->value("path")) {
- if(oldPath!="" && _settings->value("cache_path")!="")
- clean();
- isPathChange=true;
- }
-
- foreach(QString key, _settings->keys())
- oldSettings->setValue(key, _settings->value(key));
-
- foreach(QString key, settings->keys()) {
- if(key != "generateCache")
- _settings->setValue(key, settings->value(key));
- }
-
- if(!getDictionaryInfo()) {
- Q_EMIT notify(Notify::Warning,
- QString(tr("XDXF file is in wrong format")));
- qDebug()<<"Error: xdxf file is in wrong format";
- delete _settings;
- _settings=oldSettings;
- return false;
- }
-
- if(isPathChange) {
- _wordsCount=0;
- if(oldPath!="")
- _settings->setValue("cached","false");
- }
-
- if((_settings->value("cached") == "false" ||
- _settings->value("cached").isEmpty()) &&
- settings->value("generateCache") == "true") {
- clean();
- }
-
- else if (settings->value("generateCache") == "false") {
- _settings->setValue("cached", "false");
- }
- }
- else
+
+ } else
return false;
Q_EMIT settingsChanged();
return true;
if(!QFile::exists(_settings->value("path"))
|| !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
Q_EMIT notify(Notify::Warning,
- QString(tr("XDXF dictionary cannot be read from file")));
- qDebug()<<"Error: could not open file";
+ QString(tr("StarDict dictionary cannot be read from file")));
+ qDebug()<<"Error: could not open the file";
return false;
}
- bool okFormat=false;
- QXmlStreamReader reader(&dictionaryFile);
- reader.readNextStartElement();
- if(reader.name()=="xdxf") {
- okFormat=true;
- if(reader.attributes().hasAttribute("lang_from"))
- _langFrom = reader.attributes().value("lang_from").toString();
- if(reader.attributes().hasAttribute("lang_to"))
- _langTo = reader.attributes().value("lang_to").toString();
- }
- reader.readNextStartElement();
- if(reader.name()=="full_name")
- _name=reader.readElementText();
- else
- qDebug()<<"no full_name";
- reader.readNextStartElement();
- if(reader.name()=="description")
- _infoNote=reader.readElementText();
- else
- qDebug()<<"no description";
-
- _dictionaryInfo= _name + " [" + _langFrom + "-"
- + _langTo + "]";
-
- dictionaryFile.close();
- if(okFormat)
- return true;
return false;
}
int StarDictPlugin::countWords() {
- if(_wordsCount>0)
- return _wordsCount;
- QFile dictionaryFile(_settings->value("path"));
- if(!QFile::exists(_settings->value("path"))
- || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
- Q_EMIT notify(Notify::Warning,
- QString(tr("XDXF file cannot be read for %1 dictionary")
- .arg(name())));
- qDebug()<<"Error: could not open file";
- return -1;
- }
-
- dictionaryFile.seek(0);
-
- long wordsCount = 0;
-
- QString line;
- while(!dictionaryFile.atEnd()) {
- line = dictionaryFile.readLine();
- if(line.contains("<k>")) {
- wordsCount++;
- }
- }
- _wordsCount = wordsCount;
- dictionaryFile.close();
- return wordsCount;
+ return 0;
}