From 49e633039911db835beb29a77bbc890117db9845 Mon Sep 17 00:00:00 2001 From: jan Iversen Date: Mon, 9 Dec 2019 08:28:09 +0100 Subject: [PATCH] core: make shared prepareDives identical to desktop prepare_dives_for_divelogs In order to replace DivelogsDeWebServices::prepare_dives_for_divelogs with uploadDiveLogsDE::prepareDives, first step is to make the functions identical. amount_selected is not maintained for mobile, add #ifdef SUBSURFACE_MOBILE Add comment, to make code more readable add white line to make code more readable change to use variable ds (created a couple of lines earlier Avoid "goto" by adding close code Remove label and close code (it was only called in 1 place) Signed-off-by: Jan Iversen --- core/uploadDiveLogsDE.cpp | 46 ++++++++++++++++++++++++--------------- core/uploadDiveLogsDE.h | 2 +- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/core/uploadDiveLogsDE.cpp b/core/uploadDiveLogsDE.cpp index ff86cdb96..88cd0c87e 100644 --- a/core/uploadDiveLogsDE.cpp +++ b/core/uploadDiveLogsDE.cpp @@ -11,6 +11,9 @@ #include "core/membuffer.h" #include "core/divesite.h" #include "core/cloudstorage.h" +#ifndef SUBSURFACE_MOBILE +#include "core/selection.h" +#endif // SUBSURFACE_MOBILE #include "core/settings/qPrefCloudStorage.h" @@ -45,7 +48,7 @@ void uploadDiveLogsDE::doUpload(bool selected, const QString &userid, const QStr } // Make zip file, with all dives, in divelogs.de format - if (!prepareDives(selected, filename)) { + if (!prepareDives(filename, selected)) { report_error(tr("Failed to create upload file %s\n").toUtf8(), qPrintable(filename)); emit uploadFinish(false, err); timeout.stop(); @@ -58,9 +61,16 @@ void uploadDiveLogsDE::doUpload(bool selected, const QString &userid, const QStr } -bool uploadDiveLogsDE::prepareDives(bool selected, const QString &filename) +bool uploadDiveLogsDE::prepareDives(const QString &tempfile, const bool selected) { - static const char errPrefix[] = "divelog.de-uploadDiveLogsDE:"; + static const char errPrefix[] = "divelog.de-upload:"; +#ifndef SUBSURFACE_MOBILE + if (!amount_selected) { + report_error(tr("No dives were selected").toUtf8()); + return false; + } +#endif // SUBSURFACE_MOBILE + xsltStylesheetPtr xslt = NULL; struct zip *zip; @@ -73,25 +83,25 @@ bool uploadDiveLogsDE::prepareDives(bool selected, const QString &filename) // Prepare zip file int error_code; - zip = zip_open(QFile::encodeName(QDir::toNativeSeparators(filename)), ZIP_CREATE, &error_code); + zip = zip_open(QFile::encodeName(QDir::toNativeSeparators(tempfile)), ZIP_CREATE, &error_code); if (!zip) { char buffer[1024]; zip_error_to_str(buffer, sizeof buffer, error_code, errno); - report_error(tr("Failed to create zip file for uploadDiveLogsDE: %s").toUtf8(), buffer); + report_error(tr("Failed to create zip file for upload: %s").toUtf8(), buffer); return false; } - + /* walk the dive list in chronological order */ int i; struct dive *dive; for_each_dive (i, dive) { - char xmlfilename[PATH_MAX]; + char filename[PATH_MAX]; int streamsize; const char *membuf; xmlDoc *transformed; struct zip_source *s; struct membuffer mb = {}; - + /* * 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... @@ -100,8 +110,10 @@ bool uploadDiveLogsDE::prepareDives(bool selected, const QString &filename) continue; /* make sure the buffer is empty and add the dive */ - struct dive_site *ds = dive->dive_site; mb.len = 0; + + struct dive_site *ds = dive->dive_site; + if (ds) { put_format(&mb, "