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