2019-11-27 08:48:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "uploadDiveLogsDE.h"
|
|
|
|
#include <QDir>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <zip.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "core/display.h"
|
|
|
|
#include "core/errorhelper.h"
|
|
|
|
#include "core/qthelper.h"
|
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/membuffer.h"
|
|
|
|
#include "core/divesite.h"
|
|
|
|
#include "core/cloudstorage.h"
|
2021-07-17 07:21:26 +00:00
|
|
|
#include "core/xmlparams.h"
|
2019-12-09 07:28:09 +00:00
|
|
|
#ifndef SUBSURFACE_MOBILE
|
|
|
|
#include "core/selection.h"
|
|
|
|
#endif // SUBSURFACE_MOBILE
|
2019-11-27 08:48:58 +00:00
|
|
|
#include "core/settings/qPrefCloudStorage.h"
|
|
|
|
|
|
|
|
|
|
|
|
uploadDiveLogsDE *uploadDiveLogsDE::instance()
|
|
|
|
{
|
|
|
|
static uploadDiveLogsDE *self = new uploadDiveLogsDE;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uploadDiveLogsDE::uploadDiveLogsDE():
|
|
|
|
reply(NULL),
|
|
|
|
multipart(NULL)
|
|
|
|
{
|
|
|
|
timeout.setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void uploadDiveLogsDE::doUpload(bool selected, const QString &userid, const QString &password)
|
|
|
|
{
|
|
|
|
QString err;
|
|
|
|
|
2019-12-10 08:39:45 +00:00
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
/* generate a temporary filename and create/open that file with zip_open */
|
|
|
|
QString filename(QDir::tempPath() + "/divelogsde-upload.dld");
|
|
|
|
|
|
|
|
// delete file if it exist
|
2019-12-10 08:39:45 +00:00
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
QFile f(filename);
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
f.close();
|
|
|
|
f.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make zip file, with all dives, in divelogs.de format
|
2019-12-09 07:28:09 +00:00
|
|
|
if (!prepareDives(filename, selected)) {
|
2019-12-10 08:39:45 +00:00
|
|
|
emit uploadFinish(false, tr("Cannot prepare dives, none selected?"));
|
2019-11-27 08:48:58 +00:00
|
|
|
timeout.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// And upload it
|
|
|
|
uploadDives(filename, userid, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-06 21:56:10 +00:00
|
|
|
bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
|
2019-11-27 08:48:58 +00:00
|
|
|
{
|
2019-12-09 07:28:09 +00:00
|
|
|
static const char errPrefix[] = "divelog.de-upload:";
|
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
xsltStylesheetPtr xslt = NULL;
|
|
|
|
struct zip *zip;
|
|
|
|
|
2019-12-09 12:52:34 +00:00
|
|
|
emit uploadStatus(tr("building zip file to upload"));
|
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
xslt = get_stylesheet("divelogs-export.xslt");
|
|
|
|
if (!xslt) {
|
|
|
|
qDebug() << errPrefix << "missing stylesheet";
|
|
|
|
report_error(tr("Stylesheet to export to divelogs.de is not found").toUtf8());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare zip file
|
|
|
|
int error_code;
|
2019-12-09 07:28:09 +00:00
|
|
|
zip = zip_open(QFile::encodeName(QDir::toNativeSeparators(tempfile)), ZIP_CREATE, &error_code);
|
2019-11-27 08:48:58 +00:00
|
|
|
if (!zip) {
|
|
|
|
char buffer[1024];
|
|
|
|
zip_error_to_str(buffer, sizeof buffer, error_code, errno);
|
2019-12-09 07:28:09 +00:00
|
|
|
report_error(tr("Failed to create zip file for upload: %s").toUtf8(), buffer);
|
2019-11-27 08:48:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-12-09 07:28:09 +00:00
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
/* walk the dive list in chronological order */
|
|
|
|
int i;
|
|
|
|
struct dive *dive;
|
|
|
|
for_each_dive (i, dive) {
|
2019-12-09 07:28:09 +00:00
|
|
|
char filename[PATH_MAX];
|
2019-11-27 08:48:58 +00:00
|
|
|
int streamsize;
|
|
|
|
const char *membuf;
|
|
|
|
xmlDoc *transformed;
|
|
|
|
struct zip_source *s;
|
|
|
|
struct membuffer mb = {};
|
2021-07-17 07:21:26 +00:00
|
|
|
struct xml_params *params = alloc_xml_params();
|
2019-12-09 07:28:09 +00:00
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
/*
|
|
|
|
* Get the i'th dive in XML format so we can process it.
|
|
|
|
* We need to save to a file before we can reload it back into memory...
|
|
|
|
*/
|
|
|
|
if (selected && !dive->selected)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* make sure the buffer is empty and add the dive */
|
|
|
|
mb.len = 0;
|
2019-12-09 07:28:09 +00:00
|
|
|
|
|
|
|
struct dive_site *ds = dive->dive_site;
|
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
if (ds) {
|
|
|
|
put_format(&mb, "<divelog><divesites><site uuid='%8x' name='", ds->uuid);
|
|
|
|
put_quoted(&mb, ds->name, 1, 0);
|
|
|
|
put_format(&mb, "'");
|
|
|
|
put_location(&mb, &ds->location, " gps='", "'");
|
|
|
|
put_format(&mb, ">\n");
|
|
|
|
if (ds->taxonomy.nr) {
|
|
|
|
for (int j = 0; j < ds->taxonomy.nr; j++) {
|
|
|
|
struct taxonomy *t = &ds->taxonomy.category[j];
|
|
|
|
if (t->category != TC_NONE && t->category == prefs.geocoding.category[j] && t->value) {
|
|
|
|
put_format(&mb, " <geo cat='%d'", t->category);
|
|
|
|
put_format(&mb, " origin='%d' value='", t->origin);
|
|
|
|
put_quoted(&mb, t->value, 1, 0);
|
|
|
|
put_format(&mb, "'/>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put_format(&mb, "</site>\n</divesites>\n");
|
|
|
|
}
|
2019-12-09 07:28:09 +00:00
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
save_one_dive_to_mb(&mb, dive, false);
|
|
|
|
|
|
|
|
if (ds) {
|
|
|
|
put_format(&mb, "</divelog>\n");
|
|
|
|
}
|
|
|
|
membuf = mb_cstring(&mb);
|
|
|
|
streamsize = strlen(membuf);
|
|
|
|
/*
|
|
|
|
* Parse the memory buffer into XML document and
|
|
|
|
* transform it to divelogs.de format, finally dumping
|
|
|
|
* the XML into a character buffer.
|
|
|
|
*/
|
|
|
|
xmlDoc *doc = xmlReadMemory(membuf, streamsize, "divelog", NULL, 0);
|
|
|
|
if (!doc) {
|
|
|
|
qWarning() << errPrefix << "could not parse back into memory the XML file we've just created!";
|
|
|
|
report_error(tr("internal error").toUtf8());
|
|
|
|
zip_close(zip);
|
2019-12-09 07:28:09 +00:00
|
|
|
QFile::remove(tempfile);
|
2019-11-27 08:48:58 +00:00
|
|
|
xsltFreeStylesheet(xslt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
free_buffer(&mb);
|
2019-12-09 07:28:09 +00:00
|
|
|
|
2021-07-17 07:21:26 +00:00
|
|
|
xml_params_add_int(params, "allcylinders", prefs.display_unused_tanks);
|
|
|
|
transformed = xsltApplyStylesheet(xslt, doc, xml_params_get(params));
|
|
|
|
free_xml_params(params);
|
2019-11-27 08:48:58 +00:00
|
|
|
if (!transformed) {
|
|
|
|
qWarning() << errPrefix << "XSLT transform failed for dive: " << i;
|
|
|
|
report_error(tr("Conversion of dive %1 to divelogs.de format failed").arg(i).toUtf8());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
xmlDocDumpMemory(transformed, (xmlChar **)&membuf, &streamsize);
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
xmlFreeDoc(transformed);
|
2019-12-09 07:28:09 +00:00
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
/*
|
|
|
|
* Save the XML document into a zip file.
|
|
|
|
*/
|
2019-12-09 07:28:09 +00:00
|
|
|
snprintf(filename, PATH_MAX, "%d.xml", i + 1);
|
2019-11-27 08:48:58 +00:00
|
|
|
s = zip_source_buffer(zip, membuf, streamsize, 1);
|
|
|
|
if (s) {
|
2019-12-09 07:28:09 +00:00
|
|
|
int64_t ret = zip_add(zip, filename, s);
|
|
|
|
if (ret == -1)
|
2019-11-27 08:48:58 +00:00
|
|
|
qDebug() << errPrefix << "failed to include dive:" << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xsltFreeStylesheet(xslt);
|
|
|
|
if (zip_close(zip)) {
|
|
|
|
int ze, se;
|
|
|
|
#if LIBZIP_VERSION_MAJOR >= 1
|
|
|
|
zip_error_t *error = zip_get_error(zip);
|
|
|
|
ze = zip_error_code_zip(error);
|
|
|
|
se = zip_error_code_system(error);
|
|
|
|
#else
|
|
|
|
zip_error_get(zip, &ze, &se);
|
|
|
|
#endif
|
|
|
|
report_error(qPrintable(tr("error writing zip file: %s zip error %d system error %d - %s")),
|
2019-12-09 07:28:09 +00:00
|
|
|
qPrintable(QDir::toNativeSeparators(tempfile)), ze, se, zip_strerror(zip));
|
2019-11-27 08:48:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void uploadDiveLogsDE::uploadDives(const QString &filename, const QString &userid, const QString &password)
|
|
|
|
{
|
|
|
|
QHttpPart part1, part2, part3;
|
|
|
|
static QNetworkRequest request;
|
|
|
|
QString args;
|
|
|
|
|
|
|
|
// Check if there is an earlier request open
|
|
|
|
if (reply != NULL && reply->isOpen()) {
|
|
|
|
reply->abort();
|
|
|
|
delete reply;
|
|
|
|
reply = NULL;
|
|
|
|
}
|
|
|
|
if (multipart != NULL) {
|
|
|
|
delete multipart;
|
|
|
|
multipart = NULL;
|
|
|
|
}
|
|
|
|
multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
|
|
|
|
2019-12-09 12:52:34 +00:00
|
|
|
emit uploadStatus(tr("Uploading dives"));
|
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
// prepare header with filename (of all dives) and pointer to file
|
|
|
|
args = "form-data; name=\"userfile\"; filename=\"" + filename + "\"";
|
|
|
|
part1.setRawHeader("Content-Disposition", args.toLatin1());
|
|
|
|
QFile *f = new QFile(filename);
|
|
|
|
if (!f->open(QIODevice::ReadOnly)) {
|
|
|
|
qDebug() << "ERROR opening zip file: " << filename;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
part1.setBodyDevice(f);
|
|
|
|
multipart->append(part1);
|
|
|
|
|
|
|
|
// Add userid
|
|
|
|
args = "form-data; name=\"user\"";
|
|
|
|
part2.setRawHeader("Content-Disposition", args.toLatin1());
|
|
|
|
part2.setBody(qPrefCloudStorage::divelogde_user().toUtf8());
|
|
|
|
multipart->append(part2);
|
|
|
|
|
|
|
|
// Add password
|
|
|
|
args = "form-data; name=\"pass\"";
|
|
|
|
part3.setRawHeader("Content-Disposition", args.toLatin1());
|
|
|
|
part3.setBody(qPrefCloudStorage::divelogde_pass().toUtf8());
|
|
|
|
multipart->append(part3);
|
|
|
|
|
|
|
|
// Prepare network request
|
|
|
|
request.setUrl(QUrl("https://divelogs.de/DivelogsDirectImport.php"));
|
|
|
|
request.setRawHeader("Accept", "text/xml, application/xml");
|
|
|
|
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
|
|
|
|
|
|
|
|
// Execute async.
|
|
|
|
reply = manager()->post(request, multipart);
|
|
|
|
|
|
|
|
// connect signals from upload process
|
2019-12-09 09:49:40 +00:00
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(uploadFinishedSlot()));
|
2019-11-27 08:48:58 +00:00
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
|
2019-12-09 09:49:40 +00:00
|
|
|
SLOT(uploadErrorSlot(QNetworkReply::NetworkError)));
|
2019-11-27 08:48:58 +00:00
|
|
|
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this,
|
2019-12-09 09:49:40 +00:00
|
|
|
SLOT(updateProgressSlot(qint64, qint64)));
|
|
|
|
connect(&timeout, SIGNAL(timeout()), this, SLOT(uploadTimeoutSlot()));
|
2019-11-27 08:48:58 +00:00
|
|
|
|
|
|
|
timeout.start(30000); // 30s
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-09 09:49:40 +00:00
|
|
|
void uploadDiveLogsDE::updateProgressSlot(qint64 current, qint64 total)
|
2019-11-27 08:48:58 +00:00
|
|
|
{
|
|
|
|
if (!reply)
|
|
|
|
return;
|
2020-02-15 20:24:14 +00:00
|
|
|
|
2019-11-27 08:48:58 +00:00
|
|
|
if (total <= 0 || current <= 0)
|
|
|
|
return;
|
|
|
|
|
2019-12-09 11:18:33 +00:00
|
|
|
// Calculate percentage as 0.x (values between 0.0 and 1.0)
|
2019-11-27 08:48:58 +00:00
|
|
|
// And signal whoever wants to know
|
|
|
|
qreal percentage = (float)current / (float)total;
|
2019-12-09 11:18:33 +00:00
|
|
|
emit uploadProgress(percentage, 1.0);
|
2019-11-27 08:48:58 +00:00
|
|
|
|
|
|
|
// reset the timer: 30 seconds after we last got any data
|
|
|
|
timeout.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-09 09:49:40 +00:00
|
|
|
void uploadDiveLogsDE::uploadFinishedSlot()
|
2019-11-27 08:48:58 +00:00
|
|
|
{
|
|
|
|
QString err;
|
|
|
|
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// check what the server sent us: it might contain
|
|
|
|
// an error condition, such as a failed login
|
|
|
|
QByteArray xmlData = reply->readAll();
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
char *resp = xmlData.data();
|
|
|
|
if (resp) {
|
|
|
|
char *parsed = strstr(resp, "<Login>");
|
|
|
|
if (parsed) {
|
|
|
|
if (strstr(resp, "<Login>succeeded</Login>")) {
|
|
|
|
if (strstr(resp, "<FileCopy>failed</FileCopy>")) {
|
|
|
|
report_error(tr("Upload failed").toUtf8());
|
|
|
|
return;
|
|
|
|
}
|
2019-12-09 11:18:33 +00:00
|
|
|
timeout.stop();
|
2019-11-27 08:48:58 +00:00
|
|
|
err = tr("Upload successful");
|
|
|
|
emit uploadFinish(true, err);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-09 11:18:33 +00:00
|
|
|
timeout.stop();
|
2019-11-27 08:48:58 +00:00
|
|
|
err = tr("Login failed");
|
|
|
|
report_error(err.toUtf8());
|
|
|
|
emit uploadFinish(false, err);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-09 11:18:33 +00:00
|
|
|
timeout.stop();
|
2019-11-27 08:48:58 +00:00
|
|
|
err = tr("Cannot parse response");
|
|
|
|
report_error(tr("Cannot parse response").toUtf8());
|
|
|
|
emit uploadFinish(false, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-09 09:49:40 +00:00
|
|
|
void uploadDiveLogsDE::uploadTimeoutSlot()
|
2019-11-27 08:48:58 +00:00
|
|
|
{
|
2019-12-09 11:18:33 +00:00
|
|
|
timeout.stop();
|
|
|
|
if (reply) {
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
}
|
2019-11-27 08:48:58 +00:00
|
|
|
QString err(tr("divelogs.de not responding"));
|
|
|
|
report_error(err.toUtf8());
|
|
|
|
emit uploadFinish(false, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-09 09:49:40 +00:00
|
|
|
void uploadDiveLogsDE::uploadErrorSlot(QNetworkReply::NetworkError error)
|
2019-11-27 08:48:58 +00:00
|
|
|
{
|
2019-12-09 11:18:33 +00:00
|
|
|
timeout.stop();
|
|
|
|
if (reply) {
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
}
|
2019-11-27 08:48:58 +00:00
|
|
|
QString err(tr("network error %1").arg(error));
|
|
|
|
report_error(err.toUtf8());
|
|
|
|
emit uploadFinish(false, err);
|
|
|
|
}
|