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