Added error handling for invalid JSON string
authorlampehe-local <henri.lampela@ixonos.com>
Mon, 14 Jun 2010 09:02:04 +0000 (12:02 +0300)
committerlampehe-local <henri.lampela@ixonos.com>
Mon, 14 Jun 2010 09:02:04 +0000 (12:02 +0300)
Reviewed by: Kaj Wallin

res/languages/situare_fi.qm
res/languages/situare_fi.ts
src/engine/engine.cpp
src/facebookservice/facebookauthentication.cpp
src/situareservice/situareservice.cpp
src/situareservice/situareservice.h

index 0daffc4..7161474 100644 (file)
Binary files a/res/languages/situare_fi.qm and b/res/languages/situare_fi.qm differ
index 64430bc..9cefcee 100644 (file)
         <translation>Situare</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="404"/>
-        <location filename="../../src/ui/mainwindow.cpp" line="663"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="406"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="665"/>
         <source>Login</source>
         <translation>Kirjaudu</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="409"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="411"/>
         <source>Settings</source>
         <translation>Asetukset</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="414"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="416"/>
         <source>GPS</source>
         <translation>GPS</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="421"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="423"/>
         <source>Auto centering</source>
         <translation>Automaattinen keskitys</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="427"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="429"/>
         <source>Main</source>
         <translation>Päävalikko</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="432"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="434"/>
         <source>Menu</source>
         <translation>Valikko</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="535"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="537"/>
         <source>GPS timeout</source>
         <translation>GPS pyyntö aikakatkaistiin</translation>
     </message>
     <message>
-        <location filename="../../src/ui/mainwindow.cpp" line="657"/>
+        <location filename="../../src/ui/mainwindow.cpp" line="659"/>
         <source>Logout</source>
         <translation>Kirjaudu ulos</translation>
     </message>
         <translation>Osoitteen haku epäonnistui</translation>
     </message>
     <message>
+        <location filename="../../src/engine/engine.cpp" line="266"/>
+        <source>Malformatted reply from server</source>
+        <translation>Viallisesti muotoiltu serverin vastaus</translation>
+    </message>
+    <message>
         <source>Address retrieval failed, please try again</source>
         <translation type="obsolete">Osoitteen haku epäonnistui. Yritä uudelleen</translation>
     </message>
         <translation>Tuntematon vastaus palvelimelta</translation>
     </message>
     <message>
-        <location filename="../../src/engine/engine.cpp" line="266"/>
         <source>JSON parsing failed, invalid JSON string</source>
-        <translation>Virheellinen JSON, parsiminen epäonnistui</translation>
+        <translation type="obsolete">Virheellinen JSON, parsiminen epäonnistui</translation>
     </message>
     <message>
-        <location filename="../../src/engine/engine.cpp" line="299"/>
-        <location filename="../../src/engine/engine.cpp" line="307"/>
+        <location filename="../../src/engine/engine.cpp" line="302"/>
+        <location filename="../../src/engine/engine.cpp" line="310"/>
         <source>GPS enabled</source>
         <translation>GPS päällä</translation>
     </message>
     <message>
-        <location filename="../../src/engine/engine.cpp" line="300"/>
-        <location filename="../../src/engine/engine.cpp" line="310"/>
+        <location filename="../../src/engine/engine.cpp" line="303"/>
+        <location filename="../../src/engine/engine.cpp" line="313"/>
         <source>Auto centering enabled</source>
         <translation>Automaattinen keskitys päällä</translation>
     </message>
index 52dd36e..2e7cde6 100644 (file)
@@ -263,7 +263,10 @@ void SituareEngine::error(const int error)
         m_ui->buildInformationBox(tr("Unknown server response"), true);
         break;
     case SituareError::INVALID_JSON:
-        m_ui->buildInformationBox(tr("JSON parsing failed, invalid JSON string"), true);
+        m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
+        m_loggedIn = false;
+        m_ui->loggedIn(false);
+        m_facebookAuthenticator->clearAccountInformation(false); // clean all
         break;
     default:
         qCritical() << "QNetworkReply::NetworkError :" << error;
index 02d83ec..9537793 100644 (file)
@@ -129,42 +129,43 @@ bool FacebookAuthentication::updateCredentials(const QUrl &url)
                     if(list.at(i).startsWith("{")) {
                         QByteArray jsonString = list.at(i).toAscii();
                         QVariantMap result = parser.parse (jsonString, &ok).toMap();
-                        if (!ok) {
 
-                            qFatal("An error occurred during parsing");
+                        if (!ok) {
                             emit error(SituareError::INVALID_JSON);
                             found = false;
-                        }
-                        qDebug() << "Session Key" << result[SESSION_KEY].toString();
-                        m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
+                        } else {
+                            qDebug() << "Session Key" << result[SESSION_KEY].toString();
+                            m_loginCredentials.setSessionKey(result[SESSION_KEY].toString());
 
-//                        // commeted out until qjson parser can handle 64-bit integers
-//                        qDebug() << "userID" << result[USER_ID].toString();
-//                        m_loginCredentials.setUserID(result[USER_ID].toString().toAscii());
+    //                        // commeted out until qjson parser can handle 64-bit integers
+    //                        qDebug() << "userID" << result[USER_ID].toString();
+    //                        m_loginCredentials.setUserID(result[USER_ID].toString().toAscii());
 
-                        // dirty fix, get user id from session_key
-                        QStringList list = result[SESSION_KEY].toString().split("-");
-                        m_loginCredentials.setUserID(list.at(1));
-                        qDebug() << m_loginCredentials.userID();
+                            // dirty fix, get user id from session_key
+                            QStringList list = result[SESSION_KEY].toString().split("-");
+                            m_loginCredentials.setUserID(list.at(1));
+                            qDebug() << m_loginCredentials.userID();
 
-                        qDebug() << "Expires" << result[EXPIRES].toString();
-                        m_loginCredentials.setExpires(result[EXPIRES].toString());
+                            qDebug() << "Expires" << result[EXPIRES].toString();
+                            m_loginCredentials.setExpires(result[EXPIRES].toString());
 
-                        qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
-                        m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
+                            qDebug() << "Session Secret" << result[SESSION_SECRET].toString();
+                            m_loginCredentials.setSessionSecret(result[SESSION_SECRET].toString());
 
-                        qDebug() << "Signature" << result[SIGNATURE].toString();
-                        m_loginCredentials.setSig(result[SIGNATURE].toString());
+                            qDebug() << "Signature" << result[SIGNATURE].toString();
+                            m_loginCredentials.setSig(result[SIGNATURE].toString());
+
+                            found = true;
+                            m_freshLogin = false;
+                            emit saveCookiesRequest();
+                            emit credentialsReady(m_loginCredentials);
+                        }
                     }
-                }
-                found = true;
-                m_freshLogin = false;
-                emit saveCookiesRequest();
-            }
-            emit credentialsReady(m_loginCredentials);
+                }       
+            }   
         }
         else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
-            qWarning() << "login failure" << endl;
+            qDebug() << "login failure";
             qDebug() << callbackUrl;
             clearAccountInformation(true);
             if(m_freshLogin) {
@@ -189,7 +190,6 @@ bool FacebookAuthentication::updateCredentials(const QUrl &url)
         qDebug() << " Loading of page failed invalid URL" << endl;
         // we should not get a wrong page at this point
         emit loginFailure();
-        return false;
     }
     return found;
 }
index af6fd3c..2360ee1 100644 (file)
@@ -298,7 +298,7 @@ void SituareService::credentialsReady(const FacebookCredentials &credentials)
     m_credentials = credentials;    
 }
 
-void SituareService::parseUserData(const QByteArray &jsonReply)
+void SituareService::parseUserData(/*const*/ QByteArray &jsonReply)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -318,53 +318,52 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
 
     QVariantMap result = parser.parse (jsonReply, &ok).toMap();
     if (!ok) {
-
-        qFatal("An error occurred during parsing");
         emit error(SituareError::INVALID_JSON);
         return;
-    }
+    } else {
 
-    QVariant userVariant = result.value("user");
-    QMap<QString, QVariant> userMap = userVariant.toMap();
+        QVariant userVariant = result.value("user");
+        QMap<QString, QVariant> userMap = userVariant.toMap();
 
-    QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
+        QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
 
-    QUrl imageUrl = userMap[NORMAL_SIZE_PROFILE_IMAGE].toUrl();
+        QUrl imageUrl = userMap[NORMAL_SIZE_PROFILE_IMAGE].toUrl();
 
-    if(imageUrl.isEmpty()) {
-        // user doesn't have profile image, so we need to get him a silhouette image
-        m_defaultImage = true;
-    }
+        if(imageUrl.isEmpty()) {
+            // user doesn't have profile image, so we need to get him a silhouette image
+            m_defaultImage = true;
+        }
 
-    m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
-                  userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
-                  true, userMap["uid"].toString());
+        m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
+                      userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
+                      true, userMap["uid"].toString());
 
-    foreach (QVariant friendsVariant, result["friends"].toList()) {
-      QMap<QString, QVariant> friendMap = friendsVariant.toMap();
-      QVariant distance = friendMap["distance"];
-      QMap<QString, QVariant> distanceMap = distance.toMap();
+        foreach (QVariant friendsVariant, result["friends"].toList()) {
+          QMap<QString, QVariant> friendMap = friendsVariant.toMap();
+          QVariant distance = friendMap["distance"];
+          QMap<QString, QVariant> distanceMap = distance.toMap();
 
-      QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
+          QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
 
-      QUrl imageUrl = friendMap["profile_pic"].toUrl();
+          QUrl imageUrl = friendMap["profile_pic"].toUrl();
 
-      if(imageUrl.isEmpty()) {
-          // friend doesn't have profile image, so we need to get him a silhouette image
-          m_defaultImage = true;
-      }
+          if(imageUrl.isEmpty()) {
+              // friend doesn't have profile image, so we need to get him a silhouette image
+              m_defaultImage = true;
+          }
 
-      User *user = new User(friendMap["address"].toString(), coordinates,
-                            friendMap["name"].toString(),
-                            friendMap["note"].toString(), imageUrl,
-                            friendMap["timestamp"].toString(),
-                            false, friendMap["uid"].toString(),
-                            distanceMap["units"].toString(),
-                            distanceMap["value"].toDouble());
+          User *user = new User(friendMap["address"].toString(), coordinates,
+                                friendMap["name"].toString(),
+                                friendMap["note"].toString(), imageUrl,
+                                friendMap["timestamp"].toString(),
+                                false, friendMap["uid"].toString(),
+                                distanceMap["units"].toString(),
+                                distanceMap["value"].toDouble());
 
-      m_friendsList.append(user);
+          m_friendsList.append(user);
+        }
+        addProfileImages();
     }
-    addProfileImages();
 }
 
 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
index 1b40c14..4ed475f 100644 (file)
@@ -159,7 +159,7 @@ private:
     *
     * @param jsonReply JSON string
     */
-    void parseUserData(const QByteArray &jsonReply);
+    void parseUserData(/*const*/ QByteArray &jsonReply);
 
     /**
     * @brief Sends http request