changes for formeego, do not use
[googlelatitude] / liblatitudeupdater / 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         QNetworkConfigurationManager mgr;
99         if (!OauthSettings.value("net_auto").toBool() && !mgr.isOnline()) {
100             qDebug() << "* GoogleLatitude::sendCurrentLocation" << "offline";
101             return;
102         }
103
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;
111
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);
122     }
123 }
124
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;
130 }
131
132 QUrl GoogleLatitude::getUserAuthorization() {
133     qDebug() << "* GoogleLatitude::getUserAuthorization";
134     return UserAuthorization;
135 }
136
137 void GoogleLatitude::setAutoConnect(bool status) {
138     qDebug() << "* GoogleLatitude::setAutoConnect" << status;
139     OauthSettings.setValue("net_auto", status);
140 }
141
142 bool GoogleLatitude::getAutoConnect() {
143     qDebug() << "* GoogleLatitude::getAutoConnect";
144     return OauthSettings.value("net_auto", true).toBool();
145 }
146
147 void GoogleLatitude::setDaemonMode(bool status) {
148     qDebug() << "* GoogleLatitude::setDaemonMode" << status;
149     OauthSettings.setValue("daemon", status);
150 }
151
152 bool GoogleLatitude::getDaemonMode() {
153     qDebug() << "* GoogleLatitude::getDaemonMode";
154     return OauthSettings.value("daemon", true).toBool();
155 }
156
157 void GoogleLatitude::onTemporaryTokenReceived(QString temporaryToken, QString temporaryTokenSecret) {
158     qDebug() << "* GoogleLatitude::onTemporaryTokenReceived" << temporaryToken << temporaryTokenSecret;
159     if( OauthManager->lastError() == KQOAuthManager::NoError) {
160         UserAuthorization = OauthManager->getUserAuthorization(OAuthAuthorizeToken);
161         qDebug() << "* GoogleLatitude::onTemporaryTokenReceived" << "UserAuthorization" << UserAuthorization;
162         emit needAuth();
163     }
164 }
165
166 void GoogleLatitude::onAuthorizationReceived(QString token, QString verifier) {
167     qDebug() << "* GoogleLatitude::onAuthorizationReceived" << token << verifier;
168     OauthManager->getUserAccessTokens(OAuthGetAccessToken);
169     if(OauthManager->lastError() != KQOAuthManager::NoError) {
170         emit notToken();
171     }
172 }
173
174 void GoogleLatitude::onAccessTokenReceived(QString token, QString tokenSecret) {
175     qDebug() << "* GoogleLatitude::onAccessTokenReceived" << token << tokenSecret;
176     OauthSettings.setValue("oauth_token", token);
177     OauthSettings.setValue("oauth_token_secret", tokenSecret);
178     emit gotToken();
179 }
180
181 void GoogleLatitude::onRequestReady(QByteArray response) {
182     qDebug() << "* GoogleLatitude::onRequestReady" << response;
183     if (response.contains("Invalid Credentials") ) {
184         qDebug() << "* GoogleLatitude::onRequestReady" << "Invalid Credentials";
185         OauthSettings.remove("oauth_token");
186         OauthSettings.remove("oauth_token_secret");
187         UserAuthorization.clear();
188         emit notToken();
189     }
190 }