From 1d5b17022e65d3636f65820fa1807eeed8a2b760 Mon Sep 17 00:00:00 2001 From: Jakub Jaszczynski Date: Wed, 1 Sep 2010 10:39:17 +0200 Subject: [PATCH] fix bug - delete cache file, - cache name is same as dict file name - manual remov cache file --- trunk/src/includes/CommonDictInterface.h | 13 +-- trunk/src/plugins/google/src/GooglePlugin.cpp | 1 + trunk/src/plugins/xdxf/src/xdxfplugin.cpp | 130 ++++++++++++------------- trunk/src/plugins/xdxf/src/xdxfplugin.h | 22 +++-- 4 files changed, 80 insertions(+), 86 deletions(-) diff --git a/trunk/src/includes/CommonDictInterface.h b/trunk/src/includes/CommonDictInterface.h index c5dcdd9..24aec79 100644 --- a/trunk/src/includes/CommonDictInterface.h +++ b/trunk/src/includes/CommonDictInterface.h @@ -67,7 +67,6 @@ class CommonDictInterface : public QObject, public AccentsNormalizer { for adding new dictionary and changing plugin settings*/ virtual DictDialog* dictDialog() = 0; - //! returns new, clean copy of plugin with setting set as in Settings* virtual CommonDictInterface* getNew(const Settings*) const = 0; @@ -97,6 +96,7 @@ class CommonDictInterface : public QObject, public AccentsNormalizer { */ virtual void clean() {} + public Q_SLOTS: /*! performs search in dictionary \param word word to search in dictionary @@ -111,11 +111,8 @@ class CommonDictInterface : public QObject, public AccentsNormalizer { //! stops current operation virtual void stop() = 0; - Q_SIGNALS: - - //! emited after dictionary is ready to use afer being loaded - void loaded(CommonDictInterface*); +Q_SIGNALS: //! emited after change dictionary settings void settingsChanged(); @@ -126,17 +123,13 @@ class CommonDictInterface : public QObject, public AccentsNormalizer { */ void notify(Notify::NotifyType, QString); + protected: QString removeAccents(QString string) { if(settings()->value("strip_accents") == "true") return AccentsNormalizer::removeAccents(string); return string; } - - //void initAccents() { AccentsNormalizer::initAccents(); } - - - }; Q_DECLARE_INTERFACE(CommonDictInterface, "CommonDictInterface/0.1"); diff --git a/trunk/src/plugins/google/src/GooglePlugin.cpp b/trunk/src/plugins/google/src/GooglePlugin.cpp index f354b95..3fe8907 100644 --- a/trunk/src/plugins/google/src/GooglePlugin.cpp +++ b/trunk/src/plugins/google/src/GooglePlugin.cpp @@ -136,6 +136,7 @@ void GooglePlugin::setSettings(const Settings* settings) { foreach(QString key, list) _settings->setValue(key, settings->value(key)); getDictionaryInfo(); + Q_EMIT settingsChanged(); } } diff --git a/trunk/src/plugins/xdxf/src/xdxfplugin.cpp b/trunk/src/plugins/xdxf/src/xdxfplugin.cpp index ae1ba35..bc35951 100644 --- a/trunk/src/plugins/xdxf/src/xdxfplugin.cpp +++ b/trunk/src/plugins/xdxf/src/xdxfplugin.cpp @@ -28,25 +28,20 @@ #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))); } @@ -55,29 +50,35 @@ XdxfPlugin::~XdxfPlugin() { 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 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()) @@ -85,20 +86,22 @@ QList XdxfPlugin::searchWordList(QString word, int limit) { return searchWordListFile(word, limit); } + QList XdxfPlugin::searchWordListCache(QString word, int limit) { int i=0; QSet 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("*", "%"); @@ -114,6 +117,7 @@ QList XdxfPlugin::searchWordListCache(QString word, int limit) { if(limit !=0) cur.addBindValue(limit); cur.exec(); + bool in = false; while(cur.next() && (i XdxfPlugin::searchWordListCache(QString word, int limit) { } QList XdxfPlugin::searchWordListFile(QString word, int limit) { - QTime time; QSet 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()))); @@ -188,24 +192,26 @@ QList XdxfPlugin::searchWordListFile(QString word, int limit) { 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); } @@ -225,9 +231,10 @@ QString XdxfPlugin::searchCache(QString 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"; @@ -280,44 +287,26 @@ QString XdxfPlugin::searchFile(QString key) { 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; } @@ -345,15 +334,14 @@ bool XdxfPlugin::isCached() { } -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; } @@ -363,8 +351,13 @@ void XdxfPlugin::setSettings(Settings *settings) { } 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" || @@ -372,19 +365,19 @@ void XdxfPlugin::setSettings(Settings *settings) { 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"))); @@ -410,7 +403,8 @@ void XdxfPlugin::getDictionaryInfo() { 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(); } @@ -420,10 +414,11 @@ QIcon* XdxfPlugin::icon() { 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") @@ -448,17 +443,21 @@ int XdxfPlugin::countWords() { 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, @@ -552,9 +551,7 @@ bool XdxfPlugin::makeCache(QString) { 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."))); @@ -570,10 +567,9 @@ bool XdxfPlugin::makeCache(QString) { } - 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) diff --git a/trunk/src/plugins/xdxf/src/xdxfplugin.h b/trunk/src/plugins/xdxf/src/xdxfplugin.h index 1e9d0f5..593565c 100644 --- a/trunk/src/plugins/xdxf/src/xdxfplugin.h +++ b/trunk/src/plugins/xdxf/src/xdxfplugin.h @@ -51,6 +51,7 @@ class XdxfPlugin : public CommonDictInterface Q_INTERFACES(CommonDictInterface) public: XdxfPlugin(QObject *parent=0); + ~XdxfPlugin(); //! returns source language code iso 639-2 @@ -95,7 +96,7 @@ public: long wordsCount(); //! Sets new settings - void setSettings(Settings*); + void setSettings(const Settings*); //! returns plugin icon QIcon* icon(); @@ -105,6 +106,8 @@ public: */ void clean(); + + public Q_SLOTS: /*! performs search in dictionary \param word word to search in dictionary @@ -118,6 +121,8 @@ public Q_SLOTS: //! stop current operation void stop(); + + Q_SIGNALS: //! emitted with percent count of caching progress, and time elapsed from //! last signal emit @@ -132,16 +137,21 @@ private: bool isCached(); //! sets the path to dictionary file and adds it to settings - void setPath(QString); + // void setPath(QString); QList searchWordListCache(QString word, int limit=0); + QList searchWordListFile(QString word, int limit=0); + QString searchFile(QString key); + QString searchCache(QString key); + //! scan dictionary file to get information about it void getDictionaryInfo(); int countWords(); + bool makeCache(QString dir); //! language from which we translate @@ -150,26 +160,20 @@ private: QString _langTo; //! name of a dictionary QString _name; - //! type of a dictionary - QString _type; //! information about dictionary QString _infoNote; //! path to dictionary file - QString path; + // QString path; uint _hash; QIcon _icon; QSqlDatabase db; QString db_name; - //! number of words in dictionary long _wordsCount; - volatile bool stopped; Settings *_settings; XdxfDictDialog* _dictDialog; XdxfCachingDialog* cachingDialog; -// extern void initAccents(); - // extern QString removeAccents(QString string); }; -- 1.7.9.5