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