+QIcon* XdxfPlugin::icon() {
+ return &_icon;
+}
+
+int XdxfPlugin::countWords() {
+ if(_wordsCount > 0)
+ return _wordsCount;
+
+ QFile dictionaryFile(path);
+ if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+ 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;
+}
+
+
+
+bool XdxfPlugin::makeCache(QString dir) {
+ cachingDialog->setVisible(true);
+ QCoreApplication::processEvents();
+ stopped = false;
+ QFileInfo dictFileN(_settings->value("path"));
+ QString cachePathN;
+ cachePathN = QDir::homePath() + "/.mdictionary/"
+ + dictFileN.completeBaseName() + ".cache";
+
+ QFile dictionaryFile(dictFileN.filePath());
+
+
+ if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+ return 0;
+ }
+
+ QXmlStreamReader reader(&dictionaryFile);
+
+
+ db.setDatabaseName(cachePathN);
+ if(!db.open()) {
+ qDebug() << "Database error" << 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;");
+
+ QString a;
+ bool match = false;
+ QTime timer;
+ timer.start();
+ countWords();
+
+ int lastProg = -1;
+
+
+ counter=0;
+ while (!reader.atEnd() && !stopped) {
+
+ QCoreApplication::processEvents();
+ // usleep(50);
+ reader.readNext();
+
+ if(reader.tokenType() == QXmlStreamReader::StartElement) {
+ if(reader.name()=="k"){
+ a = 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+=tr("</");
+ if(reader.tokenType()==QXmlStreamReader::StartElement)
+ temp+=tr("<");
+ temp+=reader.name().toString();
+ if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
+ temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
+ temp+=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.addBindValue(temp);
+ cur.exec();
+ counter++;
+ int prog = counter*100/_wordsCount;
+ if(prog % 5 == 0 && lastProg != prog) {
+ Q_EMIT updateCachingProgress(prog,
+ timer.restart());
+ lastProg = prog;
+ }
+ }
+ }
+
+ 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;
+}
+
+