475f9b6921ff1649f4e94b6a6f7ac8cb6ef4d66b
[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
251 void MainWindow::buildWebView()
252 {
253     qDebug() << __PRETTY_FUNCTION__;
254
255     if(!m_webView) {
256         m_webView = new QWebView;
257
258         connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
259                 this, SIGNAL(updateCredentials(QUrl)));
260         connect(m_webView, SIGNAL(loadFinished(bool)),
261                 this, SLOT(loadDone(bool)));
262
263         m_webView->hide();
264     }
265 }
266
267 void MainWindow::buildZoomButtonPanel()
268 {
269     qDebug() << __PRETTY_FUNCTION__;
270
271     m_zoomButtonPanel = new ZoomButtonPanel(this);
272
273     connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
274             this, SIGNAL(zoomIn()));
275
276     connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
277             this, SIGNAL(zoomOut()));
278
279     connect(this, SIGNAL(zoomLevelChanged(int)),
280             m_zoomButtonPanel, SLOT(resetButtons()));
281
282     connect(this, SIGNAL(maxZoomLevelReached()),
283             m_zoomButtonPanel, SLOT(disableZoomInButton()));
284
285     connect(this, SIGNAL(minZoomLevelReached()),
286             m_zoomButtonPanel, SLOT(disableZoomOutButton()));
287
288     connect(m_mapView, SIGNAL(viewResized(QSize)),
289             m_zoomButtonPanel, SLOT(screenResized(QSize)));
290 }
291
292 void MainWindow::clearCookieJar()
293 {
294     qDebug() << __PRETTY_FUNCTION__;
295
296     buildWebView();
297
298     if(!m_cookieJar) {
299         m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
300     }
301     QList<QNetworkCookie> emptyList;
302     emptyList.clear();
303
304     m_cookieJar->setAllCookies(emptyList);
305     m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
306 }
307
308 void MainWindow::createMenus()
309 {
310     qDebug() << __PRETTY_FUNCTION__;
311
312     // login/logout
313     m_loginAct = new QAction(tr("Login"), this);
314     connect(m_loginAct, SIGNAL(triggered()),
315             this, SIGNAL(loginActionPressed()));
316
317     // settings
318     m_toSettingsAct = new QAction(tr("Settings"), this);
319     connect(m_toSettingsAct, SIGNAL(triggered()),
320         this, SLOT(openSettingsDialog()));
321
322     // GPS
323     m_gpsToggleAct = new QAction(tr("GPS"), this);
324     m_gpsToggleAct->setCheckable(true);
325
326     connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
327             this, SIGNAL(gpsTriggered(bool)));
328
329     // automatic centering
330     m_autoCenteringAct = new QAction(tr("Auto centering"), this);
331     m_autoCenteringAct->setCheckable(true);
332     connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
333         this, SIGNAL(autoCenteringTriggered(bool)));
334
335     // build the actual menu
336     m_viewMenu = menuBar()->addMenu(tr("Main"));
337     m_viewMenu->addAction(m_loginAct);
338     m_viewMenu->addAction(m_toSettingsAct);
339     m_viewMenu->addAction(m_gpsToggleAct);
340     m_viewMenu->addAction(m_autoCenteringAct);
341     m_viewMenu->setObjectName(tr("Menu"));
342 }
343
344 void MainWindow::drawFullScreenButton(const QSize &size)
345 {
346     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
347
348     if(m_fullScreenButton) {
349         m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
350                                  - PANEL_PEEK_AMOUNT,
351                                  size.height() - m_fullScreenButton->size().height());
352     }
353 }
354
355 void MainWindow::drawOsmLicense(const QSize &size)
356 {
357     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
358
359     m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
360                        - PANEL_PEEK_AMOUNT,
361                        size.height() - m_osmLicense->fontMetrics().height());
362 }
363
364 void MainWindow::drawOwnLocationCrosshair(int width, int height)
365 {
366     qDebug() << __PRETTY_FUNCTION__;
367
368     if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
369         m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
370                             height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
371     }
372 }
373
374 void MainWindow::gpsTimeout()
375 {
376     qDebug() << __PRETTY_FUNCTION__;
377
378     showMaemoInformationBox(tr("GPS timeout"));
379 }
380
381 void MainWindow::grabZoomKeys(bool grab)
382 {
383     qDebug() << __PRETTY_FUNCTION__;
384
385 #ifdef Q_WS_MAEMO_5
386     // Can't grab keys unless we have a window id
387     if (!winId())
388         return;
389
390     unsigned long val = (grab) ? 1 : 0;
391     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
392     if (!atom)
393         return;
394
395     XChangeProperty (QX11Info::display(),
396                      winId(),
397                      atom,
398                      XA_INTEGER,
399                      32,
400                      PropModeReplace,
401                      reinterpret_cast<unsigned char *>(&val),
402                      1);
403 #else
404     Q_UNUSED(grab);
405 #endif // Q_WS_MAEMO_5
406 }
407
408 void MainWindow::keyPressEvent(QKeyEvent* event)
409 {
410     qDebug() << __PRETTY_FUNCTION__;
411
412     switch (event->key()) {
413     case Qt::Key_F7:
414         event->accept();
415         emit zoomIn();
416         break;
417
418     case Qt::Key_F8:
419         event->accept();
420         emit zoomOut();
421         break;
422     }
423     QWidget::keyPressEvent(event);
424 }
425
426 void MainWindow::loadCookies()
427 {
428     qDebug() << __PRETTY_FUNCTION__;
429
430     QSettings settings(DIRECTORY_NAME, FILE_NAME);
431
432     QStringList list = settings.value(COOKIES, EMPTY).toStringList();
433
434     if(!list.isEmpty()) {
435         QList<QNetworkCookie> cookieList;
436         for(int i=0;i<list.count();i++) {
437             cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
438         }
439
440         if(!m_cookieJar)
441                m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
442
443         m_cookieJar->setAllCookies(cookieList);
444         m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
445
446     }
447 }
448
449 void MainWindow::loadDone(bool done)
450 {
451     qDebug() << __PRETTY_FUNCTION__;
452
453     // for the first time the login page is opened, we need to refresh it to get cookies working
454     if(m_refresh) {
455         m_webView->reload();
456         m_refresh = false;
457     }
458
459     if (done)
460     {
461         QWebFrame* frame = m_webView->page()->currentFrame();
462         if (frame!=NULL)
463         {
464             // set email box
465             QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
466
467             foreach (QWebElement element, emailCollection) {
468                 element.setAttribute("value", m_email.toAscii());
469             }
470             // set password box
471             QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
472             foreach (QWebElement element, passwordCollection) {
473                 element.setAttribute("value", m_password.toAscii());
474             }
475             // find connect button
476             QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
477             foreach (QWebElement element, buttonCollection)
478             {
479                 QPoint pos(element.geometry().center());
480
481                 // send a mouse click event to the web page
482                 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
483                                    Qt::NoModifier);
484                 QApplication::sendEvent(m_webView->page(), &event0);
485                 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
486                                    Qt::NoModifier);
487                 QApplication::sendEvent(m_webView->page(), &event1);
488             }
489         }
490     }
491 }
492
493 void MainWindow::loggedIn(bool logged)
494 {
495     qDebug() << __PRETTY_FUNCTION__;
496
497     m_loggedIn = logged;
498
499     if(logged) {
500         m_loginAct->setText(tr("Logout"));
501     }
502     else {
503         clearCookieJar();
504         m_email.clear();
505         m_password.clear();
506
507         m_loginAct->setText(tr("Login"));
508     }
509     updateItemVisibility(m_loggedIn);
510 }
511
512 void MainWindow::loginFailed()
513 {
514     qDebug() << __PRETTY_FUNCTION__;
515
516     clearCookieJar();
517
518     toggleProgressIndicator(false);
519
520     QStringList urlParts;
521     urlParts.append(FACEBOOK_LOGINBASE);
522     urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
523     urlParts.append(INTERVAL1);
524     urlParts.append(SITUARE_LOGIN_SUCCESS);
525     urlParts.append(INTERVAL2);
526     urlParts.append(SITUARE_LOGIN_FAILURE);
527     urlParts.append(FACEBOOK_LOGIN_ENDING);
528
529     startLoginProcess(urlParts.join(EMPTY));
530 }
531
532 void MainWindow::loginUsingCookies()
533 {
534     qDebug() << __PRETTY_FUNCTION__;
535
536     buildWebView();
537     loadCookies();
538     
539     QStringList urlParts;
540     urlParts.append(FACEBOOK_LOGINBASE);
541     urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
542     urlParts.append(INTERVAL1);
543     urlParts.append(SITUARE_LOGIN_SUCCESS);
544     urlParts.append(INTERVAL2);
545     urlParts.append(SITUARE_LOGIN_FAILURE);
546     urlParts.append(FACEBOOK_LOGIN_ENDING);
547
548     m_webView->load(QUrl(urlParts.join(EMPTY)));
549
550 }
551
552 void MainWindow::openSettingsDialog()
553 {
554     qDebug() << __PRETTY_FUNCTION__;
555
556     m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
557     m_settingsDialog->show();
558 }
559
560 void MainWindow::saveCookies()
561 {
562     qDebug() << __PRETTY_FUNCTION__;
563
564     if(!m_cookieJar)
565         m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
566
567     QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
568     QStringList list;
569
570     for(int i=0;i<cookieList.count();i++) {
571         QNetworkCookie cookie = cookieList.at(i);
572         QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
573         list.append(QString(byteArray));
574     }
575     list.removeDuplicates();
576
577     QSettings settings(DIRECTORY_NAME, FILE_NAME);
578     settings.setValue(COOKIES, list);
579 }
580
581 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
582 {
583     qDebug() << __PRETTY_FUNCTION__;
584
585     m_autoCenteringAct->setChecked(enabled);
586 }
587
588 void MainWindow::setGPSButtonEnabled(bool enabled)
589 {
590     qDebug() << __PRETTY_FUNCTION__;
591
592     m_gpsToggleAct->setChecked(enabled);
593
594     if(m_loggedIn)
595         setOwnLocationCrosshairVisibility(!enabled);
596
597     m_autoCenteringAct->setVisible(enabled);
598 }
599
600 void MainWindow::setMapViewScene(QGraphicsScene *scene)
601 {
602     qDebug() << __PRETTY_FUNCTION__;
603
604     m_mapView->setScene(scene);
605 }
606
607 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
608 {
609     qDebug() << __PRETTY_FUNCTION__;
610
611     if (visibility) {
612         m_ownLocationCrosshair->show();
613         m_drawOwnLocationCrosshair = true;
614         drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
615     }
616     else {
617         m_ownLocationCrosshair->hide();
618         m_drawOwnLocationCrosshair = false;
619     }
620 }
621
622 void MainWindow::setUsername(const QString &username)
623 {
624     qDebug() << __PRETTY_FUNCTION__;
625
626     m_email = username;
627 }
628
629 void MainWindow::setViewPortSize(int width, int height)
630 {
631     qDebug() << __PRETTY_FUNCTION__;
632
633     m_viewPortWidth = width;
634     m_viewPortHeight = height;
635 }
636
637 void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
638 {
639     qDebug() << __PRETTY_FUNCTION__;
640
641 #ifdef Q_WS_MAEMO_5
642     if(modal) {
643         QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
644     }
645     else {
646         QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
647     }
648 #else
649     Q_UNUSED(modal);
650     QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
651 #endif
652 }
653
654 void MainWindow::toggleFullScreen()
655 {
656     qDebug() << __PRETTY_FUNCTION__;
657
658     if(windowState() == Qt::WindowNoState)
659         showFullScreen();
660     else
661         showNormal();
662 }
663
664 void MainWindow::startLoginProcess(const QUrl &url)
665 {
666     qDebug() << __PRETTY_FUNCTION__;
667
668     buildWebView();
669
670     LoginDialog loginDialog;
671
672     emit fetchUsernameFromSettings();
673
674     if(!m_cookieJar)
675         m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
676
677     m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
678
679     loginDialog.clearTextFields();
680
681     if(!m_email.isEmpty())
682         loginDialog.setEmailField(m_email);
683
684     if(loginDialog.exec() != QDialog::Accepted) {
685         // if login dialog was canceled we need to stop processing webview
686         m_webView->stop();
687
688         emit cancelLoginProcess();
689     }
690     else {
691         loginDialog.userInput(m_email, m_password);
692         emit saveUsername(m_email);
693         m_webView->load(url);
694         toggleProgressIndicator(true);
695         m_refresh = true;
696     }
697 }
698
699 void MainWindow::toggleProgressIndicator(bool value)
700 {
701     qDebug() << __PRETTY_FUNCTION__;
702
703 #ifdef Q_WS_MAEMO_5
704     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
705 #else
706     Q_UNUSED(value);
707 #endif // Q_WS_MAEMO_5
708 }
709
710 void MainWindow::updateItemVisibility(bool show)
711 {
712     qDebug() << __PRETTY_FUNCTION__;
713     
714     if(show) {
715         m_friendsListPanel->show();
716         m_friendsListPanelSidebar->show();
717         m_userPanel->show();
718         m_userPanelSidebar->show();
719
720         if(m_drawOwnLocationCrosshair) {
721             m_ownLocationCrosshair->show();
722             setGPSButtonEnabled(false);
723             emit gpsTriggered(false);
724         }
725     }
726     else {
727         m_friendsListPanel->closePanel();
728         m_friendsListPanel->hide();
729         m_friendsListPanelSidebar->hide();
730         m_userPanel->closePanel();
731         m_userPanel->hide();
732         m_userPanelSidebar->hide();
733         m_ownLocationCrosshair->hide();       
734     }
735 }
736
737 const QString MainWindow::username()
738 {
739     qDebug() << __PRETTY_FUNCTION__;
740     
741     return m_email;
742 }