\author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
*/
-#include "xdxfplugin.h"
+#include "StarDictPlugin.h"
#include <QDebug>
#include "../../include/Notify.h"
+#include <QTranslator>
+#include <QCoreApplication>
+#include <QThread>
-XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
+StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
_langFrom(""), _langTo(""),_name(""), _infoNote("") {
_settings = new Settings();
- _dictDialog = new XdxfDictDialog(this, this);
+ _dictDialog = new StarDictDialog(this, this);
connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
this, SIGNAL(notify(Notify::NotifyType,QString)));
initAccents();
}
-void XdxfPlugin::retranslate() {
+void StarDictPlugin::retranslate() {
QString locale = QLocale::system().name();
QTranslator *translator = new QTranslator(this);
}
-XdxfPlugin::~XdxfPlugin() {
+StarDictPlugin::~StarDictPlugin() {
delete _settings;
delete _dictDialog;
}
-QString XdxfPlugin::langFrom() const {
+QString StarDictPlugin::langFrom() const {
return _langFrom;
}
-QString XdxfPlugin::langTo() const {
+QString StarDictPlugin::langTo() const {
return _langTo;
}
-QString XdxfPlugin::name() const {
+QString StarDictPlugin::name() const {
return _name;
}
-QString XdxfPlugin::type() const {
- return QString("xdxf");
+QString StarDictPlugin::type() const {
+ return QString("stardict");
}
-QString XdxfPlugin::infoNote() const {
+QString StarDictPlugin::infoNote() const {
return _infoNote;
}
-QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
+QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
word.indexOf("_")==-1 && word.indexOf("%")==-1)
word+="*";
}
-QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
- QSet<Translation*> translations;
- QString cacheFilePath = _settings->value("cache_path");
-
- db.setDatabaseName(cacheFilePath);
- if(!QFile::exists(cacheFilePath) || !db.open()) {
- qDebug() << "Database error" << db.lastError().text() << endl;
- Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
- "opened for %1 dictionary. Searching in XDXF file. "
- "You may want to recache.").arg(name())));
- _settings->setValue("cached","false");
- return searchWordListFile(word, limit);
- }
- stopped = false;
- word = word.toLower();
- word = word.replace("*", "%");
- word = word.replace("?", "_");
-
- QSqlQuery cur(db);
- if(limit !=0)
- cur.prepare("select word from dict where word like ? or normalized "
- "like ? limit ?");
- else
- cur.prepare("select word from dict where word like ? or normalized "
- "like ?");
- cur.addBindValue(word);
- cur.addBindValue(word);
- if(limit !=0)
- cur.addBindValue(limit);
- cur.exec();
-
- while(cur.next() && (translations.size()<limit || limit==0)) {
- translations.insert(new TranslationXdxf(
- cur.value(0).toString(),
- _dictionaryInfo, this));
- }
- db.close();
- return translations.toList();
-}
-
-QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
+QList<Translation*> StarDictPlugin::searchWordListFile(QString word, int limit) {
QSet<Translation*> translations;
QFile dictionaryFile(_settings->value("path"));
word = word.toLower();
|| regWord.exactMatch(removeAccents(readKey)))
&& (i<limit || limit==0) && !reader.atEnd()) {
// qDebug()<<readKey;
- translations<<(new TranslationXdxf(readKey.toLower(),
+ translations<<(new TranslationStarDict(readKey.toLower(),
_dictionaryInfo,this));
if(translations.size()==limit && limit!=0)
break;
}
-QString XdxfPlugin::search(QString key) {
- if(isCached())
- return searchCache(key);
+QString StarDictPlugin::search(QString key) {
return searchFile(key);
}
-QString XdxfPlugin::searchCache(QString key) {
- QString result("");
- QString cacheFilePath = _settings->value("cache_path");
- db.setDatabaseName(cacheFilePath);
- key = key.toLower();
-
- if(!QFile::exists(cacheFilePath) || !db.open()) {
- qDebug() << "Database error" << db.lastError().text() << endl;
- Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
- "opened for %1 dictionary. Searching in XDXF file. "
- "You may want to recache.").arg(name())));
- _settings->setValue("cached","false");
- return searchFile(key);
- }
-
- QSqlQuery cur(db);
-
- cur.prepare("select translation from dict where word like ?");
- cur.addBindValue(key);
- cur.exec();
- while(cur.next())
- result += cur.value(0).toString();
-
- db.close();
-
- return result;
-
-}
-
-QString XdxfPlugin::searchFile(QString key) {
+QString StarDictPlugin::searchFile(QString key) {
QFile dictionaryFile(_settings->value("path"));
QString resultString("");
key = key.toLower();
}
-void XdxfPlugin::stop() {
+void StarDictPlugin::stop() {
//qDebug()<<"stop";
stopped=true;
}
-DictDialog* XdxfPlugin::dictDialog() {
+DictDialog* StarDictPlugin::dictDialog() {
return _dictDialog;
}
-CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
- XdxfPlugin *plugin = new XdxfPlugin();
+CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
+ StarDictPlugin *plugin = new StarDictPlugin();
connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
this, SIGNAL(notify(Notify::NotifyType,QString)));
- ((XdxfDictDialog*)plugin->dictDialog())->setLastDialogParent(_dictDialog->lastDialogParent());
+ ((StarDictDialog*)plugin->dictDialog())->setLastDialogParent(_dictDialog->lastDialogParent());
}
-bool XdxfPlugin::isAvailable() const {
+bool StarDictPlugin::isAvailable() const {
return true;
}
-Settings* XdxfPlugin::settings() {
+Settings* StarDictPlugin::settings() {
return _settings;
}
-bool XdxfPlugin::isCached() {
+bool StarDictPlugin::isCached() {
if(_settings->value("cached") == "true")
return true;
return false;
}
-bool XdxfPlugin::setSettings(const Settings *settings) {
+bool StarDictPlugin::setSettings(const Settings *settings) {
if(settings) {
bool isPathChange=false;
QString oldPath = _settings->value("path");
_wordsCount=0;
if(oldPath!="")
_settings->setValue("cached","false");
- if(_settings->value("cached")=="true"
- && _settings->value("cache_path")!="") {
- db_name = _settings->value("type")
- + _settings->value("cache_path");
- db = QSqlDatabase::addDatabase("QSQLITE",db_name);
- }
}
if((_settings->value("cached") == "false" ||
_settings->value("cached").isEmpty()) &&
settings->value("generateCache") == "true") {
clean();
- makeCache("");
}
else if (settings->value("generateCache") == "false") {
}
-bool XdxfPlugin::getDictionaryInfo() {
+bool StarDictPlugin::getDictionaryInfo() {
QFile dictionaryFile(_settings->value("path"));
if(!QFile::exists(_settings->value("path"))
|| !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
}
-QIcon* XdxfPlugin::icon() {
+QIcon* StarDictPlugin::icon() {
return &_icon;
}
-int XdxfPlugin::countWords() {
+int StarDictPlugin::countWords() {
if(_wordsCount>0)
return _wordsCount;
QFile dictionaryFile(_settings->value("path"));
}
-bool XdxfPlugin::makeCache(QString) {
-
- XdxfCachingDialog d(_dictDialog->lastDialogParent());
-
-// qDebug()<<_dictDialog->lastDialogParent();
- connect(&d, SIGNAL(cancelCaching()),
- this, SLOT(stop()));
- connect(this, SIGNAL(updateCachingProgress(int,int)),
- &d, SLOT(updateCachingProgress(int,int)));
-
- d.show();
-
- QCoreApplication::processEvents();
- QFileInfo dictFileN(_settings->value("path"));
- QString cachePathN;
- stopped = false;
-
- /*create cache file name*/
- int i=0;
- do {
- cachePathN = QDir::homePath() + "/.mdictionary/"
- + dictFileN.completeBaseName()+"."
- +QString::number(i) + ".cache";
- i++;
- } while(QFile::exists(cachePathN));
-
- db_name = _settings->value("type") + cachePathN;
- db = QSqlDatabase::addDatabase("QSQLITE",db_name);
-
- /*checke errors (File open and db open)*/
- QFile dictionaryFile(dictFileN.filePath());
- if (!QFile::exists(_settings->value("path"))
- || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
- Q_EMIT updateCachingProgress(100, 0);
- Q_EMIT notify(Notify::Warning,
- QString(tr("XDXF file cannot be read for %1 dictionary")
- .arg(name())));
- return 0;
- }
- QXmlStreamReader reader(&dictionaryFile);
- db.setDatabaseName(cachePathN);
- if(!db.open()) {
- qDebug() << "Database error" << db.lastError().text() << endl;
- Q_EMIT updateCachingProgress(100, 0);
- Q_EMIT notify(Notify::Warning, QString(tr("Cache database cannot be "
- "opened for %1 dictionary. Searching in XDXF file. "
- "You may want to recache.").arg(name())));
- return false;
- }
+void StarDictPlugin::clean() {
- /*inicial sqlQuery*/
- QCoreApplication::processEvents();
- QSqlQuery cur(db);
- cur.exec("PRAGMA synchronous = 0");
- cur.exec("drop table dict");
- QCoreApplication::processEvents();
- cur.exec("create table dict(word text, normalized text ,translation text)");
- int counter = 0;
- cur.exec("BEGIN;");
-
- QString readKey;
- bool match = false;
- QTime timer;
- timer.start();
- countWords();
- int lastProg = -1;
- _settings->setValue("strip_accents", "true");
- counter=0;
-
- /*add all words to db*/
- while (!reader.atEnd() && !stopped) {
-
- QCoreApplication::processEvents();
- reader.readNext();
- if(reader.tokenType() == QXmlStreamReader::StartElement) {
- if(reader.name()=="k"){
- readKey = reader.readElementText();
- 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);
- temp="<key>" + readKey + "</key>" + "<t>" + temp+ "</t>";
- match=false;
- cur.prepare("insert into dict values(?,?,?)");
- cur.addBindValue(readKey.toLower());
- cur.addBindValue(removeAccents(readKey).toLower());
- cur.addBindValue(temp);
- cur.exec();
- counter++;
- int prog = counter*100/_wordsCount;
- if(prog % 2 == 0 && lastProg != prog) {
- Q_EMIT updateCachingProgress(prog,timer.restart());
- lastProg = prog;
- }
- }
- }
- cur.exec("END;");
- cur.exec("select count(*) from dict");
-
- /*checke errors (wrong number of added words)*/
- countWords();
- if(!cur.next() || countWords() != cur.value(0).toInt()) {
- Q_EMIT updateCachingProgress(100, timer.restart());
- Q_EMIT notify(Notify::Warning,
- QString(tr("Database caching error, please try again.")));
- db.close();
- _settings->setValue("cache_path", cachePathN);
- if(stopped)
- clean();
- _settings->setValue("cache_path","");
- return false;
- }
-
- _settings->setValue("cache_path", cachePathN);
- _settings->setValue("cached", "true");
-
- disconnect(&d, SIGNAL(cancelCaching()),
- this, SLOT(stop()));
- disconnect(this, SIGNAL(updateCachingProgress(int,int)),
- &d, SLOT(updateCachingProgress(int,int)));
- db.close();
- return true;
-}
-
-void XdxfPlugin::clean() {
- if(QFile::exists(_settings->value("cache_path"))) {
- QFile(_settings->value("cache_path")).remove();
- QSqlDatabase::removeDatabase(db_name);
- }
}
-Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)
+Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)