mirror of
https://github.com/GoldenCheetah/GoldenCheetah.git
synced 2026-02-13 16:18:42 +00:00
ShareDialog 2/3 : For version 3.0.1
This commit is contained in:
@@ -482,7 +482,7 @@ MainWindow::MainWindow(const QDir &home)
|
||||
connect(tweetAction, SIGNAL(triggered(bool)), this, SLOT(tweetRide()));
|
||||
rideMenu->addAction(tweetAction);
|
||||
|
||||
shareAction = new QAction(tr("Share..."), this);
|
||||
shareAction = new QAction(tr("Share (Strava, RideWithGPS)..."), this);
|
||||
connect(shareAction, SIGNAL(triggered(bool)), this, SLOT(share()));
|
||||
rideMenu->addAction(shareAction);
|
||||
#endif
|
||||
|
||||
@@ -42,8 +42,10 @@ ShareDialog::ShareDialog(Context *context, RideItem *item) :
|
||||
setMinimumHeight(400);
|
||||
|
||||
ride = item;
|
||||
stravaUploadId = ride->ride()->getTag("Strava uploadId", "");
|
||||
stravaActivityId = ride->ride()->getTag("Strava actityId", "");
|
||||
|
||||
// uploaders
|
||||
stravaUploader = new StravaUploader(context, ride, this);
|
||||
rideWithGpsUploader = new RideWithGpsUploader(context, ride, this);
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
||||
QGroupBox *groupBox1 = new QGroupBox(tr("Choose which sites you wish to share on: "));
|
||||
@@ -103,9 +105,11 @@ ShareDialog::ShareDialog(Context *context, RideItem *item) :
|
||||
QVBoxLayout *progressLayout = new QVBoxLayout;
|
||||
progressBar = new QProgressBar(this);
|
||||
progressLabel = new QLabel("", this);
|
||||
errorLabel = new QLabel("", this);
|
||||
|
||||
progressLayout->addWidget(progressBar);
|
||||
progressLayout->addWidget(progressLabel);
|
||||
progressLayout->addWidget(errorLabel);
|
||||
|
||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
||||
|
||||
@@ -120,12 +124,6 @@ ShareDialog::ShareDialog(Context *context, RideItem *item) :
|
||||
|
||||
connect(uploadButton, SIGNAL(clicked()), this, SLOT(upload()));
|
||||
connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
|
||||
|
||||
//connect(searchActivityButton, SIGNAL(clicked()), this, SLOT(getActivityFromStrava()));
|
||||
//connect(getActivityButton, SIGNAL(clicked()), this, SLOT(reject()));
|
||||
//connect(workoutTimeChk, SIGNAL(stateChanged(int)), this, SLOT(onCheck(int)));
|
||||
//connect(timeRidingChk, SIGNAL(stateChanged(int)), this, SLOT(onCheck(int)));
|
||||
//connect(totalDistanceChk, SIGNAL(stateChanged(int)), this, SLOT(onCheck(int)));
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +131,6 @@ void
|
||||
ShareDialog::upload()
|
||||
{
|
||||
show();
|
||||
overwrite = true;
|
||||
|
||||
if (!stravaChk->isChecked() && !rideWithGPSChk->isChecked()) {
|
||||
QMessageBox aMsgBox;
|
||||
@@ -142,17 +139,37 @@ ShareDialog::upload()
|
||||
return;
|
||||
}
|
||||
|
||||
shareSiteCount = 0;
|
||||
progressBar->setValue(0);
|
||||
progressLabel->setText("");
|
||||
errorLabel->setText("");
|
||||
|
||||
if (stravaChk->isChecked()) {
|
||||
uploadStrava();
|
||||
shareSiteCount ++;
|
||||
}
|
||||
|
||||
if (rideWithGPSChk->isChecked()) {
|
||||
uploadRideWithGPS();
|
||||
shareSiteCount ++;
|
||||
}
|
||||
|
||||
if (stravaChk->isChecked()) {
|
||||
stravaUploader->uploadStrava();
|
||||
}
|
||||
|
||||
if (rideWithGPSChk->isChecked()) {
|
||||
rideWithGpsUploader->uploadRideWithGPS();
|
||||
}
|
||||
}
|
||||
|
||||
StravaUploader::StravaUploader(Context *context, RideItem *ride, ShareDialog *parent) :
|
||||
context(context), ride(ride), parent(parent)
|
||||
{
|
||||
//stravaActivityId = ride->ride()->getTag("Strava uploadId");
|
||||
stravaUploadId = ride->ride()->getTag("Strava uploadId", "");
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::uploadStrava()
|
||||
StravaUploader::uploadStrava()
|
||||
{
|
||||
// OAuth no more login
|
||||
token = appsettings->cvalue(context->athlete->cyclist, GC_STRAVA_TOKEN, "").toString();
|
||||
@@ -165,7 +182,7 @@ ShareDialog::uploadStrava()
|
||||
}
|
||||
|
||||
// allready shared ?
|
||||
if(stravaActivityId.length()>0)
|
||||
if(stravaUploadId.length()>0)
|
||||
{
|
||||
overwrite = false;
|
||||
|
||||
@@ -177,15 +194,15 @@ ShareDialog::uploadStrava()
|
||||
label->setText(tr("This Ride is marked as already on Strava. Are you sure you want to upload it?"));
|
||||
layoutLabel->addWidget(label);
|
||||
|
||||
QPushButton *ok = new QPushButton(tr("OK"), this);
|
||||
QPushButton *cancel = new QPushButton(tr("Cancel"), this);
|
||||
QPushButton *ok = new QPushButton(tr("OK"), dialog);
|
||||
QPushButton *cancel = new QPushButton(tr("Cancel"), dialog);
|
||||
QHBoxLayout *buttons = new QHBoxLayout();
|
||||
buttons->addStretch();
|
||||
buttons->addWidget(cancel);
|
||||
buttons->addWidget(ok);
|
||||
|
||||
connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
|
||||
connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
|
||||
connect(cancel, SIGNAL(clicked()), this, SLOT(closeClicked()));
|
||||
|
||||
layout->addLayout(layoutLabel);
|
||||
layout->addLayout(buttons);
|
||||
@@ -199,19 +216,231 @@ ShareDialog::uploadStrava()
|
||||
|
||||
if(!uploadSuccessful)
|
||||
{
|
||||
progressLabel->setText("Error uploading to Strava");
|
||||
parent->progressLabel->setText("Error uploading to Strava");
|
||||
}
|
||||
else
|
||||
{
|
||||
//requestVerifyUpload();
|
||||
progressLabel->setText(tr("Successfully uploaded to Strava\n")+uploadStatus);
|
||||
parent->progressLabel->setText(tr("Successfully uploaded to Strava\n"));
|
||||
}
|
||||
}
|
||||
|
||||
// Documentation is at:
|
||||
// https://strava.pbworks.com/w/page/39241255/v2%20upload%20create
|
||||
void
|
||||
StravaUploader::requestUploadStrava()
|
||||
{
|
||||
parent->progressLabel->setText(tr("Upload ride to Strava..."));
|
||||
parent->progressBar->setValue(parent->progressBar->value()+10/parent->shareSiteCount);
|
||||
|
||||
QEventLoop eventLoop;
|
||||
QNetworkAccessManager networkMgr;
|
||||
|
||||
int year = ride->fileName.left(4).toInt();
|
||||
int month = ride->fileName.mid(5,2).toInt();
|
||||
int day = ride->fileName.mid(8,2).toInt();
|
||||
int hour = ride->fileName.mid(11,2).toInt();
|
||||
int minute = ride->fileName.mid(14,2).toInt();;
|
||||
int second = ride->fileName.mid(17,2).toInt();;
|
||||
|
||||
QDate rideDate = QDate(year, month, day);
|
||||
QTime rideTime = QTime(hour, minute, second);
|
||||
QDateTime rideDateTime = QDateTime(rideDate, rideTime);
|
||||
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestUploadStravaFinished(QNetworkReply*)));
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
|
||||
|
||||
TcxFileReader reader;
|
||||
|
||||
STRAVA_URL_SSL = "https://www.strava.com/api/v3/uploads" ; // The V3 API doc said "https://api.strava.com" but it is not working yet
|
||||
|
||||
QUrl url = QUrl( STRAVA_URL_SSL );
|
||||
QNetworkRequest request = QNetworkRequest(url);
|
||||
|
||||
//QString boundary = QString::number(qrand() * (90000000000) / (RAND_MAX + 1) + 10000000000, 16);
|
||||
QString boundary = QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString();
|
||||
|
||||
QByteArray file = reader.toByteArray(context, ride->ride(), parent->altitudeChk->isChecked(), parent->powerChk->isChecked(), parent->heartrateChk->isChecked(), parent->cadenceChk->isChecked());
|
||||
|
||||
// MULTIPART *****************
|
||||
|
||||
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||
multiPart->setBoundary(boundary.toAscii());
|
||||
|
||||
QHttpPart accessTokenPart;
|
||||
accessTokenPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"access_token\""));
|
||||
accessTokenPart.setBody(token.toAscii());
|
||||
|
||||
QHttpPart activityTypePart;
|
||||
activityTypePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"activity_type\""));
|
||||
activityTypePart.setBody("ride");
|
||||
|
||||
QHttpPart activityNamePart;
|
||||
activityNamePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"activity_name\""));
|
||||
activityNamePart.setBody(QString(parent->titleEdit->text()).toAscii());
|
||||
|
||||
QHttpPart dataTypePart;
|
||||
dataTypePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data_type\""));
|
||||
dataTypePart.setBody("tcx");
|
||||
|
||||
QHttpPart externalIdPart;
|
||||
externalIdPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"external_id\""));
|
||||
externalIdPart.setBody("Ride");
|
||||
|
||||
QHttpPart privatePart;
|
||||
privatePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"private\""));
|
||||
privatePart.setBody("TRUE");
|
||||
|
||||
QHttpPart filePart;
|
||||
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml"));
|
||||
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"file.tcx\"; type=\"text/xml\""));
|
||||
filePart.setBody(file);
|
||||
|
||||
|
||||
multiPart->append(accessTokenPart);
|
||||
multiPart->append(activityTypePart);
|
||||
multiPart->append(activityNamePart);
|
||||
multiPart->append(dataTypePart);
|
||||
multiPart->append(externalIdPart);
|
||||
multiPart->append(privatePart);
|
||||
multiPart->append(filePart);
|
||||
|
||||
QScopedPointer<QNetworkReply> reply( networkMgr.post(request, multiPart) );
|
||||
multiPart->setParent(reply.data());
|
||||
|
||||
parent->progressBar->setValue(parent->progressBar->value()+30/parent->shareSiteCount);
|
||||
parent->progressLabel->setText(tr("Upload ride... Sending to Strava"));
|
||||
|
||||
eventLoop.exec();
|
||||
}
|
||||
|
||||
void
|
||||
StravaUploader::requestUploadStravaFinished(QNetworkReply *reply)
|
||||
{
|
||||
parent->progressBar->setValue(parent->progressBar->value()+50/parent->shareSiteCount);
|
||||
parent->progressLabel->setText(tr("Upload to Strava finished."));
|
||||
|
||||
uploadSuccessful = false;
|
||||
|
||||
QString response = reply->readLine();
|
||||
//qDebug() << response;
|
||||
|
||||
QScriptValue sc;
|
||||
QScriptEngine se;
|
||||
|
||||
sc = se.evaluate("("+response+")");
|
||||
QString uploadError = sc.property("error").toString();
|
||||
|
||||
if (uploadError.length()>0 || reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
//qDebug() << "Error " << reply->error() ;
|
||||
//qDebug() << "Error " << uploadError;
|
||||
parent->errorLabel->setText(parent->errorLabel->text()+ tr(" Error from Strava: ") + uploadError + "\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
stravaUploadId = sc.property("upload_id").toString();
|
||||
|
||||
ride->ride()->setTag("Strava uploadId", stravaUploadId);
|
||||
ride->setDirty(true);
|
||||
|
||||
//qDebug() << "uploadId: " << stravaUploadId;
|
||||
|
||||
parent->progressBar->setValue(parent->progressBar->value()+10/parent->shareSiteCount);
|
||||
uploadSuccessful = true;
|
||||
}
|
||||
//qDebug() << reply->readAll();
|
||||
}
|
||||
|
||||
void
|
||||
StravaUploader::requestVerifyUpload()
|
||||
{
|
||||
parent->progressBar->setValue(0);
|
||||
parent->progressLabel->setText(tr("Ride processing..."));
|
||||
|
||||
QEventLoop eventLoop;
|
||||
QNetworkAccessManager networkMgr;
|
||||
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestVerifyUploadFinished(QNetworkReply*)));
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
|
||||
QByteArray out;
|
||||
|
||||
QUrl url = QUrl(STRAVA_URL_SSL + "/upload/status/"+stravaUploadId+"?token="+token);
|
||||
QNetworkRequest request = QNetworkRequest(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
|
||||
networkMgr.post( request, out);
|
||||
eventLoop.exec();
|
||||
}
|
||||
|
||||
void
|
||||
StravaUploader::requestVerifyUploadFinished(QNetworkReply *reply)
|
||||
{
|
||||
uploadSuccessful = false;
|
||||
if (reply->error() != QNetworkReply::NoError)
|
||||
qDebug() << "Error from upload " <<reply->error();
|
||||
else {
|
||||
QString response = reply->readLine();
|
||||
|
||||
//qDebug() << response;
|
||||
|
||||
QScriptValue sc;
|
||||
QScriptEngine se;
|
||||
|
||||
sc = se.evaluate("("+response+")");
|
||||
uploadProgress = sc.property("upload_progress").toString();
|
||||
|
||||
//qDebug() << "upload_progress: " << uploadProgress;
|
||||
parent->progressBar->setValue(uploadProgress.toInt());
|
||||
|
||||
stravaActivityId = sc.property("activity_id").toString();
|
||||
|
||||
if (stravaActivityId.length() == 0) {
|
||||
requestVerifyUpload();
|
||||
return;
|
||||
}
|
||||
|
||||
ride->ride()->setTag("Strava actityId", stravaActivityId);
|
||||
ride->setDirty(true);
|
||||
|
||||
sc = se.evaluate("("+response+")");
|
||||
uploadStatus = sc.property("upload_status").toString();
|
||||
|
||||
//qDebug() << "upload_status: " << uploadStatus;
|
||||
parent->progressLabel->setText(uploadStatus);
|
||||
|
||||
if (uploadProgress.toInt() < 97) {
|
||||
requestVerifyUpload();
|
||||
return;
|
||||
}
|
||||
uploadSuccessful = true;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::uploadRideWithGPS()
|
||||
StravaUploader::okClicked()
|
||||
{
|
||||
if(stravaActivityId.length()>0)
|
||||
dialog->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
StravaUploader::closeClicked()
|
||||
{
|
||||
dialog->reject();
|
||||
return;
|
||||
}
|
||||
|
||||
RideWithGpsUploader::RideWithGpsUploader(Context *context, RideItem *ride, ShareDialog *parent) :
|
||||
context(context), ride(ride), parent(parent)
|
||||
{
|
||||
rideWithGpsActivityId = ride->ride()->getTag("RideWithGPS tripid", "");
|
||||
}
|
||||
|
||||
void
|
||||
RideWithGpsUploader::uploadRideWithGPS()
|
||||
{
|
||||
if(rideWithGpsActivityId.length()>0)
|
||||
{
|
||||
overwrite = false;
|
||||
|
||||
@@ -223,15 +452,15 @@ ShareDialog::uploadRideWithGPS()
|
||||
label->setText(tr("This Ride is marked as already on RideWithGPS. Are you sure you want to upload it?"));
|
||||
layoutLabel->addWidget(label);
|
||||
|
||||
QPushButton *ok = new QPushButton(tr("OK"), this);
|
||||
QPushButton *cancel = new QPushButton(tr("Cancel"), this);
|
||||
QPushButton *ok = new QPushButton(tr("OK"), dialog);
|
||||
QPushButton *cancel = new QPushButton(tr("Cancel"), dialog);
|
||||
QHBoxLayout *buttons = new QHBoxLayout();
|
||||
buttons->addStretch();
|
||||
buttons->addWidget(cancel);
|
||||
buttons->addWidget(ok);
|
||||
|
||||
connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
|
||||
connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
|
||||
connect(cancel, SIGNAL(clicked()), this, SLOT(closeClicked()));
|
||||
|
||||
layout->addLayout(layoutLabel);
|
||||
layout->addLayout(buttons);
|
||||
@@ -245,19 +474,19 @@ ShareDialog::uploadRideWithGPS()
|
||||
|
||||
if(!uploadSuccessful)
|
||||
{
|
||||
progressLabel->setText(tr("Error uploading to RideWithGPS")+"\n");
|
||||
parent->progressLabel->setText(tr("Error uploading to RideWithGPS")+"\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
progressLabel->setText(tr("Successfully uploaded to RideWithGPS"));
|
||||
parent->progressLabel->setText(tr("Successfully uploaded to RideWithGPS"));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::requestUploadRideWithGPS()
|
||||
RideWithGpsUploader::requestUploadRideWithGPS()
|
||||
{
|
||||
progressLabel->setText(tr("Upload ride..."));
|
||||
progressBar->setValue(10);
|
||||
parent->progressLabel->setText(tr("Upload ride..."));
|
||||
parent->progressBar->setValue(parent->progressBar->value()+10/parent->shareSiteCount);
|
||||
|
||||
QEventLoop eventLoop;
|
||||
QNetworkAccessManager networkMgr;
|
||||
@@ -313,19 +542,19 @@ ShareDialog::requestUploadRideWithGPS()
|
||||
data += ", \"t\": ";
|
||||
data += QString("%1").arg(rideDateTime.toTime_t());
|
||||
|
||||
if (altitudeChk->isChecked()) {
|
||||
if (parent->altitudeChk->isChecked()) {
|
||||
data += ", \"e\": ";
|
||||
data += QString("%1").arg(point->alt);
|
||||
}
|
||||
if (powerChk->isChecked()) {
|
||||
if (parent->powerChk->isChecked()) {
|
||||
data += ", \"p\": ";
|
||||
data += QString("%1").arg(point->watts);
|
||||
}
|
||||
if (cadenceChk->isChecked()) {
|
||||
if (parent->cadenceChk->isChecked()) {
|
||||
data += ", \"c\": ";
|
||||
data += QString("%1").arg(point->cad);
|
||||
}
|
||||
if (heartrateChk->isChecked()) {
|
||||
if (parent->heartrateChk->isChecked()) {
|
||||
data += ", \"h\": ";
|
||||
data += QString("%1").arg(point->hr);
|
||||
}
|
||||
@@ -343,173 +572,22 @@ ShareDialog::requestUploadRideWithGPS()
|
||||
QNetworkRequest request = QNetworkRequest(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
|
||||
progressBar->setValue(40);
|
||||
progressLabel->setText(tr("Upload ride... Sending"));
|
||||
parent->progressBar->setValue(parent->progressBar->value()+30/parent->shareSiteCount);
|
||||
parent->progressLabel->setText(tr("Upload ride... Sending to RideWithGPS"));
|
||||
|
||||
networkMgr.post( request, out.toAscii());
|
||||
eventLoop.exec();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ShareDialog::okClicked()
|
||||
RideWithGpsUploader::requestUploadRideWithGPSFinished(QNetworkReply *reply)
|
||||
{
|
||||
dialog->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::closeClicked()
|
||||
{
|
||||
dialog->reject();
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::sslErrorHandler(QNetworkReply* qnr, const QList<QSslError> & errlist)
|
||||
{
|
||||
qDebug() << "sslErrorHandler: ";
|
||||
// show list of all ssl errors
|
||||
foreach (QSslError err, errlist)
|
||||
qDebug() << "ssl error: " << err;
|
||||
|
||||
qnr->ignoreSslErrors();
|
||||
}
|
||||
|
||||
// Documentation is at:
|
||||
// https://strava.pbworks.com/w/page/39241255/v2%20upload%20create
|
||||
|
||||
void
|
||||
ShareDialog::requestUploadStrava()
|
||||
{
|
||||
progressLabel->setText(tr("Upload ride to Strava..."));
|
||||
progressBar->setValue(10);
|
||||
|
||||
QEventLoop eventLoop;
|
||||
QNetworkAccessManager networkMgr;
|
||||
|
||||
int year = ride->fileName.left(4).toInt();
|
||||
int month = ride->fileName.mid(5,2).toInt();
|
||||
int day = ride->fileName.mid(8,2).toInt();
|
||||
int hour = ride->fileName.mid(11,2).toInt();
|
||||
int minute = ride->fileName.mid(14,2).toInt();;
|
||||
int second = ride->fileName.mid(17,2).toInt();;
|
||||
|
||||
QDate rideDate = QDate(year, month, day);
|
||||
QTime rideTime = QTime(hour, minute, second);
|
||||
QDateTime rideDateTime = QDateTime(rideDate, rideTime);
|
||||
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestUploadStravaFinished(QNetworkReply*)));
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
|
||||
connect(&networkMgr, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )), this, SLOT(sslErrorHandler(QNetworkReply*, const QList<QSslError> & )));
|
||||
|
||||
|
||||
TcxFileReader reader;
|
||||
|
||||
STRAVA_URL_SSL = "https://www.strava.com/api/v3/uploads" ; // The V3 API doc said "https://api.strava.com" but it is not working yet
|
||||
|
||||
QUrl url = QUrl( STRAVA_URL_SSL );
|
||||
QNetworkRequest request = QNetworkRequest(url);
|
||||
|
||||
QString boundary = QString::number(qrand() * (90000000000) / (RAND_MAX + 1) + 10000000000, 16);
|
||||
|
||||
QByteArray file = reader.toByteArray(context, ride->ride(), altitudeChk->isChecked(), powerChk->isChecked(), heartrateChk->isChecked(), cadenceChk->isChecked());
|
||||
|
||||
// MULTIPART *****************
|
||||
|
||||
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||
multiPart->setBoundary(boundary.toAscii());
|
||||
|
||||
QHttpPart accessTokenPart;
|
||||
accessTokenPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"access_token\""));
|
||||
accessTokenPart.setBody(token.toAscii());
|
||||
|
||||
QHttpPart activityTypePart;
|
||||
activityTypePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"activity_type\""));
|
||||
activityTypePart.setBody("Ride");
|
||||
|
||||
QHttpPart activityNamePart;
|
||||
activityNamePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"activity_name\""));
|
||||
activityNamePart.setBody(QString(titleEdit->text() + " #GoldenCheetah").toAscii());
|
||||
|
||||
QHttpPart dataTypePart;
|
||||
dataTypePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data_type\""));
|
||||
dataTypePart.setBody("tcx");
|
||||
|
||||
QHttpPart externalIdPart;
|
||||
externalIdPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"external_id\""));
|
||||
externalIdPart.setBody("test1");
|
||||
|
||||
QHttpPart privatePart;
|
||||
privatePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"private\""));
|
||||
privatePart.setBody("TRUE");
|
||||
|
||||
QHttpPart filePart;
|
||||
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml"));
|
||||
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"file.tcx\"; type=\"text/xml\""));
|
||||
filePart.setBody(file);
|
||||
|
||||
|
||||
multiPart->append(accessTokenPart);
|
||||
multiPart->append(activityTypePart);
|
||||
multiPart->append(activityNamePart);
|
||||
multiPart->append(dataTypePart);
|
||||
multiPart->append(externalIdPart);
|
||||
multiPart->append(privatePart);
|
||||
multiPart->append(filePart);
|
||||
|
||||
QScopedPointer<QNetworkReply> reply( networkMgr.post(request, multiPart) );
|
||||
multiPart->setParent(reply.data());
|
||||
|
||||
progressBar->setValue(40);
|
||||
progressLabel->setText(tr("Upload ride... Sending"));
|
||||
|
||||
|
||||
|
||||
eventLoop.exec();
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::requestUploadStravaFinished(QNetworkReply *reply)
|
||||
{
|
||||
progressBar->setValue(90);
|
||||
progressLabel->setText(tr("Upload to Strava finished."));
|
||||
|
||||
uploadSuccessful = false;
|
||||
if (reply->error() != QNetworkReply::NoError)
|
||||
qDebug() << "Error from upload " <<reply->error();
|
||||
else
|
||||
{
|
||||
QString response = reply->readLine();
|
||||
|
||||
qDebug() << response;
|
||||
|
||||
QScriptValue sc;
|
||||
QScriptEngine se;
|
||||
|
||||
sc = se.evaluate("("+response+")");
|
||||
stravaUploadId = sc.property("upload_id").toString();
|
||||
|
||||
ride->ride()->setTag("Strava uploadId", stravaUploadId);
|
||||
ride->setDirty(true);
|
||||
|
||||
qDebug() << "uploadId: " << stravaUploadId;
|
||||
|
||||
progressBar->setValue(100);
|
||||
uploadSuccessful = true;
|
||||
}
|
||||
qDebug() << reply->readAll();
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::requestUploadRideWithGPSFinished(QNetworkReply *reply)
|
||||
{
|
||||
progressBar->setValue(90);
|
||||
progressLabel->setText(tr("Upload to RideWithGPS finished."));
|
||||
parent->progressBar->setValue(parent->progressBar->value()+50/parent->shareSiteCount);
|
||||
parent->progressLabel->setText(tr("Upload to RideWithGPS finished."));
|
||||
|
||||
uploadSuccessful = false;
|
||||
|
||||
QString response = reply->readLine();
|
||||
QString response = reply->readAll();
|
||||
//qDebug() << response;
|
||||
|
||||
QScriptValue sc;
|
||||
@@ -519,7 +597,9 @@ ShareDialog::requestUploadRideWithGPSFinished(QNetworkReply *reply)
|
||||
QString uploadError = sc.property("error").toString();
|
||||
|
||||
if (uploadError.length()>0 || reply->error() != QNetworkReply::NoError) {
|
||||
//qDebug() << "Error from upload " <<reply->error();
|
||||
//qDebug() << "Error " << reply->error();
|
||||
//qDebug() << "Error; " << uploadError;
|
||||
parent->errorLabel->setText(parent->errorLabel->text()+ tr(" Error from RideWithGPS: ") + uploadError + "\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -530,72 +610,24 @@ ShareDialog::requestUploadRideWithGPSFinished(QNetworkReply *reply)
|
||||
|
||||
//qDebug() << "tripid: " << tripid;
|
||||
|
||||
progressBar->setValue(100);
|
||||
parent->progressBar->setValue(parent->progressBar->value()+10/parent->shareSiteCount);
|
||||
uploadSuccessful = true;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::requestVerifyUpload()
|
||||
RideWithGpsUploader::okClicked()
|
||||
{
|
||||
progressBar->setValue(0);
|
||||
progressLabel->setText(tr("Ride processing..."));
|
||||
|
||||
QEventLoop eventLoop;
|
||||
QNetworkAccessManager networkMgr;
|
||||
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestVerifyUploadFinished(QNetworkReply*)));
|
||||
connect(&networkMgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
|
||||
QByteArray out;
|
||||
|
||||
QUrl url = QUrl(STRAVA_URL_SSL + "/upload/status/"+stravaUploadId+"?token="+token);
|
||||
QNetworkRequest request = QNetworkRequest(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
|
||||
networkMgr.post( request, out);
|
||||
eventLoop.exec();
|
||||
dialog->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
ShareDialog::requestVerifyUploadFinished(QNetworkReply *reply)
|
||||
RideWithGpsUploader::closeClicked()
|
||||
{
|
||||
uploadSuccessful = false;
|
||||
if (reply->error() != QNetworkReply::NoError)
|
||||
qDebug() << "Error from upload " <<reply->error();
|
||||
else {
|
||||
QString response = reply->readLine();
|
||||
|
||||
//qDebug() << response;
|
||||
|
||||
QScriptValue sc;
|
||||
QScriptEngine se;
|
||||
|
||||
sc = se.evaluate("("+response+")");
|
||||
uploadProgress = sc.property("upload_progress").toString();
|
||||
|
||||
//qDebug() << "upload_progress: " << uploadProgress;
|
||||
progressBar->setValue(uploadProgress.toInt());
|
||||
|
||||
stravaActivityId = sc.property("activity_id").toString();
|
||||
|
||||
if (stravaActivityId.length() == 0) {
|
||||
requestVerifyUpload();
|
||||
return;
|
||||
}
|
||||
|
||||
ride->ride()->setTag("Strava actityId", stravaActivityId);
|
||||
ride->setDirty(true);
|
||||
|
||||
sc = se.evaluate("("+response+")");
|
||||
uploadStatus = sc.property("upload_status").toString();
|
||||
|
||||
//qDebug() << "upload_status: " << uploadStatus;
|
||||
progressLabel->setText(uploadStatus);
|
||||
|
||||
if (uploadProgress.toInt() < 97) {
|
||||
requestVerifyUpload();
|
||||
return;
|
||||
}
|
||||
uploadSuccessful = true;
|
||||
}
|
||||
dialog->reject();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -33,71 +33,121 @@ extern "C" {
|
||||
}
|
||||
#endif
|
||||
|
||||
class ShareDialog;
|
||||
|
||||
// uploader to strava.com
|
||||
class StravaUploader : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
G_OBJECT
|
||||
|
||||
public:
|
||||
StravaUploader(Context *context, RideItem *item, ShareDialog *parent = 0);
|
||||
|
||||
void uploadStrava();
|
||||
|
||||
private slots:
|
||||
void requestUploadStrava();
|
||||
void requestUploadStravaFinished(QNetworkReply *reply);
|
||||
|
||||
void requestVerifyUpload();
|
||||
void requestVerifyUploadFinished(QNetworkReply *reply);
|
||||
|
||||
void okClicked();
|
||||
void closeClicked();
|
||||
|
||||
private:
|
||||
Context *context;
|
||||
ShareDialog *parent;
|
||||
RideItem *ride;
|
||||
QDialog *dialog;
|
||||
|
||||
QString token;
|
||||
|
||||
QString STRAVA_URL_SSL;
|
||||
QString stravaUploadId, stravaActivityId;
|
||||
|
||||
bool loggedIn, uploadSuccessful;
|
||||
bool overwrite;
|
||||
|
||||
QString uploadStatus;
|
||||
QString uploadProgress;
|
||||
};
|
||||
|
||||
// uploader to ridewithgps.com
|
||||
class RideWithGpsUploader : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
G_OBJECT
|
||||
|
||||
public:
|
||||
RideWithGpsUploader(Context *context, RideItem *item, ShareDialog *parent = 0);
|
||||
|
||||
void uploadRideWithGPS();
|
||||
|
||||
private slots:
|
||||
|
||||
void requestUploadRideWithGPS();
|
||||
void requestUploadRideWithGPSFinished(QNetworkReply *reply);
|
||||
|
||||
void okClicked();
|
||||
void closeClicked();
|
||||
|
||||
private:
|
||||
Context *context;
|
||||
ShareDialog *parent;
|
||||
RideItem *ride;
|
||||
QDialog *dialog;
|
||||
|
||||
QString rideWithGpsActivityId;
|
||||
|
||||
bool loggedIn, uploadSuccessful;
|
||||
bool overwrite;
|
||||
};
|
||||
|
||||
class ShareDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
G_OBJECT
|
||||
|
||||
public:
|
||||
ShareDialog(Context *context, RideItem *item);
|
||||
ShareDialog(Context *context, RideItem *item);
|
||||
|
||||
QProgressBar *progressBar;
|
||||
QLabel *progressLabel, *errorLabel;
|
||||
|
||||
QLineEdit *titleEdit;
|
||||
|
||||
//QCheckBox *gpsChk;
|
||||
QCheckBox *altitudeChk;
|
||||
QCheckBox *powerChk;
|
||||
QCheckBox *cadenceChk;
|
||||
QCheckBox *heartrateChk;
|
||||
|
||||
int shareSiteCount;
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
void upload();
|
||||
|
||||
private slots:
|
||||
void uploadStrava();
|
||||
void uploadRideWithGPS();
|
||||
|
||||
void sslErrorHandler(QNetworkReply* qnr, const QList<QSslError> & errlist);
|
||||
|
||||
void requestUploadStrava();
|
||||
void requestUploadStravaFinished(QNetworkReply *reply);
|
||||
|
||||
void requestUploadRideWithGPS();
|
||||
void requestUploadRideWithGPSFinished(QNetworkReply *reply);
|
||||
|
||||
void requestVerifyUpload();
|
||||
void requestVerifyUploadFinished(QNetworkReply *reply);
|
||||
|
||||
void okClicked();
|
||||
void closeClicked();
|
||||
|
||||
private:
|
||||
Context *context;
|
||||
QDialog *dialog;
|
||||
|
||||
QPushButton *uploadButton;
|
||||
QPushButton *searchActivityButton;
|
||||
QPushButton *getActivityButton;
|
||||
QPushButton *closeButton;
|
||||
|
||||
QLineEdit *titleEdit;
|
||||
|
||||
QCheckBox *stravaChk;
|
||||
QCheckBox *rideWithGPSChk;
|
||||
|
||||
//QCheckBox *gpsChk;
|
||||
QCheckBox *altitudeChk;
|
||||
QCheckBox *powerChk;
|
||||
QCheckBox *cadenceChk;
|
||||
QCheckBox *heartrateChk;
|
||||
|
||||
QProgressBar *progressBar;
|
||||
QLabel *progressLabel;
|
||||
|
||||
RideItem *ride;
|
||||
|
||||
StravaUploader *stravaUploader;
|
||||
RideWithGpsUploader *rideWithGpsUploader;
|
||||
|
||||
QString athleteId;
|
||||
QString token;
|
||||
QString stravaUploadId;
|
||||
QString stravaActivityId;
|
||||
QString uploadStatus;
|
||||
QString uploadProgress;
|
||||
|
||||
QString STRAVA_URL_SSL;
|
||||
|
||||
bool overwrite, loggedIn, uploadSuccessful;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // SHAREDIALOGDIALOG_H
|
||||
|
||||
Reference in New Issue
Block a user