1 #include "googlelatitude.h"
2 #include <QtNetwork/QNetworkConfigurationManager>
4 GoogleLatitude::GoogleLatitude(QObject *parent) :
7 OAuthGetRequestToken("https://www.google.com/accounts/OAuthGetRequestToken"),
8 OAuthAuthorizeToken("https://www.google.com/accounts/OAuthAuthorizeToken"),
9 OAuthGetAccessToken("https://www.google.com/accounts/OAuthGetAccessToken"),
10 CurrentLocation("https://www.googleapis.com/latitude/v1/currentLocation"),
11 UserAuthorization(""),
12 ConsumerKey("1062862865804.apps.googleusercontent.com"),
13 ConsumerSecretKey("EYQaRaUJ9Fznw8mPMor660Kx"),
17 qDebug() << "* GoogleLatitude::GoogleLatitude";
18 OauthRequest = new KQOAuthRequest(this);
19 OauthManager = new KQOAuthManager(this);
20 GoogleOauthAdditional.insert("scope", "https://www.googleapis.com/auth/latitude");
21 GoogleOauthAdditional.insert("xoauth_displayname", "LatitudeUpdater");
23 connect(OauthManager, SIGNAL(temporaryTokenReceived(QString,QString)),
24 this, SLOT(onTemporaryTokenReceived(QString, QString)));
26 connect(OauthManager, SIGNAL(authorizationReceived(QString,QString)),
27 this, SLOT(onAuthorizationReceived(QString, QString)));
29 connect(OauthManager, SIGNAL(accessTokenReceived(QString,QString)),
30 this, SLOT(onAccessTokenReceived(QString,QString)));
32 connect(OauthManager, SIGNAL(requestReady(QByteArray)),
33 this, SLOT(onRequestReady(QByteArray)));
35 connect(OauthManager, SIGNAL(authorizedRequestDone()),
36 this, SLOT(onAuthorizedRequestDone()));
38 connect(this, SIGNAL(gotToken()),
39 this, SLOT(getCurrentLocation()));
41 connect(this, SIGNAL(notToken()),
42 this, SLOT(onNotToken()));
44 connect(this, SIGNAL(gotToken()),
45 this, SLOT(onGotToken()));
47 connect(this, SIGNAL(needAuth()),
48 this, SLOT(onNeedAuth()));
51 GoogleLatitude::~GoogleLatitude() {
52 qDebug() << "* GoogleLatitude::~GoogleLatitude";
57 void GoogleLatitude::getAccess() {
58 qDebug() << "* GoogleLatitude::getAccess";
59 if ( OauthSettings.value("oauth_token").isNull() || OauthSettings.value("oauth_token_secret").isNull() ) {
60 OauthRequest->clearRequest();
61 OauthRequest->initRequest(KQOAuthRequest::TemporaryCredentials, OAuthGetRequestToken);
62 OauthRequest->setConsumerKey(ConsumerKey);
63 OauthRequest->setConsumerSecretKey(ConsumerSecretKey);
64 OauthRequest->setAdditionalParameters(GoogleOauthAdditional);
65 OauthManager->setHandleUserAuthorization(true);
66 OauthRequest->setHttpMethod(KQOAuthRequest::POST);
67 OauthManager->executeRequest(OauthRequest);
73 void GoogleLatitude::getCurrentLocation() {
74 qDebug() << "* GoogleLatitude::getCurrentLocation";
75 if ( OauthSettings.value("oauth_token").isNull() || OauthSettings.value("oauth_token_secret").isNull() ) {
78 OauthRequest->clearRequest();
79 OauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, CurrentLocation);
80 OauthRequest->setToken(OauthSettings.value("oauth_token").toString());
81 OauthRequest->setTokenSecret(OauthSettings.value("oauth_token_secret").toString());
82 OauthRequest->setConsumerKey(ConsumerKey);
83 OauthRequest->setConsumerSecretKey(ConsumerSecretKey);
84 OauthRequest->setHttpMethod(KQOAuthRequest::GET);
85 OauthManager->executeRequest(OauthRequest);
89 void GoogleLatitude::sendCurrentLocation() {
90 qDebug() << "* GoogleLatitude::sendCurrentLocation";
91 if (OauthSettings.value("oauth_token").isNull() || OauthSettings.value("oauth_token_secret").isNull()) {
94 if (abs(CurrentLatitude) <= 0.01) return;
95 if (abs(CurrentLongitude) <= 0.01) return;
96 if (abs(CurrentAccuracy) <= 0.01) return;
98 if (!OauthSettings.value("net_auto").toBool()) {
99 QNetworkConfigurationManager mgr;
100 if (!mgr.isOnline()) {
101 qDebug() << "* GoogleLatitude::sendCurrentLocation" << "offline";
106 QByteArray json_location;
107 json_location = "{\"data\": {\"kind\":\"latitude#location\",";
108 json_location += QString("\"latitude\":%1,").arg(CurrentLatitude);
109 json_location += QString("\"longitude\":%1,").arg(CurrentLongitude);
110 json_location += QString("\"accuracy\":%1").arg(CurrentAccuracy);
111 json_location += "}}";
112 qDebug() << "json_location" << json_location;
114 OauthRequest->clearRequest();
115 OauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, CurrentLocation);
116 OauthRequest->setToken(OauthSettings.value("oauth_token").toString());
117 OauthRequest->setTokenSecret(OauthSettings.value("oauth_token_secret").toString());
118 OauthRequest->setConsumerKey(ConsumerKey);
119 OauthRequest->setConsumerSecretKey(ConsumerSecretKey);
120 OauthRequest->setHttpMethod(KQOAuthRequest::POST);
121 OauthRequest->setContentType("application/json");
122 OauthRequest->setRawData(json_location);
123 OauthManager->executeRequest(OauthRequest);
127 void GoogleLatitude::setCurrentLocation(double lat, double lon, double acc) {
128 qDebug() << "* GoogleLatitude::setCurrentLocation" << lat << lon << acc;
129 CurrentLatitude = lat;
130 CurrentLongitude = lon;
131 CurrentAccuracy = acc;
134 QUrl GoogleLatitude::getUserAuthorization() {
135 qDebug() << "* GoogleLatitude::getUserAuthorization";
136 return UserAuthorization;
139 void GoogleLatitude::setAutoConnect(bool status) {
140 qDebug() << "* GoogleLatitude::setAutoConnect" << status;
141 OauthSettings.setValue("net_auto", status);
144 bool GoogleLatitude::getAutoConnect() {
145 qDebug() << "* GoogleLatitude::getAutoConnect";
146 return OauthSettings.value("net_auto").toBool();
149 void GoogleLatitude::onTemporaryTokenReceived(QString temporaryToken, QString temporaryTokenSecret) {
150 qDebug() << "* GoogleLatitude::onTemporaryTokenReceived" << temporaryToken << temporaryTokenSecret;
151 if( OauthManager->lastError() == KQOAuthManager::NoError) {
152 UserAuthorization = OauthManager->getUserAuthorization(OAuthAuthorizeToken);
153 qDebug() << "* GoogleLatitude::onTemporaryTokenReceived" << "UserAuthorization" << UserAuthorization;
158 void GoogleLatitude::onAuthorizationReceived(QString token, QString verifier) {
159 qDebug() << "* GoogleLatitude::onAuthorizationReceived" << token << verifier;
160 OauthManager->getUserAccessTokens(OAuthGetAccessToken);
161 if(OauthManager->lastError() != KQOAuthManager::NoError) {
166 void GoogleLatitude::onAccessTokenReceived(QString token, QString tokenSecret) {
167 qDebug() << "* GoogleLatitude::onAccessTokenReceived" << token << tokenSecret;
168 OauthSettings.setValue("oauth_token", token);
169 OauthSettings.setValue("oauth_token_secret", tokenSecret);
173 void GoogleLatitude::onRequestReady(QByteArray response) {
174 qDebug() << "* GoogleLatitude::onRequestReady" << response;
175 if (response.contains("Invalid Credentials") ) {
176 OauthSettings.remove("oauth_token");
177 OauthSettings.remove("oauth_token_secret");