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