- /*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);
- }