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