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>
This commit is contained in:
jan Iversen 2019-12-09 12:51:16 +01:00 committed by Dirk Hohndel
parent 10ab833d7d
commit 8becc29ca8
2 changed files with 40 additions and 33 deletions

View file

@ -236,40 +236,13 @@ void DivelogsDeWebServices::downloadDives()
void DivelogsDeWebServices::prepareDivesForUpload(bool selected) void DivelogsDeWebServices::prepareDivesForUpload(bool selected)
{ {
/* generate a random filename and create/open that file with zip_open */ // this is called when the user selects the divelogs.de radiobutton
QString filename = QDir::tempPath() + "/import-" + QString::number(qrand() % 99999999) + ".dld";
if (!amount_selected) { // Prepare zip file
report_error(tr("No dives were selected").toUtf8()); if (!uploadDives(selected))
return; return;
}
if (uploadDiveLogsDE::instance()->prepareDives(filename, selected)) { // Adjust UI
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 {
report_error("Failed to create upload file %s\n", qPrintable(filename));
}
}
void DivelogsDeWebServices::uploadDives(QIODevice *dldContent)
{
QHttpMultiPart mp(QHttpMultiPart::FormDataType);
QHttpPart part;
QFile *f = (QFile *)dldContent;
QFileInfo fi(*f);
QString args("form-data; name=\"userfile\"; filename=\"" + fi.absoluteFilePath() + "\"");
part.setRawHeader("Content-Disposition", args.toLatin1());
part.setBodyDevice(dldContent);
mp.append(part);
multipart = &mp;
hideDownload(); hideDownload();
resetState(); resetState();
uploadMode = true; uploadMode = true;
@ -277,6 +250,37 @@ void DivelogsDeWebServices::uploadDives(QIODevice *dldContent)
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
ui.buttonBox->button(QDialogButtonBox::Apply)->setText(tr("Done")); ui.buttonBox->button(QDialogButtonBox::Apply)->setText(tr("Done"));
exec(); 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 false;
}
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;
}
QHttpMultiPart mp(QHttpMultiPart::FormDataType);
QHttpPart part;
QFileInfo fi(f);
QString args("form-data; name=\"userfile\"; filename=\"" + fi.absoluteFilePath() + "\"");
part.setRawHeader("Content-Disposition", args.toLatin1());
part.setBodyDevice((QIODevice *)&f);
mp.append(part);
multipart = &mp;
multipart = NULL; multipart = NULL;
if (reply != NULL && reply->isOpen()) { if (reply != NULL && reply->isOpen()) {
@ -284,6 +288,9 @@ void DivelogsDeWebServices::uploadDives(QIODevice *dldContent)
delete reply; delete reply;
reply = NULL; reply = NULL;
} }
f.close();
f.remove();
return true;
} }
DivelogsDeWebServices::DivelogsDeWebServices(QWidget *parent, Qt::WindowFlags f) : WebServices(parent, f), DivelogsDeWebServices::DivelogsDeWebServices(QWidget *parent, Qt::WindowFlags f) : WebServices(parent, f),

View file

@ -65,7 +65,7 @@ slots:
void startUpload(); void startUpload();
private: private:
void uploadDives(QIODevice *dldContent); bool uploadDives(bool selected);
explicit DivelogsDeWebServices(QWidget *parent = 0, Qt::WindowFlags f = 0); explicit DivelogsDeWebServices(QWidget *parent = 0, Qt::WindowFlags f = 0);
void setStatusText(int status); void setStatusText(int status);
void download_dialog_traverse_xml(xmlNodePtr node, unsigned int *download_status); void download_dialog_traverse_xml(xmlNodePtr node, unsigned int *download_status);