Try login with cookies using hidden browser, show browser if failed.
[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->disconnect();
533     m_facebookLoginBrowser->deleteLater();
534     m_facebookLoginBrowser = 0;
535 }
536
537 void MainWindow::dialogFinished(int status)
538 {
539     qDebug() << __PRETTY_FUNCTION__;
540
541     QDialog *dialog = m_queue.takeFirst();
542     SearchDialog *searchDialog = qobject_cast<SearchDialog *>(dialog);
543     if ((searchDialog) && (status != 0))
544         emit searchForLocation(searchDialog->input());
545
546     dialog->deleteLater();
547
548     if(!m_error_queue.isEmpty() && m_errorShown == false) {
549         showErrorInformationBox();
550     } else {
551         if(!m_queue.isEmpty()) {
552             showInformationBox();
553         }
554     }
555 }
556
557 void MainWindow::drawFullScreenButton(const QSize &size)
558 {
559     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
560
561     if (m_fullScreenButton) {
562         m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width(),
563                                  size.height() - m_fullScreenButton->size().height());
564     }
565 }
566
567 void MainWindow::drawMapScale(const QSize &size)
568 {
569     qDebug() << __PRETTY_FUNCTION__;
570
571     const int LEFT_SCALE_MARGIN = 10;
572     const int BOTTOM_SCALE_MARGIN = 2;
573
574     m_mapScale->move(LEFT_SCALE_MARGIN,
575                      size.height() - m_mapScale->size().height() - BOTTOM_SCALE_MARGIN);
576 }
577
578 void MainWindow::drawOsmLicense(const QSize &size)
579 {
580     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
581
582     m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
583                        - PANEL_BAR_WIDTH,
584                        size.height() - m_osmLicense->fontMetrics().height());
585 }
586
587 void MainWindow::errorDialogFinished(int status)
588 {
589     qDebug() << __PRETTY_FUNCTION__;
590
591     qDebug() << status;
592     QDialog *dialog = m_error_queue.takeFirst();
593
594     dialog->deleteLater();
595     m_errorShown = false;
596
597     if(!m_error_queue.isEmpty())
598         showErrorInformationBox();
599     else if(!m_queue.isEmpty())
600         showInformationBox();
601 }
602
603 void MainWindow::gpsTimeout()
604 {
605     qDebug() << __PRETTY_FUNCTION__;
606
607     buildInformationBox(tr("GPS timeout"));
608 }
609
610 void MainWindow::grabZoomKeys(bool grab)
611 {
612     qDebug() << __PRETTY_FUNCTION__;
613
614 #ifdef Q_WS_MAEMO_5
615     // Can't grab keys unless we have a window id
616     if (!winId())
617         return;
618
619     unsigned long val = (grab) ? 1 : 0;
620     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
621     if (!atom)
622         return;
623
624     XChangeProperty (QX11Info::display(),
625                      winId(),
626                      atom,
627                      XA_INTEGER,
628                      32,
629                      PropModeReplace,
630                      reinterpret_cast<unsigned char *>(&val),
631                      1);
632 #else
633     Q_UNUSED(grab);
634 #endif // Q_WS_MAEMO_5
635 }
636
637 void MainWindow::keyPressEvent(QKeyEvent* event)
638 {
639     qDebug() << __PRETTY_FUNCTION__;
640
641     switch (event->key()) {
642     case Qt::Key_F7:
643         event->accept();
644         emit zoomIn();
645         break;
646
647     case Qt::Key_F8:
648         event->accept();
649         emit zoomOut();
650         break;
651     }
652     QWidget::keyPressEvent(event);
653 }
654
655 void MainWindow::loggedIn(bool logged)
656 {
657     /// @todo OLD CODE
658     qDebug() << __PRETTY_FUNCTION__;
659
660     m_loggedIn = logged;
661
662     if(logged) {
663         m_loginAct->setText(tr("Logout"));
664     } else {
665 //        clearCookieJar();
666         m_email.clear();
667         m_password.clear();
668
669         m_loginAct->setText(tr("Login"));
670         m_userInfoPanel->showUserInfo(false);
671     }
672     updateItemVisibility();
673 }
674
675 void MainWindow::loginFailed()
676 {
677     /// @todo OLD CODE
678     qDebug() << __PRETTY_FUNCTION__;
679
680 //    clearCookieJar();
681     startLoginProcess();
682 }
683
684 bool MainWindow::loginState()
685 {
686     qDebug() << __PRETTY_FUNCTION__;
687
688     return m_loggedIn;
689 }
690
691 void MainWindow::mapCenterHorizontalShiftingChanged(int shifting)
692 {
693     m_mapCenterHorizontalShifting = shifting;
694     moveCrosshair();
695 }
696
697 void MainWindow::moveCrosshair()
698 {
699     qDebug() << __PRETTY_FUNCTION__;
700
701     if (m_crosshair) {
702         int mapHeight = m_mapView->size().height();
703         int mapWidth = m_mapView->size().width();
704         m_crosshair->move(mapWidth / 2 - m_crosshair->pixmap()->width() / 2
705                           - m_mapCenterHorizontalShifting,
706                           mapHeight / 2 - m_crosshair->pixmap()->height() / 2);
707     }
708 }
709
710 void MainWindow::openSettingsDialog()
711 {
712     qDebug() << __PRETTY_FUNCTION__;
713
714     SettingsDialog *settingsDialog = new SettingsDialog(this);
715     settingsDialog->enableSituareSettings((m_loggedIn && m_gpsToggleAct->isChecked()));
716     connect(settingsDialog, SIGNAL(accepted()), this, SLOT(settingsDialogAccepted()));
717
718     settingsDialog->show();
719 }
720
721 void MainWindow::queueDialog(QDialog *dialog)
722 {
723     qDebug() << __PRETTY_FUNCTION__;
724
725     // check is dialog is modal, for now all modal dialogs have hihger priority i.e. errors
726     if(dialog->isModal()) {
727         m_error_queue.append(dialog);
728     } else {
729         m_queue.append(dialog);
730     }
731
732     // show error dialog if there is only one error dialog in the queue and no error dialog is shown
733     if(m_error_queue.count() == 1 && m_errorShown == false)
734         showErrorInformationBox();
735     else if(m_queue.count() == 1 && m_errorShown == false)
736         showInformationBox();
737 }
738
739 void MainWindow::readAutomaticLocationUpdateSettings()
740 {
741     qDebug() << __PRETTY_FUNCTION__;
742
743     QSettings settings(DIRECTORY_NAME, FILE_NAME);
744     bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
745     QTime automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, QTime())
746                                       .toTime();
747
748     if (automaticUpdateEnabled && automaticUpdateInterval.isValid()) {
749         QTime time;
750         emit enableAutomaticLocationUpdate(true, time.msecsTo(automaticUpdateInterval));
751     } else {
752         emit enableAutomaticLocationUpdate(false);
753     }
754 }
755
756 ///< @todo remove old, duplicate saveCookies()
757 void MainWindow::saveCookies()
758 {
759     qDebug() << __PRETTY_FUNCTION__;
760
761 //    if(!m_cookieJar)
762 //        m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
763
764 //    QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
765 //    QStringList list;
766
767 //    for(int i=0;i<cookieList.count();i++) {
768 //        QNetworkCookie cookie = cookieList.at(i);
769 //        QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
770 //        list.append(QString(byteArray));
771 //    }
772 //    list.removeDuplicates();
773
774 //    QSettings settings(DIRECTORY_NAME, FILE_NAME);
775 //    settings.setValue(COOKIES, list);
776 }
777
778 void MainWindow::setCrosshairVisibility(bool visibility)
779 {
780     qDebug() << __PRETTY_FUNCTION__;
781
782     if (visibility) {
783         m_crosshair->show();
784         moveCrosshair();
785     } else {
786         m_crosshair->hide();
787     }
788 }
789
790 void MainWindow::setGPSButtonEnabled(bool enabled)
791 {
792     qDebug() << __PRETTY_FUNCTION__;
793
794     m_gpsToggleAct->setChecked(enabled);
795 }
796
797 void MainWindow::setIndicatorButtonEnabled(bool enabled)
798 {
799     qDebug() << __PRETTY_FUNCTION__;
800
801     m_indicatorButtonPanel->setIndicatorButtonEnabled(enabled);
802 }
803
804 void MainWindow::setMapViewScene(QGraphicsScene *scene)
805 {
806     qDebug() << __PRETTY_FUNCTION__;
807
808     m_mapView->setScene(scene);
809 }
810
811 void MainWindow::settingsDialogAccepted()
812 {
813     qDebug() << __PRETTY_FUNCTION__;
814
815     readAutomaticLocationUpdateSettings();
816 }
817
818 void MainWindow::showContactDialog(const QString &guid)
819 {
820     qDebug() << __PRETTY_FUNCTION__;
821
822 #if defined(Q_WS_MAEMO_5) & defined(ARMEL)
823     OssoABookDialog::showContactDialog(guid);
824 #else
825     Q_UNUSED(guid);
826     buildInformationBox(tr("Contact dialog works only on phone!"), true);
827 #endif
828 }
829
830 void MainWindow::showEnableAutomaticUpdateLocationDialog(const QString &text)
831 {
832     qDebug() << __PRETTY_FUNCTION__;
833
834     m_automaticUpdateLocationDialog = new QMessageBox(QMessageBox::Question,
835                                                       tr("Automatic location update"), text,
836                                                       QMessageBox::Yes | QMessageBox::No |
837                                                       QMessageBox::Cancel, this);
838     connect(m_automaticUpdateLocationDialog, SIGNAL(finished(int)),
839             this, SLOT(automaticUpdateDialogFinished(int)));
840
841     m_automaticUpdateLocationDialog->show();
842 }
843
844 void MainWindow::showErrorInformationBox()
845 {
846     qDebug() << __PRETTY_FUNCTION__;
847
848     if(m_error_queue.count()) {
849         m_errorShown = true;
850         QDialog *dialog = m_error_queue.first();
851         connect(dialog, SIGNAL(finished(int)),
852                 this, SLOT(errorDialogFinished(int)));
853         dialog->show();
854     }
855 }
856
857 void MainWindow::showInformationBox()
858 {
859     qDebug() << __PRETTY_FUNCTION__;
860
861     if(m_queue.count()) {
862         QDialog *dialog = m_queue.first();
863         connect(dialog, SIGNAL(finished(int)),
864                 this, SLOT(dialogFinished(int)));
865         dialog->show();
866     }
867 }
868
869 void MainWindow::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
870 {
871     qWarning() << __PRETTY_FUNCTION__;
872
873     reply->ignoreSslErrors();
874 }
875
876 void MainWindow::startLocationSearch()
877 {
878     qDebug() << __PRETTY_FUNCTION__;
879
880     SearchDialog *searchDialog = new SearchDialog();
881     queueDialog(searchDialog);
882 }
883
884 void MainWindow::startLoginProcess()
885 {
886     qDebug() << __PRETTY_FUNCTION__;
887
888     LoginDialog *loginDialog = new LoginDialog();
889
890     emit fetchUsernameFromSettings();
891
892     loginDialog->clearTextFields();
893
894     if(!m_email.isEmpty())
895         loginDialog->setEmailField(m_email);
896
897     queueDialog(loginDialog);
898 }
899
900 void MainWindow::toggleFullScreen()
901 {
902     qDebug() << __PRETTY_FUNCTION__;
903
904     if(windowState() == Qt::WindowNoState)
905         showFullScreen();
906     else
907         showNormal();
908 }
909
910 void MainWindow::toggleProgressIndicator(bool value)
911 {
912     qDebug() << __PRETTY_FUNCTION__;
913
914 #ifdef Q_WS_MAEMO_5
915     if(value) {
916         m_progressIndicatorCount++;
917         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
918     } else {
919         if(m_progressIndicatorCount > 0)
920             m_progressIndicatorCount--;
921
922         if(m_progressIndicatorCount == 0)
923             setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
924     }
925 #else
926     Q_UNUSED(value);
927 #endif // Q_WS_MAEMO_5
928 }
929
930 void MainWindow::updateItemVisibility()
931 {
932     qDebug() << __PRETTY_FUNCTION__;
933
934     m_tabbedPanel->setTabsEnabled(m_situareTabsIndexes, m_loggedIn);
935 }
936
937 const QString MainWindow::username()
938 {
939     qDebug() << __PRETTY_FUNCTION__;
940
941     return m_email;
942 }