2013-06-06 13:33:15 +00:00
|
|
|
#include "subsurfacewebservices.h"
|
2013-06-06 14:31:55 +00:00
|
|
|
#include "../webservice.h"
|
2013-11-30 17:18:04 +00:00
|
|
|
#include "mainwindow.h"
|
2013-06-06 14:31:55 +00:00
|
|
|
#include <libxml/parser.h>
|
2013-11-15 02:57:09 +00:00
|
|
|
#include <zip.h>
|
2013-12-05 00:29:55 +00:00
|
|
|
#include <errno.h>
|
2013-06-06 14:31:55 +00:00
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QHttpMultiPart>
|
|
|
|
#include <QMessageBox>
|
2013-06-06 13:33:15 +00:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QDebug>
|
2013-06-06 14:57:12 +00:00
|
|
|
#include <QSettings>
|
2013-11-15 02:57:09 +00:00
|
|
|
#include <QXmlStreamReader>
|
2013-06-06 14:31:55 +00:00
|
|
|
#include <qdesktopservices.h>
|
2013-06-06 13:33:15 +00:00
|
|
|
|
2013-06-06 14:57:12 +00:00
|
|
|
#include "../dive.h"
|
|
|
|
#include "../divelist.h"
|
|
|
|
|
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
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QUrlQuery>
|
2014-01-15 08:30:34 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-06 14:57:12 +00:00
|
|
|
struct dive_table gps_location_table;
|
2013-10-05 07:29:09 +00:00
|
|
|
static bool merge_locations_into_dives(void);
|
2013-06-06 14:57:12 +00:00
|
|
|
|
2013-11-14 22:59:06 +00:00
|
|
|
static bool is_automatic_fix(struct dive *gpsfix)
|
|
|
|
{
|
|
|
|
if (gpsfix && gpsfix->location &&
|
2014-02-28 04:09:57 +00:00
|
|
|
(!strcmp(gpsfix->location, "automatic fix") ||
|
|
|
|
!strcmp(gpsfix->location, "Auto-created dive")))
|
2014-01-15 08:30:42 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2013-11-14 22:59:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
#define SAME_GROUP 6 * 3600 // six hours
|
2013-11-14 22:59:06 +00:00
|
|
|
|
|
|
|
static bool merge_locations_into_dives(void)
|
|
|
|
{
|
|
|
|
int i, nr = 0, changed = 0;
|
|
|
|
struct dive *gpsfix, *last_named_fix = NULL, *dive;
|
|
|
|
|
|
|
|
sort_table(&gps_location_table);
|
|
|
|
|
|
|
|
for_each_gps_location(i, gpsfix) {
|
|
|
|
if (is_automatic_fix(gpsfix)) {
|
|
|
|
dive = find_dive_including(gpsfix->when);
|
|
|
|
if (dive && !dive_has_gps_location(dive)) {
|
|
|
|
#if DEBUG_WEBSERVICE
|
|
|
|
struct tm tm;
|
|
|
|
utc_mkdate(gpsfix->when, &tm);
|
|
|
|
printf("found dive named %s @ %04d-%02d-%02d %02d:%02d:%02d\n",
|
|
|
|
gpsfix->location,
|
2014-02-28 04:09:57 +00:00
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
2013-11-14 22:59:06 +00:00
|
|
|
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
|
|
|
#endif
|
|
|
|
changed++;
|
|
|
|
copy_gps_location(gpsfix, dive);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (last_named_fix && dive_within_time_range(last_named_fix, gpsfix->when, SAME_GROUP)) {
|
|
|
|
nr++;
|
|
|
|
} else {
|
|
|
|
nr = 1;
|
|
|
|
last_named_fix = gpsfix;
|
|
|
|
}
|
|
|
|
dive = find_dive_n_near(gpsfix->when, nr, SAME_GROUP);
|
|
|
|
if (dive) {
|
|
|
|
if (!dive_has_gps_location(dive)) {
|
|
|
|
copy_gps_location(gpsfix, dive);
|
|
|
|
changed++;
|
|
|
|
}
|
|
|
|
if (!dive->location) {
|
|
|
|
dive->location = strdup(gpsfix->location);
|
|
|
|
changed++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct tm tm;
|
|
|
|
utc_mkdate(gpsfix->when, &tm);
|
|
|
|
#if DEBUG_WEBSERVICE
|
|
|
|
printf("didn't find dive matching gps fix named %s @ %04d-%02d-%02d %02d:%02d:%02d\n",
|
|
|
|
gpsfix->location,
|
2014-02-28 04:09:57 +00:00
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
2013-11-14 22:59:06 +00:00
|
|
|
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_table(struct dive_table *table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < table->nr; i++)
|
|
|
|
free(table->dives[i]);
|
|
|
|
table->nr = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-20 01:03:23 +00:00
|
|
|
bool DivelogsDeWebServices::prepare_dives_for_divelogs(const QString &tempfile, const bool selected, QString *errorMsg)
|
2013-12-05 16:31:40 +00:00
|
|
|
{
|
2013-12-12 01:56:29 +00:00
|
|
|
static const char errPrefix[] = "divelog.de-upload:";
|
2013-12-12 01:56:28 +00:00
|
|
|
if (!amount_selected) {
|
2013-12-20 01:03:23 +00:00
|
|
|
*errorMsg = tr("no dives were selected");
|
2013-12-20 01:03:21 +00:00
|
|
|
return false;
|
2013-12-12 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 16:31:40 +00:00
|
|
|
xsltStylesheetPtr xslt = NULL;
|
|
|
|
struct zip *zip;
|
|
|
|
|
2013-12-12 01:56:30 +00:00
|
|
|
xslt = get_stylesheet("divelogs-export.xslt");
|
|
|
|
if (!xslt) {
|
|
|
|
qDebug() << errPrefix << "missing stylesheet";
|
2013-12-20 01:03:21 +00:00
|
|
|
return false;
|
2013-12-12 01:56:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 16:31:40 +00:00
|
|
|
|
2013-12-20 01:03:23 +00:00
|
|
|
int error_code;
|
|
|
|
zip = zip_open(QFile::encodeName(tempfile), ZIP_CREATE, &error_code);
|
2013-12-07 14:43:28 +00:00
|
|
|
if (!zip) {
|
2013-12-20 01:03:23 +00:00
|
|
|
char buffer[1024];
|
|
|
|
zip_error_to_str(buffer, sizeof buffer, error_code, errno);
|
|
|
|
*errorMsg = tr("failed to create zip file for upload: %1")
|
2014-02-28 04:09:57 +00:00
|
|
|
.arg(QString::fromLocal8Bit(buffer));
|
2013-12-20 01:03:21 +00:00
|
|
|
return false;
|
2013-12-07 14:43:28 +00:00
|
|
|
}
|
2013-12-05 16:31:40 +00:00
|
|
|
|
|
|
|
/* walk the dive list in chronological order */
|
2013-12-20 01:03:24 +00:00
|
|
|
for (int i = 0; i < dive_table.nr; i++) {
|
|
|
|
FILE *f;
|
|
|
|
char filename[PATH_MAX];
|
|
|
|
int streamsize;
|
|
|
|
char *membuf;
|
|
|
|
xmlDoc *transformed;
|
2013-12-20 01:03:25 +00:00
|
|
|
struct zip_source *s;
|
2013-12-20 01:03:24 +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...
|
|
|
|
*/
|
|
|
|
struct dive *dive = get_dive(i);
|
2013-12-05 16:31:40 +00:00
|
|
|
if (!dive)
|
|
|
|
continue;
|
|
|
|
if (selected && !dive->selected)
|
|
|
|
continue;
|
|
|
|
f = tmpfile();
|
2013-12-07 14:43:28 +00:00
|
|
|
if (!f) {
|
2013-12-20 01:03:23 +00:00
|
|
|
*errorMsg = tr("cannot create temporary file: %1").arg(qt_error_string());
|
2013-12-12 01:56:32 +00:00
|
|
|
goto error_close_zip;
|
2013-12-07 14:43:28 +00:00
|
|
|
}
|
2013-12-05 16:31:40 +00:00
|
|
|
save_dive(f, dive);
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
streamsize = ftell(f);
|
|
|
|
rewind(f);
|
2013-12-20 01:03:24 +00:00
|
|
|
|
2013-12-05 16:31:40 +00:00
|
|
|
membuf = (char *)malloc(streamsize + 1);
|
2014-03-06 02:30:52 +00:00
|
|
|
if (!membuf || (streamsize = fread(membuf, streamsize, 1, f)) == 0) {
|
2013-12-20 01:03:23 +00:00
|
|
|
*errorMsg = tr("internal error: %1").arg(qt_error_string());
|
2013-12-12 01:56:31 +00:00
|
|
|
fclose(f);
|
|
|
|
free((void *)membuf);
|
2013-12-12 01:56:32 +00:00
|
|
|
goto error_close_zip;
|
2013-12-07 14:43:28 +00:00
|
|
|
}
|
2013-12-05 16:31:40 +00:00
|
|
|
membuf[streamsize] = 0;
|
|
|
|
fclose(f);
|
2013-12-20 01:03:24 +00:00
|
|
|
|
2013-12-05 16:31:40 +00:00
|
|
|
/*
|
|
|
|
* Parse the memory buffer into XML document and
|
|
|
|
* transform it to divelogs.de format, finally dumping
|
|
|
|
* the XML into a character buffer.
|
|
|
|
*/
|
2013-12-12 01:56:33 +00:00
|
|
|
xmlDoc *doc = xmlReadMemory(membuf, streamsize, "divelog", NULL, 0);
|
2013-12-07 14:43:28 +00:00
|
|
|
if (!doc) {
|
2013-12-20 01:03:23 +00:00
|
|
|
qWarning() << errPrefix << "could not parse back into memory the XML file we've just created!";
|
|
|
|
*errorMsg = tr("internal error");
|
2013-12-12 01:56:31 +00:00
|
|
|
free((void *)membuf);
|
2013-12-12 01:56:32 +00:00
|
|
|
goto error_close_zip;
|
2013-12-07 14:43:28 +00:00
|
|
|
}
|
2013-12-05 16:31:40 +00:00
|
|
|
free((void *)membuf);
|
2013-12-20 01:03:24 +00:00
|
|
|
|
2013-12-05 16:31:40 +00:00
|
|
|
transformed = xsltApplyStylesheet(xslt, doc, NULL);
|
2014-02-28 04:09:57 +00:00
|
|
|
xmlDocDumpMemory(transformed, (xmlChar **)&membuf, &streamsize);
|
2013-12-05 16:31:40 +00:00
|
|
|
xmlFreeDoc(doc);
|
|
|
|
xmlFreeDoc(transformed);
|
2013-12-20 01:03:24 +00:00
|
|
|
|
2013-12-05 16:31:40 +00:00
|
|
|
/*
|
|
|
|
* Save the XML document into a zip file.
|
|
|
|
*/
|
|
|
|
snprintf(filename, PATH_MAX, "%d.xml", i + 1);
|
2013-12-20 01:03:25 +00:00
|
|
|
s = zip_source_buffer(zip, membuf, streamsize, 1);
|
|
|
|
if (s) {
|
|
|
|
int64_t ret = zip_add(zip, filename, s);
|
2013-12-05 16:31:40 +00:00
|
|
|
if (ret == -1)
|
2013-12-09 17:23:31 +00:00
|
|
|
qDebug() << errPrefix << "failed to include dive:" << i;
|
2013-12-05 16:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
zip_close(zip);
|
2013-12-12 01:56:30 +00:00
|
|
|
xsltFreeStylesheet(xslt);
|
2013-12-20 01:03:21 +00:00
|
|
|
return true;
|
2013-12-12 01:56:32 +00:00
|
|
|
|
|
|
|
error_close_zip:
|
|
|
|
zip_close(zip);
|
2013-12-20 01:03:21 +00:00
|
|
|
QFile::remove(tempfile);
|
2013-12-12 01:56:32 +00:00
|
|
|
xsltFreeStylesheet(xslt);
|
2013-12-20 01:03:21 +00:00
|
|
|
return false;
|
2013-12-05 16:31:40 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
WebServices::WebServices(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f), 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();
|
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-14 22:55:49 +00:00
|
|
|
QNetworkAccessManager *WebServices::manager()
|
|
|
|
{
|
|
|
|
static QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
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);
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.status->setText(tr("Transfering 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)
|
|
|
|
{
|
|
|
|
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)));
|
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-10-25 00:52:11 +00:00
|
|
|
// #
|
|
|
|
// #
|
|
|
|
// # Subsurface Web Service Implementation.
|
|
|
|
// #
|
|
|
|
// #
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
SubsurfaceWebServices::SubsurfaceWebServices(QWidget *parent, Qt::WindowFlags f) : WebServices(parent, f)
|
2013-10-03 18:54:25 +00:00
|
|
|
{
|
2013-06-06 14:57:12 +00:00
|
|
|
QSettings s;
|
2013-11-29 19:34:40 +00:00
|
|
|
ui.userID->setText(s.value("subsurface_webservice_uid").toString().toUpper());
|
2013-10-25 00:30:21 +00:00
|
|
|
hidePassword();
|
|
|
|
hideUpload();
|
2014-01-15 08:33:04 +00:00
|
|
|
ui.progressBar->setFormat("Enter User ID and click Download");
|
2014-02-28 04:09:57 +00:00
|
|
|
ui.progressBar->setRange(0, 1);
|
2014-01-15 08:33:04 +00:00
|
|
|
ui.progressBar->setValue(-1);
|
2013-06-06 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void SubsurfaceWebServices::buttonClicked(QAbstractButton *button)
|
2013-06-06 13:33:15 +00:00
|
|
|
{
|
2013-10-03 18:54:25 +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-11-14 22:39:17 +00:00
|
|
|
clear_table(&gps_location_table);
|
|
|
|
QByteArray url = tr("Webservice").toLocal8Bit();
|
|
|
|
parse_xml_buffer(url.data(), downloadedData.data(), downloadedData.length(), &gps_location_table, NULL, NULL);
|
|
|
|
|
|
|
|
/* now merge the data in the gps_location table into the dive_table */
|
|
|
|
if (merge_locations_into_dives()) {
|
2014-01-15 08:30:42 +00:00
|
|
|
mark_divelist_changed(true);
|
2014-02-12 14:22:54 +00:00
|
|
|
MainWindow::instance()->globe()->repopulateLabels();
|
|
|
|
MainWindow::instance()->globe()->centerOn(current_dive);
|
|
|
|
MainWindow::instance()->information()->updateDiveInfo(selected_dive);
|
2013-06-06 14:57:12 +00:00
|
|
|
}
|
2013-11-14 22:39:17 +00:00
|
|
|
|
|
|
|
/* store last entered uid in config */
|
|
|
|
QSettings s;
|
|
|
|
s.setValue("subsurface_webservice_uid", ui.userID->text().toUpper());
|
|
|
|
s.sync();
|
|
|
|
hide();
|
|
|
|
close();
|
2013-11-15 00:50:46 +00:00
|
|
|
resetState();
|
2014-02-28 04:09:57 +00:00
|
|
|
} break;
|
2013-11-14 22:39:17 +00:00
|
|
|
case QDialogButtonBox::RejectRole:
|
2013-12-09 15:42:06 +00:00
|
|
|
if (reply != NULL && reply->isOpen()) {
|
|
|
|
reply->abort();
|
|
|
|
delete reply;
|
|
|
|
reply = NULL;
|
|
|
|
}
|
2013-11-15 00:50:46 +00:00
|
|
|
resetState();
|
2013-11-14 22:39:17 +00:00
|
|
|
break;
|
|
|
|
case QDialogButtonBox::HelpRole:
|
|
|
|
QDesktopServices::openUrl(QUrl("http://api.hohndel.org"));
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-06 14:57:12 +00:00
|
|
|
break;
|
2013-06-06 14:31:55 +00:00
|
|
|
}
|
2013-06-06 13:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SubsurfaceWebServices::startDownload()
|
|
|
|
{
|
|
|
|
QUrl url("http://api.hohndel.org/api/dive/get/");
|
2014-02-28 04:09:57 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2013-11-15 01:47:30 +00:00
|
|
|
url.addQueryItem("login", ui.userID->text().toUpper());
|
2014-01-15 08:30:34 +00:00
|
|
|
#else
|
|
|
|
QUrlQuery query;
|
|
|
|
query.addQueryItem("login", ui.userID->text().toUpper());
|
|
|
|
url.setQuery(query);
|
|
|
|
#endif
|
2013-06-06 14:31:55 +00:00
|
|
|
|
2013-06-06 13:33:15 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(url);
|
|
|
|
request.setRawHeader("Accept", "text/xml");
|
2013-11-14 22:55:49 +00:00
|
|
|
reply = manager()->get(request);
|
2013-11-14 23:05:24 +00:00
|
|
|
ui.status->setText(tr("Connecting..."));
|
2014-01-15 08:33:04 +00:00
|
|
|
ui.progressBar->setEnabled(true);
|
2014-02-28 04:09:57 +00:00
|
|
|
ui.progressBar->setRange(0, 0); // this makes the progressbar do an 'infinite spin'
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.download->setEnabled(false);
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
|
2013-11-15 00:52:08 +00:00
|
|
|
connectSignalsForDownload(reply);
|
2013-06-06 13:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SubsurfaceWebServices::downloadFinished()
|
|
|
|
{
|
2013-11-15 00:52:08 +00:00
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
ui.progressBar->setRange(0, 1);
|
2014-01-15 08:33:04 +00:00
|
|
|
ui.progressBar->setValue(1);
|
|
|
|
ui.progressBar->setFormat("%p%");
|
2013-06-06 14:31:55 +00:00
|
|
|
downloadedData = reply->readAll();
|
|
|
|
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.download->setEnabled(true);
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.status->setText(tr("Download finished"));
|
2013-06-06 14:31:55 +00:00
|
|
|
|
|
|
|
uint resultCode = download_dialog_parse_response(downloadedData);
|
|
|
|
setStatusText(resultCode);
|
2014-01-16 04:50:56 +00:00
|
|
|
if (resultCode == DD_STATUS_OK) {
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
|
2013-06-06 14:31:55 +00:00
|
|
|
}
|
|
|
|
reply->deleteLater();
|
2013-11-15 00:52:08 +00:00
|
|
|
reply = NULL;
|
2013-06-06 13:33:15 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 01:47:30 +00:00
|
|
|
void SubsurfaceWebServices::downloadError(QNetworkReply::NetworkError)
|
2013-06-06 13:33:15 +00:00
|
|
|
{
|
2013-11-15 00:50:46 +00:00
|
|
|
resetState();
|
|
|
|
ui.status->setText(tr("Download error: %1").arg(reply->errorString()));
|
2013-06-06 14:31:55 +00:00
|
|
|
reply->deleteLater();
|
2013-11-15 00:52:08 +00:00
|
|
|
reply = NULL;
|
2013-06-06 13:33:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 14:31:55 +00:00
|
|
|
void SubsurfaceWebServices::setStatusText(int status)
|
|
|
|
{
|
|
|
|
QString text;
|
2014-01-16 04:50:56 +00:00
|
|
|
switch (status) {
|
2014-02-28 04:09:57 +00:00
|
|
|
case DD_STATUS_ERROR_CONNECT:
|
|
|
|
text = tr("Connection Error: ");
|
|
|
|
break;
|
|
|
|
case DD_STATUS_ERROR_ID:
|
|
|
|
text = tr("Invalid user identifier!");
|
|
|
|
break;
|
|
|
|
case DD_STATUS_ERROR_PARSE:
|
|
|
|
text = tr("Cannot parse response!");
|
|
|
|
break;
|
|
|
|
case DD_STATUS_OK:
|
|
|
|
text = tr("Download Success!");
|
|
|
|
break;
|
2013-06-06 14:31:55 +00:00
|
|
|
}
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.status->setText(text);
|
2013-06-06 14:31:55 +00:00
|
|
|
}
|
2013-06-06 13:33:15 +00:00
|
|
|
|
2013-06-06 14:31:55 +00:00
|
|
|
/* requires that there is a <download> or <error> tag under the <root> tag */
|
|
|
|
void SubsurfaceWebServices::download_dialog_traverse_xml(xmlNodePtr node, unsigned int *download_status)
|
|
|
|
{
|
|
|
|
xmlNodePtr cur_node;
|
|
|
|
for (cur_node = node; cur_node; cur_node = cur_node->next) {
|
|
|
|
if ((!strcmp((const char *)cur_node->name, (const char *)"download")) &&
|
2014-02-28 04:09:57 +00:00
|
|
|
(!strcmp((const char *)xmlNodeGetContent(cur_node), (const char *)"ok"))) {
|
2013-06-06 14:31:55 +00:00
|
|
|
*download_status = DD_STATUS_OK;
|
|
|
|
return;
|
2014-02-28 04:09:57 +00:00
|
|
|
} else if (!strcmp((const char *)cur_node->name, (const char *)"error")) {
|
2013-06-06 14:31:55 +00:00
|
|
|
*download_status = DD_STATUS_ERROR_ID;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
unsigned int SubsurfaceWebServices::download_dialog_parse_response(const QByteArray &xml)
|
2013-06-06 14:31:55 +00:00
|
|
|
{
|
|
|
|
xmlNodePtr root;
|
|
|
|
xmlDocPtr doc = xmlParseMemory(xml.data(), xml.length());
|
|
|
|
unsigned int status = DD_STATUS_ERROR_PARSE;
|
|
|
|
|
|
|
|
if (!doc)
|
|
|
|
return DD_STATUS_ERROR_PARSE;
|
|
|
|
root = xmlDocGetRootElement(doc);
|
|
|
|
if (!root) {
|
|
|
|
status = DD_STATUS_ERROR_PARSE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (root->children)
|
|
|
|
download_dialog_traverse_xml(root->children, &status);
|
2013-11-14 22:39:17 +00:00
|
|
|
end:
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
return status;
|
2013-06-06 14:31:55 +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);
|
|
|
|
const QString invalidXmlError = DivelogsDeWebServices::tr("Invalid response from server");
|
|
|
|
bool seenDiveDates = false;
|
|
|
|
DiveListResult result;
|
|
|
|
result.idCount = 0;
|
|
|
|
|
|
|
|
if (reader.readNextStartElement() && reader.name() != "DiveDateReader") {
|
|
|
|
result.errorCondition = invalidXmlError;
|
|
|
|
result.errorDetails =
|
2014-02-28 04:09:57 +00:00
|
|
|
DivelogsDeWebServices::tr("Expected XML tag 'DiveDateReader', got instead '%1")
|
|
|
|
.arg(reader.name().toString());
|
2013-12-07 01:09:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (reader.readNextStartElement()) {
|
|
|
|
if (reader.name() != "DiveDates") {
|
|
|
|
if (reader.name() == "Login") {
|
|
|
|
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()) {
|
|
|
|
if (reader.name() != "date") {
|
|
|
|
// qDebug() << "Skipping" << reader.name();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QStringRef id = reader.attributes().value("divelogsId");
|
|
|
|
// 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;
|
|
|
|
result.errorDetails = DivelogsDeWebServices::tr("Expected XML tag 'DiveDates' not found");
|
|
|
|
}
|
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;
|
|
|
|
result.errorDetails = DivelogsDeWebServices::tr("Malformed XML response. Line %1: %2")
|
2014-02-28 04:09:57 +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
|
|
|
self->setAttribute(Qt::WA_QuitOnClose, false);
|
|
|
|
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
|
|
|
|
2013-12-05 16:31:40 +00:00
|
|
|
void DivelogsDeWebServices::prepareDivesForUpload()
|
|
|
|
{
|
2013-12-20 01:03:23 +00:00
|
|
|
QString errorText;
|
2013-12-20 01:03:21 +00:00
|
|
|
|
|
|
|
/* generate a random filename and create/open that file with zip_open */
|
|
|
|
QString filename = QDir::tempPath() + "/import-" + QString::number(qrand() % 99999999) + ".dld";
|
2013-12-20 01:03:23 +00:00
|
|
|
if (prepare_dives_for_divelogs(filename, true, &errorText)) {
|
2013-12-07 14:43:28 +00:00
|
|
|
QFile f(filename);
|
2013-12-12 01:56:35 +00:00
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
2013-12-07 14:43:28 +00:00
|
|
|
uploadDives((QIODevice *)&f);
|
|
|
|
f.close();
|
|
|
|
f.remove();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-05 16:31:40 +00:00
|
|
|
}
|
2014-02-12 14:22:54 +00:00
|
|
|
MainWindow::instance()->showError(errorText);
|
2013-12-05 16:31:40 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
void DivelogsDeWebServices::uploadDives(QIODevice *dldContent)
|
|
|
|
{
|
|
|
|
QHttpMultiPart mp(QHttpMultiPart::FormDataType);
|
|
|
|
QHttpPart part;
|
2013-12-09 13:29:57 +00:00
|
|
|
QFile *f = (QFile *)dldContent;
|
|
|
|
QFileInfo fi(*f);
|
|
|
|
QString args("form-data; name=\"userfile\"; filename=\"" + fi.absoluteFilePath() + "\"");
|
|
|
|
part.setRawHeader("Content-Disposition", args.toLatin1());
|
2013-11-15 02:57:09 +00:00
|
|
|
part.setBodyDevice(dldContent);
|
|
|
|
mp.append(part);
|
|
|
|
|
|
|
|
multipart = ∓
|
|
|
|
hideDownload();
|
2013-12-09 15:42:06 +00:00
|
|
|
resetState();
|
2013-12-09 17:23:32 +00:00
|
|
|
uploadMode = true;
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(true);
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
|
|
|
|
ui.buttonBox->button(QDialogButtonBox::Apply)->setText(tr("Done"));
|
2013-11-15 02:57:09 +00:00
|
|
|
exec();
|
|
|
|
|
2013-12-09 15:42:06 +00:00
|
|
|
multipart = NULL;
|
|
|
|
if (reply != NULL && reply->isOpen()) {
|
|
|
|
reply->abort();
|
|
|
|
delete reply;
|
|
|
|
reply = NULL;
|
|
|
|
}
|
2013-11-15 02:57:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
DivelogsDeWebServices::DivelogsDeWebServices(QWidget *parent, Qt::WindowFlags f) : WebServices(parent, f), uploadMode(false)
|
2013-11-15 02:57:09 +00:00
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
ui.userID->setText(s.value("divelogde_user").toString());
|
|
|
|
ui.password->setText(s.value("divelogde_pass").toString());
|
|
|
|
hideUpload();
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::startUpload()
|
|
|
|
{
|
2013-12-09 14:45:54 +00:00
|
|
|
QSettings s;
|
|
|
|
s.setValue("divelogde_user", ui.userID->text());
|
|
|
|
s.setValue("divelogde_pass", ui.password->text());
|
|
|
|
s.sync();
|
|
|
|
|
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
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(QUrl("https://divelogs.de/DivelogsDirectImport.php"));
|
|
|
|
request.setRawHeader("Accept", "text/xml, application/xml");
|
|
|
|
|
|
|
|
QHttpPart part;
|
|
|
|
part.setRawHeader("Content-Disposition", "form-data; name=\"user\"");
|
|
|
|
part.setBody(ui.userID->text().toUtf8());
|
|
|
|
multipart->append(part);
|
|
|
|
|
|
|
|
part.setRawHeader("Content-Disposition", "form-data; name=\"pass\"");
|
|
|
|
part.setBody(ui.password->text().toUtf8());
|
|
|
|
multipart->append(part);
|
|
|
|
|
|
|
|
reply = manager()->post(request, multipart);
|
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(uploadFinished()));
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
|
|
|
|
SLOT(uploadError(QNetworkReply::NetworkError)));
|
2014-02-28 04:09:57 +00:00
|
|
|
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this,
|
|
|
|
SLOT(updateProgress(qint64, qint64)));
|
2013-11-15 02:57:09 +00:00
|
|
|
|
|
|
|
timeout.start(30000); // 30s
|
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");
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2013-11-15 02:57:09 +00:00
|
|
|
QUrl body;
|
|
|
|
body.addQueryItem("user", ui.userID->text());
|
|
|
|
body.addQueryItem("pass", ui.password->text());
|
|
|
|
|
|
|
|
reply = manager()->post(request, body.encodedQuery());
|
|
|
|
#else
|
|
|
|
QUrlQuery body;
|
|
|
|
body.addQueryItem("user", ui.userID->text());
|
|
|
|
body.addQueryItem("pass", ui.password->text());
|
|
|
|
|
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
|
|
|
#endif
|
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(listDownloadFinished()));
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
this, SLOT(downloadError(QNetworkReply::NetworkError)));
|
|
|
|
|
|
|
|
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, */*");
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2013-11-15 02:57:09 +00:00
|
|
|
QUrl body;
|
|
|
|
body.addQueryItem("user", ui.userID->text());
|
|
|
|
body.addQueryItem("pass", ui.password->text());
|
|
|
|
body.addQueryItem("ids", diveList.idList);
|
|
|
|
|
|
|
|
reply = manager()->post(request, body.encodedQuery());
|
|
|
|
#else
|
|
|
|
QUrlQuery body;
|
|
|
|
body.addQueryItem("user", ui.userID->text());
|
|
|
|
body.addQueryItem("pass", ui.password->text());
|
|
|
|
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
|
|
|
#endif
|
|
|
|
|
|
|
|
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());
|
|
|
|
struct zip *zip = zip_fdopen(duppedfd, 0, &errorcode);
|
|
|
|
if (!zip)
|
|
|
|
::close(duppedfd);
|
|
|
|
#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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::uploadFinished()
|
|
|
|
{
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
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"));
|
2013-11-15 02:57:09 +00:00
|
|
|
ui.status->setText(tr("Upload finished"));
|
|
|
|
|
|
|
|
// check what the server sent us: it might contain
|
|
|
|
// an error condition, such as a failed login
|
|
|
|
QByteArray xmlData = reply->readAll();
|
2013-12-09 15:42:06 +00:00
|
|
|
reply->deleteLater();
|
|
|
|
reply = NULL;
|
2013-12-07 12:56:03 +00:00
|
|
|
char *resp = xmlData.data();
|
|
|
|
if (resp) {
|
|
|
|
char *parsed = strstr(resp, "<Login>");
|
|
|
|
if (parsed) {
|
2013-12-07 15:10:58 +00:00
|
|
|
if (strstr(resp, "<Login>succeeded</Login>")) {
|
|
|
|
if (strstr(resp, "<FileCopy>failed</FileCopy>")) {
|
|
|
|
ui.status->setText(tr("Upload failed"));
|
|
|
|
return;
|
|
|
|
}
|
2013-12-07 12:56:03 +00:00
|
|
|
ui.status->setText(tr("Upload successful"));
|
2013-12-07 15:10:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ui.status->setText(tr("Login failed"));
|
|
|
|
return;
|
2013-12-05 16:31:40 +00:00
|
|
|
}
|
2013-12-07 15:10:58 +00:00
|
|
|
ui.status->setText(tr("Cannot parse response"));
|
2013-12-05 16:31:40 +00:00
|
|
|
}
|
2013-10-25 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivelogsDeWebServices::setStatusText(int status)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-11-15 02:57:09 +00:00
|
|
|
void DivelogsDeWebServices::uploadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
|
|
|
downloadError(error);
|
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 */
|
|
|
|
char *error = NULL;
|
2013-12-20 01:02:34 +00:00
|
|
|
parse_file(QFile::encodeName(zipFile.fileName()), &error);
|
2013-12-07 12:33:43 +00:00
|
|
|
if (error != NULL) {
|
2014-02-12 14:22:54 +00:00
|
|
|
MainWindow::instance()->showError(error);
|
2013-12-07 12:33:43 +00:00
|
|
|
free(error);
|
|
|
|
}
|
2014-01-15 08:30:42 +00:00
|
|
|
process_dives(true, false);
|
2014-02-12 14:22:54 +00:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
2013-12-07 01:10:32 +00:00
|
|
|
|
2013-12-07 12:33:43 +00:00
|
|
|
/* store last entered user/pass in config */
|
|
|
|
QSettings s;
|
|
|
|
s.setValue("divelogde_user", ui.userID->text());
|
|
|
|
s.setValue("divelogde_pass", ui.password->text());
|
|
|
|
s.sync();
|
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
|
|
|
}
|