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