Merge branch 'note_queue'
authorlampehe-local <henri.lampela@ixonos.com>
Tue, 8 Jun 2010 10:59:12 +0000 (13:59 +0300)
committerlampehe-local <henri.lampela@ixonos.com>
Tue, 8 Jun 2010 10:59:12 +0000 (13:59 +0300)
src/engine/engine.cpp
src/facebookservice/facebookauthentication.cpp
src/facebookservice/facebookauthentication.h
src/ui/mainwindow.cpp
src/ui/mainwindow.h

index aa94bfb..f569991 100644 (file)
@@ -84,8 +84,6 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
 
-    initializeGpsAndAutocentering();
-
     // signals connected, now it's time to show the main window
     // but init the MapEngine before so starting location is set
     m_mapEngine->init();
@@ -93,6 +91,8 @@ SituareEngine::SituareEngine(QMainWindow *parent)
 
     m_facebookAuthenticator->start();
 
+    initializeGpsAndAutocentering();
+
     m_automaticUpdateIntervalTimer = new QTimer(this);
     connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
             this, SLOT(automaticUpdateIntervalTimerTimeout()));
@@ -133,7 +133,7 @@ void SituareEngine::disableAutoCentering()
     qDebug() << __PRETTY_FUNCTION__;
 
     changeAutoCenteringSetting(false);
-    m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
+    m_ui->buildInformationBox(tr("Auto centering disabled"));
 }
 
 void SituareEngine::enableAutoCentering(bool enabled)
@@ -185,7 +185,7 @@ void SituareEngine::error(const QString &error)
     qDebug() << __PRETTY_FUNCTION__;
     qDebug() << "ERROR MESSAGE: " << error;
 
-    m_ui->showMaemoInformationBox(error, true);
+    m_ui->buildInformationBox(error, true);
 
     if(error.compare(SESSION_EXPIRED) == 0) {
         m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
@@ -217,18 +217,19 @@ void SituareEngine::initializeGpsAndAutocentering()
         changeAutoCenteringSetting(true);
         enableGPS(true);
 
-        m_ui->showMaemoInformationBox(tr("GPS enabled"));
-        m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
+        m_ui->buildInformationBox(tr("GPS enabled"));
+        m_ui->buildInformationBox(tr("Auto centering enabled"));
+
     } else { // Normal start
         changeAutoCenteringSetting(autoCenteringEnabled.toBool());
         enableGPS(gpsEnabled.toBool());
 
         if (gpsEnabled.toBool())
-            m_ui->showMaemoInformationBox(tr("GPS enabled"));
+            m_ui->buildInformationBox(tr("GPS enabled"));
 
         if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
-            m_ui->showMaemoInformationBox(tr("Auto centering enabled"));        
-    } 
+            m_ui->buildInformationBox(tr("Auto centering enabled"));
+    }
 }
 
 bool SituareEngine::isUserMoved()
@@ -351,8 +352,8 @@ void SituareEngine::signalsFromFacebookAuthenticator()
     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
             this, SLOT(loginOk()));
 
-    connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
-            m_ui, SLOT(startLoginProcess(QUrl)));
+    connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
+            m_ui, SLOT(startLoginProcess()));
 
     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
             m_ui, SLOT(loginFailed()));
index f376d1f..00293e2 100644 (file)
@@ -38,9 +38,7 @@
 
 FacebookAuthentication::FacebookAuthentication(QObject *parent)
     : QObject(parent),
-    m_freshLogin(false),
-    m_loginAttempts(0)
-
+    m_freshLogin(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -93,17 +91,7 @@ void FacebookAuthentication::start()
     }
     else {
         m_freshLogin = true;
-
-        QStringList list;
-        list.append(FACEBOOK_LOGINBASE);
-        list.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
-        list.append(INTERVAL1);
-        list.append(SITUARE_LOGIN_SUCCESS);
-        list.append(INTERVAL2);
-        list.append(SITUARE_LOGIN_FAILURE);
-        list.append(FACEBOOK_LOGIN_ENDING);
-
-        emit newLoginRequest(list.join(EMPTY));
+        emit newLoginRequest();
     }
 }
 
@@ -172,20 +160,14 @@ bool FacebookAuthentication::updateCredentials(const QUrl &url)
         else if ( callbackUrl.indexOf(LOGIN_FAILURE_REPLY) == 0){
             qWarning() << "login failure" << endl;
             qDebug() << callbackUrl;
-            ++m_loginAttempts;
-            /* emit loginFailure for every second login attemps, since webview loads login
-               error page (loadingDone() signal is emitted) and we need to avoid that because
-               at this point we don't have new login parameters */
-            if(m_loginAttempts % 2) {
-                clearAccountInformation(true);
-                if(m_freshLogin) {
-                    emit error(LOGIN_FAILED);
-                    emit loginFailure();
-                }
-                else {
-                    m_freshLogin = true;
-                    emit error(SESSION_EXPIRED);
-                }
+            clearAccountInformation(true);
+            if(m_freshLogin) {
+                emit error(LOGIN_FAILED);
+                emit loginFailure();
+            }
+            else {
+                m_freshLogin = true;
+                emit error(SESSION_EXPIRED);
             }
         }
         else if(callbackUrl.indexOf(LOGIN_PAGE) == 0) {
index 8a296da..4ffea66 100644 (file)
@@ -139,9 +139,8 @@ signals:
     /**
     * @brief Signals when credentials are invalid new login is needed
     *
-    * @param url Login page url
     */
-    void newLoginRequest(const QUrl &url);
+    void newLoginRequest();
 
     /**
     * @brief This signal is emitted when new cookies need to be saved.
@@ -157,8 +156,6 @@ private:
 
     bool m_freshLogin;
 
-    int m_loginAttempts; ///< Indicates login attempts
-
     /**
     * @brief Dataclass that contains authorization to use facebook. Dataclass is composed of five
     *        QStrings and setters and getters.
index fbc4d5f..40b4875 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <QtGui>
 #include <QtWebKit>
+#include <QtAlgorithms>
 
 #include "common.h"
 #include "facebookservice/facebookauthentication.h"
@@ -52,6 +53,7 @@ const int N900_APP_HEIGHT = 449;
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent),
     m_drawOwnLocationCrosshair(false),
+    m_errorShown(false),
     m_loggedIn(false),
     m_refresh(false),
     m_ownLocationCrosshair(0),
@@ -109,6 +111,12 @@ MainWindow::~MainWindow()
 
     if(m_webView)
         delete m_webView;
+
+    qDeleteAll(m_queue.begin(), m_queue.end());
+    m_queue.clear();
+
+    qDeleteAll(m_error_queue.begin(), m_error_queue.end());
+    m_error_queue.clear();
 }
 
 void MainWindow::buildFullScreenButton()
@@ -149,6 +157,32 @@ void MainWindow::buildFriendListPanel()
             m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
 }
 
+void MainWindow::buildInformationBox(const QString &message, bool modal)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+#ifdef Q_WS_MAEMO_5
+    QMaemo5InformationBox *msgBox = new QMaemo5InformationBox(this);
+    QLabel *label = new QLabel(msgBox);
+    label->setAlignment(Qt::AlignCenter);
+    label->setText(message);
+    msgBox->setWidget(label);
+
+    if(modal) {
+        msgBox->setTimeout(QMaemo5InformationBox::NoTimeout);
+    } else {
+        msgBox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
+    }
+#else
+    QMessageBox *msgBox = new QMessageBox(this);
+    msgBox->button(QMessageBox::Ok);
+    msgBox->setText(message);
+    msgBox->setModal(modal);
+#endif
+
+    queueDialog(msgBox);
+}
+
 void MainWindow::buildManualLocationCrosshair()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -261,6 +295,13 @@ void MainWindow::buildWebView()
     if(!m_webView) {
         m_webView = new QWebView;
 
+        if(!m_cookieJar)
+            m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
+
+        m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
+
+        connect(m_webView->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
+                this, SLOT(webViewRequestFinished(QNetworkReply*)));
         connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
                 this, SIGNAL(updateCredentials(QUrl)));
         connect(m_webView, SIGNAL(loadFinished(bool)),
@@ -347,6 +388,46 @@ void MainWindow::createMenus()
     m_viewMenu->setObjectName(tr("Menu"));
 }
 
+void MainWindow::dialogFinished(int status)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QDialog *dialog = m_queue.takeFirst();
+    LoginDialog *loginDialog = qobject_cast<LoginDialog *>(dialog);
+    if(loginDialog) {
+        if(status != 0) {
+            buildWebView();
+            loginDialog->userInput(m_email, m_password);
+
+            QStringList urlParts;
+            urlParts.append(FACEBOOK_LOGINBASE);
+            urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
+            urlParts.append(INTERVAL1);
+            urlParts.append(SITUARE_LOGIN_SUCCESS);
+            urlParts.append(INTERVAL2);
+            urlParts.append(SITUARE_LOGIN_FAILURE);
+            urlParts.append(FACEBOOK_LOGIN_ENDING);
+
+            emit saveUsername(m_email);
+            m_refresh = true;
+            m_webView->load(QUrl(urlParts.join(EMPTY)));
+            toggleProgressIndicator(true);
+        } else {
+            emit cancelLoginProcess();
+        }
+    }
+
+    dialog->deleteLater();
+
+    if(!m_error_queue.isEmpty() && m_errorShown == false) {
+        showErrorInformationBox();
+    } else {
+        if(!m_queue.isEmpty()) {
+            showInformationBox();
+        }
+    }
+}
+
 void MainWindow::drawFullScreenButton(const QSize &size)
 {
     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
@@ -377,11 +458,26 @@ void MainWindow::drawOwnLocationCrosshair(int width, int height)
     }
 }
 
+void MainWindow::errorDialogFinished(int status)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    qDebug() << status;
+    QDialog *dialog = m_error_queue.takeFirst();
+
+    dialog->deleteLater();
+    m_errorShown = false;
+
+    if(!m_error_queue.isEmpty()) {
+        showErrorInformationBox();
+    }
+}
+
 void MainWindow::gpsTimeout()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    showMaemoInformationBox(tr("GPS timeout"));
+    buildInformationBox(tr("GPS timeout"));
 }
 
 void MainWindow::grabZoomKeys(bool grab)
@@ -504,8 +600,7 @@ void MainWindow::loggedIn(bool logged)
 
     if(logged) {
         m_loginAct->setText(tr("Logout"));
-    }
-    else {
+    } else {
         clearCookieJar();
         m_email.clear();
         m_password.clear();
@@ -523,16 +618,7 @@ void MainWindow::loginFailed()
 
     toggleProgressIndicator(false);
 
-    QStringList urlParts;
-    urlParts.append(FACEBOOK_LOGINBASE);
-    urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
-    urlParts.append(INTERVAL1);
-    urlParts.append(SITUARE_LOGIN_SUCCESS);
-    urlParts.append(INTERVAL2);
-    urlParts.append(SITUARE_LOGIN_FAILURE);
-    urlParts.append(FACEBOOK_LOGIN_ENDING);
-
-    startLoginProcess(urlParts.join(EMPTY));
+    startLoginProcess();
 }
 
 void MainWindow::loginUsingCookies()
@@ -563,6 +649,24 @@ void MainWindow::openSettingsDialog()
     m_settingsDialog->show();
 }
 
+void MainWindow::queueDialog(QDialog *dialog)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    // check is dialog is modal, for now all modal dialogs have hihger priority i.e. errors
+    if(dialog->isModal()) {
+        m_error_queue.append(dialog);
+    } else {
+        m_queue.append(dialog);
+    }
+
+    // show error dialog if there is only one error dialog in the queue and no error dialog is shown
+    if(m_error_queue.count() == 1 && m_errorShown == false)
+        showErrorInformationBox();
+    else if(m_queue.count() == 1 && m_errorShown == false)
+        showInformationBox();
+}
+
 void MainWindow::saveCookies()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -618,8 +722,7 @@ void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
         m_ownLocationCrosshair->show();
         m_drawOwnLocationCrosshair = true;
         drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
-    }
-    else {
+    } else {
         m_ownLocationCrosshair->hide();
         m_drawOwnLocationCrosshair = false;
     }
@@ -640,23 +743,6 @@ void MainWindow::setViewPortSize(int width, int height)
     m_viewPortHeight = height;
 }
 
-void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-#ifdef Q_WS_MAEMO_5
-    if(modal) {
-        QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
-    }
-    else {
-        QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
-    }
-#else
-    Q_UNUSED(modal);
-    QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
-#endif
-}
-
 void MainWindow::toggleFullScreen()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -667,39 +753,45 @@ void MainWindow::toggleFullScreen()
         showNormal();
 }
 
-void MainWindow::startLoginProcess(const QUrl &url)
+void MainWindow::showErrorInformationBox()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    buildWebView();
+    if(m_error_queue.count()) {
+        m_errorShown = true;
+        QDialog *dialog = m_error_queue.first();
+        connect(dialog, SIGNAL(finished(int)),
+                this, SLOT(errorDialogFinished(int)));
+        dialog->show();
+    }
+}
 
-    LoginDialog loginDialog;
+void MainWindow::showInformationBox()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    emit fetchUsernameFromSettings();
+    if(m_queue.count()) {
+        QDialog *dialog = m_queue.first();
+        connect(dialog, SIGNAL(finished(int)),
+                this, SLOT(dialogFinished(int)));
+        dialog->show();
+    }
+}
 
-    if(!m_cookieJar)
-        m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
+void MainWindow::startLoginProcess()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
+    LoginDialog *loginDialog = new LoginDialog();
 
-    loginDialog.clearTextFields();
+    emit fetchUsernameFromSettings();
 
-    if(!m_email.isEmpty())
-        loginDialog.setEmailField(m_email);
+    loginDialog->clearTextFields();
 
-    if(loginDialog.exec() != QDialog::Accepted) {
-        // if login dialog was canceled we need to stop processing webview
-        m_webView->stop();
+    if(!m_email.isEmpty())
+        loginDialog->setEmailField(m_email);
 
-        emit cancelLoginProcess();
-    }
-    else {
-        loginDialog.userInput(m_email, m_password);
-        emit saveUsername(m_email);
-        m_webView->load(url);
-        toggleProgressIndicator(true);
-        m_refresh = true;
-    }
+    queueDialog(loginDialog);
 }
 
 void MainWindow::toggleProgressIndicator(bool value)
@@ -728,8 +820,7 @@ void MainWindow::updateItemVisibility(bool show)
             setGPSButtonEnabled(false);
             emit gpsTriggered(false);
         }
-    }
-    else {
+    } else {
         m_friendsListPanel->closePanel();
         m_friendsListPanel->hide();
         m_friendsListPanelSidebar->hide();
@@ -746,3 +837,14 @@ const QString MainWindow::username()
     
     return m_email;
 }
+
+void MainWindow::webViewRequestFinished(QNetworkReply *reply)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(reply->error()) {
+        qDebug() << reply->error() << reply->errorString();
+        toggleProgressIndicator(false);
+    }
+}
+
index 44d9b67..18c7406 100644 (file)
@@ -33,6 +33,7 @@
 class QGraphicsScene;
 class QLabel;
 class QWebView;
+class QNetworkReply;
 
 class FacebookAuthentication;
 class FriendListPanel;
@@ -80,6 +81,14 @@ private:
  ******************************************************************************/
 public:
     /**
+    * @brief Builds information box with message.
+    *
+    * @param message Information message
+    * @param modal Modal = true, non-modal false
+    */
+    void buildInformationBox(const QString &message, bool modal=false);
+
+    /**
     * @brief Clears cookie jar
     *
     */
@@ -116,14 +125,6 @@ public:
     void setMapViewScene(QGraphicsScene *scene);
 
     /**
-    * @brief Show Maemo information box with message.
-    *
-    * @param message Information message
-    * @param modal Modal = true, non-modal false
-    */
-    void showMaemoInformationBox(const QString &message, bool modal=false);
-
-    /**
     * @brief Gets the username from member variable for saving purposes
     *
     * @return QString Username
@@ -159,9 +160,8 @@ public slots:
     * @brief Public slot to intercept signal when old cerdentials are invalid or credentials
     *        doesn't exist yet
     *
-    * @param url Login page url
     */
-    void startLoginProcess(const QUrl &url);
+    void startLoginProcess();
 
     /**
     * @brief Toggle progress indicator.
@@ -232,14 +232,41 @@ private:
     void grabZoomKeys(bool grab);
 
     /**
+    * @brief Queues dialog/information box
+    *
+    * @param dialog Dialog to be added into queue
+    */
+    void queueDialog(QDialog *dialog);
+
+    /**
       * @brief Set own location crosshair visibility
       *
       * @param visible
       */
     void setOwnLocationCrosshairVisibility(bool visible);
 
+    /**
+    * @brief Shows queued error information box
+    *
+    */
+    void showErrorInformationBox();
+
+    /**
+    * @brief Shows queued information box
+    *
+    * @fn showInformationBox
+    */
+    void showInformationBox();
+
 private slots:
     /**
+    * @brief Slot to intercept signal when dialog/information note is processed
+    *
+    * @param status Status of the dialog
+    */
+    void dialogFinished(int status);
+
+    /**
     * @brief Slot for drawing the fullscreen toggle button
     *
     * @param size Size of the screen
@@ -262,6 +289,13 @@ private slots:
     void drawOwnLocationCrosshair(int width, int height);
 
     /**
+    * @brief Slot to intercept signal when error dialog/information note is processed
+    *
+    * @param status Status of the dialog
+    */
+    void errorDialogFinished(int status);
+
+    /**
     * @brief Slot for gps timeout.
     *
     * Called when request timeout occurs.
@@ -300,6 +334,13 @@ private slots:
     */
     void toggleFullScreen();
 
+    /**
+    * @brief Slot to intercept signal from webview's networkaccessmanager
+    *
+    * @param reply Network reply (contains errors)
+    */
+    void webViewRequestFinished(QNetworkReply* reply);
+
 /*******************************************************************************
  * SIGNALS
  ******************************************************************************/
@@ -496,6 +537,7 @@ signals:
  ******************************************************************************/
 private:
     bool m_drawOwnLocationCrosshair;        ///< Flag for making ownLocationCrosshair visible or not
+    bool m_errorShown;                      ///< Indicates if error dialog/note is shown
     bool m_loggedIn;                        ///< Indicates login state
     bool m_refresh;                         ///< Indicates when webpage is refreshed
 
@@ -510,6 +552,9 @@ private:
     QLabel *m_osmLicense;                   ///< Label for Open Street Map license
     QLabel *m_ownLocationCrosshair;         ///< Label that show ownLocationCrosshair
 
+    QList<QDialog *> m_error_queue;         ///< QList type error dialog queue
+    QList<QDialog *> m_queue;               ///< QList type dialog queue
+
     QMenu *m_viewMenu;                      ///< Object that hold the view menu items
 
     QString m_email;                        ///< Placeholder for email
@@ -528,6 +573,7 @@ private:
     ZoomButtonPanel *m_zoomButtonPanel;     ///< Instance of zoom button panel
 
     SettingsDialog *m_settingsDialog;       ///< Settings dialog
+
 };
 
 #endif // MAINWINDOW_H