Removed FacebookLoginBrowser class, replaced with QWebView.
[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       Katri Kaikkonen - katri.kaikkonen@ixonos.com
11
12    Situare is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as published by the Free Software Foundation.
15
16    Situare is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with Situare; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
24    USA.
25 */
26
27 #include <QtAlgorithms>
28 #include <QtWebKit>
29
30 #include <QAction>
31 #include <QApplication>
32 #include <QMenuBar>
33 #include <QMessageBox>
34
35 #include "common.h"
36 #include "error.h"
37 #include "facebookloginbrowser.h"
38 #include "friendlistpanel.h"
39 #include "fullscreenbutton.h"
40 #include "indicatorbuttonpanel.h"
41 #include "locationsearchpanel.h"
42 #include "logindialog.h"
43 #include "map/mapcommon.h"
44 #include "map/mapview.h"
45 #include "mapscale.h"
46 #include "panelcommon.h"
47 #include "routingpanel.h"
48 #include "searchdialog.h"
49 #include "settingsdialog.h"
50 #include "situareservice/situarecommon.h"
51 #include "tabbedpanel.h"
52 #include "userinfopanel.h"
53 #include "zoombuttonpanel.h"
54
55 #include "mainwindow.h"
56
57 // These MUST BE HERE, compiling for Maemo fails if moved
58 #ifdef Q_WS_MAEMO_5
59 #include <QtMaemo5/QMaemo5InformationBox>
60 #include <QtGui/QX11Info>
61 #include <X11/Xatom.h>
62 #include <X11/Xlib.h>
63 #endif // Q_WS_MAEMO_5
64
65 #if defined(Q_WS_MAEMO_5) & defined(ARMEL)
66 #include "ossoabookdialog.h"
67 #endif
68
69 MainWindow::MainWindow(QWidget *parent)
70     : QMainWindow(parent),
71       m_errorShown(false),
72       m_loggedIn(false),
73       m_refresh(false),
74       m_mapCenterHorizontalShifting(0),
75       m_progressIndicatorCount(0),
76       m_loginDialog(0),
77       m_crosshair(0),
78       m_email(), ///< @todo WTF?!?!?!?
79       m_password(),
80       m_fullScreenButton(0),
81       m_indicatorButtonPanel(0),
82       m_mapScale(0)
83 {
84     qDebug() << __PRETTY_FUNCTION__;
85
86     buildMap();
87
88     // map view is the only widget which size & location is handled automatically by the system
89     // default functionality
90     setCentralWidget(m_mapView);
91
92     buildPanels();
93
94     createMenus();
95     setWindowTitle(tr("Situare"));
96
97     // set stacking order of widgets (from top to bottom)
98     // m_tabbedPanel is the topmost one
99     if (m_fullScreenButton) {
100         m_fullScreenButton->stackUnder(m_tabbedPanel);
101         m_crosshair->stackUnder(m_fullScreenButton);
102     } else {
103         m_crosshair->stackUnder(m_tabbedPanel);
104     }
105     m_zoomButtonPanel->stackUnder(m_crosshair);
106     m_indicatorButtonPanel->stackUnder(m_zoomButtonPanel);
107     m_osmLicense->stackUnder(m_indicatorButtonPanel);
108     m_mapScale->stackUnder(m_osmLicense);
109     m_mapView->stackUnder(m_mapScale);
110
111     grabZoomKeys(true);
112
113     // Set default screen size
114     resize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT);
115 #ifdef Q_WS_MAEMO_5
116     setAttribute(Qt::WA_Maemo5StackedWindow);
117 #endif
118 }
119
120 MainWindow::~MainWindow()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     grabZoomKeys(false);
125
126     qDeleteAll(m_queue.begin(), m_queue.end());
127     m_queue.clear();
128
129     qDeleteAll(m_error_queue.begin(), m_error_queue.end());
130     m_error_queue.clear();
131 }
132
133 void MainWindow::automaticUpdateDialogFinished(int result)
134 {
135     qDebug() << __PRETTY_FUNCTION__;
136
137     if (result == QMessageBox::Yes) {
138         readAutomaticLocationUpdateSettings();
139     } else {
140         QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
141         settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false);
142         readAutomaticLocationUpdateSettings();
143     }
144
145     m_automaticUpdateLocationDialog->deleteLater();
146 }
147
148 void MainWindow::buildCrosshair()
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151
152     m_crosshair = new QLabel(this);
153     QPixmap crosshairImage(":/res/images/sight.png");
154     m_crosshair->setPixmap(crosshairImage);
155     m_crosshair->setFixedSize(crosshairImage.size());
156     m_crosshair->hide();
157     m_crosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
158
159     connect(m_mapView, SIGNAL(viewResized(QSize)),
160             this, SLOT(moveCrosshair()));
161
162     connect(m_mapView, SIGNAL(horizontalShiftingChanged(int)),
163             this, SLOT(mapCenterHorizontalShiftingChanged(int)));
164 }
165
166 void MainWindow::buildFriendListPanel()
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169
170     m_friendsListPanel = new FriendListPanel(this);
171
172     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
173             m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
174
175     connect(this, SIGNAL(locationItemClicked(QList<QString>)),
176             m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
177
178     connect(m_friendsListPanel, SIGNAL(findFriend(GeoCoordinate)),
179             this, SIGNAL(centerToCoordinates(GeoCoordinate)));
180
181     connect(this, SIGNAL(friendImageReady(User*)),
182             m_friendsListPanel, SLOT(friendImageReady(User*)));
183
184     connect(m_friendsListPanel, SIGNAL(routeToFriend(const GeoCoordinate&)),
185             this, SIGNAL(routeTo(const GeoCoordinate&)));
186
187     connect(m_friendsListPanel, SIGNAL(requestContactDialog(const QString &)),
188             this, SIGNAL(requestContactDialog(const QString &)));
189 }
190
191 void MainWindow::buildFullScreenButton()
192 {
193     qDebug() << __PRETTY_FUNCTION__;
194
195 #ifdef Q_WS_MAEMO_5
196     m_fullScreenButton = new FullScreenButton(this);
197
198     if (m_fullScreenButton) {
199         connect(m_fullScreenButton, SIGNAL(clicked()),
200                 this, SLOT(toggleFullScreen()));
201
202         connect(qApp, SIGNAL(showFullScreenButton()),
203                 m_fullScreenButton, SLOT(invoke()));
204     }
205 #endif // Q_WS_MAEMO_5
206 }
207
208 void MainWindow::buildIndicatorButtonPanel()
209 {
210     qDebug() << __PRETTY_FUNCTION__;
211
212     m_indicatorButtonPanel = new IndicatorButtonPanel(this);
213
214     connect(m_indicatorButtonPanel, SIGNAL(autoCenteringTriggered(bool)),
215         this, SIGNAL(autoCenteringTriggered(bool)));
216
217     connect(m_mapView, SIGNAL(viewResized(QSize)),
218             m_indicatorButtonPanel, SLOT(screenResized(QSize)));
219
220     connect(this, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)),
221             m_indicatorButtonPanel, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)));
222
223     connect(m_indicatorButtonPanel, SIGNAL(draggingModeTriggered()),
224             this, SIGNAL(draggingModeTriggered()));
225 }
226
227 void MainWindow::buildInformationBox(const QString &message, bool modal)
228 {
229     qDebug() << __PRETTY_FUNCTION__;
230
231     QString errorMessage = message;
232
233 #ifdef Q_WS_MAEMO_5
234
235     QMaemo5InformationBox *msgBox = new QMaemo5InformationBox(this);
236
237     if(modal) {
238         msgBox->setTimeout(QMaemo5InformationBox::NoTimeout);
239         // extra line changes are needed to make error notes broader
240         errorMessage.prepend("\n");
241         errorMessage.append("\n");
242     } else {
243         msgBox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
244     }
245     QLabel *label = new QLabel(msgBox);
246     label->setAlignment(Qt::AlignCenter);
247     label->setText(errorMessage);
248     msgBox->setWidget(label);
249 #else
250     QMessageBox *msgBox = new QMessageBox(this);
251     msgBox->button(QMessageBox::Ok);
252     msgBox->setText(errorMessage);
253     msgBox->setModal(modal);
254 #endif
255
256     queueDialog(msgBox);
257 }
258
259 void MainWindow::buildLocationSearchPanel()
260 {
261     qDebug() << __PRETTY_FUNCTION__;
262
263     m_locationSearchPanel = new LocationSearchPanel(this);
264
265     connect(this, SIGNAL(locationDataParsed(const QList<Location>&)),
266             m_locationSearchPanel, SLOT(populateLocationListView(const QList<Location>&)));
267
268     connect(m_locationSearchPanel, SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
269             this, SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
270
271     connect(m_locationSearchPanel, SIGNAL(routeToLocation(const GeoCoordinate&)),
272             this, SIGNAL(routeTo(const GeoCoordinate&)));
273
274     connect(m_locationSearchPanel, SIGNAL(requestSearchLocation()),
275             this, SLOT(startLocationSearch()));
276
277     connect(this, SIGNAL(searchForLocation(QString)),
278             m_locationSearchPanel, SLOT(prependSearchHistory(QString)));
279
280     connect(m_locationSearchPanel, SIGNAL(searchHistoryItemClicked(QString)),
281             this, SIGNAL(searchHistoryItemClicked(QString)));
282 }
283
284 void MainWindow::buildLoginDialog(QWebView *browser)
285 {
286     qWarning() << __PRETTY_FUNCTION__;
287
288     if (!m_loginDialog) {
289         m_loginDialog = new QDialog(this);
290         if (m_loginDialog) {
291             m_loginDialog->setWindowTitle(tr("Login"));
292             m_loginDialog->setLayout(new QVBoxLayout());
293             m_loginDialog->layout()->addWidget(browser);
294             m_loginDialog->layout()->setContentsMargins(QMargins()); // zero margins
295             connect(m_loginDialog, SIGNAL(rejected()), this, SLOT(destroyLoginDialog()));
296         }
297     }
298
299     if (m_loginDialog)
300         m_loginDialog->show();
301 }
302
303 void MainWindow::buildMap()
304 {
305     qDebug() << __PRETTY_FUNCTION__;
306
307     m_mapView = new MapView(this);
308
309     buildZoomButtonPanel();
310     buildOsmLicense();
311     buildCrosshair();
312     buildFullScreenButton();
313     buildIndicatorButtonPanel();
314     buildMapScale();
315
316     connect(m_mapView, SIGNAL(viewScrolled(SceneCoordinate)),
317             this, SIGNAL(mapViewScrolled(SceneCoordinate)));
318
319     connect(this, SIGNAL(centerToSceneCoordinates(SceneCoordinate)),
320             m_mapView, SLOT(centerToSceneCoordinates(SceneCoordinate)));
321
322     connect(m_mapView, SIGNAL(viewResized(QSize)),
323             this, SIGNAL(mapViewResized(QSize)));
324
325     connect(m_mapView, SIGNAL(viewResized(QSize)),
326             this, SLOT(drawFullScreenButton(QSize)));
327
328     connect(m_mapView, SIGNAL(viewResized(QSize)),
329             this, SLOT(drawMapScale(QSize)));
330
331     connect(m_mapView, SIGNAL(viewResized(QSize)),
332              this, SLOT(moveCrosshair()));
333
334     connect(this, SIGNAL(zoomLevelChanged(int)),
335             m_mapView, SLOT(setZoomLevel(int)));
336
337     connect(m_mapView, SIGNAL(viewZoomFinished()),
338             this, SIGNAL(viewZoomFinished()));
339
340     connect(m_mapView, SIGNAL(zoomIn()),
341             this, SIGNAL(zoomIn()));
342 }
343
344 void MainWindow::buildMapScale()
345 {
346     m_mapScale = new MapScale(this);
347     connect(this, SIGNAL(newMapResolution(qreal)),
348             m_mapScale, SLOT(updateMapResolution(qreal)));
349 }
350
351 void MainWindow::buildOsmLicense()
352 {
353     qDebug() << __PRETTY_FUNCTION__;
354
355     m_osmLicense = new QLabel(this);
356     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
357     m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
358     m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
359     m_osmLicense->setFont(QFont("Nokia Sans", 9));
360     m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
361                          m_osmLicense->fontMetrics().height());
362
363     connect(m_mapView, SIGNAL(viewResized(QSize)),
364             this, SLOT(drawOsmLicense(QSize)));
365 }
366
367 void MainWindow::buildPanels()
368 {
369     qDebug() << __PRETTY_FUNCTION__;
370
371     buildUserInfoPanel();
372     buildFriendListPanel();
373     buildLocationSearchPanel();
374     buildRoutingPanel();
375
376     m_tabbedPanel = new TabbedPanel(this);
377
378     //Save Situare related tab indexes so tabs can be enabled/disabled when logged in/out
379     m_situareTabsIndexes.append(
380             m_tabbedPanel->addTab(m_userInfoPanel, QIcon(":/res/images/user_info.png")));
381     m_situareTabsIndexes.append(
382             m_tabbedPanel->addTab(m_friendsListPanel, QIcon(":/res/images/friend_list.png")));
383
384     m_tabbedPanel->addTab(m_locationSearchPanel, QIcon(":/res/images/location_search.png"));
385     m_tabbedPanel->addTab(m_routingPanel, QIcon(":/res/images/routing.png"));
386
387     connect(m_mapView, SIGNAL(viewResized(QSize)),
388             m_tabbedPanel, SLOT(resizePanel(QSize)));
389
390     connect(m_friendsListPanel, SIGNAL(openPanelRequested(QWidget*)),
391             m_tabbedPanel, SLOT(openPanel(QWidget*)));
392
393     connect(m_routingPanel, SIGNAL(openPanelRequested(QWidget*)),
394             m_tabbedPanel, SLOT(openPanel(QWidget*)));
395
396     connect(m_tabbedPanel, SIGNAL(panelClosed()),
397             m_friendsListPanel, SLOT(anyPanelClosed()));
398
399     connect(m_tabbedPanel, SIGNAL(panelOpened()),
400             m_friendsListPanel, SLOT(anyPanelOpened()));
401
402     connect(m_tabbedPanel, SIGNAL(panelClosed()),
403             m_routingPanel, SLOT(clearListsSelections()));
404
405     connect(m_tabbedPanel, SIGNAL(panelClosed()),
406             m_mapView, SLOT(disableCenterShift()));
407
408     connect(m_tabbedPanel, SIGNAL(panelOpened()),
409             m_mapView, SLOT(enableCenterShift()));
410
411     connect(m_tabbedPanel, SIGNAL(panelClosed()),
412             m_userInfoPanel, SIGNAL(collapse()));
413
414     connect(m_tabbedPanel, SIGNAL(currentChanged(int)),
415             m_userInfoPanel, SIGNAL(collapse()));
416
417     // signals for showing and hiding list item context buttons
418     connect(m_friendsListPanel, SIGNAL(listItemSelectionChanged(bool)),
419             m_tabbedPanel, SIGNAL(listItemSelectionChanged(bool)));
420
421     connect(m_locationSearchPanel, SIGNAL(listItemSelectionChanged(bool)),
422             m_tabbedPanel, SIGNAL(listItemSelectionChanged(bool)));
423
424     connect(m_routingPanel, SIGNAL(listItemSelectionChanged(bool)),
425             m_tabbedPanel, SIGNAL(listItemSelectionChanged(bool)));
426 }
427
428 void MainWindow::buildRoutingPanel()
429 {
430     qDebug() << __PRETTY_FUNCTION__;
431
432     m_routingPanel = new RoutingPanel(this);
433
434     connect(m_routingPanel, SIGNAL(routeToCursor()),
435             this, SIGNAL(routeToCursor()));
436
437     connect(this, SIGNAL(routeParsed(Route&)),
438             m_routingPanel, SLOT(setRoute(Route&)));
439
440     connect(m_routingPanel, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
441             this, SIGNAL(centerToCoordinates(GeoCoordinate)));
442
443     connect(m_routingPanel, SIGNAL(clearRoute()),
444             this, SIGNAL(clearRoute()));
445 }
446
447 void MainWindow::buildUserInfoPanel()
448 {
449     qDebug() << __PRETTY_FUNCTION__;
450
451     m_userInfoPanel = new UserInfoPanel(this);
452
453     connect(this, SIGNAL(userLocationReady(User*)),
454             m_userInfoPanel, SLOT(userDataReceived(User*)));
455
456     connect(this, SIGNAL(reverseGeoReady(QString)),
457             m_userInfoPanel, SIGNAL(reverseGeoReady(QString)));
458
459     connect(this, SIGNAL(clearUpdateLocationDialogData()),
460             m_userInfoPanel, SIGNAL(clearUpdateLocationDialogData()));
461
462     connect(m_userInfoPanel, SIGNAL(findUser(GeoCoordinate)),
463             this, SIGNAL(centerToCoordinates(GeoCoordinate)));
464
465     connect(m_userInfoPanel, SIGNAL(requestReverseGeo()),
466             this, SIGNAL(requestReverseGeo()));
467
468     connect(m_userInfoPanel, SIGNAL(statusUpdate(QString,bool)),
469             this, SIGNAL(statusUpdate(QString,bool)));
470
471     connect(m_userInfoPanel, SIGNAL(refreshUserData()),
472             this, SIGNAL(refreshUserData()));
473
474     connect(m_userInfoPanel, SIGNAL(notificateUpdateFailing(QString, bool)),
475             this, SLOT(buildInformationBox(QString, bool)));
476 }
477
478 void MainWindow::buildZoomButtonPanel()
479 {
480     qDebug() << __PRETTY_FUNCTION__;
481
482     m_zoomButtonPanel = new ZoomButtonPanel(this);
483
484     connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
485             this, SIGNAL(zoomIn()));
486
487     connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
488             this, SIGNAL(zoomOut()));
489
490     connect(this, SIGNAL(zoomLevelChanged(int)),
491             m_zoomButtonPanel, SLOT(resetButtons()));
492
493     connect(this, SIGNAL(maxZoomLevelReached()),
494             m_zoomButtonPanel, SLOT(disableZoomInButton()));
495
496     connect(this, SIGNAL(minZoomLevelReached()),
497             m_zoomButtonPanel, SLOT(disableZoomOutButton()));
498
499     connect(m_mapView, SIGNAL(viewResized(QSize)),
500             m_zoomButtonPanel, SLOT(screenResized(QSize)));
501
502     connect(m_zoomButtonPanel, SIGNAL(draggingModeTriggered()),
503             this, SIGNAL(draggingModeTriggered()));
504 }
505
506 void MainWindow::createMenus()
507 {
508     qDebug() << __PRETTY_FUNCTION__;
509
510     // login/logout
511     m_loginAct = new QAction(tr("Login"), this);
512     connect(m_loginAct, SIGNAL(triggered()),
513             this, SIGNAL(loginActionPressed()));
514
515     // settings
516     m_toSettingsAct = new QAction(tr("Settings"), this);
517     connect(m_toSettingsAct, SIGNAL(triggered()),
518         this, SLOT(openSettingsDialog()));
519
520     // GPS
521     m_gpsToggleAct = new QAction(tr("GPS"), this);
522     m_gpsToggleAct->setCheckable(true);
523
524     connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
525             this, SIGNAL(gpsTriggered(bool)));
526
527     // build the actual menu
528     m_viewMenu = menuBar()->addMenu(tr("Main"));
529     m_viewMenu->addAction(m_loginAct);
530     m_viewMenu->addAction(m_toSettingsAct);
531     m_viewMenu->addAction(m_gpsToggleAct);
532     m_viewMenu->setObjectName(tr("Menu"));
533 }
534
535 void MainWindow::destroyLoginDialog()
536 {
537     qWarning() << __PRETTY_FUNCTION__;
538
539     if (m_loginDialog) {
540         m_loginDialog->hide();
541         m_loginDialog->deleteLater();
542         m_loginDialog = 0;
543     }
544 }
545
546 void MainWindow::dialogFinished(int status)
547 {
548     qDebug() << __PRETTY_FUNCTION__;
549
550     QDialog *dialog = m_queue.takeFirst();
551     SearchDialog *searchDialog = qobject_cast<SearchDialog *>(dialog);
552     if ((searchDialog) && (status != 0))
553         emit searchForLocation(searchDialog->input());
554
555     dialog->deleteLater();
556
557     if(!m_error_queue.isEmpty() && m_errorShown == false) {
558         showErrorInformationBox();
559     } else {
560         if(!m_queue.isEmpty()) {
561             showInformationBox();
562         }
563     }
564 }
565
566 void MainWindow::drawFullScreenButton(const QSize &size)
567 {
568     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
569
570     if (m_fullScreenButton) {
571         m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width(),
572                                  size.height() - m_fullScreenButton->size().height());
573     }
574 }
575
576 void MainWindow::drawMapScale(const QSize &size)
577 {
578     qDebug() << __PRETTY_FUNCTION__;
579
580     const int LEFT_SCALE_MARGIN = 10;
581     const int BOTTOM_SCALE_MARGIN = 2;
582
583     m_mapScale->move(LEFT_SCALE_MARGIN,
584                      size.height() - m_mapScale->size().height() - BOTTOM_SCALE_MARGIN);
585 }
586
587 void MainWindow::drawOsmLicense(const QSize &size)
588 {
589     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
590
591     m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
592                        - PANEL_BAR_WIDTH,
593                        size.height() - m_osmLicense->fontMetrics().height());
594 }
595
596 void MainWindow::errorDialogFinished(int status)
597 {
598     qDebug() << __PRETTY_FUNCTION__;
599
600     qDebug() << status;
601     QDialog *dialog = m_error_queue.takeFirst();
602
603     dialog->deleteLater();
604     m_errorShown = false;
605
606     if(!m_error_queue.isEmpty())
607         showErrorInformationBox();
608     else if(!m_queue.isEmpty())
609         showInformationBox();
610 }
611
612 void MainWindow::gpsTimeout()
613 {
614     qDebug() << __PRETTY_FUNCTION__;
615
616     buildInformationBox(tr("GPS timeout"));
617 }
618
619 void MainWindow::grabZoomKeys(bool grab)
620 {
621     qDebug() << __PRETTY_FUNCTION__;
622
623 #ifdef Q_WS_MAEMO_5
624     // Can't grab keys unless we have a window id
625     if (!winId())
626         return;
627
628     unsigned long val = (grab) ? 1 : 0;
629     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
630     if (!atom)
631         return;
632
633     XChangeProperty (QX11Info::display(),
634                      winId(),
635                      atom,
636                      XA_INTEGER,
637                      32,
638                      PropModeReplace,
639                      reinterpret_cast<unsigned char *>(&val),
640                      1);
641 #else
642     Q_UNUSED(grab);
643 #endif // Q_WS_MAEMO_5
644 }
645
646 void MainWindow::keyPressEvent(QKeyEvent* event)
647 {
648     qDebug() << __PRETTY_FUNCTION__;
649
650     switch (event->key()) {
651     case Qt::Key_F7:
652         event->accept();
653         emit zoomIn();
654         break;
655
656     case Qt::Key_F8:
657         event->accept();
658         emit zoomOut();
659         break;
660     }
661     QWidget::keyPressEvent(event);
662 }
663
664 void MainWindow::loggedIn(bool logged)
665 {
666     /// @todo OLD CODE
667     qDebug() << __PRETTY_FUNCTION__;
668
669     m_loggedIn = logged;
670
671     if(logged) {
672         m_loginAct->setText(tr("Logout"));
673     } else {
674 //        clearCookieJar();
675         m_email.clear();
676         m_password.clear();
677
678         m_loginAct->setText(tr("Login"));
679         m_userInfoPanel->showUserInfo(false);
680     }
681     updateItemVisibility();
682 }
683
684 void MainWindow::loginFailed()
685 {
686     /// @todo OLD CODE
687     qDebug() << __PRETTY_FUNCTION__;
688
689 //    clearCookieJar();
690     startLoginProcess();
691 }
692
693 bool MainWindow::loginState()
694 {
695     qDebug() << __PRETTY_FUNCTION__;
696
697     return m_loggedIn;
698 }
699
700 void MainWindow::mapCenterHorizontalShiftingChanged(int shifting)
701 {
702     m_mapCenterHorizontalShifting = shifting;
703     moveCrosshair();
704 }
705
706 void MainWindow::moveCrosshair()
707 {
708     qDebug() << __PRETTY_FUNCTION__;
709
710     if (m_crosshair) {
711         int mapHeight = m_mapView->size().height();
712         int mapWidth = m_mapView->size().width();
713         m_crosshair->move(mapWidth / 2 - m_crosshair->pixmap()->width() / 2
714                           - m_mapCenterHorizontalShifting,
715                           mapHeight / 2 - m_crosshair->pixmap()->height() / 2);
716     }
717 }
718
719 void MainWindow::openSettingsDialog()
720 {
721     qDebug() << __PRETTY_FUNCTION__;
722
723     SettingsDialog *settingsDialog = new SettingsDialog(this);
724     settingsDialog->enableSituareSettings((m_loggedIn && m_gpsToggleAct->isChecked()));
725     connect(settingsDialog, SIGNAL(accepted()), this, SLOT(settingsDialogAccepted()));
726
727     settingsDialog->show();
728 }
729
730 void MainWindow::queueDialog(QDialog *dialog)
731 {
732     qDebug() << __PRETTY_FUNCTION__;
733
734     // check is dialog is modal, for now all modal dialogs have hihger priority i.e. errors
735     if(dialog->isModal()) {
736         m_error_queue.append(dialog);
737     } else {
738         m_queue.append(dialog);
739     }
740
741     // show error dialog if there is only one error dialog in the queue and no error dialog is shown
742     if(m_error_queue.count() == 1 && m_errorShown == false)
743         showErrorInformationBox();
744     else if(m_queue.count() == 1 && m_errorShown == false)
745         showInformationBox();
746 }
747
748 void MainWindow::readAutomaticLocationUpdateSettings()
749 {
750     qDebug() << __PRETTY_FUNCTION__;
751
752     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
753     bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
754     QTime automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, QTime())
755                                       .toTime();
756
757     if (automaticUpdateEnabled && automaticUpdateInterval.isValid()) {
758         QTime time;
759         emit enableAutomaticLocationUpdate(true, time.msecsTo(automaticUpdateInterval));
760     } else {
761         emit enableAutomaticLocationUpdate(false);
762     }
763 }
764
765 void MainWindow::setCrosshairVisibility(bool visibility)
766 {
767     qDebug() << __PRETTY_FUNCTION__;
768
769     if (visibility) {
770         m_crosshair->show();
771         moveCrosshair();
772     } else {
773         m_crosshair->hide();
774     }
775 }
776
777 void MainWindow::setGPSButtonEnabled(bool enabled)
778 {
779     qDebug() << __PRETTY_FUNCTION__;
780
781     m_gpsToggleAct->setChecked(enabled);
782 }
783
784 void MainWindow::setIndicatorButtonEnabled(bool enabled)
785 {
786     qDebug() << __PRETTY_FUNCTION__;
787
788     m_indicatorButtonPanel->setIndicatorButtonEnabled(enabled);
789 }
790
791 void MainWindow::setMapViewScene(QGraphicsScene *scene)
792 {
793     qDebug() << __PRETTY_FUNCTION__;
794
795     m_mapView->setScene(scene);
796 }
797
798 void MainWindow::settingsDialogAccepted()
799 {
800     qDebug() << __PRETTY_FUNCTION__;
801
802     readAutomaticLocationUpdateSettings();
803 }
804
805 void MainWindow::showContactDialog(const QString &guid)
806 {
807     qDebug() << __PRETTY_FUNCTION__;
808
809 #if defined(Q_WS_MAEMO_5) & defined(ARMEL)
810     OssoABookDialog::showContactDialog(guid);
811 #else
812     Q_UNUSED(guid);
813     buildInformationBox(tr("Contact dialog works only on phone!"), true);
814 #endif
815 }
816
817 void MainWindow::showEnableAutomaticUpdateLocationDialog(const QString &text)
818 {
819     qDebug() << __PRETTY_FUNCTION__;
820
821     m_automaticUpdateLocationDialog = new QMessageBox(QMessageBox::Question,
822                                                       tr("Automatic location update"), text,
823                                                       QMessageBox::Yes | QMessageBox::No |
824                                                       QMessageBox::Cancel, this);
825     connect(m_automaticUpdateLocationDialog, SIGNAL(finished(int)),
826             this, SLOT(automaticUpdateDialogFinished(int)));
827
828     m_automaticUpdateLocationDialog->show();
829 }
830
831 void MainWindow::showErrorInformationBox()
832 {
833     qDebug() << __PRETTY_FUNCTION__;
834
835     if(m_error_queue.count()) {
836         m_errorShown = true;
837         QDialog *dialog = m_error_queue.first();
838         connect(dialog, SIGNAL(finished(int)),
839                 this, SLOT(errorDialogFinished(int)));
840         dialog->show();
841     }
842 }
843
844 void MainWindow::showInformationBox()
845 {
846     qDebug() << __PRETTY_FUNCTION__;
847
848     if(m_queue.count()) {
849         QDialog *dialog = m_queue.first();
850         connect(dialog, SIGNAL(finished(int)),
851                 this, SLOT(dialogFinished(int)));
852         dialog->show();
853     }
854 }
855
856 void MainWindow::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
857 {
858     qWarning() << __PRETTY_FUNCTION__;
859
860     reply->ignoreSslErrors();
861 }
862
863 void MainWindow::startLocationSearch()
864 {
865     qDebug() << __PRETTY_FUNCTION__;
866
867     SearchDialog *searchDialog = new SearchDialog();
868     queueDialog(searchDialog);
869 }
870
871 void MainWindow::startLoginProcess()
872 {
873     qDebug() << __PRETTY_FUNCTION__;
874
875     LoginDialog *loginDialog = new LoginDialog();
876
877     emit fetchUsernameFromSettings();
878
879     loginDialog->clearTextFields();
880
881     if(!m_email.isEmpty())
882         loginDialog->setEmailField(m_email);
883
884     queueDialog(loginDialog);
885 }
886
887 void MainWindow::toggleFullScreen()
888 {
889     qDebug() << __PRETTY_FUNCTION__;
890
891     if(windowState() == Qt::WindowNoState)
892         showFullScreen();
893     else
894         showNormal();
895 }
896
897 void MainWindow::toggleProgressIndicator(bool value)
898 {
899     qDebug() << __PRETTY_FUNCTION__;
900
901 #ifdef Q_WS_MAEMO_5
902     if(value) {
903         m_progressIndicatorCount++;
904         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
905     } else {
906         if(m_progressIndicatorCount > 0)
907             m_progressIndicatorCount--;
908
909         if(m_progressIndicatorCount == 0)
910             setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
911     }
912 #else
913     Q_UNUSED(value);
914 #endif // Q_WS_MAEMO_5
915 }
916
917 void MainWindow::updateItemVisibility()
918 {
919     qDebug() << __PRETTY_FUNCTION__;
920
921     m_tabbedPanel->setTabsEnabled(m_situareTabsIndexes, m_loggedIn);
922 }
923
924 const QString MainWindow::username()
925 {
926     qDebug() << __PRETTY_FUNCTION__;
927
928     return m_email;
929 }