From 1601d76b6c576a16354be70c099fd18bd44efc60 Mon Sep 17 00:00:00 2001 From: Bartosz Szatkowski Date: Fri, 13 Aug 2010 09:32:38 +0200 Subject: [PATCH] Added caching to xdxf plugin --- trunk/src/plugins/xdxf/src/src.pro | 2 +- trunk/src/plugins/xdxf/src/xdxfplugin.cpp | 95 ++++++++++++++++++++++++++++- trunk/src/plugins/xdxf/src/xdxfplugin.h | 7 +++ 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/trunk/src/plugins/xdxf/src/src.pro b/trunk/src/plugins/xdxf/src/src.pro index bcde306..a27b7c4 100644 --- a/trunk/src/plugins/xdxf/src/src.pro +++ b/trunk/src/plugins/xdxf/src/src.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core xml gui +QT += core xml gui sql TARGET = XdxfPlugin diff --git a/trunk/src/plugins/xdxf/src/xdxfplugin.cpp b/trunk/src/plugins/xdxf/src/xdxfplugin.cpp index 1e753d0..2cc11cb 100644 --- a/trunk/src/plugins/xdxf/src/xdxfplugin.cpp +++ b/trunk/src/plugins/xdxf/src/xdxfplugin.cpp @@ -30,6 +30,7 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent), _langFrom(tr("")), _langTo(tr("")),_name(tr("")), _type(tr("xdxf")), _infoNote(tr("")) { + _wordsCount = -1; _settings = new Settings(); _dictDialog = new XdxfDictDialog(this, this); _settings->setValue("type","xdxf"); @@ -38,7 +39,6 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent), else _settings->setValue("cached","false"); - _wordsCount = 0; stopped = false; @@ -196,6 +196,7 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const { QStringList list = settings->keys(); foreach(QString key, list) plugin->settings()->setValue(key, settings->value(key)); + plugin->makeCache(""); } return plugin; } @@ -275,6 +276,8 @@ QIcon* XdxfPlugin::icon() { } int XdxfPlugin::countWords() { + if(_wordsCount > 0) + return _wordsCount; QFile dictionaryFile(path); if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) { @@ -289,7 +292,7 @@ int XdxfPlugin::countWords() { QString line; while(!dictionaryFile.atEnd()) { line = dictionaryFile.readLine(); - if(line.contains("")) { + if(line.contains("")) { wordsCount++; } } @@ -298,4 +301,92 @@ int XdxfPlugin::countWords() { return wordsCount; } + + +bool XdxfPlugin::makeCache(QString dir) { + QFileInfo dictFileN(_settings->value("path")); + QString cachePathN; + cachePathN = dictFileN.dir().absolutePath() + "/" + + dictFileN.completeBaseName() + ".cache"; + + QFile dictionaryFile(dictFileN.filePath()); + + + qDebug() << dictFileN.path(); + if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) { + return 0; + } + qDebug() << "OLE"; + + QXmlStreamReader reader(&dictionaryFile); + + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + + db.setDatabaseName(cachePathN); + if(!db.open()) { + qDebug() << "Database error" << endl; + return false; + } + QSqlQuery cur; + cur.exec("PRAGMA synchronous = 0"); + cur.exec("drop table dict"); + cur.exec("create table dict(word text ,transl text)"); + int counter = 0; + cur.exec("BEGIN;"); + + QString a; + bool match = false; + QTime timer; + timer.start(); + countWords(); + + + counter=0; + while (!reader.atEnd()) { + + reader.readNext(); + + if(reader.tokenType() == QXmlStreamReader::StartElement) { + if(reader.name()=="k"){ + a = reader.readElementText(); + match = true; + } + } + 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(); + } + } + 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) + Q_EMIT update(prog); + } + + } + } + + qDebug()< #include #include +#include +#include #include "XdxfDictDialog.h" class TranslationXdxf; @@ -99,6 +101,10 @@ public Q_SLOTS: //! stop current operation void stop(); +Q_SIGNALS: + //! emited with percent count of caching progress + void update(float); + protected: QString removeAccents(QString); @@ -115,6 +121,7 @@ private: QList searchWordListCache(QString word, int limit=0); QList searchWordListFile(QString word, int limit=0); int countWords(); + bool makeCache(QString dir); //! language from which we translate QString _langFrom; -- 1.7.9.5