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