~ubuntu-branches/ubuntu/vivid/goldencheetah/vivid-proposed

« back to all changes in this revision

Viewing changes to src/StravaDownloadDialog.cpp

  • Committer: Package Import Robot
  • Author(s): KURASHIKI Satoru
  • Date: 2013-12-25 10:30:44 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20131225103044-11ttdb8r9us31lpn
Tags: 3.0.1-1
* New upstream release.
  Use dirstributed tarball to build. (Closes: #711722)
* New Standards-Version: 3.9.5
* remove qwt-format patch (merged at upstream).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2013 Damien.Grauser (damien.grauser@pev-geneve.ch)
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License as published by the Free
6
 
 * Software Foundation; either version 2 of the License, or (at your option)
7
 
 * any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful, but WITHOUT
10
 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12
 
 * more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License along
15
 
 * with this program; if not, write to the Free Software Foundation, Inc., 51
16
 
 * Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 */
18
 
 
19
 
#include "StravaDownloadDialog.h"
20
 
#include "Settings.h"
21
 
#include <QHttp>
22
 
#include <QUrl>
23
 
#include <QScriptEngine>
24
 
#include "TimeUtils.h"
25
 
#include "Units.h"
26
 
 
27
 
// acccess to metrics
28
 
#include "MetricAggregator.h"
29
 
#include "RideMetric.h"
30
 
#include "DBAccess.h"
31
 
#include "RideImportWizard.h"
32
 
#include "TcxRideFile.h"
33
 
 
34
 
 
35
 
// Download a Strava activity using the Strava API V1
36
 
// http://www.strava.com/api/v1/streams/9999
37
 
// which returns a json string with time-series of speed, position, heartrate, etc.
38
 
//
39
 
// Be aware that these APIs are being deprecated.
40
 
// The new API coming in early 2013.
41
 
//
42
 
// TODO work with the v3
43
 
// http://strava.github.com/api/v3/activities/streams/
44
 
 
45
 
StravaDownloadDialog::StravaDownloadDialog(MainWindow *mainWindow) :
46
 
    mainWindow(mainWindow)
47
 
{
48
 
    STRAVA_URL_V1 = "http://www.strava.com/api/v1/";
49
 
    STRAVA_URL_V2 = "http://www.strava.com/api/v2/";
50
 
    STRAVA_URL_V3 = "http://www.strava.com/api/v3/";
51
 
 
52
 
    setWindowTitle("Strava download");
53
 
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
54
 
 
55
 
    QGroupBox *groupBox = new QGroupBox(tr("Choose activityId to download: "));
56
 
 
57
 
    QHBoxLayout *hbox = new QHBoxLayout();
58
 
    activityIdEdit = new QLineEdit();
59
 
    hbox->addWidget(activityIdEdit);
60
 
 
61
 
    groupBox->setLayout(hbox);
62
 
    mainLayout->addWidget(groupBox);
63
 
 
64
 
    // show progress
65
 
    QVBoxLayout *progressLayout = new QVBoxLayout;
66
 
    progressBar = new QProgressBar(this);
67
 
    progressLabel = new QLabel("", this);
68
 
 
69
 
    progressLayout->addWidget(progressBar);
70
 
    progressLayout->addWidget(progressLabel);
71
 
 
72
 
    QHBoxLayout *buttonLayout = new QHBoxLayout;
73
 
 
74
 
    downloadButton = new QPushButton(tr("&Download Ride"), this);
75
 
    buttonLayout->addWidget(downloadButton);
76
 
 
77
 
    cancelButton = new QPushButton(tr("&Cancel"), this);
78
 
    buttonLayout->addStretch();
79
 
    buttonLayout->addWidget(cancelButton);
80
 
 
81
 
    mainLayout->addLayout(progressLayout);
82
 
    mainLayout->addLayout(buttonLayout);
83
 
 
84
 
    connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadRide()));
85
 
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
86
 
}
87
 
 
88
 
void
89
 
StravaDownloadDialog::downloadRide()
90
 
{
91
 
    activityId = activityIdEdit->text();
92
 
    if (activityId.trimmed().isEmpty()) {
93
 
        progressLabel->setText(tr("Enter an activity Id"));
94
 
        return;
95
 
    }
96
 
 
97
 
    if (activityId.toLong() == 0)  {
98
 
        progressLabel->setText(tr("Enter a valid activity Id"));
99
 
        return;
100
 
    }
101
 
 
102
 
    fileNames = new QStringList();
103
 
 
104
 
    progressBar->setValue(5);
105
 
    count = 1;
106
 
    requestRideDetail();
107
 
    //requestListRides();
108
 
}
109
 
 
110
 
void
111
 
StravaDownloadDialog::requestRideDetail()
112
 
{
113
 
    show();
114
 
 
115
 
    tmp = new QTemporaryFile(QDir(QDir::tempPath()).absoluteFilePath(".download."+activityId+".XXXXXX.strava"));
116
 
    //QString tmpl = mainWindow->home.absoluteFilePath(".download.XXXXXX."+activityId+".strava"); //
117
 
    //tmp.setFileTemplate(tmpl);
118
 
    tmp->setAutoRemove(true);
119
 
 
120
 
    if (!tmp->open()) {
121
 
       progressLabel->setText(tr("Failed to create temporary file ")
122
 
            + tmp->fileName() + ": " + tmp->error());
123
 
 
124
 
        return;
125
 
    }
126
 
    tmp->resize(0);
127
 
 
128
 
    progressLabel->setText(tr("Get activity %1 details").arg(activityId));
129
 
 
130
 
    QEventLoop eventLoop;
131
 
    QNetworkAccessManager networkMgr;
132
 
 
133
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestRideDetailFinished(QNetworkReply*)));
134
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
135
 
 
136
 
    QUrl url = QUrl(STRAVA_URL_V1 + "rides/"+activityId);
137
 
 
138
 
    QNetworkRequest request = QNetworkRequest(url);
139
 
 
140
 
    networkMgr.get(request);
141
 
    eventLoop.exec();
142
 
    progressBar->setValue(progressBar->value()+5/count);
143
 
}
144
 
 
145
 
void
146
 
StravaDownloadDialog::requestRideDetailFinished(QNetworkReply *reply)
147
 
{
148
 
    progressBar->setValue(15);
149
 
 
150
 
    if (reply->error() != QNetworkReply::NoError)
151
 
        progressLabel->setText(QString(tr("Error from ride details %1")).arg(reply->error()));
152
 
    else {
153
 
        QString response = reply->readLine();
154
 
 
155
 
        QScriptValue sc;
156
 
        QScriptEngine se;
157
 
 
158
 
        sc = se.evaluate("("+response+")");
159
 
        QString error = sc.property("error").toString();
160
 
        if (!error.isEmpty()) {
161
 
            progressLabel->setText(error);
162
 
            return;
163
 
        }
164
 
 
165
 
        tmp->write(response.toAscii());
166
 
        progressBar->setValue(progressBar->value()+10/count);
167
 
 
168
 
        requestDownloadRide();
169
 
    }
170
 
}
171
 
 
172
 
void
173
 
StravaDownloadDialog::requestDownloadRide()
174
 
{
175
 
    show();
176
 
 
177
 
    progressLabel->setText(tr("Download activity %1").arg(activityId));
178
 
 
179
 
    QEventLoop eventLoop;
180
 
    QNetworkAccessManager networkMgr;
181
 
 
182
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestDownloadRideFinished(QNetworkReply*)));
183
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
184
 
 
185
 
    QUrl url = QUrl(STRAVA_URL_V1 + "streams/"+activityId);
186
 
 
187
 
    QNetworkRequest request = QNetworkRequest(url);
188
 
 
189
 
    networkMgr.get(request);
190
 
    eventLoop.exec();
191
 
    progressBar->setValue(progressBar->value()+20/count);
192
 
}
193
 
 
194
 
void
195
 
StravaDownloadDialog::requestDownloadRideFinished(QNetworkReply *reply)
196
 
{
197
 
    progressBar->setValue(60);
198
 
 
199
 
    if (reply->error() != QNetworkReply::NoError)
200
 
        progressLabel->setText(QString(tr("Error from upload %1")).arg(reply->error()));
201
 
    else {
202
 
        QString response = reply->readLine();
203
 
 
204
 
        progressBar->setValue(70);
205
 
        tmp->seek(tmp->size()-2);
206
 
        tmp->write(",\"streams\":");
207
 
        tmp->write(response.toAscii());
208
 
        tmp->write("},\"api_version\":\"1\"}");
209
 
        tmp->close();
210
 
        progressBar->setValue(progressBar->value()+60/count);
211
 
 
212
 
        fileNames->append(tmp->fileName());
213
 
 
214
 
        if (fileNames->count() == count) {
215
 
            close();
216
 
            RideImportWizard *import = new RideImportWizard(*fileNames, mainWindow->home, mainWindow);
217
 
            import->process();
218
 
            progressBar->setValue(100);
219
 
        }
220
 
    }
221
 
}
222
 
 
223
 
void
224
 
StravaDownloadDialog::okClicked()
225
 
{
226
 
    dialog->accept();
227
 
    return;
228
 
}
229
 
 
230
 
void
231
 
StravaDownloadDialog::cancelClicked()
232
 
{
233
 
    dialog->reject();
234
 
    return;
235
 
}
236
 
 
237
 
 
238
 
void
239
 
StravaDownloadDialog::requestLogin()
240
 
{
241
 
    progressLabel->setText(tr("Login..."));
242
 
    progressBar->setValue(5);
243
 
 
244
 
    QString username = appsettings->cvalue(mainWindow->cyclist, GC_STRUSER).toString();
245
 
    QString password = appsettings->cvalue(mainWindow->cyclist, GC_STRPASS).toString();
246
 
 
247
 
    QNetworkAccessManager networkMgr;
248
 
    QEventLoop eventLoop;
249
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestLoginFinished(QNetworkReply*)));
250
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
251
 
 
252
 
    QByteArray data;
253
 
    /*data += "{\"email\": \"" + username + "\",";
254
 
    data += "\"password\": \"" + password + "\",";
255
 
    data += "\"agreed_to_terms\": \"true\"}";*/
256
 
 
257
 
    QUrl params;
258
 
 
259
 
    params.addQueryItem("email", username);
260
 
    params.addQueryItem("password",password);
261
 
    params.addQueryItem("agreed_to_terms", "true");
262
 
    data = params.encodedQuery();
263
 
 
264
 
    QUrl url = QUrl( STRAVA_URL_V2_SSL + "/authentication/login");
265
 
    QNetworkRequest request = QNetworkRequest(url);
266
 
    //request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
267
 
 
268
 
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
269
 
 
270
 
    networkMgr.post( request, data);
271
 
    eventLoop.exec();
272
 
}
273
 
 
274
 
void
275
 
StravaDownloadDialog::requestLoginFinished(QNetworkReply *reply)
276
 
{
277
 
    loggedIn = false;
278
 
 
279
 
 
280
 
    if (reply->error() == QNetworkReply::NoError)
281
 
    {
282
 
        QString response = reply->readLine();
283
 
 
284
 
        if(response.contains("token"))
285
 
        {
286
 
            QScriptValue sc;
287
 
            QScriptEngine se;
288
 
 
289
 
            sc = se.evaluate("("+response+")");
290
 
            token = sc.property("token").toString();
291
 
            athleteId = sc.property("athlete").property("id").toString();
292
 
 
293
 
            loggedIn = true;
294
 
        }
295
 
        else
296
 
        {
297
 
            token = "";
298
 
        }
299
 
    }
300
 
    else
301
 
    {
302
 
        token = "";
303
 
 
304
 
        #ifdef Q_OS_MACX
305
 
        #define GC_PREF tr("Golden Cheetah->Preferences")
306
 
        #else
307
 
        #define GC_PREF tr("Tools->Options")
308
 
        #endif
309
 
        QString advise = QString(tr("Please make sure to fill the Strava login info found under\n %1.")).arg(GC_PREF);
310
 
        QMessageBox err(QMessageBox::Critical, tr("Strava login Error"), advise);
311
 
        err.exec();
312
 
    }
313
 
}
314
 
 
315
 
 
316
 
void
317
 
StravaDownloadDialog::requestListRides()
318
 
{
319
 
    QString athleteId = "775290"; //Rica 131476
320
 
 
321
 
    progressBar->setValue(0);
322
 
    progressLabel->setText(tr("Download athlete"));
323
 
 
324
 
    QEventLoop eventLoop;
325
 
    QNetworkAccessManager networkMgr;
326
 
 
327
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestListRidesFinished(QNetworkReply*)));
328
 
    connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
329
 
 
330
 
    QUrl url = QUrl(STRAVA_URL_V1 + "rides?athleteId="+athleteId+"&offset=147");//+"&offset=0"
331
 
 
332
 
    QNetworkRequest request = QNetworkRequest(url);
333
 
 
334
 
    networkMgr.get(request);
335
 
    eventLoop.exec();
336
 
    progressBar->setValue(5);
337
 
}
338
 
 
339
 
void
340
 
StravaDownloadDialog::requestListRidesFinished(QNetworkReply *reply)
341
 
{
342
 
    progressBar->setValue(10);
343
 
 
344
 
    if (reply->error() != QNetworkReply::NoError)
345
 
        qDebug() << "Error from upload " <<reply->error();
346
 
    else {
347
 
        QString response = reply->readLine();
348
 
 
349
 
        QScriptValue sc;
350
 
        QScriptEngine se;
351
 
 
352
 
        sc = se.evaluate("("+response+")");
353
 
        count = sc.property("rides").property("length").toInteger();
354
 
 
355
 
        //count = 10;
356
 
        for(int i = 0; i < count; i++) {
357
 
            activityId = sc.property("rides").property(i).property("id").toString();
358
 
 
359
 
            requestRideDetail();
360
 
        }
361
 
 
362
 
 
363
 
    }
364
 
}
365
 
 
366