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