Merge branch 'cache'
[mdictionary] / trunk / src / plugins / xdxf / src / xdxfplugin.cpp
index ae3549e..e99f6ba 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "xdxfplugin.h"
 #include <QDebug>
+#include "../../../includes/Notify.h"
 
 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _langFrom(tr("")), _langTo(tr("")),_name(tr("")),
@@ -74,7 +75,6 @@ QString XdxfPlugin::infoNote() const {
 }
 
 QList<Translation*> XdxfPlugin::searchWordList(QString word, int limit) {
-    //if(_settings->value("cached") == "true")
     if(word.indexOf("*")==-1 && word.indexOf("?")==-1 && word.indexOf("_")==-1
        && word.indexOf("%")==-1)
         word+="*";
@@ -90,6 +90,9 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         db.setDatabaseName(cacheFilePath);
         if(!db.open()) {
             qDebug() << "Database error" << db.lastError().text() << endl;
+            Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
+                    "opened for %1 dictionary. Searching in xdxf file. "
+                    "You may want to recache.").arg(name()));
             return searchWordListFile(word, limit);
         }
 
@@ -98,7 +101,6 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         word = word.replace("*", "%");
         word = word.replace("?", "_");
         word = removeAccents(word);
-        //qDebug() << word;
 
         QSqlQuery cur(db);
         if(limit !=0)
@@ -138,6 +140,8 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     regWord.setPatternSyntax(QRegExp::Wildcard);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
         qDebug()<<"Error: could not open file";
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1").arg(name()));
         return translations.toList();
     }
 
@@ -189,6 +193,9 @@ QString XdxfPlugin::searchCache(QString key) {
 
     if(!db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
+        Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
+                "opened for %1 dictionary. Searching in xdxf file. "
+                "You may want to recache.").arg(name()));
         return searchFile(key);
     }
 
@@ -210,7 +217,9 @@ QString XdxfPlugin::searchFile(QString key) {
     QFile dictionaryFile(path);
     QString resultString("");
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
-        qDebug()<<"Error: could not open file when search";
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1").arg(name()));
+        qDebug()<<"Error: could not open file";
         return "";
     }
     QXmlStreamReader reader(&dictionaryFile);
@@ -340,6 +349,8 @@ void XdxfPlugin::setSettings(Settings *settings) {
 void XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+       Q_EMIT notify(Notify::Warning,
+               QString("Xdxf file cannot be read dictionary"));
         qDebug()<<"Error: could not open file";
         return;
     }
@@ -361,10 +372,6 @@ void XdxfPlugin::getDictionaryInfo() {
 
     QString format = "png";
     QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
-//  qDebug()<<initialPath;
-//  QPixmap test(":/icons/xdxf.png");
-//  qDebug()<<QPixmap(test).save(initialPath,format.toAscii());
-//  qDebug()<<QPixmap("/home/jakub/star.jpg").save(initialPath,format.toAscii());
 
     _infoNote="path=\""+initialPath+"\">"+"\n" + _name + " [" + _langFrom + "-" + _langTo + "] "+ "(" + _type + ")";
 
@@ -399,6 +406,9 @@ int XdxfPlugin::countWords() {
 
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1 dictionary")
+                .arg(name()));
         qDebug()<<"Error: could not open file";
         return -1;
     }
@@ -432,6 +442,10 @@ bool XdxfPlugin::makeCache(QString dir) {
 
 
     if (!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
+        Q_EMIT updateCachingProgress(100, 0);
+        Q_EMIT notify(Notify::Warning,
+                QString("Xdxf file cannot be read for %1 dictionary")
+                .arg(name()));
         return 0;
     }
 
@@ -440,6 +454,10 @@ bool XdxfPlugin::makeCache(QString dir) {
     db.setDatabaseName(cachePathN);
     if(!db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
+        Q_EMIT updateCachingProgress(100, 0);
+        Q_EMIT notify(Notify::Warning, QString("Cache database cannot be "
+                "opened for %1 dictionary. Searching in xdxf file. "
+                "You may want to recache.").arg(name()));
         return false;
     }
     QCoreApplication::processEvents();
@@ -514,6 +532,9 @@ bool XdxfPlugin::makeCache(QString dir) {
 
     if(!cur.next() || countWords() != cur.value(0).toInt())
     {
+        Q_EMIT updateCachingProgress(100, timer.restart());
+        Q_EMIT notify(Notify::Warning,
+                QString("Database caching error, please try againg."));
         db.close();
         return false;
     }