Added confirmation dialog to automatic location update feature.
[situare] / src / ui / mainwindow.cpp
index bd5bbd5..80dd084 100644 (file)
 #include <QtGui>
 #include <QtWebKit>
 
-#ifdef Q_WS_MAEMO_5
-#include <QtMaemo5/QMaemo5InformationBox>
-#endif // Q_WS_MAEMO_5
-
 #include "common.h"
 #include "facebookservice/facebookauthentication.h"
 #include "friendlistpanel.h"
 
 #include "mainwindow.h"
 
+// These MUST BE HERE, compiling for Maemo fails if moved
+#ifdef Q_WS_MAEMO_5
+#include <QtMaemo5/QMaemo5InformationBox>
 #include <QtGui/QX11Info>
-#include <X11/Xlib.h>
 #include <X11/Xatom.h>
+#include <X11/Xlib.h>
+#endif // Q_WS_MAEMO_5
 
 // values for setting screen size in desktop matching N900 screen size
 const int N900_APP_WIDTH = 800;
 const int N900_APP_HEIGHT = 449;
 
-
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent),
     m_drawOwnLocationCrosshair(false),
     m_loggedIn(false),
     m_refresh(false),
+    m_ownLocationCrosshair(0),
     m_email(),    
     m_password(),
+    m_fullScreenButton(0),
     m_webView(0),
     m_cookieJar(0)
 {
@@ -74,12 +75,21 @@ MainWindow::MainWindow(QWidget *parent)
     buildFriendListPanel();
     buildUserInfoPanel();
 
+    m_settingsDialog = new SettingsDialog(this);
+    m_settingsDialog->hide();
+    connect(m_settingsDialog, SIGNAL(enableAutomaticLocationUpdate(bool,int)),
+            this, SIGNAL(enableAutomaticLocationUpdate(bool,int)));
+
     createMenus();
     setWindowTitle(tr("Situare"));
 
     // set stacking order of widgets
     m_zoomButtonPanel->stackUnder(m_userPanel);
-    m_osmLicense->stackUnder(m_zoomButtonPanel);
+    if(m_fullScreenButton) {
+        m_fullScreenButton->stackUnder(m_zoomButtonPanel);
+        m_osmLicense->stackUnder(m_fullScreenButton);
+    } else
+        m_osmLicense->stackUnder(m_zoomButtonPanel);
     m_ownLocationCrosshair->stackUnder(m_osmLicense);
     m_mapView->stackUnder(m_ownLocationCrosshair);
 
@@ -101,6 +111,24 @@ MainWindow::~MainWindow()
         delete m_webView;
 }
 
+void MainWindow::automaticLocationUpdateEnabled(bool enabled)
+{
+    m_settingsDialog->setAutomaticLocationUpdateSettings(enabled);
+}
+
+void MainWindow::buildFullScreenButton()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+#ifdef Q_WS_MAEMO_5
+    m_fullScreenButton = new QToolButton(this);
+    m_fullScreenButton->setIcon(QIcon::fromTheme(QLatin1String("general_fullsize")));
+    m_fullScreenButton->setFixedSize(m_fullScreenButton->sizeHint());
+    connect(m_fullScreenButton, SIGNAL(clicked()),
+            this, SLOT(toggleFullScreen()));
+#endif // Q_WS_MAEMO_5
+
+}
+
 void MainWindow::buildFriendListPanel()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -126,16 +154,6 @@ void MainWindow::buildFriendListPanel()
             m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
 }
 
-void MainWindow::buildLoginDialog()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_loginDialog = new LoginDialog(this);
-
-    connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
-            this, SLOT(loginDialogDone(QString,QString)));
-}
-
 void MainWindow::buildManualLocationCrosshair()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -158,10 +176,9 @@ void MainWindow::buildMap()
     m_mapView = new MapView(this);
 
     buildZoomButtonPanel();
-
-    m_ownLocationCrosshair = 0;
     buildOsmLicense();
     buildManualLocationCrosshair();
+    buildFullScreenButton();
 
     connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
             this, SIGNAL(mapViewScrolled(QPoint)));
@@ -172,6 +189,9 @@ void MainWindow::buildMap()
     connect(m_mapView, SIGNAL(viewResized(QSize)),
             this, SIGNAL(mapViewResized(QSize)));
 
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            this, SLOT(drawFullScreenButton(QSize)));
+
     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
              this, SLOT(setViewPortSize(int, int)));
 
@@ -184,7 +204,7 @@ void MainWindow::buildMap()
 
 void MainWindow::buildOsmLicense()
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     m_osmLicense = new QLabel(this);
     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
@@ -208,6 +228,9 @@ void MainWindow::buildUserInfoPanel()
     m_userPanelSidebar->stackUnder(m_friendsListPanel);
     m_userPanel->stackUnder(m_userPanelSidebar);
 
+    connect(m_userPanel, SIGNAL(findUser(QPointF)),
+            this, SIGNAL(findUser(QPointF)));
+
     connect(this, SIGNAL(userLocationReady(User*)),
             m_userPanel, SLOT(userDataReceived(User*)));
 
@@ -232,7 +255,6 @@ void MainWindow::buildWebView()
     qDebug() << __PRETTY_FUNCTION__;
 
     if(!m_webView) {
-        qDebug() << "create webview";
         m_webView = new QWebView;
 
         connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
@@ -248,8 +270,7 @@ void MainWindow::buildZoomButtonPanel()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
-                                            ZOOM_BUTTON_PANEL_POSITION_Y);
+    m_zoomButtonPanel = new ZoomButtonPanel(this);
 
     connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
             this, SIGNAL(zoomIn()));
@@ -265,6 +286,9 @@ void MainWindow::buildZoomButtonPanel()
 
     connect(this, SIGNAL(minZoomLevelReached()),
             m_zoomButtonPanel, SLOT(disableZoomOutButton()));
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            m_zoomButtonPanel, SLOT(screenResized(QSize)));
 }
 
 void MainWindow::clearCookieJar()
@@ -275,7 +299,6 @@ void MainWindow::clearCookieJar()
 
     if(!m_cookieJar) {
         m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
-        qDebug() << "create cookie jar";
     }
     QList<QNetworkCookie> emptyList;
     emptyList.clear();
@@ -320,13 +343,24 @@ void MainWindow::createMenus()
     m_viewMenu->setObjectName(tr("Menu"));
 }
 
-void MainWindow::drawOsmLicense(const QSize &size)
+void MainWindow::drawFullScreenButton(const QSize &size)
 {
     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
 
-    m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
-                        size.height() - m_osmLicense->fontMetrics().height());
+    if(m_fullScreenButton) {
+        m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
+                                 - PANEL_PEEK_AMOUNT,
+                                 size.height() - m_fullScreenButton->size().height());
+    }
+}
 
+void MainWindow::drawOsmLicense(const QSize &size)
+{
+    qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
+
+    m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
+                       - PANEL_PEEK_AMOUNT,
+                       size.height() - m_osmLicense->fontMetrics().height());
 }
 
 void MainWindow::drawOwnLocationCrosshair(int width, int height)
@@ -339,13 +373,6 @@ void MainWindow::drawOwnLocationCrosshair(int width, int height)
     }
 }
 
-void MainWindow::gpsError(const QString &message)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    showMaemoInformationBox(message);
-}
-
 void MainWindow::gpsTimeout()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -404,23 +431,20 @@ void MainWindow::loadCookies()
 
     QSettings settings(DIRECTORY_NAME, FILE_NAME);
 
-    QString cookies = settings.value(COOKIES, EMPTY).toString();
-    if(!cookies.isEmpty()) {
-        QStringList list = cookies.split("|");
-        QList<QNetworkCookie> cookieList;
+    QStringList list = settings.value(COOKIES, EMPTY).toStringList();
 
+    if(!list.isEmpty()) {
+        QList<QNetworkCookie> cookieList;
         for(int i=0;i<list.count();i++) {
-            QNetworkCookie tmpCookie;
-            QList<QNetworkCookie> parserList = tmpCookie.parseCookies(list.at(i).toAscii());
-            cookieList.append(parserList.at(0));
-        }
-        if(!m_cookieJar) {
-            m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
-            qDebug() << "create cookie jar";
+            cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
         }
 
+        if(!m_cookieJar)
+               m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
+
         m_cookieJar->setAllCookies(cookieList);
         m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
+
     }
 }
 
@@ -482,72 +506,29 @@ void MainWindow::loggedIn(bool logged)
         m_email.clear();
         m_password.clear();
 
-//        if(m_loginDialog)
-//            m_loginDialog->clearTextFields();
-
         m_loginAct->setText(tr("Login"));
     }
-    showPanels(m_loggedIn);
-}
-
-void MainWindow::loginDialogDone(const QString &email, const QString &password)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_email = email;
-    m_password = password;
+    updateItemVisibility(m_loggedIn);
 }
 
 void MainWindow::loginFailed()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    toggleProgressIndicator(false);
-
-#ifdef Q_WS_MAEMO_5
-    QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
-                                       QMaemo5InformationBox::NoTimeout);
-
-#endif // Q_WS_MAEMO_5
-
-    buildLoginDialog();
-
-    if(!m_email.isEmpty()) {
-        m_loginDialog->setEmailField(m_email);
-    }
-
-    if(m_loginDialog->exec() != QDialog::Accepted) {
-        // if login dialog was canceled we need to stop processing webview
-        // stop and disconnect m_webView;
-        m_webView->stop();
-        disconnect(m_webView, SIGNAL(loadFinished(bool)),
-                   this, SLOT(loadDone(bool)));
-        disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
-                   this, SLOT(updateCredentials(const QUrl &)));
+    clearCookieJar();
 
-        emit cancelLoginProcess();
-    }
-    else {
-        // re-load login page for webview
-        toggleProgressIndicator(true);
-
-        // connect webview signals again since they might have been disconnected
-        connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
-                this, SIGNAL(updateCredentials(QUrl)));
-        connect(m_webView, SIGNAL(loadFinished(bool)),
-                this, SLOT(loadDone(bool)));
+    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);
+    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);
 
-        m_webView->load(QUrl(urlParts.join(EMPTY)));
-    }
+    startLoginProcess(urlParts.join(EMPTY));
 }
 
 void MainWindow::loginUsingCookies()
@@ -574,11 +555,8 @@ void MainWindow::openSettingsDialog()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    SettingsDialog *dialog = new SettingsDialog(this);
-    if(!m_loggedIn) {
-        dialog->disableSituareSettings();
-    }
-    dialog->show();
+    m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
+    m_settingsDialog->show();
 }
 
 void MainWindow::saveCookies()
@@ -596,9 +574,10 @@ void MainWindow::saveCookies()
         QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
         list.append(QString(byteArray));
     }
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    list.removeDuplicates();
 
-    settings.setValue(COOKIES, list.join("|"));
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    settings.setValue(COOKIES, list);
 }
 
 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
@@ -613,7 +592,10 @@ void MainWindow::setGPSButtonEnabled(bool enabled)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_gpsToggleAct->setChecked(enabled);
-    setOwnLocationCrosshairVisibility(!enabled);
+
+    if(m_loggedIn)
+        setOwnLocationCrosshairVisibility(!enabled);
+
     m_autoCenteringAct->setVisible(enabled);
 }
 
@@ -642,6 +624,7 @@ void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
 void MainWindow::setUsername(const QString &username)
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     m_email = username;
 }
 
@@ -653,34 +636,45 @@ void MainWindow::setViewPortSize(int width, int height)
     m_viewPortHeight = height;
 }
 
-void MainWindow::showMaemoInformationBox(const QString &message)
+bool MainWindow::showEnableAutomaticUpdateLocationDialog()
+{
+    QMessageBox msgBox(QMessageBox::Warning, tr("Automatic location update"),
+                       tr("Are you sure you want to enable automatic location update?"),
+                       QMessageBox::Ok | QMessageBox::Cancel, 0);
+    msgBox.button(QMessageBox::Ok)->setText(tr("Ok"));
+    int ret = msgBox.exec();
+
+    if (ret == QMessageBox::Ok)
+        return true;
+    else
+        return false;
+}
+
+void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
 #ifdef Q_WS_MAEMO_5
-    QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
+    if(modal) {
+        QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
+    }
+    else {
+        QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
+    }
 #else
-    Q_UNUSED(message);
+    Q_UNUSED(modal);
+    QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
 #endif
 }
 
-void MainWindow::showPanels(bool show)
+void MainWindow::toggleFullScreen()
 {
     qDebug() << __PRETTY_FUNCTION__;
-    if(show) {
-        m_friendsListPanel->show();
-        m_friendsListPanelSidebar->show();
-        m_userPanel->show();
-        m_userPanelSidebar->show();
-    }
-    else {
-        m_friendsListPanel->closePanel();
-        m_friendsListPanel->hide();
-        m_friendsListPanelSidebar->hide();
-        m_userPanel->closePanel();
-        m_userPanel->hide();
-        m_userPanelSidebar->hide();
-    }
+
+    if(windowState() == Qt::WindowNoState)
+        showFullScreen();
+    else
+        showNormal();
 }
 
 void MainWindow::startLoginProcess(const QUrl &url)
@@ -688,24 +682,8 @@ void MainWindow::startLoginProcess(const QUrl &url)
     qDebug() << __PRETTY_FUNCTION__;
 
     buildWebView();
-    buildLoginDialog();
-//    if(!m_webView)
-//        m_webView = new QWebView;
-
-//    if(!m_loginDialog)
-//        m_loginDialog = new LoginDialog(this);
-
 
-
-//    connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
-//            this, SIGNAL(updateCredentials(QUrl)));
-//    connect(m_webView, SIGNAL(loadFinished(bool)),
-//            this, SLOT(loadDone(bool)));
-
-//    connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
-//            this, SLOT(loginDialogDone(QString,QString)));
-
-//    m_webView->hide();
+    LoginDialog loginDialog;
 
     emit fetchUsernameFromSettings();
 
@@ -714,28 +692,20 @@ void MainWindow::startLoginProcess(const QUrl &url)
 
     m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
 
-    if(!m_email.isEmpty()) {
-        m_loginDialog->setEmailField(m_email);
-    }
+    loginDialog.clearTextFields();
+
+    if(!m_email.isEmpty())
+        loginDialog.setEmailField(m_email);
 
-    if(m_loginDialog->exec() != QDialog::Accepted) {
+    if(loginDialog.exec() != QDialog::Accepted) {
         // if login dialog was canceled we need to stop processing webview
-        // stop and disconnect m_webView;
         m_webView->stop();
-        disconnect(m_webView, SIGNAL(loadFinished(bool)),
-                   this, SLOT(loadDone(bool)));
-        disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
-                   this, SLOT(updateCredentials(const QUrl &)));
 
         emit cancelLoginProcess();
     }
     else {
-        // connect webview signals again since they might have been disconnected
-        connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
-                this, SIGNAL(updateCredentials(QUrl)));
-        connect(m_webView, SIGNAL(loadFinished(bool)),
-                this, SLOT(loadDone(bool)));
-
+        loginDialog.userInput(m_email, m_password);
+        emit saveUsername(m_email);
         m_webView->load(url);
         toggleProgressIndicator(true);
         m_refresh = true;
@@ -753,8 +723,36 @@ void MainWindow::toggleProgressIndicator(bool value)
 #endif // Q_WS_MAEMO_5
 }
 
+void MainWindow::updateItemVisibility(bool show)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+    
+    if(show) {
+        m_friendsListPanel->show();
+        m_friendsListPanelSidebar->show();
+        m_userPanel->show();
+        m_userPanelSidebar->show();
+
+        if(m_drawOwnLocationCrosshair) {
+            m_ownLocationCrosshair->show();
+            setGPSButtonEnabled(false);
+            emit gpsTriggered(false);
+        }
+    }
+    else {
+        m_friendsListPanel->closePanel();
+        m_friendsListPanel->hide();
+        m_friendsListPanelSidebar->hide();
+        m_userPanel->closePanel();
+        m_userPanel->hide();
+        m_userPanelSidebar->hide();
+        m_ownLocationCrosshair->hide();       
+    }
+}
+
 const QString MainWindow::username()
 {
     qDebug() << __PRETTY_FUNCTION__;
+    
     return m_email;
 }