_wordsCount = -1;
_settings = new Settings();
_dictDialog = new XdxfDictDialog(this, this);
- _settings->setValue("type","xdxf");
- if(isCached())
- _settings->setValue("cached","true");
- else
- _settings->setValue("cached","false");
+ cachingDialog = new XdxfCachingDialog(this);
+ connect(cachingDialog, SIGNAL(cancelCaching()),
+ this, SLOT(stop()));
+
+ _settings->setValue("type","xdxf");
stopped = false;
}
QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
- if(_settings->value("cached") == "true")
+ //if(_settings->value("cached") == "true")
+ 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*> XdxfPlugin::searchWordListCache(QString word, int limit) {
- qDebug() << "search cache";
QSet<Translation*> translations;
QString cacheFilePath = _settings->value("cache_path");
db.setDatabaseName(cacheFilePath);
}
stopped = false;
- if(word.indexOf("*")==-1)
+ if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
word+="%";
- qDebug() << word;
word = word.replace("*", "%");
- qDebug() << word;
+ word = word.replace("?", "_");
word = removeAccents(word);
qDebug() << word;
QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
- qDebug() << "search file";
QSet<Translation*> translations;
QFile dictionaryFile(path);
word = removeAccents(word);
stopped = false;
- if(word.indexOf("*")==-1)
- word+="*";
QRegExp regWord(word);
regWord.setCaseSensitivity(Qt::CaseInsensitive);
regWord.setPatternSyntax(QRegExp::Wildcard);
if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
bool ok=true;
Translation *tran;
- foreach(tran,translations)
- {
+ foreach(tran,translations) {
if(tran->key()==a)
ok=false; /*if key word is in the dictionary more that one */
}
}
QString XdxfPlugin::search(QString key) {
- if(_settings->value("cached") == "true")
+// if(_settings->value("cached") == "true")
+ if(isCached())
return searchCache(key);
return searchFile(key);
}
QString XdxfPlugin::searchCache(QString key) {
- qDebug() << "search cache";
QString result;
QString cacheFilePath = _settings->value("cache_path");
db.setDatabaseName(cacheFilePath);
}
else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
if(match) {
- QString temp(dictionaryReader.text().toString());
+ /* QString temp(dictionaryReader.text().toString());
+ if(temp=="\n")
+ temp=dictionaryReader.readElementText();
temp.replace("\n","");
if(temp == ""){
+ int i=0;
while(dictionaryReader.name()!="ar"&&
!dictionaryReader.atEnd()){
dictionaryReader.readNext();
+ if(dictionaryReader.name()!="" &&
+ dictionaryReader.name()!="ar") {
+ if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
+ temp+=tr("</");
+ if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
+ temp+=tr("<");
+ qDebug()<<dictionaryReader.tokenType();
+ temp=temp+dictionaryReader.name().toString() + tr(">");
+ i++;
+ }
temp+=dictionaryReader.text().toString();
}
}
resultString+=temp.replace("\n","")+"\n";
match=false;
+*/
+ QString temp("");
+ while(dictionaryReader.name()!="ar" && !dictionaryReader.atEnd()) {
+ if(dictionaryReader.name()!="") {
+ if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
+ temp+=tr("</");
+ if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
+ temp+=tr("<");
+ temp=temp+dictionaryReader.name().toString() + tr(">");
+ }
+ temp+= dictionaryReader.text().toString();
+ dictionaryReader.readNext();
+ }
+ resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
+ match=false;
}
}
this->thread()->yieldCurrentThread();
void XdxfPlugin::setPath(QString path){
this->path=path;
_settings->setValue("path",path);
- getDictionaryInfo();
+ //getDictionaryInfo();
}
XdxfPlugin *plugin = new XdxfPlugin();
if(settings){
plugin->setPath(settings->value("path"));
+
QStringList list = settings->keys();
foreach(QString key, list)
plugin->settings()->setValue(key, settings->value(key));
- //if(plugin->settings()->value("cached") != "true")
+
+
plugin->db_name = plugin->_settings->value("type")
+ plugin->_settings->value("path");
plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
- plugin->makeCache("");
+
+ if(settings->value("cached").isEmpty() &&
+ settings->value("generateCache") == "true") {
+ plugin->makeCache("");
+ }
}
+
+ plugin->getDictionaryInfo();
return plugin;
}
return true;
}
-void XdxfPlugin::setHash(uint _hash)
-{
+void XdxfPlugin::setHash(uint _hash) {
this->_hash=_hash;
}
-uint XdxfPlugin::hash() const
-{
+uint XdxfPlugin::hash() const {
return _hash;
}
return _settings;
}
-bool XdxfPlugin::isCached()
-{
+bool XdxfPlugin::isCached() {
+ if(_settings->value("cached") == "true")
+ return true;
return false;
}
void XdxfPlugin::setSettings(Settings *settings) {
- _settings = settings;
- setPath(_settings->value("path"));
+
+ QString oldPath = _settings->value("path");
+ if(oldPath != settings->value("path")) {
+ setPath(settings->value("path"));
+ }
+
+ if((_settings->value("cached") == "false" ||
+ _settings->value("cached").isEmpty()) &&
+ settings->value("generateCache") == "true") {
+ makeCache("");
+ }
+ else {
+ _settings->setValue("cached", "false");
+ }
+
emit settingsChanged();
}
!normalized[i].isSpace() &&
!normalized[i].isDigit() &&
normalized[i] != '*' &&
- normalized[i] != '%') {
+ normalized[i] != '%' &&
+ normalized[i] != '_' &&
+ normalized[i] != '?' ) {
normalized.remove(i,1);
}
}
bool XdxfPlugin::makeCache(QString dir) {
+ cachingDialog->setVisible(true);
+ QCoreApplication::processEvents();
+ stopped = false;
QFileInfo dictFileN(_settings->value("path"));
QString cachePathN;
cachePathN = QDir::homePath() + "/.mdictionary/"
QFile dictionaryFile(dictFileN.filePath());
- qDebug() << dictFileN.path();
if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
return 0;
}
- qDebug() << "OLE";
QXmlStreamReader reader(&dictionaryFile);
-
db.setDatabaseName(cachePathN);
if(!db.open()) {
- qDebug() << "Database error" << endl;
+ qDebug() << "Database error" << db.lastError().text() << endl;
return false;
}
+ QCoreApplication::processEvents();
QSqlQuery cur(db);
cur.exec("PRAGMA synchronous = 0");
cur.exec("drop table dict");
+ QCoreApplication::processEvents();
cur.exec("create table dict(word text ,translation text)");
int counter = 0;
cur.exec("BEGIN;");
timer.start();
countWords();
+ int lastProg = -1;
+
counter=0;
- while (!reader.atEnd()) {
+ while (!reader.atEnd() && !stopped) {
+ QCoreApplication::processEvents();
reader.readNext();
if(reader.tokenType() == QXmlStreamReader::StartElement) {
}
else if(reader.tokenType() == QXmlStreamReader::Characters) {
if(match) {
- QString temp(reader.text().toString());
- temp.replace("\n","");
- if(temp == ""){
- while(reader.name()!="ar"&&
- !reader.atEnd()){
- reader.readNext();
- temp+=reader.text().toString();
+ QString temp("");
+ while(reader.name()!="ar" && !reader.atEnd()) {
+ if(reader.name()!="") {
+ if(reader.tokenType()==QXmlStreamReader::EndElement)
+ temp+=tr("</");
+ if(reader.tokenType()==QXmlStreamReader::StartElement)
+ temp+=tr("<");
+ temp=temp+reader.name().toString() + tr(">");
}
+ temp+= reader.text().toString();
+ reader.readNext();
}
+ temp=tr("<t>") + temp.replace("\n","") + tr("</t>");
match = false;
cur.prepare("insert into dict values(?,?)");
cur.addBindValue(a);
cur.exec();
counter++;
int prog = counter*100/_wordsCount;
- if(prog % 5 == 0)
- Q_EMIT update(prog);
+ if(prog % 5 == 0 && lastProg != prog) {
+ Q_EMIT updateCachingProgress(prog,
+ timer.restart());
+ lastProg = prog;
+ }
}
}
}
- qDebug()<<counter;
cur.exec("END;");
cur.exec("select count(*) from dict");
+
+ countWords();
+ cachingDialog->setVisible(false);
+
if(!cur.next() || countWords() != cur.value(0).toInt())
return false;
_settings->setValue("cache_path", cachePathN);
_settings->setValue("cached", "true");
+
return true;
}