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