code clean
[mdictionary] / src / plugins / google / GooglePlugin.cpp
index 95ae0e4..3c3ff11 100644 (file)
     Copyright 2010 Comarch S.A.
 
 *******************************************************************************/
+/*!
+    \file GooglePlugin.cpp
+    \brief Implementation of google plugin's main class.
 
-/*! \file GooglePlugin.cpp
     \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
 */
 
@@ -28,6 +30,8 @@
 #include "GoogleDictDialog.h"
 
 QMap<QString, QString> GooglePlugin::languages;
+bool GooglePlugin::noNetworkErrorShowed = false;
+
 
 GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent),
                     _name(""),_infoNote("") {
@@ -37,20 +41,22 @@ GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent),
     _settings->setValue("type","google");
     _settings->setValue("connection_accepted","true");
     _dictDialog = new GoogleDictDialog(this,this);
-    _icon = QIcon("/usr/share/mdictionary/google.png");
+    _iconPath = "/usr/share/mdictionary/google.png";
+    _icon = QIcon(_iconPath);
 
     stopped = false;
     initLanguages();
 
-    http = new QHttp(this);
+    http = new QHttp();
     connect(http, SIGNAL(done(bool)), this, SLOT(done()));
+    threa=new QThread();
+    http->moveToThread(threa);
+    threa->start();
 }
 
 void GooglePlugin::retranslate() {
     QString locale = QLocale::system().name();
-
     QTranslator *translator = new QTranslator(this);
-
     if(!translator->load(":/google/translations/" + locale)) {
         translator->load(":/google/translations/en_US");
     }
@@ -145,6 +151,10 @@ QIcon* GooglePlugin::icon() {
     return &_icon;
 }
 
+QString GooglePlugin::iconPath(){
+    return _iconPath;
+}
+
 
 CommonDictInterface* GooglePlugin::getNew(const Settings* settings) const {
     GooglePlugin *plugin = new GooglePlugin();
@@ -155,7 +165,7 @@ CommonDictInterface* GooglePlugin::getNew(const Settings* settings) const {
 
 
 QString GooglePlugin::search(QString) {
-    qDebug() << "function is not used in this plugin";
+    qDebug() << "function is not used in google plugin";
     return QString("");
 }
 
@@ -169,8 +179,10 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int ) {
     QList<Translation*> translations;
     if(isAvailable()) {
         QString error("");
-        word.replace("*","");   /*remove wildcard*/
-        word.replace("?","");
+        char wrongChar[]={"*?&<>"};
+        for(unsigned int a=0;a<sizeof(wrongChar);a++)    /*remove wrong char*/
+            word.replace(wrongChar[a],"");
+
         QString url=QString("/translate_a/t?client=t&sl=%1&tl=%2").arg(
                                               _settings->value("lang_from"),
                                               _settings->value("lang_to"));
@@ -187,14 +199,22 @@ QList<Translation*> GooglePlugin::searchWordList(QString word, int ) {
 
         wait=true;      /* bool - change in slot done (initiate by http) */
         stopped=false;  /* bool - change in slot stop (initiate in gui)  */
+        int lastState=0;
         http->request(head, data);
-        while(wait && (error=="" || error=="Unknown error") && !stopped)
+        while(wait && (error=="" || error=="Unknown error") && !stopped){
             error=http->errorString();
-
+            if(lastState!=http->state()) {
+                lastState=http->state();
+                if(lastState==0)
+                    done();
+            }
+        }
         if(error!="" && error!="Unknown error") {
-            qDebug()<<error;
-            Q_EMIT notify(Notify::Warning,
-                    tr("GooglePlugin: %1").arg(error));
+            if(!noNetworkErrorShowed) {
+                noNetworkErrorShowed = true;
+                Q_EMIT notify(Notify::Warning,
+                        tr("GooglePlugin: %1").arg(error));
+            }
         }
 
         QString text = QString::fromUtf8(http->readAll());
@@ -272,9 +292,8 @@ QString GooglePlugin::jsonParse(QString result) {
     }
     returnLang=list1.at(index*2+1);
     if(partOfSpeach.size()==0){
-        if(translation.toLower()==original.toLower()){
+        if(translation.toLower()==original.toLower())
             return QString(""); // word doesn't exist";
-        }
         else
             return "\""+translation+"\"";
     }