Merge branch 'distance_scale'
[situare] / src / ui / mainwindow.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6       Kaj Wallin - kaj.wallin@ixonos.com
7       Jussi Laitinen jussi.laitinen@ixonos.com
8       Sami Rämö - sami.ramo@ixonos.com
9       Ville Tiensuu - ville.tiensuu@ixonos.com
10
11    Situare is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License
13    version 2 as published by the Free Software Foundation.
14
15    Situare is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with Situare; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
23    USA.
24 */
25
26 #include <QtGui>
27 #include <QtWebKit>
28 #include <QtAlgorithms>
29
30 #include "common.h"
31 #include "facebookservice/facebookauthentication.h"
32 #include "friendlistpanel.h"
33 #include "logindialog.h"
34 #include "map/mapview.h"
35 #include "settingsdialog.h"
36 #include "userinfopanel.h"
37 #include "zoombuttonpanel.h"
38 #include "mapscale.h"
39
40 #include "mainwindow.h"
41
42 // These MUST BE HERE, compiling for Maemo fails if moved
43 #ifdef Q_WS_MAEMO_5
44 #include <QtMaemo5/QMaemo5InformationBox>
45 #include <QtGui/QX11Info>
46 #include <X11/Xatom.h>
47 #include <X11/Xlib.h>
48 #endif // Q_WS_MAEMO_5
49
50 // values for setting screen size in desktop matching N900 screen size
51 const int N900_APP_WIDTH = 800;
52 const int N900_APP_HEIGHT = 449;
53
54 MainWindow::MainWindow(QWidget *parent)
55     : QMainWindow(parent),
56     m_drawOwnLocationCrosshair(false),
57     m_errorShown(false),
58     m_loggedIn(false),
59     m_refresh(false),
60     m_ownLocationCrosshair(0),
61     m_email(),    
62     m_password(),
63     m_fullScreenButton(0),
64     m_webView(0),
65     m_mapScale(0),
66     m_cookieJar(0)
67 {
68     qDebug() << __PRETTY_FUNCTION__;
69
70     buildMap();
71
72     // build main layout
73     QHBoxLayout *layout = new QHBoxLayout;
74     layout->addWidget(m_mapView);
75     layout->setMargin(0);
76     setCentralWidget(new QWidget());
77     centralWidget()->setLayout(layout);
78
79     buildFriendListPanel();
80     buildUserInfoPanel();
81
82     m_settingsDialog = new SettingsDialog(this);
83     m_settingsDialog->hide();
84     connect(m_settingsDialog, SIGNAL(enableAutomaticLocationUpdate(bool,int)),
85             this, SIGNAL(enableAutomaticLocationUpdate(bool,int)));
86
87     createMenus();
88     setWindowTitle(tr("Situare"));
89
90     // set stacking order of widgets
91     m_zoomButtonPanel->stackUnder(m_userPanel);
92     if(m_fullScreenButton) {
93         m_fullScreenButton->stackUnder(m_zoomButtonPanel);
94         m_osmLicense->stackUnder(m_fullScreenButton);
95     } else {
96         m_osmLicense->stackUnder(m_zoomButtonPanel);
97     }
98     m_ownLocationCrosshair->stackUnder(m_osmLicense);
99     m_mapScale->stackUnder(m_ownLocationCrosshair);
100     m_mapView->stackUnder(m_mapScale);
101
102     this->toggleProgressIndicator(true);
103
104     grabZoomKeys(true);
105
106     // set screen size in desktop matching N900 screen size
107     resize(N900_APP_WIDTH, N900_APP_HEIGHT);
108 }
109
110 MainWindow::~MainWindow()
111 {
112     qDebug() << __PRETTY_FUNCTION__;
113
114     grabZoomKeys(false);
115
116     if(m_webView)
117         delete m_webView;
118
119     qDeleteAll(m_queue.begin(), m_queue.end());
120     m_queue.clear();
121
122     qDeleteAll(m_error_queue.begin(), m_error_queue.end());
123     m_error_queue.clear();
124 }
125
126 void MainWindow::buildFullScreenButton()
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129 #ifdef Q_WS_MAEMO_5
130     m_fullScreenButton = new QToolButton(this);
131     m_fullScreenButton->setIcon(QIcon::fromTheme(QLatin1String("general_fullsize")));
132     m_fullScreenButton->setFixedSize(m_fullScreenButton->sizeHint());
133     connect(m_fullScreenButton, SIGNAL(clicked()),
134             this, SLOT(toggleFullScreen()));
135 #endif // Q_WS_MAEMO_5
136
137 }
138
139 void MainWindow::buildFriendListPanel()
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142
143     m_friendsListPanel = new FriendListPanel(this);
144     m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
145
146     m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
147
148     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
149             m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
150
151     connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
152             this, SIGNAL(findFriend(QPointF)));
153
154     connect(m_mapView, SIGNAL(viewResized(QSize)),
155             m_friendsListPanel, SLOT(screenResized(QSize)));
156
157     connect(this, SIGNAL(locationItemClicked(QList<QString>)),
158             m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
159
160     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
161             m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
162 }
163
164 void MainWindow::buildInformationBox(const QString &message, bool modal)
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168 #ifdef Q_WS_MAEMO_5
169     QMaemo5InformationBox *msgBox = new QMaemo5InformationBox(this);
170     QLabel *label = new QLabel(msgBox);
171     label->setAlignment(Qt::AlignCenter);
172     label->setText(message);
173     msgBox->setWidget(label);
174
175     if(modal) {
176         msgBox->setTimeout(QMaemo5InformationBox::NoTimeout);
177     } else {
178         msgBox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
179     }
180 #else
181     QMessageBox *msgBox = new QMessageBox(this);
182     msgBox->button(QMessageBox::Ok);
183     msgBox->setText(message);
184     msgBox->setModal(modal);
185 #endif
186
187     queueDialog(msgBox);
188 }
189
190 void MainWindow::buildManualLocationCrosshair()
191 {
192     qDebug() << __PRETTY_FUNCTION__;
193
194     m_ownLocationCrosshair = new QLabel(this);
195     QPixmap crosshairImage(":/res/images/sight.png");
196     m_ownLocationCrosshair->setPixmap(crosshairImage);
197     m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
198     m_ownLocationCrosshair->hide();
199     m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
200
201     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
202             this, SLOT(drawOwnLocationCrosshair(int, int)));
203 }
204
205 void MainWindow::buildMap()
206 {
207     qDebug() << __PRETTY_FUNCTION__;
208
209     m_mapView = new MapView(this);
210
211     buildZoomButtonPanel();
212     buildOsmLicense();
213     buildManualLocationCrosshair();
214     buildFullScreenButton();
215     buildMapScale();
216
217     connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
218             this, SIGNAL(mapViewScrolled(QPoint)));
219
220     connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
221             m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
222
223     connect(m_mapView, SIGNAL(viewResized(QSize)),
224             this, SIGNAL(mapViewResized(QSize)));
225
226     connect(m_mapView, SIGNAL(viewResized(QSize)),
227             this, SLOT(drawFullScreenButton(QSize)));
228
229     connect(m_mapView, SIGNAL(viewResized(QSize)),
230             this, SLOT(drawMapScale(QSize)));
231
232     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
233              this, SLOT(setViewPortSize(int, int)));
234
235     connect(this, SIGNAL(zoomLevelChanged(int)),
236             m_mapView, SLOT(setZoomLevel(int)));
237
238     connect(m_mapView, SIGNAL(viewZoomFinished()),
239             this, SIGNAL(viewZoomFinished()));
240 }
241
242 void MainWindow::buildMapScale()
243 {
244     m_mapScale = new MapScale(this);
245     connect(this, SIGNAL(newMapResolution(qreal)),
246             m_mapScale, SLOT(updateMapResolution(qreal)));
247 }
248
249 void MainWindow::buildOsmLicense()
250 {
251     qDebug() << __PRETTY_FUNCTION__;
252
253     m_osmLicense = new QLabel(this);
254     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
255     m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
256     m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
257     m_osmLicense->setFont(QFont("Nokia Sans", 9));
258     m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
259                          m_osmLicense->fontMetrics().height());
260
261     connect(m_mapView, SIGNAL(viewResized(QSize)),
262             this, SLOT(drawOsmLicense(QSize)));
263 }
264
265 void MainWindow::buildUserInfoPanel()
266 {
267     qDebug() << __PRETTY_FUNCTION__;
268
269     m_userPanel = new UserInfoPanel(this);
270     m_userPanelSidebar = new PanelSideBar(this, LEFT);
271
272     m_userPanelSidebar->stackUnder(m_friendsListPanel);
273     m_userPanel->stackUnder(m_userPanelSidebar);
274
275     connect(m_userPanel, SIGNAL(findUser(QPointF)),
276             this, SIGNAL(findUser(QPointF)));
277
278     connect(this, SIGNAL(userLocationReady(User*)),
279             m_userPanel, SLOT(userDataReceived(User*)));
280
281     connect(m_userPanel, SIGNAL(requestReverseGeo()),
282             this, SIGNAL(requestReverseGeo()));
283
284     connect(this, SIGNAL(reverseGeoReady(QString)),
285             m_userPanel, SIGNAL(reverseGeoReady(QString)));
286
287     connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
288             this, SIGNAL(statusUpdate(QString,bool)));
289
290     connect(m_userPanel, SIGNAL(refreshUserData()),
291             this, SIGNAL(refreshUserData()));
292
293     connect(m_mapView, SIGNAL(viewResized(QSize)),
294             m_userPanel, SLOT(screenResized(QSize)));
295
296     connect(this, SIGNAL(updateWasSuccessful()),
297             m_userPanel, SIGNAL(updateWasSuccessful()));
298
299     connect(this, SIGNAL(messageSendingFailed(QString)),
300             m_userPanel, SIGNAL(messageSendingFailed(QString)));
301
302     connect(m_userPanel, SIGNAL(notificateUpdateFailing(QString)),
303             this, SIGNAL(notificateUpdateFailing(QString)));
304 }
305
306 void MainWindow::buildWebView()
307 {
308     qDebug() << __PRETTY_FUNCTION__;
309
310     if(!m_webView) {
311         m_webView = new QWebView;
312
313         if(!m_cookieJar)
314             m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
315
316         m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
317
318         connect(m_webView->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
319                 this, SLOT(webViewRequestFinished(QNetworkReply*)));
320         connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
321                 this, SIGNAL(updateCredentials(QUrl)));
322         connect(m_webView, SIGNAL(loadFinished(bool)),
323                 this, SLOT(loadDone(bool)));
324
325         m_webView->hide();
326     }
327 }
328
329 void MainWindow::buildZoomButtonPanel()
330 {
331     qDebug() << __PRETTY_FUNCTION__;
332
333     m_zoomButtonPanel = new ZoomButtonPanel(this);
334
335     connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
336             this, SIGNAL(zoomIn()));
337
338     connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
339             this, SIGNAL(zoomOut()));
340
341     connect(this, SIGNAL(zoomLevelChanged(int)),
342             m_zoomButtonPanel, SLOT(resetButtons()));
343
344     connect(this, SIGNAL(maxZoomLevelReached()),
345             m_zoomButtonPanel, SLOT(disableZoomInButton()));
346
347     connect(this, SIGNAL(minZoomLevelReached()),
348             m_zoomButtonPanel, SLOT(disableZoomOutButton()));
349
350     connect(m_mapView, SIGNAL(viewResized(QSize)),
351             m_zoomButtonPanel, SLOT(screenResized(QSize)));
352 }
353
354 void MainWindow::clearCookieJar()
355 {
356     qDebug() << __PRETTY_FUNCTION__;
357
358     buildWebView();
359
360     if(!m_cookieJar) {
361         m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
362     }
363     QList<QNetworkCookie> emptyList;
364     emptyList.clear();
365
366     m_cookieJar->setAllCookies(emptyList);
367     m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
368 }
369
370 void MainWindow::createMenus()
371 {
372     qDebug() << __PRETTY_FUNCTION__;
373
374     // login/logout
375     m_loginAct = new QAction(tr("Login"), this);
376     connect(m_loginAct, SIGNAL(triggered()),
377             this, SIGNAL(loginActionPressed()));
378
379     // settings
380     m_toSettingsAct = new QAction(tr("Settings"), this);
381     connect(m_toSettingsAct, SIGNAL(triggered()),
382         this, SLOT(openSettingsDialog()));
383
384     // GPS
385     m_gpsToggleAct = new QAction(tr("GPS"), this);
386     m_gpsToggleAct->setCheckable(true);
387
388     connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
389             this, SIGNAL(gpsTriggered(bool)));
390
391     // automatic centering
392     m_autoCenteringAct = new QAction(tr("Auto centering"), this);
393     m_autoCenteringAct->setCheckable(true);
394     connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
395         this, SIGNAL(autoCenteringTriggered(bool)));
396
397     // build the actual menu
398     m_viewMenu = menuBar()->addMenu(tr("Main"));
399     m_viewMenu->addAction(m_loginAct);
400     m_viewMenu->addAction(m_toSettingsAct);
401     m_viewMenu->addAction(m_gpsToggleAct);
402     m_viewMenu->addAction(m_autoCenteringAct);
403     m_viewMenu->setObjectName(tr("Menu"));
404 }
405
406 void MainWindow::dialogFinished(int status)
407 {
408     qDebug() << __PRETTY_FUNCTION__;
409
410     QDialog *dialog = m_queue.takeFirst();
411     LoginDialog *loginDialog = qobject_cast<LoginDialog *>(dialog);
412     if(loginDialog) {
413         if(status != 0) {
414             buildWebView();
415             loginDialog->userInput(m_email, m_password);
416
417             QStringList urlParts;
418             urlParts.append(FACEBOOK_LOGINBASE);
419             urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
420             urlParts.append(INTERVAL1);
421             urlParts.append(SITUARE_LOGIN_SUCCESS);
422             urlParts.append(INTERVAL2);
423             urlParts.append(SITUARE_LOGIN_FAILURE);
424             urlParts.append(FACEBOOK_LOGIN_ENDING);
425
426             emit saveUsername(m_email);
427             m_refresh = true;
428             m_webView->load(QUrl(urlParts.join(EMPTY)));
429             toggleProgressIndicator(true);
430         } else {
431             emit cancelLoginProcess();
432         }
433     }
434
435     dialog->deleteLater();
436
437     if(!m_error_queue.isEmpty() && m_errorShown == false) {
438         showErrorInformationBox();
439     } else {
440         if(!m_queue.isEmpty()) {
441             showInformationBox();
442         }
443     }
444 }
445
446 void MainWindow::drawFullScreenButton(const QSize &size)
447 {
448     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
449
450     if(m_fullScreenButton) {
451         m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
452                                  - PANEL_PEEK_AMOUNT,
453                                  size.height() - m_fullScreenButton->size().height());
454     }
455 }
456
457 void MainWindow::drawMapScale(const QSize &size)
458 {
459     const int LEFT_SCALE_MARGIN = 10;
460     const int BOTTOM_SCALE_MARGIN = 2;
461     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
462
463     m_mapScale->move(PANEL_PEEK_AMOUNT + LEFT_SCALE_MARGIN,
464                      size.height() - m_mapScale->size().height() - BOTTOM_SCALE_MARGIN);
465 }
466
467 void MainWindow::drawOsmLicense(const QSize &size)
468 {
469     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
470
471     m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
472                        - PANEL_PEEK_AMOUNT,
473                        size.height() - m_osmLicense->fontMetrics().height());
474 }
475
476 void MainWindow::drawOwnLocationCrosshair(int width, int height)
477 {
478     qDebug() << __PRETTY_FUNCTION__;
479
480     if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
481         m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
482                             height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
483     }
484 }
485
486 void MainWindow::errorDialogFinished(int status)
487 {
488     qDebug() << __PRETTY_FUNCTION__;
489
490     qDebug() << status;
491     QDialog *dialog = m_error_queue.takeFirst();
492
493     dialog->deleteLater();
494     m_errorShown = false;
495
496     if(!m_error_queue.isEmpty())
497         showErrorInformationBox();
498     else if(!m_queue.isEmpty())
499         showInformationBox();
500 }
501
502 void MainWindow::gpsTimeout()
503 {
504     qDebug() << __PRETTY_FUNCTION__;
505
506     buildInformationBox(tr("GPS timeout"));
507 }
508
509 void MainWindow::grabZoomKeys(bool grab)
510 {
511     qDebug() << __PRETTY_FUNCTION__;
512
513 #ifdef Q_WS_MAEMO_5
514     // Can't grab keys unless we have a window id
515     if (!winId())
516         return;
517
518     unsigned long val = (grab) ? 1 : 0;
519     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
520     if (!atom)
521         return;
522
523     XChangeProperty (QX11Info::display(),
524                      winId(),
525                      atom,
526                      XA_INTEGER,
527                      32,
528                      PropModeReplace,
529                      reinterpret_cast<unsigned char *>(&val),
530                      1);
531 #else
532     Q_UNUSED(grab);
533 #endif // Q_WS_MAEMO_5
534 }
535
536 void MainWindow::keyPressEvent(QKeyEvent* event)
537 {
538     qDebug() << __PRETTY_FUNCTION__;
539
540     switch (event->key()) {
541     case Qt::Key_F7:
542         event->accept();
543         emit zoomIn();
544         break;
545
546     case Qt::Key_F8:
547         event->accept();
548         emit zoomOut();
549         break;
550     }
551     QWidget::keyPressEvent(event);
552 }
553
554 void MainWindow::loadCookies()
555 {
556     qDebug() << __PRETTY_FUNCTION__;
557
558     QSettings settings(DIRECTORY_NAME, FILE_NAME);
559
560     QStringList list = settings.value(COOKIES, EMPTY).toStringList();
561
562     if(!list.isEmpty()) {
563         QList<QNetworkCookie> cookieList;
564         for(int i=0;i<list.count();i++) {
565             cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
566         }
567
568         if(!m_cookieJar)
569                m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
570
571         m_cookieJar->setAllCookies(cookieList);
572         m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
573
574     }
575 }
576
577 void MainWindow::loadDone(bool done)
578 {
579     qDebug() << __PRETTY_FUNCTION__;
580
581     // for the first time the login page is opened, we need to refresh it to get cookies working
582     if(m_refresh) {
583         m_webView->reload();
584         m_refresh = false;
585     }
586
587     if (done)
588     {
589         QWebFrame* frame = m_webView->page()->currentFrame();
590         if (frame!=NULL)
591         {
592             // set email box
593             QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
594
595             foreach (QWebElement element, emailCollection) {
596                 element.setAttribute("value", m_email.toAscii());
597             }
598             // set password box
599             QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
600             foreach (QWebElement element, passwordCollection) {
601                 element.setAttribute("value", m_password.toAscii());
602             }
603             // find connect button
604             QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
605             foreach (QWebElement element, buttonCollection)
606             {
607                 QPoint pos(element.geometry().center());
608
609                 // send a mouse click event to the web page
610                 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
611                                    Qt::NoModifier);
612                 QApplication::sendEvent(m_webView->page(), &event0);
613                 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
614                                    Qt::NoModifier);
615                 QApplication::sendEvent(m_webView->page(), &event1);
616             }
617         }
618     }
619 }
620
621 void MainWindow::loggedIn(bool logged)
622 {
623     qDebug() << __PRETTY_FUNCTION__;
624
625     m_loggedIn = logged;
626
627     if(logged) {
628         m_loginAct->setText(tr("Logout"));
629     } else {
630         clearCookieJar();
631         m_email.clear();
632         m_password.clear();
633
634         m_loginAct->setText(tr("Login"));
635     }
636     updateItemVisibility(m_loggedIn);
637 }
638
639 void MainWindow::loginFailed()
640 {
641     qDebug() << __PRETTY_FUNCTION__;
642
643     clearCookieJar();
644
645     toggleProgressIndicator(false);
646
647     startLoginProcess();
648 }
649
650 void MainWindow::loginUsingCookies()
651 {
652     qDebug() << __PRETTY_FUNCTION__;
653
654     buildWebView();
655     loadCookies();
656     
657     QStringList urlParts;
658     urlParts.append(FACEBOOK_LOGINBASE);
659     urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
660     urlParts.append(INTERVAL1);
661     urlParts.append(SITUARE_LOGIN_SUCCESS);
662     urlParts.append(INTERVAL2);
663     urlParts.append(SITUARE_LOGIN_FAILURE);
664     urlParts.append(FACEBOOK_LOGIN_ENDING);
665
666     m_webView->load(QUrl(urlParts.join(EMPTY)));
667
668 }
669
670 void MainWindow::openSettingsDialog()
671 {
672     qDebug() << __PRETTY_FUNCTION__;
673
674     m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
675     m_settingsDialog->show();
676 }
677
678 void MainWindow::queueDialog(QDialog *dialog)
679 {
680     qDebug() << __PRETTY_FUNCTION__;
681
682     // check is dialog is modal, for now all modal dialogs have hihger priority i.e. errors
683     if(dialog->isModal()) {
684         m_error_queue.append(dialog);
685     } else {
686         m_queue.append(dialog);
687     }
688
689     // show error dialog if there is only one error dialog in the queue and no error dialog is shown
690     if(m_error_queue.count() == 1 && m_errorShown == false)
691         showErrorInformationBox();
692     else if(m_queue.count() == 1 && m_errorShown == false)
693         showInformationBox();
694 }
695
696 void MainWindow::saveCookies()
697 {
698     qDebug() << __PRETTY_FUNCTION__;
699
700     if(!m_cookieJar)
701         m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
702
703     QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
704     QStringList list;
705
706     for(int i=0;i<cookieList.count();i++) {
707         QNetworkCookie cookie = cookieList.at(i);
708         QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
709         list.append(QString(byteArray));
710     }
711     list.removeDuplicates();
712
713     QSettings settings(DIRECTORY_NAME, FILE_NAME);
714     settings.setValue(COOKIES, list);
715 }
716
717 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
718 {
719     qDebug() << __PRETTY_FUNCTION__;
720
721     m_autoCenteringAct->setChecked(enabled);
722 }
723
724 void MainWindow::setGPSButtonEnabled(bool enabled)
725 {
726     qDebug() << __PRETTY_FUNCTION__;
727
728     m_gpsToggleAct->setChecked(enabled);
729
730     if(m_loggedIn)
731         setOwnLocationCrosshairVisibility(!enabled);
732
733     m_autoCenteringAct->setVisible(enabled);
734 }
735
736 void MainWindow::setMapViewScene(QGraphicsScene *scene)
737 {
738     qDebug() << __PRETTY_FUNCTION__;
739
740     m_mapView->setScene(scene);
741 }
742
743 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
744 {
745     qDebug() << __PRETTY_FUNCTION__;
746
747     if (visibility) {
748         m_ownLocationCrosshair->show();
749         m_drawOwnLocationCrosshair = true;
750         drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
751     } else {
752         m_ownLocationCrosshair->hide();
753         m_drawOwnLocationCrosshair = false;
754     }
755 }
756
757 void MainWindow::setUsername(const QString &username)
758 {
759     qDebug() << __PRETTY_FUNCTION__;
760
761     m_email = username;
762 }
763
764 void MainWindow::setViewPortSize(int width, int height)
765 {
766     qDebug() << __PRETTY_FUNCTION__;
767
768     m_viewPortWidth = width;
769     m_viewPortHeight = height;
770 }
771
772 void MainWindow::toggleFullScreen()
773 {
774     qDebug() << __PRETTY_FUNCTION__;
775
776     if(windowState() == Qt::WindowNoState)
777         showFullScreen();
778     else
779         showNormal();
780 }
781
782 void MainWindow::showErrorInformationBox()
783 {
784     qDebug() << __PRETTY_FUNCTION__;
785
786     if(m_error_queue.count()) {
787         m_errorShown = true;
788         QDialog *dialog = m_error_queue.first();
789         connect(dialog, SIGNAL(finished(int)),
790                 this, SLOT(errorDialogFinished(int)));
791         dialog->show();
792     }
793 }
794
795 void MainWindow::showInformationBox()
796 {
797     qDebug() << __PRETTY_FUNCTION__;
798
799     if(m_queue.count()) {
800         QDialog *dialog = m_queue.first();
801         connect(dialog, SIGNAL(finished(int)),
802                 this, SLOT(dialogFinished(int)));
803         dialog->show();
804     }
805 }
806
807 void MainWindow::startLoginProcess()
808 {
809     qDebug() << __PRETTY_FUNCTION__;
810
811     LoginDialog *loginDialog = new LoginDialog();
812
813     emit fetchUsernameFromSettings();
814
815     loginDialog->clearTextFields();
816
817     if(!m_email.isEmpty())
818         loginDialog->setEmailField(m_email);
819
820     queueDialog(loginDialog);
821 }
822
823 void MainWindow::toggleProgressIndicator(bool value)
824 {
825     qDebug() << __PRETTY_FUNCTION__;
826
827 #ifdef Q_WS_MAEMO_5
828     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
829 #else
830     Q_UNUSED(value);
831 #endif // Q_WS_MAEMO_5
832 }
833
834 void MainWindow::updateItemVisibility(bool show)
835 {
836     qDebug() << __PRETTY_FUNCTION__;
837     
838     if(show) {
839         m_friendsListPanel->show();
840         m_friendsListPanelSidebar->show();
841         m_userPanel->show();
842         m_userPanelSidebar->show();
843
844         if(m_drawOwnLocationCrosshair) {
845             m_ownLocationCrosshair->show();
846             setGPSButtonEnabled(false);
847             emit gpsTriggered(false);
848         }
849     } else {
850         m_friendsListPanel->closePanel();
851         m_friendsListPanel->hide();
852         m_friendsListPanelSidebar->hide();
853         m_userPanel->closePanel();
854         m_userPanel->hide();
855         m_userPanelSidebar->hide();
856         m_ownLocationCrosshair->hide();       
857     }
858 }
859
860 const QString MainWindow::username()
861 {
862     qDebug() << __PRETTY_FUNCTION__;
863     
864     return m_email;
865 }
866
867 void MainWindow::webViewRequestFinished(QNetworkReply *reply)
868 {
869     qDebug() << __PRETTY_FUNCTION__;
870
871     if(reply->error()) {
872         qDebug() << reply->error() << reply->errorString();
873         toggleProgressIndicator(false);
874     }
875 }
876