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