Caching dialog in xdxf is set to modal
[mdictionary] / src / plugins / xdxf / xdxfplugin.cpp
index ba143b9..ad0a0bb 100644 (file)
 
 #include "xdxfplugin.h"
 #include <QDebug>
-#include "../../common/Notify.h"
+#include "../../include/Notify.h"
 
 XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _langFrom(""), _langTo(""),_name(""), _infoNote("") {
     _settings = new Settings();
-    _dictDialog = new XdxfDictDialog(this);
+    _dictDialog = new XdxfDictDialog(this, this);
+
+    connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
+            this, SIGNAL(notify(Notify::NotifyType,QString)));
+
     cachingDialog = new XdxfCachingDialog(this);
 
+
     _settings->setValue("type","xdxf");
-    _icon = QIcon(":/icons/xdxf.png");
+    _icon = QIcon("/usr/share/mdictionary/xdxf.png");
     _wordsCount = -1;
     stopped = false;
 
@@ -45,10 +50,22 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
     initAccents();
 }
 
+void XdxfPlugin::retranslate() {
+    QString locale = QLocale::system().name();
+
+    QTranslator *translator = new QTranslator(this);
+
+    if(!translator->load(":/xdxf/translations/" + locale)) {
+        translator->load(":/xdxf/translations/en_US");
+    }
+    QCoreApplication::installTranslator(translator);
+}
+
 
 XdxfPlugin::~XdxfPlugin() {
     delete _settings;
     delete cachingDialog;
+    delete _dictDialog;
 }
 
 
@@ -73,7 +90,7 @@ QString XdxfPlugin::type() const {
 
 
 QString XdxfPlugin::infoNote() const {
-    return  _infoNote;
+    return _infoNote;
 }
 
 
@@ -93,7 +110,6 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
     QSet<Translation*> translations;
     QString cacheFilePath = _settings->value("cache_path");
 
-//  QSqlDatabase::removeDatabase(cacheFilePath);
     db.setDatabaseName(cacheFilePath);
     if(!QFile::exists(cacheFilePath) || !db.open()) {
         qDebug() << "Database error" << db.lastError().text() << endl;
@@ -133,7 +149,7 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         if(ok) {  /*add key word to list*/
             translations.insert(new TranslationXdxf(
                     cur.value(0).toString().toLower(),
-                    _infoNote, this));
+                    _dictionaryInfo, this));
             i++;
         }
     }
@@ -184,7 +200,7 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                 }
                 if(ok) {  /*add key word to list*/
                     translations<<(new TranslationXdxf(readKey.toLower(),
-                                    _infoNote,this));
+                                    _dictionaryInfo,this));
                     i++;
                 }
                 if(i>=limit && limit!=0)
@@ -310,10 +326,14 @@ DictDialog* XdxfPlugin::dictDialog() {
 
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
-    if(settings){
-        plugin->setSettings(settings);
+
+    if(settings && plugin->setSettings(settings)) {
+        return plugin;
+    }
+    else {
+        delete plugin;
+        return 0;
     }
-    return  plugin;
 }
 
 
@@ -322,16 +342,6 @@ bool XdxfPlugin::isAvailable() const {
 }
 
 
-void XdxfPlugin::setHash(uint _hash) {
-    this->_hash=_hash;
-}
-
-
-uint XdxfPlugin::hash() const {
-   return _hash;
-}
-
-
 Settings* XdxfPlugin::settings() {
     return _settings;
 }
@@ -344,30 +354,45 @@ bool XdxfPlugin::isCached() {
 }
 
 
-void XdxfPlugin::setSettings(const Settings *settings) {
+bool XdxfPlugin::setSettings(const Settings *settings) {
     if(settings) {
-
         bool isPathChange=false;
         QString oldPath = _settings->value("path");
+        Settings *oldSettings =  new Settings ;
+
         if(oldPath != settings->value("path")) {
             if(oldPath!="" && _settings->value("cache_path")!="")
                 clean();
             isPathChange=true;
         }
 
+        foreach(QString key, _settings->keys())
+            oldSettings->setValue(key, _settings->value(key));
+
         foreach(QString key, settings->keys()) {
            if(key != "generateCache")
                _settings->setValue(key, settings->value(key));
         }
 
+        if(!getDictionaryInfo()) {
+            Q_EMIT notify(Notify::Warning,
+                QString(tr("XDXF file is in wrong format")));
+            qDebug()<<"Error: xdxf file is in wrong format";
+            delete _settings;
+            _settings=oldSettings;
+            return false;
+        }
+
         if(isPathChange) {
             _wordsCount=0;
-            if(oldPath!="") {
+            if(oldPath!="")
                 _settings->setValue("cached","false");
-                QSqlDatabase::removeDatabase(db_name);
+            if(_settings->value("cached")=="true"
+                    && _settings->value("cache_path")!="") {
+                db_name = _settings->value("type")
+                        + _settings->value("cache_path");
+                db = QSqlDatabase::addDatabase("QSQLITE",db_name);
             }
-            db_name = _settings->value("type") + _settings->value("path");
-            db = QSqlDatabase::addDatabase("QSQLITE",db_name);
         }
 
         if((_settings->value("cached") == "false" ||
@@ -380,30 +405,33 @@ void XdxfPlugin::setSettings(const Settings *settings) {
         else if (settings->value("generateCache") == "false") {
             _settings->setValue("cached", "false");
         }
-
-        getDictionaryInfo();
     }
+    else
+        return false;
     Q_EMIT settingsChanged();
+    return true;
 }
 
 
-void XdxfPlugin::getDictionaryInfo() {
+bool XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(_settings->value("path"));
     if(!QFile::exists(_settings->value("path"))
                 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
        Q_EMIT notify(Notify::Warning,
                QString(tr("XDXF dictionary cannot be read from file")));
         qDebug()<<"Error: could not open file";
-        return;
+        return false;
     }
 
+    bool okFormat=false;
     QXmlStreamReader reader(&dictionaryFile);
     reader.readNextStartElement();
     if(reader.name()=="xdxf") {
-      if(reader.attributes().hasAttribute("lang_from"))
-        _langFrom = reader.attributes().value("lang_from").toString();
-      if(reader.attributes().hasAttribute("lang_to"))
-        _langTo = reader.attributes().value("lang_to").toString();
+        okFormat=true;
+        if(reader.attributes().hasAttribute("lang_from"))
+            _langFrom = reader.attributes().value("lang_from").toString();
+        if(reader.attributes().hasAttribute("lang_to"))
+            _langTo = reader.attributes().value("lang_to").toString();
     }
     reader.readNextStartElement();
     if(reader.name()=="full_name")
@@ -412,12 +440,13 @@ void XdxfPlugin::getDictionaryInfo() {
     if(reader.name()=="description")
         _infoNote=reader.readElementText();
 
-    QString format = "png";
-    QString initialPath = QDir::currentPath() + "/xdxf." + format;
+    _dictionaryInfo= _name + " [" + _langFrom + "-"
+                + _langTo + "]";
 
-    _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-"
-                + _langTo + "] ( xdxf )";
     dictionaryFile.close();
+    if(okFormat)
+        return true;
+    return false;
 }
 
 
@@ -457,7 +486,7 @@ int XdxfPlugin::countWords() {
 
 
 bool XdxfPlugin::makeCache(QString) {
-    cachingDialog->setVisible(true);
+    cachingDialog->show();
     QCoreApplication::processEvents();
     QFileInfo dictFileN(_settings->value("path"));
     QString cachePathN;
@@ -472,6 +501,9 @@ bool XdxfPlugin::makeCache(QString) {
         i++;
     } while(QFile::exists(cachePathN));
 
+    db_name = _settings->value("type") + cachePathN;
+    db = QSqlDatabase::addDatabase("QSQLITE",db_name);
+
     /*checke errors (File open and db open)*/
     QFile dictionaryFile(dictFileN.filePath());
     if (!QFile::exists(_settings->value("path"))
@@ -509,7 +541,7 @@ bool XdxfPlugin::makeCache(QString) {
     timer.start();
     countWords();
     int lastProg = -1;
-    settings()->setValue("strip_accents", "true");
+    _settings->setValue("strip_accents", "true");
     counter=0;
 
     /*add all words to db*/
@@ -562,14 +594,14 @@ bool XdxfPlugin::makeCache(QString) {
     }
     cur.exec("END;");
     cur.exec("select count(*) from dict");
-    cachingDialog->setVisible(false);
+    //cachingDialog->hide();
 
     /*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 againg.")));
+                QString(tr("Database caching error, please try again.")));
         db.close();
         return false;
     }
@@ -583,8 +615,10 @@ bool XdxfPlugin::makeCache(QString) {
 
 
 void XdxfPlugin::clean() {
-    if(QFile::exists(_settings->value("cache_path")))
+    if(QFile::exists(_settings->value("cache_path"))) {
         QFile(_settings->value("cache_path")).remove();
+        QSqlDatabase::removeDatabase(db_name);
+    }
 }