2017-04-27 18:26:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/subsurfacewebservices.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2022-02-10 00:04:33 +00:00
|
|
|
#include "core/namecmp.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/webservice.h"
|
2018-09-08 17:46:11 +00:00
|
|
|
#include "core/settings/qPrefCloudStorage.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/mainwindow.h"
|
2019-11-13 14:08:40 +00:00
|
|
|
#include "commands/command.h"
|
2020-10-17 10:32:22 +00:00
|
|
|
#include "core/device.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "core/divelist.h" // For IMPORT_MERGE_ALL_TRIPS
|
|
|
|
#include "core/divelog.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "core/errorhelper.h"
|
2019-03-03 21:29:40 +00:00
|
|
|
#include "core/file.h"
|
2017-07-15 20:43:19 +00:00
|
|
|
#include "desktop-widgets/mapwidget.h"
|
2017-04-04 17:21:30 +00:00
|
|
|
#include "desktop-widgets/tab-widgets/maintab.h"
|
2019-11-24 14:02:34 +00:00
|
|
|
#include "core/selection.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/cloudstorage.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "core/subsurface-string.h"
|
2019-12-09 08:52:10 +00:00
|
|
|
#include "core/uploadDiveLogsDE.h"
|
2019-12-09 10:47:59 +00:00
|
|
|
#include "core/settings/qPrefCloudStorage.h"
|
2013-06-06 14:31:55 +00:00
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QHttpMultiPart>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QXmlStreamReader>
|
2013-06-06 14:31:55 +00:00
|
|
|
#include <qdesktopservices.h>
|
2014-04-25 17:44:23 +00:00
|
|
|
#include <QShortcut>
|
2015-02-17 14:01:49 +00:00
|
|
|
#include <QDebug>
|
2019-03-03 21:29:40 +00:00
|
|
|
#include <errno.h>
|
2019-08-05 18:07:10 +00:00
|
|
|
#include <zip.h>
|
2013-06-06 13:33:15 +00:00
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
#ifdef Q_OS_UNIX
|
2014-02-28 04:09:57 +00:00
|
|
|
#include <unistd.h> // for dup(2)
|
2013-11-15 02:57:09 +00:00
|
|
|
#endif
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
#include <QUrlQuery>
|
2014-01-15 08:30:34 +00:00
|
|
|
|
2014-08-27 21:12:05 +00:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
|
2020-10-25 21:27:39 +00:00
|
|
|
WebServices::WebServices(QWidget *parent) : QDialog(parent, QFlag(0)), reply(0)
|
2013-10-25 00:30:21 +00:00
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
2014-02-28 04:09:57 +00:00
|
|
|
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
|
2013-10-25 00:30:21 +00:00
|
|
|
connect(ui.download, SIGNAL(clicked(bool)), this, SLOT(startDownload()));
|
2013-11-15 02:57:09 +00:00
|
|
|
connect(ui.upload, SIGNAL(clicked(bool)), this, SLOT(startUpload()));
|
2013-11-15 01:47:35 +00:00
|
|
|
connect(&timeout, SIGNAL(timeout()), this, SLOT(downloadTimedOut()));
|
2013-10-25 00:30:21 +00:00
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
|
2013-11-15 01:47:35 +00:00
|
|
|
timeout.setSingleShot(true);
|
2013-12-09 17:23:32 +00:00
|
|
|
defaultApplyText = ui.buttonBox->button(QDialogButtonBox::Apply)->text();
|
2015-02-23 17:09:48 +00:00
|
|
|
userAgent = getUserAgent();
|
2013-10-25 00:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebServices::hidePassword()
|
|
|
|
{
|
|
|
|
ui.password->hide();
|
|
|
|
ui.passLabel->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebServices::hideUpload()
|
|
|
|
{
|
|
|
|
ui.upload->hide();
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.download->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebServices::hideDownload()
|
|
|
|
{
|
|
|
|
ui.download->hide();
|
|
|
|
ui.upload->show();
|
2013-10-25 00:30:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 01:47:35 +00:00
|
|
|
void WebServices::downloadTimedOut()
|
|
|
|
{
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
resetState();
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.status->setText(tr("Operation timed out"));
|
2013-11-15 01:47:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 00:52:08 +00:00
|
|
|
void WebServices::updateProgress(qint64 current, qint64 total)
|
|
|
|
{
|
|
|
|
if (!reply)
|
|
|
|
return;
|
2013-12-07 01:10:32 +00:00
|
|
|
if (total == -1) {
|
|
|
|
total = INT_MAX / 2 - 1;
|
|
|
|
}
|
2013-11-15 00:52:08 +00:00
|
|
|
if (total >= INT_MAX / 2) {
|
|
|
|
// over a gigabyte!
|
|
|
|
if (total >= Q_INT64_C(1) << 47) {
|
|
|
|
total >>= 16;
|
|
|
|
current >>= 16;
|
|
|
|
}
|
|
|
|
total >>= 16;
|
|
|
|
current >>= 16;
|
|
|
|
}
|
|
|
|
ui.progressBar->setRange(0, total);
|
|
|
|
ui.progressBar->setValue(current);
|
2014-09-12 16:29:56 +00:00
|
|
|
ui.status->setText(tr("Transferring data..."));
|
2013-11-15 00:52:08 +00:00
|
|
|
|
|
|
|
// reset the timer: 30 seconds after we last got any data
|
|
|
|
timeout.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebServices::connectSignalsForDownload(QNetworkReply *reply)
|
|
|
|
{
|
2022-04-11 04:49:11 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(reply, &QNetworkReply::finished, this, &WebServices::downloadFinished);
|
|
|
|
connect(reply, &QNetworkReply::errorOccurred, this, &WebServices::downloadError);
|
|
|
|
connect(reply, &QNetworkReply::downloadProgress, this, &WebServices::updateProgress);
|
|
|
|
#else
|
2013-11-15 00:52:08 +00:00
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
this, SLOT(downloadError(QNetworkReply::NetworkError)));
|
2014-02-28 04:09:57 +00:00
|
|
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this,
|
|
|
|
SLOT(updateProgress(qint64, qint64)));
|
2022-04-11 04:49:11 +00:00
|
|
|
#endif
|
2013-11-15 01:47:35 +00:00
|
|
|
timeout.start(30000); // 30s
|
2013-11-15 00:52:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 00:50:46 +00:00
|
|
|
void WebServices::resetState()
|
|
|
|
{
|
|
|
|
ui.download->setEnabled(true);
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.upload->setEnabled(true);
|
|
|
|
ui.userID->setEnabled(true);
|
|
|
|
ui.password->setEnabled(true);
|
2013-11-15 00:50:46 +00:00
|
|
|
ui.progressBar->reset();
|
2014-02-28 04:09:57 +00:00
|
|
|
ui.progressBar->setRange(0, 1);
|
2013-11-15 00:50:46 +00:00
|
|
|
ui.status->setText(QString());
|
2013-12-09 17:23:32 +00:00
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setText(defaultApplyText);
|
2013-11-15 00:50:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 14:57:12 +00:00
|
|
|
|
2013-10-25 00:52:11 +00:00
|
|
|
// #
|
|
|
|
// #
|
|
|
|
// # Divelogs DE Web Service Implementation.
|
|
|
|
// #
|
|
|
|
// #
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
struct DiveListResult {
|
2013-11-15 02:57:09 +00:00
|
|
|
QString errorCondition;
|
|
|
|
QString errorDetails;
|
|
|
|
QByteArray idList; // comma-separated, suitable to be sent in the fetch request
|
|
|
|
int idCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
static DiveListResult parseDiveLogsDeDiveList(const QByteArray &xmlData)
|
|
|
|
{
|
2013-12-07 01:09:08 +00:00
|
|
|
/* XML format seems to be:
|
|
|
|
* <DiveDateReader version="1.0">
|
|
|
|
* <DiveDates>
|
|
|
|
* <date diveLogsId="nnn" lastModified="YYYY-MM-DD hh:mm:ss">DD.MM.YYYY hh:mm</date>
|
|
|
|
* [repeat <date></date>]
|
|
|
|
* </DiveDates>
|
|
|
|
* </DiveDateReader>
|
|
|
|
*/
|
|
|
|
QXmlStreamReader reader(xmlData);
|
2018-07-03 14:52:20 +00:00
|
|
|
const QString invalidXmlError = gettextFromC::tr("Invalid response from server");
|
2013-12-07 01:09:08 +00:00
|
|
|
bool seenDiveDates = false;
|
|
|
|
DiveListResult result;
|
|
|
|
result.idCount = 0;
|
|
|
|
|
2022-02-10 00:04:33 +00:00
|
|
|
if (reader.readNextStartElement() && nameCmp(reader, "DiveDateReader") != 0) {
|
2013-12-07 01:09:08 +00:00
|
|
|
result.errorCondition = invalidXmlError;
|
|
|
|
result.errorDetails =
|
2018-07-03 14:52:20 +00:00
|
|
|
gettextFromC::tr("Expected XML tag 'DiveDateReader', got instead '%1")
|
2014-05-22 18:40:22 +00:00
|
|
|
.arg(reader.name().toString());
|
2013-12-07 01:09:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (reader.readNextStartElement()) {
|
2022-02-10 00:04:33 +00:00
|
|
|
if (nameCmp(reader, "DiveDates") != 0) {
|
|
|
|
if (nameCmp(reader, "Login") == 0) {
|
2013-12-07 01:09:08 +00:00
|
|
|
QString status = reader.readElementText();
|
|
|
|
// qDebug() << "Login status:" << status;
|
|
|
|
|
|
|
|
// Note: there has to be a better way to determine a successful login...
|
|
|
|
if (status == "failed") {
|
|
|
|
result.errorCondition = "Login failed";
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// qDebug() << "Skipping" << reader.name();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// process <DiveDates>
|
|
|
|
seenDiveDates = true;
|
|
|
|
while (reader.readNextStartElement()) {
|
2022-02-10 00:04:33 +00:00
|
|
|
if (nameCmp(reader, "date") != 0) {
|
2013-12-07 01:09:08 +00:00
|
|
|
// qDebug() << "Skipping" << reader.name();
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-10 00:04:33 +00:00
|
|
|
auto id = reader.attributes().value("divelogsId");
|
2013-12-07 01:09:08 +00:00
|
|
|
// qDebug() << "Found" << reader.name() << "with id =" << id;
|
|
|
|
if (!id.isEmpty()) {
|
|
|
|
result.idList += id.toLatin1();
|
|
|
|
result.idList += ',';
|
|
|
|
++result.idCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chop the ending comma, if any
|
|
|
|
result.idList.chop(1);
|
|
|
|
|
|
|
|
if (!seenDiveDates) {
|
|
|
|
result.errorCondition = invalidXmlError;
|
2018-07-03 14:52:20 +00:00
|
|
|
result.errorDetails = gettextFromC::tr("Expected XML tag 'DiveDates' not found");
|
2013-12-07 01:09:08 +00:00
|
|
|
}
|
2013-11-15 02:57:09 +00:00
|
|
|
|
|
|
|
out:
|
2013-12-07 01:09:08 +00:00
|
|
|
if (reader.hasError()) {
|
|
|
|
// if there was an XML error, overwrite the result or other error conditions
|
|
|
|
result.errorCondition = invalidXmlError;
|
2018-07-03 14:52:20 +00:00
|
|
|
result.errorDetails = gettextFromC::tr("Malformed XML response. Line %1: %2")
|
2014-05-22 18:40:22 +00:00
|
|
|
.arg(reader.lineNumber())
|
|
|
|
.arg(reader.errorString());
|
2013-12-07 01:09:08 +00:00
|
|
|
}
|
|
|
|
return result;
|
2013-11-15 02:57:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
DivelogsDeWebServices *DivelogsDeWebServices::instance()
|
2013-10-25 00:52:11 +00:00
|
|
|
{
|
2014-02-12 14:22:54 +00:00
|
|
|
static DivelogsDeWebServices *self = new DivelogsDeWebServices(MainWindow::instance());
|
2013-10-25 01:02:59 +00:00
|
|
|
return self;
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
void DivelogsDeWebServices::downloadDives()
|
2013-10-25 00:52:11 +00:00
|
|
|
{
|
2013-12-09 17:23:32 +00:00
|
|
|
uploadMode = false;
|
|
|
|
resetState();
|
2013-11-15 02:57:09 +00:00
|
|
|
hideUpload();
|
|
|
|
exec();
|
|
|
|
}
|
2013-10-25 00:52:11 +00:00
|
|
|
|
2014-05-20 16:33:32 +00:00
|
|
|
void DivelogsDeWebServices::prepareDivesForUpload(bool selected)
|
2019-12-09 11:51:16 +00:00
|
|
|
{
|
|
|
|
// this is called when the user selects the divelogs.de radiobutton
|
|
|
|
|
2019-12-09 12:24:08 +00:00
|
|
|
// Remember if all dives or selected dives are to be uploaded
|
|
|
|
useSelectedDives = selected;
|
2019-12-09 11:51:16 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2020-10-25 21:27:39 +00:00
|
|
|
DivelogsDeWebServices::DivelogsDeWebServices(QWidget *parent) : WebServices(parent),
|
2015-03-25 05:41:38 +00:00
|
|
|
uploadMode(false)
|
2013-11-15 02:57:09 +00:00
|
|
|
{
|
2019-03-19 15:22:51 +00:00
|
|
|
// should DivelogDE user and pass be stored in the prefs struct or something?
|
2019-12-09 10:47:59 +00:00
|
|
|
ui.userID->setText(qPrefCloudStorage::divelogde_user());
|
|
|
|
ui.password->setText(qPrefCloudStorage::divelogde_pass());
|
2014-04-11 06:17:35 +00:00
|
|
|
ui.saveUidLocal->hide();
|
2013-11-15 02:57:09 +00:00
|
|
|
hideUpload();
|
2022-02-08 19:24:53 +00:00
|
|
|
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), this);
|
2014-04-25 17:44:23 +00:00
|
|
|
connect(close, SIGNAL(activated()), this, SLOT(close()));
|
2022-02-08 19:24:53 +00:00
|
|
|
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this);
|
2014-04-25 17:44:23 +00:00
|
|
|
connect(quit, SIGNAL(activated()), parent, SLOT(close()));
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::startUpload()
|
|
|
|
{
|
2019-12-09 10:47:59 +00:00
|
|
|
qPrefCloudStorage::set_divelogde_user(ui.userID->text());
|
|
|
|
qPrefCloudStorage::set_divelogde_pass(ui.password->text());
|
2013-12-09 14:45:54 +00:00
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.status->setText(tr("Uploading dive list..."));
|
2014-02-28 04:09:57 +00:00
|
|
|
ui.progressBar->setRange(0, 0); // this makes the progressbar do an 'infinite spin'
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.upload->setEnabled(false);
|
|
|
|
ui.userID->setEnabled(false);
|
|
|
|
ui.password->setEnabled(false);
|
2013-10-25 00:52:11 +00:00
|
|
|
|
2019-12-09 13:02:05 +00:00
|
|
|
// do upload in shared backend
|
|
|
|
connect(uploadDiveLogsDE::instance(), SIGNAL(uploadFinish(bool, const QString &)),
|
|
|
|
this, SLOT(uploadFinished(bool, const QString &)));
|
|
|
|
connect(uploadDiveLogsDE::instance(), SIGNAL(uploadProgress(qreal, qreal)),
|
|
|
|
this, SLOT(updateProgress(qreal, qreal)));
|
|
|
|
connect(uploadDiveLogsDE::instance(), SIGNAL(uploadStatus(const QString &)),
|
|
|
|
this, SLOT(uploadStatus(const QString &)));
|
|
|
|
uploadDiveLogsDE::instance()->doUpload(useSelectedDives,
|
|
|
|
qPrefCloudStorage::divelogde_user(),
|
|
|
|
qPrefCloudStorage::divelogde_pass());
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::startDownload()
|
|
|
|
{
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.status->setText(tr("Downloading dive list..."));
|
2014-02-28 04:09:57 +00:00
|
|
|
ui.progressBar->setRange(0, 0); // this makes the progressbar do an 'infinite spin'
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.download->setEnabled(false);
|
|
|
|
ui.userID->setEnabled(false);
|
|
|
|
ui.password->setEnabled(false);
|
|
|
|
|
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(QUrl("https://divelogs.de/xml_available_dives.php"));
|
|
|
|
request.setRawHeader("Accept", "text/xml, application/xml");
|
2014-07-31 18:20:11 +00:00
|
|
|
request.setRawHeader("User-Agent", userAgent.toUtf8());
|
2013-11-15 02:57:09 +00:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
QUrlQuery body;
|
|
|
|
body.addQueryItem("user", ui.userID->text());
|
2016-02-22 18:11:22 +00:00
|
|
|
body.addQueryItem("pass", ui.password->text().replace("+", "%2b"));
|
2013-11-15 02:57:09 +00:00
|
|
|
|
2014-01-15 08:30:33 +00:00
|
|
|
reply = manager()->post(request, body.query(QUrl::FullyEncoded).toLatin1());
|
2022-04-11 04:49:11 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(reply, &QNetworkReply::finished, this, &DivelogsDeWebServices::listDownloadFinished);
|
|
|
|
connect(reply, &QNetworkReply::errorOccurred, this, &DivelogsDeWebServices::downloadError);
|
|
|
|
#else
|
2013-11-15 02:57:09 +00:00
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(listDownloadFinished()));
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
this, SLOT(downloadError(QNetworkReply::NetworkError)));
|
2022-04-11 04:49:11 +00:00
|
|
|
#endif
|
2013-11-15 02:57:09 +00:00
|
|
|
timeout.start(30000); // 30s
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::listDownloadFinished()
|
|
|
|
{
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
QByteArray xmlData = reply->readAll();
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
|
|
|
|
// parse the XML data we downloaded
|
|
|
|
DiveListResult diveList = parseDiveLogsDeDiveList(xmlData);
|
|
|
|
if (!diveList.errorCondition.isEmpty()) {
|
|
|
|
// error condition
|
|
|
|
resetState();
|
|
|
|
ui.status->setText(diveList.errorCondition);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.status->setText(tr("Downloading %1 dives...").arg(diveList.idCount));
|
|
|
|
|
|
|
|
QNetworkRequest request;
|
2013-12-29 05:04:07 +00:00
|
|
|
request.setUrl(QUrl("https://divelogs.de/DivelogsDirectExport.php"));
|
2013-11-15 02:57:09 +00:00
|
|
|
request.setRawHeader("Accept", "application/zip, */*");
|
2014-07-31 18:20:11 +00:00
|
|
|
request.setRawHeader("User-Agent", userAgent.toUtf8());
|
2013-11-15 02:57:09 +00:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
QUrlQuery body;
|
|
|
|
body.addQueryItem("user", ui.userID->text());
|
2016-02-22 18:11:22 +00:00
|
|
|
body.addQueryItem("pass", ui.password->text().replace("+", "%2b"));
|
2013-11-15 02:57:09 +00:00
|
|
|
body.addQueryItem("ids", diveList.idList);
|
|
|
|
|
2014-01-15 08:30:33 +00:00
|
|
|
reply = manager()->post(request, body.query(QUrl::FullyEncoded).toLatin1());
|
2013-11-15 02:57:09 +00:00
|
|
|
connect(reply, SIGNAL(readyRead()), this, SLOT(saveToZipFile()));
|
|
|
|
connectSignalsForDownload(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::saveToZipFile()
|
|
|
|
{
|
|
|
|
if (!zipFile.isOpen()) {
|
|
|
|
zipFile.setFileTemplate(QDir::tempPath() + "/import-XXXXXX.dld");
|
|
|
|
zipFile.open();
|
|
|
|
}
|
2013-10-25 00:52:11 +00:00
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
zipFile.write(reply->readAll());
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::downloadFinished()
|
|
|
|
{
|
2013-11-15 02:57:09 +00:00
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ui.download->setEnabled(true);
|
|
|
|
ui.status->setText(tr("Download finished - %1").arg(reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()));
|
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
|
|
|
|
int errorcode;
|
|
|
|
zipFile.seek(0);
|
2013-12-20 01:01:54 +00:00
|
|
|
#if defined(Q_OS_UNIX) && defined(LIBZIP_VERSION_MAJOR)
|
2013-11-15 02:57:09 +00:00
|
|
|
int duppedfd = dup(zipFile.handle());
|
2015-06-22 13:46:01 +00:00
|
|
|
struct zip *zip = NULL;
|
|
|
|
if (duppedfd >= 0) {
|
|
|
|
zip = zip_fdopen(duppedfd, 0, &errorcode);
|
|
|
|
if (!zip)
|
|
|
|
::close(duppedfd);
|
2015-06-23 05:23:04 +00:00
|
|
|
} else {
|
|
|
|
QMessageBox::critical(this, tr("Problem with download"),
|
2019-03-24 10:07:45 +00:00
|
|
|
tr("The archive could not be opened:\n%1").arg(QString::fromLocal8Bit(strerror(errno))));
|
2015-06-23 05:23:04 +00:00
|
|
|
return;
|
2015-06-22 13:46:01 +00:00
|
|
|
}
|
2013-11-15 02:57:09 +00:00
|
|
|
#else
|
2013-12-20 01:02:34 +00:00
|
|
|
struct zip *zip = zip_open(QFile::encodeName(zipFile.fileName()), 0, &errorcode);
|
2013-11-15 02:57:09 +00:00
|
|
|
#endif
|
|
|
|
if (!zip) {
|
|
|
|
char buf[512];
|
|
|
|
zip_error_to_str(buf, sizeof(buf), errorcode, errno);
|
|
|
|
QMessageBox::critical(this, tr("Corrupted download"),
|
|
|
|
tr("The archive could not be opened:\n%1").arg(QString::fromLocal8Bit(buf)));
|
|
|
|
zipFile.close();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-07 01:10:32 +00:00
|
|
|
// now allow the user to cancel or accept
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
|
2013-11-15 02:57:09 +00:00
|
|
|
|
|
|
|
zip_close(zip);
|
|
|
|
zipFile.close();
|
2014-03-06 23:08:27 +00:00
|
|
|
#if defined(Q_OS_UNIX) && defined(LIBZIP_VERSION_MAJOR)
|
|
|
|
::close(duppedfd);
|
|
|
|
#endif
|
2013-11-15 02:57:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 13:02:05 +00:00
|
|
|
void DivelogsDeWebServices::uploadFinished(bool success, const QString &text)
|
2013-11-15 02:57:09 +00:00
|
|
|
{
|
2014-02-28 04:09:57 +00:00
|
|
|
ui.progressBar->setRange(0, 1);
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.upload->setEnabled(true);
|
2013-12-09 19:28:05 +00:00
|
|
|
ui.userID->setEnabled(true);
|
|
|
|
ui.password->setEnabled(true);
|
2013-12-09 17:23:32 +00:00
|
|
|
ui.buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(false);
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setText(tr("Done"));
|
2019-12-09 13:02:05 +00:00
|
|
|
ui.status->setText(text);
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 16:09:09 +00:00
|
|
|
void DivelogsDeWebServices::setStatusText(int)
|
2013-10-25 00:52:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
void DivelogsDeWebServices::downloadError(QNetworkReply::NetworkError)
|
2013-10-25 00:52:11 +00:00
|
|
|
{
|
2013-11-15 02:57:09 +00:00
|
|
|
resetState();
|
2013-12-09 15:42:06 +00:00
|
|
|
ui.status->setText(tr("Error: %1").arg(reply->errorString()));
|
2013-11-15 02:57:09 +00:00
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
|
|
|
}
|
2013-10-25 00:52:11 +00:00
|
|
|
|
2019-12-09 13:02:05 +00:00
|
|
|
void DivelogsDeWebServices::updateProgress(qreal current, qreal total)
|
|
|
|
{
|
2020-04-01 16:29:49 +00:00
|
|
|
ui.progressBar->setRange(0, lrint(total));
|
|
|
|
ui.progressBar->setValue(lrint(current));
|
2019-12-09 13:02:05 +00:00
|
|
|
ui.status->setText(tr("Transferring data..."));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::uploadStatus(const QString &text)
|
2013-11-15 02:57:09 +00:00
|
|
|
{
|
2019-12-09 13:02:05 +00:00
|
|
|
ui.status->setText(text);
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void DivelogsDeWebServices::buttonClicked(QAbstractButton *button)
|
2013-10-25 00:52:11 +00:00
|
|
|
{
|
2013-12-07 01:10:32 +00:00
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
|
2014-01-16 04:50:56 +00:00
|
|
|
switch (ui.buttonBox->buttonRole(button)) {
|
|
|
|
case QDialogButtonBox::ApplyRole: {
|
2013-12-09 17:23:32 +00:00
|
|
|
/* in 'uploadMode' button is called 'Done' and closes the dialog */
|
|
|
|
if (uploadMode) {
|
|
|
|
hide();
|
|
|
|
close();
|
|
|
|
resetState();
|
|
|
|
break;
|
|
|
|
}
|
2013-12-07 12:33:43 +00:00
|
|
|
/* parse file and import dives */
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
struct divelog log;
|
|
|
|
parse_file(QFile::encodeName(zipFile.fileName()), &log);
|
|
|
|
Command::importDives(log.dives, log.trips, log.sites, log.devices, nullptr, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de"));
|
2013-12-07 01:10:32 +00:00
|
|
|
|
2013-12-07 12:33:43 +00:00
|
|
|
/* store last entered user/pass in config */
|
2019-12-09 10:47:59 +00:00
|
|
|
qPrefCloudStorage::set_divelogde_user(ui.userID->text());
|
|
|
|
qPrefCloudStorage::set_divelogde_pass(ui.password->text());
|
2013-12-07 01:10:32 +00:00
|
|
|
hide();
|
|
|
|
close();
|
|
|
|
resetState();
|
2014-02-28 04:09:57 +00:00
|
|
|
} break;
|
2013-12-07 12:33:43 +00:00
|
|
|
case QDialogButtonBox::RejectRole:
|
|
|
|
// these two seem to be causing a crash:
|
|
|
|
// reply->deleteLater();
|
|
|
|
resetState();
|
|
|
|
break;
|
|
|
|
case QDialogButtonBox::HelpRole:
|
|
|
|
QDesktopServices::openUrl(QUrl("http://divelogs.de"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-12-07 01:10:32 +00:00
|
|
|
}
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|