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