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 QNetworkConfigurationManager mgr;
99 if (!OauthSettings.value("net_auto").toBool() && !mgr.isOnline()) {
100 qDebug() << "* GoogleLatitude::sendCurrentLocation" << "offline";
104 QByteArray json_location;
105 json_location = "{\"data\": {\"kind\":\"latitude#location\",";
106 json_location += QString("\"latitude\":%1,").arg(CurrentLatitude);
107 json_location += QString("\"longitude\":%1,").arg(CurrentLongitude);
108 json_location += QString("\"accuracy\":%1").arg(CurrentAccuracy);
109 json_location += "}}";
110 qDebug() << "json_location" << json_location;
112 OauthRequest->clearRequest();
113 OauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, CurrentLocation);
114 OauthRequest->setToken(OauthSettings.value("oauth_token").toString());
115 OauthRequest->setTokenSecret(OauthSettings.value("oauth_token_secret").toString());
116 OauthRequest->setConsumerKey(ConsumerKey);
117 OauthRequest->setConsumerSecretKey(ConsumerSecretKey);
118 OauthRequest->setHttpMethod(KQOAuthRequest::POST);
119 OauthRequest->setContentType("application/json");
120 OauthRequest->setRawData(json_location);
121 OauthManager->executeRequest(OauthRequest);
125 void GoogleLatitude::setCurrentLocation(double lat, double lon, double acc) {
126 qDebug() << "* GoogleLatitude::setCurrentLocation" << lat << lon << acc;
127 CurrentLatitude = lat;
128 CurrentLongitude = lon;
129 CurrentAccuracy = acc;
132 QUrl GoogleLatitude::getUserAuthorization() {
133 qDebug() << "* GoogleLatitude::getUserAuthorization";
134 return UserAuthorization;
137 void GoogleLatitude::setAutoConnect(bool status) {
138 qDebug() << "* GoogleLatitude::setAutoConnect" << status;
139 OauthSettings.setValue("net_auto", status);
142 bool GoogleLatitude::getAutoConnect() {
143 qDebug() << "* GoogleLatitude::getAutoConnect";
144 return OauthSettings.value("net_auto").toBool();
147 void GoogleLatitude::onTemporaryTokenReceived(QString temporaryToken, QString temporaryTokenSecret) {
148 qDebug() << "* GoogleLatitude::onTemporaryTokenReceived" << temporaryToken << temporaryTokenSecret;
149 if( OauthManager->lastError() == KQOAuthManager::NoError) {
150 UserAuthorization = OauthManager->getUserAuthorization(OAuthAuthorizeToken);
151 qDebug() << "* GoogleLatitude::onTemporaryTokenReceived" << "UserAuthorization" << UserAuthorization;
156 void GoogleLatitude::onAuthorizationReceived(QString token, QString verifier) {
157 qDebug() << "* GoogleLatitude::onAuthorizationReceived" << token << verifier;
158 OauthManager->getUserAccessTokens(OAuthGetAccessToken);
159 if(OauthManager->lastError() != KQOAuthManager::NoError) {
164 void GoogleLatitude::onAccessTokenReceived(QString token, QString tokenSecret) {
165 qDebug() << "* GoogleLatitude::onAccessTokenReceived" << token << tokenSecret;
166 OauthSettings.setValue("oauth_token", token);
167 OauthSettings.setValue("oauth_token_secret", tokenSecret);
171 void GoogleLatitude::onRequestReady(QByteArray response) {
172 qDebug() << "* GoogleLatitude::onRequestReady" << response;
173 if (response.contains("Invalid Credentials") ) {
174 OauthSettings.remove("oauth_token");
175 OauthSettings.remove("oauth_token_secret");