fix bug xdxf wrong format
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Thu, 2 Sep 2010 13:35:38 +0000 (15:35 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Thu, 2 Sep 2010 13:35:38 +0000 (15:35 +0200)
trunk/src/plugins/google/src/TranslationGoogle.cpp
trunk/src/plugins/google/src/TranslationGoogle.h
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.h

index 4425691..fddaa2c 100644 (file)
 
 #include "TranslationGoogle.h"
 
-TranslationGoogle::TranslationGoogle():_key(""),_trans(""),_dictionaryInfo("") {
+TranslationGoogle::TranslationGoogle():_key(QString("")),
+                    _dictionaryInfo(QString("")), _trans(QString("")) {
     googlePlugin=0;
 }
 
 TranslationGoogle::TranslationGoogle(QString _key,QString _trans,
                     QString _dictionaryInfo,GooglePlugin *googlePlugin):
-                    _key(_key),_trans(_trans),_dictionaryInfo(_dictionaryInfo) {
+                     _key(_key),_dictionaryInfo(_dictionaryInfo), _trans(_trans) {
     this->googlePlugin=googlePlugin;
     if(googlePlugin)
         _dictHash = googlePlugin->hash();
index a5b097e..8e5fb23 100644 (file)
@@ -35,7 +35,8 @@ class TranslationGoogle : public Translation
 {
 public:
     TranslationGoogle();
-    TranslationGoogle(QString _key,QString _trans,QString _dictionaryInfo, GooglePlugin *googlePlugin);
+    TranslationGoogle(QString _key,QString _trans,QString _dictionaryInfo,
+                      GooglePlugin *googlePlugin);
     ~TranslationGoogle();
 
     //! \return word to be translated
index aa34ded..93ef568 100644 (file)
@@ -310,9 +310,12 @@ DictDialog* XdxfPlugin::dictDialog() {
 
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
-    if(settings)
-        plugin->setSettings(settings);
-    return  plugin;
+    if(settings && plugin->setSettings(settings))
+        return plugin;
+    else {
+        delete plugin;
+        return new XdxfPlugin();
+    }
 }
 
 
@@ -350,7 +353,7 @@ 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");
@@ -365,6 +368,13 @@ void XdxfPlugin::setSettings(const Settings *settings) {
                _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";
+            return false;
+        }
+
         if(isPathChange) {
             _wordsCount=0;
             if(oldPath!="")
@@ -387,30 +397,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")
@@ -425,6 +438,9 @@ void XdxfPlugin::getDictionaryInfo() {
     _infoNote="path=\""+initialPath+"\"> \n" + _name + " [" + _langFrom + "-"
                 + _langTo + "] ( xdxf )";
     dictionaryFile.close();
+    if(okFormat)
+        return true;
+    return false;
 }
 
 
index 593565c..07c4aa1 100644 (file)
@@ -96,7 +96,7 @@ public:
     long wordsCount();
 
     //! Sets new settings
-    void setSettings(const Settings*);
+    bool setSettings(const Settings*);
 
     //! returns plugin icon
     QIcon* icon();
@@ -148,7 +148,7 @@ private:
     QString searchCache(QString key);
 
     //! scan dictionary file to get information about it
-    void getDictionaryInfo();
+    bool getDictionaryInfo();
 
     int countWords();