#include "../../../includes/Notify.h"
XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
- _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
- _type(tr("xdxf")), _infoNote(tr("")) {
- _wordsCount = -1;
+ _langFrom(""), _langTo(""),_name(""), _infoNote("") {
_settings = new Settings();
_dictDialog = new XdxfDictDialog(this);
cachingDialog = new XdxfCachingDialog(this);
+ _settings->setValue("type","xdxf");
+ _icon = QIcon(":/icons/xdxf.png");
+ _wordsCount = -1;
+ stopped = false;
connect(cachingDialog, SIGNAL(cancelCaching()),
this, SLOT(stop()));
connect(this, SIGNAL(updateCachingProgress(int,int)),
- cachingDialog, SLOT(updateCachingProgress(int,int)));
-
- _settings->setValue("type","xdxf");
-
- stopped = false;
-
- _icon = QIcon(":/icons/xdxf.png");
- initAccents();
+ cachingDialog, SLOT(updateCachingProgress(int,int)));
}
delete cachingDialog;
}
+
QString XdxfPlugin::langFrom() const {
return _langFrom;
}
+
QString XdxfPlugin::langTo() const {
return _langTo;
}
+
QString XdxfPlugin::name() const {
return _name;
}
+
QString XdxfPlugin::type() const {
- return _type;
+ return QString("xdxf");
}
+
QString XdxfPlugin::infoNote() const {
return _infoNote;
}
+
QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
- if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
- && word.indexOf("%")==-1)
+ if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
+ word.indexOf("_")==-1 && word.indexOf("%")==-1)
word+="*";
if(isCached())
return searchWordListFile(word, limit);
}
+
QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
int i=0;
QSet<Translation*> translations;
-
QString cacheFilePath = _settings->value("cache_path");
+
+// QSqlDatabase::removeDatabase(cacheFilePath);
db.setDatabaseName(cacheFilePath);
- if(!db.open()) {
+ 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("*", "%");
if(limit !=0)
cur.addBindValue(limit);
cur.exec();
+
bool in = false;
while(cur.next() && (i<limit || limit==0 ) ) {
in = true;
}
QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
- QTime time;
QSet<Translation*> translations;
- QFile dictionaryFile(path);
+
+ QFile dictionaryFile(_settings->value("path"));
word = word.toLower();
- //word = removeAccents(word);
stopped = false;
QRegExp regWord(word);
regWord.setCaseSensitivity(Qt::CaseInsensitive);
regWord.setPatternSyntax(QRegExp::Wildcard);
- if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+ 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();
}
+
QString XdxfPlugin::search(QString key) {
-// if(_settings->value("cached") == "true")
if(isCached())
return searchCache(key);
return searchFile(key);
}
+
QString XdxfPlugin::searchCache(QString key) {
QString result("");
QString cacheFilePath = _settings->value("cache_path");
db.setDatabaseName(cacheFilePath);
key = key.toLower();
- if(!db.open()) {
+ 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);
}
QString XdxfPlugin::searchFile(QString key) {
key = key.toLower();
- QFile dictionaryFile(path);
+ QFile dictionaryFile(_settings->value("path"));
QString resultString("");
- if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+ 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 resultString;
}
+
void XdxfPlugin::stop() {
stopped=true;
}
+
DictDialog* XdxfPlugin::dictDialog() {
return _dictDialog;
}
-void XdxfPlugin::setPath(QString path){
- if(this->path!=path && this->path!="" && _settings->value("cache_path")!="")
- clean();
- this->path=path;
- _settings->setValue("path",path);
-}
CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
XdxfPlugin *plugin = new XdxfPlugin();
if(settings){
- plugin->setPath(settings->value("path"));
- QStringList list = settings->keys();
-
- plugin->db_name = plugin->_settings->value("type")
- + plugin->_settings->value("path");
- plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
-
- if(settings->value("cached").isEmpty() &&
- settings->value("generateCache") == "true") {
- plugin->makeCache("");
- }
- foreach(QString key, list)
- if(key != "generateCache")
- plugin->settings()->setValue(key, settings->value(key));
- delete settings;
+ plugin->setSettings(settings);
}
- plugin->getDictionaryInfo();
return plugin;
}
+
bool XdxfPlugin::isAvailable() const {
return true;
}
}
-void XdxfPlugin::setSettings(Settings *settings) {
- if(settings)
- {
- qDebug()<<"\n\n ustawienie ustawien";
+void XdxfPlugin::setSettings(const Settings *settings) {
+ if(settings) {
bool isPathChange=false;
QString oldPath = _settings->value("path");
if(oldPath != settings->value("path")) {
- setPath(settings->value("path"));
+ if(oldPath!="" && _settings->value("cache_path")!="")
+ clean();
isPathChange=true;
}
}
if(isPathChange) {
- _settings->setValue("cached","false");
_wordsCount=0;
+ if(oldPath!="") {
+ _settings->setValue("cached","false");
+ QSqlDatabase::removeDatabase(db_name);
+ }
+ db_name = _settings->value("type") + _settings->value("path");
+ db = QSqlDatabase::addDatabase("QSQLITE",db_name);
}
if((_settings->value("cached") == "false" ||
settings->value("generateCache") == "true") {
makeCache("");
}
- else if (settings->value("generateCache") != "true") {
+
+ else if (settings->value("generateCache") == "false") {
_settings->setValue("cached", "false");
}
getDictionaryInfo();
- delete settings;
}
Q_EMIT settingsChanged();
}
void XdxfPlugin::getDictionaryInfo() {
- QFile dictionaryFile(path);
+ QFile dictionaryFile(_settings->value("path"));
if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
Q_EMIT notify(Notify::Warning,
QString(tr("Xdxf file cannot be read dictionary")));
QString format = "png";
QString initialPath = QDir::currentPath() + "/xdxf." + format;
- _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-" + _langTo + "] (" + _type + ")";
+ _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-"
+ + _langTo + "] ( xdxf )";
dictionaryFile.close();
}
return &_icon;
}
+
int XdxfPlugin::countWords() {
if(_wordsCount>0)
return _wordsCount;
- QFile dictionaryFile(path);
+ QFile dictionaryFile(_settings->value("path"));
if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
Q_EMIT notify(Notify::Warning,
QString(tr("Xdxf file cannot be read for %1 dictionary")
return wordsCount;
}
+
bool XdxfPlugin::makeCache(QString) {
cachingDialog->setVisible(true);
QCoreApplication::processEvents();
stopped = false;
QFileInfo dictFileN(_settings->value("path"));
QString cachePathN;
- cachePathN = QDir::homePath() + "/.mdictionary/"
- + dictFileN.completeBaseName() + ".cache";
+ int i=0;
+ do {
+ cachePathN = QDir::homePath() + "/.mdictionary/"
+ + dictFileN.completeBaseName()+"."+QString::number(i)+ ".cache";
+ i++;
+ } while(QFile::exists(cachePathN));
QFile dictionaryFile(dictFileN.filePath());
-
if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
Q_EMIT updateCachingProgress(100, 0);
Q_EMIT notify(Notify::Warning,
countWords();
cachingDialog->setVisible(false);
- if(!cur.next() || countWords() != cur.value(0).toInt())
- {
- qDebug()<<"tu??";
+ 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 againg.")));
}
-
void XdxfPlugin::clean() {
- if(settings()->value("cached") == "true")
- QFile(settings()->value("cache_path")).remove();
+ if(_settings->value("cached") == "true")
+ QFile(_settings->value("cache_path")).remove();
}
Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)