diff options
author | jan Iversen <jan@casacondor.com> | 2019-12-09 12:51:16 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-12-11 12:36:43 -0500 |
commit | 8becc29ca881332b3567ec15a396da9845837318 (patch) | |
tree | ca926a0331289ca68331f91d182c93096c559ff7 /desktop-widgets | |
parent | 10ab833d7dc00da13aa0cf0f0490bae63cb1294a (diff) | |
download | subsurface-8becc29ca881332b3567ec15a396da9845837318.tar.gz |
desktop-widgets: split UI and network in DivelogsDeWebServices (prepare)
Clean prepareDivesForUpload() and uploadDives() so that
uploadDives() only contain network handling no UI.
Signed-off-by: Jan Iversen <jan@casacondor.com>
Diffstat (limited to 'desktop-widgets')
-rw-r--r-- | desktop-widgets/subsurfacewebservices.cpp | 57 | ||||
-rw-r--r-- | desktop-widgets/subsurfacewebservices.h | 2 |
2 files changed, 33 insertions, 26 deletions
diff --git a/desktop-widgets/subsurfacewebservices.cpp b/desktop-widgets/subsurfacewebservices.cpp index 9d2d01aca..bca36b5e4 100644 --- a/desktop-widgets/subsurfacewebservices.cpp +++ b/desktop-widgets/subsurfacewebservices.cpp @@ -236,47 +236,51 @@ void DivelogsDeWebServices::downloadDives() void DivelogsDeWebServices::prepareDivesForUpload(bool selected) { + // this is called when the user selects the divelogs.de radiobutton + + // Prepare zip file + if (!uploadDives(selected)) + return; + + // Adjust UI + hideDownload(); + resetState(); + uploadMode = true; + ui.buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(true); + ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); + ui.buttonBox->button(QDialogButtonBox::Apply)->setText(tr("Done")); + exec(); +} + +bool DivelogsDeWebServices::uploadDives(bool selected) +{ /* generate a random filename and create/open that file with zip_open */ QString filename = QDir::tempPath() + "/import-" + QString::number(qrand() % 99999999) + ".dld"; if (!amount_selected) { report_error(tr("No dives were selected").toUtf8()); - return; + return false; } - if (uploadDiveLogsDE::instance()->prepareDives(filename, selected)) { - QFile f(filename); - if (f.open(QIODevice::ReadOnly)) { - uploadDives((QIODevice *)&f); - f.close(); - f.remove(); - return; - } else { - report_error("Failed to open upload file %s\n", qPrintable(filename)); - } - } else { + if (!uploadDiveLogsDE::instance()->prepareDives(filename, selected)) { report_error("Failed to create upload file %s\n", qPrintable(filename)); + return false; + } + + QFile f(filename); + if (!f.open(QIODevice::ReadOnly)) { + report_error("Failed to open upload file %s\n", qPrintable(filename)); + return false; } -} -void DivelogsDeWebServices::uploadDives(QIODevice *dldContent) -{ QHttpMultiPart mp(QHttpMultiPart::FormDataType); QHttpPart part; - QFile *f = (QFile *)dldContent; - QFileInfo fi(*f); + QFileInfo fi(f); QString args("form-data; name=\"userfile\"; filename=\"" + fi.absoluteFilePath() + "\""); part.setRawHeader("Content-Disposition", args.toLatin1()); - part.setBodyDevice(dldContent); + part.setBodyDevice((QIODevice *)&f); mp.append(part); multipart = ∓ - hideDownload(); - resetState(); - uploadMode = true; - ui.buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(true); - ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); - ui.buttonBox->button(QDialogButtonBox::Apply)->setText(tr("Done")); - exec(); multipart = NULL; if (reply != NULL && reply->isOpen()) { @@ -284,6 +288,9 @@ void DivelogsDeWebServices::uploadDives(QIODevice *dldContent) delete reply; reply = NULL; } + f.close(); + f.remove(); + return true; } DivelogsDeWebServices::DivelogsDeWebServices(QWidget *parent, Qt::WindowFlags f) : WebServices(parent, f), diff --git a/desktop-widgets/subsurfacewebservices.h b/desktop-widgets/subsurfacewebservices.h index b67217684..4da15c392 100644 --- a/desktop-widgets/subsurfacewebservices.h +++ b/desktop-widgets/subsurfacewebservices.h @@ -65,7 +65,7 @@ slots: void startUpload(); private: - void uploadDives(QIODevice *dldContent); + bool uploadDives(bool selected); explicit DivelogsDeWebServices(QWidget *parent = 0, Qt::WindowFlags f = 0); void setStatusText(int status); void download_dialog_traverse_xml(xmlNodePtr node, unsigned int *download_status); |