81b1d5bc9628417c052883b142adf5297228b52f
[googlelatitude] / googlelatitude.cpp
1 #include "googlelatitude.h"
2 #include <QtNetwork/QNetworkConfigurationManager>
3
4 GoogleLatitude::GoogleLatitude(QObject *parent) :
5     QObject(parent),
6     OauthSettings(this),
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"),
14     CurrentLatitude(0),
15     CurrentLongitude(0),
16     CurrentAccuracy(0) {
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");
22
23     connect(OauthManager, SIGNAL(temporaryTokenReceived(QString,QString)),
24             this, SLOT(onTemporaryTokenReceived(QString, QString)));
25
26     connect(OauthManager, SIGNAL(authorizationReceived(QString,QString)),
27             this, SLOT(onAuthorizationReceived(QString, QString)));
28
29     connect(OauthManager, SIGNAL(accessTokenReceived(QString,QString)),
30             this, SLOT(onAccessTokenReceived(QString,QString)));
31
32     connect(OauthManager, SIGNAL(requestReady(QByteArray)),
33             this, SLOT(onRequestReady(QByteArray)));
34
35     connect(OauthManager, SIGNAL(authorizedRequestDone()),
36             this, SLOT(onAuthorizedRequestDone()));
37
38     connect(this, SIGNAL(gotToken()),
39             this, SLOT(getCurrentLocation()));
40
41     connect(this, SIGNAL(notToken()),
42             this, SLOT(onNotToken()));
43
44     connect(this, SIGNAL(gotToken()),
45             this, SLOT(onGotToken()));
46
47     connect(this, SIGNAL(needAuth()),
48             this, SLOT(onNeedAuth()));
49 }
50
51 GoogleLatitude::~GoogleLatitude() {
52     qDebug() << "* GoogleLatitude::~GoogleLatitude";
53     delete OauthManager;
54     delete OauthRequest;
55 }
56
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);
68     } else {
69         emit gotToken();
70     }
71 }
72
73 void GoogleLatitude::getCurrentLocation() {
74     qDebug() << "* GoogleLatitude::getCurrentLocation";
75     if ( OauthSettings.value("oauth_token").isNull() || OauthSettings.value("oauth_token_secret").isNull() ) {
76         emit notToken();
77     } else {
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);
86     }
87 }
88
89 void GoogleLatitude::sendCurrentLocation() {
90     qDebug() << "* GoogleLatitude::sendCurrentLocation";
91     if (OauthSettings.value("oauth_token").isNull() || OauthSettings.value("oauth_token_secret").isNull()) {
92         emit notToken();
93     } else {
94         if (abs(CurrentLatitude) <= 0.01) return;
95         if (abs(CurrentLongitude) <= 0.01) return;
96         if (abs(CurrentAccuracy) <= 0.01) return;
97
98         if (!OauthSettings.value("net_auto").toBool()) {
99             QNetworkConfigurationManager mgr;
100             if (!mgr.isOnline()) {
101                 qDebug() << "* GoogleLatitude::sendCurrentLocation" << "offline";
102                 return;
103             }
104         }
105
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;
113
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);
124     }
125 }
126
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;
132 }
133
134 QUrl GoogleLatitude::getUserAuthorization() {
135     qDebug() << "* GoogleLatitude::getUserAuthorization";
136     return UserAuthorization;
137 }
138
139 void GoogleLatitude::setAutoConnect(bool status) {
140     qDebug() << "* GoogleLatitude::setAutoConnect" << status;
141     OauthSettings.setValue("net_auto", status);
142 }
143
144 bool GoogleLatitude::getAutoConnect() {
145     qDebug() << "* GoogleLatitude::getAutoConnect";
146     return OauthSettings.value("net_auto").toBool();
147 }
148
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;
154         emit needAuth();
155     }
156 }
157
158 void GoogleLatitude::onAuthorizationReceived(QString token, QString verifier) {
159     qDebug() << "* GoogleLatitude::onAuthorizationReceived" << token << verifier;
160     OauthManager->getUserAccessTokens(OAuthGetAccessToken);
161     if(OauthManager->lastError() != KQOAuthManager::NoError) {
162         emit notToken();
163     }
164 }
165
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);
170     emit gotToken();
171 }
172
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");
178         emit notToken();
179     }
180 }