Merge branch 'master' into locationlistview
[situare] / src / ui / mainwindow.cpp
index cfb0edc..5a74bde 100644 (file)
 
 #include <QtGui>
 #include <QtWebKit>
+#include <QtAlgorithms>
 
-#ifdef Q_WS_MAEMO_5
-#include <QtMaemo5/QMaemo5InformationBox>
-#endif // Q_WS_MAEMO_5
-
-#include "common.h"
 #include "facebookservice/facebookauthentication.h"
+#include "map/mapcommon.h"
+#include "map/mapview.h"
+#include "common.h"
 #include "friendlistpanel.h"
+#include "fullscreenbutton.h"
+#include "indicatorbutton.h"
 #include "logindialog.h"
-#include "map/mapview.h"
+#include "mapscale.h"
+#include "searchdialog.h"
 #include "settingsdialog.h"
 #include "userinfopanel.h"
 #include "zoombuttonpanel.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>
-
-// values for setting screen size in desktop matching N900 screen size
-const int N900_APP_WIDTH = 800;
-const int N900_APP_HEIGHT = 449;
+#include <X11/Xlib.h>
+#endif // Q_WS_MAEMO_5
 
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent),
     m_drawOwnLocationCrosshair(false),
+    m_errorShown(false),
     m_loggedIn(false),
     m_refresh(false),
-    m_email(),    
+    m_progressIndicatorCount(0),
+    m_ownLocationCrosshair(0),
+    m_email(),
     m_password(),
-    m_loginUrl(),
-    m_webView(0)
+    m_webView(0),
+    m_fullScreenButton(0),
+    m_indicatorButton(0),
+    m_mapScale(0),
+    m_cookieJar(0)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -67,6 +75,8 @@ MainWindow::MainWindow(QWidget *parent)
     QHBoxLayout *layout = new QHBoxLayout;
     layout->addWidget(m_mapView);
     layout->setMargin(0);
+    layout->setSpacing(0);
+
     setCentralWidget(new QWidget());
     centralWidget()->setLayout(layout);
 
@@ -78,16 +88,24 @@ MainWindow::MainWindow(QWidget *parent)
 
     // 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);
-
-    this->toggleProgressIndicator(true);
+    m_indicatorButton->stackUnder(m_ownLocationCrosshair);
+    m_mapScale->stackUnder(m_indicatorButton);
+    m_mapView->stackUnder(m_mapScale);
 
     grabZoomKeys(true);
 
-    // set screen size in desktop matching N900 screen size
-    resize(N900_APP_WIDTH, N900_APP_HEIGHT);
+    // Set default screen size
+    resize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT);
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5StackedWindow);
+#endif
 }
 
 MainWindow::~MainWindow()
@@ -98,6 +116,37 @@ 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::automaticUpdateDialogFinished(int result)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (result == QMessageBox::Yes) {
+        readAutomaticLocationUpdateSettings();
+    } else {
+        QSettings settings(DIRECTORY_NAME, FILE_NAME);
+        settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false);
+        readAutomaticLocationUpdateSettings();
+    }
+
+    m_automaticUpdateLocationDialog->deleteLater();
+}
+
+void MainWindow::buildFullScreenButton()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+#ifdef Q_WS_MAEMO_5
+    m_fullScreenButton = new FullScreenButton(this);
+    connect(m_fullScreenButton, SIGNAL(clicked()),
+            this, SLOT(toggleFullScreen()));
+#endif // Q_WS_MAEMO_5
 }
 
 void MainWindow::buildFriendListPanel()
@@ -105,6 +154,7 @@ void MainWindow::buildFriendListPanel()
     qDebug() << __PRETTY_FUNCTION__;
 
     m_friendsListPanel = new FriendListPanel(this);
+
     m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
 
     m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
@@ -112,17 +162,71 @@ void MainWindow::buildFriendListPanel()
     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
             m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
 
-    connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
-            this, SIGNAL(findFriend(QPointF)));
+    connect(this, SIGNAL(locationItemClicked(QList<QString>)),
+            m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            m_friendsListPanel, SLOT(resizePanel(QSize)));
 
     connect(m_mapView, SIGNAL(viewResized(QSize)),
-            m_friendsListPanel, SLOT(screenResized(QSize)));
+            m_friendsListPanelSidebar, SLOT(resizeSideBar(QSize)));
 
-    connect(this, SIGNAL(locationItemClicked(QList<QString>)),
-            m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
+    connect(m_friendsListPanel, SIGNAL(findFriend(GeoCoordinate)),
+            this, SIGNAL(findFriend(GeoCoordinate)));
+
+    connect(this, SIGNAL(friendImageReady(User*)),
+            m_friendsListPanel, SLOT(friendImageReady(User*)));
+
+    connect(this, SIGNAL(locationDataParsed(QList<Location>&)),
+            m_friendsListPanel, SLOT(locationDataReady(QList<Location>&)));
+
+    connect(m_friendsListPanel, SIGNAL(locationItemClicked(GeoCoordinate&,GeoCoordinate&)),
+            this, SIGNAL(locationItemClicked(GeoCoordinate&,GeoCoordinate&)));
+}
+
+void MainWindow::buildIndicatorButton()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_indicatorButton = new IndicatorButton(this);
+
+    connect(m_indicatorButton, SIGNAL(autoCenteringTriggered(bool)),
+        this, SIGNAL(autoCenteringTriggered(bool)));
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            m_indicatorButton, SLOT(screenResized(QSize)));
+}
+
+void MainWindow::buildInformationBox(const QString &message, bool modal)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QString errorMessage = message;
+
+#ifdef Q_WS_MAEMO_5
+
+    QMaemo5InformationBox *msgBox = new QMaemo5InformationBox(this);
+
+    if(modal) {
+        msgBox->setTimeout(QMaemo5InformationBox::NoTimeout);
+        // extra line changes are needed to make error notes broader
+        errorMessage.prepend("\n");
+        errorMessage.append("\n");
+    } else {
+        msgBox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
+    }
+    QLabel *label = new QLabel(msgBox);
+    label->setAlignment(Qt::AlignCenter);
+    label->setText(errorMessage);
+    msgBox->setWidget(label);
+#else
+    QMessageBox *msgBox = new QMessageBox(this);
+    msgBox->button(QMessageBox::Ok);
+    msgBox->setText(errorMessage);
+    msgBox->setModal(modal);
+#endif
 
-    connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
-            m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
+    queueDialog(msgBox);
 }
 
 void MainWindow::buildManualLocationCrosshair()
@@ -136,8 +240,8 @@ void MainWindow::buildManualLocationCrosshair()
     m_ownLocationCrosshair->hide();
     m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
 
-    connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
-            this, SLOT(drawOwnLocationCrosshair(int, int)));
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            this, SLOT(drawOwnLocationCrosshair(QSize)));
 }
 
 void MainWindow::buildMap()
@@ -147,33 +251,50 @@ void MainWindow::buildMap()
     m_mapView = new MapView(this);
 
     buildZoomButtonPanel();
-
-    m_ownLocationCrosshair = 0;
     buildOsmLicense();
     buildManualLocationCrosshair();
+    buildFullScreenButton();
+    buildIndicatorButton();
+    buildMapScale();
 
-    connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
-            this, SIGNAL(mapViewScrolled(QPoint)));
+    connect(m_mapView, SIGNAL(viewScrolled(SceneCoordinate)),
+            this, SIGNAL(mapViewScrolled(SceneCoordinate)));
 
-    connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
-            m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
+    connect(this, SIGNAL(centerToSceneCoordinates(SceneCoordinate)),
+            m_mapView, SLOT(centerToSceneCoordinates(SceneCoordinate)));
 
     connect(m_mapView, SIGNAL(viewResized(QSize)),
             this, SIGNAL(mapViewResized(QSize)));
 
-    connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
-             this, SLOT(setViewPortSize(int, int)));
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            this, SLOT(drawFullScreenButton(QSize)));
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            this, SLOT(drawMapScale(QSize)));
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+             this, SLOT(setViewPortSize(QSize)));
 
     connect(this, SIGNAL(zoomLevelChanged(int)),
             m_mapView, SLOT(setZoomLevel(int)));
 
     connect(m_mapView, SIGNAL(viewZoomFinished()),
             this, SIGNAL(viewZoomFinished()));
+
+    connect(m_mapView, SIGNAL(zoomIn()),
+            this, SIGNAL(zoomIn()));
+}
+
+void MainWindow::buildMapScale()
+{
+    m_mapScale = new MapScale(this);
+    connect(this, SIGNAL(newMapResolution(qreal)),
+            m_mapScale, SLOT(updateMapResolution(qreal)));
 }
 
 void MainWindow::buildOsmLicense()
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     m_osmLicense = new QLabel(this);
     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
@@ -192,39 +313,71 @@ void MainWindow::buildUserInfoPanel()
     qDebug() << __PRETTY_FUNCTION__;
 
     m_userPanel = new UserInfoPanel(this);
+
     m_userPanelSidebar = new PanelSideBar(this, LEFT);
 
     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*)));
 
-    connect(m_userPanel, SIGNAL(requestReverseGeo()),
-            this, SIGNAL(requestReverseGeo()));
-
     connect(this, SIGNAL(reverseGeoReady(QString)),
             m_userPanel, SIGNAL(reverseGeoReady(QString)));
 
+    connect(this, SIGNAL(clearUpdateLocationDialogData()),
+            m_userPanel, SIGNAL(clearUpdateLocationDialogData()));
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            m_userPanel, SLOT(resizePanel(QSize)));
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            m_userPanelSidebar, SLOT(resizeSideBar(QSize)));
+
+    connect(m_userPanel, SIGNAL(findUser(GeoCoordinate)),
+            this, SIGNAL(findUser(GeoCoordinate)));
+
+    connect(m_userPanel, SIGNAL(requestReverseGeo()),
+            this, SIGNAL(requestReverseGeo()));
+
     connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
             this, SIGNAL(statusUpdate(QString,bool)));
 
     connect(m_userPanel, SIGNAL(refreshUserData()),
             this, SIGNAL(refreshUserData()));
 
-    connect(m_mapView, SIGNAL(viewResized(QSize)),
-            m_userPanel, SLOT(screenResized(QSize)));
+    connect(m_userPanel, SIGNAL(notificateUpdateFailing(QString, bool)),
+            this, SLOT(buildInformationBox(QString, bool)));
+}
+
+void MainWindow::buildWebView()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    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)),
+                this, SLOT(loadDone(bool)));
+
+        m_webView->hide();
+    }
 }
 
 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()));
@@ -240,6 +393,28 @@ 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()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    buildWebView();
+
+    m_webView->stop();
+
+    if(!m_cookieJar) {
+        m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
+    }
+    QList<QNetworkCookie> emptyList;
+    emptyList.clear();
+
+    m_cookieJar->setAllCookies(emptyList);
+    m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
 }
 
 void MainWindow::createMenus()
@@ -263,52 +438,133 @@ void MainWindow::createMenus()
     connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
             this, SIGNAL(gpsTriggered(bool)));
 
-    // automatic centering
-    m_autoCenteringAct = new QAction(tr("Auto centering"), this);
-    m_autoCenteringAct->setCheckable(true);
-    connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
-        this, SIGNAL(autoCenteringTriggered(bool)));
+    /// @todo remove when not needed!
+    m_searchLocationAct = new QAction(tr("Location search"), this);
+    connect(m_searchLocationAct, SIGNAL(triggered()),
+            this, SLOT(startLocationSearch()));
 
     // build the actual menu
     m_viewMenu = menuBar()->addMenu(tr("Main"));
     m_viewMenu->addAction(m_loginAct);
     m_viewMenu->addAction(m_toSettingsAct);
     m_viewMenu->addAction(m_gpsToggleAct);
-    m_viewMenu->addAction(m_autoCenteringAct);
+    m_viewMenu->addAction(m_searchLocationAct); /// @todo remove when not needed!
     m_viewMenu->setObjectName(tr("Menu"));
 }
 
-void MainWindow::drawOsmLicense(const QSize &size)
+void MainWindow::dialogFinished(int status)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QDialog *dialog = m_queue.takeFirst();
+    LoginDialog *loginDialog = qobject_cast<LoginDialog *>(dialog);
+    SearchDialog *searchDialog = qobject_cast<SearchDialog *>(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();
+        }
+    } else if(searchDialog) {
+        if(status != 0) {
+            emit searchForLocation(searchDialog->input());
+        }
+    }
+
+    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();
 
-    m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
-                        size.height() - m_osmLicense->fontMetrics().height());
+    if(m_fullScreenButton) {
+        if(m_loggedIn) {
+            m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
+                                     - PANEL_PEEK_AMOUNT,
+                                     size.height() - m_fullScreenButton->size().height());
+        } else {
+            m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width(),
+                                     size.height() - m_fullScreenButton->size().height());
+        }
+    }
+}
+
+void MainWindow::drawMapScale(const QSize &size)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    const int LEFT_SCALE_MARGIN = 10;
+    const int BOTTOM_SCALE_MARGIN = 2;
+    qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
 
+    m_mapScale->move(PANEL_PEEK_AMOUNT + LEFT_SCALE_MARGIN,
+                     size.height() - m_mapScale->size().height() - BOTTOM_SCALE_MARGIN);
 }
 
-void MainWindow::drawOwnLocationCrosshair(int width, int 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(const QSize &size)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
     if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
-        m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
-                            height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
+        m_ownLocationCrosshair->move(size.width()/2 - m_ownLocationCrosshair->pixmap()->width()/2,
+                            size.height()/2 - m_ownLocationCrosshair->pixmap()->height()/2);
     }
 }
 
-void MainWindow::gpsError(const QString &message)
+void MainWindow::errorDialogFinished(int status)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    showMaemoInformationBox(message);
+    qDebug() << status;
+    QDialog *dialog = m_error_queue.takeFirst();
+
+    dialog->deleteLater();
+    m_errorShown = false;
+
+    if(!m_error_queue.isEmpty())
+        showErrorInformationBox();
+    else if(!m_queue.isEmpty())
+        showInformationBox();
 }
 
 void MainWindow::gpsTimeout()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    showMaemoInformationBox(tr("GPS timeout"));
+    buildInformationBox(tr("GPS timeout"));
 }
 
 void MainWindow::grabZoomKeys(bool grab)
@@ -356,6 +612,28 @@ void MainWindow::keyPressEvent(QKeyEvent* event)
     QWidget::keyPressEvent(event);
 }
 
+void MainWindow::loadCookies()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+
+    QStringList list = settings.value(COOKIES, EMPTY).toStringList();
+
+    if(!list.isEmpty()) {
+        QList<QNetworkCookie> cookieList;
+        for(int i=0;i<list.count();i++) {
+            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);
+    }
+}
+
 void MainWindow::loadDone(bool done)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -408,74 +686,118 @@ void MainWindow::loggedIn(bool logged)
 
     if(logged) {
         m_loginAct->setText(tr("Logout"));
-    }
-    else {
+    } else {
+        clearCookieJar();
+        m_email.clear();
+        m_password.clear();
+
         m_loginAct->setText(tr("Login"));
     }
-    showPanels(m_loggedIn);
+    updateItemVisibility();
 }
 
-void MainWindow::loginDialogDone(const QString &email, const QString &password)
+void MainWindow::loginFailed()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_email = email;
-    m_password = password;
+    clearCookieJar();
+    startLoginProcess();
 }
 
-void MainWindow::loginFailed()
+bool MainWindow::loginState()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_loggedIn;
+}
+
+void MainWindow::loginUsingCookies()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_email.clear();
-    m_password.clear();
+    toggleProgressIndicator(true);
 
-    toggleProgressIndicator(false);
+    buildWebView();
+    loadCookies();
 
-#ifdef Q_WS_MAEMO_5
-    QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
-                                       QMaemo5InformationBox::NoTimeout);
+    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);
 
-#endif // Q_WS_MAEMO_5
+    m_webView->load(QUrl(urlParts.join(EMPTY)));
 
-    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 &)));
+void MainWindow::openSettingsDialog()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-        emit cancelLoginProcess();
-    }
-    else {
-        // re-load login page for webview
-        toggleProgressIndicator(true);
-        m_webView->load(m_loginUrl);
+    SettingsDialog *settingsDialog = new SettingsDialog(this);
+    settingsDialog->enableSituareSettings((m_loggedIn && m_gpsToggleAct->isChecked()));
+    connect(settingsDialog, SIGNAL(accepted()), this, SLOT(settingsDialogAccepted()));
+
+    settingsDialog->show();
+}
+
+void MainWindow::readAutomaticLocationUpdateSettings()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
+    QTime automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, QTime())
+                                      .toTime();
+
+    if (automaticUpdateEnabled && automaticUpdateInterval.isValid()) {
+        QTime time;
+        emit enableAutomaticLocationUpdate(true, time.msecsTo(automaticUpdateInterval));
+    } else {
+        emit enableAutomaticLocationUpdate(false);
     }
 }
 
-void MainWindow::openSettingsDialog()
+void MainWindow::queueDialog(QDialog *dialog)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    SettingsDialog *dialog = new SettingsDialog(this);
-    if(!m_loggedIn) {
-        dialog->disableSituareSettings();
+    // 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);
     }
-    dialog->show();
+
+    // 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::setAutoCenteringButtonEnabled(bool enabled)
+void MainWindow::saveCookies()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_autoCenteringAct->setChecked(enabled);
+    if(!m_cookieJar)
+        m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
+
+    QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
+    QStringList list;
+
+    for(int i=0;i<cookieList.count();i++) {
+        QNetworkCookie cookie = cookieList.at(i);
+        QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
+        list.append(QString(byteArray));
+    }
+    list.removeDuplicates();
+
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    settings.setValue(COOKIES, list);
 }
 
 void MainWindow::setGPSButtonEnabled(bool enabled)
@@ -483,8 +805,16 @@ void MainWindow::setGPSButtonEnabled(bool enabled)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_gpsToggleAct->setChecked(enabled);
+
     setOwnLocationCrosshairVisibility(!enabled);
-    m_autoCenteringAct->setVisible(enabled);
+}
+
+void MainWindow::setIndicatorButtonEnabled(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_indicatorButton->setChecked(enabled);
+
 }
 
 void MainWindow::setMapViewScene(QGraphicsScene *scene)
@@ -498,101 +828,127 @@ void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (visibility) {
+    if (visibility && m_loggedIn) {
         m_ownLocationCrosshair->show();
         m_drawOwnLocationCrosshair = true;
-        drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
-    }
-    else {
+        drawOwnLocationCrosshair(m_viewPortSize);
+    } else {
         m_ownLocationCrosshair->hide();
         m_drawOwnLocationCrosshair = false;
     }
 }
 
+void MainWindow::settingsDialogAccepted()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    readAutomaticLocationUpdateSettings();
+}
+
 void MainWindow::setUsername(const QString &username)
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     m_email = username;
 }
 
-void MainWindow::setViewPortSize(int width, int height)
+void MainWindow::setViewPortSize(const QSize &size)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_viewPortWidth = width;
-    m_viewPortHeight = height;
+    m_viewPortSize = size;
 }
 
-void MainWindow::showMaemoInformationBox(const QString &message)
+void MainWindow::showEnableAutomaticUpdateLocationDialog(const QString &text)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-#ifdef Q_WS_MAEMO_5
-    QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
-#else
-    Q_UNUSED(message);
-#endif
+    m_automaticUpdateLocationDialog = new QMessageBox(QMessageBox::Question,
+                                                      tr("Automatic location update"), text,
+                                                      QMessageBox::Yes | QMessageBox::No |
+                                                      QMessageBox::Cancel, this);
+    connect(m_automaticUpdateLocationDialog, SIGNAL(finished(int)),
+            this, SLOT(automaticUpdateDialogFinished(int)));
+
+    m_automaticUpdateLocationDialog->show();
+}
+
+void MainWindow::toggleFullScreen()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(windowState() == Qt::WindowNoState)
+        showFullScreen();
+    else
+        showNormal();
 }
 
-void MainWindow::showPanels(bool show)
+void MainWindow::showErrorInformationBox()
 {
     qDebug() << __PRETTY_FUNCTION__;
-    if(show) {
-        m_friendsListPanel->show();
-        m_friendsListPanelSidebar->show();
-        m_userPanel->show();
-        m_userPanelSidebar->show();
+
+    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();
     }
-    else {
-        m_friendsListPanel->closePanel();
-        m_friendsListPanel->hide();
-        m_friendsListPanelSidebar->hide();
-        m_userPanel->closePanel();
-        m_userPanel->hide();
-        m_userPanelSidebar->hide();
+}
+
+void MainWindow::showInformationBox()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(m_queue.count()) {
+        QDialog *dialog = m_queue.first();
+        connect(dialog, SIGNAL(finished(int)),
+                this, SLOT(dialogFinished(int)));
+        dialog->show();
     }
 }
 
-void MainWindow::startLoginProcess(const QUrl &url)
+void MainWindow::showPanels()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_loginUrl = url;
-    m_webView = new QWebView;
-    m_loginDialog = new LoginDialog(this);
+    drawFullScreenButton(m_viewPortSize);
+
+    if(m_loggedIn) {
+        if(!m_friendsListPanel->isVisible()) {
+            m_friendsListPanel->show();
+            m_friendsListPanelSidebar->show();
+        }
+
+        if(!m_userPanel->isVisible()) {
+            m_userPanel->show();
+            m_userPanelSidebar->show();
+        }
+    }
+}
+
+void MainWindow::startLocationSearch()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
-            this, SIGNAL(updateCredentials(QUrl)));
-    connect(m_webView, SIGNAL(loadFinished(bool)),
-            this, SLOT(loadDone(bool)));
+    SearchDialog *searchDialog = new SearchDialog();
+    queueDialog(searchDialog);
+}
 
-    connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
-            this, SLOT(loginDialogDone(QString,QString)));
+void MainWindow::startLoginProcess()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_webView->hide();
+    LoginDialog *loginDialog = new LoginDialog();
 
     emit fetchUsernameFromSettings();
 
-    if(!m_email.isEmpty()) {
-        m_loginDialog->setEmailField(m_email);
-    }
+    loginDialog->clearTextFields();
 
-    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 &)));
+    if(!m_email.isEmpty())
+        loginDialog->setEmailField(m_email);
 
-        emit cancelLoginProcess();
-    }
-    else {
-        m_webView->load(m_loginUrl);
-        toggleProgressIndicator(true);
-        m_refresh = true;
-    }
+    queueDialog(loginDialog);
 }
 
 void MainWindow::toggleProgressIndicator(bool value)
@@ -600,14 +956,55 @@ void MainWindow::toggleProgressIndicator(bool value)
     qDebug() << __PRETTY_FUNCTION__;
 
 #ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
+    if(value) {
+        m_progressIndicatorCount++;
+        setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+    } else {
+        if(m_progressIndicatorCount > 0)
+            m_progressIndicatorCount--;
+
+        if(m_progressIndicatorCount == 0)
+            setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+    }
 #else
     Q_UNUSED(value);
 #endif // Q_WS_MAEMO_5
 }
 
+void MainWindow::updateItemVisibility()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(m_loggedIn) {
+
+        if(!m_gpsToggleAct->isChecked())
+            setOwnLocationCrosshairVisibility(true);
+    } else {
+        m_friendsListPanel->closePanel();
+        m_friendsListPanel->hide();
+        m_friendsListPanelSidebar->hide();
+        m_userPanel->closePanel();
+        m_userPanel->hide();
+        m_userPanelSidebar->hide();
+        setOwnLocationCrosshairVisibility(false);
+    }
+}
+
 const QString MainWindow::username()
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     return m_email;
 }
+
+void MainWindow::webViewRequestFinished(QNetworkReply *reply)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    // omit QNetworkReply::OperationCanceledError due to it's nature to be called when ever
+    // qwebview starts to load a new page while the current page loading is not finished
+    if(reply->error() != QNetworkReply::OperationCanceledError &&
+       reply->error() != QNetworkReply::NoError) {
+        emit error(ErrorContext::NETWORK, reply->error());
+    }
+}