1 #include <QNetworkReply>
2 #include <QNetworkRequest>
5 #include <QNetworkProxy>
9 #include <json-c/json.h>
12 #define __STDC_FORMAT_MACROS
17 #include "InfoPanel.h"
18 #include "ClickableLabel.h"
22 #include <navigation.h>
24 #define NAVI_CONFIG_FILEPATH "/etc/naviconfig.ini"
26 #define DEFAULT_TEXT "Select your destination with Yelp !"
27 #define URL_AUTH "https://api.yelp.com/oauth2/token"
28 #define URL_AUTOCOMPLETE "https://api.yelp.com/v3/autocomplete"
29 #define URL_SEARCH "https://api.yelp.com/v3/businesses/search"
31 #define LEFT_OFFSET 28
32 #define FONT_SIZE_LINEDIT 20
33 #define FONT_SIZE_LIST 18
34 #define TEXT_INPUT_WIDTH 800
35 #define SEARCH_BTN_SIZE 105
37 #define WIDGET_WIDTH (SEARCH_BTN_SIZE + SPACER + TEXT_INPUT_WIDTH)
38 #define DISPLAY_WIDTH TEXT_INPUT_WIDTH
39 #define DISPLAY_HEIGHT 480
40 #define COMPLETE_W_WITH_KB 1080
41 #define COMPLETE_H_WITH_KB 1488
42 #define RESULT_ITEM_HEIGHT 80
44 #define AGL_REFRESH_DELAY 75 /* milliseconds */
46 #define SCROLLBAR_STYLE \
47 "QScrollBar:vertical {" \
48 " border: 2px solid grey;" \
49 " background: gray;" \
55 MainApp::MainApp():QMainWindow(Q_NULLPTR, Qt::FramelessWindowHint),
56 networkManager(this),searchBtn(QIcon(tr(":/images/loupe-90.png")), tr(""), this),
57 lineEdit(this),keyboard(QRect(0, 688, COMPLETE_W_WITH_KB, 720), this),
58 mutex(QMutex::Recursive),token(""),currentSearchingText(""),currentSearchedText(""),
59 pSearchReply(NULL),pInfoPanel(NULL),pResultList(NULL),currentLatitude(36.136261),currentLongitude(-115.151254),
60 navicoreSession(0),currentIndex(0),fontId(-1),isInfoScreen(false),
61 isInputDisplayed(false),isKeyboard(false),isAglNavi(false)
63 //this->setAttribute(Qt::WA_TranslucentBackground);
64 this->setStyleSheet("border: none;");
66 searchBtn.setStyleSheet("border: none; color: #FFFFFF;");
67 searchBtn.setMinimumSize(QSize(SEARCH_BTN_SIZE, SEARCH_BTN_SIZE));
68 searchBtn.setIconSize(searchBtn.size());
69 searchBtn.setGeometry(QRect(LEFT_OFFSET, 0, searchBtn.width(), searchBtn.height()));
71 lineEdit.setStyleSheet("border: none; color: #FFFFFF;");
72 lineEdit.setMinimumSize(QSize(TEXT_INPUT_WIDTH, SEARCH_BTN_SIZE));
74 lineEdit.setPlaceholderText(QString(DEFAULT_TEXT));
75 font = lineEdit.font();
76 font.setPointSize(FONT_SIZE_LINEDIT);
77 lineEdit.setFont(font);
78 lineEdit.setTextMargins(MARGINS/2, 0, 0, 0);
79 lineEdit.installEventFilter(this);
80 lineEdit.setGeometry(QRect(LEFT_OFFSET + searchBtn.width() + SPACER, 0, lineEdit.width(), lineEdit.height()));
81 lineEdit.setVisible(false);
84 QFile file(NAVI_CONFIG_FILEPATH);
85 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
87 QByteArray data = file.readAll();
88 QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
89 QJsonObject jsonObj(jsonDoc.object());
91 if (jsonObj.contains("latitude"))
92 currentLatitude = jsonObj["latitude"].toDouble();
94 if (jsonObj.contains("longitude"))
95 currentLongitude = jsonObj["longitude"].toDouble();
98 /* We might need a Japanese font: */
99 QFile fontFile(":/fonts/DroidSansJapanese.ttf");
100 if (!fontFile.open(QIODevice::ReadOnly))
102 TRACE_ERROR("failed to open font file");
106 QByteArray fontData = fontFile.readAll();
107 fontId = QFontDatabase::addApplicationFontFromData(fontData);
110 TRACE_ERROR("QFontDatabase::addApplicationFontFromData failed");
114 /* Check if "AGL_NAVI" env variable is set. If yes, we must notify
115 * AGL environment when surface needs to be resized */
116 if (getenv("AGL_NAVI"))
119 connect(this, SIGNAL(positionGotSignal()), this, SLOT(positionGot()));
120 connect(this, SIGNAL(allRoutesGotSignal()), this, SLOT(allRoutesGot()));
121 connect(this, SIGNAL(routeCreatedSignal()), this, SLOT(routeCreated()));
123 this->setGeometry(QRect(this->pos().x(), this->pos().y(), COMPLETE_W_WITH_KB, COMPLETE_H_WITH_KB));
124 this->setStyleSheet("background-image: url(:/images/AGL_POI_Background.png);");
132 QFontDatabase::removeApplicationFont(fontId);
134 searchBtn.disconnect();
135 lineEdit.disconnect();
136 networkManager.disconnect();
137 keyboard.disconnect();
144 void MainApp::searchBtnClicked()
146 isInputDisplayed = !isInputDisplayed;
147 TRACE_DEBUG("isInputDisplayed = %d", isInputDisplayed);
148 DisplayLineEdit(isInputDisplayed);
151 void MainApp::DisplayLineEdit(bool display)
155 this->setGeometry(QRect(this->pos().x(), this->pos().y(), COMPLETE_W_WITH_KB, COMPLETE_H_WITH_KB));
159 lineEdit.setVisible(true);
166 pResultList->removeEventFilter(this);
175 lineEdit.setText(tr(""));
176 lineEdit.setVisible(false);
178 isInputDisplayed = display;
183 void MainApp::UpdateAglSurfaces()
187 TRACE_DEBUG("handle AGL demo surfaces (new surface is bigger)");
188 snprintf(cmd, 1023, "/usr/bin/LayerManagerControl set surface $SURFACE_ID_CLIENT source region 0 0 %d %d",
189 this->width(), this->height());
190 TRACE_DEBUG("%s", cmd);
192 snprintf(cmd, 1023, "/usr/bin/LayerManagerControl set surface $SURFACE_ID_CLIENT destination region $CLIENT_X $CLIENT_Y %d %d",
193 this->width(), this->height());
194 TRACE_DEBUG("%s", cmd);
198 void MainApp::DisplayResultList(bool display, bool RefreshDisplay)
206 pResultList = new QTreeWidget(this);
207 pResultList->setStyleSheet("border: none; color: #FFFFFF;");
208 pResultList->setRootIsDecorated(false);
209 pResultList->setEditTriggers(QTreeWidget::NoEditTriggers);
210 pResultList->setSelectionBehavior(QTreeWidget::SelectRows);
211 pResultList->setFrameStyle(QFrame::Box | QFrame::Plain);
212 pResultList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
213 //pResultList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
214 pResultList->setAttribute(Qt::WA_AcceptTouchEvents);
215 pResultList->verticalScrollBar()->setStyleSheet(SCROLLBAR_STYLE);
216 pResultList->header()->hide();
217 //font.setPointSize(FONT_SIZE_LIST);
218 //pResultList->setFont(font);
219 pResultList->installEventFilter(this);
222 pResultList->setGeometry(QRect( LEFT_OFFSET+searchBtn.width()+SPACER, searchBtn.height()+SPACER,
223 DISPLAY_WIDTH, DISPLAY_HEIGHT));
226 this->setGeometry(QRect(this->pos().x(), this->pos().y(), COMPLETE_W_WITH_KB, COMPLETE_H_WITH_KB));
228 pResultList->setVisible(true);
229 pResultList->setFocus();
235 pResultList->removeEventFilter(this);
236 pResultList->deleteLater();
244 this->setGeometry(QRect(this->pos().x(), this->pos().y(), COMPLETE_W_WITH_KB, COMPLETE_H_WITH_KB));
251 void MainApp::textChanged(const QString & text)
253 TRACE_INFO("New text is: %s", qPrintable(text));
255 /* do not handle text input if info panel is displayed: */
256 if (pInfoPanel) return;
260 delete pSearchReply; /* cancel current search */
263 if (text.length() == 0) /* if empty text -> no search */
265 DisplayResultList(false);
270 /* if text is the same as previous search -> no need to search again */
271 if (text == currentSearchedText)
273 DisplayResultList(true);
274 FillResultList(Businesses, currentIndex);
278 this->currentSearchingText = text;
280 /* we need to know our current position */
281 std::vector<int32_t> Params;
282 Params.push_back(naviapi::NAVICORE_LONGITUDE);
283 Params.push_back(naviapi::NAVICORE_LATITUDE);
284 naviapi.getPosition(Params);
289 void MainApp::textAdded(const QString & text)
292 lineEdit.setText(lineEdit.text() + text);
294 // be sure any text is visible on initial input
295 lineEdit.setVisible(true);
300 void MainApp::keyPressed(int key)
303 if (key == '\b') /* backspace */
305 int len = lineEdit.text().length();
307 lineEdit.setText(lineEdit.text().remove(len-1, 1));
312 void MainApp::itemClicked()
317 DisplayInformation(true, false);
322 DisplayLineEdit(false);
327 void MainApp::ParseJsonBusinessList(const char* buf, std::vector<Business> & Output)
329 json_object *jobj = json_tokener_parse(buf);
332 TRACE_ERROR("json_tokener_parse failed");
333 cerr << "json_tokener_parse failed: " << buf << endl;
337 json_object_object_foreach(jobj, key, val)
342 if (json_object_get_type(val) == json_type_array)
344 TRACE_DEBUG_JSON("an array was found");
346 if(json_object_object_get_ex(jobj, "businesses", &value))
348 TRACE_DEBUG_JSON("an business was found");
350 int arraylen = json_object_array_length(value);
352 for (int i = 0; i < arraylen; i++)
354 Business NewBusiness;
356 json_object* medi_array_obj, *medi_array_obj_elem;
357 medi_array_obj = json_object_array_get_idx(value, i);
360 if (json_object_object_get_ex(medi_array_obj, "rating", &medi_array_obj_elem))
362 NewBusiness.Rating = json_object_get_double(medi_array_obj_elem);
363 TRACE_DEBUG_JSON("got Rating : %f", NewBusiness.Rating);
366 if (json_object_object_get_ex(medi_array_obj, "distance", &medi_array_obj_elem))
368 NewBusiness.Distance = json_object_get_double(medi_array_obj_elem);
369 TRACE_DEBUG_JSON("got Distance : %f", NewBusiness.Distance);
372 if (json_object_object_get_ex(medi_array_obj, "review_count", &medi_array_obj_elem))
374 NewBusiness.ReviewCount = json_object_get_int(medi_array_obj_elem);
375 TRACE_DEBUG_JSON("got ReviewCount : %u", NewBusiness.ReviewCount);
378 if (json_object_object_get_ex(medi_array_obj, "name", &medi_array_obj_elem))
380 NewBusiness.Name = QString(json_object_get_string(medi_array_obj_elem));
381 TRACE_DEBUG_JSON("got Name : %s", qPrintable(NewBusiness.Name));
384 if (json_object_object_get_ex(medi_array_obj, "image_url", &medi_array_obj_elem))
386 NewBusiness.ImageUrl = QString(json_object_get_string(medi_array_obj_elem));
387 TRACE_DEBUG_JSON("got ImageUrl : %s", qPrintable(NewBusiness.ImageUrl));
390 if (json_object_object_get_ex(medi_array_obj, "phone", &medi_array_obj_elem))
392 NewBusiness.Phone = QString(json_object_get_string(medi_array_obj_elem));
393 TRACE_DEBUG_JSON("got Phone : %s", qPrintable(NewBusiness.Phone));
396 if (json_object_object_get_ex(medi_array_obj, "coordinates", &medi_array_obj_elem))
400 TRACE_DEBUG_JSON("coordinates were found");
402 if(json_object_object_get_ex(medi_array_obj_elem, "latitude", &value2))
404 NewBusiness.Latitude = json_object_get_double(value2);
405 TRACE_DEBUG_JSON("got Latitude : %f", NewBusiness.Latitude);
408 if(json_object_object_get_ex(medi_array_obj_elem, "longitude", &value2))
410 NewBusiness.Longitude = json_object_get_double(value2);
411 TRACE_DEBUG_JSON("got Longitude : %f", NewBusiness.Longitude);
415 if (json_object_object_get_ex(medi_array_obj, "location", &medi_array_obj_elem))
419 TRACE_DEBUG_JSON("a location was found");
421 /* TODO: how do we deal with address2 and address3 ? */
422 if(json_object_object_get_ex(medi_array_obj_elem, "address1", &value2))
424 NewBusiness.Address = QString(json_object_get_string(value2));
425 TRACE_DEBUG_JSON("got Address : %s", qPrintable(NewBusiness.Address));
428 if(json_object_object_get_ex(medi_array_obj_elem, "city", &value2))
430 NewBusiness.City = QString(json_object_get_string(value2));
431 TRACE_DEBUG_JSON("got City : %s", qPrintable(NewBusiness.City));
434 if(json_object_object_get_ex(medi_array_obj_elem, "state", &value2))
436 NewBusiness.State = QString(json_object_get_string(value2));
437 TRACE_DEBUG_JSON("got State : %s", qPrintable(NewBusiness.State));
440 if(json_object_object_get_ex(medi_array_obj_elem, "zip_code", &value2))
442 NewBusiness.ZipCode = QString(json_object_get_string(value2));
443 TRACE_DEBUG_JSON("got ZipCode : %s", qPrintable(NewBusiness.ZipCode));
446 if(json_object_object_get_ex(medi_array_obj_elem, "country", &value2))
448 NewBusiness.Country = QString(json_object_get_string(value2));
449 TRACE_DEBUG_JSON("got Country : %s", qPrintable(NewBusiness.Country));
453 /* TODO: parse categories */
455 /* Add business in our list: */
456 Businesses.push_back(NewBusiness);
463 json_object_put(jobj);
466 bool MainApp::eventFilter(QObject *obj, QEvent *ev)
472 if (obj == pResultList)
474 //TRACE_DEBUG("ev->type() = %d", (int)ev->type());
476 if (ev->type() == QEvent::KeyPress)
478 bool consumed = false;
479 int key = static_cast<QKeyEvent*>(ev)->key();
480 TRACE_DEBUG("key pressed (%d)", key);
484 TRACE_DEBUG("enter or return");
487 DisplayInformation(true);
492 DisplayLineEdit(false);
498 TRACE_DEBUG("escape");
499 DisplayResultList(false);
508 case Qt::Key_PageDown:
509 TRACE_DEBUG("arrows");
513 TRACE_DEBUG("default");
522 else if (obj == &lineEdit)
524 if (pInfoPanel && ev->type() == QEvent::KeyPress)
526 switch(static_cast<QKeyEvent*>(ev)->key())
529 TRACE_DEBUG("Escape !");
530 DisplayInformation(false, false);
531 DisplayResultList(true);
532 FillResultList(Businesses, currentIndex);
537 SetDestination(currentIndex);
538 DisplayLineEdit(false);
546 ret = QMainWindow::eventFilter(obj, ev);
552 void MainApp::resizeEvent(QResizeEvent* event)
554 QMainWindow::resizeEvent(event);
557 QTimer::singleShot(AGL_REFRESH_DELAY, Qt::CoarseTimer, this, SLOT(UpdateAglSurfaces()));
561 void MainApp::SetDestination(int index)
565 /* if pResultList exists, take the selected index
566 * otherwise, take the index given as parameter */
569 QList<QTreeWidgetItem *> SelectedItems = pResultList->selectedItems();
570 if (SelectedItems.size() > 0)
572 /* select the first selected item : */
573 index = pResultList->indexOfTopLevelItem(*SelectedItems.begin());
577 TRACE_DEBUG("index is: %d", index);
579 /* retrieve the coordinates of this item : */
580 this->destinationLatitude = Businesses[index].Latitude;
581 this->destinationLongitude = Businesses[index].Longitude;
583 naviapi.getAllRoutes();
588 void MainApp::DisplayInformation(bool display, bool RefreshDisplay)
593 /* pResultList must exist, so that we can retrieve the selected index: */
596 TRACE_ERROR("pResultList is null");
601 QList<QTreeWidgetItem *> SelectedItems = pResultList->selectedItems();
602 if (SelectedItems.size() <= 0)
604 TRACE_ERROR("no item is selected");
609 /* select the first selected item : */
610 currentIndex = pResultList->indexOfTopLevelItem(*SelectedItems.begin());
613 DisplayResultList(false, false);
615 /* Display info for the selected item: */
616 QRect rect( LEFT_OFFSET+searchBtn.width()+SPACER, searchBtn.height()+SPACER,
617 DISPLAY_WIDTH, DISPLAY_HEIGHT);
618 pInfoPanel = new InfoPanel(this, Businesses[currentIndex], rect);
622 this->setGeometry(QRect(this->pos().x(), this->pos().y(), COMPLETE_W_WITH_KB, COMPLETE_H_WITH_KB));
625 connect(pInfoPanel->getGoButton(), SIGNAL(clicked(bool)), this, SLOT(goClicked()));
626 connect(pInfoPanel->getCancelButton(), SIGNAL(clicked(bool)), this, SLOT(cancelClicked()));
632 pInfoPanel->getGoButton()->disconnect();
633 pInfoPanel->getCancelButton()->disconnect();
641 this->setGeometry(QRect(this->pos().x(), this->pos().y(), COMPLETE_W_WITH_KB, COMPLETE_H_WITH_KB));
648 void MainApp::networkReplySearch(QNetworkReply* reply)
654 /* memorize the text which gave this result: */
655 currentSearchedText = lineEdit.text();
657 if (reply->error() == QNetworkReply::NoError)
659 // we only handle this callback if it matches the last search request:
660 if (reply != pSearchReply)
662 TRACE_INFO("this reply is already too late (or about a different network request)");
667 buf = reply->readAll();
679 ParseJsonBusinessList(buf.data(), Businesses);
680 DisplayResultList(true);
681 FillResultList(Businesses);
685 fprintf(stderr,"POI: reply error network please check to poikey and system time (adjusted?)\n");
691 /* pResultList must be allocated at this point ! */
692 int MainApp::FillResultList(vector<Business> & list, int focusIndex)
698 pResultList->setUpdatesEnabled(false);
699 pResultList->clear();
701 /* filling the dropdown menu: */
702 for (vector<Business>::iterator it = list.begin(); it != list.end(); it++)
704 /* workaround to avoid entries with wrong coordinates returned by Yelp: */
705 if (IsCoordinatesConsistent(*it) == false)
711 QTreeWidgetItem * item = new QTreeWidgetItem(pResultList);
713 ClickableLabel *label = new ClickableLabel("<b>"+(*it).Name+
714 "</b><br>"+(*it).Address+", "+(*it).City+", "+(*it).State+
715 " "+(*it).ZipCode+", "+(*it).Country, pResultList);
716 label->setTextFormat(Qt::RichText);
717 font.setPointSize(FONT_SIZE_LIST);
718 label->setFont(font);
719 label->setIndent(MARGINS);
720 label->setAttribute(Qt::WA_AcceptTouchEvents);
721 item->setSizeHint(0, QSize(TEXT_INPUT_WIDTH, RESULT_ITEM_HEIGHT));
722 pResultList->setItemWidget(item, 0, label);
723 connect(label, SIGNAL(clicked()), this, SLOT(itemClicked()));
725 //item->setText(0, (*it).Name);
727 if (nbElem == focusIndex)
729 pResultList->setCurrentItem(item);
734 pResultList->setUpdatesEnabled(true);
740 /* Well... some of the POI returned by Yelp have coordinates which are
741 * completely inconsistent with the distance at which the POI is
743 * https://github.com/Yelp/yelp-fusion/issues/104
744 * Let's skip them for the moment: */
745 #define PI 3.14159265
746 #define EARTH_RADIUS 6371000
747 static inline double toRadians(double a) { return a * PI / 180.0; }
748 bool MainApp::IsCoordinatesConsistent(Business & business)
750 double lat1 = toRadians(currentLatitude);
751 double lon1 = toRadians(currentLongitude);
752 double lat2 = toRadians(business.Latitude);
753 double lon2 = toRadians(business.Longitude);
754 double x = (lon2 - lon1) * cos((lat1 + lat2)/2);
755 double y = lat2 - lat1;
756 double DistanceFromCoords = EARTH_RADIUS * sqrt(pow(x, 2) + pow(y, 2));
758 /* if calculated distance is not between +/- 10% of the announced
759 * distance -> skip this POI: */
760 if (DistanceFromCoords < business.Distance * 0.9 ||
761 DistanceFromCoords > business.Distance * 1.1)
763 TRACE_ERROR("Announced distance: %f, calculated distance: %f", business.Distance, DistanceFromCoords);
769 /* end of workaround */
771 bool MainApp::CheckNaviApi(int argc, char *argv[])
773 bool ret = naviapi.connect(argc, argv, this);
777 naviapi.getAllSessions();
783 int MainApp::AuthenticatePOI(const QString & CredentialsFile)
788 QString ProxyHostName;
794 /* First, read AppId and AppSecret from credentials file: */
795 FILE* filep = fopen(qPrintable(CredentialsFile), "r");
798 fprintf(stderr,"Failed to open credentials file \"%s\": %m", qPrintable(CredentialsFile));
802 if (!fgets(buf, 512, filep))
804 fprintf(stderr,"Failed to read AppId from credentials file \"%s\"", qPrintable(CredentialsFile));
808 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
809 buf[strlen(buf)-1] = '\0';
810 AppId = QString(buf);
812 if (!fgets(buf, 512, filep))
814 fprintf(stderr,"Failed to read AppSecret from credentials file \"%s\"", qPrintable(CredentialsFile));
818 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
819 buf[strlen(buf)-1] = '\0';
820 AppSecret = QString(buf);
825 if (!fgets(buf, 512, filep))
827 TRACE_INFO("Failed to read ProxyHostName from credentials file \"%s\"", qPrintable(CredentialsFile));
831 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
832 buf[strlen(buf)-1] = '\0';
833 ProxyHostName = QString(buf);
834 ProxyHostName.replace(0, 14, tr(""));
837 if (!fgets(buf, 512, filep))
839 TRACE_ERROR("Failed to read Port from credentials file \"%s\"", qPrintable(CredentialsFile));
843 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
844 buf[strlen(buf)-1] = '\0';
845 PortStr = QString(buf);
846 PortStr.replace(0, 5, tr(""));
847 portnum = PortStr.toInt();
850 if (!fgets(buf, 512, filep))
852 TRACE_ERROR("Failed to read User from credentials file \"%s\"", qPrintable(CredentialsFile));
856 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
857 buf[strlen(buf)-1] = '\0';
859 User.replace(0, 5, tr(""));
862 if (!fgets(buf, 512, filep))
864 TRACE_ERROR("Failed to read Password from credentials file \"%s\"", qPrintable(CredentialsFile));
868 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
869 buf[strlen(buf)-1] = '\0';
870 Password = QString(buf);
871 Password.replace(0, 9, tr(""));
873 proxy.setType(QNetworkProxy::HttpProxy);
874 proxy.setHostName(qPrintable(ProxyHostName));
875 proxy.setPort(portnum);
876 proxy.setUser(qPrintable(User));
877 proxy.setPassword(qPrintable(Password));
878 QNetworkProxy::setApplicationProxy(proxy);
883 TRACE_INFO("Found credentials");
885 /* Then, send a HTTP request to get the token and wait for answer (synchronously): */
891 int MainApp::StartMonitoringUserInput()
893 connect(&searchBtn, SIGNAL(clicked(bool)), this, SLOT(searchBtnClicked()));
894 connect(&lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
895 connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkReplySearch(QNetworkReply*)));
896 connect(&keyboard, SIGNAL(keyClicked(const QString &)), this, SLOT(textAdded(const QString &)));
897 connect(&keyboard, SIGNAL(specialKeyClicked(int)), this, SLOT(keyPressed(int)));
901 void MainApp::SetWayPoints(uint32_t myRoute)
903 /* set the destination : */
904 naviapi::Waypoint destWp(this->destinationLatitude, this->destinationLongitude);
905 std::vector<naviapi::Waypoint> myWayPoints;
906 myWayPoints.push_back(destWp);
907 naviapi.setWaypoints(navicoreSession, myRoute, true, myWayPoints);
909 naviapi.calculateRoute(navicoreSession, myRoute);
912 currentSearchingText = tr("");
913 currentSearchedText = tr("");
918 void MainApp::goClicked()
920 TRACE_DEBUG("Go clicked !");
921 SetDestination(currentIndex);
922 DisplayLineEdit(false);
925 void MainApp::cancelClicked()
927 TRACE_DEBUG("Cancel clicked !");
928 DisplayInformation(false, false);
929 DisplayResultList(true, false);
930 FillResultList(Businesses, currentIndex);
933 void MainApp::getAllSessions_reply(const std::map< uint32_t, std::string >& allSessions)
937 if (allSessions.empty())
939 TRACE_ERROR("Error: could not find an instance of Navicore");
944 this->navicoreSession = allSessions.begin()->first;
946 TRACE_INFO("Current session: %d", this->navicoreSession);
952 void MainApp::getPosition_reply(std::map< int32_t, naviapi::variant > position)
956 std::map< int32_t, naviapi::variant >::iterator it;
957 for (it = position.begin(); it != position.end(); it++)
959 if (it->first == naviapi::NAVICORE_LATITUDE)
961 currentLatitude = it->second._double;
963 else if (it->first == naviapi::NAVICORE_LONGITUDE)
965 currentLongitude = it->second._double;
969 TRACE_INFO("Current position: %f, %f", currentLatitude, currentLongitude);
973 emit positionGotSignal();
976 void MainApp::getAllRoutes_reply(std::vector< uint32_t > allRoutes)
980 uint32_t routeHandle = 0;
982 if (allRoutes.size() != 0)
984 routeHandle = allRoutes[0];
987 this->currentRouteHandle = routeHandle;
991 emit allRoutesGotSignal();
994 void MainApp::createRoute_reply(uint32_t routeHandle)
998 this->currentRouteHandle = routeHandle;
1002 emit routeCreatedSignal();
1005 void MainApp::positionGot()
1009 /* let's generate a search request : */
1010 QString myUrlStr = URL_SEARCH + tr("?") + tr("term=") + currentSearchingText +
1011 tr("&latitude=") + QString::number(currentLatitude) +
1012 tr("&longitude=") + QString::number(currentLongitude);
1014 TRACE_DEBUG("URL: %s", qPrintable(myUrlStr));
1016 QUrl myUrl = QUrl(myUrlStr);
1017 QNetworkRequest req(myUrl);
1018 req.setRawHeader(QByteArray("Authorization"), (tr("bearer ") + token).toLocal8Bit());
1020 /* Then, send a HTTP request to get the token and wait for answer (synchronously): */
1022 pSearchReply = networkManager.get(req);
1027 void MainApp::allRoutesGot()
1031 /* check if a route already exists, if not create it : */
1032 if (this->currentRouteHandle == 0)
1034 naviapi.createRoute(navicoreSession);
1038 naviapi.pauseSimulation(navicoreSession);
1039 naviapi.setSimulationMode(navicoreSession, false);
1040 naviapi.cancelRouteCalculation(navicoreSession, this->currentRouteHandle);
1043 SetWayPoints(this->currentRouteHandle);
1049 void MainApp::routeCreated()
1053 SetWayPoints(this->currentRouteHandle);